-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
108 lines (98 loc) · 2.17 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
97
98
99
100
101
102
103
104
105
106
107
108
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aabda <aabda@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 15:59:37 by aabda #+# #+# */
/* Updated: 2022/07/23 18:04:52 by aabda ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t check_str(char const *s, char c)
{
size_t i;
size_t n;
i = 1;
n = 0;
if (!(s[0]))
return (0);
if (s[0] != c)
n++;
while (s[i])
{
if (s[i] != c && s[i - 1] == c)
n++;
i++;
}
return (n);
}
static void ft_free(char **spl, size_t i)
{
while (i > 0)
{
free(spl[i]);
i--;
}
free(spl[0]);
free(spl);
}
static char *ft_strndup(char const *str, size_t size)
{
char *dest;
size_t i;
dest = (char *)malloc(sizeof(*dest) * (size + 1));
if (!dest)
return (NULL);
i = 0;
while (str[i] && i < size)
{
dest[i] = str[i];
i++;
}
dest[i] = '\0';
return (dest);
}
static char **ft_create_str(char **spl, char const *s, char c, int i)
{
size_t start;
size_t end;
start = 0;
while (s[start])
{
end = 0;
while (s[start + end] && s[start + end] != c)
end++;
if (end > 0)
{
spl[i] = ft_strndup(s + start, end);
if (!spl[i])
{
ft_free(spl, i);
return (NULL);
}
i++;
start = start + end;
}
if (s[start])
start++;
}
spl[i] = NULL;
return (spl);
}
char **ft_split(char const *s, char c)
{
char **spl;
size_t size;
int i;
if (!s)
return (NULL);
i = 0;
size = check_str(s, c);
spl = (char **)malloc(sizeof(spl) * (size + 1));
if (!spl)
return (NULL);
spl = ft_create_str(spl, s, c, i);
return (spl);
}