Wed, 19 Nov 2025 11:41:33 +0100
add first win32 listview/table code
| 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)) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
94 | return 1; |
|
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); |
| 440 | 140 | if (slen > SIZE_MAX - str.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 | } |
| 440 | 159 | if (newstr == NULL) { |
|
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}; |
| 162 | } | |
| 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 | }; | |
| 524 | if (result.ptr == NULL) { | |
| 525 | result.length = 0; | |
| 526 | return result; | |
| 527 | } | |
| 528 | memcpy(result.ptr, string.ptr, string.length); | |
| 529 | result.ptr[string.length] = '\0'; | |
| 530 | return result; | |
| 531 | } | |
| 532 | ||
| 533 | cxstring cx_strtrim(cxstring string) { | |
| 534 | cxstring result = string; | |
| 845 | 535 | while (result.length > 0 && isspace((unsigned char)(result.ptr[0]))) { |
| 174 | 536 | result.ptr++; |
| 537 | result.length--; | |
| 538 | } | |
| 845 | 539 | while (result.length > 0 && isspace((unsigned char)result.ptr[result.length - 1])) { |
| 174 | 540 | result.length--; |
| 541 | } | |
| 542 | return result; | |
| 543 | } | |
| 544 | ||
| 545 | cxmutstr cx_strtrim_m(cxmutstr string) { | |
| 546 | cxstring result = cx_strtrim(cx_strcast(string)); | |
| 547 | return (cxmutstr) {(char *) result.ptr, result.length}; | |
| 548 | } | |
| 549 | ||
| 870 | 550 | bool cx_strprefix_( |
| 174 | 551 | cxstring string, |
| 552 | cxstring prefix | |
| 553 | ) { | |
| 554 | if (string.length < prefix.length) return false; | |
| 555 | return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; | |
| 556 | } | |
| 557 | ||
| 870 | 558 | bool cx_strsuffix_( |
| 174 | 559 | cxstring string, |
| 560 | cxstring suffix | |
| 561 | ) { | |
| 562 | if (string.length < suffix.length) return false; | |
| 563 | return memcmp(string.ptr + string.length - suffix.length, | |
| 564 | suffix.ptr, suffix.length) == 0; | |
| 565 | } | |
| 566 | ||
| 870 | 567 | bool cx_strcaseprefix_( |
| 174 | 568 | cxstring string, |
| 569 | cxstring prefix | |
| 570 | ) { | |
| 571 | if (string.length < prefix.length) return false; | |
| 572 | #ifdef _WIN32 | |
| 573 | return _strnicmp(string.ptr, prefix.ptr, prefix.length) == 0; | |
| 574 | #else | |
| 575 | return strncasecmp(string.ptr, prefix.ptr, prefix.length) == 0; | |
| 576 | #endif | |
| 577 | } | |
| 578 | ||
| 870 | 579 | bool cx_strcasesuffix_( |
| 174 | 580 | cxstring string, |
| 581 | cxstring suffix | |
| 582 | ) { | |
| 583 | if (string.length < suffix.length) return false; | |
| 584 | #ifdef _WIN32 | |
| 585 | return _strnicmp(string.ptr+string.length-suffix.length, | |
| 586 | suffix.ptr, suffix.length) == 0; | |
| 587 | #else | |
| 588 | return strncasecmp(string.ptr + string.length - suffix.length, | |
| 589 | suffix.ptr, suffix.length) == 0; | |
| 590 | #endif | |
| 591 | } | |
| 592 | ||
| 593 | cxmutstr cx_strreplacen_a( | |
| 324 | 594 | const CxAllocator *allocator, |
| 174 | 595 | 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
|
596 | cxstring search, |
| 174 | 597 | cxstring replacement, |
| 598 | size_t replmax | |
| 599 | ) { | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
600 | // special cases |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
601 | if (search.length == 0 || search.length > str.length || replmax == 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
602 | return cx_strdup_a(allocator, str); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
603 | } |
| 174 | 604 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
605 | size_t in_len = str.length; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
606 | size_t search_len = search.length; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
607 | size_t repl_len = replacement.length; |
| 174 | 608 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
609 | // first run, count the occurrences |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
610 | // and remember where the first is |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
611 | size_t occurrences = 1; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
612 | cxstring first = cx_strstr(str, search); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
613 | if (first.length == 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
614 | // special case, no replacements |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
615 | return cx_strdup_a(allocator, str); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
616 | } |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
617 | cxstring tmp = cx_strsubs(first, search_len); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
618 | while (occurrences < replmax && |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
619 | (tmp = cx_strstr(tmp, search)).length > 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
620 | occurrences++; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
621 | tmp = cx_strsubs(tmp, search_len); |
| 174 | 622 | } |
| 623 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
624 | // calculate necessary memory |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
625 | 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
|
626 | size_t out_len = in_len + diff_len * occurrences; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
627 | cxmutstr out = { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
628 | cxMalloc(allocator, out_len + 1), |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
629 | out_len |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
630 | }; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
631 | if (out.ptr == NULL) return out; |
| 174 | 632 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
633 | // second run: perform the replacements |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
634 | // but start where we found the first occurrence |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
635 | const char *inp = str.ptr; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
636 | tmp = first; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
637 | char *outp = out.ptr; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
638 | while (occurrences-- > 0 && (tmp = cx_strstr(tmp, search)).length > 0) { |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
639 | size_t copylen = tmp.ptr - inp; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
640 | memcpy(outp, inp, copylen); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
641 | outp += copylen; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
642 | memcpy(outp, replacement.ptr, repl_len); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
643 | outp += repl_len; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
644 | inp += copylen + search_len; |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
645 | tmp = cx_strsubs(tmp, search_len); |
| 174 | 646 | } |
| 647 | ||
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
648 | // add the remaining string |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
649 | size_t copylen = in_len - (inp - str.ptr); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
650 | memcpy(outp, inp, copylen); |
|
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
651 | out.ptr[out_len] = '\0'; |
| 174 | 652 | |
|
629
0385a450c2a6
add list initializer
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
471
diff
changeset
|
653 | return out; |
| 174 | 654 | } |
| 655 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
656 | CxStrtokCtx cx_strtok_( |
| 174 | 657 | cxstring str, |
| 658 | cxstring delim, | |
| 659 | size_t limit | |
| 660 | ) { | |
| 661 | CxStrtokCtx ctx; | |
| 662 | ctx.str = str; | |
| 663 | ctx.delim = delim; | |
| 664 | ctx.limit = limit; | |
| 665 | ctx.pos = 0; | |
| 666 | ctx.next_pos = 0; | |
| 667 | ctx.delim_pos = 0; | |
| 668 | ctx.found = 0; | |
| 669 | ctx.delim_more = NULL; | |
| 670 | ctx.delim_more_count = 0; | |
| 671 | return ctx; | |
| 672 | } | |
| 673 | ||
| 674 | bool cx_strtok_next( | |
| 675 | CxStrtokCtx *ctx, | |
| 676 | cxstring *token | |
| 677 | ) { | |
| 678 | // abortion criteria | |
| 679 | if (ctx->found >= ctx->limit || ctx->delim_pos >= ctx->str.length) { | |
| 680 | return false; | |
| 681 | } | |
| 682 | ||
| 683 | // determine the search start | |
| 684 | cxstring haystack = cx_strsubs(ctx->str, ctx->next_pos); | |
| 685 | ||
| 686 | // search the next delimiter | |
| 687 | cxstring delim = cx_strstr(haystack, ctx->delim); | |
| 688 | ||
| 689 | // if found, make delim capture exactly the delimiter | |
| 690 | if (delim.length > 0) { | |
| 691 | delim.length = ctx->delim.length; | |
| 692 | } | |
| 693 | ||
| 694 | // if more delimiters are specified, check them now | |
| 695 | if (ctx->delim_more_count > 0) { | |
| 440 | 696 | for (size_t i = 0; i < ctx->delim_more_count; i++) { |
| 174 | 697 | cxstring d = cx_strstr(haystack, ctx->delim_more[i]); |
| 698 | if (d.length > 0 && (delim.length == 0 || d.ptr < delim.ptr)) { | |
| 699 | delim.ptr = d.ptr; | |
| 700 | delim.length = ctx->delim_more[i].length; | |
| 701 | } | |
| 702 | } | |
| 703 | } | |
| 704 | ||
| 705 | // store the token information and adjust the context | |
| 706 | ctx->found++; | |
| 707 | ctx->pos = ctx->next_pos; | |
| 708 | token->ptr = &ctx->str.ptr[ctx->pos]; | |
| 709 | ctx->delim_pos = delim.length == 0 ? | |
| 710 | ctx->str.length : (size_t) (delim.ptr - ctx->str.ptr); | |
| 711 | token->length = ctx->delim_pos - ctx->pos; | |
| 712 | ctx->next_pos = ctx->delim_pos + delim.length; | |
| 713 | ||
| 714 | return true; | |
| 715 | } | |
| 716 | ||
| 717 | bool cx_strtok_next_m( | |
| 718 | CxStrtokCtx *ctx, | |
| 719 | cxmutstr *token | |
| 720 | ) { | |
| 721 | return cx_strtok_next(ctx, (cxstring *) token); | |
| 722 | } | |
| 723 | ||
| 724 | void cx_strtok_delim( | |
| 725 | CxStrtokCtx *ctx, | |
| 324 | 726 | const cxstring *delim, |
| 174 | 727 | size_t count |
| 728 | ) { | |
| 729 | ctx->delim_more = delim; | |
| 730 | ctx->delim_more_count = count; | |
| 731 | } | |
| 440 | 732 | |
| 733 | #define cx_strtoX_signed_impl(rtype, rmin, rmax) \ | |
| 734 | long long result; \ | |
| 735 | if (cx_strtoll_lc(str, &result, base, groupsep)) { \ | |
| 736 | return -1; \ | |
| 737 | } \ | |
| 738 | if (result < rmin || result > rmax) { \ | |
| 739 | errno = ERANGE; \ | |
| 740 | return -1; \ | |
| 741 | } \ | |
| 742 | *output = (rtype) result; \ | |
| 743 | return 0 | |
| 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_strtos_lc_(cxstring str, short *output, int base, const char *groupsep) { |
| 440 | 746 | cx_strtoX_signed_impl(short, SHRT_MIN, SHRT_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_strtoi_lc_(cxstring str, int *output, int base, const char *groupsep) { |
| 440 | 750 | cx_strtoX_signed_impl(int, INT_MIN, INT_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_strtol_lc_(cxstring str, long *output, int base, const char *groupsep) { |
| 440 | 754 | cx_strtoX_signed_impl(long, LONG_MIN, LONG_MAX); |
| 755 | } | |
| 756 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
757 | int cx_strtoll_lc_(cxstring str, long long *output, int base, const char *groupsep) { |
| 440 | 758 | // strategy: parse as unsigned, check range, negate if required |
| 759 | bool neg = false; | |
| 760 | size_t start_unsigned = 0; | |
| 761 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
762 | // emptiness check |
| 440 | 763 | if (str.length == 0) { |
| 764 | errno = EINVAL; | |
| 765 | return -1; | |
| 766 | } | |
| 767 | ||
| 768 | // test if we have a negative sign character | |
| 769 | if (str.ptr[start_unsigned] == '-') { | |
| 770 | neg = true; | |
| 771 | start_unsigned++; | |
| 772 | // must not be followed by positive sign character | |
| 773 | if (str.length == 1 || str.ptr[start_unsigned] == '+') { | |
| 774 | errno = EINVAL; | |
| 775 | return -1; | |
| 776 | } | |
| 777 | } | |
| 778 | ||
| 779 | // now parse the number with strtoull | |
| 780 | unsigned long long v; | |
| 781 | cxstring ustr = start_unsigned == 0 ? str | |
| 782 | : cx_strn(str.ptr + start_unsigned, str.length - start_unsigned); | |
| 783 | int ret = cx_strtoull_lc(ustr, &v, base, groupsep); | |
| 784 | if (ret != 0) return ret; | |
| 785 | if (neg) { | |
| 786 | if (v - 1 > LLONG_MAX) { | |
| 787 | errno = ERANGE; | |
| 788 | return -1; | |
| 789 | } | |
| 790 | *output = -(long long) v; | |
| 791 | return 0; | |
| 792 | } else { | |
| 793 | if (v > LLONG_MAX) { | |
| 794 | errno = ERANGE; | |
| 795 | return -1; | |
| 796 | } | |
| 797 | *output = (long long) v; | |
| 798 | return 0; | |
| 799 | } | |
| 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_strtoi8_lc_(cxstring str, int8_t *output, int base, const char *groupsep) { |
| 440 | 803 | cx_strtoX_signed_impl(int8_t, INT8_MIN, INT8_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_strtoi16_lc_(cxstring str, int16_t *output, int base, const char *groupsep) { |
| 440 | 807 | cx_strtoX_signed_impl(int16_t, INT16_MIN, INT16_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_strtoi32_lc_(cxstring str, int32_t *output, int base, const char *groupsep) { |
| 440 | 811 | cx_strtoX_signed_impl(int32_t, INT32_MIN, INT32_MAX); |
| 812 | } | |
| 813 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
814 | int cx_strtoi64_lc_(cxstring str, int64_t *output, int base, const char *groupsep) { |
| 440 | 815 | assert(sizeof(long long) == sizeof(int64_t)); // should be true on all platforms |
| 816 | return cx_strtoll_lc(str, (long long*) output, base, groupsep); | |
| 817 | } | |
| 818 | ||
| 819 | #define cx_strtoX_unsigned_impl(rtype, rmax) \ | |
| 820 | uint64_t result; \ | |
| 821 | if (cx_strtou64_lc(str, &result, base, groupsep)) { \ | |
| 822 | return -1; \ | |
| 823 | } \ | |
| 824 | if (result > rmax) { \ | |
| 825 | errno = ERANGE; \ | |
| 826 | return -1; \ | |
| 827 | } \ | |
| 828 | *output = (rtype) result; \ | |
| 829 | return 0 | |
| 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_strtous_lc_(cxstring str, unsigned short *output, int base, const char *groupsep) { |
| 440 | 832 | cx_strtoX_unsigned_impl(unsigned short, USHRT_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_strtou_lc_(cxstring str, unsigned int *output, int base, const char *groupsep) { |
| 440 | 836 | cx_strtoX_unsigned_impl(unsigned int, UINT_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_strtoul_lc_(cxstring str, unsigned long *output, int base, const char *groupsep) { |
| 440 | 840 | cx_strtoX_unsigned_impl(unsigned long, ULONG_MAX); |
| 841 | } | |
| 842 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
843 | int cx_strtoull_lc_(cxstring str, unsigned long long *output, int base, const char *groupsep) { |
| 440 | 844 | // some sanity checks |
| 845 | if (str.length == 0) { | |
| 846 | errno = EINVAL; | |
| 847 | return -1; | |
| 848 | } | |
| 849 | if (!(base == 2 || base == 8 || base == 10 || base == 16)) { | |
| 850 | errno = EINVAL; | |
| 851 | return -1; | |
| 852 | } | |
| 853 | if (groupsep == NULL) groupsep = ""; | |
| 854 | ||
| 855 | // find the actual start of the number | |
| 856 | if (str.ptr[0] == '+') { | |
| 857 | str.ptr++; | |
| 858 | str.length--; | |
| 859 | if (str.length == 0) { | |
| 860 | errno = EINVAL; | |
| 861 | return -1; | |
| 862 | } | |
| 863 | } | |
| 864 | size_t start = 0; | |
| 865 | ||
| 866 | // if base is 2 or 16, some leading stuff may appear | |
| 867 | if (base == 2) { | |
| 868 | if ((str.ptr[0] | 32) == 'b') { | |
| 869 | start = 1; | |
| 870 | } else if (str.ptr[0] == '0' && str.length > 1) { | |
| 871 | if ((str.ptr[1] | 32) == 'b') { | |
| 872 | start = 2; | |
| 873 | } | |
| 874 | } | |
| 875 | } else if (base == 16) { | |
| 876 | if ((str.ptr[0] | 32) == 'x' || str.ptr[0] == '#') { | |
| 877 | start = 1; | |
| 878 | } else if (str.ptr[0] == '0' && str.length > 1) { | |
| 879 | if ((str.ptr[1] | 32) == 'x') { | |
| 880 | start = 2; | |
| 881 | } | |
| 882 | } | |
| 883 | } | |
| 884 | ||
| 885 | // check if there are digits left | |
| 886 | if (start >= str.length) { | |
| 887 | errno = EINVAL; | |
| 888 | return -1; | |
| 889 | } | |
| 890 | ||
| 891 | // now parse the number | |
| 892 | unsigned long long result = 0; | |
| 893 | for (size_t i = start; i < str.length; i++) { | |
| 894 | // ignore group separators | |
| 895 | if (strchr(groupsep, str.ptr[i])) continue; | |
| 896 | ||
| 897 | // determine the digit value of the character | |
| 898 | unsigned char c = str.ptr[i]; | |
| 899 | if (c >= 'a') c = 10 + (c - 'a'); | |
| 900 | else if (c >= 'A') c = 10 + (c - 'A'); | |
| 901 | else if (c >= '0') c = c - '0'; | |
| 902 | else c = 255; | |
| 903 | if (c >= base) { | |
| 904 | errno = EINVAL; | |
| 905 | return -1; | |
| 906 | } | |
| 907 | ||
| 908 | // now combine the digit with what we already have | |
| 909 | unsigned long right = (result & 0xff) * base + c; | |
| 910 | unsigned long long left = (result >> 8) * base + (right >> 8); | |
| 911 | if (left > (ULLONG_MAX >> 8)) { | |
| 912 | errno = ERANGE; | |
| 913 | return -1; | |
| 914 | } | |
| 915 | result = (left << 8) + (right & 0xff); | |
| 916 | } | |
| 917 | ||
| 918 | *output = result; | |
| 919 | return 0; | |
| 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_strtou8_lc_(cxstring str, uint8_t *output, int base, const char *groupsep) { |
| 440 | 923 | cx_strtoX_unsigned_impl(uint8_t, UINT8_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_strtou16_lc_(cxstring str, uint16_t *output, int base, const char *groupsep) { |
| 440 | 927 | cx_strtoX_unsigned_impl(uint16_t, UINT16_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_strtou32_lc_(cxstring str, uint32_t *output, int base, const char *groupsep) { |
| 440 | 931 | cx_strtoX_unsigned_impl(uint32_t, UINT32_MAX); |
| 932 | } | |
| 933 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
934 | int cx_strtou64_lc_(cxstring str, uint64_t *output, int base, const char *groupsep) { |
| 440 | 935 | assert(sizeof(unsigned long long) == sizeof(uint64_t)); // should be true on all platforms |
| 936 | return cx_strtoull_lc(str, (unsigned long long*) output, base, groupsep); | |
| 937 | } | |
| 938 | ||
|
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 | int cx_strtoz_lc_(cxstring str, size_t *output, int base, const char *groupsep) { |
| 440 | 940 | #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
|
941 | return cx_strtou32_lc_(str, (uint32_t*) output, base, groupsep); |
| 440 | 942 | #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
|
943 | return cx_strtoull_lc_(str, (unsigned long long *) output, base, groupsep); |
| 440 | 944 | #else |
| 945 | #error "unsupported size_t size" | |
| 946 | #endif | |
| 947 | } | |
| 948 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
949 | int cx_strtof_lc_(cxstring str, float *output, char decsep, const char *groupsep) { |
| 440 | 950 | // use string to double and add a range check |
| 951 | 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
|
952 | int ret = cx_strtod_lc_(str, &d, decsep, groupsep); |
| 440 | 953 | if (ret != 0) return ret; |
| 954 | // note: FLT_MIN is the smallest POSITIVE number that can be represented | |
| 955 | double test = d < 0 ? -d : d; | |
| 956 | if (test < FLT_MIN || test > FLT_MAX) { | |
| 957 | errno = ERANGE; | |
| 958 | return -1; | |
| 959 | } | |
| 960 | *output = (float) d; | |
| 961 | return 0; | |
| 962 | } | |
| 963 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
964 | int cx_strtod_lc_(cxstring str, double *output, char decsep, const char *groupsep) { |
| 440 | 965 | // TODO: overflow check |
| 966 | // TODO: increase precision | |
| 967 | ||
|
471
063a9f29098c
ucx update + fix doc attach/detach + fix ui_set with unbound values
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
440
diff
changeset
|
968 | // emptiness check |
| 440 | 969 | if (str.length == 0) { |
| 970 | errno = EINVAL; | |
| 971 | return -1; | |
| 972 | } | |
| 973 | ||
| 974 | double result = 0.; | |
| 975 | int sign = 1; | |
| 976 | ||
| 977 | // check if there is a sign | |
| 978 | if (str.ptr[0] == '-') { | |
| 979 | sign = -1; | |
| 980 | str.ptr++; | |
| 981 | str.length--; | |
| 982 | } else if (str.ptr[0] == '+') { | |
| 983 | str.ptr++; | |
| 984 | str.length--; | |
| 985 | } | |
| 986 | ||
| 987 | // there must be at least one char to parse | |
| 988 | if (str.length == 0) { | |
| 989 | errno = EINVAL; | |
| 990 | return -1; | |
| 991 | } | |
| 992 | ||
| 993 | // parse all digits until we find the decsep | |
| 994 | size_t pos = 0; | |
| 995 | do { | |
| 845 | 996 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 440 | 997 | result = result * 10 + (str.ptr[pos] - '0'); |
| 998 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 999 | break; | |
| 1000 | } | |
| 1001 | } while (++pos < str.length); | |
| 1002 | ||
| 1003 | // already done? | |
| 1004 | if (pos == str.length) { | |
| 1005 | *output = result * sign; | |
| 1006 | return 0; | |
| 1007 | } | |
| 1008 | ||
| 1009 | // is the next char the decsep? | |
| 1010 | if (str.ptr[pos] == decsep) { | |
| 1011 | pos++; | |
| 1012 | // it may end with the decsep, if it did not start with it | |
| 1013 | if (pos == str.length) { | |
| 1014 | if (str.length == 1) { | |
| 1015 | errno = EINVAL; | |
| 1016 | return -1; | |
| 1017 | } else { | |
| 1018 | *output = result * sign; | |
| 1019 | return 0; | |
| 1020 | } | |
| 1021 | } | |
| 1022 | // parse everything until exponent or end | |
| 1023 | double factor = 1.; | |
| 1024 | do { | |
| 845 | 1025 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 440 | 1026 | factor *= 0.1; |
| 1027 | result = result + factor * (str.ptr[pos] - '0'); | |
| 1028 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 1029 | break; | |
| 1030 | } | |
| 1031 | } while (++pos < str.length); | |
| 1032 | } | |
| 1033 | ||
| 1034 | // no exponent? | |
| 1035 | if (pos == str.length) { | |
| 1036 | *output = result * sign; | |
| 1037 | return 0; | |
| 1038 | } | |
| 1039 | ||
| 1040 | // now the next separator MUST be the exponent separator | |
| 1041 | // and at least one char must follow | |
| 1042 | if ((str.ptr[pos] | 32) != 'e' || str.length <= pos + 1) { | |
| 1043 | errno = EINVAL; | |
| 1044 | return -1; | |
| 1045 | } | |
| 1046 | pos++; | |
| 1047 | ||
| 1048 | // check if we have a sign for the exponent | |
| 1049 | double factor = 10.; | |
| 1050 | if (str.ptr[pos] == '-') { | |
| 1051 | factor = .1; | |
| 1052 | pos++; | |
| 1053 | } else if (str.ptr[pos] == '+') { | |
| 1054 | pos++; | |
| 1055 | } | |
| 1056 | ||
| 1057 | // at least one digit must follow | |
| 1058 | if (pos == str.length) { | |
| 1059 | errno = EINVAL; | |
| 1060 | return -1; | |
| 1061 | } | |
| 1062 | ||
| 1063 | // parse the exponent | |
| 1064 | unsigned int exp = 0; | |
| 1065 | do { | |
| 845 | 1066 | if (isdigit((unsigned char)str.ptr[pos])) { |
| 440 | 1067 | exp = 10 * exp + (str.ptr[pos] - '0'); |
| 1068 | } else if (strchr(groupsep, str.ptr[pos]) == NULL) { | |
| 1069 | errno = EINVAL; | |
| 1070 | return -1; | |
| 1071 | } | |
| 1072 | } while (++pos < str.length); | |
| 1073 | ||
| 1074 | // apply the exponent by fast exponentiation | |
| 1075 | do { | |
| 1076 | if (exp & 1) { | |
| 1077 | result *= factor; | |
| 1078 | } | |
| 1079 | factor *= factor; | |
| 1080 | } while ((exp >>= 1) > 0); | |
| 1081 | ||
| 1082 | // store the result and exit | |
| 1083 | *output = result * sign; | |
| 1084 | return 0; | |
| 1085 | } |