diff options
Diffstat (limited to 'libft/src/ft_split.c')
-rw-r--r-- | libft/src/ft_split.c | 84 |
1 files changed, 43 insertions, 41 deletions
diff --git a/libft/src/ft_split.c b/libft/src/ft_split.c index f0cac95..3035a00 100644 --- a/libft/src/ft_split.c +++ b/libft/src/ft_split.c @@ -1,14 +1,13 @@ /* ************************************************************************** */ -/* LE - / */ -/* / */ -/* ft_split.c .:: .:/ . .:: */ -/* +:+:+ +: +: +:+:+ */ -/* By: rbousset <marvin@le-101.fr> +:+ +: +: +:+ */ -/* #+# #+ #+ #+# */ -/* Created: 2019/10/12 19:24:20 by rbousset #+# ## ## #+# */ -/* Updated: 2019/10/13 08:37:16 by rbousset ### #+. /#+ ###.fr */ -/* / */ -/* / */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:07:05 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:07:05 by rbousset ### ########lyon.fr */ +/* */ /* ************************************************************************** */ #include <libft.h> @@ -16,16 +15,8 @@ #include <stdlib.h> #include <inttypes.h> -static uint8_t - ft_check(int c, char sep) -{ - if (c == sep) - return (1); - return (0); -} - static size_t - ft_strlen_plus(const char *str, char c) + ft_count_words(const char *s, char c) { size_t i; size_t count; @@ -34,11 +25,11 @@ static size_t i = 0; count = 0; ibool = 1; - while (str[i]) + while (s[i]) { - while (ft_check(str[i], c) && str[i]) + while (s[i] == c && s[i]) i++; - while (!ft_check(str[i], c) && str[i]) + while (s[i] != c && s[i]) { if (ibool == 1) count++; @@ -51,59 +42,70 @@ static size_t } static size_t - ft_strlen_again(const char *str, char c) + ft_splitlen(const char *str, char c) { size_t i; i = 0; - while (!ft_check(str[i], c) && str[i]) + while (str[i] != c && str[i]) i++; return (i); } static char - *ft_strdup_plus(const char *src, char c) + *ft_splitdup(const char *str, char c) { + char *word; size_t i; - size_t slen; - char *nstr; i = 0; - slen = ft_strlen_again(src, c) + 1; - if (!(nstr = (char*)ft_calloc(slen, sizeof(char)))) + if (!(word = (char*)malloc((ft_splitlen(str, c) + 1) * sizeof(char)))) return (NULL); - while (!ft_check(src[i], c) && src[i]) + while (str[i] != c && str[i]) { - nstr[i] = src[i]; + word[i] = str[i]; i++; } - nstr[i] = '\0'; - return (nstr); + word[i] = '\0'; + return (word); +} + +static char + **ft_splitfree(char **best_split, size_t j) +{ + while (j > 0) + { + ft_memdel((void**)&best_split[j]); + j--; + } + ft_memdel((void**)best_split); + return (NULL); } char **ft_split(const char *s, char c) { + char **best_split; size_t i; size_t j; - char **best_split; i = 0; j = 0; - if (!(best_split = (char **)ft_calloc(ft_strlen_plus(s, c) + 1, - sizeof(char *)))) + if (!(best_split = (char **)malloc((ft_count_words(s, c) + 1) + * sizeof(char *)))) return (NULL); while (s[i]) { - while (ft_check(s[i], c) && s[i]) + while (s[i] == c && s[i]) i++; - while (!ft_check(s[i], c) && s[i]) + while (s[i] != c && s[i]) { - best_split[j] = ft_strdup_plus(&s[i], c); - i += ft_strlen_again(&s[i], c); + if (!(best_split[j] = ft_splitdup(s + i, c))) + return (ft_splitfree(best_split, j)); + i += ft_splitlen(s + i, c); j++; } } - best_split[j] = 0; + best_split[j] = NULL; return (best_split); } |