diff --git a/libft/libft.h b/libft/libft.h index 3011865..60cb00b 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* By: jguelen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 16:06:09 by jguelen #+# #+# */ -/* Updated: 2025/02/20 14:44:53 by khais ### ########.fr */ +/* Updated: 2025/02/20 14:48:28 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -130,6 +130,8 @@ size_t ft_strlcat(char *dst, const char *src, size_t size); int ft_toupper(int c); int ft_tolower(int c); char *ft_strchr(const char *s, int c); +size_t ft_strchridx(const char *s, char c); +size_t ft_strnchridx(const char *s, char c); char *ft_strrchr(const char *s, int c); int ft_strcmp(const char *s1, const char *s2); int ft_strncmp(const char *s1, const char *s2, size_t n); diff --git a/libft/libft/Makefile b/libft/libft/Makefile index cebfad2..4eb2890 100644 --- a/libft/libft/Makefile +++ b/libft/libft/Makefile @@ -4,14 +4,57 @@ ifeq ($(CFLAGS),) CFLAGS = -Wall -Wextra -Werror -g endif NAME = libft.a -SOURCES = free2.c \ - ft_atoi.c ft_itoa.c ft_putendl_fd.c ft_strlcat.c ft_substr.c \ - ft_bzero.c ft_putnbr_fd.c ft_strlcpy.c ft_tolower.c ft_calloc.c \ - ft_memchr.c ft_putstr_fd.c ft_strlen.c ft_toupper.c ft_isalnum.c \ - ft_memcmp.c ft_split.c ft_strmapi.c ft_isalpha.c ft_memcpy.c ft_strchr.c \ - ft_strcmp.c ft_strncmp.c ft_isascii.c ft_memmove.c ft_strdup.c ft_strnstr.c \ - ft_isdigit.c ft_memset.c ft_striteri.c ft_strrchr.c ft_isprint.c \ - ft_putchar_fd.c ft_strjoin.c ft_strtrim.c ft_min_max.c ft_strjoin_sepc.c +SOURCES = \ + free2.c \ + ft_atoi.c \ + ft_bzero.c \ + ft_calloc.c \ + ft_isalnum.c \ + ft_isalpha.c \ + ft_isascii.c \ + ft_isdigit.c \ + ft_isprint.c \ + ft_itoa.c \ + ft_lstadd_back_bonus.c \ + ft_lstadd_front_bonus.c \ + ft_lstclear_bonus.c \ + ft_lstdelone_bonus.c \ + ft_lstiter_bonus.c \ + ft_lstlast_bonus.c \ + ft_lstmap_bonus.c \ + ft_lstnew_bonus.c \ + ft_lstsize_bonus.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_memcpy.c \ + ft_memmove.c \ + ft_memset.c \ + ft_min_max.c \ + ft_putchar_fd.c \ + ft_putendl_fd.c \ + ft_putnbr_fd.c \ + ft_putstr_fd.c \ + ft_split.c \ + ft_strchr.c \ + ft_strchridx.c \ + ft_strcmp.c \ + ft_strdup.c \ + ft_striteri.c \ + ft_strjoin.c \ + ft_strjoin_sepc.c \ + ft_strlcat.c \ + ft_strlcpy.c \ + ft_strlen.c \ + ft_strmapi.c \ + ft_strnchridx.c \ + ft_strncmp.c \ + ft_strnstr.c \ + ft_strrchr.c \ + ft_strtrim.c \ + ft_substr.c \ + ft_tolower.c \ + ft_toupper.c \ + BONUSSOURCES = ft_lstnew_bonus.c ft_lstadd_back_bonus.c ft_lstsize_bonus.c \ ft_lstadd_front_bonus.c ft_lstclear_bonus.c ft_lstdelone_bonus.c \ ft_lstiter_bonus.c ft_lstlast_bonus.c ft_lstmap_bonus.c diff --git a/libft/libft/ft_strchridx.c b/libft/libft/ft_strchridx.c new file mode 100644 index 0000000..57f70f3 --- /dev/null +++ b/libft/libft/ft_strchridx.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strchridx.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/14 14:02:34 by khais #+# #+# */ +/* Updated: 2025/02/14 15:12:01 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +/* +** return the index of the character c in the string s, or 0 if s is null, or +** length of s if c is not found. +*/ +size_t ft_strchridx(const char *s, char c) +{ + size_t i; + + if (s == NULL) + return (0); + i = 0; + while (s[i] != c && s[i] != '\0') + i++; + return (i); +} diff --git a/libft/libft/ft_strnchridx.c b/libft/libft/ft_strnchridx.c new file mode 100644 index 0000000..2b62e91 --- /dev/null +++ b/libft/libft/ft_strnchridx.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strnchridx.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/14 14:16:32 by khais #+# #+# */ +/* Updated: 2025/02/14 15:12:19 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +/* +** return the index of the first character that is not c in the string s, or 0 +** if not found or s is null. +*/ +size_t ft_strnchridx(const char *s, char c) +{ + size_t i; + + if (s == NULL) + return (0); + i = 0; + while (s[i] == c && s[i] != '\0') + i++; + if (s[i] == '\0') + return (0); + return (i); +} diff --git a/src/parser/worddesc/worddesc.c b/src/parser/worddesc/worddesc.c index e46f617..f7f7081 100644 --- a/src/parser/worddesc/worddesc.c +++ b/src/parser/worddesc/worddesc.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 17:20:36 by khais #+# #+# */ -/* Updated: 2025/02/13 17:23:11 by khais ### ########.fr */ +/* Updated: 2025/02/14 15:23:44 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,3 +31,14 @@ t_worddesc *worddesc_create(char *word) retvalue->word = word; return (retvalue); } + +/* +** free all memory associated with this worddesc +*/ +void worddesc_destroy(t_worddesc *worddesc) +{ + if (worddesc == NULL) + return ; + free(worddesc->word); + free(worddesc); +} diff --git a/src/parser/worddesc/worddesc.h b/src/parser/worddesc/worddesc.h index fbcb86a..3fbf19b 100644 --- a/src/parser/worddesc/worddesc.h +++ b/src/parser/worddesc/worddesc.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 15:47:58 by khais #+# #+# */ -/* Updated: 2025/02/13 17:20:25 by khais ### ########.fr */ +/* Updated: 2025/02/14 13:57:10 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,5 +29,6 @@ typedef struct s_worddesc } t_worddesc; t_worddesc *worddesc_create(char *word); +void worddesc_destroy(t_worddesc *worddesc); #endif diff --git a/src/parser/wordlist/wordlist.c b/src/parser/wordlist/wordlist.c index be888e3..1aebfea 100644 --- a/src/parser/wordlist/wordlist.c +++ b/src/parser/wordlist/wordlist.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 17:07:01 by khais #+# #+# */ -/* Updated: 2025/02/14 13:29:16 by khais ### ########.fr */ +/* Updated: 2025/02/14 13:56:45 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,9 @@ t_wordlist *wordlist_create(t_worddesc *word) */ void wordlist_destroy(t_wordlist *wordlist) { + if (wordlist == NULL) + return ; + worddesc_destroy(wordlist->word); free(wordlist); } diff --git a/src/parser/wordsplit/wordsplit.c b/src/parser/wordsplit/wordsplit.c index a497995..54b43ee 100644 --- a/src/parser/wordsplit/wordsplit.c +++ b/src/parser/wordsplit/wordsplit.c @@ -6,12 +6,12 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 17:02:32 by khais #+# #+# */ -/* Updated: 2025/02/14 13:28:38 by khais ### ########.fr */ +/* Updated: 2025/02/14 14:19:30 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "wordsplit.h" -#include +#include "libft.h" /* ** split a string into words, respecting quotes etc. @@ -21,5 +21,12 @@ */ t_wordlist *minishell_wordsplit(char *original) { - return (wordlist_create(worddesc_create(original))); + size_t start; + size_t length; + char *outstr; + + start = ft_strnchridx(original, ' '); + length = ft_strchridx(original + start, ' '); + 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 12a50d8..205ff42 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/13 17:24:33 by khais ### ########.fr */ +/* Updated: 2025/02/14 13:35:26 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,16 @@ void test_wordsplit_singleword(void) wordlist_destroy(words); } +void test_wordsplit_singleword_with_spaces(void) +{ + t_wordlist *words; + + words = minishell_wordsplit(" echo "); + assert_strequal("echo", wordlist_get(words, 0)->word); + assert(NULL == wordlist_get(words, 1)); + wordlist_destroy(words); +} + void test_wordsplit_basic(void) { t_wordlist *words; @@ -45,5 +55,6 @@ void test_wordsplit_basic(void) int main(void) { test_wordsplit_singleword(); + test_wordsplit_singleword_with_spaces(); return (0); }