aboutsummaryrefslogtreecommitdiff
path: root/common/stringop.c
diff options
context:
space:
mode:
authorIan Fan <ianfan0@gmail.com>2018-12-09 11:52:55 +0000
committerIan Fan <ianfan0@gmail.com>2019-01-01 09:01:24 +0000
commit967566e37f93890bd5255725129c929aeeac709e (patch)
tree7d6bc1470609b2c0da92889db36833cc2e5e8431 /common/stringop.c
parent3b4cf3718b05d5ebd98d730abb742e82f8980287 (diff)
stringop.c: rewrite strip_whitespace
Diffstat (limited to 'common/stringop.c')
-rw-r--r--common/stringop.c27
1 files changed, 10 insertions, 17 deletions
diff --git a/common/stringop.c b/common/stringop.c
index 4b8c9a38..f8b7aaec 100644
--- a/common/stringop.c
+++ b/common/stringop.c
@@ -9,24 +9,17 @@
#include "string.h"
#include "list.h"
-const char whitespace[] = " \f\n\r\t\v";
+static const char whitespace[] = " \f\n\r\t\v";
-char *strip_whitespace(char *_str) {
- if (*_str == '\0')
- return _str;
- char *strold = _str;
- while (*_str == ' ' || *_str == '\t') {
- _str++;
- }
- char *str = strdup(_str);
- free(strold);
- int i;
- for (i = 0; str[i] != '\0'; ++i);
- do {
- i--;
- } while (i >= 0 && (str[i] == ' ' || str[i] == '\t'));
- str[i + 1] = '\0';
- return str;
+void strip_whitespace(char *str) {
+ size_t len = strlen(str);
+ size_t start = strspn(str, whitespace);
+ memmove(str, &str[start], len + 1 - start);
+
+ if (!*str) return;
+
+ for (len -= start + 1; isspace(str[len]); --len) {}
+ str[len + 1] = '\0';
}
void strip_quotes(char *str) {