From c89d59fe9676c008f676a24e2f831ba2136646c4 Mon Sep 17 00:00:00 2001 From: Ian Barwick Date: Tue, 24 Oct 2017 15:34:43 +0900 Subject: [PATCH] Improve trim() function Did not cope well with trailing spaces or entirely blank strings. --- strutil.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/strutil.c b/strutil.c index dd5ce485..ed352758 100644 --- a/strutil.c +++ b/strutil.c @@ -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;