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
This commit is contained in:
Khaïs COLIN 2025-02-14 15:06:01 +01:00
parent aa12f7c971
commit 00fd2380cf
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 12 additions and 7 deletions

View file

@ -6,18 +6,23 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 17:02:32 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 "wordsplit.h"
#include "libft.h" #include "libft.h"
#include "../matchers/blank.h"
/* /*
** split a string into words, respecting quotes etc. ** split a string into words, respecting quotes etc.
** **
** cf. Token Recognition section at ** cf. Token Recognition section at
** https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html ** https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
**
** words are separated with <blanks>, which are defined here
** https://pubs.opengroup.org/onlinepubs/9699919799/
** section 7.3.1 LC_CTYPE
*/ */
t_wordlist *minishell_wordsplit(char *original) t_wordlist *minishell_wordsplit(char *original)
{ {
@ -25,8 +30,8 @@ t_wordlist *minishell_wordsplit(char *original)
size_t length; size_t length;
char *outstr; char *outstr;
start = ft_strnchridx(original, ' '); start = ft_strnfchridx(original, is_blank);
length = ft_strchridx(original + start, ' '); length = ft_strfchridx(original + start, is_blank);
outstr = ft_substr(original, start, length); outstr = ft_substr(original, start, length);
return (wordlist_create(worddesc_create(outstr))); return (wordlist_create(worddesc_create(outstr)));
} }

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 15:17:56 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); wordlist_destroy(words);
} }
void test_wordsplit_singleword_with_spaces(void) void test_wordsplit_singleword_with_blanks(void)
{ {
t_wordlist *words; t_wordlist *words;
words = minishell_wordsplit(" echo "); words = minishell_wordsplit("\t \t echo \t\t ");
assert_strequal("echo", wordlist_get(words, 0)->word); assert_strequal("echo", wordlist_get(words, 0)->word);
assert(NULL == wordlist_get(words, 1)); assert(NULL == wordlist_get(words, 1));
wordlist_destroy(words); wordlist_destroy(words);
@ -55,6 +55,6 @@ void test_wordsplit_basic(void)
int main(void) { int main(void) {
test_wordsplit_singleword(); test_wordsplit_singleword();
test_wordsplit_singleword_with_spaces(); test_wordsplit_singleword_with_blanks();
return (0); return (0);
} }