From 281c8c0f63f3da2af534fbc39ddd29bc8b8c75b7 Mon Sep 17 00:00:00 2001 From: Nathan Lebrun Date: Tue, 21 Jan 2025 12:02:30 +0100 Subject: [PATCH] split_keep --- lib/libft/libft.h | 2 + lib/libft/srcs/format/ft_split.c | 24 ++---- lib/libft/srcs/format/ft_split_keep.c | 102 +++++++++++++++++++++++++ lib/libft/srcs/format/ft_split_utils.c | 26 +++++++ 4 files changed, 135 insertions(+), 19 deletions(-) create mode 100644 lib/libft/srcs/format/ft_split_keep.c diff --git a/lib/libft/libft.h b/lib/libft/libft.h index e8c894e..0b42e70 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -59,8 +59,10 @@ void *ft_calloc(size_t count, size_t size); void ft_bzero(void *s, size_t n); char **ft_split(const char *s, char *set); +char **ft_split_keep(const char *s, char *set); int is_charset(char c, char *set); void free_tab(char **tab); +char **free_all(char **tab, int count); void ft_put_c_fd(char c, int fd); void ft_put_s_fd(char *s, int fd); diff --git a/lib/libft/srcs/format/ft_split.c b/lib/libft/srcs/format/ft_split.c index 50163a1..5f3028b 100644 --- a/lib/libft/srcs/format/ft_split.c +++ b/lib/libft/srcs/format/ft_split.c @@ -3,29 +3,15 @@ /* ::: :::::::: */ /* ft_split.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: nalebrun +#+ +:+ +#+ */ +/* By: nalebrun +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/10/11 16:15:47 by nalebrun #+# #+# */ -/* Updated: 2024/11/27 12:58:41 by nalebrun ### ########.fr */ +/* Created: 2025/01/21 11:31:07 by nalebrun #+# #+# */ +/* Updated: 2025/01/21 11:31:07 by nalebrun ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../libft.h" -static char **free_all(char **tab, int count) -{ - int i; - - i = 0; - while (i < count) - { - free(tab[i]); - i++; - } - free(tab); - return (NULL); -} - static int count_strings(char const *s, char *set) { int count; @@ -33,8 +19,8 @@ static int count_strings(char const *s, char *set) count = 0; while (*s) { - if (!is_charset(*s, set) && ( is_charset(*(s + 1), set) - || *(s + 1) == '\0')) + if (!is_charset(*s, set) && (is_charset(*(s + 1), set) + || *(s + 1) == '\0')) count ++; s++; } diff --git a/lib/libft/srcs/format/ft_split_keep.c b/lib/libft/srcs/format/ft_split_keep.c new file mode 100644 index 0000000..24c4a71 --- /dev/null +++ b/lib/libft/srcs/format/ft_split_keep.c @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_keep.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/21 11:31:48 by nalebrun #+# #+# */ +/* Updated: 2025/01/21 11:31:48 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../libft.h" + +static int count_strings(char const *s, char *set) +{ + int count; + int in_word; + + count = 0; + in_word = 0; + while (*s) + { + if (!is_charset(*s, set) && !in_word) + { + count++; + in_word = 1; + } + else if (is_charset(*s, set)) + in_word = 0; + s++; + } + return (count); +} + +static void build_str(char const *src, char *dest, char *set) +{ + int i; + + i = 0; + while (src[i] && !is_charset(src[i], set)) + { + dest[i] = src[i]; + i++; + } + while (src[i] && is_charset(src[i], set)) + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; +} + +static char **alloc_tab(char const *s, char *set) +{ + int count; + char **tab; + + count = count_strings(s, set); + tab = ft_calloc(count + 1, sizeof(char *)); + return (tab); +} + +static char **build_tab(char **tab, char const *s, char *set) +{ + int i; + int j; + int count; + + i = 0; + count = 0; + while (tab && s[i]) + { + if (is_charset(s[i], set)) + i++; + else + { + j = 0; + while (s[i + j] && !is_charset(s[i + j], set)) + j++; + while (s[i + j] && is_charset(s[i + j], set)) + j++; + tab[count] = ft_calloc((j + 1), sizeof(char)); + if (!tab[count]) + return (free_all(tab, count)); + build_str(&s[i], tab[count++], set); + i += j; + } + } + return (tab); +} + +char **ft_split_keep(char const *s, char *set) +{ + char **tab; + + tab = alloc_tab(s, set); + if (!tab) + return (NULL); + tab = build_tab(tab, s, set); + return (tab); +} diff --git a/lib/libft/srcs/format/ft_split_utils.c b/lib/libft/srcs/format/ft_split_utils.c index 3baf6f6..dd44b0b 100644 --- a/lib/libft/srcs/format/ft_split_utils.c +++ b/lib/libft/srcs/format/ft_split_utils.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/21 11:31:30 by nalebrun #+# #+# */ +/* Updated: 2025/01/21 11:31:30 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../../libft.h" int is_charset(char c, char *set) @@ -26,3 +38,17 @@ void free_tab(char **tab) } free(tab); } + +char **free_all(char **tab, int count) +{ + int i; + + i = 0; + while (i < count) + { + free(tab[i]); + i++; + } + free(tab); + return (NULL); +}