From abd0afb03a2929931cb684e5aaeac312affe6e5f Mon Sep 17 00:00:00 2001 From: taiyu Date: Tue, 18 Aug 2015 03:22:31 -0700 Subject: [PATCH] enhanced whitespace remover --- include/stringop.h | 4 ++-- sway/commands.c | 6 +++--- sway/config.c | 5 ++--- sway/stringop.c | 50 +++++++++++++++++++++++----------------------- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/include/stringop.h b/include/stringop.h index a5346829..03387345 100644 --- a/include/stringop.h +++ b/include/stringop.h @@ -2,8 +2,8 @@ #define _SWAY_STRINGOP_H #include "list.h" -char *strip_whitespace(char *str, int *trimmed_start); -char *strip_comments(char *str); +void strip_whitespace(char *str); +void strip_comments(char *str); list_t *split_string(const char *str, const char *delims); void free_flat_list(list_t *list); char *code_strchr(const char *string, char delimiter); diff --git a/sway/commands.c b/sway/commands.c index 444e6159..642fa3ce 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -490,7 +490,7 @@ static char **split_directive(char *line, int *argc) { if (!*line) return parts; int in_string = 0, in_character = 0; - int i, j, _; + int i, j; for (i = 0, j = 0; line[i]; ++i) { if (line[i] == '\\') { ++i; @@ -503,7 +503,7 @@ static char **split_directive(char *line, int *argc) { char *item = malloc(i - j + 1); strncpy(item, line + j, i - j); item[i - j] = '\0'; - item = strip_whitespace(item, &_); + strip_whitespace(item); if (item[0] == '\0') { free(item); } else { @@ -521,7 +521,7 @@ static char **split_directive(char *line, int *argc) { char *item = malloc(i - j + 1); strncpy(item, line + j, i - j); item[i - j] = '\0'; - item = strip_whitespace(item, &_); + strip_whitespace(item); if (*argc == capacity) { capacity++; parts = realloc(parts, sizeof(char *) * capacity); diff --git a/sway/config.c b/sway/config.c index 44943769..bde65614 100644 --- a/sway/config.c +++ b/sway/config.c @@ -186,10 +186,9 @@ bool read_config(FILE *file, bool is_active) { int temp_depth = 0; // Temporary: skip all config sections with depth while (!feof(file)) { - int _; char *line = read_line(file); - line = strip_comments(line); - line = strip_whitespace(line, &_); + strip_comments(line); + strip_whitespace(line); if (!line[0]) { goto _continue; } diff --git a/sway/stringop.c b/sway/stringop.c index cbaa69b9..00cc32b8 100644 --- a/sway/stringop.c +++ b/sway/stringop.c @@ -1,37 +1,38 @@ -#include "stringop.h" #include #include +#include +#include +#include "stringop.h" #include "string.h" #include "list.h" -#include /* Note: This returns 8 characters for trimmed_start per tab character. */ -char *strip_whitespace(char *_str, int *trimmed_start) { - *trimmed_start = 0; - if (*_str == '\0') - return _str; - char *strold = _str; - while (*_str == ' ' || *_str == '\t') { - if (*_str == '\t') { - *trimmed_start += 8; - } else { - *trimmed_start += 1; +void strip_whitespace(char *str) { + int shift = 0; + int bpair = 1; + int in_str = 0, in_ch = 0; + while (*str) { + str[-shift] = str[0]; + if (*str == '"' && !in_ch) { + in_str = !in_str; + } else if (*str == '\'' && !in_str) { + in_ch = !in_ch; + } else if (!in_ch && !in_str) { + if (isblank(*str)) { + if (bpair) { + ++shift; + } + bpair=1; + } else { + bpair = 0; + } } - _str++; + ++str; } - char *str = malloc(strlen(_str) + 1); - strcpy(str, _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; + str[-shift-bpair] = 0; } -char *strip_comments(char *str) { +void strip_comments(char *str) { int in_string = 0, in_character = 0; int i = 0; while (str[i] != '\0') { @@ -47,7 +48,6 @@ char *strip_comments(char *str) { } ++i; } - return str; } list_t *split_string(const char *str, const char *delims) {