-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils_bonus.c
113 lines (102 loc) · 2.36 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hel-mefe <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 15:31:32 by hel-mefe #+# #+# */
/* Updated: 2021/12/01 15:31:35 by hel-mefe ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
size_t ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i])
{
if (str[i] == '\n')
return (i + 1);
i++;
}
return (i);
}
void ft_bzero(char *s, unsigned int start, size_t len)
{
unsigned char *str;
unsigned char c;
str = (unsigned char *) s;
c = 0;
while (start < len)
{
str[start] = c;
start++;
}
}
char *ft_strdup(const char *s)
{
size_t i;
size_t len;
char *res;
len = ft_strlen(s);
res = (char *) malloc ((len + 1) * sizeof(char));
if (!res)
return (NULL);
i = 0;
while (i < len)
{
res[i] = s[i];
i++;
}
res[i] = 0;
return (res);
}
char *ft_substr(const char *s, unsigned int start, size_t len)
{
size_t i;
char *res;
if (!s)
return (NULL);
if (start > len || start > ft_strlen(s))
return (ft_strdup(""));
else if (len > ft_strlen(s))
len = ft_strlen(s) - start;
res = (char *) malloc ((len + 1) * sizeof(char));
if (!res)
return (NULL);
i = 0;
while (i < len)
{
res[i] = s[start];
i++;
start++;
}
res[i] = 0;
return (res);
}
char *ft_strjoin(const char *s1, const char *s2)
{
size_t i;
size_t j;
size_t len;
char *res;
if (!s1 && !s2)
return (NULL);
else if (!s1 && s2)
return (ft_strdup(s2));
else if (!s2 && s1)
return (ft_strdup(s1));
len = ft_strlen(s1) + ft_strlen(s2);
res = (char *) malloc ((len + 1) * sizeof(char));
if (!res)
return (NULL);
i = -1;
while (++i < len && s1[i])
res[i] = s1[i];
j = 0;
while (i < len)
res[i++] = s2[j++];
res[i] = 0;
return (res);
}