From 67ea55d5c715d23e371eb8ff54bce89ea18bdd85 Mon Sep 17 00:00:00 2001 From: Adam Joly Date: Sat, 11 Nov 2023 11:56:43 +0100 Subject: [PATCH] strmapi done && split fix --- Makefile | 3 ++- ft_calloc.c | 6 ++++-- ft_split.c | 16 +++------------- libft.h | 3 ++- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index a51d80c..636893d 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: adjoly +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/01 11:03:22 by adjoly #+# #+# # -# Updated: 2023/11/11 11:12:02 by adjoly ### ########.fr # +# Updated: 2023/11/11 11:49:54 by adjoly ### ########.fr # # # # **************************************************************************** # @@ -46,6 +46,7 @@ SRCS = ft_atoi.c \ ft_putendl_fd.c \ ft_strtrim.c \ ft_split.c \ + ft_strmapi.c \ OBJS = $(SRCS:.c=.o) diff --git a/ft_calloc.c b/ft_calloc.c index 6d2e83c..77e79ff 100644 --- a/ft_calloc.c +++ b/ft_calloc.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/01 16:02:26 by adjoly #+# #+# */ -/* Updated: 2023/11/11 00:55:15 by adjoly ### ########.fr */ +/* Updated: 2023/11/11 01:32:30 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,9 @@ void *ft_calloc(size_t nmemb, size_t size) void *result; if (size != 0 && nmemb != 0 && (nmemb * size) / nmemb != size) - return (malloc(0)); + return (NULL); + if (nmemb == 0 || size == 0) + return (malloc(1)); result = malloc(size * nmemb); if (result == NULL) return (NULL); diff --git a/ft_split.c b/ft_split.c index ccd0e04..e0727d3 100644 --- a/ft_split.c +++ b/ft_split.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 19:04:42 by adjoly #+# #+# */ -/* Updated: 2023/11/11 11:15:37 by adjoly ### ########.fr */ +/* Updated: 2023/11/11 11:27:49by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,7 +59,6 @@ char **ft_split(char const *s, char c) char **result; int i; int j; - int k; i = 0; j = 0; @@ -75,17 +74,8 @@ char **ft_split(char const *s, char c) j++; if (s[j] != '\0' && s[j] != c) { - k = 0; - result[i] = malloc((ft_countletter(&s[j], c) + 1) * sizeof(char)); - if (result == NULL) - return (ft_freearr(result)); - while (s[j] != c) - { - result[i][k] = s[j]; - j++; - k++; - } - result[i][k] = '\0'; + result[i] = ft_substr(s, j, ft_countletter(s + j, c)); + j += ft_countletter(s + j, c); } i++; } diff --git a/libft.h b/libft.h index 540b4d6..dbd0ef7 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/01 10:06:03 by adjoly #+# #+# */ -/* Updated: 2023/11/11 10:40:20 by adjoly ### ########.fr */ +/* Updated: 2023/11/11 11:49:37 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,5 +51,6 @@ char *ft_strnstr(const char *big, const char *little, size_t len); void ft_putendl_fd(char *s, int fd); char *ft_strtrim(char const *s1, char const *set); char **ft_split(char const *s, char c); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); #endif \ No newline at end of file