Improve trim() function

Did not cope well with trailing spaces or entirely blank strings.
This commit is contained in:
Ian Barwick
2017-10-24 15:34:43 +09:00
parent c5d91ca88c
commit 52655e9cd5

View File

@@ -413,12 +413,16 @@ trim(char *s)
--s2;
*(s2 + 1) = '\0';
/* String is all whitespace - no need for further processing */
if (s2 + 1 == s1)
return s;
/* Trim left side */
while ((isspace(*s1)) && (s1 < s2))
++s1;
/* Copy finished string */
memmove(s, s1, s2 - s1);
memmove(s, s1, (s2 - s1) + 1);
s[s2 - s1 + 1] = '\0';
return s;