-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_utils.c
74 lines (65 loc) · 1.81 KB
/
split_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* split_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bpisak-l <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 20:38:57 by bpisak-l #+# #+# */
/* Updated: 2024/07/25 15:52:43 by bpisak-l ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h"
int is_word_start(char *map, int i)
{
int after_delim;
after_delim = !i || map[i - 1] == DELIMITER;
return (after_delim && map[i] != DELIMITER);
}
char **init_words(char *map)
{
int words;
int i;
char **res;
words = 0;
i = -1;
while (map[++i])
{
if (is_word_start(map, i))
words++;
}
res = m_ft_calloc(words + 1, sizeof(char *));
return (res);
}
char *set_delim_zeros(char *map, char *s)
{
char *clone;
clone = m_ft_strdup(s);
ft_replace_chars(clone, map, 0);
return (clone);
}
char **str_split(char *s, char *delim, char *skip)
{
char *map;
int i;
int j;
char *clone;
char **res;
map = operation_map(s, delim, skip);
clone = set_delim_zeros(map, s);
res = init_words(map);
i = -1;
j = 0;
while (map[++i])
{
if (is_word_start(map, i))
{
res[j++] = ft_strdup(clone + i);
if (!res[j - 1])
return (split_fail_free(res, j, map, clone));
}
}
free(clone);
free(map);
return (res);
}