xref: /freebsd/lib/libc/string/strstr.c (revision 88521634e964e3da78a0ce54109cddb59bf3f099)
158f0484fSRodney W. Grimes /*-
2*88521634SEd Maste  * Copyright (c) 2005-2014 Rich Felker, et al.
358f0484fSRodney W. Grimes  *
4*88521634SEd Maste  * Permission is hereby granted, free of charge, to any person obtaining
5*88521634SEd Maste  * a copy of this software and associated documentation files (the
6*88521634SEd Maste  * "Software"), to deal in the Software without restriction, including
7*88521634SEd Maste  * without limitation the rights to use, copy, modify, merge, publish,
8*88521634SEd Maste  * distribute, sublicense, and/or sell copies of the Software, and to
9*88521634SEd Maste  * permit persons to whom the Software is furnished to do so, subject to
10*88521634SEd Maste  * the following conditions:
1158f0484fSRodney W. Grimes  *
12*88521634SEd Maste  * The above copyright notice and this permission notice shall be
13*88521634SEd Maste  * included in all copies or substantial portions of the Software.
1458f0484fSRodney W. Grimes  *
15*88521634SEd Maste  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16*88521634SEd Maste  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17*88521634SEd Maste  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18*88521634SEd Maste  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19*88521634SEd Maste  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20*88521634SEd Maste  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21*88521634SEd Maste  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2258f0484fSRodney W. Grimes  */
2358f0484fSRodney W. Grimes #include <sys/cdefs.h>
24de5fe5d5SDavid E. O'Brien __FBSDID("$FreeBSD$");
25de5fe5d5SDavid E. O'Brien 
2658f0484fSRodney W. Grimes #include <string.h>
27*88521634SEd Maste #include <stdint.h>
2858f0484fSRodney W. Grimes 
29*88521634SEd Maste static char *twobyte_strstr(const unsigned char *h, const unsigned char *n)
3058f0484fSRodney W. Grimes {
31*88521634SEd Maste 	uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
32*88521634SEd Maste 	for (h++; *h && hw != nw; hw = hw<<8 | *++h);
33*88521634SEd Maste 	return *h ? (char *)h-1 : 0;
3458f0484fSRodney W. Grimes }
35*88521634SEd Maste 
36*88521634SEd Maste static char *threebyte_strstr(const unsigned char *h, const unsigned char *n)
37*88521634SEd Maste {
38*88521634SEd Maste 	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
39*88521634SEd Maste 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
40*88521634SEd Maste 	for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
41*88521634SEd Maste 	return *h ? (char *)h-2 : 0;
42*88521634SEd Maste }
43*88521634SEd Maste 
44*88521634SEd Maste static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n)
45*88521634SEd Maste {
46*88521634SEd Maste 	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
47*88521634SEd Maste 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
48*88521634SEd Maste 	for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
49*88521634SEd Maste 	return *h ? (char *)h-3 : 0;
50*88521634SEd Maste }
51*88521634SEd Maste 
52*88521634SEd Maste #define MAX(a,b) ((a)>(b)?(a):(b))
53*88521634SEd Maste #define MIN(a,b) ((a)<(b)?(a):(b))
54*88521634SEd Maste 
55*88521634SEd Maste #define BITOP(a,b,op) \
56*88521634SEd Maste  ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
57*88521634SEd Maste 
58*88521634SEd Maste static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
59*88521634SEd Maste {
60*88521634SEd Maste 	const unsigned char *z;
61*88521634SEd Maste 	size_t l, ip, jp, k, p, ms, p0, mem, mem0;
62*88521634SEd Maste 	size_t byteset[32 / sizeof(size_t)] = { 0 };
63*88521634SEd Maste 	size_t shift[256];
64*88521634SEd Maste 
65*88521634SEd Maste 	/* Computing length of needle and fill shift table */
66*88521634SEd Maste 	for (l=0; n[l] && h[l]; l++)
67*88521634SEd Maste 		BITOP(byteset, n[l], |=), shift[n[l]] = l+1;
68*88521634SEd Maste 	if (n[l]) return 0; /* hit the end of h */
69*88521634SEd Maste 
70*88521634SEd Maste 	/* Compute maximal suffix */
71*88521634SEd Maste 	ip = -1; jp = 0; k = p = 1;
72*88521634SEd Maste 	while (jp+k<l) {
73*88521634SEd Maste 		if (n[ip+k] == n[jp+k]) {
74*88521634SEd Maste 			if (k == p) {
75*88521634SEd Maste 				jp += p;
76*88521634SEd Maste 				k = 1;
77*88521634SEd Maste 			} else k++;
78*88521634SEd Maste 		} else if (n[ip+k] > n[jp+k]) {
79*88521634SEd Maste 			jp += k;
80*88521634SEd Maste 			k = 1;
81*88521634SEd Maste 			p = jp - ip;
82*88521634SEd Maste 		} else {
83*88521634SEd Maste 			ip = jp++;
84*88521634SEd Maste 			k = p = 1;
85*88521634SEd Maste 		}
86*88521634SEd Maste 	}
87*88521634SEd Maste 	ms = ip;
88*88521634SEd Maste 	p0 = p;
89*88521634SEd Maste 
90*88521634SEd Maste 	/* And with the opposite comparison */
91*88521634SEd Maste 	ip = -1; jp = 0; k = p = 1;
92*88521634SEd Maste 	while (jp+k<l) {
93*88521634SEd Maste 		if (n[ip+k] == n[jp+k]) {
94*88521634SEd Maste 			if (k == p) {
95*88521634SEd Maste 				jp += p;
96*88521634SEd Maste 				k = 1;
97*88521634SEd Maste 			} else k++;
98*88521634SEd Maste 		} else if (n[ip+k] < n[jp+k]) {
99*88521634SEd Maste 			jp += k;
100*88521634SEd Maste 			k = 1;
101*88521634SEd Maste 			p = jp - ip;
102*88521634SEd Maste 		} else {
103*88521634SEd Maste 			ip = jp++;
104*88521634SEd Maste 			k = p = 1;
105*88521634SEd Maste 		}
106*88521634SEd Maste 	}
107*88521634SEd Maste 	if (ip+1 > ms+1) ms = ip;
108*88521634SEd Maste 	else p = p0;
109*88521634SEd Maste 
110*88521634SEd Maste 	/* Periodic needle? */
111*88521634SEd Maste 	if (memcmp(n, n+p, ms+1)) {
112*88521634SEd Maste 		mem0 = 0;
113*88521634SEd Maste 		p = MAX(ms, l-ms-1) + 1;
114*88521634SEd Maste 	} else mem0 = l-p;
115*88521634SEd Maste 	mem = 0;
116*88521634SEd Maste 
117*88521634SEd Maste 	/* Initialize incremental end-of-haystack pointer */
118*88521634SEd Maste 	z = h;
119*88521634SEd Maste 
120*88521634SEd Maste 	/* Search loop */
121*88521634SEd Maste 	for (;;) {
122*88521634SEd Maste 		/* Update incremental end-of-haystack pointer */
123*88521634SEd Maste 		if (z-h < l) {
124*88521634SEd Maste 			/* Fast estimate for MIN(l,63) */
125*88521634SEd Maste 			size_t grow = l | 63;
126*88521634SEd Maste 			const unsigned char *z2 = memchr(z, 0, grow);
127*88521634SEd Maste 			if (z2) {
128*88521634SEd Maste 				z = z2;
129*88521634SEd Maste 				if (z-h < l) return 0;
130*88521634SEd Maste 			} else z += grow;
131*88521634SEd Maste 		}
132*88521634SEd Maste 
133*88521634SEd Maste 		/* Check last byte first; advance by shift on mismatch */
134*88521634SEd Maste 		if (BITOP(byteset, h[l-1], &)) {
135*88521634SEd Maste 			k = l-shift[h[l-1]];
136*88521634SEd Maste 			//printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l);
137*88521634SEd Maste 			if (k) {
138*88521634SEd Maste 				if (mem0 && mem && k < p) k = l-p;
139*88521634SEd Maste 				h += k;
140*88521634SEd Maste 				mem = 0;
141*88521634SEd Maste 				continue;
142*88521634SEd Maste 			}
143*88521634SEd Maste 		} else {
144*88521634SEd Maste 			h += l;
145*88521634SEd Maste 			mem = 0;
146*88521634SEd Maste 			continue;
147*88521634SEd Maste 		}
148*88521634SEd Maste 
149*88521634SEd Maste 		/* Compare right half */
150*88521634SEd Maste 		for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
151*88521634SEd Maste 		if (n[k]) {
152*88521634SEd Maste 			h += k-ms;
153*88521634SEd Maste 			mem = 0;
154*88521634SEd Maste 			continue;
155*88521634SEd Maste 		}
156*88521634SEd Maste 		/* Compare left half */
157*88521634SEd Maste 		for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
158*88521634SEd Maste 		if (k <= mem) return (char *)h;
159*88521634SEd Maste 		h += p;
160*88521634SEd Maste 		mem = mem0;
161*88521634SEd Maste 	}
162*88521634SEd Maste }
163*88521634SEd Maste 
164*88521634SEd Maste char *strstr(const char *h, const char *n)
165*88521634SEd Maste {
166*88521634SEd Maste 	/* Return immediately on empty needle */
167*88521634SEd Maste 	if (!n[0]) return (char *)h;
168*88521634SEd Maste 
169*88521634SEd Maste 	/* Use faster algorithms for short needles */
170*88521634SEd Maste 	h = strchr(h, *n);
171*88521634SEd Maste 	if (!h || !n[1]) return (char *)h;
172*88521634SEd Maste 	if (!h[1]) return 0;
173*88521634SEd Maste 	if (!n[2]) return twobyte_strstr((void *)h, (void *)n);
174*88521634SEd Maste 	if (!h[2]) return 0;
175*88521634SEd Maste 	if (!n[3]) return threebyte_strstr((void *)h, (void *)n);
176*88521634SEd Maste 	if (!h[3]) return 0;
177*88521634SEd Maste 	if (!n[4]) return fourbyte_strstr((void *)h, (void *)n);
178*88521634SEd Maste 
179*88521634SEd Maste 	return twoway_strstr((void *)h, (void *)n);
18058f0484fSRodney W. Grimes }
181