-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
112 lines (102 loc) · 2.6 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
109
110
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gpiccion <gpiccion@student.42wolfsburg. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/12 01:05:20 by gpiccion #+# #+# */
/* Updated: 2022/02/12 01:05:20 by gpiccion ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
// Returns the length of a word delimited by the character 'c'.
static size_t ft_word_length(const char *s, char c)
{
size_t i;
size_t len;
i = 0;
len = 0;
while (s[i] == c)
i++;
while (s[i] && s[i] != c)
{
i++;
len++;
}
return (len);
}
// Counts the amount of words delimited by 'c' in the array 's'.
static size_t ft_countwords(const char *s, char c)
{
size_t wcount;
size_t i;
i = 0;
wcount = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i] && s[i] != c)
wcount++;
while (s[i] && s[i] != c)
i++;
}
return (wcount);
}
// Liberates memory of the array pointed by 's'.
static char *ft_freearray(char *s)
{
while (*s)
{
free(s);
s++;
}
return (NULL);
}
// Allocates a word delimited by 'c', and returns a pointer to it.
static char *ft_walloc(char *out, const char *s, char c)
{
size_t i;
i = 0;
while (s[i] == c)
i++;
out = malloc(ft_word_length(s, c) + 1);
if (!out)
return (ft_freearray(out));
while (s[i] != c && s[i])
{
out[i] = s[i];
i++;
}
out[i] = '\0';
return (out);
}
// Allocates and returns an array of strings obtained by splitting 's'
// using the character 'c' as a delimiter.
// The array must be ended by a NULL pointer.
// Returns NULL if the allocation fails.
char **ft_split(char const *s, char c)
{
size_t wcount;
size_t i;
char **out;
if (!s)
return (NULL);
wcount = ft_countwords(s, c);
out = (char **)malloc(sizeof(char *) * (wcount + 1));
if (!out)
return (NULL);
i = 0;
while (i < wcount)
{
while (*s == c)
s++;
out[i] = ft_walloc(out[i], s, c);
i++;
while (*s && *s != c)
s++;
}
out[i] = NULL;
return (out);
}