-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit_whitespace.c
97 lines (88 loc) · 2.14 KB
/
ft_strsplit_whitespace.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit_whitespace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbaldy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/14 17:51:24 by dbaldy #+# #+# */
/* Updated: 2016/03/14 18:00:08 by dbaldy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
#include <stdio.h>
static int escape_quotes(char const *s, unsigned int j)
{
int i;
i = j;
if (s[i] == '"')
{
i++;
while (s[i] && s[i] != '"')
i++;
}
if ((unsigned char)s[i] == 39)
{
i++;
while (s[i] && (unsigned char)s[i] != 39)
i++;
}
return (i - j);
}
static int ft_count(char const *s)
{
size_t i;
int j;
i = 0;
j = 0;
while (s[i])
{
i += escape_quotes(s, i);
if (s[i] && (s[i] == ' ' || s[i] == '\n' || s[i] == '\t') &&
s[i + 1] != '\0' && s[i + 1] != ' ' && s[i + 1] != '\n' && s[i + 1] != '\t')
j++;
i++;
}
return (j + 1);
}
static int ft_size(char const *s, unsigned int i)
{
unsigned int j;
j = i;
while (s[i])
{
i += escape_quotes(s, i);
if (s[i] == ' ' || s[i] == '\n' || s[i] == '\t')
break ;
i++;
}
return ((i - j));
}
char **ft_strsplit_whitespace(char const *s)
{
char **s1;
unsigned int i;
unsigned int j;
int size;
if (s == NULL)
return (NULL);
if ((s1 = (char**)malloc((ft_count(s) + 1) * sizeof(char*))) == NULL)
return (NULL);
i = 0;
j = 0;
while (s[j])
{
if (s[j] != ' ' && s[j] != '\n' && s[j] != '\t')
{
size = ft_size(s, j);
s1[i] = ft_strsub(s, j, size);
i++;
j = size + j;
}
else
j++;
}
s1[i] = NULL;
return (s1);
}