GIT 1.5.4-rc5
[git/git-p4.git] / compat / memmem.c
blobcd0d8773641f2fdc808d8b246a8dd2bcd0e5814d
1 #include "../git-compat-util.h"
3 void *gitmemmem(const void *haystack, size_t haystack_len,
4 const void *needle, size_t needle_len)
6 const char *begin = haystack;
7 const char *last_possible = begin + haystack_len - needle_len;
9 /*
10 * The first occurrence of the empty string is deemed to occur at
11 * the beginning of the string.
13 if (needle_len == 0)
14 return (void *)begin;
17 * Sanity check, otherwise the loop might search through the whole
18 * memory.
20 if (haystack_len < needle_len)
21 return NULL;
23 for (; begin <= last_possible; begin++) {
24 if (!memcmp(begin, needle, needle_len))
25 return (void *)begin;
28 return NULL;