From 75b4295d5a269c824493c0406841f5e1e189e36b Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 13 Feb 2017 15:30:49 +0100 Subject: [PATCH] strtok, need to rewrite strcspn next --- libft/Makefile | 2 +- libft/includes/libft.h | 1 + libft/src/str/ft_strreplace.c | 2 + libft/src/str/ft_strsplit.c | 70 ----------------------------------- libft/src/str/ft_strtok.c | 19 ++++++++++ 5 files changed, 23 insertions(+), 71 deletions(-) delete mode 100644 libft/src/str/ft_strsplit.c create mode 100644 libft/src/str/ft_strtok.c diff --git a/libft/Makefile b/libft/Makefile index 809f204c..ced47d26 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -174,9 +174,9 @@ str/ft_strnstr.c\ str/ft_strrchr.c\ str/ft_strreplace.c\ str/ft_strrev.c\ -str/ft_strsplit.c\ str/ft_strstr.c\ str/ft_strsub.c\ +str/ft_strtok.c\ str/ft_strtrim.c\ sys/dup2_close.c\ time/ft_mytime_free.c\ diff --git a/libft/includes/libft.h b/libft/includes/libft.h index bd8a14a7..d6020056 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -130,6 +130,7 @@ char *ft_strinsert(char *str, char c, int n); int ft_strappend(char **dst, char *src); char *ft_strbetween(char *start, char *end); char *ft_strreplace(char **str, char *start, char *end, char *new); +char *ft_strtok(char *s, const char *delim); char *ft_itoa_base(int nbr, char *base, char *flags); char *ft_lltoa_base(long long nbr, char *base, char *flags); diff --git a/libft/src/str/ft_strreplace.c b/libft/src/str/ft_strreplace.c index fad78191..7fb72527 100644 --- a/libft/src/str/ft_strreplace.c +++ b/libft/src/str/ft_strreplace.c @@ -20,5 +20,7 @@ char *ft_strreplace(char **str, char *start, char *end, char *new) ft_strncpy(out, *str, start - *str); ft_strcat(out, new); ft_strcat(out, end + 1); + ft_strdel(str); + *str = out; return (out); } diff --git a/libft/src/str/ft_strsplit.c b/libft/src/str/ft_strsplit.c deleted file mode 100644 index a44c4ca1..00000000 --- a/libft/src/str/ft_strsplit.c +++ /dev/null @@ -1,70 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_strsplit.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2016/11/03 14:58:40 by jhalford #+# #+# */ -/* Updated: 2016/12/05 17:20:45 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -static int ft_countwords(char const *s, char c) -{ - if (c == '\0') - return ((*s == '\0') ? 0 : 1); - while (*s == c) - s++; - if (*s == '\0') - return (0); - while (*s != c && *s != '\0') - s++; - return (1 + ft_countwords(s, c)); -} - -static int get_word_len(char const *str, char c) -{ - int i; - int len; - - i = 0; - len = 0; - while (str[i] == c) - i++; - while (str[i] != c && str[i] != '\0') - { - i++; - len++; - } - return (len); -} - -char **ft_strsplit(char const *s, char c) -{ - int i; - int j; - int k; - char **str2; - - if (!s || !(str2 = (char **)malloc(sizeof(*str2) * - (ft_countwords(s, c) + 1)))) - return (NULL); - i = -1; - j = 0; - while (++i < ft_countwords(s, c)) - { - k = 0; - if (!(str2[i] = ft_strnew(get_word_len(&s[j], c) + 1))) - str2[i] = NULL; - while (s[j] == c) - j++; - while (s[j] != c && s[j]) - str2[i][k++] = s[j++]; - str2[i][k] = '\0'; - } - str2[i] = 0; - return (str2); -} diff --git a/libft/src/str/ft_strtok.c b/libft/src/str/ft_strtok.c new file mode 100644 index 00000000..74ebd7e6 --- /dev/null +++ b/libft/src/str/ft_strtok.c @@ -0,0 +1,19 @@ +#include "libft.h" + +char *ft_strtok(char *s, const char *delim) +{ + static char *lasts; + int ch; + + if (s == 0) + s = lasts; + do { + if ((ch = *s++) == 0) + return (0); + } while (ft_strchr(delim, ch)); + --s; + lasts = s + strcspn(s, delim); + if (*lasts != 0) + *lasts++ = 0; + return (s); +}