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