1 /* Searching in a string.
2 Copyright (C) 2005-2008 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 */
29 /* Knuth-Morris-Pratt algorithm. */
30 #define CANON_ELEMENT(c) c
33 /* Knuth-Morris-Pratt algorithm.
34 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
35 Return a boolean indicating success:
36 Return true and set *RESULTP if the search was completed.
37 Return false if it was aborted because not enough memory was available. */
39 knuth_morris_pratt_multibyte (const char *haystack, const char *needle,
42 size_t m = mbslen (needle);
43 mbchar_t *needle_mbchars;
46 /* Allocate room for needle_mbchars and the table. */
47 char *memory = (char *) nmalloca (m, sizeof (mbchar_t) + sizeof (size_t));
50 needle_mbchars = (mbchar_t *) memory;
51 table = (size_t *) (memory + m * sizeof (mbchar_t));
53 /* Fill needle_mbchars. */
59 for (mbui_init (iter, needle); mbui_avail (iter); mbui_advance (iter), j++)
60 mb_copy (&needle_mbchars[j], &mbui_cur (iter));
65 0 < table[i] <= i is defined such that
66 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
67 and table[i] is as large as possible with this property.
71 needle[table[i]..i-1] = needle[0..i-1-table[i]].
73 rhaystack[0..i-1] == needle[0..i-1]
74 and exists h, i <= h < m: rhaystack[h] != needle[h]
76 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
77 table[0] remains uninitialized. */
81 /* i = 1: Nothing to verify for x = 0. */
85 for (i = 2; i < m; i++)
87 /* Here: j = i-1 - table[i-1].
88 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
89 for x < table[i-1], by induction.
90 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
91 mbchar_t *b = &needle_mbchars[i - 1];
95 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
96 is known to hold for x < i-1-j.
97 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
98 if (mb_equal (*b, needle_mbchars[j]))
100 /* Set table[i] := i-1-j. */
104 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
105 for x = i-1-j, because
106 needle[i-1] != needle[j] = needle[i-1-x]. */
109 /* The inequality holds for all possible x. */
113 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
114 for i-1-j < x < i-1-j+table[j], because for these x:
116 = needle[x-(i-1-j)..j-1]
117 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
119 hence needle[x..i-1] != needle[0..i-1-x].
121 needle[i-1-j+table[j]..i-2]
122 = needle[table[j]..j-1]
123 = needle[0..j-1-table[j]] (by definition of table[j]). */
126 /* Here: j = i - table[i]. */
130 /* Search, using the table to accelerate the processing. */
133 mbui_iterator_t rhaystack;
134 mbui_iterator_t phaystack;
138 mbui_init (rhaystack, haystack);
139 mbui_init (phaystack, haystack);
140 /* Invariant: phaystack = rhaystack + j. */
141 while (mbui_avail (phaystack))
142 if (mb_equal (needle_mbchars[j], mbui_cur (phaystack)))
145 mbui_advance (phaystack);
148 /* The entire needle has been found. */
149 *resultp = mbui_cur_ptr (rhaystack);
155 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
156 size_t count = table[j];
158 for (; count > 0; count--)
160 if (!mbui_avail (rhaystack))
162 mbui_advance (rhaystack);
167 /* Found a mismatch at needle[0] already. */
168 if (!mbui_avail (rhaystack))
170 mbui_advance (rhaystack);
171 mbui_advance (phaystack);
179 /* Find the first occurrence of the character string NEEDLE in the character
180 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK. */
182 mbsstr (const char *haystack, const char *needle)
184 /* Be careful not to look at the entire extent of haystack or needle
185 until needed. This is useful because of these two cases:
186 - haystack may be very long, and a match of needle found early,
187 - needle may be very long, and not even a short initial segment of
188 needle may be found in haystack. */
191 mbui_iterator_t iter_needle;
193 mbui_init (iter_needle, needle);
194 if (mbui_avail (iter_needle))
196 /* Minimizing the worst-case complexity:
197 Let n = mbslen(haystack), m = mbslen(needle).
198 The naïve algorithm is O(n*m) worst-case.
199 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
201 To achieve linear complexity and yet amortize the cost of the
202 memory allocation, we activate the Knuth-Morris-Pratt algorithm
203 only once the naïve algorithm has already run for some time; more
205 - the outer loop count is >= 10,
206 - the average number of comparisons per outer loop is >= 5,
207 - the total number of comparisons is >= m.
208 But we try it only once. If the memory allocation attempt failed,
209 we don't retry it. */
211 size_t outer_loop_count = 0;
212 size_t comparison_count = 0;
213 size_t last_ccount = 0; /* last comparison count */
214 mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */
216 mbui_iterator_t iter_haystack;
218 mbui_init (iter_needle_last_ccount, needle);
219 mbui_init (iter_haystack, haystack);
220 for (;; mbui_advance (iter_haystack))
222 if (!mbui_avail (iter_haystack))
226 /* See whether it's advisable to use an asymptotically faster
229 && outer_loop_count >= 10
230 && comparison_count >= 5 * outer_loop_count)
232 /* See if needle + comparison_count now reaches the end of
234 size_t count = comparison_count - last_ccount;
236 count > 0 && mbui_avail (iter_needle_last_ccount);
238 mbui_advance (iter_needle_last_ccount);
239 last_ccount = comparison_count;
240 if (!mbui_avail (iter_needle_last_ccount))
242 /* Try the Knuth-Morris-Pratt algorithm. */
245 knuth_morris_pratt_multibyte (haystack, needle,
248 return (char *) result;
255 if (mb_equal (mbui_cur (iter_haystack), mbui_cur (iter_needle)))
256 /* The first character matches. */
258 mbui_iterator_t rhaystack;
259 mbui_iterator_t rneedle;
261 memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t));
262 mbui_advance (rhaystack);
264 mbui_init (rneedle, needle);
265 if (!mbui_avail (rneedle))
267 mbui_advance (rneedle);
269 for (;; mbui_advance (rhaystack), mbui_advance (rneedle))
271 if (!mbui_avail (rneedle))
273 return (char *) mbui_cur_ptr (iter_haystack);
274 if (!mbui_avail (rhaystack))
278 if (!mb_equal (mbui_cur (rhaystack), mbui_cur (rneedle)))
279 /* Nothing in this round. */
286 return (char *) haystack;
292 /* Minimizing the worst-case complexity:
293 Let n = strlen(haystack), m = strlen(needle).
294 The naïve algorithm is O(n*m) worst-case.
295 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
297 To achieve linear complexity and yet amortize the cost of the
298 memory allocation, we activate the Knuth-Morris-Pratt algorithm
299 only once the naïve algorithm has already run for some time; more
301 - the outer loop count is >= 10,
302 - the average number of comparisons per outer loop is >= 5,
303 - the total number of comparisons is >= m.
304 But we try it only once. If the memory allocation attempt failed,
305 we don't retry it. */
307 size_t outer_loop_count = 0;
308 size_t comparison_count = 0;
309 size_t last_ccount = 0; /* last comparison count */
310 const char *needle_last_ccount = needle; /* = needle + last_ccount */
312 /* Speed up the following searches of needle by caching its first
318 if (*haystack == '\0')
322 /* See whether it's advisable to use an asymptotically faster
325 && outer_loop_count >= 10
326 && comparison_count >= 5 * outer_loop_count)
328 /* See if needle + comparison_count now reaches the end of
330 if (needle_last_ccount != NULL)
332 needle_last_ccount +=
333 strnlen (needle_last_ccount,
334 comparison_count - last_ccount);
335 if (*needle_last_ccount == '\0')
336 needle_last_ccount = NULL;
337 last_ccount = comparison_count;
339 if (needle_last_ccount == NULL)
341 /* Try the Knuth-Morris-Pratt algorithm. */
344 knuth_morris_pratt_unibyte (haystack, needle - 1,
347 return (char *) result;
355 /* The first character matches. */
357 const char *rhaystack = haystack + 1;
358 const char *rneedle = needle;
360 for (;; rhaystack++, rneedle++)
362 if (*rneedle == '\0')
364 return (char *) haystack;
365 if (*rhaystack == '\0')
369 if (*rhaystack != *rneedle)
370 /* Nothing in this round. */
377 return (char *) haystack;