diff --git a/cmText.c b/cmText.c index 00a22c1..300676d 100644 --- a/cmText.c +++ b/cmText.c @@ -519,6 +519,49 @@ void cmTextClip( cmChar_t* s, unsigned n ) } +cmChar_t* cmTextTrimBegin( cmChar_t* s ) +{ + if( s==NULL || strlen(s) == 0 ) + return s; + + cmChar_t* s0 = cmTextNextNonWhite(s); + + // no non-white char's exist + if( s0 == NULL ) + { + s[0] = 0; + return s; + } + + if( s0 != s ) + cmTextShrinkS(s,s,s0-s); + + return s; +} + +cmChar_t* cmTextTrimEnd( cmChar_t* s ) +{ + unsigned sn; + + if( s==NULL || (sn = strlen(s))==0) + return s; + + cmChar_t* s0 = cmTextLastNonWhiteChar(s); + + if(s0-s+1 < sn ) + s[s0-s+1] = 0; + + + return s; +} + +cmChar_t* cmTextTrim( cmChar_t* s) +{ + cmTextTrimBegin(s); + cmTextTrimEnd(s); + return s; +} + cmChar_t* cmTextExpandS( cmChar_t* s, const cmChar_t* t, unsigned tn ) { return cmVOC_Expand(s,strlen(s)+1,t,tn); } diff --git a/cmText.h b/cmText.h index cca3818..fde6e70 100644 --- a/cmText.h +++ b/cmText.h @@ -161,6 +161,11 @@ extern "C" { // Remove the last n characters from s by inserting a '\0' at s[ strlen(s)-n ]. void cmTextClip( cmChar_t* s, unsigned n ); + // Trim white space from the begining/end/both of a string + cmChar_t* cmTextTrimBegin( cmChar_t* s ); + cmChar_t* cmTextTrimEnd( cmChar_t* s ); + cmChar_t* cmTextTrim( cmChar_t* ); + // Expand s by copying all bytes past t to t+tn. cmChar_t* cmTextExpandS( cmChar_t* s, const cmChar_t* t, unsigned tn );