-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
85 lines (77 loc) · 1.97 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sperez-p <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/24 13:45:37 by sperez-p #+# #+# */
/* Updated: 2021/09/06 17:21:30 by sperez-p ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_line_counter(const char *s, char c)
{
size_t count;
count = 0;
while (*s == c && *s)
s++;
while (*s)
{
while (*s && *s != c)
s++;
while (*s && *s == c)
s++;
count++;
}
return (count);
}
static char *ft_splitdup(const char *s, size_t start, size_t finish)
{
char *dest;
size_t i;
i = 0;
dest = (char *)malloc(sizeof(char) * (finish - start + 1));
if (!dest)
return (NULL);
while (start < finish)
dest[i++] = s[start++];
dest[i] = '\0';
return (dest);
}
static char **ft_fill_split(char **dest, const char *s, char c)
{
size_t i;
size_t p1;
size_t start;
i = 0;
p1 = 0;
start = 0;
while (s[i])
{
while (s[i] != c && s[i])
{
i++;
if (s[i] == c || i == ft_strlen(s))
dest[p1++] = ft_splitdup(s, start, i);
}
while (s[i] == c && s[i])
{
i++;
start = i;
}
}
dest[p1] = NULL;
return (dest);
}
char **ft_split(const char *s, char c)
{
char **dest;
if (!s)
return (NULL);
dest = (char **)malloc(sizeof(char *) * (ft_line_counter(s, c) + 1));
if (!dest)
return (NULL);
ft_fill_split(dest, s, c);
return (dest);
}