wordsplit: set quote and dquote flags

This commit is contained in:
Khaïs COLIN 2025-02-20 14:22:38 +01:00
parent ac8475c71d
commit abf37eb7f5
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
3 changed files with 61 additions and 10 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/19 13:20:01 by jguelen #+# #+# */
/* Updated: 2025/02/20 14:10:57 by khais ### ########.fr */
/* Updated: 2025/02/20 14:17:03 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -78,6 +78,9 @@ bool rule_quote(t_token_build *builder, char *original)
if (is_quote(original[builder->idx]))
{
quote_flip(builder, original[builder->idx]);
builder->cur_flags |= W_QUOTED;
if (builder->quote == '"')
builder->cur_flags |= W_DQUOTE;
builder->idx++;
return (true);
}

View file

@ -5,18 +5,14 @@
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 14:10/48 by khais #+# #+# */
/* Updated: 2025/02/20 14:10:48 by khais ### ########.fr */
/* Created: 2025/02/20 15:39/01 by khais #+# #+# */
/* Updated: 2025/02/20 15:39:01 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "wordsplit.h"
#include "libft.h"
/*
** TODO: set flags
*/
/*
** split a string into words, respecting quotes etc.
**

View file

@ -6,15 +6,13 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:17:56 by khais #+# #+# */
/* Updated: 2025/02/20 14:05:21 by khais ### ########.fr */
/* Updated: 2025/02/20 14:22:06 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include <assert.h>
#include "testutil.h"
#include "libft.h"
#include "../src/parser/wordsplit/wordsplit.h"
#include "unistd.h"
#include <stdlib.h>
/*
@ -187,7 +185,58 @@ static void test_wordsplit_var_substitution(void)
assert_strequal("here", wordlist_get(words, 2)->word);
assert(0 == wordlist_get(words, 2)->flags);
wordlist_destroy(words);
}
static void test_wordsplit_quote_detection_nonnested(void)
{
t_wordlist *words;
words = minishell_wordsplit("echo 'single quotes' here \"double quotes\" here");
assert_strequal("echo", wordlist_get(words, 0)->word);
assert(0 == wordlist_get(words, 0)->flags);
assert_strequal("'single quotes'", wordlist_get(words, 1)->word);
assert(W_QUOTED == wordlist_get(words, 1)->flags);
assert_strequal("here", wordlist_get(words, 2)->word);
assert(0 == wordlist_get(words, 2)->flags);
assert_strequal("\"double quotes\"", wordlist_get(words, 3)->word);
assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 3)->flags);
assert_strequal("here", wordlist_get(words, 4)->word);
assert(0 == wordlist_get(words, 4)->flags);
wordlist_destroy(words);
}
static void test_wordsplit_quote_detection_nested_double_in_simple(void)
{
t_wordlist *words;
char *str;
str = "'these are single quotes \"with double\" inside'";
words = minishell_wordsplit(str);
assert_strequal(str, wordlist_get(words, 0)->word);
assert(W_QUOTED == wordlist_get(words, 0)->flags);
wordlist_destroy(words);
str = "'\"these are single quotes with double inside\"'";
words = minishell_wordsplit(str);
assert_strequal(str, wordlist_get(words, 0)->word);
assert(W_QUOTED == wordlist_get(words, 0)->flags);
wordlist_destroy(words);
}
static void test_wordsplit_quote_detection_nested_single_in_double(void)
{
t_wordlist *words;
char *str;
str = "\"these are double quotes 'with single' inside\"";
words = minishell_wordsplit(str);
assert_strequal(str, wordlist_get(words, 0)->word);
assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 0)->flags);
wordlist_destroy(words);
str = "\"'these are double quotes with single inside'\"";
words = minishell_wordsplit(str);
assert_strequal(str, wordlist_get(words, 0)->word);
assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 0)->flags);
wordlist_destroy(words);
}
int main(void) {
@ -205,5 +254,8 @@ int main(void) {
test_wordsplit_all_operators();
test_wordsplit_operator_combining();
test_wordsplit_var_substitution();
test_wordsplit_quote_detection_nonnested();
test_wordsplit_quote_detection_nested_double_in_simple();
test_wordsplit_quote_detection_nested_single_in_double();
return (0);
}