}
void cx_strfree_a(
- CxAllocator *alloc,
+ CxAllocator const *alloc,
cxmutstr *str
) {
cxFree(alloc, str->ptr);
return size;
}
-cxmutstr cx_strcat_a(
- CxAllocator *alloc,
+cxmutstr cx_strcat_ma(
+ CxAllocator const *alloc,
+ cxmutstr str,
size_t count,
...
) {
+ if (count == 0) return str;
+
cxstring *strings = calloc(count, sizeof(cxstring));
if (!strings) abort();
va_start(ap, count);
// get all args and overall length
- size_t slen = 0;
+ size_t slen = str.length;
cx_for_n(i, count) {
cxstring s = va_arg (ap, cxstring);
strings[i] = s;
slen += s.length;
}
+ va_end(ap);
- // create new string
- cxmutstr result;
- result.ptr = cxMalloc(alloc, slen + 1);
- result.length = slen;
- if (result.ptr == NULL) abort();
+ // reallocate or create new string
+ if (str.ptr == NULL) {
+ str.ptr = cxMalloc(alloc, slen + 1);
+ } else {
+ str.ptr = cxRealloc(alloc, str.ptr, slen + 1);
+ }
+ if (str.ptr == NULL) abort();
// concatenate strings
- size_t pos = 0;
+ size_t pos = str.length;
+ str.length = slen;
cx_for_n(i, count) {
cxstring s = strings[i];
- memcpy(result.ptr + pos, s.ptr, s.length);
+ memcpy(str.ptr + pos, s.ptr, s.length);
pos += s.length;
}
// terminate string
- result.ptr[result.length] = '\0';
+ str.ptr[str.length] = '\0';
// free temporary array
free(strings);
- return result;
+ return str;
}
cxstring cx_strsubs(
}
size_t cx_strsplit_a(
- CxAllocator *allocator,
+ CxAllocator const *allocator,
cxstring string,
cxstring delim,
size_t limit,
}
size_t cx_strsplit_ma(
- CxAllocator *allocator,
+ CxAllocator const *allocator,
cxmutstr string,
cxstring delim,
size_t limit,
}
cxmutstr cx_strdup_a(
- CxAllocator *allocator,
+ CxAllocator const *allocator,
cxstring string
) {
cxmutstr result = {
}
cxmutstr cx_strreplacen_a(
- CxAllocator *allocator,
+ CxAllocator const *allocator,
cxstring str,
cxstring pattern,
cxstring replacement,