-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
153 lines (143 loc) · 4.08 KB
/
ft_split.c
File metadata and controls
153 lines (143 loc) · 4.08 KB
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
149
150
151
152
153
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lgasc <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/06 19:40:37 by lgasc #+# #+# */
/* Updated: 2023/10/24 12:15:49 by lgasc ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static unsigned int count_subtrings(
const char *const superstring,
const char delimiter);
static unsigned int allocate_substrings(
const char *const whole,
const char delimiter,
char **const array);
static void set_substrings(
const char *const whole,
const char delimiter,
char *const *const array);
///Allocates (with `malloc`(3)) and returns an array of strings obtained by
/// splitting the `whole` using a `delimiter` character.
///The array must end with a `NULL` pointer.
///
///Leading and trailing delimiters are skup;
///There will be no empty string (`""`) in the returned array.
///
///@param[in] whole The string to be split.
///@param[in] delimiter The delimiter character.
///
///@returns The array of new strings resulting from the split.
/// `NULL` if the allocation fails.
///
///@remarks External functions: `malloc`, `free`
char **ft_split(char const *whole, char delimiter)
{
char **split;
unsigned int error;
unsigned int i;
if (! whole)
return (NULL);
split = ft_calloc(count_subtrings(whole, delimiter) + 1, sizeof * split);
if (! split)
return (NULL);
split[count_subtrings(whole, delimiter)] = NULL;
error = allocate_substrings(whole, delimiter, split);
if (error)
{
i = 0;
while (split[i])
{
free(split[i]);
i++;
}
free(split);
return (NULL);
}
set_substrings(whole, delimiter, split);
return (split);
}
static unsigned int count_subtrings(
const char *const superstring, const char delimiter)
{
unsigned int i;
unsigned char in_substring;
unsigned int substring_count;
i = 0;
in_substring = 0;
substring_count = 0;
while (superstring[i])
{
if (in_substring && superstring[i] == delimiter)
in_substring = 0;
else if (! in_substring && superstring[i] != delimiter)
{
in_substring = 1;
substring_count++;
}
i++;
}
return (substring_count);
}
/** @returns An error code: 0 means no errors; 1 means allocation error. */
static unsigned int allocate_substrings(
const char *const whole, const char delimiter, char **const array)
{
unsigned int i;
unsigned char is_at_a_delimiter;
unsigned int sub_index;
unsigned int sub_length;
i = 0;
is_at_a_delimiter = 1;
sub_index = 0;
while (whole[i])
{
if (! is_at_a_delimiter && whole[i] == delimiter)
is_at_a_delimiter = 1;
else if (is_at_a_delimiter && whole[i] != delimiter)
{
sub_length = 0;
while (whole[i + sub_length] && whole[i + sub_length] != delimiter)
sub_length++;
array[sub_index] = ft_calloc(sub_length + 1, sizeof * *array);
if (! array[sub_index++])
return (1);
is_at_a_delimiter = 0;
}
i++;
}
return (0);
}
void set_substrings(
const char *const whole, const char delimiter, char *const *const split)
{
unsigned int i;
unsigned char in_substring;
unsigned int substring_index;
unsigned int j;
i = 0;
in_substring = 0;
substring_index = 0;
while (whole[i])
{
if (in_substring && whole[i] == delimiter)
in_substring = 0;
else if ((! in_substring) && whole[i] != delimiter)
{
j = 0;
while (whole[i + j] && whole[i + j] != delimiter)
{
split[substring_index][j] = whole[i + j];
j++;
}
split[substring_index++][j] = '\0';
in_substring++;
}
i++;
}
}