src/string.c

Sun, 21 Jan 2018 10:13:21 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 21 Jan 2018 10:13:21 +0100
changeset 270
3d80d425543b
parent 259
2f5dea574a75
child 272
2def28b65328
permissions
-rw-r--r--

adds integer overflow checks

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));
olaf@270 272 if(result) {
olaf@270 273 *result = sstrdup_a(allocator, s);
olaf@270 274 } else {
olaf@270 275 *n = -2;
olaf@270 276 }
universe@231 277 return result;
universe@231 278 }
universe@231 279 }
universe@231 280
universe@173 281 ssize_t nmax = *n;
universe@235 282 size_t arrlen = 16;
olaf@270 283 sstr_t* result = (sstr_t*) alcalloc(allocator, arrlen, sizeof(sstr_t));
universe@39 284
universe@119 285 if (result) {
universe@233 286 sstr_t curpos = s;
universe@233 287 ssize_t j = 1;
universe@233 288 while (1) {
universe@234 289 sstr_t match;
universe@234 290 /* optimize for one byte delimiters */
universe@234 291 if (d.length == 1) {
universe@234 292 match = curpos;
universe@234 293 for (size_t i = 0 ; i < curpos.length ; i++) {
universe@234 294 if (curpos.ptr[i] == *(d.ptr)) {
universe@234 295 match.ptr = curpos.ptr + i;
universe@234 296 break;
universe@234 297 }
universe@234 298 match.length--;
universe@234 299 }
universe@234 300 } else {
universe@234 301 match = sstrstr(curpos, d);
universe@234 302 }
universe@233 303 if (match.length > 0) {
universe@233 304 /* is this our last try? */
universe@233 305 if (nmax == 0 || j < nmax) {
universe@233 306 /* copy the current string to the array */
universe@233 307 sstr_t item = sstrn(curpos.ptr, match.ptr - curpos.ptr);
universe@233 308 result[j-1] = sstrdup_a(allocator, item);
universe@233 309 size_t processed = item.length + d.length;
universe@233 310 curpos.ptr += processed;
universe@233 311 curpos.length -= processed;
universe@39 312
universe@233 313 /* allocate memory for the next string */
universe@233 314 j++;
universe@235 315 if (j > arrlen) {
universe@235 316 arrlen *= 2;
olaf@270 317 size_t reallocsz;
olaf@270 318 sstr_t* reallocated = NULL;
olaf@270 319 if(!ucx_szmul(arrlen, sizeof(sstr_t), &reallocsz)) {
olaf@270 320 reallocated = (sstr_t*) alrealloc(
olaf@270 321 allocator, result, reallocsz);
olaf@270 322 }
universe@235 323 if (reallocated) {
universe@235 324 result = reallocated;
universe@235 325 } else {
universe@235 326 for (ssize_t i = 0 ; i < j-1 ; i++) {
universe@235 327 alfree(allocator, result[i].ptr);
universe@235 328 }
universe@235 329 alfree(allocator, result);
universe@235 330 *n = -2;
universe@235 331 return NULL;
universe@233 332 }
universe@233 333 }
universe@233 334 } else {
universe@233 335 /* nmax reached, copy the _full_ remaining string */
universe@233 336 result[j-1] = sstrdup_a(allocator, curpos);
universe@233 337 break;
universe@233 338 }
universe@173 339 } else {
universe@233 340 /* no more matches, copy last string */
universe@233 341 result[j-1] = sstrdup_a(allocator, curpos);
universe@173 342 break;
universe@173 343 }
universe@119 344 }
universe@233 345 *n = j;
universe@119 346 } else {
universe@119 347 *n = -2;
universe@39 348 }
universe@39 349
universe@39 350 return result;
universe@39 351 }
universe@39 352
olaf@68 353 int sstrcmp(sstr_t s1, sstr_t s2) {
universe@116 354 if (s1.length == s2.length) {
universe@116 355 return memcmp(s1.ptr, s2.ptr, s1.length);
universe@116 356 } else if (s1.length > s2.length) {
universe@116 357 return 1;
universe@116 358 } else {
universe@116 359 return -1;
universe@116 360 }
olaf@20 361 }
olaf@20 362
universe@149 363 int sstrcasecmp(sstr_t s1, sstr_t s2) {
universe@149 364 if (s1.length == s2.length) {
universe@149 365 #ifdef _WIN32
universe@149 366 return _strnicmp(s1.ptr, s2.ptr, s1.length);
universe@149 367 #else
universe@149 368 return strncasecmp(s1.ptr, s2.ptr, s1.length);
universe@149 369 #endif
universe@149 370 } else if (s1.length > s2.length) {
universe@149 371 return 1;
universe@149 372 } else {
universe@149 373 return -1;
universe@149 374 }
universe@149 375 }
universe@149 376
olaf@68 377 sstr_t sstrdup(sstr_t s) {
universe@125 378 return sstrdup_a(ucx_default_allocator(), s);
olaf@109 379 }
olaf@20 380
universe@125 381 sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
olaf@109 382 sstr_t newstring;
universe@173 383 newstring.ptr = (char*)almalloc(allocator, s.length + 1);
olaf@109 384 if (newstring.ptr) {
olaf@109 385 newstring.length = s.length;
olaf@109 386 newstring.ptr[newstring.length] = 0;
olaf@109 387
olaf@109 388 memcpy(newstring.ptr, s.ptr, s.length);
olaf@109 389 } else {
olaf@109 390 newstring.length = 0;
olaf@109 391 }
olaf@109 392
olaf@20 393 return newstring;
olaf@20 394 }
olaf@96 395
olaf@96 396 sstr_t sstrtrim(sstr_t string) {
olaf@96 397 sstr_t newstr = string;
universe@189 398
universe@189 399 while (newstr.length > 0 && isspace(*newstr.ptr)) {
universe@189 400 newstr.ptr++;
universe@189 401 newstr.length--;
universe@98 402 }
universe@189 403 while (newstr.length > 0 && isspace(newstr.ptr[newstr.length-1])) {
universe@189 404 newstr.length--;
olaf@96 405 }
olaf@96 406
olaf@96 407 return newstr;
olaf@96 408 }
universe@146 409
universe@146 410 int sstrprefix(sstr_t string, sstr_t prefix) {
universe@146 411 if (string.length == 0) {
universe@146 412 return prefix.length == 0;
universe@146 413 }
universe@146 414 if (prefix.length == 0) {
universe@146 415 return 1;
universe@146 416 }
universe@146 417
universe@146 418 if (prefix.length > string.length) {
universe@146 419 return 0;
universe@146 420 } else {
universe@146 421 return memcmp(string.ptr, prefix.ptr, prefix.length) == 0;
universe@146 422 }
universe@146 423 }
universe@146 424
universe@146 425 int sstrsuffix(sstr_t string, sstr_t suffix) {
universe@146 426 if (string.length == 0) {
universe@146 427 return suffix.length == 0;
universe@146 428 }
universe@146 429 if (suffix.length == 0) {
universe@146 430 return 1;
universe@146 431 }
universe@146 432
universe@146 433 if (suffix.length > string.length) {
universe@146 434 return 0;
universe@146 435 } else {
universe@146 436 return memcmp(string.ptr+string.length-suffix.length,
universe@146 437 suffix.ptr, suffix.length) == 0;
universe@146 438 }
universe@146 439 }
universe@210 440
universe@210 441 sstr_t sstrlower(sstr_t string) {
universe@210 442 sstr_t ret = sstrdup(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 sstrlower_a(UcxAllocator *allocator, sstr_t string) {
universe@210 450 sstr_t ret = sstrdup_a(allocator, string);
universe@210 451 for (size_t i = 0; i < ret.length ; i++) {
universe@210 452 ret.ptr[i] = tolower(ret.ptr[i]);
universe@210 453 }
universe@210 454 return ret;
universe@210 455 }
universe@210 456
universe@210 457 sstr_t sstrupper(sstr_t string) {
universe@210 458 sstr_t ret = sstrdup(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 }
universe@210 464
universe@210 465 sstr_t sstrupper_a(UcxAllocator *allocator, sstr_t string) {
universe@210 466 sstr_t ret = sstrdup_a(allocator, string);
universe@210 467 for (size_t i = 0; i < ret.length ; i++) {
universe@210 468 ret.ptr[i] = toupper(ret.ptr[i]);
universe@210 469 }
universe@210 470 return ret;
universe@210 471 }

mercurial