get_next_line/get_next_line_utils.c

95 lines
2 KiB
C
Raw Normal View History

2024-11-01 12:35:30 +01:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/01 12:31:58 by kcolin #+# #+# */
2024-11-06 17:57:34 +01:00
/* Updated: 2024/11/06 17:37:39 by kcolin ### ########.fr */
2024-11-01 12:35:30 +01:00
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <stdlib.h>
size_t ft_strlen(const char *s)
{
size_t i;
2024-11-06 17:57:34 +01:00
if (s == NULL)
return (0);
2024-11-01 12:35:30 +01:00
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
2024-11-06 17:57:34 +01:00
char *ft_strjoin(char *s1, char *s2)
2024-11-01 12:35:30 +01:00
{
2024-11-06 17:57:34 +01:00
char *out;
2024-11-01 12:35:30 +01:00
size_t i;
2024-11-06 17:57:34 +01:00
size_t j;
2024-11-01 12:35:30 +01:00
2024-11-06 17:57:34 +01:00
if (s1 == NULL)
{
s1 = malloc(1);
s1[0] = '\0';
}
out = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
if (out == NULL)
return (NULL);
2024-11-01 12:35:30 +01:00
i = 0;
2024-11-06 17:57:34 +01:00
j = 0;
while (s1[i] != '\0')
2024-11-01 12:35:30 +01:00
{
2024-11-06 17:57:34 +01:00
out[i] = s1[i];
2024-11-01 12:35:30 +01:00
i++;
}
2024-11-06 17:57:34 +01:00
while (s2[j] != '\0')
2024-11-01 12:35:30 +01:00
{
2024-11-06 17:57:34 +01:00
out[i] = s2[j];
i++;
j++;
2024-11-01 12:35:30 +01:00
}
2024-11-06 17:57:34 +01:00
return (out);
2024-11-01 12:35:30 +01:00
}
2024-11-06 17:57:34 +01:00
char *ft_strchr(const char *s, int c)
2024-11-01 12:35:30 +01:00
{
size_t i;
2024-11-06 17:57:34 +01:00
if (s == NULL)
return (0);
2024-11-01 12:35:30 +01:00
i = 0;
2024-11-06 17:57:34 +01:00
while (s[i] != '\0')
2024-11-01 12:35:30 +01:00
{
2024-11-06 17:57:34 +01:00
if (s[i] == (char)c)
return ((char *)s + i);
2024-11-01 12:35:30 +01:00
i++;
}
2024-11-06 17:57:34 +01:00
if ((char)c == '\0')
return ((char *)s + i);
return (0);
2024-11-01 12:35:30 +01:00
}
2024-11-06 17:57:34 +01:00
char *ft_substr(char const *s, unsigned int start, size_t len)
2024-11-01 12:35:30 +01:00
{
char *out;
2024-11-06 17:57:34 +01:00
size_t i;
2024-11-01 12:35:30 +01:00
2024-11-06 17:57:34 +01:00
i = 0;
if (s[i] == '\0' || start > ft_strlen(s) || len == 0)
return (NULL);
out = malloc((len + 1) * sizeof(char));
2024-11-01 12:35:30 +01:00
if (out == NULL)
return (NULL);
2024-11-06 17:57:34 +01:00
while (i < len)
{
out[i] = s[start + i];
i++;
2024-11-01 12:35:30 +01:00
}
2024-11-06 17:57:34 +01:00
out[i] = '\0';
2024-11-01 12:35:30 +01:00
return (out);
}