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