1 /* Return the offset of one string within another.
2 Copyright (C) 1994-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
25 #define RETURN_TYPE char *
26 #define AVAILABLE(h, h_l, j, n_l) \
27 (((j) + (n_l) <= (h_l)) \
28 || ((h_l) += __strnlen ((void*)((h) + (h_l)), (n_l) + 512), \
29 (j) + (n_l) <= (h_l)))
30 #include "str-two-way.h"
39 strstr2 (const unsigned char *hs
, const unsigned char *ne
)
41 uint32_t h1
= (ne
[0] << 16) | ne
[1];
43 for (int c
= hs
[0]; h1
!= h2
&& c
!= 0; c
= *++hs
)
45 return h1
== h2
? (char *)hs
- 2 : NULL
;
49 strstr3 (const unsigned char *hs
, const unsigned char *ne
)
51 uint32_t h1
= ((uint32_t)ne
[0] << 24) | (ne
[1] << 16) | (ne
[2] << 8);
53 for (int c
= hs
[0]; h1
!= h2
&& c
!= 0; c
= *++hs
)
55 return h1
== h2
? (char *)hs
- 3 : NULL
;
58 /* Hash character pairs so a small shift table can be used. All bits of
59 p[0] are included, but not all bits from p[-1]. So if two equal hashes
60 match on p[-1], p[0] matches too. Hash collisions are harmless and result
62 #define hash2(p) (((size_t)(p)[0] - ((size_t)(p)[-1] << 3)) % sizeof (shift))
64 /* Fast strstr algorithm with guaranteed linear-time performance.
65 Small needles up to size 3 use a dedicated linear search. Longer needles
66 up to size 256 use a novel modified Horspool algorithm. It hashes pairs
67 of characters to quickly skip past mismatches. The main search loop only
68 exits if the last 2 characters match, avoiding unnecessary calls to memcmp
69 and allowing for a larger skip if there is no match. A self-adapting
70 filtering check is used to quickly detect mismatches in long needles.
71 By limiting the needle length to 256, the shift table can be reduced to 8
72 bits per entry, lowering preprocessing overhead and minimizing cache effects.
73 The limit also implies worst-case performance is linear.
74 Needles larger than 256 characters use the linear-time Two-Way algorithm. */
76 STRSTR (const char *haystack
, const char *needle
)
78 const unsigned char *hs
= (const unsigned char *) haystack
;
79 const unsigned char *ne
= (const unsigned char *) needle
;
81 /* Handle short needle special cases first. */
84 hs
= (const unsigned char *)strchr ((const char*)hs
, ne
[0]);
85 if (hs
== NULL
|| ne
[1] == '\0')
88 return strstr2 (hs
, ne
);
90 return strstr3 (hs
, ne
);
92 /* Ensure haystack length is at least as long as needle length.
93 Since a match may occur early on in a huge haystack, use strnlen
94 and read ahead a few cachelines for improved performance. */
95 size_t ne_len
= strlen ((const char*)ne
);
96 size_t hs_len
= __strnlen ((const char*)hs
, ne_len
| 512);
100 /* Check whether we have a match. This improves performance since we
101 avoid initialization overheads. */
102 if (memcmp (hs
, ne
, ne_len
) == 0)
105 /* Use Two-Way algorithm for very long needles. */
106 if (__glibc_unlikely (ne_len
> 256))
107 return two_way_long_needle (hs
, hs_len
, ne
, ne_len
);
109 const unsigned char *end
= hs
+ hs_len
- ne_len
;
112 size_t m1
= ne_len
- 1;
115 /* Initialize bad character shift hash table. */
116 memset (shift
, 0, sizeof (shift
));
117 for (int i
= 1; i
< m1
; i
++)
118 shift
[hash2 (ne
+ i
)] = i
;
119 /* Shift1 is the amount we can skip after matching the hash of the
120 needle end but not the full needle. */
121 shift1
= m1
- shift
[hash2 (ne
+ m1
)];
122 shift
[hash2 (ne
+ m1
)] = m1
;
126 if (__glibc_unlikely (hs
> end
))
128 end
+= __strnlen ((const char*)end
+ m1
+ 1, 2048);
133 /* Skip past character pairs not in the needle. */
137 tmp
= shift
[hash2 (hs
)];
139 while (tmp
== 0 && hs
<= end
);
141 /* If the match is not at the end of the needle, shift to the end
142 and continue until we match the hash of the needle end. */
147 /* Hash of the last 2 characters matches. If the needle is long,
148 try to quickly filter out mismatches. */
149 if (m1
< 15 || memcmp (hs
+ offset
, ne
+ offset
, 8) == 0)
151 if (memcmp (hs
, ne
, m1
) == 0)
154 /* Adjust filter offset when it doesn't find the mismatch. */
155 offset
= (offset
>= 8 ? offset
: m1
) - 8;
158 /* Skip based on matching the hash of the needle end. */
162 libc_hidden_builtin_def (strstr
)