-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strtrim.c
49 lines (47 loc) · 2.44 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lmartin2 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/23 21:40:50 by lmartin2 #+# #+# */
/* Updated: 2022/02/25 17:47:43 by lmartin2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strtrim(char const *s1, char const *set)
// The ft_strtrim function takes two parameters: s1 and set. It trims leading
// and trailing characters from the input string s1 that are present in the
// set of characters set. It returns a pointer to a newly allocated trimmed
// string or NULL if an error occurs.
{
char *p1;
char *p2;
// The function first checks if the input string s1 is NULL. If it is,
// it returns NULL.
if (!s1)
return (NULL);
// Next, it sets p1 to the start of s1. It then iterates through s1 until
// it reaches a character that is not present in the set. This trims any
// leading characters that match the set.
p1 = (char *)s1;
// Trim leading characters from s1 that are present in the set
while (*p1 && ft_strchr(set, *p1))
p1++;
// If p1 reaches the end of s1, it means all characters have been trimmed.
// In this case, it returns a duplicate of an empty string.
if (*p1 == '\0')
return (ft_strdup(""));
// Then, it sets p2 to the end of s1.
p2 = (char *)s1 + ft_strlen(s1) - 1;
// It then iterates backwards from p2 towards p1 until it reaches a
// character that is not present in the set. This trims any trailing
// characters that match the set.
while (p2 > p1 && ft_strchr(set, *p2))
p2--;
// Finally, it calls ft_substr to extract a substring from s1 starting
// from p1 and ending at p2. This substring represents the trimmed string,
// which is then returned.
return (ft_substr(s1, (unsigned int)(p1 - s1), (size_t)(p2 - p1 + 1)));
}