-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split_charset.c
More file actions
101 lines (91 loc) · 2.26 KB
/
Copy pathft_split_charset.c
File metadata and controls
101 lines (91 loc) · 2.26 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_charset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atamousse.red <atamousse.red@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/07 10:55:48 by atamousse.red #+# #+# */
/* Updated: 2024/12/18 14:25:07 by atamousse.red ### ########.fr */
/* */
/* ************************************************************************** */
#include "cube.h"
// clang-format off
int check_charset(char *charset, char c)
{
int i;
i = 0;
while (charset[i])
{
if (charset[i] == c)
return (1);
i++;
}
return (0);
}
static int ft_count_words(char const *str, char *charset)
{
int count;
int i;
count = 0;
i = 0;
while (str[i])
{
while (str[i] && check_charset(charset, str[i]) == 1)
i++;
if (str[i] && !(check_charset(charset, str[i]) == 1))
{
count++;
while (str[i] && !(check_charset(charset, str[i]) == 1))
i++;
}
}
return (count);
}
static char **ft_free2(char **strs)
{
int i;
i = 0;
while (strs[i])
free_ptr(strs[i++]);
free_ptr(strs);
return (0);
}
static char **do_it(char **res, char const *s, char *charset, int i)
{
int start;
int end;
int j;
j = 0;
while (s[i])
{
while (s[i] && check_charset(charset, s[i]) == 1)
i++;
start = i;
while (s[i] && check_charset(charset, s[i]) != 1)
i++;
end = i;
if (end > start)
{
res[j] = (char *)mallocate((end - start + 1) * sizeof(char));
if (!res[j])
return (ft_free2(res));
ft_strlcpy(res[j], &s[start], end - start + 1);
j++;
}
}
res[j] = NULL;
return (res);
}
char **ft_split2(char const *s, char *charset)
{
char **res;
int i;
i = 0;
if (!s)
return (NULL);
res = (char **)mallocate((ft_count_words(s, charset) + 1) * sizeof(char *));
if (!res)
return (NULL);
return (do_it(res, s, charset, i));
}