From: Lukas Jiriste Date: Sun, 21 Jul 2024 16:50:28 +0000 (+0200) Subject: Transplant a better split function to Libft X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=6f5f51ae6d82e1104613df4837a0ad03684e70f4;p=Libft.git Transplant a better split function to Libft This function was created during the 42 Piscine. --- diff --git a/ft_str/ft_split.c b/ft_str/ft_split.c index 710248d..1b23477 100644 --- a/ft_str/ft_split.c +++ b/ft_str/ft_split.c @@ -3,90 +3,90 @@ /* ::: :::::::: */ /* ft_split.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ljiriste +#+ +:+ +#+ */ +/* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/08/15 15:30:26 by ljiriste #+# #+# */ -/* Updated: 2023/12/09 15:24:36 by ljiriste ### ########.fr */ +/* Created: 2023/06/20 11:51:05 by ljiriste #+# #+# */ +/* Updated: 2023/06/28 21:04:24 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include -#include "ft_str.h" -static void free_str_array(char **arr) +int is_sep(char c, char *seps) { - size_t i; - - i = 0; - while (arr[i] != NULL) - free(arr[i++]); - free(arr); - return ; + while (*seps) + { + if (c == *seps) + return (1); + ++seps; + } + return (0); } -// Returns 0 when terminating null is encountered, otherwise returns true. -static int to_next_word(const char **s, char c) +unsigned int substr_len(char *str, char *seps) { - while (**s && **s == c) - ++(*s); - return (**s); + unsigned int len; + + len = 0; + while (!is_sep(str[len], seps)) + ++len; + return (len); } -static int word_count(const char *s, char c) +int count_valid_strs(char *str, char *seps) { - int count; + int i; + int str_count; - if (s == NULL) - return (-1); - if (c == '\0') - return (1); - count = 0; - while (*s) + str_count = 0; + i = 0; + while (str[i]) { - while (*s == c) - ++s; - if (*s) - ++count; - while (*s != c && *s) - ++s; + while (is_sep(str[i], seps) && str[i]) + ++i; + ++str_count; + while (!is_sep(str[i], seps) && str[i]) + ++i; } - return (count); + return (str_count); } -static size_t strlen_to_char(const char *s, char c) +char *extract_substr(char *str, char *seps) { - size_t len; + char *res; + int i; - len = 0; - while (s[len] != c && s[len]) - ++len; - return (len); + res = malloc((substr_len(str, seps) + 1) * sizeof(char)); + i = 0; + while (!is_sep(str[i], seps)) + { + res[i] = str[i]; + ++i; + } + res[i] = '\0'; + return (res); } -char **ft_split(const char *s, char c) +char **ft_split(char *str, char *seps) { - char **res; - size_t size; - size_t i; + char **res; + int i; + int str_num; + int str_count; - res = malloc((word_count(s, c) + 1) * sizeof(char *)); - if (res == NULL || s == NULL) - return (res); + str_count = count_valid_strs(str, seps); + res = malloc((str_count + 1) * sizeof(char *)); i = 0; - while (*s) + str_num = 0; + while (str[i]) { - if (!to_next_word(&s, c)) - break ; - size = (strlen_to_char(s, c) + 1); - res[i] = malloc(size * sizeof(char)); - if (res[i] == NULL) + if (!is_sep(str[i], seps)) { - free_str_array(res); - return (NULL); + res[str_num++] = extract_substr(&str[i], seps); + i += substr_len(&str[i], seps); } - ft_strlcpy(res[i++], s, size); - s += size - 1; + ++i; } - res[i] = NULL; + res[str_num] = 0; return (res); }