3 <<strcasestr>>---case-insensitive character string search
10 char *strcasestr(const char *<[s]>, const char *<[find]>);
14 int strcasecmp(<[s]>, <[find]>)
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
26 A pointer to the first case-insensitive occurrence of the sequence
27 <[find]> or <<NULL>> if no match was found.
30 <<strcasestr>> is in the Berkeley Software Distribution.
32 <<strcasestr>> requires no supporting OS subroutines. It uses
33 tolower() from elsewhere in this library.
40 * Copyright (c) 1990, 1993
41 * The Regents of the University of California. All rights reserved.
43 * The quadratic code is derived from software contributed to Berkeley by
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
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.
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
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.
75 #include <sys/cdefs.h>
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"
91 # define CMP_FUNC strncasecmp
92 # include "str-two-way.h"
96 * Find the first occurrence of find in s, ignore case.
100 const char *s, *find;
102 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
104 /* Less code size, but quadratic performance in the worst case. */
108 if ((c = *find++) != 0) {
109 c = tolower((unsigned char)c);
113 if ((sc = *s++) == 0)
115 } while ((char)tolower((unsigned char)sc) != c);
116 } while (strncasecmp(s, find, len) != 0);
121 #else /* compilation for speed */
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. */
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++));
140 needle_len = needle - find;
142 haystack_len = needle_len - 1;
144 /* Perform the search. */
145 if (needle_len < LONG_NEEDLE_THRESHOLD)
146 return two_way_short_needle ((const unsigned char *) haystack,
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 */