-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split_sep.c
83 lines (74 loc) · 1.83 KB
/
ft_split_sep.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_sep.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <zhabri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 17:37:29 by zhabri #+# #+# */
/* Updated: 2023/01/09 13:48:15 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static bool is_in_str(char *sep, char c)
{
int i;
i = 0;
while (sep[i])
if (sep[i++] == c)
return (true);
return (false);
}
static int tab_len(char *str, char *sep)
{
int i;
int n;
i = 0;
n = 0;
while (str[i])
{
if (!is_in_str(sep, str[i]))
if ((!is_in_str(sep, str[i + 1]))
|| !str[i + 1])
n++;
i++;
}
return (n);
}
static char **split_loop(char **tab, char const *s, char *sep)
{
size_t i;
size_t j;
char *tmp;
i = 0;
j = 0;
while (i <= ft_strlen(s))
{
if (!s[i] || (is_in_str(sep, s[i])))
{
if (j < i)
{
tmp = ft_substr(s, j, i - j);
if (!tmp)
{
free_tab(tab);
return (NULL);
}
add_to_tab(tab, tmp);
}
j = i + 1;
}
i++;
}
return (tab);
}
char **ft_split_sep(char const *s, char *sep)
{
char **tab;
if (!s)
return (NULL);
tab = NULL;
while (tab == NULL)
tab = ft_calloc(tab_len((char *)s, sep) + 2, sizeof(char *));
return (split_loop(tab, s, sep));
}