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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 #include <stddef.h> /* for NULL, in case a nonstandard string.h lacks it */
33 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
35 /* Knuth-Morris-Pratt algorithm.
36 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
37 Return a boolean indicating success. */
40 knuth_morris_pratt_unibyte (const char *haystack, const char *needle,
43 size_t m = strlen (needle);
45 /* Allocate the table. */
46 size_t *table = (size_t *) allocsa (m * sizeof (size_t));
51 0 < table[i] <= i is defined such that
52 rhaystack[0..i-1] == needle[0..i-1] and rhaystack[i] != needle[i]
54 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1],
55 and table[i] is as large as possible with this property.
56 table[0] remains uninitialized. */
62 for (i = 2; i < m; i++)
64 unsigned char b = TOLOWER ((unsigned char) needle[i - 1]);
68 if (b == TOLOWER ((unsigned char) needle[j]))
83 /* Search, using the table to accelerate the processing. */
86 const char *rhaystack;
87 const char *phaystack;
93 /* Invariant: phaystack = rhaystack + j. */
94 while (*phaystack != '\0')
95 if (TOLOWER ((unsigned char) needle[j])
96 == TOLOWER ((unsigned char) *phaystack))
102 /* The entire needle has been found. */
103 *resultp = rhaystack;
109 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
110 rhaystack += table[j];
115 /* Found a mismatch at needle[0] already. */
127 knuth_morris_pratt_multibyte (const char *haystack, const char *needle,
128 const char **resultp)
130 size_t m = mbslen (needle);
131 mbchar_t *needle_mbchars;
134 /* Allocate room for needle_mbchars and the table. */
135 char *memory = (char *) allocsa (m * (sizeof (mbchar_t) + sizeof (size_t)));
138 needle_mbchars = (mbchar_t *) memory;
139 table = (size_t *) (memory + m * sizeof (mbchar_t));
141 /* Fill needle_mbchars. */
143 mbui_iterator_t iter;
147 for (mbui_init (iter, needle); mbui_avail (iter); mbui_advance (iter), j++)
149 mb_copy (&needle_mbchars[j], &mbui_cur (iter));
150 if (needle_mbchars[j].wc_valid)
151 needle_mbchars[j].wc = towlower (needle_mbchars[j].wc);
157 0 < table[i] <= i is defined such that
158 rhaystack[0..i-1] == needle[0..i-1] and rhaystack[i] != needle[i]
160 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1],
161 and table[i] is as large as possible with this property.
162 table[0] remains uninitialized. */
168 for (i = 2; i < m; i++)
170 mbchar_t *b = &needle_mbchars[i - 1];
174 if (mb_equal (*b, needle_mbchars[j]))
189 /* Search, using the table to accelerate the processing. */
192 mbui_iterator_t rhaystack;
193 mbui_iterator_t phaystack;
197 mbui_init (rhaystack, haystack);
198 mbui_init (phaystack, haystack);
199 /* Invariant: phaystack = rhaystack + j. */
200 while (mbui_avail (phaystack))
204 mb_copy (&c, &mbui_cur (phaystack));
206 c.wc = towlower (c.wc);
207 if (mb_equal (needle_mbchars[j], c))
210 mbui_advance (phaystack);
213 /* The entire needle has been found. */
214 *resultp = mbui_cur_ptr (rhaystack);
220 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
221 size_t count = table[j];
223 for (; count > 0; count--)
225 if (!mbui_avail (rhaystack))
227 mbui_advance (rhaystack);
232 /* Found a mismatch at needle[0] already. */
233 if (!mbui_avail (rhaystack))
235 mbui_advance (rhaystack);
236 mbui_advance (phaystack);
246 /* Find the first occurrence of the character string NEEDLE in the character
247 string HAYSTACK, using case-insensitive comparison.
248 Note: This function may, in multibyte locales, return success even if
249 strlen (haystack) < strlen (needle) ! */
251 mbscasestr (const char *haystack, const char *needle)
253 /* Be careful not to look at the entire extent of haystack or needle
254 until needed. This is useful because of these two cases:
255 - haystack may be very long, and a match of needle found early,
256 - needle may be very long, and not even a short initial segment of
257 needle may be found in haystack. */
261 mbui_iterator_t iter_needle;
263 mbui_init (iter_needle, needle);
264 if (mbui_avail (iter_needle))
266 /* Minimizing the worst-case complexity:
267 Let n = mbslen(haystack), m = mbslen(needle).
268 The naïve algorithm is O(n*m) worst-case.
269 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
271 To achieve linear complexity and yet amortize the cost of the
272 memory allocation, we activate the Knuth-Morris-Pratt algorithm
273 only once the naïve algorithm has already run for some time; more
275 - the outer loop count is >= 10,
276 - the average number of comparisons per outer loop is >= 5,
277 - the total number of comparisons is >= m.
278 But we try it only once. If the memory allocation attempt failed,
279 we don't retry it. */
281 size_t outer_loop_count = 0;
282 size_t comparison_count = 0;
283 size_t last_ccount = 0; /* last comparison count */
284 mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */
287 mbui_iterator_t iter_haystack;
289 mbui_init (iter_needle_last_ccount, needle);
291 mb_copy (&b, &mbui_cur (iter_needle));
293 b.wc = towlower (b.wc);
295 mbui_init (iter_haystack, haystack);
296 for (;; mbui_advance (iter_haystack))
300 if (!mbui_avail (iter_haystack))
304 /* See whether it's advisable to use an asymptotically faster
307 && outer_loop_count >= 10
308 && comparison_count >= 5 * outer_loop_count)
310 /* See if needle + comparison_count now reaches the end of
312 size_t count = comparison_count - last_ccount;
314 count > 0 && mbui_avail (iter_needle_last_ccount);
316 mbui_advance (iter_needle_last_ccount);
317 last_ccount = comparison_count;
318 if (!mbui_avail (iter_needle_last_ccount))
320 /* Try the Knuth-Morris-Pratt algorithm. */
323 knuth_morris_pratt_multibyte (haystack, needle,
326 return (char *) result;
333 mb_copy (&c, &mbui_cur (iter_haystack));
335 c.wc = towlower (c.wc);
337 /* The first character matches. */
339 mbui_iterator_t rhaystack;
340 mbui_iterator_t rneedle;
342 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
343 mbui_advance (rhaystack);
345 mbui_init (rneedle, needle);
346 if (!mbui_avail (rneedle))
348 mbui_advance (rneedle);
350 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
352 if (!mbui_avail (rneedle))
354 return (char *) mbui_cur_ptr (iter_haystack);
355 if (!mbui_avail (rhaystack))
359 if (!mb_caseequal (mbui_cur (rhaystack),
361 /* Nothing in this round. */
368 return (char *) haystack;
375 /* Minimizing the worst-case complexity:
376 Let n = strlen(haystack), m = strlen(needle).
377 The naïve algorithm is O(n*m) worst-case.
378 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
380 To achieve linear complexity and yet amortize the cost of the
381 memory allocation, we activate the Knuth-Morris-Pratt algorithm
382 only once the naïve algorithm has already run for some time; more
384 - the outer loop count is >= 10,
385 - the average number of comparisons per outer loop is >= 5,
386 - the total number of comparisons is >= m.
387 But we try it only once. If the memory allocation attempt failed,
388 we don't retry it. */
390 size_t outer_loop_count = 0;
391 size_t comparison_count = 0;
392 size_t last_ccount = 0; /* last comparison count */
393 const char *needle_last_ccount = needle; /* = needle + last_ccount */
395 /* Speed up the following searches of needle by caching its first
397 unsigned char b = TOLOWER ((unsigned char) *needle);
402 if (*haystack == '\0')
406 /* See whether it's advisable to use an asymptotically faster
409 && outer_loop_count >= 10
410 && comparison_count >= 5 * outer_loop_count)
412 /* See if needle + comparison_count now reaches the end of
414 if (needle_last_ccount != NULL)
416 needle_last_ccount +=
417 strnlen (needle_last_ccount,
418 comparison_count - last_ccount);
419 if (*needle_last_ccount == '\0')
420 needle_last_ccount = NULL;
421 last_ccount = comparison_count;
423 if (needle_last_ccount == NULL)
425 /* Try the Knuth-Morris-Pratt algorithm. */
428 knuth_morris_pratt_unibyte (haystack, needle - 1,
431 return (char *) result;
438 if (TOLOWER ((unsigned char) *haystack) == b)
439 /* The first character matches. */
441 const char *rhaystack = haystack + 1;
442 const char *rneedle = needle;
444 for (;; rhaystack++, rneedle++)
446 if (*rneedle == '\0')
448 return (char *) haystack;
449 if (*rhaystack == '\0')
453 if (TOLOWER ((unsigned char) *rhaystack)
454 != TOLOWER ((unsigned char) *rneedle))
455 /* Nothing in this round. */
462 return (char *) haystack;