newlib-cygwin/newlib/libc/string/strnstr.c

21 lines
523 B
C
Raw Normal View History

2017-08-30 11:03:56 +08:00
#undef __STRICT_ANSI__
#include <_ansi.h>
2017-08-25 15:35:38 +08:00
#include <string.h>
/*
* Find the first occurrence of find in s, where the search is limited to the
* first slen characters of s.
*/
char *
2017-08-30 11:03:56 +08:00
strnstr(const char *haystack, const char *needle, size_t haystack_len)
2017-08-25 15:35:38 +08:00
{
2017-08-30 11:03:56 +08:00
size_t needle_len = strnlen(needle, haystack_len);
2017-08-25 15:35:38 +08:00
2017-08-30 11:03:56 +08:00
if (needle_len < haystack_len || !needle[needle_len]) {
char *x = memmem(haystack, haystack_len, needle, needle_len);
if (x && !memchr(haystack, 0, x - haystack))
return x;
}
return NULL;
2017-08-25 15:35:38 +08:00
}