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