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 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 */
32 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
34 /* Knuth-Morris-Pratt algorithm.
35 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
36 Return a boolean indicating success. */
39 knuth_morris_pratt_unibyte (const char *haystack, const char *needle,
42 size_t m = strlen (needle);
44 /* Allocate the table. */
45 size_t *table = (size_t *) nmalloca (m, sizeof (size_t));
50 0 < table[i] <= i is defined such that
51 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
52 and table[i] is as large as possible with this property.
56 needle[table[i]..i-1] = needle[0..i-1-table[i]].
58 rhaystack[0..i-1] == needle[0..i-1]
59 and exists h, i <= h < m: rhaystack[h] != needle[h]
61 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
62 table[0] remains uninitialized. */
66 /* i = 1: Nothing to verify for x = 0. */
70 for (i = 2; i < m; i++)
72 /* Here: j = i-1 - table[i-1].
73 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
74 for x < table[i-1], by induction.
75 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
76 unsigned char b = TOLOWER ((unsigned char) needle[i - 1]);
80 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
81 is known to hold for x < i-1-j.
82 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
83 if (b == TOLOWER ((unsigned char) needle[j]))
85 /* Set table[i] := i-1-j. */
89 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
90 for x = i-1-j, because
91 needle[i-1] != needle[j] = needle[i-1-x]. */
94 /* The inequality holds for all possible x. */
98 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
99 for i-1-j < x < i-1-j+table[j], because for these x:
101 = needle[x-(i-1-j)..j-1]
102 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
104 hence needle[x..i-1] != needle[0..i-1-x].
106 needle[i-1-j+table[j]..i-2]
107 = needle[table[j]..j-1]
108 = needle[0..j-1-table[j]] (by definition of table[j]). */
111 /* Here: j = i - table[i]. */
115 /* Search, using the table to accelerate the processing. */
118 const char *rhaystack;
119 const char *phaystack;
123 rhaystack = haystack;
124 phaystack = haystack;
125 /* Invariant: phaystack = rhaystack + j. */
126 while (*phaystack != '\0')
127 if (TOLOWER ((unsigned char) needle[j])
128 == TOLOWER ((unsigned char) *phaystack))
134 /* The entire needle has been found. */
135 *resultp = rhaystack;
141 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
142 rhaystack += table[j];
147 /* Found a mismatch at needle[0] already. */
159 knuth_morris_pratt_multibyte (const char *haystack, const char *needle,
160 const char **resultp)
162 size_t m = mbslen (needle);
163 mbchar_t *needle_mbchars;
166 /* Allocate room for needle_mbchars and the table. */
167 char *memory = (char *) nmalloca (m, sizeof (mbchar_t) + sizeof (size_t));
170 needle_mbchars = (mbchar_t *) memory;
171 table = (size_t *) (memory + m * sizeof (mbchar_t));
173 /* Fill needle_mbchars. */
175 mbui_iterator_t iter;
179 for (mbui_init (iter, needle); mbui_avail (iter); mbui_advance (iter), j++)
181 mb_copy (&needle_mbchars[j], &mbui_cur (iter));
182 if (needle_mbchars[j].wc_valid)
183 needle_mbchars[j].wc = towlower (needle_mbchars[j].wc);
189 0 < table[i] <= i is defined such that
190 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
191 and table[i] is as large as possible with this property.
195 needle[table[i]..i-1] = needle[0..i-1-table[i]].
197 rhaystack[0..i-1] == needle[0..i-1]
198 and exists h, i <= h < m: rhaystack[h] != needle[h]
200 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
201 table[0] remains uninitialized. */
205 /* i = 1: Nothing to verify for x = 0. */
209 for (i = 2; i < m; i++)
211 /* Here: j = i-1 - table[i-1].
212 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
213 for x < table[i-1], by induction.
214 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
215 mbchar_t *b = &needle_mbchars[i - 1];
219 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
220 is known to hold for x < i-1-j.
221 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
222 if (mb_equal (*b, needle_mbchars[j]))
224 /* Set table[i] := i-1-j. */
228 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
229 for x = i-1-j, because
230 needle[i-1] != needle[j] = needle[i-1-x]. */
233 /* The inequality holds for all possible x. */
237 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
238 for i-1-j < x < i-1-j+table[j], because for these x:
240 = needle[x-(i-1-j)..j-1]
241 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
243 hence needle[x..i-1] != needle[0..i-1-x].
245 needle[i-1-j+table[j]..i-2]
246 = needle[table[j]..j-1]
247 = needle[0..j-1-table[j]] (by definition of table[j]). */
250 /* Here: j = i - table[i]. */
254 /* Search, using the table to accelerate the processing. */
257 mbui_iterator_t rhaystack;
258 mbui_iterator_t phaystack;
262 mbui_init (rhaystack, haystack);
263 mbui_init (phaystack, haystack);
264 /* Invariant: phaystack = rhaystack + j. */
265 while (mbui_avail (phaystack))
269 mb_copy (&c, &mbui_cur (phaystack));
271 c.wc = towlower (c.wc);
272 if (mb_equal (needle_mbchars[j], c))
275 mbui_advance (phaystack);
278 /* The entire needle has been found. */
279 *resultp = mbui_cur_ptr (rhaystack);
285 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
286 size_t count = table[j];
288 for (; count > 0; count--)
290 if (!mbui_avail (rhaystack))
292 mbui_advance (rhaystack);
297 /* Found a mismatch at needle[0] already. */
298 if (!mbui_avail (rhaystack))
300 mbui_advance (rhaystack);
301 mbui_advance (phaystack);
311 /* Find the first occurrence of the character string NEEDLE in the character
312 string HAYSTACK, using case-insensitive comparison.
313 Note: This function may, in multibyte locales, return success even if
314 strlen (haystack) < strlen (needle) ! */
316 mbscasestr (const char *haystack, const char *needle)
318 /* Be careful not to look at the entire extent of haystack or needle
319 until needed. This is useful because of these two cases:
320 - haystack may be very long, and a match of needle found early,
321 - needle may be very long, and not even a short initial segment of
322 needle may be found in haystack. */
326 mbui_iterator_t iter_needle;
328 mbui_init (iter_needle, needle);
329 if (mbui_avail (iter_needle))
331 /* Minimizing the worst-case complexity:
332 Let n = mbslen(haystack), m = mbslen(needle).
333 The naïve algorithm is O(n*m) worst-case.
334 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
336 To achieve linear complexity and yet amortize the cost of the
337 memory allocation, we activate the Knuth-Morris-Pratt algorithm
338 only once the naïve algorithm has already run for some time; more
340 - the outer loop count is >= 10,
341 - the average number of comparisons per outer loop is >= 5,
342 - the total number of comparisons is >= m.
343 But we try it only once. If the memory allocation attempt failed,
344 we don't retry it. */
346 size_t outer_loop_count = 0;
347 size_t comparison_count = 0;
348 size_t last_ccount = 0; /* last comparison count */
349 mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */
352 mbui_iterator_t iter_haystack;
354 mbui_init (iter_needle_last_ccount, needle);
356 mb_copy (&b, &mbui_cur (iter_needle));
358 b.wc = towlower (b.wc);
360 mbui_init (iter_haystack, haystack);
361 for (;; mbui_advance (iter_haystack))
365 if (!mbui_avail (iter_haystack))
369 /* See whether it's advisable to use an asymptotically faster
372 && outer_loop_count >= 10
373 && comparison_count >= 5 * outer_loop_count)
375 /* See if needle + comparison_count now reaches the end of
377 size_t count = comparison_count - last_ccount;
379 count > 0 && mbui_avail (iter_needle_last_ccount);
381 mbui_advance (iter_needle_last_ccount);
382 last_ccount = comparison_count;
383 if (!mbui_avail (iter_needle_last_ccount))
385 /* Try the Knuth-Morris-Pratt algorithm. */
388 knuth_morris_pratt_multibyte (haystack, needle,
391 return (char *) result;
398 mb_copy (&c, &mbui_cur (iter_haystack));
400 c.wc = towlower (c.wc);
402 /* The first character matches. */
404 mbui_iterator_t rhaystack;
405 mbui_iterator_t rneedle;
407 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
408 mbui_advance (rhaystack);
410 mbui_init (rneedle, needle);
411 if (!mbui_avail (rneedle))
413 mbui_advance (rneedle);
415 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
417 if (!mbui_avail (rneedle))
419 return (char *) mbui_cur_ptr (iter_haystack);
420 if (!mbui_avail (rhaystack))
424 if (!mb_caseequal (mbui_cur (rhaystack),
426 /* Nothing in this round. */
433 return (char *) haystack;
440 /* Minimizing the worst-case complexity:
441 Let n = strlen(haystack), m = strlen(needle).
442 The naïve algorithm is O(n*m) worst-case.
443 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
445 To achieve linear complexity and yet amortize the cost of the
446 memory allocation, we activate the Knuth-Morris-Pratt algorithm
447 only once the naïve algorithm has already run for some time; more
449 - the outer loop count is >= 10,
450 - the average number of comparisons per outer loop is >= 5,
451 - the total number of comparisons is >= m.
452 But we try it only once. If the memory allocation attempt failed,
453 we don't retry it. */
455 size_t outer_loop_count = 0;
456 size_t comparison_count = 0;
457 size_t last_ccount = 0; /* last comparison count */
458 const char *needle_last_ccount = needle; /* = needle + last_ccount */
460 /* Speed up the following searches of needle by caching its first
462 unsigned char b = TOLOWER ((unsigned char) *needle);
467 if (*haystack == '\0')
471 /* See whether it's advisable to use an asymptotically faster
474 && outer_loop_count >= 10
475 && comparison_count >= 5 * outer_loop_count)
477 /* See if needle + comparison_count now reaches the end of
479 if (needle_last_ccount != NULL)
481 needle_last_ccount +=
482 strnlen (needle_last_ccount,
483 comparison_count - last_ccount);
484 if (*needle_last_ccount == '\0')
485 needle_last_ccount = NULL;
486 last_ccount = comparison_count;
488 if (needle_last_ccount == NULL)
490 /* Try the Knuth-Morris-Pratt algorithm. */
493 knuth_morris_pratt_unibyte (haystack, needle - 1,
496 return (char *) result;
503 if (TOLOWER ((unsigned char) *haystack) == b)
504 /* The first character matches. */
506 const char *rhaystack = haystack + 1;
507 const char *rneedle = needle;
509 for (;; rhaystack++, rneedle++)
511 if (*rneedle == '\0')
513 return (char *) haystack;
514 if (*rhaystack == '\0')
518 if (TOLOWER ((unsigned char) *rhaystack)
519 != TOLOWER ((unsigned char) *rneedle))
520 /* Nothing in this round. */
527 return (char *) haystack;