miniRT/libft/ft_substr.c
2025-05-31 19:08:53 +02:00

42 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vvobis <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/04 18:58:25 by vvobis #+# #+# */
/* Updated: 2024/04/09 21:50:44 by vvobis ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *tmp;
unsigned int i;
i = 0;
if (!s || start >= ft_strlen(s) || len <= 0)
{
tmp = malloc(1);
if (!tmp)
return (NULL);
tmp[i] = 0;
return (tmp);
}
if (len + start > ft_strlen(s))
len = ft_strlen(s) - start;
tmp = malloc(sizeof(*tmp) * len + 1);
if (!tmp)
return (NULL);
while (i < len && s[i])
{
tmp[i] = s[i + start];
i++;
}
tmp[i] = 0;
return (tmp);
}