mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
wordsplit: handle single word with spaces before or after
This commit is contained in:
parent
fc985ea046
commit
8defbf4d13
9 changed files with 154 additions and 16 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
29
libft/libft/ft_strchridx.c
Normal file
29
libft/libft/ft_strchridx.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strchridx.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/14 14:02:34 by khais #+# #+# */
|
||||
/* Updated: 2025/02/14 15:12:01 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/*
|
||||
** 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);
|
||||
}
|
||||
31
libft/libft/ft_strnchridx.c
Normal file
31
libft/libft/ft_strnchridx.c
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strnchridx.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/14 14:16:32 by khais #+# #+# */
|
||||
/* Updated: 2025/02/14 15:12:19 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/*
|
||||
** 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);
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,12 +6,12 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdlib.h>
|
||||
#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)));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue