diff --git a/Makefile b/Makefile index ed0d854..edad84c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: cchauvet next; ft_lstdelone(*lst, del); diff --git a/ft_lstdelone.c b/ft_lstdelone.c index 10e5005..73ac454 100644 --- a/ft_lstdelone.c +++ b/ft_lstdelone.c @@ -6,7 +6,7 @@ /* By: cchauvet next == NULL) + if (f == NULL) + return ; + while (lst != NULL) { - f(lst); + f(lst->content); lst = lst->next; } } diff --git a/ft_lstmap.c b/ft_lstmap.c index 2ae9995..ed99c7c 100644 --- a/ft_lstmap.c +++ b/ft_lstmap.c @@ -6,8 +6,34 @@ /* By: cchauvet content)); + if (root == NULL) + return (NULL); + last = root; + lst = lst->next; + while (lst != NULL) + { + last->next = ft_lstnew(f(lst->content)); + if (last->next == NULL) + { + ft_lstclear(&root, del); + return (NULL); + } + lst = lst->next; + last = last->next; + } + return (root); +} diff --git a/ft_split.c b/ft_split.c index 648b553..455b0d1 100644 --- a/ft_split.c +++ b/ft_split.c @@ -5,94 +5,88 @@ /* +:+ +:+ +:+ */ /* By: cchauvet + +static void *free_array(char **a) +{ + size_t i; + + if (a != NULL) + { + i = 0; + while (a[i] != NULL) + free(a[i++]); + free(a); + } + return (NULL); +} + +static size_t skip_char(const char **s, char c, bool invert) +{ + size_t i; + const char *p; + + i = 0; + p = *s; + while (((!invert && *p == c) || (invert && *p != c)) && *p != '\0') + { + p++; + i++; + } + *s = p; + return (i); +} + +static size_t count_elems(const char *s, char c) +{ + size_t n_elem; + + n_elem = 0; + skip_char(&s, c, false); + while (*s != '\0') + { + skip_char(&s, c, true); + skip_char(&s, c, false); + n_elem++; + } + return (n_elem); +} + +char **ft_split(char const *s, char c) +{ + char **array; + size_t n_elem; + size_t arr_i; + const char *rem_s; + size_t len; + + if (s == NULL) + return (NULL); + n_elem = count_elems(s, c); + array = ft_calloc(n_elem + 1, sizeof(char *)); + if (array == NULL) + return (array); + skip_char(&s, c, false); + arr_i = 0; + while (arr_i < n_elem) + { + rem_s = s; + len = skip_char(&s, c, true); + array[arr_i] = ft_substr(rem_s, 0, len); + if (array[arr_i++] == NULL) + return (free_array(array)); + skip_char(&s, c, false); + } + return (array); +} diff --git a/ft_strchr.c b/ft_strchr.c index 652082d..8a2bb03 100644 --- a/ft_strchr.c +++ b/ft_strchr.c @@ -6,7 +6,7 @@ /* By: cchauvet 0) - if (s[i] == c) + if (s[i] == (char) c) return ((char *) s + i); return (NULL); } diff --git a/ft_substr.c b/ft_substr.c index 3f76d93..3200ce9 100644 --- a/ft_substr.c +++ b/ft_substr.c @@ -6,29 +6,30 @@ /* By: cchauvet len) - max = len; - if (start >= ft_strlen(s)) - return (ft_strdup("")); - sub = malloc((max + 1) * sizeof(char)); - if (sub == NULL) + size = ft_strlen(s); + size -= start; + if (size < 0) + size = 0; + if ((size_t)size > len) + size = len; + ptr = malloc((size + 1) * sizeof(char)); + if (ptr == NULL) return (NULL); - sub[max] = 0; - while (max-- > 0) - sub[max] = s[max + start]; - return (sub); + ptr[size] = '\0'; + while (size-- > 0) + ptr[size] = s[start + size]; + return (ptr); } diff --git a/libft.h.gch b/libft.h.gch deleted file mode 100644 index aa571dc..0000000 Binary files a/libft.h.gch and /dev/null differ diff --git a/main.c b/main.c deleted file mode 100644 index 1f42788..0000000 --- a/main.c +++ /dev/null @@ -1,35 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* main.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cchauvet -#include "libft.h" - -int main(int argc, char **argv) -{ - char **splitted; - char *a; - char c; - - if (argc < 2) - return (1); - a = argv[1]; - c = argv[2][0]; - splitted = ft_split(a, c); -//splitted = ft_split("lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse", ' '); - while (*splitted != NULL) - { - puts(*splitted); - free(*splitted); - splitted++; - } - return (0); -}