Blame string/strstr.c

Packit 6c4009
/* Return the offset of one string within another.
Packit 6c4009
   Copyright (C) 1994-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit Service 6de65a
/* This particular implementation was written by Eric Blake, 2008.  */
Packit Service 6de65a
Packit 6c4009
#ifndef _LIBC
Packit 6c4009
# include <config.h>
Packit 6c4009
#endif
Packit 6c4009
Packit Service 6de65a
/* Specification of strstr.  */
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit Service 6de65a
#include <stdbool.h>
Packit Service 6de65a
Packit Service 6de65a
#ifndef _LIBC
Packit Service 6de65a
# define __builtin_expect(expr, val)   (expr)
Packit Service 6de65a
#endif
Packit Service 6de65a
Packit 6c4009
#define RETURN_TYPE char *
Packit 6c4009
#define AVAILABLE(h, h_l, j, n_l)			\
Packit Service 0dba75
  (((j) + (n_l) <= (h_l)) \
Packit Service 0dba75
   || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
Packit Service 0dba75
       (j) + (n_l) <= (h_l)))
Packit Service 6de65a
#define CHECK_EOL (1)
Packit Service 6de65a
#define RET0_IF_0(a) if (!a) goto ret0
Packit Service 6de65a
#define FASTSEARCH(S,C,N) (void*) strchr ((void*)(S), (C))
Packit 6c4009
#include "str-two-way.h"
Packit 6c4009
Packit 6c4009
#undef strstr
Packit 6c4009
Packit 6c4009
#ifndef STRSTR
Packit 6c4009
#define STRSTR strstr
Packit 6c4009
#endif
Packit 6c4009
Packit Service 6de65a
/* Return the first occurrence of NEEDLE in HAYSTACK.  Return HAYSTACK
Packit Service 6de65a
   if NEEDLE is empty, otherwise NULL if NEEDLE is not found in
Packit Service 6de65a
   HAYSTACK.  */
Packit Service f3a811
char *
Packit Service caf619
STRSTR (const char *haystack, const char *needle)
Packit Service f3a811
{
Packit Service 6de65a
  size_t needle_len; /* Length of NEEDLE.  */
Packit Service 6de65a
  size_t haystack_len; /* Known minimum length of HAYSTACK.  */
Packit Service caf619
Packit Service caf619
  /* Handle empty NEEDLE special case.  */
Packit Service caf619
  if (needle[0] == '\0')
Packit Service caf619
    return (char *) haystack;
Packit Service caf619
Packit Service caf619
  /* Skip until we find the first matching char from NEEDLE.  */
Packit Service caf619
  haystack = strchr (haystack, needle[0]);
Packit Service caf619
  if (haystack == NULL || needle[1] == '\0')
Packit Service caf619
    return (char *) haystack;
Packit Service caf619
Packit Service caf619
  /* Ensure HAYSTACK length is at least as long as NEEDLE length.
Packit Service caf619
     Since a match may occur early on in a huge HAYSTACK, use strnlen
Packit Service caf619
     and read ahead a few cachelines for improved performance.  */
Packit Service caf619
  needle_len = strlen (needle);
Packit Service caf619
  haystack_len = __strnlen (haystack, needle_len + 256);
Packit Service caf619
  if (haystack_len < needle_len)
Packit 6c4009
    return NULL;
Packit Service caf619
Packit Service caf619
  /* Check whether we have a match.  This improves performance since we avoid
Packit Service caf619
     the initialization overhead of the two-way algorithm.  */
Packit Service caf619
  if (memcmp (haystack, needle, needle_len) == 0)
Packit Service 6de65a
    return (char *) haystack;
Packit Service 6de65a
Packit Service 6de65a
  /* Perform the search.  Abstract memory is considered to be an array
Packit Service 6de65a
     of 'unsigned char' values, not an array of 'char' values.  See
Packit Service 6de65a
     ISO C 99 section 6.2.6.1.  */
Packit Service 6de65a
  if (needle_len < LONG_NEEDLE_THRESHOLD)
Packit Service 6de65a
    return two_way_short_needle ((const unsigned char *) haystack,
Packit Service 6de65a
				 haystack_len,
Packit Service 6de65a
				 (const unsigned char *) needle, needle_len);
Packit Service 6de65a
  return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
Packit Service 6de65a
			      (const unsigned char *) needle, needle_len);
Packit 6c4009
}
Packit 6c4009
libc_hidden_builtin_def (strstr)
Packit Service 6de65a
Packit Service 6de65a
#undef LONG_NEEDLE_THRESHOLD