src/string.c

Sat, 28 Oct 2017 15:59:16 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 28 Oct 2017 15:59:16 +0200
changeset 260
a6184aff5108
parent 259
2f5dea574a75
child 270
3d80d425543b
permissions
-rw-r--r--

rename LICENSE to COPYING to be distributed by autoconf

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@68 53 size_t sstrnlen(size_t n, sstr_t s, ...) {
olaf@20 54 va_list ap;
olaf@20 55 size_t size = s.length;
olaf@20 56 va_start(ap, s);
olaf@20 57
universe@116 58 for (size_t i = 1 ; i < n ; i++) {
olaf@20 59 sstr_t str = va_arg(ap, sstr_t);
olaf@20 60 size += str.length;
olaf@20 61 }
universe@24 62 va_end(ap);
olaf@20 63
olaf@20 64 return size;
olaf@20 65 }
olaf@20 66
olaf@180 67 static sstr_t sstrvcat_a(
olaf@180 68 UcxAllocator *a,
olaf@180 69 size_t count,
olaf@180 70 sstr_t s1,
olaf@180 71 sstr_t s2,
olaf@180 72 va_list ap) {
olaf@180 73 sstr_t str;
olaf@180 74 str.ptr = NULL;
olaf@180 75 str.length = 0;
olaf@180 76 if(count < 2) {
olaf@180 77 return str;
olaf@180 78 }
olaf@180 79
universe@185 80 sstr_t *strings = (sstr_t*) calloc(count, sizeof(sstr_t));
olaf@180 81 if(!strings) {
olaf@180 82 return str;
olaf@180 83 }
olaf@180 84
olaf@180 85 // get all args and overall length
olaf@180 86 strings[0] = s1;
olaf@180 87 strings[1] = s2;
olaf@180 88 size_t strlen = s1.length + s2.length;
olaf@180 89 for (size_t i=2;i<count;i++) {
olaf@180 90 sstr_t s = va_arg (ap, sstr_t);
olaf@180 91 strings[i] = s;
olaf@180 92 strlen += s.length;
olaf@180 93 }
olaf@180 94
olaf@180 95 // create new string
universe@185 96 str.ptr = (char*) almalloc(a, strlen + 1);
olaf@180 97 str.length = strlen;
olaf@180 98 if(!str.ptr) {
olaf@180 99 free(strings);
olaf@180 100 str.length = 0;
olaf@180 101 return str;
olaf@180 102 }
olaf@180 103
olaf@180 104 // concatenate strings
olaf@180 105 size_t pos = 0;
olaf@180 106 for (size_t i=0;i<count;i++) {
olaf@180 107 sstr_t s = strings[i];
olaf@180 108 memcpy(str.ptr + pos, s.ptr, s.length);
olaf@180 109 pos += s.length;
olaf@180 110 }
olaf@180 111
olaf@180 112 str.ptr[str.length] = '\0';
olaf@180 113
olaf@180 114 free(strings);
olaf@180 115
olaf@180 116 return str;
olaf@180 117 }
olaf@180 118
olaf@180 119 sstr_t sstrcat(size_t count, sstr_t s1, sstr_t s2, ...) {
olaf@180 120 va_list ap;
olaf@180 121 va_start(ap, s2);
olaf@180 122 sstr_t s = sstrvcat_a(ucx_default_allocator(), count, s1, s2, ap);
olaf@180 123 va_end(ap);
olaf@180 124 return s;
olaf@180 125 }
olaf@180 126
olaf@180 127 sstr_t sstrcat_a(UcxAllocator *a, size_t count, sstr_t s1, sstr_t s2, ...) {
olaf@180 128 va_list ap;
olaf@180 129 va_start(ap, s2);
olaf@180 130 sstr_t s = sstrvcat_a(a, count, s1, s2, ap);
olaf@180 131 va_end(ap);
olaf@180 132 return s;
olaf@180 133 }
olaf@180 134
olaf@68 135 sstr_t sstrsubs(sstr_t s, size_t start) {
olaf@20 136 return sstrsubsl (s, start, s.length-start);
olaf@20 137 }
olaf@20 138
olaf@68 139 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
olaf@20 140 sstr_t new_sstr;
olaf@104 141 if (start >= s.length) {
universe@173 142 new_sstr.ptr = NULL;
universe@173 143 new_sstr.length = 0;
universe@173 144 } else {
universe@173 145 if (length > s.length-start) {
universe@173 146 length = s.length-start;
universe@173 147 }
universe@173 148 new_sstr.ptr = &s.ptr[start];
universe@173 149 new_sstr.length = length;
olaf@20 150 }
olaf@20 151 return new_sstr;
olaf@20 152 }
olaf@20 153
olaf@108 154 sstr_t sstrchr(sstr_t s, int c) {
olaf@108 155 for(size_t i=0;i<s.length;i++) {
olaf@108 156 if(s.ptr[i] == c) {
olaf@108 157 return sstrsubs(s, i);
olaf@108 158 }
olaf@108 159 }
olaf@108 160 sstr_t n;
olaf@108 161 n.ptr = NULL;
olaf@108 162 n.length = 0;
olaf@108 163 return n;
olaf@108 164 }
olaf@108 165
universe@148 166 sstr_t sstrrchr(sstr_t s, int c) {
universe@148 167 if (s.length > 0) {
universe@152 168 for(size_t i=s.length;i>0;i--) {
universe@152 169 if(s.ptr[i-1] == c) {
universe@152 170 return sstrsubs(s, i-1);
universe@148 171 }
universe@148 172 }
universe@148 173 }
universe@148 174 sstr_t n;
universe@148 175 n.ptr = NULL;
universe@148 176 n.length = 0;
universe@148 177 return n;
universe@148 178 }
universe@148 179
universe@237 180 #define ptable_r(dest, useheap, ptable, index) (dest = useheap ? \
universe@237 181 ((size_t*)ptable)[index] : (size_t) ((uint8_t*)ptable)[index])
universe@236 182
universe@237 183 #define ptable_w(useheap, ptable, index, src) do {\
universe@237 184 if (!useheap) ((uint8_t*)ptable)[index] = (uint8_t) src;\
universe@237 185 else ((size_t*)ptable)[index] = src;\
universe@237 186 } while (0);
universe@236 187
universe@214 188 sstr_t sstrstr(sstr_t string, sstr_t match) {
universe@214 189 if (match.length == 0) {
universe@214 190 return string;
universe@214 191 }
universe@214 192
universe@236 193 /* prepare default return value in case of no match */
universe@236 194 sstr_t result = sstrn(NULL, 0);
universe@236 195
universe@236 196 /*
universe@236 197 * IMPORTANT:
universe@236 198 * our prefix table contains the prefix length PLUS ONE
universe@236 199 * this is our decision, because we want to use the full range of size_t
universe@236 200 * the original algorithm needs a (-1) at one single place
universe@236 201 * and we want to avoid that
universe@236 202 */
universe@236 203
universe@236 204 /* static prefix table */
universe@236 205 static uint8_t s_prefix_table[256];
universe@236 206
universe@236 207 /* check pattern length and use appropriate prefix table */
universe@237 208 /* if the pattern exceeds static prefix table, allocate on the heap */
universe@237 209 register int useheap = match.length > 255;
universe@237 210 register void* ptable = useheap ?
universe@237 211 calloc(match.length+1, sizeof(size_t)): s_prefix_table;
universe@236 212
universe@236 213 /* keep counter in registers */
universe@236 214 register size_t i, j;
universe@236 215
universe@236 216 /* fill prefix table */
universe@236 217 i = 0; j = 0;
universe@237 218 ptable_w(useheap, ptable, i, j);
universe@236 219 while (i < match.length) {
universe@236 220 while (j >= 1 && match.ptr[j-1] != match.ptr[i]) {
universe@238 221 ptable_r(j, useheap, ptable, j-1);
universe@236 222 }
universe@236 223 i++; j++;
universe@237 224 ptable_w(useheap, ptable, i, j);
universe@236 225 }
universe@236 226
universe@236 227 /* search */
universe@236 228 i = 0; j = 1;
universe@236 229 while (i < string.length) {
universe@236 230 while (j >= 1 && string.ptr[i] != match.ptr[j-1]) {
universe@237 231 ptable_r(j, useheap, ptable, j-1);
universe@236 232 }
universe@236 233 i++; j++;
universe@236 234 if (j-1 == match.length) {
universe@236 235 size_t start = i - match.length;
universe@236 236 result.ptr = string.ptr + start;
universe@236 237 result.length = string.length - start;
universe@236 238 break;
universe@214 239 }
universe@214 240 }
universe@236 241
universe@236 242 /* if prefix table was allocated on the heap, free it */
universe@236 243 if (ptable != s_prefix_table) {
universe@236 244 free(ptable);
universe@236 245 }
universe@214 246
universe@236 247 return result;
universe@214 248 }
universe@214 249
universe@237 250 #undef ptable_r
universe@237 251 #undef ptable_w
universe@237 252
universe@173 253 sstr_t* sstrsplit(sstr_t s, sstr_t d, ssize_t *n) {
universe@125 254 return sstrsplit_a(ucx_default_allocator(), s, d, n);
universe@119 255 }
universe@119 256
universe@173 257 sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, ssize_t *n) {
universe@119 258 if (s.length == 0 || d.length == 0) {
universe@119 259 *n = -1;
universe@39 260 return NULL;
universe@39 261 }
universe@231 262
universe@231 263 /* special cases: delimiter is at least as large as the string */
universe@231 264 if (d.length >= s.length) {
universe@231 265 /* exact match */
universe@231 266 if (sstrcmp(s, d) == 0) {
universe@231 267 *n = 0;
universe@231 268 return NULL;
universe@231 269 } else /* no match possible */ {
universe@231 270 *n = 1;
universe@231 271 sstr_t *result = (sstr_t*) almalloc(allocator, sizeof(sstr_t));
universe@233 272 *result = sstrdup_a(allocator, s);
universe@231 273 return result;
universe@231 274 }
universe@231 275 }
universe@231 276
universe@173 277 ssize_t nmax = *n;
universe@235 278 size_t arrlen = 16;
universe@235 279 sstr_t* result = (sstr_t*) almalloc(allocator, arrlen*sizeof(sstr_t));
universe@39 280
universe@119 281 if (result) {
universe@233 282 sstr_t curpos = s;
universe@233 283 ssize_t j = 1;
universe@233 284 while (1) {
universe@234 285 sstr_t match;
universe@234 286 /* optimize for one byte delimiters */
universe@234 287 if (d.length == 1) {
universe@234 288 match = curpos;
universe@234 289 for (size_t i = 0 ; i < curpos.length ; i++) {
universe@234 290 if (curpos.ptr[i] == *(d.ptr)) {
universe@234 291 match.ptr = curpos.ptr + i;
universe@234 292 break;
universe@234 293 }
universe@234 294 match.length--;
universe@234 295 }
universe@234 296 } else {
universe@234 297 match = sstrstr(curpos, d);
universe@234 298 }
universe@233 299 if (match.length > 0) {
universe@233 300 /* is this our last try? */
universe@233 301 if (nmax == 0 || j < nmax) {
universe@233 302 /* copy the current string to the array */
universe@233 303 sstr_t item = sstrn(curpos.ptr, match.ptr - curpos.ptr);
universe@233 304 result[j-1] = sstrdup_a(allocator, item);
universe@233 305 size_t processed = item.length + d.length;
universe@233 306 curpos.ptr += processed;
universe@233 307 curpos.length -= processed;
universe@39 308
universe@233 309 /* allocate memory for the next string */
universe@233 310 j++;
universe@235 311 if (j > arrlen) {
universe@235 312 arrlen *= 2;
universe@235 313 sstr_t* reallocated = (sstr_t*) alrealloc(
universe@235 314 allocator, result, arrlen*sizeof(sstr_t));
universe@235 315 if (reallocated) {
universe@235 316 result = reallocated;
universe@235 317 } else {
universe@235 318 for (ssize_t i = 0 ; i < j-1 ; i++) {
universe@235 319 alfree(allocator, result[i].ptr);
universe@235 320 }
universe@235 321 alfree(allocator, result);
universe@235 322 *n = -2;
universe@235 323 return NULL;
universe@233 324 }
universe@233 325 }
universe@233 326 } else {
universe@233 327 /* nmax reached, copy the _full_ remaining string */
universe@233 328 result[j-1] = sstrdup_a(allocator, curpos);
universe@233 329 break;
universe@233 330 }
universe@173 331 } else {
universe@233 332 /* no more matches, copy last string */
universe@233 333 result[j-1] = sstrdup_a(allocator, curpos);
universe@173 334 break;
universe@173 335 }
universe@119 336 }
universe@233 337 *n = j;
universe@119 338 } else {
universe@119 339 *n = -2;
universe@39 340 }
universe@39 341
universe@39 342 return result;
universe@39 343 }
universe@39 344
olaf@68 345 int sstrcmp(sstr_t s1, sstr_t s2) {
universe@116 346 if (s1.length == s2.length) {
universe@116 347 return memcmp(s1.ptr, s2.ptr, s1.length);
universe@116 348 } else if (s1.length > s2.length) {
universe@116 349 return 1;
universe@116 350 } else {
universe@116 351 return -1;
universe@116 352 }
olaf@20 353 }
olaf@20 354
universe@149 355 int sstrcasecmp(sstr_t s1, sstr_t s2) {
universe@149 356 if (s1.length == s2.length) {
universe@149 357 #ifdef _WIN32
universe@149 358 return _strnicmp(s1.ptr, s2.ptr, s1.length);
universe@149 359 #else
universe@149 360 return strncasecmp(s1.ptr, s2.ptr, s1.length);
universe@149 361 #endif
universe@149 362 } else if (s1.length > s2.length) {
universe@149 363 return 1;
universe@149 364 } else {
universe@149 365 return -1;
universe@149 366 }
universe@149 367 }
universe@149 368
olaf@68 369 sstr_t sstrdup(sstr_t s) {
universe@125 370 return sstrdup_a(ucx_default_allocator(), s);
olaf@109 371 }
olaf@20 372
universe@125 373 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
olaf@109 374 sstr_t newstring;
universe@173 375 newstring.ptr = (char*)almalloc(allocator, s.length + 1);
olaf@109 376 if (newstring.ptr) {
olaf@109 377 newstring.length = s.length;
olaf@109 378 newstring.ptr[newstring.length] = 0;
olaf@109 379
olaf@109 380 memcpy(newstring.ptr, s.ptr, s.length);
olaf@109 381 } else {
olaf@109 382 newstring.length = 0;
olaf@109 383 }
olaf@109 384
olaf@20 385 return newstring;
olaf@20 386 }
olaf@96 387
olaf@96 388 sstr_t sstrtrim(sstr_t string) {
olaf@96 389 sstr_t newstr = string;
universe@189 390
universe@189 391 while (newstr.length > 0 && isspace(*newstr.ptr)) {
universe@189 392 newstr.ptr++;
universe@189 393 newstr.length--;
universe@98 394 }
universe@189 395 while (newstr.length > 0 && isspace(newstr.ptr[newstr.length-1])) {
universe@189 396 newstr.length--;
olaf@96 397 }
olaf@96 398
olaf@96 399 return newstr;
olaf@96 400 }
universe@146 401
universe@146 402 int sstrprefix(sstr_t string, sstr_t prefix) {
universe@146 403 if (string.length == 0) {
universe@146 404 return prefix.length == 0;
universe@146 405 }
universe@146 406 if (prefix.length == 0) {
universe@146 407 return 1;
universe@146 408 }
universe@146 409
universe@146 410 if (prefix.length > string.length) {
universe@146 411 return 0;
universe@146 412 } else {
universe@146 413 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0;
universe@146 414 }
universe@146 415 }
universe@146 416
universe@146 417 int sstrsuffix(sstr_t string, sstr_t suffix) {
universe@146 418 if (string.length == 0) {
universe@146 419 return suffix.length == 0;
universe@146 420 }
universe@146 421 if (suffix.length == 0) {
universe@146 422 return 1;
universe@146 423 }
universe@146 424
universe@146 425 if (suffix.length > string.length) {
universe@146 426 return 0;
universe@146 427 } else {
universe@146 428 return memcmp(string.ptr+string.length-suffix.length,
universe@146 429 suffix.ptr, suffix.length) == 0;
universe@146 430 }
universe@146 431 }
universe@210 432
universe@210 433 sstr_t sstrlower(sstr_t string) {
universe@210 434 sstr_t ret = sstrdup(string);
universe@210 435 for (size_t i = 0; i < ret.length ; i++) {
universe@210 436 ret.ptr[i] = tolower(ret.ptr[i]);
universe@210 437 }
universe@210 438 return ret;
universe@210 439 }
universe@210 440
universe@210 441 sstr_t sstrlower_a(UcxAllocator *allocator, sstr_t string) {
universe@210 442 sstr_t ret = sstrdup_a(allocator, string);
universe@210 443 for (size_t i = 0; i < ret.length ; i++) {
universe@210 444 ret.ptr[i] = tolower(ret.ptr[i]);
universe@210 445 }
universe@210 446 return ret;
universe@210 447 }
universe@210 448
universe@210 449 sstr_t sstrupper(sstr_t string) {
universe@210 450 sstr_t ret = sstrdup(string);
universe@210 451 for (size_t i = 0; i < ret.length ; i++) {
universe@210 452 ret.ptr[i] = toupper(ret.ptr[i]);
universe@210 453 }
universe@210 454 return ret;
universe@210 455 }
universe@210 456
universe@210 457 sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string) {
universe@210 458 sstr_t ret = sstrdup_a(allocator, string);
universe@210 459 for (size_t i = 0; i < ret.length ; i++) {
universe@210 460 ret.ptr[i] = toupper(ret.ptr[i]);
universe@210 461 }
universe@210 462 return ret;
universe@210 463 }

mercurial