2 * Copyright (c) 2005-2014 Rich Felker, et al.
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
29 static char *twobyte_strstr(const unsigned char *h, const unsigned char *n)
31 uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
32 for (h++; *h && hw != nw; hw = hw<<8 | *++h);
33 return *h ? (char *)h-1 : 0;
36 static char *threebyte_strstr(const unsigned char *h, const unsigned char *n)
38 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
39 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
40 for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
41 return *h ? (char *)h-2 : 0;
44 static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n)
46 uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
47 uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
48 for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
49 return *h ? (char *)h-3 : 0;
52 #define MAX(a,b) ((a)>(b)?(a):(b))
53 #define MIN(a,b) ((a)<(b)?(a):(b))
55 #define BITOP(a,b,op) \
56 ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
59 * Two Way string search algorithm, with a bad shift table applied to the last
60 * byte of the window. A bit array marks which entries in the shift table are
61 * initialized to avoid fully initializing a 1kb/2kb table.
63 * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
64 * Journal of the ACM 38(3):651-675
66 static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
68 const unsigned char *z;
69 size_t l, ip, jp, k, p, ms, p0, mem, mem0;
70 size_t byteset[32 / sizeof(size_t)] = { 0 };
73 /* Computing length of needle and fill shift table */
74 for (l=0; n[l] && h[l]; l++)
75 BITOP(byteset, n[l], |=), shift[n[l]] = l+1;
76 if (n[l]) return 0; /* hit the end of h */
78 /* Compute maximal suffix */
79 ip = -1; jp = 0; k = p = 1;
81 if (n[ip+k] == n[jp+k]) {
86 } else if (n[ip+k] > n[jp+k]) {
98 /* And with the opposite comparison */
99 ip = -1; jp = 0; k = p = 1;
101 if (n[ip+k] == n[jp+k]) {
106 } else if (n[ip+k] < n[jp+k]) {
115 if (ip+1 > ms+1) ms = ip;
118 /* Periodic needle? */
119 if (memcmp(n, n+p, ms+1)) {
121 p = MAX(ms, l-ms-1) + 1;
125 /* Initialize incremental end-of-haystack pointer */
130 /* Update incremental end-of-haystack pointer */
132 /* Fast estimate for MIN(l,63) */
133 size_t grow = l | 63;
134 const unsigned char *z2 = memchr(z, 0, grow);
137 if (z-h < l) return 0;
141 /* Check last byte first; advance by shift on mismatch */
142 if (BITOP(byteset, h[l-1], &)) {
144 //printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l);
146 if (mem0 && mem && k < p) k = l-p;
157 /* Compare right half */
158 for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
164 /* Compare left half */
165 for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
166 if (k <= mem) return (char *)h;
172 char *strstr(const char *h, const char *n)
174 /* Return immediately on empty needle */
175 if (!n[0]) return (char *)h;
177 /* Use faster algorithms for short needles */
179 if (!h || !n[1]) return (char *)h;
181 if (!n[2]) return twobyte_strstr((void *)h, (void *)n);
183 if (!n[3]) return threebyte_strstr((void *)h, (void *)n);
185 if (!n[4]) return fourbyte_strstr((void *)h, (void *)n);
187 return twoway_strstr((void *)h, (void *)n);