Thu, 18 Dec 2025 17:50:15 +0100
update ucx
| 174 | 1 | /* |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
| 3 | * | |
| 4 | * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * | |
| 9 | * 1. Redistributions of source code must retain the above copyright | |
| 10 | * notice, this list of conditions and the following disclaimer. | |
| 11 | * | |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 13 | * notice, this list of conditions and the following disclaimer in the | |
| 14 | * documentation and/or other materials provided with the distribution. | |
| 15 | * | |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
| 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | * POSSIBILITY OF SUCH DAMAGE. | |
| 27 | */ | |
| 1016 | 28 | |
| 29 | // for memrchr in glibc | |
| 845 | 30 | #define _GNU_SOURCE |
| 31 | ||
| 174 | 32 | #include "cx/string.h" |
| 33 | ||
| 34 | #include <string.h> | |
| 35 | #include <stdarg.h> | |
| 440 | 36 | #include <assert.h> |
| 37 | #include <errno.h> | |
| 38 | #include <limits.h> | |
| 39 | #include <float.h> | |
| 845 | 40 | #include <ctype.h> |
| 174 | 41 | |
| 440 | 42 | #ifdef _WIN32 |
| 43 | #define cx_strcasecmp_impl _strnicmp | |
| 44 | #else | |
| 45 | #include <strings.h> | |
| 46 | #define cx_strcasecmp_impl strncasecmp | |
| 47 | #endif | |
| 174 | 48 | |
| 49 | void cx_strfree(cxmutstr *str) { | |
| 440 | 50 | if (str == NULL) return; |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
51 | cxFreeDefault(str->ptr); |
| 174 | 52 | str->ptr = NULL; |
| 53 | str->length = 0; | |
| 54 | } | |
| 55 | ||
| 56 | void cx_strfree_a( | |
| 324 | 57 | const CxAllocator *alloc, |
| 174 | 58 | cxmutstr *str |
| 59 | ) { | |
| 440 | 60 | if (str == NULL) return; |
| 174 | 61 | cxFree(alloc, str->ptr); |
| 62 | str->ptr = NULL; | |
| 63 | str->length = 0; | |
| 64 | } | |
| 65 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
66 | int cx_strcpy_a( |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
67 | const CxAllocator *alloc, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
68 | cxmutstr *dest, |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
69 | cxstring src |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
70 | ) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
71 | if (cxReallocate(alloc, &dest->ptr, src.length + 1)) { |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
72 | return 1; // LCOV_EXCL_LINE |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
73 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
74 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
75 | memcpy(dest->ptr, src.ptr, src.length); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
76 | dest->length = src.length; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
77 | dest->ptr[dest->length] = '\0'; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
78 | |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
79 | return 0; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
80 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
81 | |
| 174 | 82 | size_t cx_strlen( |
| 83 | size_t count, | |
| 84 | ... | |
| 85 | ) { | |
| 86 | if (count == 0) return 0; | |
| 87 | ||
| 88 | va_list ap; | |
| 89 | va_start(ap, count); | |
| 90 | size_t size = 0; | |
| 440 | 91 | for (size_t i = 0; i < count; i++) { |
| 174 | 92 | cxstring str = va_arg(ap, cxstring); |
| 440 | 93 | if (size > SIZE_MAX - str.length) errno = EOVERFLOW; |
| 174 | 94 | size += str.length; |
| 95 | } | |
| 96 | va_end(ap); | |
| 97 | ||
| 98 | return size; | |
| 99 | } | |
| 100 | ||
| 101 | cxmutstr cx_strcat_ma( | |
| 324 | 102 | const CxAllocator *alloc, |
| 174 | 103 | cxmutstr str, |
| 104 | size_t count, | |
| 105 | ... | |
| 106 | ) { | |
| 107 | if (count == 0) return str; | |
| 108 | va_list ap; | |
| 109 | va_start(ap, count); | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
110 | va_list ap2; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
111 | va_copy(ap2, ap); |
| 174 | 112 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
113 | // compute overall length |
| 440 | 114 | bool overflow = false; |
| 174 | 115 | size_t slen = str.length; |
| 440 | 116 | for (size_t i = 0; i < count; i++) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
117 | cxstring s = va_arg(ap, cxstring); |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
118 | if (slen > SIZE_MAX - s.length) overflow = true; |
| 174 | 119 | slen += s.length; |
| 120 | } | |
| 121 | va_end(ap); | |
| 122 | ||
| 440 | 123 | // abort in case of overflow |
| 124 | if (overflow) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
125 | va_end(ap2); |
| 440 | 126 | errno = EOVERFLOW; |
| 127 | return (cxmutstr) { NULL, 0 }; | |
| 128 | } | |
| 129 | ||
| 174 | 130 | // reallocate or create new string |
| 440 | 131 | char *newstr; |
| 174 | 132 | if (str.ptr == NULL) { |
| 440 | 133 | newstr = cxMalloc(alloc, slen + 1); |
| 174 | 134 | } else { |
| 440 | 135 | newstr = cxRealloc(alloc, str.ptr, slen + 1); |
| 174 | 136 | } |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
137 | if (newstr == NULL) { // LCOV_EXCL_START |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
138 | va_end(ap2); |
| 440 | 139 | return (cxmutstr) {NULL, 0}; |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
140 | } // LCOV_EXCL_STOP |
| 440 | 141 | str.ptr = newstr; |
| 174 | 142 | |
| 143 | // concatenate strings | |
| 144 | size_t pos = str.length; | |
| 145 | str.length = slen; | |
| 440 | 146 | for (size_t i = 0; i < count; i++) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
147 | cxstring s = va_arg(ap2, cxstring); |
| 174 | 148 | memcpy(str.ptr + pos, s.ptr, s.length); |
| 149 | pos += s.length; | |
| 150 | } | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
151 | va_end(ap2); |
| 174 | 152 | |
| 153 | // terminate string | |
| 154 | str.ptr[str.length] = '\0'; | |
| 155 | ||
| 156 | return str; | |
| 157 | } | |
| 158 | ||
| 159 | cxstring cx_strsubs( | |
| 160 | cxstring string, | |
| 161 | size_t start | |
| 162 | ) { | |
| 163 | return cx_strsubsl(string, start, string.length - start); | |
| 164 | } | |
| 165 | ||
| 166 | cxmutstr cx_strsubs_m( | |
| 167 | cxmutstr string, | |
| 168 | size_t start | |
| 169 | ) { | |
| 170 | return cx_strsubsl_m(string, start, string.length - start); | |
| 171 | } | |
| 172 | ||
| 173 | cxstring cx_strsubsl( | |
| 174 | cxstring string, | |
| 175 | size_t start, | |
| 176 | size_t length | |
| 177 | ) { | |
| 178 | if (start > string.length) { | |
| 179 | return (cxstring) {NULL, 0}; | |
| 180 | } | |
| 181 | ||
| 182 | size_t rem_len = string.length - start; | |
| 183 | if (length > rem_len) { | |
| 184 | length = rem_len; | |
| 185 | } | |
| 186 | ||
| 187 | return (cxstring) {string.ptr + start, length}; | |
| 188 | } | |
| 189 | ||
| 190 | cxmutstr cx_strsubsl_m( | |
| 191 | cxmutstr string, | |
| 192 | size_t start, | |
| 193 | size_t length | |
| 194 | ) { | |
| 195 | cxstring result = cx_strsubsl(cx_strcast(string), start, length); | |
| 196 | return (cxmutstr) {(char *) result.ptr, result.length}; | |
| 197 | } | |
| 198 | ||
| 199 | cxstring cx_strchr( | |
| 200 | cxstring string, | |
| 201 | int chr | |
| 202 | ) { | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
203 | char *ret = memchr(string.ptr, 0xFF & chr, string.length); |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
204 | if (ret == NULL) return (cxstring) {NULL, 0}; |
|
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
205 | return (cxstring) {ret, string.length - (ret - string.ptr)}; |
| 174 | 206 | } |
| 207 | ||
| 208 | cxmutstr cx_strchr_m( | |
| 209 | cxmutstr string, | |
| 210 | int chr | |
| 211 | ) { | |
| 212 | cxstring result = cx_strchr(cx_strcast(string), chr); | |
| 213 | return (cxmutstr) {(char *) result.ptr, result.length}; | |
| 214 | } | |
| 215 | ||
| 216 | cxstring cx_strrchr( | |
| 845 | 217 | cxstring string, |
| 218 | int chr | |
| 174 | 219 | ) { |
| 845 | 220 | #ifdef WITH_MEMRCHR |
| 221 | char *ret = memrchr(string.ptr, 0xFF & chr, string.length); | |
| 222 | if (ret == NULL) return (cxstring) {NULL, 0}; | |
| 223 | return (cxstring) {ret, string.length - (ret - string.ptr)}; | |
| 224 | #else | |
| 174 | 225 | chr = 0xFF & chr; |
| 226 | size_t i = string.length; | |
| 227 | while (i > 0) { | |
| 228 | i--; | |
| 229 | if (string.ptr[i] == chr) { | |
| 230 | return cx_strsubs(string, i); | |
| 231 | } | |
| 232 | } | |
| 233 | return (cxstring) {NULL, 0}; | |
| 845 | 234 | #endif |
| 174 | 235 | } |
| 236 | ||
| 237 | cxmutstr cx_strrchr_m( | |
| 238 | cxmutstr string, | |
| 239 | int chr | |
| 240 | ) { | |
| 241 | cxstring result = cx_strrchr(cx_strcast(string), chr); | |
| 242 | return (cxmutstr) {(char *) result.ptr, result.length}; | |
| 243 | } | |
| 244 | ||
| 1016 | 245 | #ifndef cx_strSTR_SBO_SIZE |
| 246 | #define cx_strSTR_SBO_SIZE 128 | |
| 174 | 247 | #endif |
| 1016 | 248 | const unsigned cx_strstr_sbo_size = cx_strSTR_SBO_SIZE; |
| 174 | 249 | |
| 250 | cxstring cx_strstr( | |
| 251 | cxstring haystack, | |
| 252 | cxstring needle | |
| 253 | ) { | |
| 254 | if (needle.length == 0) { | |
| 255 | return haystack; | |
| 256 | } | |
| 257 | ||
| 258 | // optimize for single-char needles | |
| 259 | if (needle.length == 1) { | |
| 260 | return cx_strchr(haystack, *needle.ptr); | |
| 261 | } | |
| 262 | ||
| 263 | /* | |
| 264 | * IMPORTANT: | |
| 265 | * Our prefix table contains the prefix length PLUS ONE | |
| 266 | * this is our decision, because we want to use the full range of size_t. | |
| 267 | * The original algorithm needs a (-1) at one single place, | |
| 268 | * and we want to avoid that. | |
| 269 | */ | |
| 270 | ||
| 271 | // local prefix table | |
| 1016 | 272 | size_t s_prefix_table[cx_strSTR_SBO_SIZE]; |
| 174 | 273 | |
| 274 | // check needle length and use appropriate prefix table | |
| 275 | // if the pattern exceeds static prefix table, allocate on the heap | |
| 1016 | 276 | const bool useheap = needle.length >= cx_strSTR_SBO_SIZE; |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
277 | register size_t *ptable = useheap |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
278 | ? cxCallocDefault(needle.length + 1, sizeof(size_t)) |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
279 | : s_prefix_table; |
| 174 | 280 | |
| 281 | // keep counter in registers | |
| 282 | register size_t i, j; | |
| 283 | ||
| 284 | // fill prefix table | |
| 285 | i = 0; | |
| 286 | j = 0; | |
| 287 | ptable[i] = j; | |
| 288 | while (i < needle.length) { | |
| 289 | while (j >= 1 && needle.ptr[j - 1] != needle.ptr[i]) { | |
| 290 | j = ptable[j - 1]; | |
| 291 | } | |
| 292 | i++; | |
| 293 | j++; | |
| 294 | ptable[i] = j; | |
| 295 | } | |
| 296 | ||
| 297 | // search | |
| 298 | cxstring result = {NULL, 0}; | |
| 299 | i = 0; | |
| 300 | j = 1; | |
| 301 | while (i < haystack.length) { | |
| 302 | while (j >= 1 && haystack.ptr[i] != needle.ptr[j - 1]) { | |
| 303 | j = ptable[j - 1]; | |
| 304 | } | |
| 305 | i++; | |
| 306 | j++; | |
| 307 | if (j - 1 == needle.length) { | |
| 308 | size_t start = i - needle.length; | |
| 309 | result.ptr = haystack.ptr + start; | |
| 310 | result.length = haystack.length - start; | |
| 311 | break; | |
| 312 | } | |
| 313 | } | |
| 314 | ||
| 315 | // if prefix table was allocated on the heap, free it | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
316 | if (useheap) { |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
317 | cxFreeDefault(ptable); |
| 174 | 318 | } |
| 319 | ||
| 320 | return result; | |
| 321 | } | |
| 322 | ||
| 323 | cxmutstr cx_strstr_m( | |
| 324 | cxmutstr haystack, | |
| 325 | cxstring needle | |
| 326 | ) { | |
| 327 | cxstring result = cx_strstr(cx_strcast(haystack), needle); | |
| 328 | return (cxmutstr) {(char *) result.ptr, result.length}; | |
| 329 | } | |
| 330 | ||
| 331 | size_t cx_strsplit( | |
| 332 | cxstring string, | |
| 333 | cxstring delim, | |
| 334 | size_t limit, | |
| 335 | cxstring *output | |
| 336 | ) { | |
| 337 | // special case: output limit is zero | |
| 338 | if (limit == 0) return 0; | |
| 339 | ||
| 340 | // special case: delimiter is empty | |
| 341 | if (delim.length == 0) { | |
| 342 | output[0] = string; | |
| 343 | return 1; | |
| 344 | } | |
| 345 | ||
| 346 | // special cases: delimiter is at least as large as the string | |
| 347 | if (delim.length >= string.length) { | |
| 348 | // exact match | |
| 349 | if (cx_strcmp(string, delim) == 0) { | |
| 350 | output[0] = cx_strn(string.ptr, 0); | |
| 351 | output[1] = cx_strn(string.ptr + string.length, 0); | |
| 352 | return 2; | |
| 353 | } else { | |
| 354 | // no match possible | |
| 355 | output[0] = string; | |
| 356 | return 1; | |
| 357 | } | |
| 358 | } | |
| 359 | ||
| 360 | size_t n = 0; | |
| 361 | cxstring curpos = string; | |
| 362 | while (1) { | |
| 363 | ++n; | |
| 364 | cxstring match = cx_strstr(curpos, delim); | |
| 365 | if (match.length > 0) { | |
| 366 | // is the limit reached? | |
| 367 | if (n < limit) { | |
| 368 | // copy the current string to the array | |
| 369 | cxstring item = cx_strn(curpos.ptr, match.ptr - curpos.ptr); | |
| 370 | output[n - 1] = item; | |
| 371 | size_t processed = item.length + delim.length; | |
| 372 | curpos.ptr += processed; | |
| 373 | curpos.length -= processed; | |
| 374 | } else { | |
| 375 | // limit reached, copy the _full_ remaining string | |
| 376 | output[n - 1] = curpos; | |
| 377 | break; | |
| 378 | } | |
| 379 | } else { | |
| 380 | // no more matches, copy last string | |
| 381 | output[n - 1] = curpos; | |
| 382 | break; | |
| 383 | } | |
| 384 | } | |
| 385 | ||
| 386 | return n; | |
| 387 | } | |
| 388 | ||
| 389 | size_t cx_strsplit_a( | |
| 324 | 390 | const CxAllocator *allocator, |
| 174 | 391 | cxstring string, |
| 392 | cxstring delim, | |
| 393 | size_t limit, | |
| 394 | cxstring **output | |
| 395 | ) { | |
| 396 | // find out how many splits we're going to make and allocate memory | |
| 397 | size_t n = 0; | |
| 398 | cxstring curpos = string; | |
| 399 | while (1) { | |
| 400 | ++n; | |
| 401 | cxstring match = cx_strstr(curpos, delim); | |
| 402 | if (match.length > 0) { | |
| 403 | // is the limit reached? | |
| 404 | if (n < limit) { | |
| 405 | size_t processed = match.ptr - curpos.ptr + delim.length; | |
| 406 | curpos.ptr += processed; | |
| 407 | curpos.length -= processed; | |
| 408 | } else { | |
| 409 | // limit reached | |
| 410 | break; | |
| 411 | } | |
| 412 | } else { | |
| 413 | // no more matches | |
| 414 | break; | |
| 415 | } | |
| 416 | } | |
| 417 | *output = cxCalloc(allocator, n, sizeof(cxstring)); | |
| 418 | return cx_strsplit(string, delim, n, *output); | |
| 419 | } | |
| 420 | ||
| 421 | size_t cx_strsplit_m( | |
| 422 | cxmutstr string, | |
| 423 | cxstring delim, | |
| 424 | size_t limit, | |
| 425 | cxmutstr *output | |
| 426 | ) { | |
| 427 | return cx_strsplit(cx_strcast(string), | |
| 428 | delim, limit, (cxstring *) output); | |
| 429 | } | |
| 430 | ||
| 431 | size_t cx_strsplit_ma( | |
| 324 | 432 | const CxAllocator *allocator, |
| 174 | 433 | cxmutstr string, |
| 434 | cxstring delim, | |
| 435 | size_t limit, | |
| 436 | cxmutstr **output | |
| 437 | ) { | |
| 438 | return cx_strsplit_a(allocator, cx_strcast(string), | |
| 439 | delim, limit, (cxstring **) output); | |
| 440 | } | |
| 441 | ||
| 870 | 442 | int cx_strcmp_( |
| 174 | 443 | cxstring s1, |
| 444 | cxstring s2 | |
| 445 | ) { | |
| 446 | if (s1.length == s2.length) { | |
| 440 | 447 | return strncmp(s1.ptr, s2.ptr, s1.length); |
| 174 | 448 | } else if (s1.length > s2.length) { |
| 440 | 449 | int r = strncmp(s1.ptr, s2.ptr, s2.length); |
| 450 | if (r != 0) return r; | |
| 174 | 451 | return 1; |
| 452 | } else { | |
| 440 | 453 | int r = strncmp(s1.ptr, s2.ptr, s1.length); |
| 454 | if (r != 0) return r; | |
| 174 | 455 | return -1; |
| 456 | } | |
| 457 | } | |
| 458 | ||
| 870 | 459 | int cx_strcasecmp_( |
| 174 | 460 | cxstring s1, |
| 461 | cxstring s2 | |
| 462 | ) { | |
| 463 | if (s1.length == s2.length) { | |
| 440 | 464 | return cx_strcasecmp_impl(s1.ptr, s2.ptr, s1.length); |
| 174 | 465 | } else if (s1.length > s2.length) { |
| 440 | 466 | int r = cx_strcasecmp_impl(s1.ptr, s2.ptr, s2.length); |
| 467 | if (r != 0) return r; | |
| 174 | 468 | return 1; |
| 469 | } else { | |
| 440 | 470 | int r = cx_strcasecmp_impl(s1.ptr, s2.ptr, s1.length); |
| 471 | if (r != 0) return r; | |
| 174 | 472 | return -1; |
| 473 | } | |
| 474 | } | |
| 475 | ||
| 476 | int cx_strcmp_p( | |
| 324 | 477 | const void *s1, |
| 478 | const void *s2 | |
| 174 | 479 | ) { |
| 324 | 480 | const cxstring *left = s1; |
| 481 | const cxstring *right = s2; | |
| 174 | 482 | return cx_strcmp(*left, *right); |
| 483 | } | |
| 484 | ||
| 485 | int cx_strcasecmp_p( | |
| 324 | 486 | const void *s1, |
| 487 | const void *s2 | |
| 174 | 488 | ) { |
| 324 | 489 | const cxstring *left = s1; |
| 490 | const cxstring *right = s2; | |
| 174 | 491 | return cx_strcasecmp(*left, *right); |
| 492 | } | |
| 493 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
494 | cxmutstr cx_strdup_a_( |
| 324 | 495 | const CxAllocator *allocator, |
| 174 | 496 | cxstring string |
| 497 | ) { | |
| 498 | cxmutstr result = { | |
| 499 | cxMalloc(allocator, string.length + 1), | |
| 500 | string.length | |
| 501 | }; | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
502 | // LCOV_EXCL_START |
| 174 | 503 | if (result.ptr == NULL) { |
| 504 | result.length = 0; | |
| 505 | return result; | |
| 506 | } | |
|
943
9b5948aa5b90
update ucx to version 3.2
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
870
diff
changeset
|
507 | // LCOV_EXCL_STOP |
| 174 | 508 | memcpy(result.ptr, string.ptr, string.length); |
| 509 | result.ptr[string.length] = '\0'; | |
| 510 | return result; | |
| 511 | } | |
| 512 | ||
| 513 | cxstring cx_strtrim(cxstring string) { | |
| 514 | cxstring result = string; | |
| 845 | 515 | while (result.length > 0 && isspace((unsigned char)(result.ptr[0]))) { |
| 174 | 516 | result.ptr++; |
| 517 | result.length--; | |
| 518 | } | |
| 845 | 519 | while (result.length > 0 && isspace((unsigned char)result.ptr[result.length - 1])) { |
| 174 | 520 | result.length--; |
| 521 | } | |
| 522 | return result; | |
| 523 | } | |
| 524 | ||
| 525 | cxmutstr cx_strtrim_m(cxmutstr string) { | |
| 526 | cxstring result = cx_strtrim(cx_strcast(string)); | |
| 527 | return (cxmutstr) {(char *) result.ptr, result.length}; | |
| 528 | } | |
| 529 | ||
| 870 | 530 | bool cx_strprefix_( |
| 174 | 531 | cxstring string, |
| 532 | cxstring prefix | |
| 533 | ) { | |
| 534 | if (string.length < prefix.length) return false; | |
| 535 | return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; | |
| 536 | } | |
| 537 | ||
| 870 | 538 | bool cx_strsuffix_( |
| 174 | 539 | cxstring string, |
| 540 | cxstring suffix | |
| 541 | ) { | |
| 542 | if (string.length < suffix.length) return false; | |
| 543 | return memcmp(string.ptr + string.length - suffix.length, | |
| 544 | suffix.ptr, suffix.length) == 0; | |
| 545 | } | |
| 546 | ||
| 870 | 547 | bool cx_strcaseprefix_( |
| 174 | 548 | cxstring string, |
| 549 | cxstring prefix | |
| 550 | ) { | |
| 551 | if (string.length < prefix.length) return false; | |
| 552 | #ifdef _WIN32 | |
| 553 | return _strnicmp(string.ptr, prefix.ptr, prefix.length) == 0; | |
| 554 | #else | |
| 555 | return strncasecmp(string.ptr, prefix.ptr, prefix.length) == 0; | |
| 556 | #endif | |
| 557 | } | |
| 558 | ||
| 870 | 559 | bool cx_strcasesuffix_( |
| 174 | 560 | cxstring string, |
| 561 | cxstring suffix | |
| 562 | ) { | |
| 563 | if (string.length < suffix.length) return false; | |
| 564 | #ifdef _WIN32 | |
| 565 | return _strnicmp(string.ptr+string.length-suffix.length, | |
| 566 | suffix.ptr, suffix.length) == 0; | |
| 567 | #else | |
| 568 | return strncasecmp(string.ptr + string.length - suffix.length, | |
| 569 | suffix.ptr, suffix.length) == 0; | |
| 570 | #endif | |
| 571 | } | |
| 572 | ||
| 573 | cxmutstr cx_strreplacen_a( | |
| 324 | 574 | const CxAllocator *allocator, |
| 174 | 575 | cxstring str, |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
576 | cxstring search, |
| 174 | 577 | cxstring replacement, |
| 578 | size_t replmax | |
| 579 | ) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
580 | // special cases |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
581 | if (search.length == 0 || search.length > str.length || replmax == 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
582 | return cx_strdup_a(allocator, str); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
583 | } |
| 174 | 584 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
585 | size_t in_len = str.length; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
586 | size_t search_len = search.length; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
587 | size_t repl_len = replacement.length; |
| 174 | 588 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
589 | // first run, count the occurrences |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
590 | // and remember where the first is |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
591 | size_t occurrences = 1; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
592 | cxstring first = cx_strstr(str, search); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
593 | if (first.length == 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
594 | // special case, no replacements |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
595 | return cx_strdup_a(allocator, str); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
596 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
597 | cxstring tmp = cx_strsubs(first, search_len); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
598 | while (occurrences < replmax && |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
599 | (tmp = cx_strstr(tmp, search)).length > 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
600 | occurrences++; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
601 | tmp = cx_strsubs(tmp, search_len); |
| 174 | 602 | } |
| 603 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
604 | // calculate necessary memory |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
605 | signed long long diff_len = (signed long long) repl_len - search_len; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
606 | size_t out_len = in_len + diff_len * occurrences; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
607 | cxmutstr out = { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
608 | cxMalloc(allocator, out_len + 1), |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
609 | out_len |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
610 | }; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
611 | if (out.ptr == NULL) return out; |
| 174 | 612 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
613 | // second run: perform the replacements |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
614 | // but start where we found the first occurrence |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
615 | const char *inp = str.ptr; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
616 | tmp = first; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
617 | char *outp = out.ptr; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
618 | while (occurrences-- > 0 && (tmp = cx_strstr(tmp, search)).length > 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
619 | size_t copylen = tmp.ptr - inp; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
620 | memcpy(outp, inp, copylen); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
621 | outp += copylen; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
622 | memcpy(outp, replacement.ptr, repl_len); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
623 | outp += repl_len; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
624 | inp += copylen + search_len; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
625 | tmp = cx_strsubs(tmp, search_len); |
| 174 | 626 | } |
| 627 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
628 | // add the remaining string |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
629 | size_t copylen = in_len - (inp - str.ptr); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
630 | memcpy(outp, inp, copylen); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
631 | out.ptr[out_len] = '\0'; |
| 174 | 632 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
633 | return out; |
| 174 | 634 | } |
| 635 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
636 | CxStrtokCtx cx_strtok_( |
| 174 | 637 | cxstring str, |
| 638 | cxstring delim, | |
| 639 | size_t limit | |
| 640 | ) { | |
| 641 | CxStrtokCtx ctx; | |
| 642 | ctx.str = str; | |
| 643 | ctx.delim = delim; | |
| 644 | ctx.limit = limit; | |
| 645 | ctx.pos = 0; | |
| 646 | ctx.next_pos = 0; | |
| 647 | ctx.delim_pos = 0; | |
| 648 | ctx.found = 0; | |
| 649 | ctx.delim_more = NULL; | |
| 650 | ctx.delim_more_count = 0; | |
| 651 | return ctx; | |
| 652 | } | |
| 653 | ||
| 654 | bool cx_strtok_next( | |
| 655 | CxStrtokCtx *ctx, | |
| 656 | cxstring *token | |
| 657 | ) { | |
| 658 | // abortion criteria | |
| 659 | if (ctx->found >= ctx->limit || ctx->delim_pos >= ctx->str.length) { | |
| 660 | return false; | |
| 661 | } | |
| 662 | ||
| 663 | // determine the search start | |
| 664 | cxstring haystack = cx_strsubs(ctx->str, ctx->next_pos); | |
| 665 | ||
| 666 | // search the next delimiter | |
| 667 | cxstring delim = cx_strstr(haystack, ctx->delim); | |
| 668 | ||
| 669 | // if found, make delim capture exactly the delimiter | |
| 670 | if (delim.length > 0) { | |
| 671 | delim.length = ctx->delim.length; | |
| 672 | } | |
| 673 | ||
| 674 | // if more delimiters are specified, check them now | |
| 675 | if (ctx->delim_more_count > 0) { | |
| 440 | 676 | for (size_t i = 0; i < ctx->delim_more_count; i++) { |
| 174 | 677 | cxstring d = cx_strstr(haystack, ctx->delim_more[i]); |
| 678 | if (d.length > 0 && (delim.length == 0 || d.ptr < delim.ptr)) { | |
| 679 | delim.ptr = d.ptr; | |
| 680 | delim.length = ctx->delim_more[i].length; | |
| 681 | } | |
| 682 | } | |
| 683 | } | |
| 684 | ||
| 685 | // store the token information and adjust the context | |
| 686 | ctx->found++; | |
| 687 | ctx->pos = ctx->next_pos; | |
| 688 | token->ptr = &ctx->str.ptr[ctx->pos]; | |
| 689 | ctx->delim_pos = delim.length == 0 ? | |
| 690 | ctx->str.length : (size_t) (delim.ptr - ctx->str.ptr); | |
| 691 | token->length = ctx->delim_pos - ctx->pos; | |
| 692 | ctx->next_pos = ctx->delim_pos + delim.length; | |
| 693 | ||
| 694 | return true; | |
| 695 | } | |
| 696 | ||
| 697 | bool cx_strtok_next_m( | |
| 698 | CxStrtokCtx *ctx, | |
| 699 | cxmutstr *token | |
| 700 | ) { | |
| 701 | return cx_strtok_next(ctx, (cxstring *) token); | |
| 702 | } | |
| 703 | ||
| 704 | void cx_strtok_delim( | |
| 705 | CxStrtokCtx *ctx, | |
| 324 | 706 | const cxstring *delim, |
| 174 | 707 | size_t count |
| 708 | ) { | |
| 709 | ctx->delim_more = delim; | |
| 710 | ctx->delim_more_count = count; | |
| 711 | } | |
| 440 | 712 | |
| 713 | #define cx_strtoX_signed_impl(rtype, rmin, rmax) \ | |
| 714 | long long result; \ | |
| 715 | if (cx_strtoll_lc(str, &result, base, groupsep)) { \ | |
| 716 | return -1; \ | |
| 717 | } \ | |
| 718 | if (result < rmin || result > rmax) { \ | |
| 719 | errno = ERANGE; \ | |
| 720 | return -1; \ | |
| 721 | } \ | |
| 722 | *output = (rtype) result; \ | |
| 723 | return 0 | |
| 724 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
725 | int cx_strtos_lc_(cxstring str, short *output, int base, const char *groupsep) { |
| 440 | 726 | cx_strtoX_signed_impl(short, SHRT_MIN, SHRT_MAX); |
| 727 | } | |
| 728 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
729 | int cx_strtoi_lc_(cxstring str, int *output, int base, const char *groupsep) { |
| 440 | 730 | cx_strtoX_signed_impl(int, INT_MIN, INT_MAX); |
| 731 | } | |
| 732 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
733 | int cx_strtol_lc_(cxstring str, long *output, int base, const char *groupsep) { |
| 440 | 734 | cx_strtoX_signed_impl(long, LONG_MIN, LONG_MAX); |
| 735 | } | |
| 736 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
737 | int cx_strtoll_lc_(cxstring str, long long *output, int base, const char *groupsep) { |
| 440 | 738 | // strategy: parse as unsigned, check range, negate if required |
| 739 | bool neg = false; | |
| 740 | size_t start_unsigned = 0; | |
| 741 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
742 | // emptiness check |
| 440 | 743 | if (str.length == 0) { |
| 744 | errno = EINVAL; | |
| 745 | return -1; | |
| 746 | } | |
| 747 | ||
| 748 | // test if we have a negative sign character | |
| 749 | if (str.ptr[start_unsigned] == '-') { | |
| 750 | neg = true; | |
| 751 | start_unsigned++; | |
| 752 | // must not be followed by positive sign character | |
| 753 | if (str.length == 1 || str.ptr[start_unsigned] == '+') { | |
| 754 | errno = EINVAL; | |
| 755 | return -1; | |
| 756 | } | |
| 757 | } | |
| 758 | ||
| 759 | // now parse the number with strtoull | |
| 760 | unsigned long long v; | |
| 761 | cxstring ustr = start_unsigned == 0 ? str | |
| 762 | : cx_strn(str.ptr + start_unsigned, str.length - start_unsigned); | |
| 763 | int ret = cx_strtoull_lc(ustr, &v, base, groupsep); | |
| 764 | if (ret != 0) return ret; | |
| 765 | if (neg) { | |
| 766 | if (v - 1 > LLONG_MAX) { | |
| 767 | errno = ERANGE; | |
| 768 | return -1; | |
| 769 | } | |
| 770 | *output = -(long long) v; | |
| 771 | return 0; | |
| 772 | } else { | |
| 773 | if (v > LLONG_MAX) { | |
| 774 | errno = ERANGE; | |
| 775 | return -1; | |
| 776 | } | |
| 777 | *output = (long long) v; | |
| 778 | return 0; | |
| 779 | } | |
| 780 | } | |
| 781 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
782 | int cx_strtoi8_lc_(cxstring str, int8_t *output, int base, const char *groupsep) { |
| 440 | 783 | cx_strtoX_signed_impl(int8_t, INT8_MIN, INT8_MAX); |
| 784 | } | |
| 785 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
786 | int cx_strtoi16_lc_(cxstring str, int16_t *output, int base, const char *groupsep) { |
| 440 | 787 | cx_strtoX_signed_impl(int16_t, INT16_MIN, INT16_MAX); |
| 788 | } | |
| 789 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
790 | int cx_strtoi32_lc_(cxstring str, int32_t *output, int base, const char *groupsep) { |
| 440 | 791 | cx_strtoX_signed_impl(int32_t, INT32_MIN, INT32_MAX); |
| 792 | } | |
| 793 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
794 | int cx_strtoi64_lc_(cxstring str, int64_t *output, int base, const char *groupsep) { |
| 440 | 795 | assert(sizeof(long long) == sizeof(int64_t)); // should be true on all platforms |
| 796 | return cx_strtoll_lc(str, (long long*) output, base, groupsep); | |
| 797 | } | |
| 798 | ||
| 799 | #define cx_strtoX_unsigned_impl(rtype, rmax) \ | |
| 800 | uint64_t result; \ | |
| 801 | if (cx_strtou64_lc(str, &result, base, groupsep)) { \ | |
| 802 | return -1; \ | |
| 803 | } \ | |
| 804 | if (result > rmax) { \ | |
| 805 | errno = ERANGE; \ | |
| 806 | return -1; \ | |
| 807 | } \ | |
| 808 | *output = (rtype) result; \ | |
| 809 | return 0 | |
| 810 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
811 | int cx_strtous_lc_(cxstring str, unsigned short *output, int base, const char *groupsep) { |
| 440 | 812 | cx_strtoX_unsigned_impl(unsigned short, USHRT_MAX); |
| 813 | } | |
| 814 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
815 | int cx_strtou_lc_(cxstring str, unsigned int *output, int base, const char *groupsep) { |
| 440 | 816 | cx_strtoX_unsigned_impl(unsigned int, UINT_MAX); |
| 817 | } | |
| 818 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
819 | int cx_strtoul_lc_(cxstring str, unsigned long *output, int base, const char *groupsep) { |
| 440 | 820 | cx_strtoX_unsigned_impl(unsigned long, ULONG_MAX); |
| 821 | } | |
| 822 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
823 | int cx_strtoull_lc_(cxstring str, unsigned long long *output, int base, const char *groupsep) { |
| 440 | 824 | // some sanity checks |
| 825 | if (str.length == 0) { | |
| 826 | errno = EINVAL; | |
| 827 | return -1; | |
| 828 | } | |
| 829 | if (!(base == 2 || base == 8 || base == 10 || base == 16)) { | |
| 830 | errno = EINVAL; | |
| 831 | return -1; | |
| 832 | } | |
| 833 | if (groupsep == NULL) groupsep = ""; | |
| 834 | ||
| 835 | // find the actual start of the number | |
| 836 | if (str.ptr[0] == '+') { | |
| 837 | str.ptr++; | |
| 838 | str.length--; | |
| 839 | if (str.length == 0) { | |
| 840 | errno = EINVAL; | |
| 841 | return -1; | |
| 842 | } | |
| 843 | } | |
| 844 | size_t start = 0; | |
| 845 | ||
| 846 | // if base is 2 or 16, some leading stuff may appear | |
| 847 | if (base == 2) { | |
| 848 | if ((str.ptr[0] | 32) == 'b') { | |
| 849 | start = 1; | |
| 850 | } else if (str.ptr[0] == '0' && str.length > 1) { | |
| 851 | if ((str.ptr[1] | 32) == 'b') { | |
| 852 | start = 2; | |
| 853 | } | |
| 854 | } | |
| 855 | } else if (base == 16) { | |
| 856 | if ((str.ptr[0] | 32) == 'x' || str.ptr[0] == '#') { | |
| 857 | start = 1; | |
| 858 | } else if (str.ptr[0] == '0' && str.length > 1) { | |
| 859 | if ((str.ptr[1] | 32) == 'x') { | |
| 860 | start = 2; | |
| 861 | } | |
| 862 | } | |
| 863 | } | |
| 864 | ||
| 865 | // check if there are digits left | |
| 866 | if (start >= str.length) { | |
| 867 | errno = EINVAL; | |
| 868 | return -1; | |
| 869 | } | |
| 870 | ||
| 871 | // now parse the number | |
| 872 | unsigned long long result = 0; | |
| 873 | for (size_t i = start; i < str.length; i++) { | |
| 874 | // ignore group separators | |
| 875 | if (strchr(groupsep, str.ptr[i])) continue; | |
| 876 | ||
| 877 | // determine the digit value of the character | |
| 878 | unsigned char c = str.ptr[i]; | |
| 879 | if (c >= 'a') c = 10 + (c - 'a'); | |
| 880 | else if (c >= 'A') c = 10 + (c - 'A'); | |
| 881 | else if (c >= '0') c = c - '0'; | |
| 882 | else c = 255; | |
| 883 | if (c >= base) { | |
| 884 | errno = EINVAL; | |
| 885 | return -1; | |
| 886 | } | |
| 887 | ||
| 888 | // now combine the digit with what we already have | |
| 889 | unsigned long right = (result & 0xff) * base + c; | |
| 890 | unsigned long long left = (result >> 8) * base + (right >> 8); | |
| 891 | if (left > (ULLONG_MAX >> 8)) { | |
| 892 | errno = ERANGE; | |
| 893 | return -1; | |
| 894 | } | |
| 895 | result = (left << 8) + (right & 0xff); | |
| 896 | } | |
| 897 | ||
| 898 | *output = result; | |
| 899 | return 0; | |
| 900 | } | |
| 901 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
902 | int cx_strtou8_lc_(cxstring str, uint8_t *output, int base, const char *groupsep) { |
| 440 | 903 | cx_strtoX_unsigned_impl(uint8_t, UINT8_MAX); |
| 904 | } | |
| 905 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
906 | int cx_strtou16_lc_(cxstring str, uint16_t *output, int base, const char *groupsep) { |
| 440 | 907 | cx_strtoX_unsigned_impl(uint16_t, UINT16_MAX); |
| 908 | } | |
| 909 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
910 | int cx_strtou32_lc_(cxstring str, uint32_t *output, int base, const char *groupsep) { |
| 440 | 911 | cx_strtoX_unsigned_impl(uint32_t, UINT32_MAX); |
| 912 | } | |
| 913 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
914 | int cx_strtou64_lc_(cxstring str, uint64_t *output, int base, const char *groupsep) { |
| 440 | 915 | assert(sizeof(unsigned long long) == sizeof(uint64_t)); // should be true on all platforms |
| 916 | return cx_strtoull_lc(str, (unsigned long long*) output, base, groupsep); | |
| 917 | } | |
| 918 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
919 | int cx_strtoz_lc_(cxstring str, size_t *output, int base, const char *groupsep) { |
| 440 | 920 | #if SIZE_MAX == UINT32_MAX |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
921 | return cx_strtou32_lc_(str, (uint32_t*) output, base, groupsep); |
| 440 | 922 | #elif SIZE_MAX == UINT64_MAX |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
923 | return cx_strtoull_lc_(str, (unsigned long long *) output, base, groupsep); |
| 440 | 924 | #else |
| 925 | #error "unsupported size_t size" | |
| 926 | #endif | |
| 927 | } | |
| 928 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
929 | int cx_strtof_lc_(cxstring str, float *output, char decsep, const char *groupsep) { |
| 440 | 930 | // use string to double and add a range check |
| 931 | double d; | |
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
932 | int ret = cx_strtod_lc_(str, &d, decsep, groupsep); |
| 440 | 933 | if (ret != 0) return ret; |
| 934 | // note: FLT_MIN is the smallest POSITIVE number that can be represented | |
| 935 | double test = d < 0 ? -d : d; | |
| 936 | if (test < FLT_MIN || test > FLT_MAX) { | |
| 937 | errno = ERANGE; | |
| 938 | return -1; | |
| 939 | } | |
| 940 | *output = (float) d; | |
| 941 | return 0; | |
| 942 | } | |
| 943 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
944 | int cx_strtod_lc_(cxstring str, double *output, char decsep, const char *groupsep) { |
| 440 | 945 | // TODO: overflow check |
| 946 | // TODO: increase precision | |
| 947 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
948 | // emptiness check |
| 440 | 949 | if (str.length == 0) { |
| 950 | errno = EINVAL; | |
| 951 | return -1; | |
| 952 | } | |
| 953 | ||
| 954 | double result = 0.; | |
| 955 | int sign = 1; | |
| 956 | ||
| 957 | // check if there is a sign | |
| 958 | if (str.ptr[0] == '-') { | |
| 959 | sign = -1; | |
| 960 | str.ptr++; | |
| 961 | str.length--; | |
| 962 | } else if (str.ptr[0] == '+') { | |
| 963 | str.ptr++; | |
| 964 | str.length--; | |
| 965 | } | |
| 966 | ||
| 967 | // there must be at least one char to parse | |
| 968 | if (str.length == 0) { | |
| 969 | errno = EINVAL; | |
| 970 | return -1; | |
| 971 | } | |
| 972 | ||
| 973 | // parse all digits until we find the decsep | |
| 974 | size_t pos = 0; | |
| 975 | do { | |
| 845 | 976 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 440 | 977 | result = result * 10 + (str.ptr[pos] - '0'); |
| 978 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 979 | break; | |
| 980 | } | |
| 981 | } while (++pos < str.length); | |
| 982 | ||
| 983 | // already done? | |
| 984 | if (pos == str.length) { | |
| 985 | *output = result * sign; | |
| 986 | return 0; | |
| 987 | } | |
| 988 | ||
| 989 | // is the next char the decsep? | |
| 990 | if (str.ptr[pos] == decsep) { | |
| 991 | pos++; | |
| 992 | // it may end with the decsep, if it did not start with it | |
| 993 | if (pos == str.length) { | |
| 994 | if (str.length == 1) { | |
| 995 | errno = EINVAL; | |
| 996 | return -1; | |
| 997 | } else { | |
| 998 | *output = result * sign; | |
| 999 | return 0; | |
| 1000 | } | |
| 1001 | } | |
| 1002 | // parse everything until exponent or end | |
| 1003 | double factor = 1.; | |
| 1004 | do { | |
| 845 | 1005 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 440 | 1006 | factor *= 0.1; |
| 1007 | result = result + factor * (str.ptr[pos] - '0'); | |
| 1008 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 1009 | break; | |
| 1010 | } | |
| 1011 | } while (++pos < str.length); | |
| 1012 | } | |
| 1013 | ||
| 1014 | // no exponent? | |
| 1015 | if (pos == str.length) { | |
| 1016 | *output = result * sign; | |
| 1017 | return 0; | |
| 1018 | } | |
| 1019 | ||
| 1020 | // now the next separator MUST be the exponent separator | |
| 1021 | // and at least one char must follow | |
| 1022 | if ((str.ptr[pos] | 32) != 'e' || str.length <= pos + 1) { | |
| 1023 | errno = EINVAL; | |
| 1024 | return -1; | |
| 1025 | } | |
| 1026 | pos++; | |
| 1027 | ||
| 1028 | // check if we have a sign for the exponent | |
| 1029 | double factor = 10.; | |
| 1030 | if (str.ptr[pos] == '-') { | |
| 1031 | factor = .1; | |
| 1032 | pos++; | |
| 1033 | } else if (str.ptr[pos] == '+') { | |
| 1034 | pos++; | |
| 1035 | } | |
| 1036 | ||
| 1037 | // at least one digit must follow | |
| 1038 | if (pos == str.length) { | |
| 1039 | errno = EINVAL; | |
| 1040 | return -1; | |
| 1041 | } | |
| 1042 | ||
| 1043 | // parse the exponent | |
| 1044 | unsigned int exp = 0; | |
| 1045 | do { | |
| 845 | 1046 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 440 | 1047 | exp = 10 * exp + (str.ptr[pos] - '0'); |
| 1048 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 1049 | errno = EINVAL; | |
| 1050 | return -1; | |
| 1051 | } | |
| 1052 | } while (++pos < str.length); | |
| 1053 | ||
| 1054 | // apply the exponent by fast exponentiation | |
| 1055 | do { | |
| 1056 | if (exp & 1) { | |
| 1057 | result *= factor; | |
| 1058 | } | |
| 1059 | factor *= factor; | |
| 1060 | } while ((exp >>= 1) > 0); | |
| 1061 | ||
| 1062 | // store the result and exit | |
| 1063 | *output = result * sign; | |
| 1064 | return 0; | |
| 1065 | } |