diff -r 545010bc5e71 -r 22eca559aded src/ucx/string.c --- a/src/ucx/string.c Sun Nov 20 12:43:44 2022 +0100 +++ b/src/ucx/string.c Sat Nov 26 17:07:08 2022 +0100 @@ -35,9 +35,9 @@ #ifndef _WIN32 -#include /* for strncasecmp() */ +#include // for strncasecmp() -#endif /* _WIN32 */ +#endif // _WIN32 cxmutstr cx_mutstr(char *cstring) { return (cxmutstr) {cstring, strlen(cstring)}; @@ -236,7 +236,7 @@ return haystack; } - /* optimize for single-char needles */ + // optimize for single-char needles if (needle.length == 1) { return cx_strchr(haystack, *needle.ptr); } @@ -249,19 +249,19 @@ * and we want to avoid that. */ - /* local prefix table */ + // local prefix table size_t s_prefix_table[STRSTR_SBO_BUFLEN]; - /* check needle length and use appropriate prefix table */ - /* if the pattern exceeds static prefix table, allocate on the heap */ + // check needle length and use appropriate prefix table + // if the pattern exceeds static prefix table, allocate on the heap bool useheap = needle.length >= STRSTR_SBO_BUFLEN; register size_t *ptable = useheap ? calloc(needle.length + 1, sizeof(size_t)) : s_prefix_table; - /* keep counter in registers */ + // keep counter in registers register size_t i, j; - /* fill prefix table */ + // fill prefix table i = 0; j = 0; ptable[i] = j; @@ -274,7 +274,7 @@ ptable[i] = j; } - /* search */ + // search cxstring result = {NULL, 0}; i = 0; j = 1; @@ -292,7 +292,7 @@ } } - /* if prefix table was allocated on the heap, free it */ + // if prefix table was allocated on the heap, free it if (ptable != s_prefix_table) { free(ptable); } @@ -314,23 +314,24 @@ size_t limit, cxstring *output ) { - /* special case: output limit is zero */ + // special case: output limit is zero if (limit == 0) return 0; - /* special case: delimiter is empty */ + // special case: delimiter is empty if (delim.length == 0) { output[0] = string; return 1; } - /* special cases: delimiter is at least as large as the string */ + // special cases: delimiter is at least as large as the string if (delim.length >= string.length) { - /* exact match */ + // exact match if (cx_strcmp(string, delim) == 0) { output[0] = cx_strn(string.ptr, 0); output[1] = cx_strn(string.ptr + string.length, 0); return 2; - } else /* no match possible */ { + } else { + // no match possible output[0] = string; return 1; } @@ -342,21 +343,21 @@ ++n; cxstring match = cx_strstr(curpos, delim); if (match.length > 0) { - /* is the limit reached? */ + // is the limit reached? if (n < limit) { - /* copy the current string to the array */ + // copy the current string to the array cxstring item = cx_strn(curpos.ptr, match.ptr - curpos.ptr); output[n - 1] = item; size_t processed = item.length + delim.length; curpos.ptr += processed; curpos.length -= processed; } else { - /* limit reached, copy the _full_ remaining string */ + // limit reached, copy the _full_ remaining string output[n - 1] = curpos; break; } } else { - /* no more matches, copy last string */ + // no more matches, copy last string output[n - 1] = curpos; break; } @@ -372,24 +373,24 @@ size_t limit, cxstring **output ) { - /* find out how many splits we're going to make and allocate memory */ + // find out how many splits we're going to make and allocate memory size_t n = 0; cxstring curpos = string; while (1) { ++n; cxstring match = cx_strstr(curpos, delim); if (match.length > 0) { - /* is the limit reached? */ + // is the limit reached? if (n < limit) { size_t processed = match.ptr - curpos.ptr + delim.length; curpos.ptr += processed; curpos.length -= processed; } else { - /* limit reached */ + // limit reached break; } } else { - /* no more matches */ + // no more matches break; } } @@ -566,14 +567,14 @@ if (pattern.length == 0 || pattern.length > str.length || replmax == 0) return cx_strdup_a(allocator, str); - /* Compute expected buffer length */ + // Compute expected buffer length size_t ibufmax = str.length / pattern.length; size_t ibuflen = replmax < ibufmax ? replmax : ibufmax; if (ibuflen > REPLACE_INDEX_BUFFER_MAX) { ibuflen = REPLACE_INDEX_BUFFER_MAX; } - /* Allocate first index buffer */ + // Allocate first index buffer struct cx_strreplace_ibuf *firstbuf, *curbuf; firstbuf = curbuf = calloc(1, sizeof(struct cx_strreplace_ibuf)); if (!firstbuf) return cx_mutstrn(NULL, 0); @@ -583,13 +584,13 @@ return cx_mutstrn(NULL, 0); } - /* Search occurrences */ + // Search occurrences cxstring searchstr = str; size_t found = 0; do { cxstring match = cx_strstr(searchstr, pattern); if (match.length > 0) { - /* Allocate next buffer in chain, if required */ + // Allocate next buffer in chain, if required if (curbuf->len == ibuflen) { struct cx_strreplace_ibuf *nextbuf = calloc(1, sizeof(struct cx_strreplace_ibuf)); @@ -607,7 +608,7 @@ curbuf = nextbuf; } - /* Record match index */ + // Record match index found++; size_t idx = match.ptr - str.ptr; curbuf->buf[curbuf->len++] = idx; @@ -618,7 +619,7 @@ } } while (searchstr.length > 0 && found < replmax); - /* Allocate result string */ + // Allocate result string cxmutstr result; { ssize_t adjlen = (ssize_t) replacement.length - (ssize_t) pattern.length; @@ -636,13 +637,13 @@ } } - /* Build result string */ + // Build result string curbuf = firstbuf; size_t srcidx = 0; char *destptr = result.ptr; do { for (size_t i = 0; i < curbuf->len; i++) { - /* Copy source part up to next match*/ + // Copy source part up to next match size_t idx = curbuf->buf[i]; size_t srclen = idx - srcidx; if (srclen > 0) { @@ -651,7 +652,7 @@ srcidx += srclen; } - /* Copy the replacement and skip the source pattern */ + // Copy the replacement and skip the source pattern srcidx += pattern.length; memcpy(destptr, replacement.ptr, replacement.length); destptr += replacement.length; @@ -660,10 +661,10 @@ } while (curbuf); memcpy(destptr, str.ptr + srcidx, str.length - srcidx); - /* Result is guaranteed to be zero-terminated */ + // Result is guaranteed to be zero-terminated result.ptr[result.length] = '\0'; - /* Free index buffer */ + // Free index buffer cx_strrepl_free_ibuf(firstbuf); return result;