Back to index

nagios-plugins  1.4.16
Defines | Functions
strstr.c File Reference
#include <config.h>
#include <string.h>
#include <stdbool.h>
#include "str-two-way.h"

Go to the source code of this file.


#define __builtin_expect(expr, val)   (expr)
#define RETURN_TYPE   char *
#define AVAILABLE(h, h_l, j, n_l)


char * strstr (const char *haystack_start, const char *needle_start)

Define Documentation

#define __builtin_expect (   expr,
)    (expr)

Definition at line 31 of file strstr.c.

#define AVAILABLE (   h,
(!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l))     \
   && ((h_l) = (j) + (n_l)))

Definition at line 35 of file strstr.c.

#define RETURN_TYPE   char *

Definition at line 34 of file strstr.c.

Function Documentation

char* strstr ( const char *  haystack_start,
const char *  needle_start 

Definition at line 44 of file strstr.c.

  const char *haystack = haystack_start;
  const char *needle = needle_start;
  size_t needle_len; /* Length of NEEDLE.  */
  size_t haystack_len; /* Known minimum length of HAYSTACK.  */
  bool ok = true; /* True if NEEDLE is prefix of HAYSTACK.  */

  /* Determine length of NEEDLE, and in the process, make sure
     HAYSTACK is at least as long (no point processing all of a long
     NEEDLE if HAYSTACK is too short).  */
  while (*haystack && *needle)
    ok &= *haystack++ == *needle++;
  if (*needle)
    return NULL;
  if (ok)
    return (char *) haystack_start;

  /* Reduce the size of haystack using strchr, since it has a smaller
     linear coefficient than the Two-Way algorithm.  */
  needle_len = needle - needle_start;
  haystack = strchr (haystack_start + 1, *needle_start);
  if (!haystack || __builtin_expect (needle_len == 1, 0))
    return (char *) haystack;
  needle -= needle_len;
  haystack_len = (haystack > haystack_start + needle_len ? 1
                  : needle_len + haystack_start - haystack);

  /* Perform the search.  Abstract memory is considered to be an array
     of 'unsigned char' values, not an array of 'char' values.  See
     ISO C 99 section  */
  if (needle_len < LONG_NEEDLE_THRESHOLD)
    return two_way_short_needle ((const unsigned char *) haystack,
                                 (const unsigned char *) needle, needle_len);
  return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
                              (const unsigned char *) needle, needle_len);

Here is the call graph for this function:

Here is the caller graph for this function: