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 */
30 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
32 /* Knuth-Morris-Pratt algorithm.
33 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
34 Return a boolean indicating success. */
36 knuth_morris_pratt (const char *haystack, const char *needle,
39 size_t m = strlen (needle);
41 /* Allocate the table. */
42 size_t *table = (size_t *) malloca (m * sizeof (size_t));
47 0 < table[i] <= i is defined such that
48 rhaystack[0..i-1] == needle[0..i-1] and rhaystack[i] != needle[i]
50 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1],
51 and table[i] is as large as possible with this property.
52 table[0] remains uninitialized. */
58 for (i = 2; i < m; i++)
60 unsigned char b = TOLOWER ((unsigned char) needle[i - 1]);
64 if (b == TOLOWER ((unsigned char) needle[j]))
79 /* Search, using the table to accelerate the processing. */
82 const char *rhaystack;
83 const char *phaystack;
89 /* Invariant: phaystack = rhaystack + j. */
90 while (*phaystack != '\0')
91 if (TOLOWER ((unsigned char) needle[j])
92 == TOLOWER ((unsigned char) *phaystack))
98 /* The entire needle has been found. */
105 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
106 rhaystack += table[j];
111 /* Found a mismatch at needle[0] already. */
121 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
123 Note: This function may, in multibyte locales, return success even if
124 strlen (haystack) < strlen (needle) ! */
126 strcasestr (const char *haystack, const char *needle)
130 /* Minimizing the worst-case complexity:
131 Let n = strlen(haystack), m = strlen(needle).
132 The naïve algorithm is O(n*m) worst-case.
133 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
135 To achieve linear complexity and yet amortize the cost of the memory
136 allocation, we activate the Knuth-Morris-Pratt algorithm only once
137 the naïve algorithm has already run for some time; more precisely,
139 - the outer loop count is >= 10,
140 - the average number of comparisons per outer loop is >= 5,
141 - the total number of comparisons is >= m.
142 But we try it only once. If the memory allocation attempt failed,
143 we don't retry it. */
145 size_t outer_loop_count = 0;
146 size_t comparison_count = 0;
147 size_t last_ccount = 0; /* last comparison count */
148 const char *needle_last_ccount = needle; /* = needle + last_ccount */
150 /* Speed up the following searches of needle by caching its first
152 unsigned char b = TOLOWER ((unsigned char) *needle);
157 if (*haystack == '\0')
161 /* See whether it's advisable to use an asymptotically faster
164 && outer_loop_count >= 10
165 && comparison_count >= 5 * outer_loop_count)
167 /* See if needle + comparison_count now reaches the end of
169 if (needle_last_ccount != NULL)
171 needle_last_ccount +=
172 strnlen (needle_last_ccount, comparison_count - last_ccount);
173 if (*needle_last_ccount == '\0')
174 needle_last_ccount = NULL;
175 last_ccount = comparison_count;
177 if (needle_last_ccount == NULL)
179 /* Try the Knuth-Morris-Pratt algorithm. */
182 knuth_morris_pratt (haystack, needle - 1, &result);
184 return (char *) result;
191 if (TOLOWER ((unsigned char) *haystack) == b)
192 /* The first character matches. */
194 const char *rhaystack = haystack + 1;
195 const char *rneedle = needle;
197 for (;; rhaystack++, rneedle++)
199 if (*rneedle == '\0')
201 return (char *) haystack;
202 if (*rhaystack == '\0')
206 if (TOLOWER ((unsigned char) *rhaystack)
207 != TOLOWER ((unsigned char) *rneedle))
208 /* Nothing in this round. */
215 return (char *) haystack;