1 /* Case-insensitive searching in a string. -*- coding: utf-8 -*-
2 Copyright (C) 2005-2020 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 <https://www.gnu.org/licenses/>. */
25 #include <stddef.h> /* for NULL, in case a nonstandard string.h lacks it */
31 #define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
33 /* Knuth-Morris-Pratt algorithm. */
34 #define UNIT unsigned char
35 #define CANON_ELEMENT(c) TOLOWER (c)
38 /* Knuth-Morris-Pratt algorithm.
39 See https://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
40 Return a boolean indicating success:
41 Return true and set *RESULTP if the search was completed.
42 Return false if it was aborted because not enough memory was available. */
44 knuth_morris_pratt_multibyte (const char *haystack
, const char *needle
,
47 size_t m
= mbslen (needle
);
48 mbchar_t
*needle_mbchars
;
51 /* Allocate room for needle_mbchars and the table. */
52 char *memory
= (char *) nmalloca (m
, sizeof (mbchar_t
) + sizeof (size_t));
55 needle_mbchars
= (mbchar_t
*) memory
;
56 table
= (size_t *) (memory
+ m
* sizeof (mbchar_t
));
58 /* Fill needle_mbchars. */
64 for (mbui_init (iter
, needle
); mbui_avail (iter
); mbui_advance (iter
), j
++)
66 mb_copy (&needle_mbchars
[j
], &mbui_cur (iter
));
67 if (needle_mbchars
[j
].wc_valid
)
68 needle_mbchars
[j
].wc
= towlower (needle_mbchars
[j
].wc
);
74 0 < table[i] <= i is defined such that
75 forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x],
76 and table[i] is as large as possible with this property.
80 needle[table[i]..i-1] = needle[0..i-1-table[i]].
82 rhaystack[0..i-1] == needle[0..i-1]
83 and exists h, i <= h < m: rhaystack[h] != needle[h]
85 forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1].
86 table[0] remains uninitialized. */
90 /* i = 1: Nothing to verify for x = 0. */
94 for (i
= 2; i
< m
; i
++)
96 /* Here: j = i-1 - table[i-1].
97 The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold
98 for x < table[i-1], by induction.
99 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
100 mbchar_t
*b
= &needle_mbchars
[i
- 1];
104 /* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x]
105 is known to hold for x < i-1-j.
106 Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */
107 if (mb_equal (*b
, needle_mbchars
[j
]))
109 /* Set table[i] := i-1-j. */
113 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
114 for x = i-1-j, because
115 needle[i-1] != needle[j] = needle[i-1-x]. */
118 /* The inequality holds for all possible x. */
122 /* The inequality needle[x..i-1] != needle[0..i-1-x] also holds
123 for i-1-j < x < i-1-j+table[j], because for these x:
125 = needle[x-(i-1-j)..j-1]
126 != needle[0..j-1-(x-(i-1-j))] (by definition of table[j])
128 hence needle[x..i-1] != needle[0..i-1-x].
130 needle[i-1-j+table[j]..i-2]
131 = needle[table[j]..j-1]
132 = needle[0..j-1-table[j]] (by definition of table[j]). */
135 /* Here: j = i - table[i]. */
139 /* Search, using the table to accelerate the processing. */
142 mbui_iterator_t rhaystack
;
143 mbui_iterator_t phaystack
;
147 mbui_init (rhaystack
, haystack
);
148 mbui_init (phaystack
, haystack
);
149 /* Invariant: phaystack = rhaystack + j. */
150 while (mbui_avail (phaystack
))
154 mb_copy (&c
, &mbui_cur (phaystack
));
156 c
.wc
= towlower (c
.wc
);
157 if (mb_equal (needle_mbchars
[j
], c
))
160 mbui_advance (phaystack
);
163 /* The entire needle has been found. */
164 *resultp
= mbui_cur_ptr (rhaystack
);
170 /* Found a match of needle[0..j-1], mismatch at needle[j]. */
171 size_t count
= table
[j
];
173 for (; count
> 0; count
--)
175 if (!mbui_avail (rhaystack
))
177 mbui_advance (rhaystack
);
182 /* Found a mismatch at needle[0] already. */
183 if (!mbui_avail (rhaystack
))
185 mbui_advance (rhaystack
);
186 mbui_advance (phaystack
);
195 /* Find the first occurrence of the character string NEEDLE in the character
196 string HAYSTACK, using case-insensitive comparison.
197 Note: This function may, in multibyte locales, return success even if
198 strlen (haystack) < strlen (needle) ! */
200 mbscasestr (const char *haystack
, const char *needle
)
202 /* Be careful not to look at the entire extent of haystack or needle
203 until needed. This is useful because of these two cases:
204 - haystack may be very long, and a match of needle found early,
205 - needle may be very long, and not even a short initial segment of
206 needle may be found in haystack. */
209 mbui_iterator_t iter_needle
;
211 mbui_init (iter_needle
, needle
);
212 if (mbui_avail (iter_needle
))
214 /* Minimizing the worst-case complexity:
215 Let n = mbslen(haystack), m = mbslen(needle).
216 The naïve algorithm is O(n*m) worst-case.
217 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
219 To achieve linear complexity and yet amortize the cost of the
220 memory allocation, we activate the Knuth-Morris-Pratt algorithm
221 only once the naïve algorithm has already run for some time; more
223 - the outer loop count is >= 10,
224 - the average number of comparisons per outer loop is >= 5,
225 - the total number of comparisons is >= m.
226 But we try it only once. If the memory allocation attempt failed,
227 we don't retry it. */
229 size_t outer_loop_count
= 0;
230 size_t comparison_count
= 0;
231 size_t last_ccount
= 0; /* last comparison count */
232 mbui_iterator_t iter_needle_last_ccount
; /* = needle + last_ccount */
235 mbui_iterator_t iter_haystack
;
237 mbui_init (iter_needle_last_ccount
, needle
);
239 mb_copy (&b
, &mbui_cur (iter_needle
));
241 b
.wc
= towlower (b
.wc
);
243 mbui_init (iter_haystack
, haystack
);
244 for (;; mbui_advance (iter_haystack
))
248 if (!mbui_avail (iter_haystack
))
252 /* See whether it's advisable to use an asymptotically faster
255 && outer_loop_count
>= 10
256 && comparison_count
>= 5 * outer_loop_count
)
258 /* See if needle + comparison_count now reaches the end of
260 size_t count
= comparison_count
- last_ccount
;
262 count
> 0 && mbui_avail (iter_needle_last_ccount
);
264 mbui_advance (iter_needle_last_ccount
);
265 last_ccount
= comparison_count
;
266 if (!mbui_avail (iter_needle_last_ccount
))
268 /* Try the Knuth-Morris-Pratt algorithm. */
271 knuth_morris_pratt_multibyte (haystack
, needle
,
274 return (char *) result
;
281 mb_copy (&c
, &mbui_cur (iter_haystack
));
283 c
.wc
= towlower (c
.wc
);
285 /* The first character matches. */
287 mbui_iterator_t rhaystack
;
288 mbui_iterator_t rneedle
;
290 memcpy (&rhaystack
, &iter_haystack
, sizeof (mbui_iterator_t
));
291 mbui_advance (rhaystack
);
293 mbui_init (rneedle
, needle
);
294 if (!mbui_avail (rneedle
))
296 mbui_advance (rneedle
);
298 for (;; mbui_advance (rhaystack
), mbui_advance (rneedle
))
300 if (!mbui_avail (rneedle
))
302 return (char *) mbui_cur_ptr (iter_haystack
);
303 if (!mbui_avail (rhaystack
))
307 if (!mb_caseequal (mbui_cur (rhaystack
),
309 /* Nothing in this round. */
316 return (char *) haystack
;
322 /* Minimizing the worst-case complexity:
323 Let n = strlen(haystack), m = strlen(needle).
324 The naïve algorithm is O(n*m) worst-case.
325 The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a
327 To achieve linear complexity and yet amortize the cost of the
328 memory allocation, we activate the Knuth-Morris-Pratt algorithm
329 only once the naïve algorithm has already run for some time; more
331 - the outer loop count is >= 10,
332 - the average number of comparisons per outer loop is >= 5,
333 - the total number of comparisons is >= m.
334 But we try it only once. If the memory allocation attempt failed,
335 we don't retry it. */
337 size_t outer_loop_count
= 0;
338 size_t comparison_count
= 0;
339 size_t last_ccount
= 0; /* last comparison count */
340 const char *needle_last_ccount
= needle
; /* = needle + last_ccount */
342 /* Speed up the following searches of needle by caching its first
344 unsigned char b
= TOLOWER ((unsigned char) *needle
);
349 if (*haystack
== '\0')
353 /* See whether it's advisable to use an asymptotically faster
356 && outer_loop_count
>= 10
357 && comparison_count
>= 5 * outer_loop_count
)
359 /* See if needle + comparison_count now reaches the end of
361 if (needle_last_ccount
!= NULL
)
363 needle_last_ccount
+=
364 strnlen (needle_last_ccount
,
365 comparison_count
- last_ccount
);
366 if (*needle_last_ccount
== '\0')
367 needle_last_ccount
= NULL
;
368 last_ccount
= comparison_count
;
370 if (needle_last_ccount
== NULL
)
372 /* Try the Knuth-Morris-Pratt algorithm. */
373 const unsigned char *result
;
375 knuth_morris_pratt ((const unsigned char *) haystack
,
376 (const unsigned char *) (needle
- 1),
380 return (char *) result
;
387 if (TOLOWER ((unsigned char) *haystack
) == b
)
388 /* The first character matches. */
390 const char *rhaystack
= haystack
+ 1;
391 const char *rneedle
= needle
;
393 for (;; rhaystack
++, rneedle
++)
395 if (*rneedle
== '\0')
397 return (char *) haystack
;
398 if (*rhaystack
== '\0')
402 if (TOLOWER ((unsigned char) *rhaystack
)
403 != TOLOWER ((unsigned char) *rneedle
))
404 /* Nothing in this round. */
411 return (char *) haystack
;