-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
80 lines (71 loc) · 2.24 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thakala <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/18 10:09:08 by thakala #+# #+# */
/* Updated: 2021/12/12 17:36:38 by thakala ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static size_t ft_count_words(const char *string, const char delimiter)
{
size_t words;
words = 0;
words += *string != delimiter;
while (*string && *(string + 1))
words += (*string++ == delimiter && *string != delimiter);
words += !words;
return (words);
}
static void ft_fill_word(char *word, const char delimiter, const char **s_ptr)
{
while (**s_ptr && **s_ptr != delimiter)
*word++ = *(*s_ptr)++;
*word = '\0';
}
static char *ft_get_next_word(const char **s_ptr, const char delimiter)
{
char *word;
size_t word_length;
word_length = 0;
while (**s_ptr && **s_ptr == delimiter)
*s_ptr += 1;
while ((*s_ptr)[word_length] && (*s_ptr)[word_length] != delimiter)
word_length++;
word = (char *)malloc(sizeof(char) * (word_length + 1));
if (!word)
return (NULL);
ft_fill_word(word, delimiter, s_ptr);
return (word);
}
static void *free_all(char **words, size_t count)
{
while (count + 1 > 0)
free(words[count--]);
free(words);
return (NULL);
}
char **ft_strsplit(char const *s, char c)
{
size_t word_count;
size_t i;
char **words;
word_count = ft_count_words(s, c);
words = (char **)malloc(sizeof(*words) * (word_count + 1));
if (!words)
return (NULL);
i = 0;
while (i < word_count)
{
words[i] = ft_get_next_word(&s, c);
if (!words[i])
return (free_all(words, i - 1));
i++;
}
words[i] = 0;
return (words);
}