From 0e9cfacaf15bb0f4d8a7bd72bae56010d263b2c1 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 4 Sep 2020 22:02:44 +0200 Subject: Time to escape chars --- src/p_args_len.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/p_args_len.c (limited to 'src/p_args_len.c') diff --git a/src/p_args_len.c b/src/p_args_len.c new file mode 100644 index 0000000..8bab9d7 --- /dev/null +++ b/src/p_args_len.c @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* p_args_len.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +#include "d_define.h" + +static size_t + p_skip_delim_size(const char word[], char c, size_t end) +{ + end++; + if (word[end] == c) + return (end + 1); + while (word[end] != C_NULL && word[end] != c) + { + end++; + if (word[end] == c && c == C_DQUOTE) + { + if (word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) + { + end++; + } + } + } + if (word[end] != C_NULL) + { + end++; + } + return (end); +} + +static size_t + p_skip_unquote(const char word[], size_t end) +{ + while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + { + end++; + if (ft_iswhitespace(word[end]) == TRUE && end == 1 && + word[end - 1] == C_BACKSLASH) + { + end++; + } + else if (ft_iswhitespace(word[end]) == TRUE && end > 1 && + word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) + { + end++; + } + } + return (end); +} + +static size_t + p_skip_quote(const char word[], size_t end) +{ + while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + { + if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) + { + end = p_skip_delim_size(word, word[end], end); + } + else + { + while (word[end] != C_NULL && + ft_iswhitespace(word[end]) == FALSE) + { + end++; + } + } + } + return (end); +} + +size_t + p_arg_len(const char word[], const size_t start) +{ + size_t end; + + end = start; + if (word[start] != C_SQUOTE && word[start] != C_DQUOTE && + word[start] != C_NULL) + { + end = p_skip_unquote(word, end); + } + else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) + { + end = p_skip_quote(word, end); + } + return (end); +} + -- cgit v1.2.3 From 3976118a6e9acd035d20ef79d92632ecb962814e Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 5 Sep 2020 14:48:49 +0200 Subject: Rewrite in progress --- src/p_args_len.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/p_args_len.c') diff --git a/src/p_args_len.c b/src/p_args_len.c index 8bab9d7..c736a3a 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -26,7 +26,7 @@ static size_t end++; if (word[end] == c && c == C_DQUOTE) { - if (word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) + if (word[end - 1] == C_BS && word[end - 2] != C_BS) { end++; } @@ -46,12 +46,12 @@ static size_t { end++; if (ft_iswhitespace(word[end]) == TRUE && end == 1 && - word[end - 1] == C_BACKSLASH) + word[end - 1] == C_BS) { end++; } else if (ft_iswhitespace(word[end]) == TRUE && end > 1 && - word[end - 1] == C_BACKSLASH && word[end - 2] != C_BACKSLASH) + word[end - 1] == C_BS && word[end - 2] != C_BS) { end++; } -- cgit v1.2.3 From ab69117ff18f8b4a2b13adeada00cabd7d997855 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 6 Sep 2020 17:56:59 +0200 Subject: Better defines --- src/p_args_len.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/p_args_len.c') diff --git a/src/p_args_len.c b/src/p_args_len.c index c736a3a..801406f 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -21,18 +21,18 @@ static size_t end++; if (word[end] == c) return (end + 1); - while (word[end] != C_NULL && word[end] != c) + while (word[end] != C_NUL && word[end] != c) { end++; if (word[end] == c && c == C_DQUOTE) { - if (word[end - 1] == C_BS && word[end - 2] != C_BS) + if (word[end - 1] == C_BACKS && word[end - 2] != C_BACKS) { end++; } } } - if (word[end] != C_NULL) + if (word[end] != C_NUL) { end++; } @@ -42,16 +42,16 @@ static size_t static size_t p_skip_unquote(const char word[], size_t end) { - while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) { end++; if (ft_iswhitespace(word[end]) == TRUE && end == 1 && - word[end - 1] == C_BS) + word[end - 1] == C_BACKS) { end++; } else if (ft_iswhitespace(word[end]) == TRUE && end > 1 && - word[end - 1] == C_BS && word[end - 2] != C_BS) + word[end - 1] == C_BACKS && word[end - 2] != C_BACKS) { end++; } @@ -62,7 +62,7 @@ static size_t static size_t p_skip_quote(const char word[], size_t end) { - while (word[end] != C_NULL && ft_iswhitespace(word[end]) == FALSE) + while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) { if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) { @@ -70,7 +70,7 @@ static size_t } else { - while (word[end] != C_NULL && + while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) { end++; @@ -87,7 +87,7 @@ size_t end = start; if (word[start] != C_SQUOTE && word[start] != C_DQUOTE && - word[start] != C_NULL) + word[start] != C_NUL) { end = p_skip_unquote(word, end); } -- cgit v1.2.3 From bea195a2e8d8e26af7511a65af4c8641cb4d1c4f Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 17:28:06 +0200 Subject: Even better parse --- src/p_args_len.c | 93 +++++++++++++++----------------------------------------- 1 file changed, 24 insertions(+), 69 deletions(-) (limited to 'src/p_args_len.c') diff --git a/src/p_args_len.c b/src/p_args_len.c index 801406f..3e5bfbf 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -14,87 +14,42 @@ #include #include "d_define.h" +#include "u_parse.h" +#include "u_utils.h" -static size_t - p_skip_delim_size(const char word[], char c, size_t end) +static t_bool + p_meet_whitespace(const char *head, char *ptr, t_quote_mode mode) { - end++; - if (word[end] == c) - return (end + 1); - while (word[end] != C_NUL && word[end] != c) + if (mode == Q_NONE && u_is_not_escaped(head, ptr) == TRUE) { - end++; - if (word[end] == c && c == C_DQUOTE) - { - if (word[end - 1] == C_BACKS && word[end - 2] != C_BACKS) - { - end++; - } - } + return (TRUE); } - if (word[end] != C_NUL) - { - end++; - } - return (end); -} - -static size_t - p_skip_unquote(const char word[], size_t end) -{ - while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) - { - end++; - if (ft_iswhitespace(word[end]) == TRUE && end == 1 && - word[end - 1] == C_BACKS) - { - end++; - } - else if (ft_iswhitespace(word[end]) == TRUE && end > 1 && - word[end - 1] == C_BACKS && word[end - 2] != C_BACKS) - { - end++; - } - } - return (end); -} - -static size_t - p_skip_quote(const char word[], size_t end) -{ - while (word[end] != C_NUL && ft_iswhitespace(word[end]) == FALSE) - { - if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) - { - end = p_skip_delim_size(word, word[end], end); - } - else - { - while (word[end] != C_NUL && - ft_iswhitespace(word[end]) == FALSE) - { - end++; - } - } - } - return (end); + return (FALSE); } size_t p_arg_len(const char word[], const size_t start) { - size_t end; + t_quote_mode mode; + char *ptr; + size_t end; + t_bool terminate; + mode = Q_NONE; end = start; - if (word[start] != C_SQUOTE && word[start] != C_DQUOTE && - word[start] != C_NUL) - { - end = p_skip_unquote(word, end); - } - else if (word[end] == C_SQUOTE || word[end] == C_DQUOTE) + ptr = (char*)word + start; + terminate = FALSE; + while (*ptr != C_NUL && terminate == FALSE) { - end = p_skip_quote(word, end); + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(word, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(word, ptr, mode); + else if (ft_iswhitespace(*ptr)) + terminate = p_meet_whitespace(word, ptr, mode); + ptr++; } - return (end); + ptr -= 1; + return (ptr - word); } -- cgit v1.2.3 From f165808b1cb551cd86655268b8dfa4fb7dcd5eb7 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 10 Sep 2020 18:13:47 +0200 Subject: FIX --- src/p_args_len.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/p_args_len.c') diff --git a/src/p_args_len.c b/src/p_args_len.c index 3e5bfbf..e624b37 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -49,7 +49,8 @@ size_t terminate = p_meet_whitespace(word, ptr, mode); ptr++; } - ptr -= 1; + if (*ptr != C_NUL) + ptr -= 1; return (ptr - word); } -- cgit v1.2.3 From b2ffd2aeb4a91501ae7931cb43a9679b01eb0351 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Fri, 11 Sep 2020 16:25:51 +0200 Subject: Nice fix --- src/p_args_len.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/p_args_len.c') diff --git a/src/p_args_len.c b/src/p_args_len.c index e624b37..729e5c5 100644 --- a/src/p_args_len.c +++ b/src/p_args_len.c @@ -45,11 +45,11 @@ size_t mode = u_meet_dquote(word, ptr, mode); else if (*ptr == C_SQUOTE) mode = u_meet_squote(word, ptr, mode); - else if (ft_iswhitespace(*ptr)) + else if (ft_iswhitespace(*ptr) == TRUE) terminate = p_meet_whitespace(word, ptr, mode); ptr++; } - if (*ptr != C_NUL) + if (terminate == TRUE) ptr -= 1; return (ptr - word); } -- cgit v1.2.3