From: Lukas Jiriste Date: Tue, 5 Sep 2023 17:14:17 +0000 (+0200) Subject: Changed the functions so that they pass the alelievr/libft-unit-test. X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=0beab7c9eed152a91c60612ec90d2a40390f12b1;p=Libft.git Changed the functions so that they pass the alelievr/libft-unit-test. ft_strtrim needs to also handle empty strings and strings containing only chars from set. --- diff --git a/ft_calloc.c b/ft_calloc.c index e0bc9da..a4c55b0 100644 --- a/ft_calloc.c +++ b/ft_calloc.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 11:34:52 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 17:12:08 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:26:57 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ void *ft_calloc(size_t nmemb, size_t size) void *res; if (nmemb == 0 || size == 0) - return (NULL); + return (malloc(0)); total_bytes = nmemb * size; if (total_bytes / size != nmemb) { diff --git a/ft_lstclear.c b/ft_lstclear.c index afd9211..14e60d5 100644 --- a/ft_lstclear.c +++ b/ft_lstclear.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/16 13:45:28 by ljiriste #+# #+# */ -/* Updated: 2023/08/16 16:48:58 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:33:24 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ void ft_lstclear(t_list **lst, void (*del)(void *)) { t_list *temp; + if (lst == NULL || del == NULL) + return ; while (*lst) { temp = (*lst)->next; diff --git a/ft_lstdelone.c b/ft_lstdelone.c index 622b3eb..b76d40b 100644 --- a/ft_lstdelone.c +++ b/ft_lstdelone.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/16 13:43:02 by ljiriste #+# #+# */ -/* Updated: 2023/08/16 13:46:37 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:33:02 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,8 @@ void ft_lstdelone(t_list *lst, void (*del)(void *)) { + if (lst == NULL || del == NULL) + return ; del(lst->content); free(lst); return ; diff --git a/ft_memcpy.c b/ft_memcpy.c index e4d2b2c..0ab6249 100644 --- a/ft_memcpy.c +++ b/ft_memcpy.c @@ -6,15 +6,18 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/14 13:35:59 by ljiriste #+# #+# */ -/* Updated: 2023/08/14 13:36:20 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:16:35 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ +#include #include #include "libft.h" void *ft_memcpy(void *dest, const void *src, size_t n) { + if (dest == NULL && src == NULL) + return (NULL); while (n > 0) { --n; diff --git a/ft_putstr_fd.c b/ft_putstr_fd.c index 5f3fb14..6a632f3 100644 --- a/ft_putstr_fd.c +++ b/ft_putstr_fd.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 16:23:36 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 16:39:00 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:29:20 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,8 @@ void ft_putstr_fd(char *s, int fd) { + if (s == NULL) + return ; write(fd, s, ft_strlen(s)); return ; } diff --git a/ft_split.c b/ft_split.c index b566a2f..082ebce 100644 --- a/ft_split.c +++ b/ft_split.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 15:30:26 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 17:02:33 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 17:53:03 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,10 +32,12 @@ static int to_next_word(const char **s, char c) return (**s); } -static size_t word_count(const char *s, char c) +static int word_count(const char *s, char c) { - size_t count; + int count; + if (s == NULL) + return (-1); if (c == '\0') return (1); count = 0; @@ -68,7 +70,7 @@ char **ft_split(const char *s, char c) size_t i; res = malloc((word_count(s, c) + 1) * sizeof(char *)); - if (res == NULL) + if (res == NULL || s == NULL) return (res); i = 0; while (*s) diff --git a/ft_striteri.c b/ft_striteri.c index 606ce6b..bf2c89a 100644 --- a/ft_striteri.c +++ b/ft_striteri.c @@ -6,16 +6,19 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 16:18:09 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 16:47:35 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:33:39 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ +#include #include "libft.h" void ft_striteri(char *s, void (*f)(unsigned int, char *)) { unsigned int i; + if (s == NULL || f == NULL) + return ; i = 0; while (s[i]) { diff --git a/ft_strjoin.c b/ft_strjoin.c index 644ddbc..45b8f78 100644 --- a/ft_strjoin.c +++ b/ft_strjoin.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 15:04:10 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 16:43:17 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:33:53 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,8 @@ char *ft_strjoin(const char *s1, const char *s2) char *res; size_t size; + if (s1 == NULL || s2 == NULL) + return (NULL); size = ft_strlen(s1) + ft_strlen(s2) + 1; res = malloc(size * sizeof(char)); if (res == NULL) diff --git a/ft_strlcat.c b/ft_strlcat.c index 86949ea..08395ca 100644 --- a/ft_strlcat.c +++ b/ft_strlcat.c @@ -6,10 +6,11 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/12 17:32:33 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 12:55:06 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:40:14 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ +#include #include #include "libft.h" @@ -17,6 +18,8 @@ size_t ft_strlcat(char *dst, const char *src, size_t size) { size_t length; + if ((dst == NULL && src == NULL) || size == 0) + return (0); length = 0; while (*dst && size > length) { diff --git a/ft_strmapi.c b/ft_strmapi.c index 14550a0..614ddb7 100644 --- a/ft_strmapi.c +++ b/ft_strmapi.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 16:12:48 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 16:17:31 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:32:11 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,8 @@ char *ft_strmapi(const char *s, char (*f)(unsigned int, char)) char *res; unsigned int i; + if (s == NULL || f == NULL) + return (NULL); res = malloc((ft_strlen(s) + 1) * sizeof(char)); if (res == NULL) return (NULL); diff --git a/ft_strncmp.c b/ft_strncmp.c index fb68698..165753c 100644 --- a/ft_strncmp.c +++ b/ft_strncmp.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/12 17:02:07 by ljiriste #+# #+# */ -/* Updated: 2023/08/16 13:16:34 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:24:25 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ int ft_strncmp(const char *s1, const char *s2, size_t n) while (s1[i] == s2[i] && s1[i] && s2[i] && i + 1 < n) ++i; if (n > 0) - return (s1[i] - s2[i]); + return ((unsigned char)s1[i] - (unsigned char)s2[i]); else return (0); } diff --git a/ft_strnstr.c b/ft_strnstr.c index 617ab02..b747c19 100644 --- a/ft_strnstr.c +++ b/ft_strnstr.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/06/12 17:15:34 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 12:50:26 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:57:24 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,8 +19,12 @@ char *ft_strnstr(const char *big, const char *little, size_t len) size_t little_len; size_t i; + if (little == NULL && len == 0) + return ((char *)big); if (*little == '\0') return ((char *)big); + if (big == NULL && len == 0) + return (NULL); little_len = ft_strlen(little); i = 0; while (big[i] && i + little_len <= len) diff --git a/ft_strtrim.c b/ft_strtrim.c index 7bbf776..853a6a4 100644 --- a/ft_strtrim.c +++ b/ft_strtrim.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 15:11:52 by ljiriste #+# #+# */ -/* Updated: 2023/08/16 17:30:54 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 19:12:36 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,32 +16,42 @@ static size_t size_needed(const char *s1, const char *set) { - size_t res; + size_t at_start; + size_t at_end; + size_t whole; - res = 1; + whole = ft_strlen(s1); + at_start = 0; + at_end = 0; + while (ft_strchr(set, *s1)) + { + ++s1; + ++at_start; + } while (*s1) - if (ft_strchr(set, *(s1++)) == NULL) - ++res; - return (res); + ++s1; + --s1; + while (ft_strchr(set, *s1)) + { + --s1; + ++at_end; + } + return (whole - at_start - at_end + 1); } char *ft_strtrim(const char *s1, const char *set) { + size_t size; char *res; - size_t i; - size_t j; - res = malloc(size_needed(s1, set) * sizeof(char)); + if (s1 == NULL) + return (NULL); + size = size_needed(s1, set); + res = malloc(size * sizeof(char)); if (res == NULL) return (res); - i = 0; - j = 0; - while (s1[i]) - { - if (ft_strchr(set, s1[i]) == NULL) - res[j++] = s1[i]; - ++i; - } - res[j] = '\0'; + while (ft_strchr(set, *s1)) + ++s1; + ft_strlcpy(res, s1, size); return (res); } diff --git a/ft_substr.c b/ft_substr.c index c55b4f3..a80cb6e 100644 --- a/ft_substr.c +++ b/ft_substr.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 15:04:24 by ljiriste #+# #+# */ -/* Updated: 2023/08/15 16:41:57 by ljiriste ### ########.fr */ +/* Updated: 2023/09/05 18:27:38 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,8 @@ char *ft_substr(const char *s, unsigned int start, size_t len) char *res; size_t size; + if (s == NULL) + return (NULL); size = ft_strlen(s) + 1; if (start > size) {