added sstrtrim

Wed, 27 Feb 2013 13:53:28 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 27 Feb 2013 13:53:28 +0100
changeset 96
fbbff331beba
parent 95
ecfdc1c4a552
child 97
499e1b465d77

added sstrtrim

ucx/string.c file | annotate | diff | comparison | revisions
ucx/string.h file | annotate | diff | comparison | revisions
--- a/ucx/string.c	Wed Feb 27 13:30:21 2013 +0100
+++ b/ucx/string.c	Wed Feb 27 13:53:28 2013 +0100
@@ -161,6 +161,7 @@
 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;
@@ -172,3 +173,26 @@
 
     return newstring;
 }
+
+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;
+}
--- a/ucx/string.h	Wed Feb 27 13:30:21 2013 +0100
+++ b/ucx/string.h	Wed Feb 27 13:53:28 2013 +0100
@@ -92,6 +92,8 @@
 
 sstr_t sstrdup(sstr_t s);
 
+sstr_t sstrtrim(sstr_t string);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial