-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
130 lines (119 loc) · 3.01 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lsilva-q <lsilva-q@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/19 11:13:52 by lsilva-q #+# #+# */
/* Updated: 2021/09/25 11:22:03 by lsilva-q ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** Function name:
** > word_count
** Parameters:
** > s (char const *) : String to be word counted.
** > c (char) : Separator character.
** Description:
** > Count how many non-empty words the string s has, based on separator c.
** Return:
** > (size_t). Return the count of non-empty words inside the string.
*/
static size_t word_count(char const *s, char c)
{
size_t i;
size_t count;
i = 0;
count = 0;
while (s[i] != '\0')
{
if (s[i] != c)
{
while (s[i] != c && s[i] != '\0')
i += 1;
count += 1;
}
else
i += 1;
}
return (count);
}
/*
** Function name:
** > freeall
** Parameters:
** > array (char **) : Array to be freed.
** > curr_index (size_t) : Current index of array
** Description:
** > Free all allocated strings in array, to prevent memory leaks.
** > This function must be called only in case of error.
** Return:
** > (void).
*/
static void freeall(char **array, size_t curr_index)
{
while (curr_index > 0)
{
free(array[curr_index]);
curr_index -= 1;
}
free(array[0]);
free(array);
}
/*
** Function name:
** > fill_array
** Parameters:
** > s (char const *) : String containing the expected words to fill array.
** > c (char) : Separator character.
** > array (char **) : Array to be filled.
** Description:
** > Slice string s, considering separator c, then add the words to each
** position of array.
** Return:
** > (char **). Return the filled array.
*/
static char **fill_array(char const *s, char c, char **array)
{
size_t i;
size_t found_chr;
size_t curr_str;
i = 0;
curr_str = 0;
while (s[i] != '\0')
{
if (s[i] != c)
{
found_chr = i;
while (s[i] != c && s[i] != '\0')
i += 1;
array[curr_str] = ft_substr(s, found_chr, (i - found_chr));
if (array[curr_str] == NULL)
{
freeall(array, i);
return (NULL);
}
curr_str += 1;
}
else
i += 1;
}
return (array);
}
char **ft_split(char const *s, char c)
{
char **array;
size_t wc;
if (!s)
return (NULL);
wc = word_count(s, c);
array = ft_calloc(wc + 1, sizeof(char *));
if (!array)
return (NULL);
array = fill_array(s, c, array);
if (array)
array[wc] = NULL;
return (array);
}