================ @@ -173,5 +173,141 @@ RT_API_ATTRS void ShallowCopy(const Descriptor &to, const Descriptor &from) { ShallowCopy(to, from, to.IsContiguous(), from.IsContiguous()); } +RT_API_ATTRS const char *EnsureNullTerminated( + const char *str, size_t length, Terminator &terminator) { + if (length <= std::strlen(str)) { + char *newCmd{(char *)AllocateMemoryOrCrash(terminator, length + 1)}; + std::memcpy(newCmd, str, length); + newCmd[length] = '\0'; + return newCmd; + } else { + return str; + } +} + +RT_API_ATTRS std::size_t LengthWithoutTrailingSpaces(const Descriptor &d) { + std::size_t s{d.ElementBytes() - 1}; + while (*d.OffsetElement(s) == ' ') { + --s; + } + return s + 1; +} + +// Returns the length of the \p string. Assumes \p string is valid. +RT_API_ATTRS std::int64_t StringLength(const char *string) { + return static_cast<std::int64_t>(std::strlen(string)); +} + +// Assumes Descriptor \p value is not nullptr. ---------------- klausler wrote:
The comment is incorrect; the code works fine if `value` is null. https://github.com/llvm/llvm-project/pull/74077 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits