sstrtrim now uses isspace()

Tue, 31 Mar 2015 10:17:36 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 31 Mar 2015 10:17:36 +0200
changeset 189
f43bbd33fec0
parent 188
63f87e2884c1
child 190
07e2f2a78ac6

sstrtrim now uses isspace()

ucx/string.c file | annotate | diff | comparison | revisions
--- a/ucx/string.c	Mon Jul 28 15:10:22 2014 +0200
+++ b/ucx/string.c	Tue Mar 31 10:17:36 2015 +0200
@@ -29,6 +29,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdarg.h>
+#include <ctype.h>
 
 #include "string.h"
 #include "allocator.h"
@@ -296,38 +297,15 @@
 
 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;
-        }
+    while (newstr.length > 0 && isspace(*newstr.ptr)) {
+        newstr.ptr++;
+        newstr.length--;
     }
-    newstr.ptr = &string.ptr[i];
-    newstr.length = string.length - i;
-    
-    if(newstr.length == 0) {
-        return newstr;
+    while (newstr.length > 0 && isspace(newstr.ptr[newstr.length-1])) {
+        newstr.length--;
     }
     
-    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