-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_strnstr.c
42 lines (39 loc) · 1.47 KB
/
ft_strnstr.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oadhesiv <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/18 12:30:31 by oadhesiv #+# #+# */
/* Updated: 2019/06/01 16:42:58 by oadhesiv ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
char *ptr;
char *tmp;
size_t needle_len;
if (!haystack || !needle || !*needle)
return ((char *)haystack);
needle_len = ft_strlen(needle);
ptr = (char *)haystack;
while (ptr && len > 0)
{
tmp = ft_strchr(ptr, *needle);
if ((t_ulong)(tmp - ptr) > len)
return ((void *)0);
len -= tmp - ptr;
ptr = tmp;
if (!ptr)
return ((void *)0);
if (len < needle_len)
return ((void *)0);
if (ft_strncmp(ptr, needle, ft_strlen(needle)) == 0)
return (ptr);
ptr++;
len--;
}
return ((void *)0);
}