-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
67 lines (61 loc) · 1.68 KB
/
ft_split.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marmulle <marmulle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/04 16:07:52 by marmulle #+# #+# */
/* Updated: 2022/11/29 13:10:43 by marmulle ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_words(char const *s, char c)
{
int count;
count = 0;
while (*s)
{
while (*s == c)
++s;
if (*s)
++count;
while (*s && *s != c)
++s;
}
return (count);
}
static char **ft_cleanup(char **split, int w)
{
while (--w >= 0)
free(split[w]);
free(split);
return (NULL);
}
char **ft_split(char const *s, char c)
{
char **split;
size_t word_len;
int w;
if (!s)
return (NULL);
split = ft_calloc(ft_count_words(s, c) + 1, sizeof(char *));
if (!split)
return (NULL);
w = 0;
while (*s)
{
while (*s == c)
++s;
if (!(*s))
return (split);
word_len = 0;
while (s[word_len] != '\0' && s[word_len] != c)
++word_len;
split[w] = ft_substr(s, 0, word_len);
if (!split[w++])
return (ft_cleanup(split, w));
s += word_len;
}
return (split);
}