Mon, 20 Feb 2017 15:25:28 +0100
improves handling of special cases delimiter size >= string size in sstrsplit
ucx/string.c | file | annotate | diff | comparison | revisions |
--- 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;