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 #define CANON_ELEMENT(c) TOLOWER (c)
39 /* Knuth-Morris-Pratt algorithm.
40 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
41 Return a boolean indicating success. */
43 knuth_morris_pratt_multibyte (const char *haystack, const char *needle,
46 size_t m = mbslen (needle);
47 mbchar_t *needle_mbchars;
50 /* Allocate room for needle_mbchars and the table. */
51 char *memory = (char *) nmalloca (m, sizeof (mbchar_t) + sizeof (size_t));
54 needle_mbchars = (mbchar_t *) memory;
55 table = (size_t *) (memory + m * sizeof (mbchar_t));
57 /* Fill needle_mbchars. */
63 for (mbui_init (iter, needle); mbui_avail (iter); mbui_advance (iter), j++)
65 mb_copy (&needle_mbchars[j], &mbui_cur (iter));
66 if (needle_mbchars[j].wc_valid)
67 needle_mbchars[j].wc = towlower (needle_mbchars[j].wc);
73 0 < table[i] <= i is defined such that
74 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
75 and table[i] is as large as possible with this property.
79 needle[table[i]..i-1] = needle[0..i-1-table[i]].
81 rhaystack[0..i-1] == needle[0..i-1]
82 and exists h, i <= h < m: rhaystack[h] != needle[h]
84 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
85 table[0] remains uninitialized. */
89 /* i = 1: Nothing to verify for x = 0. */
93 for (i = 2; i < m; i++)
95 /* Here: j = i-1 - table[i-1].
96 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
97 for x < table[i-1], by induction.
98 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
99 mbchar_t *b = &needle_mbchars[i - 1];
103 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
104 is known to hold for x < i-1-j.
105 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
106 if (mb_equal (*b, needle_mbchars[j]))
108 /* Set table[i] := i-1-j. */
112 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
113 for x = i-1-j, because
114 needle[i-1] != needle[j] = needle[i-1-x]. */
117 /* The inequality holds for all possible x. */
121 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
122 for i-1-j < x < i-1-j+table[j], because for these x:
124 = needle[x-(i-1-j)..j-1]
125 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
127 hence needle[x..i-1] != needle[0..i-1-x].
129 needle[i-1-j+table[j]..i-2]
130 = needle[table[j]..j-1]
131 = needle[0..j-1-table[j]] (by definition of table[j]). */
134 /* Here: j = i - table[i]. */
138 /* Search, using the table to accelerate the processing. */
141 mbui_iterator_t rhaystack;
142 mbui_iterator_t phaystack;
146 mbui_init (rhaystack, haystack);
147 mbui_init (phaystack, haystack);
148 /* Invariant: phaystack = rhaystack + j. */
149 while (mbui_avail (phaystack))
153 mb_copy (&c, &mbui_cur (phaystack));
155 c.wc = towlower (c.wc);
156 if (mb_equal (needle_mbchars[j], c))
159 mbui_advance (phaystack);
162 /* The entire needle has been found. */
163 *resultp = mbui_cur_ptr (rhaystack);
169 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
170 size_t count = table[j];
172 for (; count > 0; count--)
174 if (!mbui_avail (rhaystack))
176 mbui_advance (rhaystack);
181 /* Found a mismatch at needle[0] already. */
182 if (!mbui_avail (rhaystack))
184 mbui_advance (rhaystack);
185 mbui_advance (phaystack);
195 /* Find the first occurrence of the character string NEEDLE in the character
196 string HAYSTACK, using case-insensitive comparison.
197 Note: This function may, in multibyte locales, return success even if
198 strlen (haystack) < strlen (needle) ! */
200 mbscasestr (const char *haystack, const char *needle)
202 /* Be careful not to look at the entire extent of haystack or needle
203 until needed. This is useful because of these two cases:
204 - haystack may be very long, and a match of needle found early,
205 - needle may be very long, and not even a short initial segment of
206 needle may be found in haystack. */
210 mbui_iterator_t iter_needle;
212 mbui_init (iter_needle, needle);
213 if (mbui_avail (iter_needle))
215 /* Minimizing the worst-case complexity:
216 Let n = mbslen(haystack), m = mbslen(needle).
217 The naïve algorithm is O(n*m) worst-case.
218 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
220 To achieve linear complexity and yet amortize the cost of the
221 memory allocation, we activate the Knuth-Morris-Pratt algorithm
222 only once the naïve algorithm has already run for some time; more
224 - the outer loop count is >= 10,
225 - the average number of comparisons per outer loop is >= 5,
226 - the total number of comparisons is >= m.
227 But we try it only once. If the memory allocation attempt failed,
228 we don't retry it. */
230 size_t outer_loop_count = 0;
231 size_t comparison_count = 0;
232 size_t last_ccount = 0; /* last comparison count */
233 mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */
236 mbui_iterator_t iter_haystack;
238 mbui_init (iter_needle_last_ccount, needle);
240 mb_copy (&b, &mbui_cur (iter_needle));
242 b.wc = towlower (b.wc);
244 mbui_init (iter_haystack, haystack);
245 for (;; mbui_advance (iter_haystack))
249 if (!mbui_avail (iter_haystack))
253 /* See whether it's advisable to use an asymptotically faster
256 && outer_loop_count >= 10
257 && comparison_count >= 5 * outer_loop_count)
259 /* See if needle + comparison_count now reaches the end of
261 size_t count = comparison_count - last_ccount;
263 count > 0 && mbui_avail (iter_needle_last_ccount);
265 mbui_advance (iter_needle_last_ccount);
266 last_ccount = comparison_count;
267 if (!mbui_avail (iter_needle_last_ccount))
269 /* Try the Knuth-Morris-Pratt algorithm. */
272 knuth_morris_pratt_multibyte (haystack, needle,
275 return (char *) result;
282 mb_copy (&c, &mbui_cur (iter_haystack));
284 c.wc = towlower (c.wc);
286 /* The first character matches. */
288 mbui_iterator_t rhaystack;
289 mbui_iterator_t rneedle;
291 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
292 mbui_advance (rhaystack);
294 mbui_init (rneedle, needle);
295 if (!mbui_avail (rneedle))
297 mbui_advance (rneedle);
299 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
301 if (!mbui_avail (rneedle))
303 return (char *) mbui_cur_ptr (iter_haystack);
304 if (!mbui_avail (rhaystack))
308 if (!mb_caseequal (mbui_cur (rhaystack),
310 /* Nothing in this round. */
317 return (char *) haystack;
324 /* Minimizing the worst-case complexity:
325 Let n = strlen(haystack), m = strlen(needle).
326 The naïve algorithm is O(n*m) worst-case.
327 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
329 To achieve linear complexity and yet amortize the cost of the
330 memory allocation, we activate the Knuth-Morris-Pratt algorithm
331 only once the naïve algorithm has already run for some time; more
333 - the outer loop count is >= 10,
334 - the average number of comparisons per outer loop is >= 5,
335 - the total number of comparisons is >= m.
336 But we try it only once. If the memory allocation attempt failed,
337 we don't retry it. */
339 size_t outer_loop_count = 0;
340 size_t comparison_count = 0;
341 size_t last_ccount = 0; /* last comparison count */
342 const char *needle_last_ccount = needle; /* = needle + last_ccount */
344 /* Speed up the following searches of needle by caching its first
346 unsigned char b = TOLOWER ((unsigned char) *needle);
351 if (*haystack == '\0')
355 /* See whether it's advisable to use an asymptotically faster
358 && outer_loop_count >= 10
359 && comparison_count >= 5 * outer_loop_count)
361 /* See if needle + comparison_count now reaches the end of
363 if (needle_last_ccount != NULL)
365 needle_last_ccount +=
366 strnlen (needle_last_ccount,
367 comparison_count - last_ccount);
368 if (*needle_last_ccount == '\0')
369 needle_last_ccount = NULL;
370 last_ccount = comparison_count;
372 if (needle_last_ccount == NULL)
374 /* Try the Knuth-Morris-Pratt algorithm. */
377 knuth_morris_pratt_unibyte (haystack, needle - 1,
380 return (char *) result;
387 if (TOLOWER ((unsigned char) *haystack) == b)
388 /* The first character matches. */
390 const char *rhaystack = haystack + 1;
391 const char *rneedle = needle;
393 for (;; rhaystack++, rneedle++)
395 if (*rneedle == '\0')
397 return (char *) haystack;
398 if (*rhaystack == '\0')
402 if (TOLOWER ((unsigned char) *rhaystack)
403 != TOLOWER ((unsigned char) *rneedle))
404 /* Nothing in this round. */
411 return (char *) haystack;