91 lines
2 KiB
C
91 lines
2 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* get_next_line_utils.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/11/01 12:31:58 by kcolin #+# #+# */
|
|
/* Updated: 2024/11/08 15:04:19 by kcolin ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "get_next_line.h"
|
|
#include <stdlib.h>
|
|
|
|
size_t ft_strlen(const char *s)
|
|
{
|
|
size_t i;
|
|
|
|
if (s == NULL)
|
|
return (0);
|
|
i = 0;
|
|
while (s[i] != '\0')
|
|
i++;
|
|
return (i);
|
|
}
|
|
|
|
char *ft_strjoin(char *s1, char *s2)
|
|
{
|
|
char *out;
|
|
size_t i;
|
|
size_t j;
|
|
|
|
if (s1 == NULL)
|
|
{
|
|
s1 = malloc(1);
|
|
if (s1 == NULL)
|
|
return (NULL);
|
|
s1[0] = '\0';
|
|
}
|
|
out = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
|
|
if (out == NULL)
|
|
return (NULL);
|
|
i = -1;
|
|
j = -1;
|
|
while (s1[++i] != '\0')
|
|
out[i] = s1[i];
|
|
while (s2[++j] != '\0')
|
|
out[i++] = s2[j];
|
|
out[i] = '\0';
|
|
free(s1);
|
|
return (out);
|
|
}
|
|
|
|
char *ft_strchr(const char *s, int c)
|
|
{
|
|
size_t i;
|
|
|
|
if (s == NULL)
|
|
return (0);
|
|
i = 0;
|
|
while (s[i] != '\0')
|
|
{
|
|
if (s[i] == (char)c)
|
|
return ((char *)s + i);
|
|
i++;
|
|
}
|
|
if ((char)c == '\0')
|
|
return ((char *)s + i);
|
|
return (0);
|
|
}
|
|
|
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
|
{
|
|
char *out;
|
|
size_t i;
|
|
|
|
i = 0;
|
|
if (s[i] == '\0' || start > ft_strlen(s) || len == 0)
|
|
return (NULL);
|
|
out = malloc((len + 1) * sizeof(char));
|
|
if (out == NULL)
|
|
return (NULL);
|
|
while (i < len)
|
|
{
|
|
out[i] = s[start + i];
|
|
i++;
|
|
}
|
|
out[i] = '\0';
|
|
return (out);
|
|
}
|