-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_next_line_utils.c
101 lines (91 loc) · 2.23 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ytaya <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/17 15:10:57 by ytaya #+# #+# */
/* Updated: 2021/11/19 00:10:36 by ytaya ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i])
i++;
return (i);
}
char *ft_strchr(const char *s, int c)
{
while (*s && *s != (char)c)
s++;
if (*s == (char)c)
return ((char *)s);
return (0);
}
char *ft_strdup(const char *s1)
{
int len;
int i;
char *ret;
len = 0;
while (s1[len])
len++;
ret = malloc(sizeof(char) * (len + 1));
if (ret)
{
i = 0;
while (s1[i])
{
ret[i] = s1[i];
i++;
}
ret[i] = '\0';
}
return (ret);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *string;
int i;
i = 0;
if (!(s1) || !(s2))
return (NULL);
if (*s1 == '\0' && *s2 == '\0')
return (ft_strdup("\0"));
string = (char *)
malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (string)
{
while (*s1)
string[i++] = *(s1++);
while (*s2)
string[i++] = *(s2++);
string[i] = '\0';
}
return (string);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *dst;
size_t i;
if (!s)
return (NULL);
i = ft_strlen(s);
if (start >= i)
dst = (char *)malloc(sizeof(*dst));
else if (i - start < len)
dst = (char *)malloc(sizeof(*dst) * (i - start + 1));
else
dst = (char *)malloc(sizeof(*dst) * (len + 1));
if (!dst)
return (NULL);
i = 0;
while (s[start] && i < len && start < ft_strlen(s))
dst[i++] = s[start++];
dst[i] = '\0';
return (dst);
}