-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_next_line_utils.c
89 lines (79 loc) · 2.01 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gandrade <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/13 15:44:29 by gandrade #+# #+# */
/* Updated: 2021/06/29 21:05:58 by gandrade ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
size_t ft_strlen(const char *s)
{
size_t len;
len = 0;
while (s[len])
len++;
return (len);
}
char *ft_strchr(const char *s, int c)
{
while (*s)
{
if (*s == (char)c)
return ((char *)s);
s++;
}
if ((char)c == 0)
return ((char *)s);
return (NULL);
}
char *ft_strdup(const char *s1)
{
char *dup;
char *tmp;
dup = malloc((ft_strlen(s1) + 1) * sizeof(char));
if (!dup)
return (NULL);
tmp = dup;
while (*s1)
*tmp++ = *s1++;
*tmp = '\0';
return (dup);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *joined;
char *tmp;
joined = malloc((ft_strlen(s1) + ft_strlen(s2) + 1) * sizeof(char));
if (!joined)
return (NULL);
tmp = joined;
while (*s1)
*tmp++ = *s1++;
while (*s2)
*tmp++ = *s2++;
*tmp = '\0';
return (joined);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *sub;
char *tmp;
size_t size;
if (ft_strlen(s) < start)
return (ft_strdup(""));
size = ft_strlen(&s[start]);
if (size < len)
len = size;
sub = malloc((len + 1) * sizeof(char));
if (!sub)
return (NULL);
tmp = sub;
while (len--)
*tmp++ = s[start++];
*tmp = '\0';
return (sub);
}