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