1 /* c-strcasestr.c -- case insensitive substring search in C locale
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 3 of the License, or
8 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
21 #include "c-strcasestr.h"
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 *) nmalloca (m, sizeof (size_t));
45 0 < table[i] <= i is defined such that
46 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
47 and table[i] is as large as possible with this property.
51 needle[table[i]..i-1] = needle[0..i-1-table[i]].
53 rhaystack[0..i-1] == needle[0..i-1]
54 and exists h, i <= h < m: rhaystack[h] != needle[h]
56 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
57 table[0] remains uninitialized. */
61 /* i = 1: Nothing to verify for x = 0. */
65 for (i = 2; i < m; i++)
67 /* Here: j = i-1 - table[i-1].
68 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
69 for x < table[i-1], by induction.
70 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
71 unsigned char b = c_tolower ((unsigned char) needle[i - 1]);
75 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
76 is known to hold for x < i-1-j.
77 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
78 if (b == c_tolower ((unsigned char) needle[j]))
80 /* Set table[i] := i-1-j. */
84 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
85 for x = i-1-j, because
86 needle[i-1] != needle[j] = needle[i-1-x]. */
89 /* The inequality holds for all possible x. */
93 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
94 for i-1-j < x < i-1-j+table[j], because for these x:
96 = needle[x-(i-1-j)..j-1]
97 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
99 hence needle[x..i-1] != needle[0..i-1-x].
101 needle[i-1-j+table[j]..i-2]
102 = needle[table[j]..j-1]
103 = needle[0..j-1-table[j]] (by definition of table[j]). */
106 /* Here: j = i - table[i]. */
110 /* Search, using the table to accelerate the processing. */
113 const char *rhaystack;
114 const char *phaystack;
118 rhaystack = haystack;
119 phaystack = haystack;
120 /* Invariant: phaystack = rhaystack + j. */
121 while (*phaystack != '\0')
122 if (c_tolower ((unsigned char) needle[j])
123 == c_tolower ((unsigned char) *phaystack))
129 /* The entire needle has been found. */
130 *resultp = rhaystack;
136 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
137 rhaystack += table[j];
142 /* Found a mismatch at needle[0] already. */
152 /* Find the first occurrence of NEEDLE in HAYSTACK, using case-insensitive
154 Note: This function may, in multibyte locales, return success even if
155 strlen (haystack) < strlen (needle) ! */
157 c_strcasestr (const char *haystack, const char *needle)
159 /* Be careful not to look at the entire extent of haystack or needle
160 until needed. This is useful because of these two cases:
161 - haystack may be very long, and a match of needle found early,
162 - needle may be very long, and not even a short initial segment of
163 needle may be found in haystack. */
166 /* Minimizing the worst-case complexity:
167 Let n = strlen(haystack), m = strlen(needle).
168 The naïve algorithm is O(n*m) worst-case.
169 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
171 To achieve linear complexity and yet amortize the cost of the memory
172 allocation, we activate the Knuth-Morris-Pratt algorithm only once
173 the naïve algorithm has already run for some time; more precisely,
175 - the outer loop count is >= 10,
176 - the average number of comparisons per outer loop is >= 5,
177 - the total number of comparisons is >= m.
178 But we try it only once. If the memory allocation attempt failed,
179 we don't retry it. */
181 size_t outer_loop_count = 0;
182 size_t comparison_count = 0;
183 size_t last_ccount = 0; /* last comparison count */
184 const char *needle_last_ccount = needle; /* = needle + last_ccount */
186 /* Speed up the following searches of needle by caching its first
188 unsigned char b = c_tolower ((unsigned char) *needle);
193 if (*haystack == '\0')
197 /* See whether it's advisable to use an asymptotically faster
200 && outer_loop_count >= 10
201 && comparison_count >= 5 * outer_loop_count)
203 /* See if needle + comparison_count now reaches the end of
205 if (needle_last_ccount != NULL)
207 needle_last_ccount +=
208 strnlen (needle_last_ccount, comparison_count - last_ccount);
209 if (*needle_last_ccount == '\0')
210 needle_last_ccount = NULL;
211 last_ccount = comparison_count;
213 if (needle_last_ccount == NULL)
215 /* Try the Knuth-Morris-Pratt algorithm. */
218 knuth_morris_pratt (haystack, needle - 1, &result);
220 return (char *) result;
227 if (c_tolower ((unsigned char) *haystack) == b)
228 /* The first character matches. */
230 const char *rhaystack = haystack + 1;
231 const char *rneedle = needle;
233 for (;; rhaystack++, rneedle++)
235 if (*rneedle == '\0')
237 return (char *) haystack;
238 if (*rhaystack == '\0')
242 if (c_tolower ((unsigned char) *rhaystack)
243 != c_tolower ((unsigned char) *rneedle))
244 /* Nothing in this round. */
251 return (char *) haystack;