strtok, need to rewrite strcspn next
This commit is contained in:
parent
18a111051f
commit
7d6845a44c
5 changed files with 23 additions and 71 deletions
|
|
@ -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\
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,70 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_strsplit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
19
libftasm/src/str/ft_strtok.c
Normal file
19
libftasm/src/str/ft_strtok.c
Normal file
|
|
@ -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);
|
||||
}
|
||||
Loading…
Reference in a new issue