-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
102 lines (91 loc) · 2.16 KB
/
get_next_line_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmasuda <mmasuda@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/23 17:42:23 by mmasuda #+# #+# */
/* Updated: 2021/04/27 23:28:40 by mmasuda ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
static char *ft_strcpy(char *dst, const char *src)
{
int i;
i = 0;
while (src[i] != '\0')
{
dst[i] = src[i];
i++;
}
dst[i] = '\0';
return (dst);
}
char *ft_strdup(const char *s1)
{
char *s2;
int len;
len = ft_strlen(s1);
s2 = (char *)malloc(sizeof(char) * (len + 1));
if (!s2)
return (NULL);
return (ft_strcpy(s2, s1));
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *newstr;
int s1_len;
int s2_len;
int i;
if (s1)
s1_len = ft_strlen(s1);
else
s1_len = 0;
if (s2)
s2_len = ft_strlen(s2);
else
s2_len = 0;
newstr = (char *)malloc(sizeof(char) * ((s1_len + s2_len) + 1));
if (!newstr)
return (NULL);
i = 0;
while (s1_len-- > 0)
newstr[i++] = *s1++;
while (s2_len-- > 0)
newstr[i++] = *s2++;
newstr[i] = '\0';
return (newstr);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
size_t i;
char *substr;
size_t s_len;
i = 0;
s_len = 0;
if (!s)
return (NULL);
while (s[s_len])
s_len++;
if (start >= s_len)
{
start = 0;
len = 0;
}
substr = (char *)malloc(sizeof(char) * (len + 1));
if (!substr)
return (NULL);
while (i < len)
substr[i++] = s[start++];
substr[i] = '\0';
return (substr);
}