src/server/ucx/string.c

changeset 88
73b3485e96f1
parent 71
069c152f6272
--- a/src/server/ucx/string.c	Thu Jul 11 14:21:23 2013 +0200
+++ b/src/server/ucx/string.c	Tue Jul 16 12:14:13 2013 +0200
@@ -31,6 +31,7 @@
 #include <stdarg.h>
 
 #include "string.h"
+#include "allocator.h"
 
 sstr_t sstr(char *s) {
     sstr_t string;
@@ -106,6 +107,18 @@
     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 NULL;
@@ -168,16 +181,30 @@
 sstr_t sstrdup(sstr_t s) {
     sstr_t newstring;
     newstring.ptr = (char*) malloc(s.length + 1);
-    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 sstrdup_alloc(UcxAllocator *allocator, sstr_t s) {
+    sstr_t newstring;
+    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;
 }
 
@@ -218,7 +245,6 @@
     return newstr;
 }
 
-
 // webserver extension
 
 int sstr_startswith(sstr_t string, sstr_t cmp) {

mercurial