Repaired functions to pass the Tripouille libftTester (mostly - I don't think some...
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 16 Aug 2023 09:28:16 +0000 (11:28 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 16 Aug 2023 09:28:16 +0000 (11:28 +0200)
12 files changed:
ft_calloc.c
ft_itoa.c
ft_memcmp.c
ft_putnbr_fd.c
ft_split.c
ft_strchr.c
ft_strlcat.c
ft_strlcpy.c
ft_strnstr.c
ft_strrchr.c
ft_strtrim.c
ft_substr.c

index d7fe1f3539a378509460b791db74fb59a7b00d0c..e0bc9dad1394dd730dd65ffdc1bbb0f0412d009a 100644 (file)
@@ -10,6 +10,7 @@
 /*                                                                            */
 /* ************************************************************************** */
 
+#include <errno.h>
 #include <stdlib.h>    // for malloc
 #include "libft.h"
 
@@ -22,8 +23,13 @@ void *ft_calloc(size_t nmemb, size_t size)
                return (NULL);
        total_bytes = nmemb * size;
        if (total_bytes / size != nmemb)
+       {
+               errno = ENOMEM;
                return (NULL);
+       }
        res = malloc(total_bytes);
+       if (res == NULL)
+               return (NULL);
        ft_memset(res, 0, total_bytes);
        return (res);
 }
index b47047cf48f7c956843c15d09c85dd48ccdc70b7..88539db42570553f648286ecbe4c4dec3ab2e153 100644 (file)
--- a/ft_itoa.c
+++ b/ft_itoa.c
@@ -19,7 +19,7 @@ static unsigned int   size_needed(int n)
 
        res = 1;
        if (n == 0)
-               return (1);
+               return (2);
        if (n < 0)
                ++res;
        while (n != 0)
index e3c320c7b636136a6c429bea6e46f55987f4fc7b..09dd5a5b90122e846a517dab51d2f0a63ab24ed0 100644 (file)
@@ -22,7 +22,7 @@ int   ft_memcmp(const void *s1, const void *s2, size_t n)
                && i + 1 < n)
                ++i;
        if (n > 0)
-               return (*((unsigned char *)s1 + i) == *((unsigned char *)s2 + i));
+               return (*((unsigned char *)s1 + i) - *((unsigned char *)s2 + i));
        else
                return (0);
 }
index fb9d62b566472c954c01f02a15699d5301d64433..953510d3fe841c71808fba9b7d8ca3f3e432f4bb 100644 (file)
 
 void   ft_putnbr_fd(int n, int fd)
 {
-       ft_putstr_fd(ft_itoa(n), fd);
+       char    digit;
+
+       if (n < 0)
+       {
+               write(fd, "-", 1);
+               digit = '0' - (n % 10);
+               if (n / 10 != 0)
+                       ft_putnbr_fd(-(n / 10), fd);
+       }
+       else if (n < 10)
+               digit = '0' + n;
+       else
+       {
+               digit = '0' + n % 10;
+               ft_putnbr_fd(n / 10, fd);
+       }
+       write(fd, &digit, 1);
        return ;
 }
index 673b9d5c8430e09a62e69d85a1efdfd31ea58018..b566a2f7c69b193253dd69e5d53e4616be0cc1e8 100644 (file)
 #include <stdlib.h>
 #include "libft.h"
 
-/*     If str_p points (double deref.) to character that is equal to c,
-       this function advances it to first until it is not equal to c.
-       Otherwise it advances the pointer to first character, that equals c.
-       Function returns when encountering terminating null.                                    */
-static void    advance(const char **str_p, char c)
-{
-       if (**str_p == c)
-               while (**str_p == c && **str_p)
-                       ++(*str_p);
-       else
-               while (**str_p != c && **str_p)
-                       ++(*str_p);
-       return ;
-}
-
 static void    free_str_array(char **arr)
 {
        size_t  i;
@@ -39,10 +24,20 @@ static void free_str_array(char **arr)
        return ;
 }
 
