-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
66 lines (60 loc) · 1.88 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: orezek <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/18 12:38:16 by orezek #+# #+# */
/* Updated: 2023/11/03 13:05:19 by orezek ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_start_is_bigger(char *ptr)
{
ptr = malloc(1 * sizeof(char));
if (ptr == NULL)
return (NULL);
*ptr = '\0';
return (ptr);
}
char *ft_start_is_smaller(char *ptr, unsigned int start,
size_t str_size, char const *s)
{
int i;
char *start_ptr;
i = 0;
ptr = malloc(((str_size - start) + 1) * sizeof(char));
if (ptr == NULL)
return (NULL);
start_ptr = ptr;
i = str_size - start;
s += start;
while (i-- > 0)
*ptr++ = *s++;
*ptr = '\0';
return (start_ptr);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *ptr;
char *start_ptr;
size_t s_len;
size_t str_size;
ptr = NULL;
s_len = len;
str_size = ft_strlen(s);
if (start > str_size)
return (ft_start_is_bigger(ptr));
if (start <= str_size && s_len >= str_size)
return (ft_start_is_smaller(ptr, start, str_size, s));
ptr = malloc((len + 1) * sizeof(char));
start_ptr = ptr;
if (ptr == NULL)
return (NULL);
s += (start);
while (len-- > 0)
*ptr++ = *s++;
*ptr = '\0';
return (start_ptr);
}