From: Lukas Jiriste Date: Wed, 16 Aug 2023 09:28:16 +0000 (+0200) Subject: Repaired functions to pass the Tripouille libftTester (mostly - I don't think some... X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=1827eed14dae113a5b1ebf05e7f1abc0a96a9f5b;p=Libft.git Repaired functions to pass the Tripouille libftTester (mostly - I don't think some KOs are reasonable). --- diff --git a/ft_calloc.c b/ft_calloc.c index d7fe1f3..e0bc9da 100644 --- a/ft_calloc.c +++ b/ft_calloc.c @@ -10,6 +10,7 @@ /* */ /* ************************************************************************** */ +#include #include // 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); } diff --git a/ft_itoa.c b/ft_itoa.c index b47047c..88539db 100644 --- 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) diff --git a/ft_memcmp.c b/ft_memcmp.c index e3c320c..09dd5a5 100644 --- a/ft_memcmp.c +++ b/ft_memcmp.c @@ -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); } diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c index fb9d62b..953510d 100644 --- a/ft_putnbr_fd.c +++ b/ft_putnbr_fd.c @@ -15,6 +15,22 @@ 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 ; } diff --git a/ft_split.c b/ft_split.c index 673b9d5..b566a2f 100644 --- a/ft_split.c +++ b/ft_split.c @@ -13,21 +13,6 @@ #include #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); diff --git a/ft_strchr.c b/ft_strchr.c index 65c51a2..67dc972 100644 --- a/ft_strchr.c +++ b/ft_strchr.c @@ -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; } diff --git a/ft_strlcat.c b/ft_strlcat.c index 1a435b6..86949ea 100644 --- a/ft_strlcat.c +++ b/ft_strlcat.c @@ -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); } diff --git a/ft_strlcpy.c b/ft_strlcpy.c index 4b42275..423e2fd 100644 --- a/ft_strlcpy.c +++ b/ft_strlcpy.c @@ -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); } diff --git a/ft_strnstr.c b/ft_strnstr.c index ccb257c..617ab02 100644 --- a/ft_strnstr.c +++ b/ft_strnstr.c @@ -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); diff --git a/ft_strrchr.c b/ft_strrchr.c index 54b5195..f85b16c 100644 --- a/ft_strrchr.c +++ b/ft_strrchr.c @@ -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; } diff --git a/ft_strtrim.c b/ft_strtrim.c index 44cddeb..ac21682 100644 --- a/ft_strtrim.c +++ b/ft_strtrim.c @@ -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); } diff --git a/ft_substr.c b/ft_substr.c index 99b30a4..c55b4f3 100644 --- a/ft_substr.c +++ b/ft_substr.c @@ -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); }