Hi On Sat, Sep 19, 2015 at 03:10:50PM +1000, Allan McRae wrote:
On 19/09/15 03:17, Silvan Jegen wrote:
On Mon, Sep 14, 2015 at 06:37:38PM -0400, Andrew Gregory wrote:
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> +/* Trim whitespace and newlines from a string + */ +size_t strtrim(char *str) +{ + char *end, *pch = str; + + if(str == NULL || *str == '\0') { + /* string is empty, so we're done. */ + return 0; + } + + while(isspace((unsigned char)*pch)) { + pch++; + } + if(pch != str) { + size_t len = strlen(pch); + if(len) { + memmove(str, pch, len + 1); + pch = str; + } else { + *str = '\0'; + } + }
The last part could be simplified as follows.
[...] if(pch != str) { size_t len = strlen(pch); if(!len) { /* check if there wasn't anything but whitespace in the string. */ *str = '\0'; return 0; } memmove(str, pch, len + 1); pch = str; }
and these three lines could then be removed...
+ if(*str == '\0') { + return 0; + } [...]
...because we already checked for (*str == '\0') in the empty string case further up in the function.
This patch is a pure cut and paste. Any improvements should go in a other patches.
I will send a patch after this code move has been applied then. Cheers, Silvan