diff options
Diffstat (limited to 'src/lib/strbuf.c')
| -rw-r--r-- | src/lib/strbuf.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/lib/strbuf.c b/src/lib/strbuf.c index f56815a0..572f11cc 100644 --- a/src/lib/strbuf.c +++ b/src/lib/strbuf.c @@ -37,6 +37,29 @@ int suffixcmp(const char *str, const char *suffix) return strcmp(str + len_minus_suflen, suffix); } +/* + * Trims whitespace characters both from left and right side of a string. + * Modifies the string in-place. Returns the trimmed string. + */ +char *strtrim(char *str) +{ + if (!str) + return NULL; + + // Remove leading spaces. + overlapping_strcpy(str, skip_whitespace(str)); + + // Remove trailing spaces. + int i = strlen(str); + while (--i >= 0) + { + if (!isspace(str[i])) + break; + } + str[++i] = '\0'; + return str; +} + struct strbuf *strbuf_new(void) { struct strbuf *buf = xzalloc(sizeof(*buf)); |
