ft_strtrim - gabrielcoelhodacunha-old/42sp-libft GitHub Wiki
Description
Creates a new string by removing, from the beginning and the end, the characters of set
from s1
.
Implementation
char *ft_strtrim(char const *s1, char const *set)
{
size_t start;
size_t end;
if (!s1 || !set)
return (NULL);
start = get_start_idx(s1, set);
end = get_end_idx(s1, set);
if (start > end)
return (ft_strdup(""));
return (ft_substr(s1, start, end - start + 1));
}
static size_t get_start_idx(char const *s1, char const *set)
{
size_t start;
start = 0;
// Searches for characters to remove from the beginning
while (s1[start] && ft_strchr(set, s1[start]))
start++;
return (start);
}
static size_t get_end_idx(char const *s1, char const *set)
{
size_t end;
end = ft_strlen(s1);
// Searches for characters to remove from the end
while (end && ft_strchr(set, s1[end]))
end--;
return (end);
}