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 6c4009
  (((j) + (n_l) <= (h_l)) || ((h_l) += __strnlen ((void*)((h) + (h_l)), 512), \
Packit 6c4009
			      (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 6c4009
STRCASESTR (const char *haystack_start, const char *needle_start)
Packit 6c4009
{
Packit 6c4009
  const char *haystack = haystack_start;
Packit 6c4009
  const char *needle = needle_start;
Packit 6c4009
  size_t needle_len; /* Length of NEEDLE.  */
Packit 6c4009
  size_t haystack_len; /* Known minimum length of HAYSTACK.  */
Packit 6c4009
  bool ok = true; /* True if NEEDLE is prefix of HAYSTACK.  */
Packit 6c4009
Packit 6c4009
  /* Determine length of NEEDLE, and in the process, make sure
Packit 6c4009
     HAYSTACK is at least as long (no point processing all of a long
Packit 6c4009
     NEEDLE if HAYSTACK is too short).  */
Packit 6c4009
  while (*haystack && *needle)
Packit 6c4009
    {
Packit 6c4009
      ok &= (TOLOWER ((unsigned char) *haystack)
Packit 6c4009
	     == TOLOWER ((unsigned char) *needle));
Packit 6c4009
      haystack++;
Packit 6c4009
      needle++;
Packit 6c4009
    }
Packit 6c4009
  if (*needle)
Packit 6c4009
    return NULL;
Packit 6c4009
  if (ok)
Packit 6c4009
    return (char *) haystack_start;
Packit 6c4009
  needle_len = needle - needle_start;
Packit 6c4009
  haystack = haystack_start + 1;
Packit 6c4009
  haystack_len = needle_len - 1;
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 6c4009
				 (const unsigned char *) needle_start,
Packit 6c4009
				 needle_len);
Packit 6c4009
  return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
Packit 6c4009
			      (const unsigned char *) needle_start,
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