-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
85 lines (80 loc) · 4.65 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahabachi <ahabachi@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 15:12:34 by ahabachi #+# #+# */
/* Updated: 2022/10/06 15:12:36 by ahabachi ### ########.fr */
/* */
/* ************************************************************************** */
/* */
/* ▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄▄▄▄▄ */
/* ▐░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌ */
/* ▐░▌ ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▀▀▀▀█░█▀▀▀▀ */
/* ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ */
/* ▐░▌ ▐░▌ ▐░█▄▄▄▄▄▄▄█░▌▐░█▄▄▄▄▄▄▄▄▄ ▐░▌ */
/* ▐░▌ ▐░▌ ▐░░░░░░░░░░▌ ▐░░░░░░░░░░░▌ ▐░▌ */
/* ▐░▌ ▐░▌ ▐░█▀▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀▀▀ ▐░▌ */
/* ▐░▌ ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ */
/* ▐░█▄▄▄▄▄▄▄▄▄ ▄▄▄▄█░█▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░▌ ▐░▌ */
/* ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░▌ ▐░▌ ▐░▌ */
/* ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀▀▀▀ ▀ ▀ */
/* */
/* ************************************************************************** */
/* █████████ ██████████ ██████████ ██████████ */
/* ██ ██ ██ ██ ██ ██ */
/* ██ ██ ██ ██ ██ */
/* ██ ██ ██ ██ */
/* ██ ██████████ ██████████ ██ */
/* ██ ██ ██ ██ */
/* ██ ██ ██ ██ */
/* ██ ██ ██ ██ */
/* ████████ ██████████ ██████████ ██ */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int my_spliter(char **ret, char const *s, char c)
{
int i;
int count;
count = 0;
while (s[0])
{
i = 0;
while (s[i] && (s[i] != c))
i++;
if (ret)
ret[count] = ft_substr(s, 0, i);
if (ret && ret[count] == NULL)
return (-1);
count++;
while (s[i] && (s[i] == c))
i++;
s = s + i;
}
return (count);
}
char **ft_split(char const *s, char c)
{
int i;
char **ret;
if (!s)
return (0);
while (*s && *s == c)
s++;
if (!*s)
return (ft_calloc(1, sizeof(char *)));
ret = ft_calloc(my_spliter(0, s, c) + 1, sizeof(char *));
if (my_spliter(ret, s, c) < 0)
{
i = 0;
while (ret[i])
free(ret[i++]);
free(ret);
return (NULL);
}
return (ret);
}