1 /* Case-insensitive searching in a string.
2 Copyright (C) 2005-2011 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/>. */
25 #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 #define CANON_ELEMENT(c) TOLOWER (c)
36 /* Knuth-Morris-Pratt algorithm.
37 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
38 Return a boolean indicating success:
39 Return true and set *RESULTP if the search was completed.
40 Return false if it was aborted because not enough memory was available. */
42 knuth_morris_pratt_multibyte (const char *haystack, const char *needle,
45 size_t m = mbslen (needle);
46 mbchar_t *needle_mbchars;
49 /* Allocate room for needle_mbchars and the table. */
50 char *memory = (char *) nmalloca (m, sizeof (mbchar_t) + sizeof (size_t));
53 needle_mbchars = (mbchar_t *) memory;
54 table = (size_t *) (memory + m * sizeof (mbchar_t));
56 /* Fill needle_mbchars. */
62 for (mbui_init (iter, needle); mbui_avail (iter); mbui_advance (iter), j++)
64 mb_copy (&needle_mbchars[j], &mbui_cur (iter));
65 if (needle_mbchars[j].wc_valid)
66 needle_mbchars[j].wc = towlower (needle_mbchars[j].wc);
72 0 < table[i] <= i is defined such that
73 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
74 and table[i] is as large as possible with this property.
78 needle[table[i]..i-1] = needle[0..i-1-table[i]].
80 rhaystack[0..i-1] == needle[0..i-1]
81 and exists h, i <= h < m: rhaystack[h] != needle[h]
83 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
84 table[0] remains uninitialized. */
88 /* i = 1: Nothing to verify for x = 0. */
92 for (i = 2; i < m; i++)
94 /* Here: j = i-1 - table[i-1].
95 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
96 for x < table[i-1], by induction.
97 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
98 mbchar_t *b = &needle_mbchars[i - 1];
102 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
103 is known to hold for x < i-1-j.
104 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
105 if (mb_equal (*b, needle_mbchars[j]))
107 /* Set table[i] := i-1-j. */
111 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
112 for x = i-1-j, because
113 needle[i-1] != needle[j] = needle[i-1-x]. */
116 /* The inequality holds for all possible x. */
120 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
121 for i-1-j < x < i-1-j+table[j], because for these x:
123 = needle[x-(i-1-j)..j-1]
124 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
126 hence needle[x..i-1] != needle[0..i-1-x].
128 needle[i-1-j+table[j]..i-2]
129 = needle[table[j]..j-1]
130 = needle[0..j-1-table[j]] (by definition of table[j]). */
133 /* Here: j = i - table[i]. */
137 /* Search, using the table to accelerate the processing. */
140 mbui_iterator_t rhaystack;
141 mbui_iterator_t phaystack;
145 mbui_init (rhaystack, haystack);
146 mbui_init (phaystack, haystack);
147 /* Invariant: phaystack = rhaystack + j. */
148 while (mbui_avail (phaystack))
152 mb_copy (&c, &mbui_cur (phaystack));
154 c.wc = towlower (c.wc);
155 if (mb_equal (needle_mbchars[j], c))
158 mbui_advance (phaystack);
161 /* The entire needle has been found. */
162 *resultp = mbui_cur_ptr (rhaystack);
168 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
169 size_t count = table[j];
171 for (; count > 0; count--)
173 if (!mbui_avail (rhaystack))
175 mbui_advance (rhaystack);
180 /* Found a mismatch at needle[0] already. */
181 if (!mbui_avail (rhaystack))
183 mbui_advance (rhaystack);
184 mbui_advance (phaystack);
193 /* Find the first occurrence of the character string NEEDLE in the character
194 string HAYSTACK, using case-insensitive comparison.
195 Note: This function may, in multibyte locales, return success even if
196 strlen (haystack) < strlen (needle) ! */
198 mbscasestr (const char *haystack, const char *needle)
200 /* Be careful not to look at the entire extent of haystack or needle
201 until needed. This is useful because of these two cases:
202 - haystack may be very long, and a match of needle found early,
203 - needle may be very long, and not even a short initial segment of
204 needle may be found in haystack. */
207 mbui_iterator_t iter_needle;
209 mbui_init (iter_needle, needle);
210 if (mbui_avail (iter_needle))
212 /* Minimizing the worst-case complexity:
213 Let n = mbslen(haystack), m = mbslen(needle).
214 The naïve algorithm is O(n*m) worst-case.
215 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
217 To achieve linear complexity and yet amortize the cost of the
218 memory allocation, we activate the Knuth-Morris-Pratt algorithm
219 only once the naïve algorithm has already run for some time; more
221 - the outer loop count is >= 10,
222 - the average number of comparisons per outer loop is >= 5,
223 - the total number of comparisons is >= m.
224 But we try it only once. If the memory allocation attempt failed,
225 we don't retry it. */
227 size_t outer_loop_count = 0;
228 size_t comparison_count = 0;
229 size_t last_ccount = 0; /* last comparison count */
230 mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */
233 mbui_iterator_t iter_haystack;
235 mbui_init (iter_needle_last_ccount, needle);
237 mb_copy (&b, &mbui_cur (iter_needle));
239 b.wc = towlower (b.wc);
241 mbui_init (iter_haystack, haystack);
242 for (;; mbui_advance (iter_haystack))
246 if (!mbui_avail (iter_haystack))
250 /* See whether it's advisable to use an asymptotically faster
253 && outer_loop_count >= 10
254 && comparison_count >= 5 * outer_loop_count)
256 /* See if needle + comparison_count now reaches the end of
258 size_t count = comparison_count - last_ccount;
260 count > 0 && mbui_avail (iter_needle_last_ccount);
262 mbui_advance (iter_needle_last_ccount);
263 last_ccount = comparison_count;
264 if (!mbui_avail (iter_needle_last_ccount))
266 /* Try the Knuth-Morris-Pratt algorithm. */
269 knuth_morris_pratt_multibyte (haystack, needle,
272 return (char *) result;
279 mb_copy (&c, &mbui_cur (iter_haystack));
281 c.wc = towlower (c.wc);
283 /* The first character matches. */
285 mbui_iterator_t rhaystack;
286 mbui_iterator_t rneedle;
288 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
289 mbui_advance (rhaystack);
291 mbui_init (rneedle, needle);
292 if (!mbui_avail (rneedle))
294 mbui_advance (rneedle);
296 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
298 if (!mbui_avail (rneedle))
300 return (char *) mbui_cur_ptr (iter_haystack);
301 if (!mbui_avail (rhaystack))
305 if (!mb_caseequal (mbui_cur (rhaystack),
307 /* Nothing in this round. */
314 return (char *) haystack;
320 /* Minimizing the worst-case complexity:
321 Let n = strlen(haystack), m = strlen(needle).
322 The naïve algorithm is O(n*m) worst-case.
323 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
325 To achieve linear complexity and yet amortize the cost of the
326 memory allocation, we activate the Knuth-Morris-Pratt algorithm
327 only once the naïve algorithm has already run for some time; more
329 - the outer loop count is >= 10,
330 - the average number of comparisons per outer loop is >= 5,
331 - the total number of comparisons is >= m.
332 But we try it only once. If the memory allocation attempt failed,
333 we don't retry it. */
335 size_t outer_loop_count = 0;
336 size_t comparison_count = 0;
337 size_t last_ccount = 0; /* last comparison count */
338 const char *needle_last_ccount = needle; /* = needle + last_ccount */
340 /* Speed up the following searches of needle by caching its first
342 unsigned char b = TOLOWER ((unsigned char) *needle);
347 if (*haystack == '\0')
351 /* See whether it's advisable to use an asymptotically faster
354 && outer_loop_count >= 10
355 && comparison_count >= 5 * outer_loop_count)
357 /* See if needle + comparison_count now reaches the end of
359 if (needle_last_ccount != NULL)
361 needle_last_ccount +=
362 strnlen (needle_last_ccount,
363 comparison_count - last_ccount);
364 if (*needle_last_ccount == '\0')
365 needle_last_ccount = NULL;
366 last_ccount = comparison_count;
368 if (needle_last_ccount == NULL)
370 /* Try the Knuth-Morris-Pratt algorithm. */
373 knuth_morris_pratt_unibyte (haystack, needle - 1,
376 return (char *) result;
383 if (TOLOWER ((unsigned char) *haystack) == b)
384 /* The first character matches. */
386 const char *rhaystack = haystack + 1;
387 const char *rneedle = needle;
389 for (;; rhaystack++, rneedle++)
391 if (*rneedle == '\0')
393 return (char *) haystack;
394 if (*rhaystack == '\0')
398 if (TOLOWER ((unsigned char) *rhaystack)
399 != TOLOWER ((unsigned char) *rneedle))
400 /* Nothing in this round. */
407 return (char *) haystack;