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 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. */
25 #include <stddef.h> /* for NULL, in case a nonstandard string.h lacks it */
32 /* Knuth-Morris-Pratt algorithm.
33 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
34 Return a boolean indicating success. */
37 knuth_morris_pratt_unibyte (const char *haystack, const char *needle,
40 size_t m = strlen (needle);
42 /* Allocate the table. */
43 size_t *table = (size_t *) malloca (m * sizeof (size_t));
48 0 < table[i] <= i is defined such that
49 rhaystack[0..i-1] == needle[0..i-1] and rhaystack[i] != needle[i]
51 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1],
52 and table[i] is as large as possible with this property.
53 table[0] remains uninitialized. */
59 for (i = 2; i < m; i++)
61 unsigned char b = (unsigned char) needle[i - 1];
65 if (b == (unsigned char) needle[j])
80 /* Search, using the table to accelerate the processing. */
83 const char *rhaystack;
84 const char *phaystack;
90 /* Invariant: phaystack = rhaystack + j. */
91 while (*phaystack != '\0')
92 if ((unsigned char) needle[j] == (unsigned char) *phaystack)
98 /* The entire needle has been found. */
105 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
106 rhaystack += table[j];
111 /* Found a mismatch at needle[0] already. */
123 knuth_morris_pratt_multibyte (const char *haystack, const char *needle,
124 const char **resultp)
126 size_t m = mbslen (needle);
127 mbchar_t *needle_mbchars;
130 /* Allocate room for needle_mbchars and the table. */
131 char *memory = (char *) malloca (m * (sizeof (mbchar_t) + sizeof (size_t)));
134 needle_mbchars = (mbchar_t *) memory;
135 table = (size_t *) (memory + m * sizeof (mbchar_t));
137 /* Fill needle_mbchars. */
139 mbui_iterator_t iter;
143 for (mbui_init (iter, needle); mbui_avail (iter); mbui_advance (iter), j++)
144 mb_copy (&needle_mbchars[j], &mbui_cur (iter));
149 0 < table[i] <= i is defined such that
150 rhaystack[0..i-1] == needle[0..i-1] and rhaystack[i] != needle[i]
152 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1],
153 and table[i] is as large as possible with this property.
154 table[0] remains uninitialized. */
160 for (i = 2; i < m; i++)
162 mbchar_t *b = &needle_mbchars[i - 1];
166 if (mb_equal (*b, needle_mbchars[j]))
181 /* Search, using the table to accelerate the processing. */
184 mbui_iterator_t rhaystack;
185 mbui_iterator_t phaystack;
189 mbui_init (rhaystack, haystack);
190 mbui_init (phaystack, haystack);
191 /* Invariant: phaystack = rhaystack + j. */
192 while (mbui_avail (phaystack))
193 if (mb_equal (needle_mbchars[j], mbui_cur (phaystack)))
196 mbui_advance (phaystack);
199 /* The entire needle has been found. */
200 *resultp = mbui_cur_ptr (rhaystack);
206 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
207 size_t count = table[j];
209 for (; count > 0; count--)
211 if (!mbui_avail (rhaystack))
213 mbui_advance (rhaystack);
218 /* Found a mismatch at needle[0] already. */
219 if (!mbui_avail (rhaystack))
221 mbui_advance (rhaystack);
222 mbui_advance (phaystack);
231 /* Find the first occurrence of the character string NEEDLE in the character
232 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK. */
234 mbsstr (const char *haystack, const char *needle)
236 /* Be careful not to look at the entire extent of haystack or needle
237 until needed. This is useful because of these two cases:
238 - haystack may be very long, and a match of needle found early,
239 - needle may be very long, and not even a short initial segment of
240 needle may be found in haystack. */
244 mbui_iterator_t iter_needle;
246 mbui_init (iter_needle, needle);
247 if (mbui_avail (iter_needle))
249 /* Minimizing the worst-case complexity:
250 Let n = mbslen(haystack), m = mbslen(needle).
251 The naïve algorithm is O(n*m) worst-case.
252 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
254 To achieve linear complexity and yet amortize the cost of the
255 memory allocation, we activate the Knuth-Morris-Pratt algorithm
256 only once the naïve algorithm has already run for some time; more
258 - the outer loop count is >= 10,
259 - the average number of comparisons per outer loop is >= 5,
260 - the total number of comparisons is >= m.
261 But we try it only once. If the memory allocation attempt failed,
262 we don't retry it. */
264 size_t outer_loop_count = 0;
265 size_t comparison_count = 0;
266 size_t last_ccount = 0; /* last comparison count */
267 mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */
269 mbui_iterator_t iter_haystack;
271 mbui_init (iter_needle_last_ccount, needle);
272 mbui_init (iter_haystack, haystack);
273 for (;; mbui_advance (iter_haystack))
275 if (!mbui_avail (iter_haystack))
279 /* See whether it's advisable to use an asymptotically faster
282 && outer_loop_count >= 10
283 && comparison_count >= 5 * outer_loop_count)
285 /* See if needle + comparison_count now reaches the end of
287 size_t count = comparison_count - last_ccount;
289 count > 0 && mbui_avail (iter_needle_last_ccount);
291 mbui_advance (iter_needle_last_ccount);
292 last_ccount = comparison_count;
293 if (!mbui_avail (iter_needle_last_ccount))
295 /* Try the Knuth-Morris-Pratt algorithm. */
298 knuth_morris_pratt_multibyte (haystack, needle,
301 return (char *) result;
308 if (mb_equal (mbui_cur (iter_haystack), mbui_cur (iter_needle)))
309 /* The first character matches. */
311 mbui_iterator_t rhaystack;
312 mbui_iterator_t rneedle;
314 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
315 mbui_advance (rhaystack);
317 mbui_init (rneedle, needle);
318 if (!mbui_avail (rneedle))
320 mbui_advance (rneedle);
322 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
324 if (!mbui_avail (rneedle))
326 return (char *) mbui_cur_ptr (iter_haystack);
327 if (!mbui_avail (rhaystack))
331 if (!mb_equal (mbui_cur (rhaystack), mbui_cur (rneedle)))
332 /* Nothing in this round. */
339 return (char *) haystack;
346 /* Minimizing the worst-case complexity:
347 Let n = strlen(haystack), m = strlen(needle).
348 The naïve algorithm is O(n*m) worst-case.
349 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
351 To achieve linear complexity and yet amortize the cost of the
352 memory allocation, we activate the Knuth-Morris-Pratt algorithm
353 only once the naïve algorithm has already run for some time; more
355 - the outer loop count is >= 10,
356 - the average number of comparisons per outer loop is >= 5,
357 - the total number of comparisons is >= m.
358 But we try it only once. If the memory allocation attempt failed,
359 we don't retry it. */
361 size_t outer_loop_count = 0;
362 size_t comparison_count = 0;
363 size_t last_ccount = 0; /* last comparison count */
364 const char *needle_last_ccount = needle; /* = needle + last_ccount */
366 /* Speed up the following searches of needle by caching its first
372 if (*haystack == '\0')
376 /* See whether it's advisable to use an asymptotically faster
379 && outer_loop_count >= 10
380 && comparison_count >= 5 * outer_loop_count)
382 /* See if needle + comparison_count now reaches the end of
384 if (needle_last_ccount != NULL)
386 needle_last_ccount +=
387 strnlen (needle_last_ccount,
388 comparison_count - last_ccount);
389 if (*needle_last_ccount == '\0')
390 needle_last_ccount = NULL;
391 last_ccount = comparison_count;
393 if (needle_last_ccount == NULL)
395 /* Try the Knuth-Morris-Pratt algorithm. */
398 knuth_morris_pratt_unibyte (haystack, needle - 1,
401 return (char *) result;
409 /* The first character matches. */
411 const char *rhaystack = haystack + 1;
412 const char *rneedle = needle;
414 for (;; rhaystack++, rneedle++)
416 if (*rneedle == '\0')
418 return (char *) haystack;
419 if (*rhaystack == '\0')
423 if (*rhaystack != *rneedle)
424 /* Nothing in this round. */
431 return (char *) haystack;