stringop.c: rewrite strip_whitespace

master
Ian Fan 6 years ago
parent 3b4cf3718b
commit 967566e37f

@ -9,24 +9,17 @@
#include "string.h" #include "string.h"
#include "list.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) { void strip_whitespace(char *str) {
if (*_str == '\0') size_t len = strlen(str);
return _str; size_t start = strspn(str, whitespace);
char *strold = _str; memmove(str, &str[start], len + 1 - start);
while (*_str == ' ' || *_str == '\t') {
_str++; if (!*str) return;
}
char *str = strdup(_str); for (len -= start + 1; isspace(str[len]); --len) {}
free(strold); str[len + 1] = '\0';
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_quotes(char *str) { void strip_quotes(char *str) {

@ -3,10 +3,7 @@
#include "list.h" #include "list.h"
// array of whitespace characters to use for delims void strip_whitespace(char *str);
extern const char whitespace[];
char *strip_whitespace(char *str);
char *strip_comments(char *str); char *strip_comments(char *str);
void strip_quotes(char *str); void strip_quotes(char *str);

@ -236,15 +236,15 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
criteria_destroy(criteria); criteria_destroy(criteria);
config->handler_context.using_criteria = true; config->handler_context.using_criteria = true;
// Skip leading whitespace // Skip leading whitespace
head += strspn(head, whitespace); for (; isspace(*head); ++head) {}
} }
// Split command list // Split command list
cmdlist = argsep(&head, ";"); cmdlist = argsep(&head, ";");
cmdlist += strspn(cmdlist, whitespace); for (; isspace(*cmdlist); ++cmdlist) {}
do { do {
// Split commands // Split commands
cmd = argsep(&cmdlist, ","); cmd = argsep(&cmdlist, ",");
cmd += strspn(cmd, whitespace); for (; isspace(*cmd); ++cmd) {}
if (strcmp(cmd, "") == 0) { if (strcmp(cmd, "") == 0) {
wlr_log(WLR_INFO, "Ignoring empty command."); wlr_log(WLR_INFO, "Ignoring empty command.");
continue; continue;

@ -580,7 +580,7 @@ static int detect_brace_on_following_line(FILE *file, char *line,
free(peeked); free(peeked);
peeked = peek_line(file, lines, &position); peeked = peek_line(file, lines, &position);
if (peeked) { if (peeked) {
peeked = strip_whitespace(peeked); strip_whitespace(peeked);
} }
lines++; lines++;
} while (peeked && strlen(peeked) == 0); } while (peeked && strlen(peeked) == 0);
@ -663,7 +663,7 @@ bool read_config(FILE *file, struct sway_config *config,
read += length + 1; read += length + 1;
} }
line = strip_whitespace(line); strip_whitespace(line);
if (line[0] == '#') { if (line[0] == '#') {
free(line); free(line);
continue; continue;

@ -49,7 +49,7 @@ char *input_device_get_identifier(struct wlr_input_device *device) {
int vendor = device->vendor; int vendor = device->vendor;
int product = device->product; int product = device->product;
char *name = strdup(device->name); char *name = strdup(device->name);
name = strip_whitespace(name); strip_whitespace(name);
char *p = name; char *p = name;
for (; *p; ++p) { for (; *p; ++p) {

Loading…
Cancel
Save