improves handling of special cases delimiter size >= string size in sstrsplit

Mon, 20 Feb 2017 15:25:28 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 20 Feb 2017 15:25:28 +0100
changeset 231
35490eb48214
parent 230
4044131874f9
child 232
5f2d650eade7

improves handling of special cases delimiter size >= string size in sstrsplit

ucx/string.c file | annotate | diff | comparison | revisions
     1.1 --- a/ucx/string.c	Fri Dec 16 23:22:18 2016 +0100
     1.2 +++ b/ucx/string.c	Mon Feb 20 15:25:28 2017 +0100
     1.3 @@ -202,16 +202,28 @@
     1.4          *n = -1;
     1.5          return NULL;
     1.6      }
     1.7 -
     1.8 +    
     1.9 +    /* special cases: delimiter is at least as large as the string */
    1.10 +    if (d.length >= s.length) {
    1.11 +        /* exact match */
    1.12 +        if (sstrcmp(s, d) == 0) {
    1.13 +            *n = 0;
    1.14 +            return NULL;
    1.15 +        } else /* no match possible */ {
    1.16 +            *n = 1;
    1.17 +            sstr_t *result = (sstr_t*) almalloc(allocator, sizeof(sstr_t));
    1.18 +            result->ptr = (char*) almalloc(allocator, 1+s.length);
    1.19 +            memcpy(result->ptr, s.ptr, s.length);
    1.20 +            result->ptr[s.length] = '\0';
    1.21 +            result->length = s.length;
    1.22 +            return result;
    1.23 +        }
    1.24 +    }
    1.25 +    
    1.26      sstr_t* result;
    1.27      ssize_t nmax = *n;
    1.28      *n = 1;
    1.29 -
    1.30 -    /* special case: exact match - no processing needed */
    1.31 -    if (sstrcmp(s, d) == 0) {
    1.32 -        *n = 0;
    1.33 -        return NULL;
    1.34 -    }
    1.35 +    
    1.36      sstr_t sv = sstrdup(s);
    1.37      if (sv.length == 0) {
    1.38          *n = -2;
    1.39 @@ -238,7 +250,7 @@
    1.40              char* ptr = (char*) almalloc(allocator, l + 1);
    1.41              if (ptr) {
    1.42                  memcpy(ptr, pptr, l);
    1.43 -                ptr[l] = 0;
    1.44 +                ptr[l] = '\0';
    1.45  
    1.46                  result[i] = sstrn(ptr, l);
    1.47                  pptr += l + d.length;

mercurial