@@ -173,5 +173,141 @@ RT_API_ATTRS void ShallowCopy(const Descriptor , const
Descriptor ) {
ShallowCopy(to, from, to.IsContiguous(), from.IsContiguous());
}
+RT_API_ATTRS const char *EnsureNullTerminated(
+const char *str, size_t length, 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 ) {
+ 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::strlen(string));
+}
+
+// Assumes Descriptor \p value is not nullptr.
+RT_API_ATTRS bool IsValidCharDescriptor(const Descriptor *value) {
+ return value && value->IsAllocated() &&
+ value->type() == TypeCode(TypeCategory::Character, 1) &&
+ value->rank() == 0;
+}
+
+// Assumes Descriptor \p intVal is not nullptr.
+RT_API_ATTRS bool IsValidIntDescriptor(const Descriptor *intVal) {
+ auto typeCode{intVal->type().GetCategoryAndKind()};
+ // Check that our descriptor is allocated and is a scalar integer with
+ // kind != 1 (i.e. with a large enough decimal exponent range).
+ return intVal->IsAllocated() && intVal->rank() == 0 &&
+ intVal->type().IsInteger() && typeCode && typeCode->second != 1;
+}
+
+// Assume Descriptor \p value is valid: pass IsValidCharDescriptor check.
+RT_API_ATTRS void FillWithSpaces(const Descriptor , std::size_t offset) {
klausler wrote:
What if `offset` is larger than `ElementBytes()`?
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