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 UNIT unsigned char
34 #define CANON_ELEMENT(c) TOLOWER (c)
37 /* Knuth-Morris-Pratt algorithm.
38 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
39 Return a boolean indicating success:
40 Return true and set *RESULTP if the search was completed.
41 Return false if it was aborted because not enough memory was available. */
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);
194 /* Find the first occurrence of the character string NEEDLE in the character
195 string HAYSTACK, using case-insensitive comparison.
196 Note: This function may, in multibyte locales, return success even if
197 strlen (haystack) < strlen (needle) ! */
199 mbscasestr (const char *haystack, const char *needle)
201 /* Be careful not to look at the entire extent of haystack or needle
202 until needed. This is useful because of these two cases:
203 - haystack may be very long, and a match of needle found early,
204 - needle may be very long, and not even a short initial segment of
205 needle may be found in haystack. */
208 mbui_iterator_t iter_needle;
210 mbui_init (iter_needle, needle);
211 if (mbui_avail (iter_needle))
213 /* Minimizing the worst-case complexity:
214 Let n = mbslen(haystack), m = mbslen(needle).
215 The naïve algorithm is O(n*m) worst-case.
216 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
218 To achieve linear complexity and yet amortize the cost of the
219 memory allocation, we activate the Knuth-Morris-Pratt algorithm
220 only once the naïve algorithm has already run for some time; more
222 - the outer loop count is >= 10,
223 - the average number of comparisons per outer loop is >= 5,
224 - the total number of comparisons is >= m.
225 But we try it only once. If the memory allocation attempt failed,
226 we don't retry it. */
228 size_t outer_loop_count = 0;
229 size_t comparison_count = 0;
230 size_t last_ccount = 0; /* last comparison count */
231 mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */
234 mbui_iterator_t iter_haystack;
236 mbui_init (iter_needle_last_ccount, needle);
238 mb_copy (&b, &mbui_cur (iter_needle));
240 b.wc = towlower (b.wc);
242 mbui_init (iter_haystack, haystack);
243 for (;; mbui_advance (iter_haystack))
247 if (!mbui_avail (iter_haystack))
251 /* See whether it's advisable to use an asymptotically faster
254 && outer_loop_count >= 10
255 && comparison_count >= 5 * outer_loop_count)
257 /* See if needle + comparison_count now reaches the end of
259 size_t count = comparison_count - last_ccount;
261 count > 0 && mbui_avail (iter_needle_last_ccount);
263 mbui_advance (iter_needle_last_ccount);
264 last_ccount = comparison_count;
265 if (!mbui_avail (iter_needle_last_ccount))
267 /* Try the Knuth-Morris-Pratt algorithm. */
270 knuth_morris_pratt_multibyte (haystack, needle,
273 return (char *) result;
280 mb_copy (&c, &mbui_cur (iter_haystack));
282 c.wc = towlower (c.wc);
284 /* The first character matches. */
286 mbui_iterator_t rhaystack;
287 mbui_iterator_t rneedle;
289 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
290 mbui_advance (rhaystack);
292 mbui_init (rneedle, needle);
293 if (!mbui_avail (rneedle))
295 mbui_advance (rneedle);
297 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
299 if (!mbui_avail (rneedle))
301 return (char *) mbui_cur_ptr (iter_haystack);
302 if (!mbui_avail (rhaystack))
306 if (!mb_caseequal (mbui_cur (rhaystack),
308 /* Nothing in this round. */
315 return (char *) haystack;
321 /* Minimizing the worst-case complexity:
322 Let n = strlen(haystack), m = strlen(needle).
323 The naïve algorithm is O(n*m) worst-case.
324 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
326 To achieve linear complexity and yet amortize the cost of the
327 memory allocation, we activate the Knuth-Morris-Pratt algorithm
328 only once the naïve algorithm has already run for some time; more
330 - the outer loop count is >= 10,
331 - the average number of comparisons per outer loop is >= 5,
332 - the total number of comparisons is >= m.
333 But we try it only once. If the memory allocation attempt failed,
334 we don't retry it. */
336 size_t outer_loop_count = 0;
337 size_t comparison_count = 0;
338 size_t last_ccount = 0; /* last comparison count */
339 const char *needle_last_ccount = needle; /* = needle + last_ccount */
341 /* Speed up the following searches of needle by caching its first
343 unsigned char b = TOLOWER ((unsigned char) *needle);
348 if (*haystack == '\0')
352 /* See whether it's advisable to use an asymptotically faster
355 && outer_loop_count >= 10
356 && comparison_count >= 5 * outer_loop_count)
358 /* See if needle + comparison_count now reaches the end of
360 if (needle_last_ccount != NULL)
362 needle_last_ccount +=
363 strnlen (needle_last_ccount,
364 comparison_count - last_ccount);
365 if (*needle_last_ccount == '\0')
366 needle_last_ccount = NULL;
367 last_ccount = comparison_count;
369 if (needle_last_ccount == NULL)
371 /* Try the Knuth-Morris-Pratt algorithm. */
372 const unsigned char *result;
374 knuth_morris_pratt ((const unsigned char *) haystack,
375 (const unsigned char *) (needle - 1),
379 return (char *) result;
386 if (TOLOWER ((unsigned char) *haystack) == b)
387 /* The first character matches. */
389 const char *rhaystack = haystack + 1;
390 const char *rneedle = needle;
392 for (;; rhaystack++, rneedle++)
394 if (*rneedle == '\0')
396 return (char *) haystack;
397 if (*rhaystack == '\0')
401 if (TOLOWER ((unsigned char) *rhaystack)
402 != TOLOWER ((unsigned char) *rneedle))
403 /* Nothing in this round. */
410 return (char *) haystack;