Subversion Repositories Kolibri OS

Rev

Rev 4874 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2. FUNCTION
  3.         <<strcasestr>>---case-insensitive character string search
  4.  
  5. INDEX
  6.         strcasestr
  7.  
  8. ANSI_SYNOPSIS
  9.         #include <string.h>
  10.         char *strcasestr(const char *<[s]>, const char *<[find]>);
  11.  
  12. TRAD_SYNOPSIS
  13.         #include <string.h>
  14.         int strcasecmp(<[s]>, <[find]>)
  15.         char *<[s]>;
  16.         char *<[find]>;
  17.  
  18. DESCRIPTION
  19.         <<strcasestr>> searchs the string <[s]> for
  20.         the first occurrence of the sequence <[find]>.  <<strcasestr>>
  21.         is identical to <<strstr>> except the search is
  22.         case-insensitive.
  23.  
  24. RETURNS
  25.  
  26.         A pointer to the first case-insensitive occurrence of the sequence
  27.         <[find]> or <<NULL>> if no match was found.
  28.  
  29. PORTABILITY
  30. <<strcasestr>> is in the Berkeley Software Distribution.
  31.  
  32. <<strcasestr>> requires no supporting OS subroutines. It uses
  33. tolower() from elsewhere in this library.
  34.  
  35. QUICKREF
  36.         strcasestr
  37. */
  38.  
  39. /*-
  40.  * Copyright (c) 1990, 1993
  41.  *      The Regents of the University of California.  All rights reserved.
  42.  *
  43.  * The quadratic code is derived from software contributed to Berkeley by
  44.  * Chris Torek.
  45.  *
  46.  * Redistribution and use in source and binary forms, with or without
  47.  * modification, are permitted provided that the following conditions
  48.  * are met:
  49.  * 1. Redistributions of source code must retain the above copyright
  50.  *    notice, this list of conditions and the following disclaimer.
  51.  * 2. Redistributions in binary form must reproduce the above copyright
  52.  *    notice, this list of conditions and the following disclaimer in the
  53.  *    documentation and/or other materials provided with the distribution.
  54.  * 4. Neither the name of the University nor the names of its contributors
  55.  *    may be used to endorse or promote products derived from this software
  56.  *    without specific prior written permission.
  57.  *
  58.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  59.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  61.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  62.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  63.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  64.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  65.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  66.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  67.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  68.  * SUCH DAMAGE.
  69.  */
  70. /* Linear algorithm Copyright (C) 2008 Eric Blake
  71.  * Permission to use, copy, modify, and distribute the linear portion of
  72.  * software is freely granted, provided that this notice is preserved.
  73.  */
  74.  
  75. #include <sys/cdefs.h>
  76.  
  77. #include <ctype.h>
  78. #include <string.h>
  79. #include <strings.h>
  80.  
  81. #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
  82. # define RETURN_TYPE char *
  83. # define AVAILABLE(h, h_l, j, n_l)                      \
  84.   (!memchr ((h) + (h_l), '\0', (j) + (n_l) - (h_l))     \
  85.    && ((h_l) = (j) + (n_l)))
  86. # define CANON_ELEMENT(c) tolower (c)
  87. #if __GNUC_PREREQ (4, 2)
  88. /* strncasecmp uses signed char, CMP_FUNC is expected to use unsigned char. */
  89. #pragma GCC diagnostic ignored "-Wpointer-sign"
  90. #endif
  91. # define CMP_FUNC strncasecmp
  92. # include "str-two-way.h"
  93. #endif
  94.  
  95. /*
  96.  * Find the first occurrence of find in s, ignore case.
  97.  */
  98. char *
  99. strcasestr(s, find)
  100.         const char *s, *find;
  101. {
  102. #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
  103.  
  104.   /* Less code size, but quadratic performance in the worst case.  */
  105.         char c, sc;
  106.         size_t len;
  107.  
  108.         if ((c = *find++) != 0) {
  109.                 c = tolower((unsigned char)c);
  110.                 len = strlen(find);
  111.                 do {
  112.                         do {
  113.                                 if ((sc = *s++) == 0)
  114.                                         return (NULL);
  115.                         } while ((char)tolower((unsigned char)sc) != c);
  116.                 } while (strncasecmp(s, find, len) != 0);
  117.                 s--;
  118.         }
  119.         return ((char *)s);
  120.  
  121. #else /* compilation for speed */
  122.  
  123.   /* Larger code size, but guaranteed linear performance.  */
  124.   const char *haystack = s;
  125.   const char *needle = find;
  126.   size_t needle_len; /* Length of NEEDLE.  */
  127.   size_t haystack_len; /* Known minimum length of HAYSTACK.  */
  128.   int ok = 1; /* True if NEEDLE is prefix of HAYSTACK.  */
  129.  
  130.   /* Determine length of NEEDLE, and in the process, make sure
  131.      HAYSTACK is at least as long (no point processing all of a long
  132.      NEEDLE if HAYSTACK is too short).  */
  133.   while (*haystack && *needle)
  134.     ok &= (tolower ((unsigned char) *haystack++)
  135.            == tolower ((unsigned char) *needle++));
  136.   if (*needle)
  137.     return NULL;
  138.   if (ok)
  139.     return (char *) s;
  140.   needle_len = needle - find;
  141.   haystack = s + 1;
  142.   haystack_len = needle_len - 1;
  143.  
  144.   /* Perform the search.  */
  145.   if (needle_len < LONG_NEEDLE_THRESHOLD)
  146.     return two_way_short_needle ((const unsigned char *) haystack,
  147.                                  haystack_len,
  148.                                  (const unsigned char *) find, needle_len);
  149.   return two_way_long_needle ((const unsigned char *) haystack, haystack_len,
  150.                               (const unsigned char *) find, needle_len);
  151. #endif /* compilation for speed */
  152. }
  153.