diff --git a/Makefile b/Makefile index aaf23b7..57b6219 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/17 12:36:39 by kcolin ### ########.fr # +# Updated: 2024/10/17 14:12:23 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -37,7 +37,8 @@ SOURCES = ft_isalpha.c \ ft_strdup.c \ ft_substr.c \ ft_strjoin.c \ - ft_strtrim.c + ft_strtrim.c \ + ft_split.c OBJECTS = $(SOURCES:.c=.o) CC = gcc diff --git a/ft_split.c b/ft_split.c new file mode 100644 index 0000000..fd65651 --- /dev/null +++ b/ft_split.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 16:09:01 by kcolin #+# #+# */ +/* Updated: 2024/10/17 16:50:01 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +static size_t count_segments(char const *s, char c) +{ + size_t i; + int count; + + i = 0; + count = 0; + while (s[i] != '\0') + { + if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0')) + count++; + i++; + } + return (count); +} + +static char *ft_strnchr(const char *s, int c) +{ + int i; + + i = 0; + while (s[i] != '\0') + { + if (s[i] != c) + return ((char *)s + i); + i++; + } + return (0); +} + +static void free_buf(char **buf, size_t length) +{ + size_t i; + + i = 0; + while (i < length) + { + free(buf[i]); + i++; + } + free(buf); +} + +char **ft_split(char const *s, char c) +{ + char **out; + size_t s_idx; + size_t out_idx; + size_t length; + + out = ft_calloc(count_segments(s, c) + 1, sizeof(char *)); + if (out == NULL) + return (NULL); + s_idx = 0; + out_idx = 0; + while (out_idx < count_segments(s, c)) + { + s_idx = ft_strnchr(s + s_idx, c) - s; + length = ft_strchr(s + s_idx, c) - s - s_idx; + out[out_idx] = ft_substr(s, s_idx, length); + if (out[out_idx] == NULL) + { + free_buf(out, out_idx); + return (NULL); + } + s_idx += length; + out_idx++; + } + return (out); +} + +/* +#include // TODO: BAD + +int main(int argc, char **argv) +{ + if (argc == 3) + { + int count = count_segments(argv[1], argv[2][0]); + printf("segments: %d\n", count); + char **buf = ft_split(argv[1], argv[2][0]); + size_t i = 0; + while (buf[i] != NULL) + { + printf("%zu\t'%s'\n", i, buf[i]); + i++; + } + free_buf(buf, i); + return (0); + } + else + { + printf("Usage: %s \n", argv[0]); + return (1); + } +} +*/ diff --git a/libft.h b/libft.h index 30a9f25..cdab05f 100644 --- a/libft.h +++ b/libft.h @@ -50,5 +50,6 @@ char *ft_strdup(const char *s); char *ft_substr(char const *s, unsigned int start, size_t len); char *ft_strjoin(char const *s1, char const *s2); char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *s, char c); #endif