-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
40 lines (37 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abaudot <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/05 15:10:58 by abaudot #+# #+# */
/* Updated: 2021/01/05 18:00:20 by abaudot ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(const char *s1, const char *set)
{
unsigned char hastab[128];
size_t len;
char *ans;
if (!s1)
return (0);
if (!set || !*set)
return (ft_strdup(s1));
ft_bzero(hastab, 128);
while (*set)
hastab[(unsigned char)*set++] = 1;
while (*s1 && hastab[(unsigned char)*s1])
s1++;
len = ft_strlen(s1);
if (len--)
while (hastab[(unsigned char)*(s1 + len)])
len--;
++len;
if (!(ans = (char *)malloc(len + 1)))
return (0);
ft_memcpy(ans, s1, len);
ans[len] = 0;
return (ans);
}