1 /* Searching in a string.
2 Copyright (C) 2005-2013 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 UNIT unsigned char
31 #define CANON_ELEMENT(c) c
34 /* Knuth-Morris-Pratt algorithm.
35 See http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
36 Return a boolean indicating success:
37 Return true and set *RESULTP if the search was completed.
38 Return false if it was aborted because not enough memory was available. */
40 knuth_morris_pratt_multibyte (const char *haystack
, const char *needle
,
43 size_t m
= mbslen (needle
);
44 mbchar_t
*needle_mbchars
;
47 /* Allocate room for needle_mbchars and the table. */
48 char *memory
= (char *) nmalloca (m
, sizeof (mbchar_t
) + sizeof (size_t));
51 needle_mbchars
= (mbchar_t
*) memory
;
52 table
= (size_t *) (memory
+ m
* sizeof (mbchar_t
));
54 /* Fill needle_mbchars. */
60 for (mbui_init (iter
, needle
); mbui_avail (iter
); mbui_advance (iter
), j
++)
61 mb_copy (&needle_mbchars
[j
], &mbui_cur (iter
));
66 0 < table[i] <= i is defined such that
67 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
68 and table[i] is as large as possible with this property.
72 needle[table[i]..i-1] = needle[0..i-1-table[i]].
74 rhaystack[0..i-1] == needle[0..i-1]
75 and exists h, i <= h < m: rhaystack[h] != needle[h]
77 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
78 table[0] remains uninitialized. */
82 /* i = 1: Nothing to verify for x = 0. */
86 for (i
= 2; i
< m
; i
++)
88 /* Here: j = i-1 - table[i-1].
89 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
90 for x < table[i-1], by induction.
91 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
92 mbchar_t
*b
= &needle_mbchars
[i
- 1];
96 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
97 is known to hold for x < i-1-j.
98 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
99 if (mb_equal (*b
, needle_mbchars
[j
]))
101 /* Set table[i] := i-1-j. */
105 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
106 for x = i-1-j, because
107 needle[i-1] != needle[j] = needle[i-1-x]. */
110 /* The inequality holds for all possible x. */
114 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
115 for i-1-j < x < i-1-j+table[j], because for these x:
117 = needle[x-(i-1-j)..j-1]
118 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
120 hence needle[x..i-1] != needle[0..i-1-x].
122 needle[i-1-j+table[j]..i-2]
123 = needle[table[j]..j-1]
124 = needle[0..j-1-table[j]] (by definition of table[j]). */
127 /* Here: j = i - table[i]. */
131 /* Search, using the table to accelerate the processing. */
134 mbui_iterator_t rhaystack
;
135 mbui_iterator_t phaystack
;
139 mbui_init (rhaystack
, haystack
);
140 mbui_init (phaystack
, haystack
);
141 /* Invariant: phaystack = rhaystack + j. */
142 while (mbui_avail (phaystack
))
143 if (mb_equal (needle_mbchars
[j
], mbui_cur (phaystack
)))
146 mbui_advance (phaystack
);
149 /* The entire needle has been found. */
150 *resultp
= mbui_cur_ptr (rhaystack
);
156 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
157 size_t count
= table
[j
];
159 for (; count
> 0; count
--)
161 if (!mbui_avail (rhaystack
))
163 mbui_advance (rhaystack
);
168 /* Found a mismatch at needle[0] already. */
169 if (!mbui_avail (rhaystack
))
171 mbui_advance (rhaystack
);
172 mbui_advance (phaystack
);
180 /* Find the first occurrence of the character string NEEDLE in the character
181 string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK. */
183 mbsstr (const char *haystack
, const char *needle
)
185 /* Be careful not to look at the entire extent of haystack or needle
186 until needed. This is useful because of these two cases:
187 - haystack may be very long, and a match of needle found early,
188 - needle may be very long, and not even a short initial segment of
189 needle may be found in haystack. */
192 mbui_iterator_t iter_needle
;
194 mbui_init (iter_needle
, needle
);
195 if (mbui_avail (iter_needle
))
197 /* Minimizing the worst-case complexity:
198 Let n = mbslen(haystack), m = mbslen(needle).
199 The naïve algorithm is O(n*m) worst-case.
200 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
202 To achieve linear complexity and yet amortize the cost of the
203 memory allocation, we activate the Knuth-Morris-Pratt algorithm
204 only once the naïve algorithm has already run for some time; more
206 - the outer loop count is >= 10,
207 - the average number of comparisons per outer loop is >= 5,
208 - the total number of comparisons is >= m.
209 But we try it only once. If the memory allocation attempt failed,
210 we don't retry it. */
212 size_t outer_loop_count
= 0;
213 size_t comparison_count
= 0;
214 size_t last_ccount
= 0; /* last comparison count */
215 mbui_iterator_t iter_needle_last_ccount
; /* = needle + last_ccount */
217 mbui_iterator_t iter_haystack
;
219 mbui_init (iter_needle_last_ccount
, needle
);
220 mbui_init (iter_haystack
, haystack
);
221 for (;; mbui_advance (iter_haystack
))
223 if (!mbui_avail (iter_haystack
))
227 /* See whether it's advisable to use an asymptotically faster
230 && outer_loop_count
>= 10
231 && comparison_count
>= 5 * outer_loop_count
)
233 /* See if needle + comparison_count now reaches the end of
235 size_t count
= comparison_count
- last_ccount
;
237 count
> 0 && mbui_avail (iter_needle_last_ccount
);
239 mbui_advance (iter_needle_last_ccount
);
240 last_ccount
= comparison_count
;
241 if (!mbui_avail (iter_needle_last_ccount
))
243 /* Try the Knuth-Morris-Pratt algorithm. */
246 knuth_morris_pratt_multibyte (haystack
, needle
,
249 return (char *) result
;
256 if (mb_equal (mbui_cur (iter_haystack
), mbui_cur (iter_needle
)))
257 /* The first character matches. */
259 mbui_iterator_t rhaystack
;
260 mbui_iterator_t rneedle
;
262 memcpy (&rhaystack
, &iter_haystack
, sizeof (mbui_iterator_t
));
263 mbui_advance (rhaystack
);
265 mbui_init (rneedle
, needle
);
266 if (!mbui_avail (rneedle
))
268 mbui_advance (rneedle
);
270 for (;; mbui_advance (rhaystack
), mbui_advance (rneedle
))
272 if (!mbui_avail (rneedle
))
274 return (char *) mbui_cur_ptr (iter_haystack
);
275 if (!mbui_avail (rhaystack
))
279 if (!mb_equal (mbui_cur (rhaystack
), mbui_cur (rneedle
)))
280 /* Nothing in this round. */
287 return (char *) haystack
;
293 /* Minimizing the worst-case complexity:
294 Let n = strlen(haystack), m = strlen(needle).
295 The naïve algorithm is O(n*m) worst-case.
296 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
298 To achieve linear complexity and yet amortize the cost of the
299 memory allocation, we activate the Knuth-Morris-Pratt algorithm
300 only once the naïve algorithm has already run for some time; more
302 - the outer loop count is >= 10,
303 - the average number of comparisons per outer loop is >= 5,
304 - the total number of comparisons is >= m.
305 But we try it only once. If the memory allocation attempt failed,
306 we don't retry it. */
308 size_t outer_loop_count
= 0;
309 size_t comparison_count
= 0;
310 size_t last_ccount
= 0; /* last comparison count */
311 const char *needle_last_ccount
= needle
; /* = needle + last_ccount */
313 /* Speed up the following searches of needle by caching its first
319 if (*haystack
== '\0')
323 /* See whether it's advisable to use an asymptotically faster
326 && outer_loop_count
>= 10
327 && comparison_count
>= 5 * outer_loop_count
)
329 /* See if needle + comparison_count now reaches the end of
331 if (needle_last_ccount
!= NULL
)
333 needle_last_ccount
+=
334 strnlen (needle_last_ccount
,
335 comparison_count
- last_ccount
);
336 if (*needle_last_ccount
== '\0')
337 needle_last_ccount
= NULL
;
338 last_ccount
= comparison_count
;
340 if (needle_last_ccount
== NULL
)
342 /* Try the Knuth-Morris-Pratt algorithm. */
343 const unsigned char *result
;
345 knuth_morris_pratt ((const unsigned char *) haystack
,
346 (const unsigned char *) (needle
- 1),
350 return (char *) result
;
358 /* The first character matches. */
360 const char *rhaystack
= haystack
+ 1;
361 const char *rneedle
= needle
;
363 for (;; rhaystack
++, rneedle
++)
365 if (*rneedle
== '\0')
367 return (char *) haystack
;
368 if (*rhaystack
== '\0')
372 if (*rhaystack
!= *rneedle
)
373 /* Nothing in this round. */
380 return (char *) haystack
;