/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_strtrim.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cchauvet <cchauvet@student.42angoulem      +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/09/29 11:33:53 by cchauvet          #+#    #+#             */
/*   Updated: 2022/09/29 18:03:20 by cchauvet         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"

static int	ft_is_in(const char c, const char *charset)
{
	while (*charset)
	{
		if (c == *charset)
			return (1);
		charset++;
	}
	return (0);
}

char	*ft_strtrim(const char *s1, const char *set)
{
	size_t	start;
	size_t	stop;
	char	*str;

	if (s1 == NULL || set == NULL)
		return (NULL);
	if (!*s1 || !*s1)
		return (ft_strdup(""));
	start = 0;
	while (ft_is_in(s1[start], set))
		start++;
	stop = ft_strlen(s1) - 1;
	while (ft_is_in(s1[stop], set))
		stop--;
	str = ft_substr(s1, start, stop - start + 1);
	return (str);
}