-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strtrim.c
48 lines (44 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_strtrim.c :+: :+: */
/* +:+ */
/* By: splattje <splattje@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2023/10/09 12:37:55 by splattje #+# #+# */
/* Updated: 2023/10/19 10:23:51 by splattje ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
static void set_start_end(char **start, char **end, char const *set)
{
while (**start && ft_strchr(set, **start))
(*start)++;
while (*end > *start && ft_strchr(set, **end))
(*end)--;
}
char *ft_strtrim(char const *s1, char const *set)
{
size_t len;
char *start;
char *end;
size_t new_len;
char *trimmed;
if (s1 == NULL)
return (NULL);
if (set == NULL)
return ((char *)s1);
len = 0;
while (s1[len])
len++;
start = (char *)s1;
end = (char *)s1 + len - 1;
set_start_end(&start, &end, set);
new_len = (size_t)(end - start) + 1;
trimmed = (char *)malloc(new_len + 1);
if (trimmed == NULL)
return (NULL);
ft_memcpy(trimmed, start, new_len);
trimmed[new_len] = '\0';
return (trimmed);
}