1  /* Case-insensitive searching in a string.
       2     Copyright (C) 2005-2023 Free Software Foundation, Inc.
       3     Written by Bruno Haible <bruno@clisp.org>, 2005.
       4  
       5     This file is free software: you can redistribute it and/or modify
       6     it under the terms of the GNU Lesser General Public License as
       7     published by the Free Software Foundation; either version 2.1 of the
       8     License, or (at your option) any later version.
       9  
      10     This file is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13     GNU Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public License
      16     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <config.h>
      19  
      20  /* Specification.  */
      21  #include <string.h>
      22  
      23  #include <ctype.h>
      24  #include <strings.h>
      25  
      26  /* Two-Way algorithm.  */
      27  #define RETURN_TYPE char *
      28  #define AVAILABLE(h, h_l, j, n_l)                       \
      29    (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l))     \
      30     && ((h_l) = (j) + (n_l)))
      31  #define CANON_ELEMENT(c) tolower (c)
      32  #define CMP_FUNC(p1, p2, l)                             \
      33    strncasecmp ((const char *) (p1), (const char *) (p2), l)
      34  #include "str-two-way.h"
      35  
      36  /* Find the first occurrence of NEEDLE in HAYSTACK, using
      37     case-insensitive comparison.  This function gives unspecified
      38     results in multibyte locales.  */
      39  char *
      40  strcasestr (const char *haystack_start, const char *needle_start)
      41  {
      42    const char *haystack = haystack_start;
      43    const char *needle = needle_start;
      44    size_t needle_len; /* Length of NEEDLE.  */
      45    size_t haystack_len; /* Known minimum length of HAYSTACK.  */
      46    bool ok = true; /* True if NEEDLE is prefix of HAYSTACK.  */
      47  
      48    /* Determine length of NEEDLE, and in the process, make sure
      49       HAYSTACK is at least as long (no point processing all of a long
      50       NEEDLE if HAYSTACK is too short).  */
      51    while (*haystack && *needle)
      52      {
      53        ok &= (tolower ((unsigned char) *haystack)
      54               == tolower ((unsigned char) *needle));
      55        haystack++;
      56        needle++;
      57      }
      58    if (*needle)
      59      return NULL;
      60    if (ok)
      61      return (char *) haystack_start;
      62    needle_len = needle - needle_start;
      63    haystack = haystack_start + 1;
      64    haystack_len = needle_len - 1;
      65  
      66    /* Perform the search.  Abstract memory is considered to be an array
      67       of 'unsigned char' values, not an array of 'char' values.  See
      68       ISO C 99 section 6.2.6.1.  */
      69    if (needle_len < LONG_NEEDLE_THRESHOLD)
      70      return two_way_short_needle ((const unsigned char *) haystack,
      71                                   haystack_len,
      72                                   (const unsigned char *) needle_start,
      73                                   needle_len);
      74    return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
      75                                (const unsigned char *) needle_start,
      76                                needle_len);
      77  }
      78  
      79  #undef LONG_NEEDLE_THRESHOLD