Blame lib/strstr.c

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