1 /* 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/>. */
24 #include <stddef.h> /* for NULL, in case a nonstandard string.h lacks it */
31 /* Knuth-Morris-Pratt algorithm.
32 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
33 Return a boolean indicating success. */
36 knuth_morris_pratt_unibyte (const char *haystack, const char *needle,
39 size_t m = strlen (needle);
41 /* Allocate the table. */
42 size_t *table = (size_t *) nmalloca (m, sizeof (size_t));
47 0 < table[i] <= i is defined such that
48 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
49 and table[i] is as large as possible with this property.
53 needle[table[i]..i-1] = needle[0..i-1-table[i]].
55 rhaystack[0..i-1] == needle[0..i-1]
56 and exists h, i <= h < m: rhaystack[h] != needle[h]
58 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
59 table[0] remains uninitialized. */
63 /* i = 1: Nothing to verify for x = 0. */
67 for (i = 2; i < m; i++)
69 /* Here: j = i-1 - table[i-1].
70 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
71 for x < table[i-1], by induction.
72 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
73 unsigned char b = (unsigned char) needle[i - 1];
77 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
78 is known to hold for x < i-1-j.
79 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
80 if (b == (unsigned char) needle[j])
82 /* Set table[i] := i-1-j. */
86 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
87 for x = i-1-j, because
88 needle[i-1] != needle[j] = needle[i-1-x]. */
91 /* The inequality holds for all possible x. */
95 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
96 for i-1-j < x < i-1-j+table[j], because for these x:
98 = needle[x-(i-1-j)..j-1]
99 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
101 hence needle[x..i-1] != needle[0..i-1-x].
103 needle[i-1-j+table[j]..i-2]
104 = needle[table[j]..j-1]
105 = needle[0..j-1-table[j]] (by definition of table[j]). */
108 /* Here: j = i - table[i]. */
112 /* Search, using the table to accelerate the processing. */
115 const char *rhaystack;
116 const char *phaystack;
120 rhaystack = haystack;
121 phaystack = haystack;
122 /* Invariant: phaystack = rhaystack + j. */
123 while (*phaystack != '\0')
124 if ((unsigned char) needle[j] == (unsigned char) *phaystack)
130 /* The entire needle has been found. */
131 *resultp = rhaystack;
137 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
138 rhaystack += table[j];
143 /* Found a mismatch at needle[0] already. */
155 knuth_morris_pratt_multibyte (const char *haystack, const char *needle,
156 const char **resultp)
158 size_t m = mbslen (needle);
159 mbchar_t *needle_mbchars;
162 /* Allocate room for needle_mbchars and the table. */
163 char *memory = (char *) nmalloca (m, sizeof (mbchar_t) + sizeof (size_t));
166 needle_mbchars = (mbchar_t *) memory;
167 table = (size_t *) (memory + m * sizeof (mbchar_t));
169 /* Fill needle_mbchars. */
171 mbui_iterator_t iter;
175 for (mbui_init (iter, needle); mbui_avail (iter); mbui_advance (iter), j++)
176 mb_copy (&needle_mbchars[j], &mbui_cur (iter));
181 0 < table[i] <= i is defined such that
182 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
183 and table[i] is as large as possible with this property.
187 needle[table[i]..i-1] = needle[0..i-1-table[i]].
189 rhaystack[0..i-1] == needle[0..i-1]
190 and exists h, i <= h < m: rhaystack[h] != needle[h]
192 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
193 table[0] remains uninitialized. */
197 /* i = 1: Nothing to verify for x = 0. */
201 for (i = 2; i < m; i++)
203 /* Here: j = i-1 - table[i-1].
204 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
205 for x < table[i-1], by induction.
206 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
207 mbchar_t *b = &needle_mbchars[i - 1];
211 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
212 is known to hold for x < i-1-j.
213 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
214 if (mb_equal (*b, needle_mbchars[j]))
216 /* Set table[i] := i-1-j. */
220 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
221 for x = i-1-j, because
222 needle[i-1] != needle[j] = needle[i-1-x]. */
225 /* The inequality holds for all possible x. */
229 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
230 for i-1-j < x < i-1-j+table[j], because for these x:
232 = needle[x-(i-1-j)..j-1]
233 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
235 hence needle[x..i-1] != needle[0..i-1-x].
237 needle[i-1-j+table[j]..i-2]
238 = needle[table[j]..j-1]
239 = needle[0..j-1-table[j]] (by definition of table[j]). */
242 /* Here: j = i - table[i]. */
246 /* Search, using the table to accelerate the processing. */
249 mbui_iterator_t rhaystack;
250 mbui_iterator_t phaystack;
254 mbui_init (rhaystack, haystack);
255 mbui_init (phaystack, haystack);
256 /* Invariant: phaystack = rhaystack + j. */
257 while (mbui_avail (phaystack))
258 if (mb_equal (needle_mbchars[j], mbui_cur (phaystack)))
261 mbui_advance (phaystack);
264 /* The entire needle has been found. */
265 *resultp = mbui_cur_ptr (rhaystack);
271 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
272 size_t count = table[j];
274 for (; count > 0; count--)
276 if (!mbui_avail (rhaystack))
278 mbui_advance (rhaystack);
283 /* Found a mismatch at needle[0] already. */
284 if (!mbui_avail (rhaystack))
286 mbui_advance (rhaystack);
287 mbui_advance (phaystack);
296 /* Find the first occurrence of the character string NEEDLE in the character
297 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK. */
299 mbsstr (const char *haystack, const char *needle)
301 /* Be careful not to look at the entire extent of haystack or needle
302 until needed. This is useful because of these two cases:
303 - haystack may be very long, and a match of needle found early,
304 - needle may be very long, and not even a short initial segment of
305 needle may be found in haystack. */
309 mbui_iterator_t iter_needle;
311 mbui_init (iter_needle, needle);
312 if (mbui_avail (iter_needle))
314 /* Minimizing the worst-case complexity:
315 Let n = mbslen(haystack), m = mbslen(needle).
316 The naïve algorithm is O(n*m) worst-case.
317 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
319 To achieve linear complexity and yet amortize the cost of the
320 memory allocation, we activate the Knuth-Morris-Pratt algorithm
321 only once the naïve algorithm has already run for some time; more
323 - the outer loop count is >= 10,
324 - the average number of comparisons per outer loop is >= 5,
325 - the total number of comparisons is >= m.
326 But we try it only once. If the memory allocation attempt failed,
327 we don't retry it. */
329 size_t outer_loop_count = 0;
330 size_t comparison_count = 0;
331 size_t last_ccount = 0; /* last comparison count */
332 mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */
334 mbui_iterator_t iter_haystack;
336 mbui_init (iter_needle_last_ccount, needle);
337 mbui_init (iter_haystack, haystack);
338 for (;; mbui_advance (iter_haystack))
340 if (!mbui_avail (iter_haystack))
344 /* See whether it's advisable to use an asymptotically faster
347 && outer_loop_count >= 10
348 && comparison_count >= 5 * outer_loop_count)
350 /* See if needle + comparison_count now reaches the end of
352 size_t count = comparison_count - last_ccount;
354 count > 0 && mbui_avail (iter_needle_last_ccount);
356 mbui_advance (iter_needle_last_ccount);
357 last_ccount = comparison_count;
358 if (!mbui_avail (iter_needle_last_ccount))
360 /* Try the Knuth-Morris-Pratt algorithm. */
363 knuth_morris_pratt_multibyte (haystack, needle,
366 return (char *) result;
373 if (mb_equal (mbui_cur (iter_haystack), mbui_cur (iter_needle)))
374 /* The first character matches. */
376 mbui_iterator_t rhaystack;
377 mbui_iterator_t rneedle;
379 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
380 mbui_advance (rhaystack);
382 mbui_init (rneedle, needle);
383 if (!mbui_avail (rneedle))
385 mbui_advance (rneedle);
387 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
389 if (!mbui_avail (rneedle))
391 return (char *) mbui_cur_ptr (iter_haystack);
392 if (!mbui_avail (rhaystack))
396 if (!mb_equal (mbui_cur (rhaystack), mbui_cur (rneedle)))
397 /* Nothing in this round. */
404 return (char *) haystack;
411 /* Minimizing the worst-case complexity:
412 Let n = strlen(haystack), m = strlen(needle).
413 The naïve algorithm is O(n*m) worst-case.
414 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
416 To achieve linear complexity and yet amortize the cost of the
417 memory allocation, we activate the Knuth-Morris-Pratt algorithm
418 only once the naïve algorithm has already run for some time; more
420 - the outer loop count is >= 10,
421 - the average number of comparisons per outer loop is >= 5,
422 - the total number of comparisons is >= m.
423 But we try it only once. If the memory allocation attempt failed,
424 we don't retry it. */
426 size_t outer_loop_count = 0;
427 size_t comparison_count = 0;
428 size_t last_ccount = 0; /* last comparison count */
429 const char *needle_last_ccount = needle; /* = needle + last_ccount */
431 /* Speed up the following searches of needle by caching its first
437 if (*haystack == '\0')
441 /* See whether it's advisable to use an asymptotically faster
444 && outer_loop_count >= 10
445 && comparison_count >= 5 * outer_loop_count)
447 /* See if needle + comparison_count now reaches the end of
449 if (needle_last_ccount != NULL)
451 needle_last_ccount +=
452 strnlen (needle_last_ccount,
453 comparison_count - last_ccount);
454 if (*needle_last_ccount == '\0')
455 needle_last_ccount = NULL;
456 last_ccount = comparison_count;
458 if (needle_last_ccount == NULL)
460 /* Try the Knuth-Morris-Pratt algorithm. */
463 knuth_morris_pratt_unibyte (haystack, needle - 1,
466 return (char *) result;
474 /* The first character matches. */
476 const char *rhaystack = haystack + 1;
477 const char *rneedle = needle;
479 for (;; rhaystack++, rneedle++)
481 if (*rneedle == '\0')
483 return (char *) haystack;
484 if (*rhaystack == '\0')
488 if (*rhaystack != *rneedle)
489 /* Nothing in this round. */
496 return (char *) haystack;