65 lines
1.8 KiB
C
Executable file
65 lines
1.8 KiB
C
Executable file
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_strtrim.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/10/15 12:44:26 by thrieg #+# #+# */
|
|
/* Updated: 2025/02/16 19:05:20 by thrieg ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
|
|
static size_t ft_str_size(char const *s1, char usedchar[256])
|
|
{
|
|
size_t size;
|
|
size_t index;
|
|
|
|
size = 0;
|
|
index = 0;
|
|
while (*s1)
|
|
{
|
|
while (usedchar[((int)*s1) + 128])
|
|
{
|
|
s1++;
|
|
index++;
|
|
}
|
|
if (*s1)
|
|
{
|
|
s1++;
|
|
index++;
|
|
size = index;
|
|
}
|
|
}
|
|
return (size);
|
|
}
|
|
|
|
//returns NULL if any argument is null or if an allocation fails
|
|
char *ft_strtrim(char const *s1, char const *set)
|
|
{
|
|
char *ret;
|
|
char usedchar[256];
|
|
int start_index;
|
|
size_t size;
|
|
|
|
if (!set || !s1)
|
|
return (NULL);
|
|
if (!*set)
|
|
return (ft_strdup(s1));
|
|
ft_bzero(usedchar, 256);
|
|
while (*set)
|
|
usedchar[((int)*set++) + 128] = 1;
|
|
start_index = 0;
|
|
while (usedchar[((int)s1[start_index]) + 128])
|
|
start_index++;
|
|
if (!s1[start_index])
|
|
return (ft_strdup(""));
|
|
size = ft_str_size(&s1[start_index], usedchar);
|
|
ret = malloc(sizeof(char) * (size + 1));
|
|
if (!ret)
|
|
return (NULL);
|
|
ft_strlcpy(ret, &s1[start_index], (size + 1));
|
|
return (ret);
|
|
}
|