1 /* Locate a substring in a wide string.
2 Copyright (C) 1999, 2011-2025 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake, 2008. */
19 #define AVAILABLE(h, h_l, j, n_l) \
20 (!MEMCHR0 ((h) + (h_l), (j) + (n_l) - (h_l)) \
21 && ((h_l) = (j) + (n_l)))
22 #include "wcs-two-way.h"
25 FUNC (const UNIT
*haystack_start
, const UNIT
*needle_start
)
27 const UNIT
*haystack
= haystack_start
;
28 const UNIT
*needle
= needle_start
;
29 size_t needle_len
; /* Length of NEEDLE. */
30 size_t haystack_len
; /* Known minimum length of HAYSTACK. */
31 bool ok
= true; /* True if NEEDLE is prefix of HAYSTACK. */
33 /* Determine length of NEEDLE, and in the process, make sure
34 HAYSTACK is at least as long (no point processing all of a long
35 NEEDLE if HAYSTACK is too short). */
36 while (*haystack
&& *needle
)
37 ok
&= *haystack
++ == *needle
++;
41 return (RETURN_TYPE
) haystack_start
;
43 /* Reduce the size of haystack using STRCHR, since it has a smaller
44 linear coefficient than the Two-Way algorithm. */
45 needle_len
= needle
- needle_start
;
46 haystack
= STRCHR (haystack_start
+ 1, *needle_start
);
47 if (!haystack
|| __builtin_expect (needle_len
== 1, 0))
48 return (RETURN_TYPE
) haystack
;
50 haystack_len
= (haystack
> haystack_start
+ needle_len
? 1
51 : needle_len
+ haystack_start
- haystack
);
53 /* Perform the search. */
54 return two_way_short_needle (haystack
, haystack_len
,