home
search
help
profile
liking the experience? our app is even better
registration for
employee referral programs
are now open
get referred to google, amazon, flipkart and more
register now  
Search
showing results for
1void *memmem(const void *haystack_start, size_t haystack_len, const void *needle_start, size_t needle_len)
2{
3
4    const unsigned char *haystack = (const unsigned char *) haystack_start;
5    const unsigned char *needle = (const unsigned char *) needle_start;
6    const unsigned char *h = NULL;
7    const unsigned char *n = NULL;
8    size_t x = needle_len;
9
10    /* The first occurrence of the empty string is deemed to occur at
11    the beginning of the string.  */
12    if (needle_len == 0)
13        return (void *) haystack_start;
14
15    /* Sanity check, otherwise the loop might search through the whole
16        memory.  */
17     if (haystack_len < needle_len)
18       return NULL;
19
20    for (; *haystack && haystack_len--; haystack++) {
21
22        x = needle_len;
23        n = needle;
24        h = haystack;
25
26        if (haystack_len < needle_len)
27            break;
28
29        if ((*haystack != *needle) || ( *haystack + needle_len != *needle + needle_len))
30            continue;
31
32        for (; x ; h++ , n++) {
33            x--;
34
35            if (*h != *n) 
36                break;
37
38           if (x == 0)
39            return (void *)haystack;
40        }
41    }
42
43    return NULL;
44}
upvote
downvote
source
queries leading to this page