-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strtrim.c
More file actions
64 lines (57 loc) · 1.66 KB
/
ft_strtrim.c
File metadata and controls
64 lines (57 loc) · 1.66 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sryou <sryou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 19:53:35 by sryou #+# #+# */
/* Updated: 2022/03/19 11:46:50 by sryou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t get_idx_st(char const *s1, char const *set)
{
size_t idx;
idx = 0;
while (s1[idx])
{
if (!ft_strchr(set, s1[idx]))
break ;
idx++;
}
return (idx);
}
static size_t get_idx_ed(char const *s1, char const *set)
{
size_t idx;
idx = ft_strlen(s1);
while (1)
{
if (!ft_strchr(set, s1[idx]))
break ;
if (idx == 0)
break ;
idx--;
}
return (idx);
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t st;
size_t ed;
char *mkstr;
if (s1 == 0)
return (0);
if (set == 0)
return (ft_strdup(s1));
st = get_idx_st(s1, set);
ed = get_idx_ed(s1, set);
if (st > ed)
return (ft_strdup("\0"));
mkstr = (char *)malloc(sizeof(char) * (ed - st + 2));
if (mkstr == 0)
return (0);
ft_strlcpy(mkstr, s1 + st, ed - st + 2);
return (mkstr);
}