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