diff --git a/common/stringop.c b/common/stringop.c index 4b8c9a384..f8b7aaec6 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) { diff --git a/include/stringop.h b/include/stringop.h index d1bfa29d0..f7ca60a51 100644 --- a/include/stringop.h +++ b/include/stringop.h @@ -3,10 +3,7 @@ #include "list.h" -// array of whitespace characters to use for delims -extern const char whitespace[]; - -char *strip_whitespace(char *str); +void strip_whitespace(char *str); char *strip_comments(char *str); void strip_quotes(char *str); diff --git a/sway/commands.c b/sway/commands.c index 927434bcd..cd595b030 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -236,15 +236,15 @@ list_t *execute_command(char *_exec, struct sway_seat *seat, criteria_destroy(criteria); config->handler_context.using_criteria = true; // Skip leading whitespace - head += strspn(head, whitespace); + for (; isspace(*head); ++head) {} } // Split command list cmdlist = argsep(&head, ";"); - cmdlist += strspn(cmdlist, whitespace); + for (; isspace(*cmdlist); ++cmdlist) {} do { // Split commands cmd = argsep(&cmdlist, ","); - cmd += strspn(cmd, whitespace); + for (; isspace(*cmd); ++cmd) {} if (strcmp(cmd, "") == 0) { wlr_log(WLR_INFO, "Ignoring empty command."); continue; diff --git a/sway/config.c b/sway/config.c index bb7f796d1..c71f315a2 100644 --- a/sway/config.c +++ b/sway/config.c @@ -580,7 +580,7 @@ static int detect_brace_on_following_line(FILE *file, char *line, free(peeked); peeked = peek_line(file, lines, &position); if (peeked) { - peeked = strip_whitespace(peeked); + strip_whitespace(peeked); } lines++; } while (peeked && strlen(peeked) == 0); @@ -663,7 +663,7 @@ bool read_config(FILE *file, struct sway_config *config, read += length + 1; } - line = strip_whitespace(line); + strip_whitespace(line); if (line[0] == '#') { free(line); continue; diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c index 610877332..04e14355c 100644 --- a/sway/input/input-manager.c +++ b/sway/input/input-manager.c @@ -49,7 +49,7 @@ char *input_device_get_identifier(struct wlr_input_device *device) { int vendor = device->vendor; int product = device->product; char *name = strdup(device->name); - name = strip_whitespace(name); + strip_whitespace(name); char *p = name; for (; *p; ++p) {