# HG changeset patch # User Mike Becker # Date 1487600728 -3600 # Node ID 35490eb48214c57eb37d4dfa92f5f47f85a5c9b0 # Parent 4044131874f978c28a3e5ac9ce2dcf00cedf3020 improves handling of special cases delimiter size >= string size in sstrsplit diff -r 4044131874f9 -r 35490eb48214 ucx/string.c --- a/ucx/string.c Fri Dec 16 23:22:18 2016 +0100 +++ b/ucx/string.c Mon Feb 20 15:25:28 2017 +0100 @@ -202,16 +202,28 @@ *n = -1; return NULL; } - + + /* special cases: delimiter is at least as large as the string */ + if (d.length >= s.length) { + /* exact match */ + if (sstrcmp(s, d) == 0) { + *n = 0; + return NULL; + } else /* no match possible */ { + *n = 1; + sstr_t *result = (sstr_t*) almalloc(allocator, sizeof(sstr_t)); + result->ptr = (char*) almalloc(allocator, 1+s.length); + memcpy(result->ptr, s.ptr, s.length); + result->ptr[s.length] = '\0'; + result->length = s.length; + return result; + } + } + sstr_t* result; ssize_t nmax = *n; *n = 1; - - /* special case: exact match - no processing needed */ - if (sstrcmp(s, d) == 0) { - *n = 0; - return NULL; - } + sstr_t sv = sstrdup(s); if (sv.length == 0) { *n = -2; @@ -238,7 +250,7 @@ char* ptr = (char*) almalloc(allocator, l + 1); if (ptr) { memcpy(ptr, pptr, l); - ptr[l] = 0; + ptr[l] = '\0'; result[i] = sstrn(ptr, l); pptr += l + d.length;