From: Lukas Jiriste Date: Sun, 21 Jul 2024 16:54:44 +0000 (+0200) Subject: Adapt the transplanted ft_split to libft X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=10facec461d542d637412bdc3e55c299e7bfe49a;p=Libft.git Adapt the transplanted ft_split to libft --- diff --git a/ft_str/ft_split.c b/ft_str/ft_split.c index 1b23477..a9bd121 100644 --- a/ft_str/ft_split.c +++ b/ft_str/ft_split.c @@ -6,59 +6,49 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/20 11:51:05 by ljiriste #+# #+# */ -/* Updated: 2023/06/28 21:04:24 by ljiriste ### ########.fr */ +/* Updated: 2024/07/21 18:53:26 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ +#include "ft_str.h" #include -int is_sep(char c, char *seps) +size_t substr_len(const char *str, const char *seps) { - while (*seps) - { - if (c == *seps) - return (1); - ++seps; - } - return (0); -} - -unsigned int substr_len(char *str, char *seps) -{ - unsigned int len; + size_t len; len = 0; - while (!is_sep(str[len], seps)) + while (!ft_strchr(seps, str[len])) ++len; return (len); } -int count_valid_strs(char *str, char *seps) +size_t count_valid_strs(const char *str, const char *seps) { - int i; - int str_count; + size_t i; + size_t str_count; str_count = 0; i = 0; while (str[i]) { - while (is_sep(str[i], seps) && str[i]) + while (ft_strchr(seps, str[i]) && str[i]) ++i; ++str_count; - while (!is_sep(str[i], seps) && str[i]) + while (!ft_strchr(seps, str[i]) && str[i]) ++i; } return (str_count); } -char *extract_substr(char *str, char *seps) +char *extract_substr(const char *str, const char *seps) { char *res; - int i; + size_t i; res = malloc((substr_len(str, seps) + 1) * sizeof(char)); i = 0; - while (!is_sep(str[i], seps)) + while (!ft_strchr(seps, str[i])) { res[i] = str[i]; ++i; @@ -67,12 +57,12 @@ char *extract_substr(char *str, char *seps) return (res); } -char **ft_split(char *str, char *seps) +char **ft_split(const char *str, const char *seps) { char **res; - int i; - int str_num; - int str_count; + size_t i; + size_t str_num; + size_t str_count; str_count = count_valid_strs(str, seps); res = malloc((str_count + 1) * sizeof(char *)); @@ -80,13 +70,13 @@ char **ft_split(char *str, char *seps) str_num = 0; while (str[i]) { - if (!is_sep(str[i], seps)) + if (!ft_strchr(seps, str[i])) { res[str_num++] = extract_substr(&str[i], seps); i += substr_len(&str[i], seps); } ++i; } - res[str_num] = 0; + res[str_num] = NULL; return (res); } diff --git a/inc/ft_str.h b/inc/ft_str.h index 6adc40d..b632bf1 100644 --- a/inc/ft_str.h +++ b/inc/ft_str.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/09 11:50:15 by ljiriste #+# #+# */ -/* Updated: 2024/06/15 08:34:22 by ljiriste ### ########.fr */ +/* Updated: 2024/07/21 18:08:41 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ char *ft_substr(const char *s, unsigned int start, size_t len); char *ft_strjoin(const char *s1, const char *s2); char *ft_strtrim(const char *s1, const char *set); char *ft_remove_space(const char *str); -char **ft_split(const char *s, char c); +char **ft_split(const char *str, const char *seps); char *ft_strmapi(const char *s, char (*f)(unsigned int, char)); void ft_striteri(char *s, void (*f)(unsigned int, char *));