ucx/string.c

Thu, 23 Feb 2017 15:25:26 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 23 Feb 2017 15:25:26 +0100
changeset 237
5ba9de6361ff
parent 236
ffc6d0910342
child 238
27b31c2c959c
permissions
-rw-r--r--

further performance tweaks to sstrstr() function

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

mercurial