diff options
author | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-01-23 20:18:36 +0100 |
---|---|---|
committer | Rudy Bousset <rbousset@z2r4p3.le-101.fr> | 2020-01-23 20:18:36 +0100 |
commit | daf41fc015b4cd475e6169fb6186fddf46c7a5ac (patch) | |
tree | 870bc7c6d4c6e361158f9d6307f1bdffd940eb64 /libft/src/ft_split_redo.c | |
parent | ok but might redo ft_split (diff) | |
download | 42-cub3d-daf41fc015b4cd475e6169fb6186fddf46c7a5ac.tar.gz 42-cub3d-daf41fc015b4cd475e6169fb6186fddf46c7a5ac.tar.bz2 42-cub3d-daf41fc015b4cd475e6169fb6186fddf46c7a5ac.tar.xz 42-cub3d-daf41fc015b4cd475e6169fb6186fddf46c7a5ac.tar.zst 42-cub3d-daf41fc015b4cd475e6169fb6186fddf46c7a5ac.zip |
Work in progress, split redone
Diffstat (limited to '')
-rw-r--r-- | libft/src/ft_split_redo.c | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/libft/src/ft_split_redo.c b/libft/src/ft_split_redo.c new file mode 100644 index 0000000..1d33dda --- /dev/null +++ b/libft/src/ft_split_redo.c @@ -0,0 +1,101 @@ +#include <libft.h> +#include <stddef.h> +#include <stdlib.h> +#include <inttypes.h> + +static size_t +ft_count_words(const char *s, char c) +{ + size_t i; + size_t count; + uint8_t ibool; + + i = 0; + count = 0; + ibool = 1; + while (s[i]) + { + while (s[i] == c && s[i]) + i++; + while (s[i] != c && s[i]) + { + if (ibool == 1) + count++; + ibool = 0; + i++; + } + ibool = 1; + } + return (count); +} + +static size_t +ft_splitlen(const char *str, char c) +{ + size_t i; + + i = 0; + while (str[i] != c && str[i]) + i++; + return (i); +} + +static char + *ft_splitdup(const char *str, char c) +{ + char *word; + size_t i; + + i = 0; + if (!(word = (char*)malloc((ft_splitlen(str, c) + 1) * sizeof(char)))) + return (NULL); + while (str[i] != c && str[i]) + { + word[i] = str[i]; + i++; + } + word[i] = '\0'; + return (word); +} + +static void +ft_splitfree(char **best_split, size_t j) +{ + while (j > 0) + { + free(best_split[j]); + j--; + } + free(best_split); +} + +char + **ft_split(const char *s, char c) +{ + char **best_split; + size_t i; + size_t j; + + i = 0; + j = 0; + if (!(best_split = (char **)malloc((ft_count_words(s, c) + 1) + * sizeof(char *)))) + return (NULL); + while (s[i]) + { + while (s[i] == c && s[i]) + i++; + while (s[i] != c && s[i]) + { + if (!(best_split[j] = ft_splitdup(s + i, c))) + { + ft_splitfree(best_split, j); + return (NULL); + } + i += ft_splitlen(s + i, c); + j++; + } + } + best_split[j] = NULL; + return (best_split); +} |