Blame string/strcasestr.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 6c4009
/*
Packit 6c4009
 * My personal strstr() implementation that beats most other algorithms.
Packit 6c4009
 * Until someone tells me otherwise, I assume that this is the
Packit 6c4009
 * fastest implementation of strstr() in C.
Packit 6c4009
 * I deliberately chose not to comment it.  You should have at least
Packit 6c4009
 * as much fun trying to understand it, as I had to write it :-).
Packit 6c4009
 *
Packit 6c4009
 * Stephen R. van den Berg, berg@pool.informatik.rwth-aachen.de	*/
Packit 6c4009
Packit 6c4009
/* Specification.  */
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
#include <ctype.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <strings.h>
Packit 6c4009
Packit 6c4009
#define TOLOWER(Ch) tolower (Ch)
Packit 6c4009
Packit 6c4009
/* Two-Way algorithm.  */
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 6c4009
#define CHECK_EOL (1)
Packit 6c4009
#define RET0_IF_0(a) if (!a) goto ret0
Packit 6c4009
#define CANON_ELEMENT(c) TOLOWER (c)
Packit 6c4009
#define CMP_FUNC(p1, p2, l)				\
Packit 6c4009
  __strncasecmp ((const char *) (p1), (const char *) (p2), l)
Packit 6c4009
#include "str-two-way.h"
Packit 6c4009
Packit 6c4009
#undef strcasestr
Packit 6c4009
#undef __strcasestr
Packit 6c4009
Packit 6c4009
#ifndef STRCASESTR
Packit 6c4009
#define STRCASESTR __strcasestr
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Find the first occurrence of NEEDLE in HAYSTACK, using
Packit 6c4009
   case-insensitive comparison.  This function gives unspecified
Packit 6c4009
   results in multibyte locales.  */
Packit 6c4009
char *
Packit Service caf619
STRCASESTR (const char *haystack, const char *needle)
Packit 6c4009
{
Packit 6c4009
  size_t needle_len; /* Length of NEEDLE.  */
Packit 6c4009
  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
  /* 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 6c4009
Packit 6c4009
  /* Perform the search.  Abstract memory is considered to be an array
Packit 6c4009
     of 'unsigned char' values, not an array of 'char' values.  See
Packit 6c4009
     ISO C 99 section 6.2.6.1.  */
Packit 6c4009
  if (needle_len < LONG_NEEDLE_THRESHOLD)
Packit 6c4009
    return two_way_short_needle ((const unsigned char *) haystack,
Packit 6c4009
				 haystack_len,
Packit Service caf619
				 (const unsigned char *) needle,
Packit 6c4009
				 needle_len);
Packit 6c4009
  return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
Packit Service caf619
			      (const unsigned char *) needle,
Packit 6c4009
			      needle_len);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#undef LONG_NEEDLE_THRESHOLD
Packit 6c4009
Packit 6c4009
#ifndef NO_ALIAS
Packit 6c4009
weak_alias (__strcasestr, strcasestr)
Packit 6c4009
#endif