| 1 |
/* Searching in a string. -*- coding: utf-8 -*- |
| 2 |
Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 3 |
Written by Bruno Haible <[email protected]>, 2005. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation, either version 3 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file 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 Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Don't use the const-improved function macros in this compilation unit. */ |
| 19 |
#define _GL_NO_CONST_GENERICS |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
/* Specification. */ |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include <stddef.h> /* for NULL, in case a nonstandard string.h lacks it */ |
| 27 |
#include <stdlib.h> |
| 28 |
|
| 29 |
#include "malloca.h" |
| 30 |
|
| 31 |
#if GNULIB_MCEL_PREFER |
| 32 |
# include "mcel.h" |
| 33 |
typedef mcel_t mbchar_t; |
| 34 |
static bool mb_equal (mcel_t a, mcel_t b) { return mcel_cmp (a, b) == 0; } |
| 35 |
#else |
| 36 |
# include "mbuiter.h" |
| 37 |
#endif |
| 38 |
|
| 39 |
/* Knuth-Morris-Pratt algorithm. */ |
| 40 |
#define UNIT unsigned char |
| 41 |
#define CANON_ELEMENT(c) c |
| 42 |
#include "str-kmp.h" |
| 43 |
|
| 44 |
/* Knuth-Morris-Pratt algorithm. |
| 45 |
See https://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm |
| 46 |
Return a boolean indicating success: |
| 47 |
Return true and set *RESULTP if the search was completed. |
| 48 |
Return false if it was aborted because not enough memory was available. */ |
| 49 |
static bool |
| 50 |
knuth_morris_pratt_multibyte (const char *haystack, const char *needle, |
| 51 |
const char **resultp) |
| 52 |
{ |
| 53 |
size_t m = mbslen (needle); |
| 54 |
mbchar_t *needle_mbchars; |
| 55 |
size_t extra_align = (alignof (mbchar_t) < alignof (size_t) |
| 56 |
? alignof (size_t) - alignof (mbchar_t) |
| 57 |
: 0); |
| 58 |
|
| 59 |
/* Allocate room for needle_mbchars and the table. */ |
| 60 |
void *memory = nmalloca (m + !!extra_align, |
| 61 |
sizeof (mbchar_t) + sizeof (size_t)); |
| 62 |
void *table_memory; |
| 63 |
if (memory == NULL) |
| 64 |
return false; |
| 65 |
needle_mbchars = memory; |
| 66 |
table_memory = needle_mbchars + m; |
| 67 |
char *aligned = table_memory; |
| 68 |
aligned += extra_align; |
| 69 |
aligned -= (uintptr_t) aligned % alignof (size_t); |
| 70 |
size_t *table = table_memory = aligned; |
| 71 |
|
| 72 |
/* Fill needle_mbchars. */ |
| 73 |
#if GNULIB_MCEL_PREFER |
| 74 |
for (size_t j = 0; *needle; needle += needle_mbchars[j++].len) |
| 75 |
needle_mbchars[j] = mcel_scanz (needle); |
| 76 |
#else |
| 77 |
{ |
| 78 |
mbui_iterator_t iter; |
| 79 |
size_t j; |
| 80 |
|
| 81 |
j = 0; |
| 82 |
for (mbui_init (iter, needle); mbui_avail (iter); mbui_advance (iter), j++) |
| 83 |
mb_copy (&needle_mbchars[j], &mbui_cur (iter)); |
| 84 |
} |
| 85 |
#endif |
| 86 |
|
| 87 |
/* Fill the table. |
| 88 |
For 0 < i < m: |
| 89 |
0 < table[i] <= i is defined such that |
| 90 |
forall 0 < x < table[i]: needle[x..i-1] != needle[0..i-1-x], |
| 91 |
and table[i] is as large as possible with this property. |
| 92 |
This implies: |
| 93 |
1) For 0 < i < m: |
| 94 |
If table[i] < i, |
| 95 |
needle[table[i]..i-1] = needle[0..i-1-table[i]]. |
| 96 |
2) For 0 < i < m: |
| 97 |
rhaystack[0..i-1] == needle[0..i-1] |
| 98 |
and exists h, i <= h < m: rhaystack[h] != needle[h] |
| 99 |
implies |
| 100 |
forall 0 <= x < table[i]: rhaystack[x..x+m-1] != needle[0..m-1]. |
| 101 |
table[0] remains uninitialized. */ |
| 102 |
{ |
| 103 |
size_t i, j; |
| 104 |
|
| 105 |
/* i = 1: Nothing to verify for x = 0. */ |
| 106 |
table[1] = 1; |
| 107 |
j = 0; |
| 108 |
|
| 109 |
for (i = 2; i < m; i++) |
| 110 |
{ |
| 111 |
/* Here: j = i-1 - table[i-1]. |
| 112 |
The inequality needle[x..i-1] != needle[0..i-1-x] is known to hold |
| 113 |
for x < table[i-1], by induction. |
| 114 |
Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */ |
| 115 |
mbchar_t *b = &needle_mbchars[i - 1]; |
| 116 |
|
| 117 |
for (;;) |
| 118 |
{ |
| 119 |
/* Invariants: The inequality needle[x..i-1] != needle[0..i-1-x] |
| 120 |
is known to hold for x < i-1-j. |
| 121 |
Furthermore, if j>0: needle[i-1-j..i-2] = needle[0..j-1]. */ |
| 122 |
if (mb_equal (*b, needle_mbchars[j])) |
| 123 |
{ |
| 124 |
/* Set table[i] := i-1-j. */ |
| 125 |
table[i] = i - ++j; |
| 126 |
break; |
| 127 |
} |
| 128 |
/* The inequality needle[x..i-1] != needle[0..i-1-x] also holds |
| 129 |
for x = i-1-j, because |
| 130 |
needle[i-1] != needle[j] = needle[i-1-x]. */ |
| 131 |
if (j == 0) |
| 132 |
{ |
| 133 |
/* The inequality holds for all possible x. */ |
| 134 |
table[i] = i; |
| 135 |
break; |
| 136 |
} |
| 137 |
/* The inequality needle[x..i-1] != needle[0..i-1-x] also holds |
| 138 |
for i-1-j < x < i-1-j+table[j], because for these x: |
| 139 |
needle[x..i-2] |
| 140 |
= needle[x-(i-1-j)..j-1] |
| 141 |
!= needle[0..j-1-(x-(i-1-j))] (by definition of table[j]) |
| 142 |
= needle[0..i-2-x], |
| 143 |
hence needle[x..i-1] != needle[0..i-1-x]. |
| 144 |
Furthermore |
| 145 |
needle[i-1-j+table[j]..i-2] |
| 146 |
= needle[table[j]..j-1] |
| 147 |
= needle[0..j-1-table[j]] (by definition of table[j]). */ |
| 148 |
j = j - table[j]; |
| 149 |
} |
| 150 |
/* Here: j = i - table[i]. */ |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/* Search, using the table to accelerate the processing. */ |
| 155 |
{ |
| 156 |
#if GNULIB_MCEL_PREFER |
| 157 |
size_t j; |
| 158 |
char const *rhaystack = haystack; |
| 159 |
char const *phaystack = haystack; |
| 160 |
|
| 161 |
j = 0; |
| 162 |
/* Invariant: phaystack = rhaystack + j. */ |
| 163 |
for (;;) |
| 164 |
{ |
| 165 |
if (!*phaystack) |
| 166 |
{ |
| 167 |
rhaystack = NULL; |
| 168 |
break; |
| 169 |
} |
| 170 |
mcel_t g = mcel_scanz (phaystack); |
| 171 |
if (mcel_cmp (needle_mbchars[j], g) == 0) |
| 172 |
{ |
| 173 |
j++; |
| 174 |
/* Exit loop successfully if the entire needle has been found. */ |
| 175 |
if (j == m) |
| 176 |
break; |
| 177 |
phaystack += g.len; |
| 178 |
} |
| 179 |
else if (j == 0) |
| 180 |
{ |
| 181 |
/* Found a mismatch at needle[0] already. */ |
| 182 |
rhaystack += mcel_scanz (rhaystack).len; |
| 183 |
phaystack += g.len; |
| 184 |
} |
| 185 |
else |
| 186 |
{ |
| 187 |
/* Found a match of needle[0..j-1], mismatch at needle[j]. */ |
| 188 |
size_t count = table[j]; |
| 189 |
j -= count; |
| 190 |
for (; count != 0; count--) |
| 191 |
rhaystack += mcel_scanz (rhaystack).len; |
| 192 |
} |
| 193 |
} |
| 194 |
*resultp = rhaystack; |
| 195 |
#else |
| 196 |
size_t j; |
| 197 |
mbui_iterator_t rhaystack; |
| 198 |
mbui_iterator_t phaystack; |
| 199 |
|
| 200 |
*resultp = NULL; |
| 201 |
j = 0; |
| 202 |
mbui_init (rhaystack, haystack); |
| 203 |
mbui_init (phaystack, haystack); |
| 204 |
/* Invariant: phaystack = rhaystack + j. */ |
| 205 |
while (mbui_avail (phaystack)) |
| 206 |
if (mb_equal (needle_mbchars[j], mbui_cur (phaystack))) |
| 207 |
{ |
| 208 |
j++; |
| 209 |
mbui_advance (phaystack); |
| 210 |
if (j == m) |
| 211 |
{ |
| 212 |
/* The entire needle has been found. */ |
| 213 |
*resultp = mbui_cur_ptr (rhaystack); |
| 214 |
break; |
| 215 |
} |
| 216 |
} |
| 217 |
else if (j > 0) |
| 218 |
{ |
| 219 |
/* Found a match of needle[0..j-1], mismatch at needle[j]. */ |
| 220 |
size_t count = table[j]; |
| 221 |
j -= count; |
| 222 |
for (; count > 0; count--) |
| 223 |
{ |
| 224 |
if (!mbui_avail (rhaystack)) |
| 225 |
abort (); |
| 226 |
mbui_advance (rhaystack); |
| 227 |
} |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
/* Found a mismatch at needle[0] already. */ |
| 232 |
if (!mbui_avail (rhaystack)) |
| 233 |
abort (); |
| 234 |
mbui_advance (rhaystack); |
| 235 |
mbui_advance (phaystack); |
| 236 |
} |
| 237 |
#endif |
| 238 |
} |
| 239 |
|
| 240 |
freea (memory); |
| 241 |
return true; |
| 242 |
} |
| 243 |
|
| 244 |
/* Find the first occurrence of the character string NEEDLE in the character |
| 245 |
string HAYSTACK. Return NULL if NEEDLE is not found in HAYSTACK. */ |
| 246 |
char * |
| 247 |
mbsstr (const char *haystack, const char *needle) |
| 248 |
{ |
| 249 |
/* Be careful not to look at the entire extent of haystack or needle |
| 250 |
until needed. This is useful because of these two cases: |
| 251 |
- haystack may be very long, and a match of needle found early, |
| 252 |
- needle may be very long, and not even a short initial segment of |
| 253 |
needle may be found in haystack. */ |
| 254 |
if (MB_CUR_MAX > 1) |
| 255 |
{ |
| 256 |
#if GNULIB_MCEL_PREFER |
| 257 |
if (!*needle) |
| 258 |
return (char *) haystack; |
| 259 |
|
| 260 |
mcel_t ng = mcel_scanz (needle); |
| 261 |
|
| 262 |
/* Minimizing the worst-case complexity: |
| 263 |
Let n = mbslen(haystack), m = mbslen(needle). |
| 264 |
The naïve algorithm is O(n*m) worst-case. |
| 265 |
The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a |
| 266 |
memory allocation. |
| 267 |
To achieve linear complexity and yet amortize the cost of the |
| 268 |
memory allocation, we activate the Knuth-Morris-Pratt algorithm |
| 269 |
only once the naïve algorithm has already run for some time; more |
| 270 |
precisely, when |
| 271 |
- the outer loop count is >= 10, |
| 272 |
- the average number of comparisons per outer loop is >= 5, |
| 273 |
- the total number of comparisons is >= m. |
| 274 |
But we try it only once. If the memory allocation attempt failed, |
| 275 |
we don't retry it. */ |
| 276 |
bool try_kmp = true; |
| 277 |
size_t outer_loop_count = 0; |
| 278 |
size_t comparison_count = 0; |
| 279 |
|
| 280 |
/* Last comparison count, and needle + last_ccount. */ |
| 281 |
size_t last_ccount = 0; |
| 282 |
char const *iter_needle_last_ccount = needle; |
| 283 |
|
| 284 |
char const *iter_haystack = haystack; |
| 285 |
|
| 286 |
for (mcel_t hg; *iter_haystack; iter_haystack += hg.len) |
| 287 |
{ |
| 288 |
/* See whether it's advisable to use an asymptotically faster |
| 289 |
algorithm. */ |
| 290 |
if (try_kmp |
| 291 |
&& outer_loop_count >= 10 |
| 292 |
&& comparison_count >= 5 * outer_loop_count) |
| 293 |
{ |
| 294 |
/* See if needle + comparison_count now reaches the end of |
| 295 |
needle. */ |
| 296 |
size_t count = comparison_count - last_ccount; |
| 297 |
for (; |
| 298 |
count > 0 && *iter_needle_last_ccount; |
| 299 |
count--) |
| 300 |
iter_needle_last_ccount |
| 301 |
+= mcel_scanz (iter_needle_last_ccount).len; |
| 302 |
last_ccount = comparison_count; |
| 303 |
if (!*iter_needle_last_ccount) |
| 304 |
{ |
| 305 |
char const *result; |
| 306 |
if (knuth_morris_pratt_multibyte (haystack, needle, |
| 307 |
&result)) |
| 308 |
return (char *) result; |
| 309 |
try_kmp = false; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
outer_loop_count++; |
| 314 |
comparison_count++; |
| 315 |
hg = mcel_scanz (iter_haystack); |
| 316 |
if (mcel_cmp (hg, ng) == 0) |
| 317 |
/* The first character matches. */ |
| 318 |
{ |
| 319 |
char const *rhaystack = iter_haystack + hg.len; |
| 320 |
char const *rneedle = needle + ng.len; |
| 321 |
mcel_t rhg, rng; |
| 322 |
do |
| 323 |
{ |
| 324 |
if (!*rneedle) |
| 325 |
return (char *) iter_haystack; |
| 326 |
if (!*rhaystack) |
| 327 |
return NULL; |
| 328 |
rhg = mcel_scanz (rhaystack); rhaystack += rhg.len; |
| 329 |
rng = mcel_scanz (rneedle); rneedle += rng.len; |
| 330 |
comparison_count++; |
| 331 |
} |
| 332 |
while (mcel_cmp (rhg, rng) == 0); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return NULL; |
| 337 |
#else |
| 338 |
mbui_iterator_t iter_needle; |
| 339 |
|
| 340 |
mbui_init (iter_needle, needle); |
| 341 |
if (mbui_avail (iter_needle)) |
| 342 |
{ |
| 343 |
/* Minimizing the worst-case complexity: |
| 344 |
Let n = mbslen(haystack), m = mbslen(needle). |
| 345 |
The naïve algorithm is O(n*m) worst-case. |
| 346 |
The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a |
| 347 |
memory allocation. |
| 348 |
To achieve linear complexity and yet amortize the cost of the |
| 349 |
memory allocation, we activate the Knuth-Morris-Pratt algorithm |
| 350 |
only once the naïve algorithm has already run for some time; more |
| 351 |
precisely, when |
| 352 |
- the outer loop count is >= 10, |
| 353 |
- the average number of comparisons per outer loop is >= 5, |
| 354 |
- the total number of comparisons is >= m. |
| 355 |
But we try it only once. If the memory allocation attempt failed, |
| 356 |
we don't retry it. */ |
| 357 |
bool try_kmp = true; |
| 358 |
size_t outer_loop_count = 0; |
| 359 |
size_t comparison_count = 0; |
| 360 |
size_t last_ccount = 0; /* last comparison count */ |
| 361 |
mbui_iterator_t iter_needle_last_ccount; /* = needle + last_ccount */ |
| 362 |
|
| 363 |
mbui_iterator_t iter_haystack; |
| 364 |
|
| 365 |
mbui_init (iter_needle_last_ccount, needle); |
| 366 |
mbui_init (iter_haystack, haystack); |
| 367 |
for (;; mbui_advance (iter_haystack)) |
| 368 |
{ |
| 369 |
if (!mbui_avail (iter_haystack)) |
| 370 |
/* No match. */ |
| 371 |
return NULL; |
| 372 |
|
| 373 |
/* See whether it's advisable to use an asymptotically faster |
| 374 |
algorithm. */ |
| 375 |
if (try_kmp |
| 376 |
&& outer_loop_count >= 10 |
| 377 |
&& comparison_count >= 5 * outer_loop_count) |
| 378 |
{ |
| 379 |
/* See if needle + comparison_count now reaches the end of |
| 380 |
needle. */ |
| 381 |
size_t count = comparison_count - last_ccount; |
| 382 |
for (; |
| 383 |
count > 0 && mbui_avail (iter_needle_last_ccount); |
| 384 |
count--) |
| 385 |
mbui_advance (iter_needle_last_ccount); |
| 386 |
last_ccount = comparison_count; |
| 387 |
if (!mbui_avail (iter_needle_last_ccount)) |
| 388 |
{ |
| 389 |
/* Try the Knuth-Morris-Pratt algorithm. */ |
| 390 |
const char *result; |
| 391 |
bool success = |
| 392 |
knuth_morris_pratt_multibyte (haystack, needle, |
| 393 |
&result); |
| 394 |
if (success) |
| 395 |
return (char *) result; |
| 396 |
try_kmp = false; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
outer_loop_count++; |
| 401 |
comparison_count++; |
| 402 |
if (mb_equal (mbui_cur (iter_haystack), mbui_cur (iter_needle))) |
| 403 |
/* The first character matches. */ |
| 404 |
{ |
| 405 |
mbui_iterator_t rhaystack; |
| 406 |
mbui_iterator_t rneedle; |
| 407 |
|
| 408 |
memcpy (&rhaystack, &iter_haystack, sizeof (mbui_iterator_t)); |
| 409 |
mbui_advance (rhaystack); |
| 410 |
|
| 411 |
mbui_init (rneedle, needle); |
| 412 |
if (!mbui_avail (rneedle)) |
| 413 |
abort (); |
| 414 |
mbui_advance (rneedle); |
| 415 |
|
| 416 |
for (;; mbui_advance (rhaystack), mbui_advance (rneedle)) |
| 417 |
{ |
| 418 |
if (!mbui_avail (rneedle)) |
| 419 |
/* Found a match. */ |
| 420 |
return (char *) mbui_cur_ptr (iter_haystack); |
| 421 |
if (!mbui_avail (rhaystack)) |
| 422 |
/* No match. */ |
| 423 |
return NULL; |
| 424 |
comparison_count++; |
| 425 |
if (!mb_equal (mbui_cur (rhaystack), mbui_cur (rneedle))) |
| 426 |
/* Nothing in this round. */ |
| 427 |
break; |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
else |
| 433 |
return (char *) haystack; |
| 434 |
#endif |
| 435 |
} |
| 436 |
else |
| 437 |
{ |
| 438 |
if (*needle != '\0') |
| 439 |
{ |
| 440 |
/* Minimizing the worst-case complexity: |
| 441 |
Let n = strlen(haystack), m = strlen(needle). |
| 442 |
The naïve algorithm is O(n*m) worst-case. |
| 443 |
The Knuth-Morris-Pratt algorithm is O(n) worst-case but it needs a |
| 444 |
memory allocation. |
| 445 |
To achieve linear complexity and yet amortize the cost of the |
| 446 |
memory allocation, we activate the Knuth-Morris-Pratt algorithm |
| 447 |
only once the naïve algorithm has already run for some time; more |
| 448 |
precisely, when |
| 449 |
- the outer loop count is >= 10, |
| 450 |
- the average number of comparisons per outer loop is >= 5, |
| 451 |
- the total number of comparisons is >= m. |
| 452 |
But we try it only once. If the memory allocation attempt failed, |
| 453 |
we don't retry it. */ |
| 454 |
bool try_kmp = true; |
| 455 |
size_t outer_loop_count = 0; |
| 456 |
size_t comparison_count = 0; |
| 457 |
size_t last_ccount = 0; /* last comparison count */ |
| 458 |
const char *needle_last_ccount = needle; /* = needle + last_ccount */ |
| 459 |
|
| 460 |
/* Speed up the following searches of needle by caching its first |
| 461 |
character. */ |
| 462 |
char b = *needle++; |
| 463 |
|
| 464 |
for (;; haystack++) |
| 465 |
{ |
| 466 |
if (*haystack == '\0') |
| 467 |
/* No match. */ |
| 468 |
return NULL; |
| 469 |
|
| 470 |
/* See whether it's advisable to use an asymptotically faster |
| 471 |
algorithm. */ |
| 472 |
if (try_kmp |
| 473 |
&& outer_loop_count >= 10 |
| 474 |
&& comparison_count >= 5 * outer_loop_count) |
| 475 |
{ |
| 476 |
/* See if needle + comparison_count now reaches the end of |
| 477 |
needle. */ |
| 478 |
if (needle_last_ccount != NULL) |
| 479 |
{ |
| 480 |
needle_last_ccount += |
| 481 |
strnlen (needle_last_ccount, |
| 482 |
comparison_count - last_ccount); |
| 483 |
if (*needle_last_ccount == '\0') |
| 484 |
needle_last_ccount = NULL; |
| 485 |
last_ccount = comparison_count; |
| 486 |
} |
| 487 |
if (needle_last_ccount == NULL) |
| 488 |
{ |
| 489 |
/* Try the Knuth-Morris-Pratt algorithm. */ |
| 490 |
const unsigned char *result; |
| 491 |
bool success = |
| 492 |
knuth_morris_pratt ((const unsigned char *) haystack, |
| 493 |
(const unsigned char *) (needle - 1), |
| 494 |
strlen (needle - 1), |
| 495 |
&result); |
| 496 |
if (success) |
| 497 |
return (char *) result; |
| 498 |
try_kmp = false; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
outer_loop_count++; |
| 503 |
comparison_count++; |
| 504 |
if (*haystack == b) |
| 505 |
/* The first character matches. */ |
| 506 |
{ |
| 507 |
const char *rhaystack = haystack + 1; |
| 508 |
const char *rneedle = needle; |
| 509 |
|
| 510 |
for (;; rhaystack++, rneedle++) |
| 511 |
{ |
| 512 |
if (*rneedle == '\0') |
| 513 |
/* Found a match. */ |
| 514 |
return (char *) haystack; |
| 515 |
if (*rhaystack == '\0') |
| 516 |
/* No match. */ |
| 517 |
return NULL; |
| 518 |
comparison_count++; |
| 519 |
if (*rhaystack != *rneedle) |
| 520 |
/* Nothing in this round. */ |
| 521 |
break; |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
} |
| 526 |
else |
| 527 |
return (char *) haystack; |
| 528 |
} |
| 529 |
} |