src/string.c

changeset 1667
608cc0b25352
parent 1652
db8299984bfe
child 1668
3ffdfe1776b4
--- a/src/string.c	Wed Dec 24 12:13:59 2025 +0100
+++ b/src/string.c	Thu Dec 25 11:10:13 2025 +0100
@@ -99,13 +99,13 @@
     return size;
 }
 
-cxmutstr cx_strcat_ma(
+int cx_strcat_a(
         const CxAllocator *alloc,
-        cxmutstr str,
+        cxmutstr *str,
         size_t count,
         ...
 ) {
-    if (count == 0) return str;
+    if (count == 0) return 0;
     va_list ap;
     va_start(ap, count);
     va_list ap2;
@@ -113,7 +113,7 @@
 
     // compute overall length
     bool overflow = false;
-    size_t slen = str.length;
+    size_t slen = str->length;
     for (size_t i = 0; i < count; i++) {
         cxstring s = va_arg(ap, cxstring);
         if (slen > SIZE_MAX - s.length) overflow = true;
@@ -125,36 +125,31 @@
     if (overflow) {
         va_end(ap2);
         errno = EOVERFLOW;
-        return (cxmutstr) { NULL, 0 };
+        return -1;
     }
 
     // reallocate or create new string
-    char *newstr;
-    if (str.ptr == NULL) {
-        newstr = cxMalloc(alloc, slen + 1);
-    } else {
-        newstr = cxRealloc(alloc, str.ptr, slen + 1);
+    if (cxReallocate(alloc, &str->ptr, slen + 1)) {
+        // LCOV_EXCL_START
+        va_end(ap2);
+        return -1;
+        // LCOV_EXCL_STOP
     }
-    if (newstr == NULL) { // LCOV_EXCL_START
-        va_end(ap2);
-        return (cxmutstr) {NULL, 0};
-    } // LCOV_EXCL_STOP
-    str.ptr = newstr;
 
     // concatenate strings
-    size_t pos = str.length;
-    str.length = slen;
+    size_t pos = str->length;
+    str->length = slen;
     for (size_t i = 0; i < count; i++) {
         cxstring s = va_arg(ap2, cxstring);
-        memcpy(str.ptr + pos, s.ptr, s.length);
+        memcpy(str->ptr + pos, s.ptr, s.length);
         pos += s.length;
     }
     va_end(ap2);
 
     // terminate string
-    str.ptr[str.length] = '\0';
+    str->ptr[str->length] = '\0';
 
-    return str;
+    return 0;
 }
 
 cxstring cx_strsubs(

mercurial