-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
91 lines (82 loc) · 2.23 KB
/
get_next_line_utils_bonus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* ************************************************************************** */
/* */
/* :::::::: */
/* get_next_line_utils_bonus.c :+: :+: */
/* +:+ */
/* By: dreijans <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/12/22 13:18:14 by dreijans #+# #+# */
/* Updated: 2023/01/23 16:53:00 by dreijans ######## odam.nl */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
if (s == NULL)
return (0);
while (s[i] != '\0')
i++;
return (i);
}
char *ft_strlcpy(char *dst, const char *src, size_t dstsize)
{
size_t i;
i = 0;
while (src[i] != '\0' && (i + 1) < dstsize)
{
dst[i] = src[i];
i++;
}
if (i < dstsize)
dst[i] = '\0';
return (dst);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *new_str;
size_t strlen;
strlen = ft_strlen((char *)s);
if (start >= strlen)
len = 0;
else if (len > strlen - start)
len = strlen - start;
if (len == 0)
return (NULL);
new_str = malloc(sizeof(char) * (len + 1));
if (new_str == NULL)
return (NULL);
if (strlen == 0 || len == 0)
new_str[0] = 0;
else
ft_strlcpy(new_str, &s[start], len + 1);
return (new_str);
}
char *ft_copy_join(char *s1, char *s2)
{
char *new_str;
size_t len;
size_t i;
size_t j;
i = 0;
j = 0;
if (s1 == NULL && s2 == NULL)
return (NULL);
len = (ft_strlen(s1) + ft_strlen(s2)) + 1;
new_str = (char *)malloc(sizeof (char) * len);
if (new_str == NULL)
return (NULL);
if (s1 == NULL)
{
ft_strlcpy(new_str, s2, ft_strlen(s2) + 1);
return (new_str);
}
while (s1 != NULL && s1[i] != '\0')
new_str[j++] = s1[i++];
i = 0;
while (s2 != NULL && s2[i] != '\0')
new_str[j++] = s2[i++];
new_str[j] = '\0';
return (new_str);
}