Blame lib/strstr.c

Packit 709fb3
/* Copyright (C) 1991-1994, 1996-1998, 2000, 2004, 2007-2017 Free Software
Packit 709fb3
   Foundation, Inc.
Packit 709fb3
   This file is part of the GNU C Library.
Packit 709fb3
Packit 709fb3
   This program is free software; you can redistribute it and/or modify
Packit 709fb3
   it under the terms of the GNU General Public License as published by
Packit 709fb3
   the Free Software Foundation; either version 3, or (at your option)
Packit 709fb3
   any later version.
Packit 709fb3
Packit 709fb3
   This program is distributed in the hope that it will be useful,
Packit 709fb3
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 709fb3
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 709fb3
   GNU General Public License for more details.
Packit 709fb3
Packit 709fb3
   You should have received a copy of the GNU General Public License along
Packit 709fb3
   with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 709fb3
Packit 709fb3
/* This particular implementation was written by Eric Blake, 2008.  */
Packit 709fb3
Packit 709fb3
#ifndef _LIBC
Packit 709fb3
# include <config.h>
Packit 709fb3
#endif
Packit 709fb3
Packit 709fb3
/* Specification of strstr.  */
Packit 709fb3
#include <string.h>
Packit 709fb3
Packit 709fb3
#include <stdbool.h>
Packit 709fb3
Packit 709fb3
#define RETURN_TYPE char *
Packit 709fb3
#define AVAILABLE(h, h_l, j, n_l)                       \
Packit 709fb3
  (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l))     \
Packit 709fb3
   && ((h_l) = (j) + (n_l)))
Packit 709fb3
#include "str-two-way.h"
Packit 709fb3
Packit 709fb3
/* Return the first occurrence of NEEDLE in HAYSTACK.  Return HAYSTACK
Packit 709fb3
   if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
Packit 709fb3
   HAYSTACK.  */
Packit 709fb3
char *
Packit 709fb3
strstr (const char *haystack_start, const char *needle_start)
Packit 709fb3
{
Packit 709fb3
  const char *haystack = haystack_start;
Packit 709fb3
  const char *needle = needle_start;
Packit 709fb3
  size_t needle_len; /* Length of NEEDLE.  */
Packit 709fb3
  size_t haystack_len; /* Known minimum length of HAYSTACK.  */
Packit 709fb3
  bool ok = true; /* True if NEEDLE is prefix of HAYSTACK.  */
Packit 709fb3
Packit 709fb3
  /* Determine length of NEEDLE, and in the process, make sure
Packit 709fb3
     HAYSTACK is at least as long (no point processing all of a long
Packit 709fb3
     NEEDLE if HAYSTACK is too short).  */
Packit 709fb3
  while (*haystack && *needle)
Packit 709fb3
    ok &= *haystack++ == *needle++;
Packit 709fb3
  if (*needle)
Packit 709fb3
    return NULL;
Packit 709fb3
  if (ok)
Packit 709fb3
    return (char *) haystack_start;
Packit 709fb3
Packit 709fb3
  /* Reduce the size of haystack using strchr, since it has a smaller
Packit 709fb3
     linear coefficient than the Two-Way algorithm.  */
Packit 709fb3
  needle_len = needle - needle_start;
Packit 709fb3
  haystack = strchr (haystack_start + 1, *needle_start);
Packit 709fb3
  if (!haystack || __builtin_expect (needle_len == 1, 0))
Packit 709fb3
    return (char *) haystack;
Packit 709fb3
  needle -= needle_len;
Packit 709fb3
  haystack_len = (haystack > haystack_start + needle_len ? 1
Packit 709fb3
                  : needle_len + haystack_start - haystack);
Packit 709fb3
Packit 709fb3
  /* Perform the search.  Abstract memory is considered to be an array
Packit 709fb3
     of 'unsigned char' values, not an array of 'char' values.  See
Packit 709fb3
     ISO C 99 section 6.2.6.1.  */
Packit 709fb3
  if (needle_len < LONG_NEEDLE_THRESHOLD)
Packit 709fb3
    return two_way_short_needle ((const unsigned char *) haystack,
Packit 709fb3
                                 haystack_len,
Packit 709fb3
                                 (const unsigned char *) needle, needle_len);
Packit 709fb3
  return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
Packit 709fb3
                              (const unsigned char *) needle, needle_len);
Packit 709fb3
}
Packit 709fb3
Packit 709fb3
#undef LONG_NEEDLE_THRESHOLD