Changed the functions so that they pass the alelievr/libft-unit-test.
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 5 Sep 2023 17:14:17 +0000 (19:14 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 5 Sep 2023 17:14:17 +0000 (19:14 +0200)
ft_strtrim needs to also handle empty strings and strings containing only chars from set.

14 files changed:
ft_calloc.c
ft_lstclear.c
ft_lstdelone.c
ft_memcpy.c
ft_putstr_fd.c
ft_split.c
ft_striteri.c
ft_strjoin.c
ft_strlcat.c
ft_strmapi.c
ft_strncmp.c
ft_strnstr.c
ft_strtrim.c
ft_substr.c

index e0bc9dad1394dd730dd65ffdc1bbb0f0412d009a..a4c55b0207dd5f35a1f607cfc9b3d86be0404077 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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)
        {
index afd921193e3b3f2eea5c92437f067b509a80d430..14e60d5e9e985a8819cdb4a6888d8bab114eb8ed 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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;
index 622b3eba45e267c10d402cf550e9cbb35d055909..b76d40bd9de6a65be0798fee1bb98d293166308e 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 ;
index e4d2b2c836290bf220821a22403858b59aa0ba11..0ab62499bf8649749fbb84263d3737ebff572a3c 100644 (file)
@@ -6,15 +6,18 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <stddef.h>
 #include <sys/types.h>
 #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;
index 5f3fb14127385ed9f096397c3dd92491c16a20c2..6a632f37799fb2ba1dcc48184a32a4ffdea68763 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 ;
 }
index b566a2f7c69b193253dd69e5d53e4616be0cc1e8..082ebce9f44386c35ae0cf675d9f7816cd23a942 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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)
index 606ce6baf5aa79d6c45b235362a50d2dbd0214e4..bf2c89a762d24051b5c90b2c53c7f41d165af954 100644 (file)
@@ -6,16 +6,19 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <stddef.h>
 #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])
        {
index 644ddbcec39e35086c881eca872ab0d5836c5caf..45b8f78afcc4c9ba8548e62ded8594bac641dc22 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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)
index 86949ea890da0c7356757b03fdb15a27b7925818..08395ca93b63dc27377cc542364734faa0f38bfc 100644 (file)
@@ -6,10 +6,11 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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 <stddef.h>
 #include <sys/types.h>
 #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)
        {
index 14550a047f18cdc39d17cec0164a20a7ab2984af..614ddb75599c1491a79cd5c360fccd5e94060779 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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);
index fb68698bfedaa166ba7696b40ace9e3f5db14759..165753c1402995b570f1856ff377b89438b932a7 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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);
 }
index 617ab025e53dd25bc15b2bf171f7f4a0bc2eb47d..b747c19c1ae33e6f6cc3ce5d600b087b37a29fbe 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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)
index 7bbf77654a96ebecf77102bd48cce78adb009c4e..853a6a4d583e97299a1fd4e68b93efc43e080b36 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
 
 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);
 }
index c55b4f39989ea025f23a00cf542fb7e7568eed99..a80cb6e3dd54caf05670a04a1741920e38e31919 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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)
        {