This repository was archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
executable file
·148 lines (137 loc) · 3.16 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_split.c :+: :+: */
/* +:+ */
/* By: fbes <fbes@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/10/27 15:41:13 by fbes #+# #+# */
/* Updated: 2022/02/08 19:48:05 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static size_t ft_getsepsamount(char const *s, char c)
{
size_t amount;
size_t i;
amount = 0;
i = 0;
while (s[i] != '\0')
{
if (i > 0 && s[i] == c && s[i - 1] != c)
amount++;
else if (s[i] != c && s[i + 1] == '\0')
amount++;
i++;
}
return (amount);
}
static char **ft_getpartsstart(char const *s, char c, size_t amount)
{
size_t i;
size_t j;
char **ret;
ret = (char **)malloc((amount + 1) * sizeof(char *));
if (ret)
{
i = 0;
j = 0;
while (s[i] != '\0')
{
if (i == 0 && s[i] != c)
{
ret[j] = (char *)&s[i];
j++;
}
else if (i > 0 && s[i] != c && s[i - 1] == c)
{
ret[j] = (char *)&s[i];
j++;
}
i++;
}
}
return (ret);
}
static char **ft_getpartsend(char const *s, char c, size_t amount)
{
size_t i;
size_t j;
char **ret;
ret = (char **)malloc((amount + 1) * sizeof(char *));
if (ret)
{
i = 0;
j = 0;
while (s[i] != '\0')
{
if (i > 0 && s[i] == c && s[i - 1] != c)
{
ret[j] = (char *)&s[i];
j++;
}
else if (i > 0 && s[i] != c && s[i + 1] == '\0')
{
ret[j] = (char *)&s[i + 1];
j++;
}
i++;
}
}
return (ret);
}
static char **splitfree(char **arr, char **p_s, char **p_e, size_t amount)
{
size_t i;
if (arr)
{
i = 0;
while (i < amount)
{
if (arr[i])
free(arr[i]);
i++;
}
free(arr);
}
if (p_s)
free(p_s);
if (p_e)
free(p_e);
return (NULL);
}
/**
* Split a string on certain characters
* @param[in] *s The string to split
* @param[in] c The character to split the string at
* @return An array of strings containing the split up string,
* NULL on error, array ends with NULL
*/
char **ft_split(char const *s, char c)
{
char **arr;
char **p_start;
char **p_end;
size_t i;
size_t p_amount;
p_amount = ft_getsepsamount(s, c);
arr = (char **)malloc((p_amount + 1) * sizeof(char *));
if (!arr)
return (NULL);
p_start = ft_getpartsstart(s, c, p_amount);
p_end = ft_getpartsend(s, c, p_amount);
i = 0;
if (!p_start || !p_end)
return (splitfree(arr, p_start, p_end, p_amount));
while (i < p_amount)
{
arr[i] = ft_substr(s, p_start[i] - s, p_end[i] - p_start[i]);
if (!arr[i])
return (splitfree(arr, p_start, p_end, p_amount));
i++;
}
splitfree(NULL, p_start, p_end, p_amount);
arr[i] = 0;
return (arr);
}