Back to index

glibc  2.9
Defines | Functions
memmem.c File Reference
#include <config.h>
#include <string.h>
#include "str-two-way.h"

Go to the source code of this file.


#define __builtin_expect(expr, val)   (expr)
#define RETURN_TYPE   void *
#define AVAILABLE(h, h_l, j, n_l)   ((j) <= (h_l) - (n_l))


void * memmem (const void *haystack_start, size_t haystack_len, const void *needle_start, size_t needle_len)

Define Documentation

#define __builtin_expect (   expr,
)    (expr)

Definition at line 29 of file memmem.c.

#define AVAILABLE (   h,
)    ((j) <= (h_l) - (n_l))

Definition at line 33 of file memmem.c.

#define RETURN_TYPE   void *

Definition at line 32 of file memmem.c.

Function Documentation

void* memmem ( const void *  haystack_start,
size_t  haystack_len,
const void *  needle_start,
size_t  needle_len 

Definition at line 42 of file memmem.c.

  /* Abstract memory is considered to be an array of 'unsigned char' values,
     not an array of 'char' values.  See ISO C 99 section  */
  const unsigned char *haystack = (const unsigned char *) haystack_start;
  const unsigned char *needle = (const unsigned char *) needle_start;

  if (needle_len == 0)
    /* The first occurrence of the empty string is deemed to occur at
       the beginning of the string.  */
    return (void *) haystack;

  /* Sanity check, otherwise the loop might search through the whole
     memory.  */
  if (__builtin_expect (haystack_len < needle_len, 0))
    return NULL;

  /* Use optimizations in memchr when possible, to reduce the search
     size of haystack using a linear algorithm with a smaller
     coefficient.  However, avoid memchr for long needles, since we
     can often achieve sublinear performance.  */
  if (needle_len < LONG_NEEDLE_THRESHOLD)
      haystack = memchr (haystack, *needle, haystack_len);
      if (!haystack || __builtin_expect (needle_len == 1, 0))
       return (void *) haystack;
      haystack_len -= haystack - (const unsigned char *) haystack_start;
      if (haystack_len < needle_len)
       return NULL;
      return two_way_short_needle (haystack, haystack_len, needle, needle_len);
    return two_way_long_needle (haystack, haystack_len, needle, needle_len);

Here is the call graph for this function:

Here is the caller graph for this function: