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
--- 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;

mercurial