+// Returns 0 when terminating null is encountered, otherwise returns true.
+static int     to_next_word(const char **s, char c)
+{
+       while (**s && **s == c)
+               ++(*s);
+       return (**s);
+}
+
 static size_t  word_count(const char *s, char c)
 {
        size_t  count;
 
+       if (c == '\0')
+               return (1);
        count = 0;
        while (*s)
        {
@@ -78,8 +73,7 @@ char  **ft_split(const char *s, char c)
        i = 0;
        while (*s)
        {
-               advance(&s, c);
-               if (*s == '\0')
+               if (!to_next_word(&s, c))
                        break ;
                size = (strlen_to_char(s, c) + 1);
                res[i] = malloc(size * sizeof(char));
@@ -89,7 +83,7 @@ char  **ft_split(const char *s, char c)
                        return (NULL);
                }
                ft_strlcpy(res[i++], s, size);
-               advance(&s, c);
+               s += size - 1;
        }
        res[i] = NULL;
        return (res);
index 65c51a298c4989940b3b6e88685a4152e3c858d6..67dc9724b87f988a50b1ce394e2e4510581d4714 100644 (file)
@@ -15,9 +15,9 @@
 
 char   *ft_strchr(const char *s, int c)
 {
-       while (*s != '\0')
+       while (*s)
        {
-               if (*s == c)
+               if (*s == (char)c)
                        return ((char *)s);
                ++s;
        }
index 1a435b6321f824abca355354178369d3be5f1718..86949ea890da0c7356757b03fdb15a27b7925818 100644 (file)
@@ -18,25 +18,24 @@ size_t      ft_strlcat(char *dst, const char *src, size_t size)
        size_t  length;
 
        length = 0;
-       while (*dst != '\0' && size > 1)
+       while (*dst && size > length)
        {
                ++dst;
                ++length;
-               --size;
        }
-       while (*src != '\0' && size > 1)
+       while (*src && size > length + 1)
        {
                *dst = *src;
                ++src;
                ++dst;
                ++length;
-               --size;
        }
-       while (size > 0)
-       {
+       if (size > length)
                *dst = '\0';
-               ++dst;
-               --size;
+       while (*src)
+       {
+               ++length;
+               ++src;
        }
        return (length);
 }
index 4b42275fa98b04ad3314350529618ab132f4cdb7..423e2fdcb94fb94eca98dccdb1d57a2bcc81660c 100644 (file)
@@ -18,12 +18,14 @@ size_t      ft_strlcpy(char *dst, const char *src, size_t size)
        size_t  i;
 
        i = 0;
-       while (*src != '\0' && i + 1 < size)
+       while (src[i] && i + 1 < size)
        {
                dst[i] = src[i];
                ++i;
        }
        if (size > 0)
                dst[i] = '\0';
-       return (ft_strlen(src));
+       while (src[i])
+               ++i;
+       return (i);
 }
index ccb257cc6678b40cc3beedc2f65735d08c364836..617ab025e53dd25bc15b2bf171f7f4a0bc2eb47d 100644 (file)
@@ -19,9 +19,11 @@ char *ft_strnstr(const char *big, const char *little, size_t len)
        size_t  little_len;
        size_t  i;
 
+       if (*little == '\0')
+               return ((char *)big);
        little_len = ft_strlen(little);
        i = 0;
-       while (big[i] != '\0' && i + little_len <= len)
+       while (big[i] && i + little_len <= len)
        {
                if (!(ft_strncmp(big + i, little, little_len)))
                        return ((char *)big + i);
index 54b51954f76bb7dc0e776618ad387a5da1ebd172..f85b16c3879ffaf3ddbf60213c442a9094660b66 100644 (file)
@@ -20,7 +20,7 @@ char  *ft_strrchr(const char *s, int c)
        result = NULL;
        while (*s != '\0')
        {
-               if (*s == c)
+               if (*s == (char)c)
                        result = (char *)s;
                ++s;
        }
index 44cddebd6d74f248615e7a15b10a73536e6d6e20..ac21682b99b18e0a287df020e22a3063178efb30 100644 (file)
@@ -25,11 +25,12 @@ char        *ft_strtrim(const char *s1, const char *set)
                return (res);
        i = 0;
        j = 0;
-       while (s1[i] != '\0')
+       while (s1[i])
        {
                if (ft_strchr(set, s1[i]) == NULL)
                        res[j++] = s1[i];
                ++i;
        }
+       res[j] = '\0';
        return (res);
 }
index 99b30a4a2359d95fb4536717e659c17a93f835ea..c55b4f39989ea025f23a00cf542fb7e7568eed99 100644 (file)
@@ -18,12 +18,22 @@ char        *ft_substr(const char *s, unsigned int start, size_t len)
        char    *res;
        size_t  size;
 
-       size = ft_strlen(s + start) + 1;
+       size = ft_strlen(s) + 1;
+       if (start > size)
+       {
+               res = malloc(1 * sizeof(char));
+               if (res == NULL)
+                       return (NULL);
+               *res = '\0';
+               return (res);
+       }
+       else
+               size -= start;
        if (size > len + 1)
                size = len + 1;
        res = malloc(size * sizeof(char));
        if (res == NULL)
-               return (res);
+               return (NULL);
        ft_strlcpy(res, s + start, size);
        return (res);
 }