src/server/ucx/string.c

changeset 71
069c152f6272
parent 70
4e6e812c1d97
child 88
73b3485e96f1
--- a/src/server/ucx/string.c	Thu Jun 20 14:07:46 2013 +0200
+++ b/src/server/ucx/string.c	Fri Jun 21 12:10:44 2013 +0200
@@ -1,36 +1,57 @@
 /*
- * 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>
-#include <strings.h>
+#include <string.h>
 #include <stdarg.h>
 
 #include "string.h"
 
-sstr_t sstr (char *s) {
+sstr_t sstr(char *s) {
     sstr_t string;
     string.ptr = s;
     string.length = strlen(s);
     return string;
 }
 
-sstr_t sstrn (char *s, size_t n) {
+sstr_t sstrn(char *s, size_t n) {
     sstr_t string;
     string.ptr = s;
     string.length = n;
     return string;
 }
 
-size_t sstrnlen (size_t n, sstr_t s, ...) {
+size_t sstrnlen(size_t n, sstr_t s, ...) {
     va_list ap;
     size_t size = s.length;
     va_start(ap, s);
 
-    for (int i=0;i<n-1;i++) {
+    for (size_t i = 0 ; i < n-1 ; i++) {
         sstr_t str = va_arg(ap, sstr_t);
         size += str.length;
     }
@@ -39,22 +60,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(size_t n, sstr_t s, sstr_t c1, ...) {
     va_list ap;
     va_start(ap, c1);
     s.ptr[0] = 0;
@@ -66,7 +72,7 @@
     memcpy(ptr, c1.ptr, cplen);
     len -= cplen;
     ptr += cplen;
-    for (int i=0;i<n-1;i++) {
+    for (size_t i = 0 ; i < n-1 ; i++) {
         sstr_t str = va_arg (ap, sstr_t);
         cplen = str.length > len ? len : str.length;
         if(cplen <= 0) {
@@ -78,15 +84,16 @@
         ptr += cplen;
     }
     va_end(ap);
+    s.length = ptr - s.ptr;
 
     return s;
 }
 
-sstr_t sstrsubs (sstr_t s, size_t start) {
+sstr_t sstrsubs(sstr_t s, size_t start) {
     return sstrsubsl (s, start, s.length-start);
 }
 
-sstr_t sstrsubsl (sstr_t s, size_t start, size_t length) {
+sstr_t sstrsubsl(sstr_t s, size_t start, size_t length) {
     sstr_t new_sstr;
     if (start >= s.length) {
         return s;
@@ -110,16 +117,15 @@
 
     /* special case: exact match - no processing needed */
     if (s.length == d.length && strncmp(s.ptr, d.ptr, s.length) == 0) {
-        result = malloc(sizeof(sstr_t));
-        result[0] = sstrn("", 0);
-        return result;
+        *n = 0;
+        return NULL;
     }
     sstr_t sv = sstrdup(s);
 
-    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 {
@@ -129,7 +135,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;
@@ -137,12 +143,12 @@
         }
         if ((*n) == nmax) break;
     }
-    result = malloc(sizeof(sstr_t) * (*n));
+    result = (sstr_t*) malloc(sizeof(sstr_t) * (*n));
 
     char *pptr = sv.ptr;
-    for (int i = 0 ; i < *n ; i++) {
+    for (size_t i = 0 ; i < *n ; i++) {
         size_t l = strlen(pptr);
-        char* ptr = malloc(l + 1);
+        char* ptr = (char*) malloc(l + 1);
         memcpy(ptr, pptr, l);
         ptr[l] = 0;
 
@@ -162,16 +168,57 @@
 sstr_t sstrdup(sstr_t s) {
     sstr_t newstring;
     newstring.ptr = (char*) malloc(s.length + 1);
-    if (newstring.ptr != NULL) {
+    newstring.length = 0;
+    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;
+}
+
+
 // webserver extension
 
 int sstr_startswith(sstr_t string, sstr_t cmp) {
@@ -183,30 +230,6 @@
     }
 }
 
-sstr_t sstrtrim(sstr_t string) {
-    sstr_t newstr = string;
-    int 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;
-    
-    for(i=newstr.length-1;i>=0;i--) {
-        char c = newstr.ptr[i];
-        if(c > 32) {
-            break;
-        }
-    }
-    newstr.length = i + 1;
-    
-    return newstr;
-}
-
 sstr_t sstrdup_mp(UcxMempool *pool, sstr_t s) {
     sstr_t newstring;
     newstring.ptr = (char*)ucx_mempool_malloc(pool, s.length + 1);

mercurial