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