diff options
Diffstat (limited to 'src/lexer/trim.c')
| -rw-r--r-- | src/lexer/trim.c | 62 |
1 files changed, 29 insertions, 33 deletions
diff --git a/src/lexer/trim.c b/src/lexer/trim.c index d0ba90a..efc5171 100644 --- a/src/lexer/trim.c +++ b/src/lexer/trim.c @@ -6,13 +6,13 @@ /* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:36 by nahaddac #+# #+# */ -/* Updated: 2020/10/09 15:17:45 by cacharle ### ########.fr */ +/* Updated: 2020/10/10 08:32:41 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "lexer.h" -void del_space(char *str) +static void st_del_space(char *str) { int i; @@ -30,50 +30,46 @@ void del_space(char *str) } } -int del_quote(char *str) +static size_t st_count_quote(char *str) { - size_t i; - size_t quote_counter; + char quote_char; + size_t counter; - i = 0; - quote_counter = 1; - if (str[0] == '\'') - { - while (str[i++] != '\0') - { - if (str[i] == '\'') - { - quote_counter++; - break ; - } - } - } - else if (str[0] == '"') + if (*str != '\'' && *str != '"') + return (0); + quote_char = *str; + counter = 1; + str++; + while (*str != '\0') { - while (str[i++] != '\0') + if (quote_char == '"' && *str == '\\') + str += 2; + if (*str == quote_char) { - if (str[i] == '\\') - i += 2; - if (str[i] == '"') - { - quote_counter++; - break ; - } + counter++; + *str = '\0'; + break ; } + str++; } - else - return (0); + return (counter); +} + +static int st_del_quote(char *str) +{ + size_t quote_counter; + + quote_counter = st_count_quote(str); if (quote_counter % 2 == 1) { errorf("unexpected EOF while looking for matching `%c'\n", str[0]); return (2); } - str[i] = '\0'; ft_memmove(str, str + 1, ft_strlen(str + 1) + 1); return (0); } -int lexer_trim(t_tok_lst *tokens) +int lexer_trim(t_tok_lst *tokens) { int status; @@ -81,14 +77,14 @@ int lexer_trim(t_tok_lst *tokens) { if (tokens->tag & (TAG_STR_DOUBLE | TAG_STR_SINGLE)) { - if ((status = del_quote(tokens->content)) != 0) + if ((status = st_del_quote(tokens->content)) != 0) return (status); if (tokens->next == NULL) tokens->tag &= ~TAG_STICK; } else { - del_space(tokens->content); + st_del_space(tokens->content); if (tokens->next == NULL) tokens->tag &= ~TAG_STICK; } |
