-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
61 lines (55 loc) · 1.67 KB
/
ft_strtrim.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbrandao <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/09 00:10:58 by dbrandao #+# #+# */
/* Updated: 2022/07/04 06:35:13 by dbrandao ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int find_char(const char *set, char c)
{
if (ft_strchr(set, c))
return (1);
return (0);
}
static int get_begin_position(char **begin, char const *s1, char const *set)
{
size_t i;
i = 0;
while (find_char(set, **begin))
{
(*begin)++;
i++;
if (i >= ft_strlen(s1))
return (0);
}
return (1);
}
char *ft_strtrim(char const *s1, char const *set)
{
char *begin;
char *end;
char *trimmed;
size_t i;
if (!s1 || !set)
return (NULL);
begin = (char *) s1;
if (!get_begin_position(&begin, s1, set))
return ft_strdup("");
end = (char *) &s1[ft_strlen(s1) - 1];
while (find_char(set, *end))
end--;
i = 0;
while (&begin[i] != end)
i++;
i++;
trimmed = (char *) malloc(i + 1);
if (!trimmed)
return (NULL);
ft_strlcpy(trimmed, begin, i + 1);
return (trimmed);
}