Wed, 27 Feb 2013 13:53:28 +0100
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; +}