ucx/string.c

changeset 5
88625853ae74
parent 1
1bcaac272cdf
child 17
11dffb40cd91
--- a/ucx/string.c	Sat Dec 01 20:34:55 2012 +0100
+++ b/ucx/string.c	Mon Aug 12 14:40:19 2013 +0200
@@ -1,8 +1,29 @@
 /*
- * File:   sstring.c
- * Author: olaf
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
  *
- * Created on 17. Juni 2010, 13:27
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include <stdlib.h>
@@ -10,18 +31,19 @@
 #include <stdarg.h>
 
 #include "string.h"
+#include "allocator.h"
 
-sstr_t sstr(char *s) {
+sstr_t sstr(char *cstring) {
     sstr_t string;
-    string.ptr = s;
-    string.length = strlen(s);
+    string.ptr = cstring;
+    string.length = strlen(cstring);
     return string;
 }
 
-sstr_t sstrn(char *s, size_t n) {
+sstr_t sstrn(char *cstring, size_t length) {
     sstr_t string;
-    string.ptr = s;
-    string.length = n;
+    string.ptr = cstring;
+    string.length = length;
     return string;
 }
 
@@ -30,7 +52,7 @@
     size_t size = s.length;
     va_start(ap, s);
 
-    for (int i=0;i<n-1;i++) {
+    for (size_t i = 1 ; i < n ; i++) {
         sstr_t str = va_arg(ap, sstr_t);
         size += str.length;
     }
@@ -39,22 +61,7 @@
     return size;
 }
 
-sstr_t sstrcat(sstr_t s, ...) {
-    va_list ap;
-    va_start(ap, s);
-    s.ptr[0] = 0;
-
-    sstr_t str = va_arg (ap, sstr_t);
-    while (str.ptr != NULL) {
-        s.ptr = strncat (s.ptr, str.ptr, s.length);
-        str = va_arg (ap, sstr_t);
-    }
-    va_end(ap);
-
-    return s;
-}
-
-sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...) {
+sstr_t sstrncat(sstr_t s, size_t n, sstr_t c1, ...) {
     va_list ap;
     va_start(ap, c1);
     s.ptr[0] = 0;
@@ -66,7 +73,7 @@
     memcpy(ptr, c1.ptr, cplen);
     len -= cplen;
     ptr += cplen;
-    for (int i=0;i<n-1;i++) {
+    for (size_t i = 1 ; i < n ; i++) {
         sstr_t str = va_arg (ap, sstr_t);
         cplen = str.length > len ? len : str.length;
         if(cplen <= 0) {
@@ -78,6 +85,7 @@
         ptr += cplen;
     }
     va_end(ap);
+    s.length = ptr - s.ptr;
 
     return s;
 }
@@ -88,7 +96,7 @@
 
 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
     sstr_t new_sstr;
-    if (start < 0 || start >= s.length || length < 0) {
+    if (start >= s.length) {
         return s;
     }
     if (length > s.length-start) {
@@ -99,8 +107,25 @@
     return new_sstr;
 }
 
+sstr_t sstrchr(sstr_t s, int c) {
+    for(size_t i=0;i<s.length;i++) {
+        if(s.ptr[i] == c) {
+            return sstrsubs(s, i);
+        }
+    }
+    sstr_t n;
+    n.ptr = NULL;
+    n.length = 0;
+    return n;
+}
+
 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) {
-    if (d.length == 0) {
+    return sstrsplit_a(ucx_default_allocator(), s, d, n);
+}
+
+sstr_t* sstrsplit_a(UcxAllocator *allocator, sstr_t s, sstr_t d, size_t *n) {
+    if (s.length == 0 || d.length == 0) {
+        *n = -1;
         return NULL;
     }
 
@@ -109,16 +134,20 @@
     *n = 1;
 
     /* special case: exact match - no processing needed */
