-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_next_line_utils.c
145 lines (129 loc) · 3.71 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_next_line_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cvidon <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/15 18:00:00 by cvidon #+# #+# */
/* Updated: 2022/08/15 18:00:00 by cvidon ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Unmodified from Libft.
*/
#include "get_next_line.h"
/*
** @brief Locate a character in string.
**
** "The strchr() function locates the first occurrence of c (converted to a
** char) in the string pointed to by s. The terminating null character is
** considered to be part of the string; therefore if c is ‘\0’, the functions
** locate the terminating ‘\0’."
**
** @see STRCHR(3) <string.h>
*/
char *ft_strchr(char const *s, int c)
{
while (*s && *s != (unsigned char)c)
++s;
if (*s == (unsigned char)c)
return ((char *)(unsigned long)s);
else
return (0);
}
/*
** @brief Find length of string.
**
** "The strlen() function computes the length of the string s. The strnlen()
** function attempts to compute the length of s, but never scans beyond the **
** first maxlen bytes of s."
**
** @see STRLEN(3) <string.h>
*/
size_t ft_strlen(char const *str)
{
char const *ptr;
ptr = str;
while (*ptr)
++ptr;
return ((size_t)(ptr - str));
}
/*
** @brief Save a copy of a string.
**
** "The strdup() function allocates sufficient memory for a copy of the string
** s1, does the copy, and returns a pointer to it. The pointer may
** subsequently be used as an argument to the function free(3)."
**
** @see STRDUP(3) <string.h>
*/
char *ft_strdup(char const *str)
{
char *dup;
char *ptr;
dup = malloc (sizeof (*dup) * (ft_strlen(str) + 1));
if (!dup)
return (NULL);
ptr = dup;
while (*str)
*ptr++ = *str++;
*ptr = 0;
return (dup);
}
/*
** @brief Concatenate two strings into a new string (with malloc).
**
** @param[in] s1 the first string (will be free).
** @param[in] s2 the second string.
** @return A string made of s1 + s2 or NULL if malloc fail.
*/
char *ft_strjoin_free_s1(char *s1, char const *s2)
{
char *s3;
char *p3;
char *p1;
s3 = malloc (sizeof (*s3) * (ft_strlen (s1) + ft_strlen (s2) + 1));
if (!s3)
{
free (s1);
return (NULL);
}
p3 = s3;
p1 = s1;
while (*p1)
*p3++ = *p1++;
while (*s2)
*p3++ = *s2++;
*p3 = 0;
free (s1);
return (s3);
}
/*
** @brief Extract substring from string.
**
** "Allocates (with malloc(3)) and returns a substring from the string s.
** The substring begins at index start and is of maximum size len."
**
** @param[in] str the string that contain the cherished substring.
** @param[in] start the beginning of the substring.
** @param[in] size the length of the substring.
** @return The cherished substring or NULL if malloc fail.
*/
char *ft_substr(const char *str, unsigned int start, size_t size)
{
size_t len;
char *sub;
len = ft_strlen (str);
if (start >= len)
return (ft_strdup(""));
if (len - start < size)
size = len - start;
sub = malloc (sizeof (*sub) * (size + 1));
if (!sub)
return (NULL);
sub[size] = 0;
while (size--)
sub[size] = str[start + size];
return (sub);
}