From 00fd2380cf2a5a81193a3e7e9bec0167a14b9d5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 14 Feb 2025 15:06:01 +0100 Subject: [PATCH] wordsplit: handle a sigle word separated surrounded by blanks a blank here is as defined by POSIX https://pubs.opengroup.org/onlinepubs/9699919799/ section 7.3.1 --- src/parser/wordsplit/wordsplit.c | 11 ++++++++--- tests/word_splitting.c | 8 ++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/parser/wordsplit/wordsplit.c b/src/parser/wordsplit/wordsplit.c index 54b43ee..5db7d6d 100644 --- a/src/parser/wordsplit/wordsplit.c +++ b/src/parser/wordsplit/wordsplit.c @@ -6,18 +6,23 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 17:02:32 by khais #+# #+# */ -/* Updated: 2025/02/14 14:19:30 by khais ### ########.fr */ +/* Updated: 2025/02/14 15:22:57 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "wordsplit.h" #include "libft.h" +#include "../matchers/blank.h" /* ** split a string into words, respecting quotes etc. ** ** cf. Token Recognition section at ** https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html +** +** words are separated with , which are defined here +** https://pubs.opengroup.org/onlinepubs/9699919799/ +** section 7.3.1 LC_CTYPE */ t_wordlist *minishell_wordsplit(char *original) { @@ -25,8 +30,8 @@ t_wordlist *minishell_wordsplit(char *original) size_t length; char *outstr; - start = ft_strnchridx(original, ' '); - length = ft_strchridx(original + start, ' '); + start = ft_strnfchridx(original, is_blank); + length = ft_strfchridx(original + start, is_blank); outstr = ft_substr(original, start, length); return (wordlist_create(worddesc_create(outstr))); } diff --git a/tests/word_splitting.c b/tests/word_splitting.c index 205ff42..29ca564 100644 --- a/tests/word_splitting.c +++ b/tests/word_splitting.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 15:17:56 by khais #+# #+# */ -/* Updated: 2025/02/14 13:35:26 by khais ### ########.fr */ +/* Updated: 2025/02/14 15:06:50 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,11 +28,11 @@ void test_wordsplit_singleword(void) wordlist_destroy(words); } -void test_wordsplit_singleword_with_spaces(void) +void test_wordsplit_singleword_with_blanks(void) { t_wordlist *words; - words = minishell_wordsplit(" echo "); + words = minishell_wordsplit("\t \t echo \t\t "); assert_strequal("echo", wordlist_get(words, 0)->word); assert(NULL == wordlist_get(words, 1)); wordlist_destroy(words); @@ -55,6 +55,6 @@ void test_wordsplit_basic(void) int main(void) { test_wordsplit_singleword(); - test_wordsplit_singleword_with_spaces(); + test_wordsplit_singleword_with_blanks(); return (0); }