From 6fe30c97aad7a4a3564e7037d64a43edcb9a2162 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 9 Jun 2020 17:57:10 +0200 Subject: Added ft_dstrwrap, ft_splitf, fixing bug in ft_split --- include/libft_dstr.h | 3 ++- include/libft_str.h | 3 ++- src/dstr/ft_dstrnew.c | 14 +++++++------- src/dstr/ft_dstrwrap.c | 25 +++++++++++++++++++++++++ src/str/ft_split.c | 14 ++------------ src/str/ft_splitf.c | 22 ++++++++++++++++++++++ 6 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 src/dstr/ft_dstrwrap.c create mode 100644 src/str/ft_splitf.c diff --git a/include/libft_dstr.h b/include/libft_dstr.h index 732e475..d482f73 100644 --- a/include/libft_dstr.h +++ b/include/libft_dstr.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 10:39:51 by charles #+# #+# */ -/* Updated: 2020/04/05 00:37:05 by charles ### ########.fr */ +/* Updated: 2020/06/09 17:35:47 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,7 @@ typedef struct s_ftdstr t_ftdstr *ft_dstrnew(char *from); void ft_dstrdestroy(t_ftdstr *dstr); t_ftdstr *ft_dstrgrow(t_ftdstr *dstr, size_t at_least); +t_ftdstr *ft_dstrwrap(char *str); char *ft_dstrunwrap(t_ftdstr *dstr); t_ftdstr *ft_dstrinsert(t_ftdstr *dstr, char *inserted, size_t i); void ft_dstrerase(t_ftdstr *dstr, size_t start, size_t len); diff --git a/include/libft_str.h b/include/libft_str.h index 195e750..bfe4ad4 100644 --- a/include/libft_str.h +++ b/include/libft_str.h @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:22 by cacharle #+# #+# */ -/* Updated: 2020/04/05 13:52:10 by charles ### ########.fr */ +/* Updated: 2020/06/09 17:47:21 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,6 +59,7 @@ char *ft_strjoin3(char const *s1, char const *s2, char const *s3); char *ft_strjoinf(char const *s1, char const *s2, t_ftstrjoinf_tag tag); char *ft_strtrim(char const *s1, char const *set); char **ft_split(char const *s, char c); +char **ft_splitf(char *s, char c); int ft_strcount(char *str, char c); char *ft_itoa(int n); int ft_atoi_strict(const char *s); diff --git a/src/dstr/ft_dstrnew.c b/src/dstr/ft_dstrnew.c index 8ae4a64..fe9ce46 100644 --- a/src/dstr/ft_dstrnew.c +++ b/src/dstr/ft_dstrnew.c @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/03 13:54:52 by charles #+# #+# */ -/* Updated: 2020/04/04 19:50:38 by charles ### ########.fr */ +/* Updated: 2020/06/09 17:35:14 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,12 +21,12 @@ t_ftdstr *ft_dstrnew(char *from) { - t_ftdstr *dstr; + char *clone; + t_ftdstr *ret; - if ((dstr = (t_ftdstr*)malloc(sizeof(t_ftdstr))) == NULL || - (dstr->str = ft_strdup(from)) == NULL) + if ((clone = ft_strdup(from)) == NULL) return (NULL); - dstr->length = ft_strlen(from); - dstr->capacity = dstr->length + 1; - return (dstr); + if ((ret = ft_dstrwrap(clone)) == NULL) + free(clone); + return (ret); } diff --git a/src/dstr/ft_dstrwrap.c b/src/dstr/ft_dstrwrap.c new file mode 100644 index 0000000..0acf684 --- /dev/null +++ b/src/dstr/ft_dstrwrap.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_dstrwrap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/09 17:32:30 by charles #+# #+# */ +/* Updated: 2020/06/09 17:33:57 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_dstr.h" + +t_ftdstr *ft_dstrwrap(char *str) +{ + t_ftdstr *dstr; + + if ((dstr = (t_ftdstr*)malloc(sizeof(t_ftdstr))) == NULL) + return (NULL); + dstr->str = str; + dstr->length = ft_strlen(str); + dstr->capacity = dstr->length + 1; + return (dstr); +} diff --git a/src/str/ft_split.c b/src/str/ft_split.c index 6fb5964..b61b09f 100644 --- a/src/str/ft_split.c +++ b/src/str/ft_split.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/17 08:29:02 by cacharle #+# #+# */ -/* Updated: 2019/11/20 04:08:27 by cacharle ### ########.fr */ +/* Updated: 2020/06/09 17:14:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,16 +33,6 @@ static size_t count_segment(char const *s, char c) return (counter); } -static void *destroy_strs(char **strs) -{ - if (strs == NULL) - return (NULL); - while (*strs != NULL) - free(*strs++); - free(strs); - return (NULL); -} - char **ft_split(char const *s, char c) { char **strs; @@ -65,7 +55,7 @@ char **ft_split(char const *s, char c) while (s[j + i] && s[j + i] != c) i++; if ((strs[tab_counter++] = ft_strndup(&s[j], i)) == NULL) - return (destroy_strs(strs)); + return (ft_split_destroy(strs)); j += i - 1; } strs[tab_counter] = NULL; diff --git a/src/str/ft_splitf.c b/src/str/ft_splitf.c new file mode 100644 index 0000000..2a1afab --- /dev/null +++ b/src/str/ft_splitf.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_splitf.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/09 17:45:52 by charles #+# #+# */ +/* Updated: 2020/06/09 17:47:17 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_str.h" + +char **ft_splitf(char *s, char delim) +{ + char **ret; + + ret = ft_split(s, delim); + free(s); + return (ret); +} -- cgit