1 /* Case-insensitive searching in a string.
2 Copyright (C) 2005-2007 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2005.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 #include <stddef.h> /* for NULL, in case a nonstandard string.h lacks it */
28 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
30 /* Knuth-Morris-Pratt algorithm.
31 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
32 Return a boolean indicating success. */
34 knuth_morris_pratt (const char *haystack, const char *needle,
37 size_t m = strlen (needle);
39 /* Allocate the table. */
40 size_t *table = (size_t *) malloc (m * sizeof (size_t));
45 0 < table[i] <= i is defined such that
46 rhaystack[0..i-1] == needle[0..i-1] and rhaystack[i] != needle[i]
48 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1],
49 and table[i] is as large as possible with this property.
50 table[0] remains uninitialized. */
56 for (i = 2; i < m; i++)
58 unsigned char b = TOLOWER ((unsigned char) needle[i - 1]);
62 if (b == TOLOWER ((unsigned char) needle[j]))
77 /* Search, using the table to accelerate the processing. */
80 const char *rhaystack;
81 const char *phaystack;
87 /* Invariant: phaystack = rhaystack + j. */
88 while (*phaystack != '\0')
89 if (TOLOWER ((unsigned char) needle[j])
90 == TOLOWER ((unsigned char) *phaystack))
96 /* The entire needle has been found. */
103 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
104 rhaystack += table[j];
109 /* Found a mismatch at needle[0] already. */
119 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
121 Note: This function may, in multibyte locales, return success even if
122 strlen (haystack) < strlen (needle) ! */
124 strcasestr (const char *haystack, const char *needle)
128 /* Minimizing the worst-case complexity:
129 Let n = strlen(haystack), m = strlen(needle).
130 The naïve algorithm is O(n*m) worst-case.
131 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
133 To achieve linear complexity and yet amortize the cost of the memory
134 allocation, we activate the Knuth-Morris-Pratt algorithm only once
135 the naïve algorithm has already run for some time; more precisely,
137 - the outer loop count is >= 10,
138 - the average number of comparisons per outer loop is >= 5,
139 - the total number of comparisons is >= m.
140 But we try it only once. If the memory allocation attempt failed,
141 we don't retry it. */
143 size_t outer_loop_count = 0;
144 size_t comparison_count = 0;
145 size_t last_ccount = 0; /* last comparison count */
146 const char *needle_last_ccount = needle; /* = needle + last_ccount */
148 /* Speed up the following searches of needle by caching its first
150 unsigned char b = TOLOWER ((unsigned char) *needle);
155 if (*haystack == '\0')
159 /* See whether it's advisable to use an asymptotically faster
162 && outer_loop_count >= 10
163 && comparison_count >= 5 * outer_loop_count)
165 /* See if needle + comparison_count now reaches the end of
167 if (needle_last_ccount != NULL)
169 needle_last_ccount +=
170 strnlen (needle_last_ccount, comparison_count - last_ccount);
171 if (*needle_last_ccount == '\0')
172 needle_last_ccount = NULL;
173 last_ccount = comparison_count;
175 if (needle_last_ccount == NULL)
177 /* Try the Knuth-Morris-Pratt algorithm. */
180 knuth_morris_pratt (haystack, needle - 1, &result);
182 return (char *) result;
189 if (TOLOWER ((unsigned char) *haystack) == b)
190 /* The first character matches. */
192 const char *rhaystack = haystack + 1;
193 const char *rneedle = needle;
195 for (;; rhaystack++, rneedle++)
197 if (*rneedle == '\0')
199 return (char *) haystack;
200 if (*rhaystack == '\0')
204 if (TOLOWER ((unsigned char) *rhaystack)
205 != TOLOWER ((unsigned char) *rneedle))
206 /* Nothing in this round. */
213 return (char *) haystack;