-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
43 lines (40 loc) · 1.69 KB
/
ft_substr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: flverge <flverge@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/02 15:26:41 by flverge #+# #+# */
/* Updated: 2024/09/22 17:15:13 by flverge ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* @brief Allocates and returns a substring from the string 's'.
*
* The substring begins at index 'start' and is of maximum size 'len'.
*
* @param s The string from which to create the substring.
* @param start The start index of the substring in the string 's'.
* @param len The maximum length of the substring.
* @return The substring. NULL if the allocation fails or if 's' is NULL.
*/
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *buffer;
size_t len_s;
if (!s)
return (NULL);
len_s = ft_strlen(s);
if (start > len_s)
return (ft_calloc(1, sizeof(char)));
if (len > ft_strlen(s + start))
len = ft_strlen(s + start);
buffer = ft_calloc(len + 1, sizeof(char));
if (!buffer)
return (NULL);
ft_memcpy(buffer, &s[start], len);
buffer[len] = '\0';
return (buffer);
}