-    if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) {
+    if (sstrcmp(s, d) == 0) {
         *n = 0;
         return NULL;
     }
     sstr_t sv = sstrdup(s);
+    if (sv.length == 0) {
+        *n = -2;
+        return NULL;
+    }
 
-    for (int i = 0 ; i < s.length ; i++) {
+    for (size_t i = 0 ; i < s.length ; i++) {
         if (sv.ptr[i] == d.ptr[0]) {
             _Bool match = 1;
-            for (int j = 1 ; j < d.length ; j++) {
+            for (size_t j = 1 ; j < d.length ; j++) {
                 if (j+i < s.length) {
                     match &= (sv.ptr[i+j] == d.ptr[j]);
                 } else {
@@ -128,7 +157,7 @@
             }
             if (match) {
                 (*n)++;
-                for (int j = 0 ; j < d.length ; j++) {
+                for (size_t j = 0 ; j < d.length ; j++) {
                     sv.ptr[i+j] = 0;
                 }
                 i += d.length;
@@ -136,37 +165,90 @@
         }
         if ((*n) == nmax) break;
     }
-    result = (sstr_t*) malloc(sizeof(sstr_t) * (*n));
+    result = (sstr_t*) allocator->malloc(allocator->pool, sizeof(sstr_t)*(*n));
 
-    char *pptr = sv.ptr;
-    for (int i = 0 ; i < *n ; i++) {
-        size_t l = strlen(pptr);
-        char* ptr = (char*) malloc(l + 1);
-        memcpy(ptr, pptr, l);
-        ptr[l] = 0;
+    if (result) {
+        char *pptr = sv.ptr;
+        for (size_t i = 0 ; i < *n ; i++) {
+            size_t l = strlen(pptr);
+            char* ptr = (char*) allocator->malloc(allocator->pool, l + 1);
+            memcpy(ptr, pptr, l);
+            ptr[l] = 0;
 
-        result[i] = sstrn(ptr, l);
-        pptr += l + d.length;
+            result[i] = sstrn(ptr, l);
+            pptr += l + d.length;
+        }
+    } else {
+        *n = -2;
     }
-
+    
     free(sv.ptr);
 
     return result;
 }
 
 int sstrcmp(sstr_t s1, sstr_t s2) {
-    return strncmp(s1.ptr, s2.ptr, s1.length>s2.length ? s2.length: s1.length);
+    if (s1.length == s2.length) {
+        return memcmp(s1.ptr, s2.ptr, s1.length);
+    } else if (s1.length > s2.length) {
+        return 1;
+    } else {
+        return -1;
+    }
 }
 
 sstr_t sstrdup(sstr_t s) {
+    return sstrdup_a(ucx_default_allocator(), s);
+}
+
+sstr_t sstrdup_a(UcxAllocator *allocator, sstr_t s) {
     sstr_t newstring;
-    newstring.ptr = (char*) malloc(s.length + 1);
-    if (newstring.ptr != NULL) {
+    newstring.ptr = (char*)allocator->malloc(allocator->pool, s.length + 1);
+    if (newstring.ptr) {
         newstring.length = s.length;
         newstring.ptr[newstring.length] = 0;
-
+        
         memcpy(newstring.ptr, s.ptr, s.length);
+    } else {
+        newstring.length = 0;
     }
-
+    
     return newstring;
 }
+
+sstr_t sstrtrim(sstr_t string) {
+    sstr_t newstr = string;
+    if (string.length == 0) {
+        return newstr;
+    }
+    
+    size_t i;
+    for(i=0;i<string.length;i++) {
+        char c = string.ptr[i];
+        if(c > 32) {
+            break;
+        }
+    }
+    newstr.ptr = &string.ptr[i];
+    newstr.length = string.length - i;
+    
+    if(newstr.length == 0) {
+        return newstr;
+    }
+    
+    i = newstr.length - 1;
+    for(;;) {
+        char c = newstr.ptr[i];
+        if(c > 32) {
+            break;
+        }
+        if(i > 0) {
+            i--;
+        } else {
+            break;
+        }
+    }
+    newstr.length = i + 1;
+    
+    return newstr;
+}

mercurial