Libft
42 Libft library documentation
Loading...
Searching...
No Matches
ft_split.c File Reference
+ Include dependency graph for ft_split.c:

Go to the source code of this file.

Functions

static size_t ft_count_words (char const *s, char c)
 Counts words separated by a delimiter in a string.
 
static char ** ft_fill_split (char **split, char const *s, char c)
 Fills the split array with allocated word substrings.
 
static void ft_free_split (char **split, size_t i)
 Frees partially allocated split entries.
 
char ** ft_split (char const *s, char c)
 Splits a string into an array of substrings.
 

Function Documentation

◆ ft_count_words()

static size_t ft_count_words ( char const *  s,
char  c 
)
static

Counts words separated by a delimiter in a string.

Parameters
sInput string.
cDelimiter character.
Returns
Number of words.

Definition at line 83 of file ft_split.c.

84{
85 size_t count;
86 size_t i;
87
88 count = 0;
89 i = 0;
90 while (s[i])
91 {
92 if (s[i] != c)
93 {
94 count++;
95 while (s[i] && s[i] != c)
96 i++;
97 }
98 else
99 i++;
100 }
101 return (count);
102}

Referenced by ft_split().

+ Here is the caller graph for this function:

◆ ft_fill_split()

static char ** ft_fill_split ( char **  split,
char const *  s,
char  c 
)
static

Fills the split array with allocated word substrings.

Parameters
splitDestination array.
sInput string.
cDelimiter character.
Returns
split on success, NULL on allocation failure.

Definition at line 50 of file ft_split.c.

51{
52 size_t i;
53 size_t j;
54 size_t start;
55
56 i = 0;
57 j = 0;
58 while (s[i])
59 {
60 if (s[i] != c)
61 {
62 start = i;
63 while (s[i] && s[i] != c)
64 i++;
65 split[j] = ft_substr(s, start, i - start);
66 if (!split[j++])
67 return (ft_free_split(split, j - 1), NULL);
68 }
69 else
70 i++;
71 }
72 split[j] = NULL;
73 return (split);
74}
static void ft_free_split(char **split, size_t i)
Frees partially allocated split entries.
Definition ft_split.c:110
char * ft_substr(char const *s, unsigned int start, size_t len)
Extracts a substring from a string.
Definition ft_substr.c:23

References ft_free_split(), and ft_substr().

Referenced by ft_split().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ft_free_split()

static void ft_free_split ( char **  split,
size_t  i 
)
static

Frees partially allocated split entries.

Parameters
splitArray of strings.
iNumber of initialized entries.

Definition at line 110 of file ft_split.c.

111{
112 while (i > 0)
113 free(split[--i]);
114 free(split);
115}

Referenced by ft_fill_split().

+ Here is the caller graph for this function:

◆ ft_split()

char ** ft_split ( char const *  s,
char  c 
)

Splits a string into an array of substrings.

Uses the delimiter c to separate words. The returned array is NULL-terminated.

Parameters
sInput string.
cDelimiter character.
Returns
Newly allocated NULL-terminated array, or NULL on failure.

Definition at line 30 of file ft_split.c.

31{
32 char **split;
33
34 if (!s)
35 return (NULL);
36 split = malloc(sizeof(char *) * (ft_count_words(s, c) + 1));
37 if (!split)
38 return (NULL);
39 return (ft_fill_split(split, s, c));
40}
static size_t ft_count_words(char const *s, char c)
Counts words separated by a delimiter in a string.
Definition ft_split.c:83
static char ** ft_fill_split(char **split, char const *s, char c)
Fills the split array with allocated word substrings.
Definition ft_split.c:50

References ft_count_words(), and ft_fill_split().

+ Here is the call graph for this function: