diff --git a/ft_split.c b/ft_split.c deleted file mode 100644 index 73b7949..0000000 --- a/ft_split.c +++ /dev/null @@ -1,111 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_split.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lderidde +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/07/17 12:48:12 by lderidde #+# #+# */ -/* Updated: 2024/07/25 14:13:12 by lderidde ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include - -int check_sep(char c, char *charset) -{ - while (*charset) - { - if (c == *charset) - return (1); - charset++; - } - return (0); -} - -int count_strings(char *str, char *charset) -{ - int i; - int count; - - i = 0; - count = 0; - while (str[i]) - { - if (!(check_sep(str[i], charset)) - && (check_sep(str[i + 1], charset) - || str[i + 1] == '\0')) - count++; - i++; - } - return (count); -} - -void build_str(char *charset, char *src, char *dest) -{ - int i; - - i = 0; - while (src[i] && !(check_sep(src[i], charset))) - { - dest[i] = src[i]; - i++; - } - dest[i] = '\0'; -} - -void build_tab(char **tab, char *str, char *charset) -{ - int i; - int count; - int j; - - i = 0; - count = 0; - while (str[i]) - { - if (check_sep(str[i], charset)) - i++; - else - { - j = 0; - while (!(check_sep(str[j + i], charset)) && str[j + i]) - j++; - tab[count] = malloc(sizeof(char) * (j + 1)); - build_str(charset, &str[i], tab[count]); - count++; - i = i + j; - } - } -} - -char **ft_split(char *str, char *charset) -{ - int count; - char **tab; - - count = count_strings(str, charset); - tab = malloc(sizeof(char *) * (count + 1)); - if (tab == NULL) - return (NULL); - tab[count] = 0; - build_tab(tab, str, charset); - return (tab); -} -/* -#include -int main (int argc, char **argv) -{ - int i = 0; - (void)argc; - char **res = ft_split(argv[1], argv[2]); - int len = count_strings(argv[1], argv[2]); - while (i < len) - { - printf("%s\n", res[i]); - free(res[i]); - i++; - } - free(res); -} -*/ diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 56a9638..e8c894e 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -58,7 +58,8 @@ char *ft_itoa(int n); void *ft_calloc(size_t count, size_t size); void ft_bzero(void *s, size_t n); -char **ft_split(const char *s, char c); +char **ft_split(const char *s, char *set); +int is_charset(char c, char *set); void free_tab(char **tab); void ft_put_c_fd(char c, int fd); diff --git a/lib/libft/srcs/format/ft_split.c b/lib/libft/srcs/format/ft_split.c index 6bb27c6..e31a914 100644 --- a/lib/libft/srcs/format/ft_split.c +++ b/lib/libft/srcs/format/ft_split.c @@ -12,77 +12,85 @@ #include "../../libft.h" -static int ft_word_count(const char *str, char c) +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; - if (!str) - return (0); count = 0; - while (*str) + while (*s) { - while (*str && *str == c) - str++; - if (*str && *str != c) - count++; - while (*str && *str != c) - str++; + if (!is_charset(*s, set) && is_charset(*(s + 1), set) + || *(s + 1) == '\0') + count ++; + s++; } return (count); } -static char *malloccpy(const char *str, char c) +static void build_str(char const *src, char *dest, char *set) { - char *result; - int i; + int i; i = 0; - while (str[i] && str[i] != c) - i++; - result = (char *)malloc(sizeof(char) * (i + 1)); - if (!result) - return (NULL); - ft_strlcpy(result, str, i + 1); - return (result); -} - -void free_tab(char **tab) -{ - char **pos; - - if (!tab) - return ; - pos = tab; - while (*pos != NULL) - free(*(pos++)); - free(tab); -} - -char **ft_split(const char *s, char c) -{ - char **result; - int i; - int strs_len; - - if (!s) - return (NULL); - strs_len = ft_word_count(s, c); - result = (char **)malloc(sizeof(char *) * (strs_len + 1)); - if (!result) - return (NULL); - i = -1; - while (++i < strs_len) + while (src[i] && !is_charset(src[i], set)) { - while (*s && *s == c) - s++; - result[i] = malloccpy(s, c); - if (!result[i]) - { - free_tab(result); - return (NULL); - } - s += ft_strlen(result[i]); + dest[i] = src[i]; + i++; } - result[i] = 0; - return (result); + dest[i] = '\0'; +} + +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 (!is_charset(s[i + j], set) && s[i + j]) + 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 = i + j; + } + } + return (tab); +} + +char **ft_split(char const *s, char *set) +{ + int count; + char **tab; + + count = count_strings(s, set); + tab = ft_calloc(count + 1, sizeof(char *)); + if (!tab) + return (NULL); + tab[count] = 0; + 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 new file mode 100644 index 0000000..e83f3f8 --- /dev/null +++ b/lib/libft/srcs/format/ft_split_utils.c @@ -0,0 +1,15 @@ +#include "lib/libft/libft.h" + +int is_charset(char c, char *set) +{ + int i; + + i = 0; + while (set[i]) + { + if (c == set[i]) + return (1); + i++; + } + return (0); +}