-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
96 lines (85 loc) · 2.09 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
86
87
88
89
90
91
92
93
94
95
96
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: meldora <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/05 13:22:32 by meldora #+# #+# */
/* Updated: 2020/11/06 19:25:10 by meldora ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_counter(const char *s, const char c)
{
char *ptr;
size_t count;
ptr = (char *)s;
count = 0;
while (*ptr)
{
if (*ptr != c)
count++;
while (*ptr != c && *ptr)
ptr++;
if (*ptr)
ptr++;
}
return (count);
}
static size_t ft_strclen(const char *s, const char c)
{
size_t i;
i = 0;
while (s[i] != c && s[i])
i++;
return (i);
}
static void ft_free(char **res, size_t count)
{
size_t i;
i = 0;
while (i < count)
{
free(res[i]);
i++;
}
}
static char **ft_sub_split(const char *s, char **res, char c, size_t count)
{
char *ptr;
size_t len;
size_t i;
ptr = (char *)s;
len = -1;
i = 0;
while (i < count)
{
ptr += len + 1;
while (*ptr == c && *ptr)
ptr++;
len = ft_strclen(ptr, c);
if (!(res[i] = (char *)malloc(sizeof(char) * (len + 1))))
{
ft_free(res, count);
return (NULL);
}
ft_strlcpy(res[i], ptr, len + 1);
res[i][len] = '\0';
i++;
}
res[i] = NULL;
return (res);
}
char **ft_split(const char *s, char c)
{
char **res;
size_t count;
if (!s || !c)
return (NULL);
count = ft_counter(s, c);
if (!(res = (char **)malloc(sizeof(char *) * (count + 1))))
return (NULL);
res = ft_sub_split(s, res, c, count);
return (res);
}