Created
March 22, 2015 21:09
-
-
Save reportbase/54e7b29ddf2c46f16e69 to your computer and use it in GitHub Desktop.
trim strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline char* ltrim(char* const s) | |
{ | |
int len; | |
char* cur; | |
if(s && *s) | |
{ | |
len = strlen(s); | |
cur = s; | |
while(*cur && isspace(*cur)) | |
++cur, --len; | |
if(s != cur) | |
memmove(s, cur, len + 1); | |
} | |
return s; | |
} | |
inline char* rtrim(char* const s) | |
{ | |
int len; | |
char* cur; | |
if(s && *s) | |
{ | |
len = strlen(s); | |
cur = s + len - 1; | |
while(cur != s && isspace(*cur)) | |
--cur, --len; | |
cur[isspace(*cur) ? 0 : 1] = '\0'; | |
} | |
return s; | |
} | |
inline char* trim(char* const s) | |
{ | |
rtrim(s); | |
ltrim(s); | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment