This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
47 lines (43 loc) · 1.54 KB
/
ft_strsplit.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jstaunto <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/25 13:58:42 by jstaunto #+# #+# */
/* Updated: 2020/08/25 13:58:42 by jstaunto ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **free_wrd(char **words, size_t i)
{
while (i--)
ft_strdel(&(words[i]));
free(*words);
return (NULL);
}
char **ft_strsplit(char const *s, char c)
{
char **new;
size_t idx;
size_t widx;
size_t inw;
size_t bg;
if (!s || !c || !(new = (char **)ft_memalloc(sizeof(char *) *
(ft_howmwords(s, c) + 1))))
return (NULL);
idx = -1;
widx = 0;
inw = 0;
while (s[++idx])
{
if (inw && s[idx] == c)
if (!(new[widx++] = ft_strsub(s, bg, idx - bg)))
return (free_wrd(new, widx));
(!inw && s[idx] != c) ? bg = idx : 0;
inw = (s[idx] == c) ? 0 : 1;
}
inw ? new[widx] = ft_strsub(s, bg, idx - bg) : 0;
return (new);
}