Sat, 05 Oct 2019 17:07:16 +0200
adds missing include for strncasecmp() to avoid an implicit declaration
olaf@20 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
olaf@20 | 3 | * |
universe@259 | 4 | * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved. |
universe@103 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@103 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
olaf@20 | 27 | */ |
olaf@20 | 28 | |
universe@251 | 29 | #include "ucx/string.h" |
universe@251 | 30 | |
universe@251 | 31 | #include "ucx/allocator.h" |
universe@251 | 32 | |
olaf@20 | 33 | #include <stdlib.h> |
universe@69 | 34 | #include <string.h> |
olaf@20 | 35 | #include <stdarg.h> |
universe@236 | 36 | #include <stdint.h> |
universe@189 | 37 | #include <ctype.h> |
olaf@20 | 38 | |
universe@361 | 39 | #ifndef _WIN32 |
universe@361 | 40 | #include <strings.h> /* for strncasecmp() */ |
universe@361 | 41 | #endif /* _WIN32 */ |
universe@361 | 42 | |
universe@116 | 43 | sstr_t sstr(char *cstring) { |
olaf@20 | 44 | sstr_t string; |
universe@116 | 45 | string.ptr = cstring; |
universe@116 | 46 | string.length = strlen(cstring); |
olaf@20 | 47 | return string; |
olaf@20 | 48 | } |
olaf@20 | 49 | |
universe@116 | 50 | sstr_t sstrn(char *cstring, size_t length) { |
olaf@20 | 51 | sstr_t string; |
universe@116 | 52 | string.ptr = cstring; |
universe@116 | 53 | string.length = length; |
olaf@20 | 54 | return string; |
olaf@20 | 55 | } |
olaf@20 | 56 | |
olaf@275 | 57 | scstr_t scstr(const char *cstring) { |
olaf@275 | 58 | scstr_t string; |
olaf@275 | 59 | string.ptr = cstring; |
olaf@275 | 60 | string.length = strlen(cstring); |
olaf@275 | 61 | return string; |
olaf@275 | 62 | } |
olaf@275 | 63 | |
olaf@275 | 64 | scstr_t scstrn(const char *cstring, size_t length) { |
olaf@275 | 65 | scstr_t string; |
olaf@275 | 66 | string.ptr = cstring; |
olaf@275 | 67 | string.length = length; |
olaf@275 | 68 | return string; |
olaf@275 | 69 | } |
olaf@275 | 70 | |
olaf@275 | 71 | |
universe@319 | 72 | size_t scstrnlen(size_t n, ...) { |
olaf@20 | 73 | va_list ap; |
olaf@288 | 74 | va_start(ap, n); |
olaf@288 | 75 | |
olaf@288 | 76 | size_t size = 0; |
olaf@20 | 77 | |
olaf@288 | 78 | for (size_t i = 0 ; i < n ; i++) { |
olaf@288 | 79 | scstr_t str = va_arg(ap, scstr_t); |
universe@317 | 80 | if(SIZE_MAX - str.length < size) { |
universe@317 | 81 | size = SIZE_MAX; |
olaf@272 | 82 | break; |
olaf@272 | 83 | } |
olaf@20 | 84 | size += str.length; |
olaf@20 | 85 | } |
universe@24 | 86 | va_end(ap); |
olaf@20 | 87 | |
olaf@20 | 88 | return size; |
olaf@20 | 89 | } |
olaf@20 | 90 | |
olaf@180 | 91 | static sstr_t sstrvcat_a( |
olaf@180 | 92 | UcxAllocator *a, |
olaf@180 | 93 | size_t count, |
olaf@288 | 94 | scstr_t s1, |
olaf@180 | 95 | va_list ap) { |
olaf@180 | 96 | sstr_t str; |
olaf@180 | 97 | str.ptr = NULL; |
olaf@180 | 98 | str.length = 0; |
olaf@180 | 99 | if(count < 2) { |
olaf@180 | 100 | return str; |
olaf@180 | 101 | } |
olaf@180 | 102 | |
olaf@288 | 103 | scstr_t s2 = va_arg (ap, scstr_t); |
olaf@288 | 104 | |
olaf@272 | 105 | if(((size_t)-1) - s1.length < s2.length) { |
olaf@272 | 106 | return str; |
olaf@272 | 107 | } |
olaf@272 | 108 | |
olaf@288 | 109 | scstr_t *strings = (scstr_t*) calloc(count, sizeof(scstr_t)); |
olaf@180 | 110 | if(!strings) { |
olaf@180 | 111 | return str; |
olaf@180 | 112 | } |
olaf@180 | 113 | |
olaf@180 | 114 | // get all args and overall length |
olaf@180 | 115 | strings[0] = s1; |
olaf@180 | 116 | strings[1] = s2; |
olaf@272 | 117 | size_t slen = s1.length + s2.length; |
olaf@272 | 118 | int error = 0; |
olaf@180 | 119 | for (size_t i=2;i<count;i++) { |
olaf@288 | 120 | scstr_t s = va_arg (ap, scstr_t); |
olaf@180 | 121 | strings[i] = s; |
olaf@272 | 122 | if(((size_t)-1) - s.length < slen) { |
olaf@272 | 123 | error = 1; |
olaf@272 | 124 | break; |
olaf@272 | 125 | } |
olaf@272 | 126 | slen += s.length; |
olaf@272 | 127 | } |
olaf@272 | 128 | if(error) { |
olaf@272 | 129 | free(strings); |
olaf@272 | 130 | return str; |
olaf@180 | 131 | } |
olaf@180 | 132 | |
olaf@180 | 133 | // create new string |
olaf@272 | 134 | str.ptr = (char*) almalloc(a, slen + 1); |
olaf@272 | 135 | str.length = slen; |
olaf@180 | 136 | if(!str.ptr) { |
olaf@180 | 137 | free(strings); |
olaf@180 | 138 | str.length = 0; |
olaf@180 | 139 | return str; |
olaf@180 | 140 | } |
olaf@180 | 141 | |
olaf@180 | 142 | // concatenate strings |
olaf@180 | 143 | size_t pos = 0; |
olaf@180 | 144 | for (size_t i=0;i<count;i++) { |
olaf@288 | 145 | scstr_t s = strings[i]; |
olaf@180 | 146 | memcpy(str.ptr + pos, s.ptr, s.length); |
olaf@180 | 147 | pos += s.length; |
olaf@180 | 148 | } |
olaf@180 | 149 | |
olaf@180 | 150 | str.ptr[str.length] = '\0'; |
olaf@180 | 151 | |
olaf@180 | 152 | free(strings); |
olaf@180 | 153 | |
olaf@180 | 154 | return str; |
olaf@180 | 155 | } |
olaf@180 | 156 | |
universe@319 | 157 | sstr_t scstrcat(size_t count, scstr_t s1, ...) { |
olaf@180 | 158 | va_list ap; |
olaf@288 | 159 | va_start(ap, s1); |
olaf@288 | 160 | sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, ap); |
olaf@180 | 161 | va_end(ap); |
olaf@180 | 162 | return s; |
olaf@180 | 163 | } |
olaf@180 | 164 | |
universe@319 | 165 | sstr_t scstrcat_a(UcxAllocator *a, size_t count, scstr_t s1, ...) { |
olaf@180 | 166 | va_list ap; |
olaf@288 | 167 | va_start(ap, s1); |
olaf@288 | 168 | sstr_t s = sstrvcat_a(a, count, s1, ap); |
olaf@180 | 169 | va_end(ap); |
olaf@180 | 170 | return s; |
olaf@180 | 171 | } |
olaf@180 | 172 | |
olaf@300 | 173 | static int ucx_substring( |
olaf@300 | 174 | size_t str_length, |
olaf@300 | 175 | size_t start, |
olaf@300 | 176 | size_t length, |
olaf@300 | 177 | size_t *newlen, |
olaf@300 | 178 | size_t *newpos) |
olaf@300 | 179 | { |
olaf@300 | 180 | *newlen = 0; |
olaf@300 | 181 | *newpos = 0; |
olaf@300 | 182 | |
olaf@300 | 183 | if(start > str_length) { |
olaf@300 | 184 | return 0; |
olaf@300 | 185 | } |
olaf@300 | 186 | |
olaf@300 | 187 | if(length > str_length - start) { |
olaf@300 | 188 | length = str_length - start; |
olaf@300 | 189 | } |
olaf@300 | 190 | *newlen = length; |
olaf@300 | 191 | *newpos = start; |
olaf@300 | 192 | return 1; |
olaf@300 | 193 | } |
olaf@300 | 194 | |
olaf@68 | 195 | sstr_t sstrsubs(sstr_t s, size_t start) { |
olaf@20 | 196 | return sstrsubsl (s, start, s.length-start); |
olaf@20 | 197 | } |
olaf@20 | 198 | |
olaf@68 | 199 | sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) { |
olaf@300 | 200 | size_t pos; |
olaf@300 | 201 | sstr_t ret = { NULL, 0 }; |
olaf@300 | 202 | if(ucx_substring(s.length, start, length, &ret.length, &pos)) { |
olaf@300 | 203 | ret.ptr = s.ptr + pos; |
olaf@300 | 204 | } |
olaf@300 | 205 | return ret; |
olaf@300 | 206 | } |
olaf@300 | 207 | |
universe@318 | 208 | scstr_t scstrsubs(scstr_t string, size_t start) { |
universe@318 | 209 | return scstrsubsl(string, start, string.length-start); |
olaf@300 | 210 | } |
olaf@300 | 211 | |
olaf@300 | 212 | scstr_t scstrsubsl(scstr_t s, size_t start, size_t length) { |
olaf@300 | 213 | size_t pos; |
olaf@300 | 214 | scstr_t ret = { NULL, 0 }; |
olaf@300 | 215 | if(ucx_substring(s.length, start, length, &ret.length, &pos)) { |
olaf@300 | 216 | ret.ptr = s.ptr + pos; |
olaf@300 | 217 | } |
olaf@300 | 218 | return ret; |
olaf@300 | 219 | } |
olaf@300 | 220 | |
olaf@300 | 221 | |
universe@318 | 222 | static int ucx_strchr(const char *str, size_t length, int chr, size_t *pos) { |
olaf@300 | 223 | for(size_t i=0;i<length;i++) { |
universe@318 | 224 | if(str[i] == chr) { |
olaf@300 | 225 | *pos = i; |
olaf@300 | 226 | return 1; |
universe@173 | 227 | } |
olaf@20 | 228 | } |
olaf@300 | 229 | return 0; |
olaf@300 | 230 | } |
olaf@300 | 231 | |
universe@318 | 232 | static int ucx_strrchr(const char *str, size_t length, int chr, size_t *pos) { |
olaf@300 | 233 | if(length > 0) { |
universe@306 | 234 | for(size_t i=length ; i>0 ; i--) { |
universe@318 | 235 | if(str[i-1] == chr) { |
universe@306 | 236 | *pos = i-1; |
olaf@300 | 237 | return 1; |
olaf@300 | 238 | } |
olaf@300 | 239 | } |
olaf@300 | 240 | } |
olaf@300 | 241 | return 0; |
olaf@20 | 242 | } |
olaf@20 | 243 | |
olaf@108 | 244 | sstr_t sstrchr(sstr_t s, int c) { |
olaf@300 | 245 | size_t pos = 0; |
olaf@300 | 246 | if(ucx_strchr(s.ptr, s.length, c, &pos)) { |
olaf@300 | 247 | return sstrsubs(s, pos); |
olaf@108 | 248 | } |
olaf@300 | 249 | return sstrn(NULL, 0); |
olaf@108 | 250 | } |
olaf@108 | 251 | |
universe@148 | 252 | sstr_t sstrrchr(sstr_t s, int c) { |
olaf@300 | 253 | size_t pos = 0; |
olaf@300 | 254 | if(ucx_strrchr(s.ptr, s.length, c, &pos)) { |
olaf@300 | 255 | return sstrsubs(s, pos); |
universe@148 | 256 | } |
olaf@300 | 257 | return sstrn(NULL, 0); |
olaf@300 | 258 | } |
olaf@300 | 259 | |
olaf@300 | 260 | scstr_t scstrchr(scstr_t s, int c) { |
olaf@300 | 261 | size_t pos = 0; |
olaf@300 | 262 | if(ucx_strchr(s.ptr, s.length, c, &pos)) { |
olaf@300 | 263 | return scstrsubs(s, pos); |
olaf@300 | 264 | } |
olaf@300 | 265 | return scstrn(NULL, 0); |
olaf@300 | 266 | } |
olaf@300 | 267 | |
olaf@300 | 268 | scstr_t scstrrchr(scstr_t s, int c) { |
olaf@300 | 269 | size_t pos = 0; |
olaf@300 | 270 | if(ucx_strrchr(s.ptr, s.length, c, &pos)) { |
olaf@300 | 271 | return scstrsubs(s, pos); |
olaf@300 | 272 | } |
olaf@300 | 273 | return scstrn(NULL, 0); |
universe@148 | 274 | } |
universe@148 | 275 | |
universe@237 | 276 | #define ptable_r(dest, useheap, ptable, index) (dest = useheap ? \ |
universe@237 | 277 | ((size_t*)ptable)[index] : (size_t) ((uint8_t*)ptable)[index]) |
universe@236 | 278 | |
universe@237 | 279 | #define ptable_w(useheap, ptable, index, src) do {\ |
universe@237 | 280 | if (!useheap) ((uint8_t*)ptable)[index] = (uint8_t) src;\ |
universe@237 | 281 | else ((size_t*)ptable)[index] = src;\ |
universe@237 | 282 | } while (0); |
universe@236 | 283 | |
olaf@276 | 284 | |
universe@318 | 285 | static const char* ucx_strstr( |
olaf@276 | 286 | const char *str, |
olaf@276 | 287 | size_t length, |
olaf@276 | 288 | const char *match, |
olaf@276 | 289 | size_t matchlen, |
olaf@276 | 290 | size_t *newlen) |
olaf@276 | 291 | { |
olaf@276 | 292 | *newlen = length; |
olaf@276 | 293 | if (matchlen == 0) { |
olaf@276 | 294 | return str; |
universe@214 | 295 | } |
universe@214 | 296 | |
olaf@276 | 297 | const char *result = NULL; |
olaf@276 | 298 | size_t resultlen = 0; |
universe@236 | 299 | |
universe@236 | 300 | /* |
universe@236 | 301 | * IMPORTANT: |
universe@236 | 302 | * our prefix table contains the prefix length PLUS ONE |
universe@236 | 303 | * this is our decision, because we want to use the full range of size_t |
universe@236 | 304 | * the original algorithm needs a (-1) at one single place |
universe@236 | 305 | * and we want to avoid that |
universe@236 | 306 | */ |
universe@236 | 307 | |
universe@236 | 308 | /* static prefix table */ |
universe@236 | 309 | static uint8_t s_prefix_table[256]; |
universe@236 | 310 | |
universe@236 | 311 | /* check pattern length and use appropriate prefix table */ |
universe@237 | 312 | /* if the pattern exceeds static prefix table, allocate on the heap */ |
olaf@276 | 313 | register int useheap = matchlen > 255; |
universe@237 | 314 | register void* ptable = useheap ? |
olaf@276 | 315 | calloc(matchlen+1, sizeof(size_t)): s_prefix_table; |
universe@236 | 316 | |
universe@236 | 317 | /* keep counter in registers */ |
universe@236 | 318 | register size_t i, j; |
universe@236 | 319 | |
universe@236 | 320 | /* fill prefix table */ |
universe@236 | 321 | i = 0; j = 0; |
universe@237 | 322 | ptable_w(useheap, ptable, i, j); |
olaf@276 | 323 | while (i < matchlen) { |
olaf@276 | 324 | while (j >= 1 && match[j-1] != match[i]) { |
universe@238 | 325 | ptable_r(j, useheap, ptable, j-1); |
universe@236 | 326 | } |
universe@236 | 327 | i++; j++; |
universe@237 | 328 | ptable_w(useheap, ptable, i, j); |
universe@236 | 329 | } |
universe@236 | 330 | |
universe@236 | 331 | /* search */ |
universe@236 | 332 | i = 0; j = 1; |
olaf@276 | 333 | while (i < length) { |
olaf@276 | 334 | while (j >= 1 && str[i] != match[j-1]) { |
universe@237 | 335 | ptable_r(j, useheap, ptable, j-1); |
universe@236 | 336 | } |
universe@236 | 337 | i++; j++; |
olaf@276 | 338 | if (j-1 == matchlen) { |
olaf@276 | 339 | size_t start = i - matchlen; |
olaf@276 | 340 | result = str + start; |
olaf@276 | 341 | resultlen = length - start; |
universe@236 | 342 | break; |
universe@214 | 343 | } |
universe@214 | 344 | } |
universe@236 | 345 | |
universe@236 | 346 | /* if prefix table was allocated on the heap, free it */ |
universe@236 | 347 | if (ptable != s_prefix_table) { |
universe@236 | 348 | free(ptable); |
universe@236 | 349 | } |
universe@214 | 350 | |
olaf@276 | 351 | *newlen = resultlen; |
olaf@276 | 352 | return result; |
olaf@276 | 353 | } |
olaf@276 | 354 | |
universe@319 | 355 | sstr_t scstrsstr(sstr_t string, scstr_t match) { |
olaf@276 | 356 | sstr_t result; |
olaf@276 | 357 | |
olaf@276 | 358 | size_t reslen; |
olaf@276 | 359 | const char *resstr = ucx_strstr(string.ptr, string.length, match.ptr, match.length, &reslen); |
olaf@276 | 360 | if(!resstr) { |
olaf@276 | 361 | result.ptr = NULL; |
olaf@276 | 362 | result.length = 0; |
olaf@276 | 363 | return result; |
olaf@276 | 364 | } |
olaf@276 | 365 | |
olaf@276 | 366 | size_t pos = resstr - string.ptr; |
olaf@276 | 367 | result.ptr = string.ptr + pos; |
olaf@276 | 368 | result.length = reslen; |
olaf@276 | 369 | |
olaf@276 | 370 | return result; |
olaf@276 | 371 | } |
olaf@276 | 372 | |
universe@319 | 373 | scstr_t scstrscstr(scstr_t string, scstr_t match) { |
olaf@276 | 374 | scstr_t result; |
olaf@276 | 375 | |
olaf@276 | 376 | size_t reslen; |
olaf@276 | 377 | const char *resstr = ucx_strstr(string.ptr, string.length, match.ptr, match.length, &reslen); |
olaf@276 | 378 | if(!resstr) { |
olaf@276 | 379 | result.ptr = NULL; |
olaf@276 | 380 | result.length = 0; |
olaf@276 | 381 | return result; |
olaf@276 | 382 | } |
olaf@276 | 383 | |
olaf@276 | 384 | size_t pos = resstr - string.ptr; |
olaf@276 | 385 | result.ptr = string.ptr + pos; |
olaf@276 | 386 | result.length = reslen; |
olaf@276 | 387 | |
universe@236 | 388 | return result; |
universe@214 | 389 | } |
universe@214 | 390 | |
universe@237 | 391 | #undef ptable_r |
universe@237 | 392 | #undef ptable_w |
universe@237 | 393 | |
universe@319 | 394 | sstr_t* scstrsplit(scstr_t s, scstr_t d, ssize_t *n) { |
universe@319 | 395 | return scstrsplit_a(ucx_default_allocator(), s, d, n); |
universe@119 | 396 | } |
universe@119 | 397 | |
universe@319 | 398 | sstr_t* scstrsplit_a(UcxAllocator *allocator, scstr_t s, scstr_t d, ssize_t *n) { |
universe@119 | 399 | if (s.length == 0 || d.length == 0) { |
universe@119 | 400 | *n = -1; |
universe@39 | 401 | return NULL; |
universe@39 | 402 | } |
universe@231 | 403 | |
universe@231 | 404 | /* special cases: delimiter is at least as large as the string */ |
universe@231 | 405 | if (d.length >= s.length) { |
universe@231 | 406 | /* exact match */ |
universe@231 | 407 | if (sstrcmp(s, d) == 0) { |
universe@231 | 408 | *n = 0; |
universe@231 | 409 | return NULL; |
universe@231 | 410 | } else /* no match possible */ { |
universe@231 | 411 | *n = 1; |
universe@231 | 412 | sstr_t *result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)); |
olaf@270 | 413 | if(result) { |
olaf@270 | 414 | *result = sstrdup_a(allocator, s); |
olaf@270 | 415 | } else { |
olaf@270 | 416 | *n = -2; |
olaf@270 | 417 | } |
universe@231 | 418 | return result; |
universe@231 | 419 | } |
universe@231 | 420 | } |
universe@231 | 421 | |
universe@173 | 422 | ssize_t nmax = *n; |
universe@235 | 423 | size_t arrlen = 16; |
olaf@270 | 424 | sstr_t* result = (sstr_t*) alcalloc(allocator, arrlen, sizeof(sstr_t)); |
universe@39 | 425 | |
universe@119 | 426 | if (result) { |
olaf@276 | 427 | scstr_t curpos = s; |
universe@233 | 428 | ssize_t j = 1; |
universe@233 | 429 | while (1) { |
olaf@276 | 430 | scstr_t match; |
universe@234 | 431 | /* optimize for one byte delimiters */ |
universe@234 | 432 | if (d.length == 1) { |
universe@234 | 433 | match = curpos; |
universe@234 | 434 | for (size_t i = 0 ; i < curpos.length ; i++) { |
universe@234 | 435 | if (curpos.ptr[i] == *(d.ptr)) { |
universe@234 | 436 | match.ptr = curpos.ptr + i; |
universe@234 | 437 | break; |
universe@234 | 438 | } |
universe@234 | 439 | match.length--; |
universe@234 | 440 | } |
universe@234 | 441 | } else { |
universe@319 | 442 | match = scstrscstr(curpos, d); |
universe@234 | 443 | } |
universe@233 | 444 | if (match.length > 0) { |
universe@233 | 445 | /* is this our last try? */ |
universe@233 | 446 | if (nmax == 0 || j < nmax) { |
universe@233 | 447 | /* copy the current string to the array */ |
olaf@276 | 448 | scstr_t item = scstrn(curpos.ptr, match.ptr - curpos.ptr); |
universe@233 | 449 | result[j-1] = sstrdup_a(allocator, item); |
universe@233 | 450 | size_t processed = item.length + d.length; |
universe@233 | 451 | curpos.ptr += processed; |
universe@233 | 452 | curpos.length -= processed; |
universe@39 | 453 | |
universe@233 | 454 | /* allocate memory for the next string */ |
universe@233 | 455 | j++; |
universe@235 | 456 | if (j > arrlen) { |
universe@235 | 457 | arrlen *= 2; |
olaf@270 | 458 | size_t reallocsz; |
olaf@270 | 459 | sstr_t* reallocated = NULL; |
olaf@270 | 460 | if(!ucx_szmul(arrlen, sizeof(sstr_t), &reallocsz)) { |
olaf@270 | 461 | reallocated = (sstr_t*) alrealloc( |
olaf@270 | 462 | allocator, result, reallocsz); |
olaf@270 | 463 | } |
universe@235 | 464 | if (reallocated) { |
universe@235 | 465 | result = reallocated; |
universe@235 | 466 | } else { |
universe@235 | 467 | for (ssize_t i = 0 ; i < j-1 ; i++) { |
universe@235 | 468 | alfree(allocator, result[i].ptr); |
universe@235 | 469 | } |
universe@235 | 470 | alfree(allocator, result); |
universe@235 | 471 | *n = -2; |
universe@235 | 472 | return NULL; |
universe@233 | 473 | } |
universe@233 | 474 | } |
universe@233 | 475 | } else { |
universe@233 | 476 | /* nmax reached, copy the _full_ remaining string */ |
universe@233 | 477 | result[j-1] = sstrdup_a(allocator, curpos); |
universe@233 | 478 | break; |
universe@233 | 479 | } |
universe@173 | 480 | } else { |
universe@233 | 481 | /* no more matches, copy last string */ |
universe@233 | 482 | result[j-1] = sstrdup_a(allocator, curpos); |
universe@173 | 483 | break; |
universe@173 | 484 | } |
universe@119 | 485 | } |
universe@233 | 486 | *n = j; |
universe@119 | 487 | } else { |
universe@119 | 488 | *n = -2; |
universe@39 | 489 | } |
universe@39 | 490 | |
universe@39 | 491 | return result; |
universe@39 | 492 | } |
universe@39 | 493 | |
universe@319 | 494 | int scstrcmp(scstr_t s1, scstr_t s2) { |
universe@116 | 495 | if (s1.length == s2.length) { |
universe@116 | 496 | return memcmp(s1.ptr, s2.ptr, s1.length); |
universe@116 | 497 | } else if (s1.length > s2.length) { |
universe@116 | 498 | return 1; |
universe@116 | 499 | } else { |
universe@116 | 500 | return -1; |
universe@116 | 501 | } |
olaf@20 | 502 | } |
olaf@20 | 503 | |
universe@319 | 504 | int scstrcasecmp(scstr_t s1, scstr_t s2) { |
universe@149 | 505 | if (s1.length == s2.length) { |
universe@149 | 506 | #ifdef _WIN32 |
universe@149 | 507 | return _strnicmp(s1.ptr, s2.ptr, s1.length); |
universe@149 | 508 | #else |
universe@149 | 509 | return strncasecmp(s1.ptr, s2.ptr, s1.length); |
universe@149 | 510 | #endif |
universe@149 | 511 | } else if (s1.length > s2.length) { |
universe@149 | 512 | return 1; |
universe@149 | 513 | } else { |
universe@149 | 514 | return -1; |
universe@149 | 515 | } |
universe@149 | 516 | } |
universe@149 | 517 | |
universe@319 | 518 | sstr_t scstrdup(scstr_t s) { |
universe@125 | 519 | return sstrdup_a(ucx_default_allocator(), s); |
olaf@109 | 520 | } |
olaf@20 | 521 | |
universe@319 | 522 | sstr_t scstrdup_a(UcxAllocator *allocator, scstr_t s) { |
olaf@109 | 523 | sstr_t newstring; |
universe@173 | 524 | newstring.ptr = (char*)almalloc(allocator, s.length + 1); |
olaf@109 | 525 | if (newstring.ptr) { |
olaf@109 | 526 | newstring.length = s.length; |
olaf@109 | 527 | newstring.ptr[newstring.length] = 0; |
olaf@109 | 528 | |
olaf@109 | 529 | memcpy(newstring.ptr, s.ptr, s.length); |
olaf@109 | 530 | } else { |
olaf@109 | 531 | newstring.length = 0; |
olaf@109 | 532 | } |
olaf@109 | 533 | |
olaf@20 | 534 | return newstring; |
olaf@20 | 535 | } |
olaf@96 | 536 | |
olaf@276 | 537 | |
universe@318 | 538 | static size_t ucx_strtrim(const char *s, size_t len, size_t *newlen) { |
olaf@276 | 539 | const char *newptr = s; |
olaf@276 | 540 | size_t length = len; |
universe@189 | 541 | |
olaf@276 | 542 | while(length > 0 && isspace(*newptr)) { |
olaf@276 | 543 | newptr++; |
olaf@276 | 544 | length--; |
universe@98 | 545 | } |
olaf@276 | 546 | while(length > 0 && isspace(newptr[length-1])) { |
olaf@276 | 547 | length--; |
olaf@96 | 548 | } |
olaf@96 | 549 | |
olaf@276 | 550 | *newlen = length; |
olaf@276 | 551 | return newptr - s; |
olaf@276 | 552 | } |
olaf@276 | 553 | |
olaf@276 | 554 | sstr_t sstrtrim(sstr_t string) { |
olaf@276 | 555 | sstr_t newstr; |
olaf@276 | 556 | newstr.ptr = string.ptr |
olaf@276 | 557 | + ucx_strtrim(string.ptr, string.length, &newstr.length); |
olaf@276 | 558 | return newstr; |
olaf@276 | 559 | } |
olaf@276 | 560 | |
olaf@276 | 561 | scstr_t scstrtrim(scstr_t string) { |
olaf@276 | 562 | scstr_t newstr; |
olaf@276 | 563 | newstr.ptr = string.ptr |
olaf@276 | 564 | + ucx_strtrim(string.ptr, string.length, &newstr.length); |
olaf@96 | 565 | return newstr; |
olaf@96 | 566 | } |
universe@146 | 567 | |
universe@319 | 568 | int scstrprefix(scstr_t string, scstr_t prefix) { |
universe@146 | 569 | if (string.length == 0) { |
universe@146 | 570 | return prefix.length == 0; |
universe@146 | 571 | } |
universe@146 | 572 | if (prefix.length == 0) { |
universe@146 | 573 | return 1; |
universe@146 | 574 | } |
universe@146 | 575 | |
universe@146 | 576 | if (prefix.length > string.length) { |
universe@146 | 577 | return 0; |
universe@146 | 578 | } else { |
universe@146 | 579 | return memcmp(string.ptr, prefix.ptr, prefix.length) == 0; |
universe@146 | 580 | } |
universe@146 | 581 | } |
universe@146 | 582 | |
universe@319 | 583 | int scstrsuffix(scstr_t string, scstr_t suffix) { |
universe@146 | 584 | if (string.length == 0) { |
universe@146 | 585 | return suffix.length == 0; |
universe@146 | 586 | } |
universe@146 | 587 | if (suffix.length == 0) { |
universe@146 | 588 | return 1; |
universe@146 | 589 | } |
universe@146 | 590 | |
universe@146 | 591 | if (suffix.length > string.length) { |
universe@146 | 592 | return 0; |
universe@146 | 593 | } else { |
universe@146 | 594 | return memcmp(string.ptr+string.length-suffix.length, |
universe@146 | 595 | suffix.ptr, suffix.length) == 0; |
universe@146 | 596 | } |
universe@146 | 597 | } |
universe@210 | 598 | |
universe@319 | 599 | sstr_t scstrlower(scstr_t string) { |
universe@210 | 600 | sstr_t ret = sstrdup(string); |
universe@210 | 601 | for (size_t i = 0; i < ret.length ; i++) { |
universe@210 | 602 | ret.ptr[i] = tolower(ret.ptr[i]); |
universe@210 | 603 | } |
universe@210 | 604 | return ret; |
universe@210 | 605 | } |
universe@210 | 606 | |
universe@319 | 607 | sstr_t scstrlower_a(UcxAllocator *allocator, scstr_t string) { |
universe@210 | 608 | sstr_t ret = sstrdup_a(allocator, string); |
universe@210 | 609 | for (size_t i = 0; i < ret.length ; i++) { |
universe@210 | 610 | ret.ptr[i] = tolower(ret.ptr[i]); |
universe@210 | 611 | } |
universe@210 | 612 | return ret; |
universe@210 | 613 | } |
universe@210 | 614 | |
universe@319 | 615 | sstr_t scstrupper(scstr_t string) { |
universe@210 | 616 | sstr_t ret = sstrdup(string); |
universe@210 | 617 | for (size_t i = 0; i < ret.length ; i++) { |
universe@210 | 618 | ret.ptr[i] = toupper(ret.ptr[i]); |
universe@210 | 619 | } |
universe@210 | 620 | return ret; |
universe@210 | 621 | } |
universe@210 | 622 | |
universe@319 | 623 | sstr_t scstrupper_a(UcxAllocator *allocator, scstr_t string) { |
universe@210 | 624 | sstr_t ret = sstrdup_a(allocator, string); |
universe@210 | 625 | for (size_t i = 0; i < ret.length ; i++) { |
universe@210 | 626 | ret.ptr[i] = toupper(ret.ptr[i]); |
universe@210 | 627 | } |
universe@210 | 628 | return ret; |
universe@210 | 629 | } |
olaf@275 | 630 | |
universe@316 | 631 | // type adjustment functions |
universe@316 | 632 | scstr_t ucx_sc2sc(scstr_t str) { |
universe@316 | 633 | return str; |
olaf@275 | 634 | } |
olaf@275 | 635 | scstr_t ucx_ss2sc(sstr_t str) { |
olaf@275 | 636 | scstr_t cs; |
olaf@275 | 637 | cs.ptr = str.ptr; |
olaf@275 | 638 | cs.length = str.length; |
olaf@275 | 639 | return cs; |
olaf@275 | 640 | } |
olaf@275 | 641 | scstr_t ucx_ss2c_s(scstr_t c) { |
olaf@275 | 642 | return c; |
olaf@275 | 643 | } |