-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
71 lines (64 loc) · 1.73 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
62
63
64
65
66
67
68
69
70
71
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aizsak <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/07 11:48:15 by aizsak #+# #+# */
/* Updated: 2022/11/12 10:11:07 by aizsak ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int iz_char(char c, const char *set)
{
size_t i;
i = 0;
while (set[i])
{
if (set[i] == c)
return (1);
i++;
}
return (0);
}
static char *trim(const char *s1, const char *set, size_t *k, size_t i)
{
size_t j;
size_t len;
char *dst;
len = ft_strlen(s1);
j = 0;
while (iz_char(*(s1 + len - j - 1), set))
j++;
dst = ft_calloc(sizeof(char), len - (j + i) + 1);
if (dst == NULL)
return (NULL);
while (*k < len - (j + i))
{
*(dst + *k) = *(s1 + i + *k);
*k += 1;
}
return (dst);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t i;
size_t k;
size_t len;
char *dst;
if (s1 == NULL)
return (NULL);
i = 0;
len = ft_strlen(s1);
while (iz_char(*(s1 + i), set))
i++;
k = 0;
if (i == len)
dst = malloc(1);
else
dst = trim(s1, set, &k, i);
if (dst != NULL)
*(dst + k) = '\0';
return (dst);
}