Tue, 31 Mar 2015 10:17:36 +0200
sstrtrim now uses isspace()
ucx/string.c | file | annotate | diff | comparison | revisions |
1.1 --- a/ucx/string.c Mon Jul 28 15:10:22 2014 +0200 1.2 +++ b/ucx/string.c Tue Mar 31 10:17:36 2015 +0200 1.3 @@ -29,6 +29,7 @@ 1.4 #include <stdlib.h> 1.5 #include <string.h> 1.6 #include <stdarg.h> 1.7 +#include <ctype.h> 1.8 1.9 #include "string.h" 1.10 #include "allocator.h" 1.11 @@ -296,37 +297,14 @@ 1.12 1.13 sstr_t sstrtrim(sstr_t string) { 1.14 sstr_t newstr = string; 1.15 - if (string.length == 0) { 1.16 - return newstr; 1.17 + 1.18 + while (newstr.length > 0 && isspace(*newstr.ptr)) { 1.19 + newstr.ptr++; 1.20 + newstr.length--; 1.21 } 1.22 - 1.23 - size_t i; 1.24 - for(i=0;i<string.length;i++) { 1.25 - char c = string.ptr[i]; 1.26 - if(c > 32) { 1.27 - break; 1.28 - } 1.29 + while (newstr.length > 0 && isspace(newstr.ptr[newstr.length-1])) { 1.30 + newstr.length--; 1.31 } 1.32 - newstr.ptr = &string.ptr[i]; 1.33 - newstr.length = string.length - i; 1.34 - 1.35 - if(newstr.length == 0) { 1.36 - return newstr; 1.37 - } 1.38 - 1.39 - i = newstr.length - 1; 1.40 - for(;;) { 1.41 - char c = newstr.ptr[i]; 1.42 - if(c > 32) { 1.43 - break; 1.44 - } 1.45 - if(i > 0) { 1.46 - i--; 1.47 - } else { 1.48 - break; 1.49 - } 1.50 - } 1.51 - newstr.length = i + 1; 1.52 1.53 return newstr; 1.54 }