You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

57 lines
1.6 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 15:27:26 by emartine #+# #+# */
/* Updated: 2017/11/08 15:27:28 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_strtrim_space(char c)
{
return (c == ' ' || c == '\n' || c == '\t');
}
static char *ft_strtrim_copy(char const *s, size_t start, size_t end)
{
size_t i;
char *r;
if (!(r = (char *)malloc(end - start + 1)))
return (NULL);
i = 0;
while (start < end)
r[i++] = s[start++];
r[i] = '\0';
return (r);
}
char *ft_strtrim(char const *s)
{
size_t start;
size_t end;
if (!s)
return (NULL);
end = ft_strlen(s);
start = 0;
if (end > 0)
{
while (s[start] && ft_strtrim_space(s[start]))
start++;
if (s[start] == '\0')
start = end;
else
{
while (--end > 0 && ft_strtrim_space(s[end]))
(void)0;
end++;
}
}
return (ft_strtrim_copy(s, start, end));
}