1.1 --- a/src/string.c Sun Nov 20 17:48:42 2022 +0100 1.2 +++ b/src/string.c Sun Nov 20 21:08:36 2022 +0100 1.3 @@ -35,9 +35,9 @@ 1.4 1.5 #ifndef _WIN32 1.6 1.7 -#include <strings.h> /* for strncasecmp() */ 1.8 +#include <strings.h> // for strncasecmp() 1.9 1.10 -#endif /* _WIN32 */ 1.11 +#endif // _WIN32 1.12 1.13 cxmutstr cx_mutstr(char *cstring) { 1.14 return (cxmutstr) {cstring, strlen(cstring)}; 1.15 @@ -236,7 +236,7 @@ 1.16 return haystack; 1.17 } 1.18 1.19 - /* optimize for single-char needles */ 1.20 + // optimize for single-char needles 1.21 if (needle.length == 1) { 1.22 return cx_strchr(haystack, *needle.ptr); 1.23 } 1.24 @@ -249,19 +249,19 @@ 1.25 * and we want to avoid that. 1.26 */ 1.27 1.28 - /* local prefix table */ 1.29 + // local prefix table 1.30 size_t s_prefix_table[STRSTR_SBO_BUFLEN]; 1.31 1.32 - /* check needle length and use appropriate prefix table */ 1.33 - /* if the pattern exceeds static prefix table, allocate on the heap */ 1.34 + // check needle length and use appropriate prefix table 1.35 + // if the pattern exceeds static prefix table, allocate on the heap 1.36 bool useheap = needle.length >= STRSTR_SBO_BUFLEN; 1.37 register size_t *ptable = useheap ? calloc(needle.length + 1, 1.38 sizeof(size_t)) : s_prefix_table; 1.39 1.40 - /* keep counter in registers */ 1.41 + // keep counter in registers 1.42 register size_t i, j; 1.43 1.44 - /* fill prefix table */ 1.45 + // fill prefix table 1.46 i = 0; 1.47 j = 0; 1.48 ptable[i] = j; 1.49 @@ -274,7 +274,7 @@ 1.50 ptable[i] = j; 1.51 } 1.52 1.53 - /* search */ 1.54 + // search 1.55 cxstring result = {NULL, 0}; 1.56 i = 0; 1.57 j = 1; 1.58 @@ -292,7 +292,7 @@ 1.59 } 1.60 } 1.61 1.62 - /* if prefix table was allocated on the heap, free it */ 1.63 + // if prefix table was allocated on the heap, free it 1.64 if (ptable != s_prefix_table) { 1.65 free(ptable); 1.66 } 1.67 @@ -314,23 +314,24 @@ 1.68 size_t limit, 1.69 cxstring *output 1.70 ) { 1.71 - /* special case: output limit is zero */ 1.72 + // special case: output limit is zero 1.73 if (limit == 0) return 0; 1.74 1.75 - /* special case: delimiter is empty */ 1.76 + // special case: delimiter is empty 1.77 if (delim.length == 0) { 1.78 output[0] = string; 1.79 return 1; 1.80 } 1.81 1.82 - /* special cases: delimiter is at least as large as the string */ 1.83 + // special cases: delimiter is at least as large as the string 1.84 if (delim.length >= string.length) { 1.85 - /* exact match */ 1.86 + // exact match 1.87 if (cx_strcmp(string, delim) == 0) { 1.88 output[0] = cx_strn(string.ptr, 0); 1.89 output[1] = cx_strn(string.ptr + string.length, 0); 1.90 return 2; 1.91 - } else /* no match possible */ { 1.92 + } else { 1.93 + // no match possible 1.94 output[0] = string; 1.95 return 1; 1.96 } 1.97 @@ -342,21 +343,21 @@ 1.98 ++n; 1.99 cxstring match = cx_strstr(curpos, delim); 1.100 if (match.length > 0) { 1.101 - /* is the limit reached? */ 1.102 + // is the limit reached? 1.103 if (n < limit) { 1.104 - /* copy the current string to the array */ 1.105 + // copy the current string to the array 1.106 cxstring item = cx_strn(curpos.ptr, match.ptr - curpos.ptr); 1.107 output[n - 1] = item; 1.108 size_t processed = item.length + delim.length; 1.109 curpos.ptr += processed; 1.110 curpos.length -= processed; 1.111 } else { 1.112 - /* limit reached, copy the _full_ remaining string */ 1.113 + // limit reached, copy the _full_ remaining string 1.114 output[n - 1] = curpos; 1.115 break; 1.116 } 1.117 } else { 1.118 - /* no more matches, copy last string */ 1.119 + // no more matches, copy last string 1.120 output[n - 1] = curpos; 1.121 break; 1.122 } 1.123 @@ -372,24 +373,24 @@ 1.124 size_t limit, 1.125 cxstring **output 1.126 ) { 1.127 - /* find out how many splits we're going to make and allocate memory */ 1.128 + // find out how many splits we're going to make and allocate memory 1.129 size_t n = 0; 1.130 cxstring curpos = string; 1.131 while (1) { 1.132 ++n; 1.133 cxstring match = cx_strstr(curpos, delim); 1.134 if (match.length > 0) { 1.135 - /* is the limit reached? */ 1.136 + // is the limit reached? 1.137 if (n < limit) { 1.138 size_t processed = match.ptr - curpos.ptr + delim.length; 1.139 curpos.ptr += processed; 1.140 curpos.length -= processed; 1.141 } else { 1.142 - /* limit reached */ 1.143 + // limit reached 1.144 break; 1.145 } 1.146 } else { 1.147 - /* no more matches */ 1.148 + // no more matches 1.149 break; 1.150 } 1.151 } 1.152 @@ -566,14 +567,14 @@ 1.153 if (pattern.length == 0 || pattern.length > str.length || replmax == 0) 1.154 return cx_strdup_a(allocator, str); 1.155 1.156 - /* Compute expected buffer length */ 1.157 + // Compute expected buffer length 1.158 size_t ibufmax = str.length / pattern.length; 1.159 size_t ibuflen = replmax < ibufmax ? replmax : ibufmax; 1.160 if (ibuflen > REPLACE_INDEX_BUFFER_MAX) { 1.161 ibuflen = REPLACE_INDEX_BUFFER_MAX; 1.162 } 1.163 1.164 - /* Allocate first index buffer */ 1.165 + // Allocate first index buffer 1.166 struct cx_strreplace_ibuf *firstbuf, *curbuf; 1.167 firstbuf = curbuf = calloc(1, sizeof(struct cx_strreplace_ibuf)); 1.168 if (!firstbuf) return cx_mutstrn(NULL, 0); 1.169 @@ -583,13 +584,13 @@ 1.170 return cx_mutstrn(NULL, 0); 1.171 } 1.172 1.173 - /* Search occurrences */ 1.174 + // Search occurrences 1.175 cxstring searchstr = str; 1.176 size_t found = 0; 1.177 do { 1.178 cxstring match = cx_strstr(searchstr, pattern); 1.179 if (match.length > 0) { 1.180 - /* Allocate next buffer in chain, if required */ 1.181 + // Allocate next buffer in chain, if required 1.182 if (curbuf->len == ibuflen) { 1.183 struct cx_strreplace_ibuf *nextbuf = 1.184 calloc(1, sizeof(struct cx_strreplace_ibuf)); 1.185 @@ -607,7 +608,7 @@ 1.186 curbuf = nextbuf; 1.187 } 1.188 1.189 - /* Record match index */ 1.190 + // Record match index 1.191 found++; 1.192 size_t idx = match.ptr - str.ptr; 1.193 curbuf->buf[curbuf->len++] = idx; 1.194 @@ -618,7 +619,7 @@ 1.195 } 1.196 } while (searchstr.length > 0 && found < replmax); 1.197 1.198 - /* Allocate result string */ 1.199 + // Allocate result string 1.200 cxmutstr result; 1.201 { 1.202 ssize_t adjlen = (ssize_t) replacement.length - (ssize_t) pattern.length; 1.203 @@ -636,13 +637,13 @@ 1.204 } 1.205 } 1.206 1.207 - /* Build result string */ 1.208 + // Build result string 1.209 curbuf = firstbuf; 1.210 size_t srcidx = 0; 1.211 char *destptr = result.ptr; 1.212 do { 1.213 for (size_t i = 0; i < curbuf->len; i++) { 1.214 - /* Copy source part up to next match*/ 1.215 + // Copy source part up to next match 1.216 size_t idx = curbuf->buf[i]; 1.217 size_t srclen = idx - srcidx; 1.218 if (srclen > 0) { 1.219 @@ -651,7 +652,7 @@ 1.220 srcidx += srclen; 1.221 } 1.222 1.223 - /* Copy the replacement and skip the source pattern */ 1.224 + // Copy the replacement and skip the source pattern 1.225 srcidx += pattern.length; 1.226 memcpy(destptr, replacement.ptr, replacement.length); 1.227 destptr += replacement.length; 1.228 @@ -660,10 +661,10 @@ 1.229 } while (curbuf); 1.230 memcpy(destptr, str.ptr + srcidx, str.length - srcidx); 1.231 1.232 - /* Result is guaranteed to be zero-terminated */ 1.233 + // Result is guaranteed to be zero-terminated 1.234 result.ptr[result.length] = '\0'; 1.235 1.236 - /* Free index buffer */ 1.237 + // Free index buffer 1.238 cx_strrepl_free_ibuf(firstbuf); 1.239 1.240 return result;