xref: /freebsd/lib/libc/string/strstr.c (revision 01dc206b226cccc287d6e1bfb2c1ff619728fc72)
158f0484fSRodney W. Grimes /*-
288521634SEd Maste  * Copyright (c) 2005-2014 Rich Felker, et al.
358f0484fSRodney W. Grimes  *
488521634SEd Maste  * Permission is hereby granted, free of charge, to any person obtaining
588521634SEd Maste  * a copy of this software and associated documentation files (the
688521634SEd Maste  * "Software"), to deal in the Software without restriction, including
788521634SEd Maste  * without limitation the rights to use, copy, modify, merge, publish,
888521634SEd Maste  * distribute, sublicense, and/or sell copies of the Software, and to
988521634SEd Maste  * permit persons to whom the Software is furnished to do so, subject to
1088521634SEd Maste  * the following conditions:
1158f0484fSRodney W. Grimes  *
1288521634SEd Maste  * The above copyright notice and this permission notice shall be
1388521634SEd Maste  * included in all copies or substantial portions of the Software.
1458f0484fSRodney W. Grimes  *
1588521634SEd Maste  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1688521634SEd Maste  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1788521634SEd Maste  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
1888521634SEd Maste  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
1988521634SEd Maste  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2088521634SEd Maste  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2188521634SEd 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>
2788521634SEd Maste #include <stdint.h>
2858f0484fSRodney W. Grimes 
2988521634SEd Maste static char *twobyte_strstr(const unsigned char *h, const unsigned char *n)
3058f0484fSRodney W. Grimes {
3188521634SEd Maste 	uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
3288521634SEd Maste 	for (h++; *h && hw != nw; hw = hw<<8 | *++h);
3388521634SEd Maste 	return *h ? (char *)h-1 : 0;
3458f0484fSRodney W. Grimes }
3588521634SEd Maste 
3688521634SEd Maste static char *threebyte_strstr(const unsigned char *h, const unsigned char *n)
3788521634SEd Maste {
3888521634SEd Maste 	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
3988521634SEd Maste 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
4088521634SEd Maste 	for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
4188521634SEd Maste 	return *h ? (char *)h-2 : 0;
4288521634SEd Maste }
4388521634SEd Maste 
4488521634SEd Maste static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n)
4588521634SEd Maste {
4688521634SEd Maste 	uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
4788521634SEd Maste 	uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
4888521634SEd Maste 	for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
4988521634SEd Maste 	return *h ? (char *)h-3 : 0;
5088521634SEd Maste }
5188521634SEd Maste 
5288521634SEd Maste #define MAX(a,b) ((a)>(b)?(a):(b))
5388521634SEd Maste #define MIN(a,b) ((a)<(b)?(a):(b))
5488521634SEd Maste 
5588521634SEd Maste #define BITOP(a,b,op) \
5688521634SEd Maste  ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
5788521634SEd Maste 
58*01dc206bSEd Maste /*
59*01dc206bSEd Maste  * Two Way string search algorithm, with a bad shift table applied to the last
60*01dc206bSEd Maste  * byte of the window. A bit array marks which entries in the shift table are
61*01dc206bSEd Maste  * initialized to avoid fully initializing a 1kb/2kb table.
62*01dc206bSEd Maste  *
63*01dc206bSEd Maste  * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
64*01dc206bSEd Maste  * Journal of the ACM 38(3):651-675
65*01dc206bSEd Maste  */
6688521634SEd Maste static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
6788521634SEd Maste {
6888521634SEd Maste 	const unsigned char *z;
6988521634SEd Maste 	size_t l, ip, jp, k, p, ms, p0, mem, mem0;
7088521634SEd Maste 	size_t byteset[32 / sizeof(size_t)] = { 0 };
7188521634SEd Maste 	size_t shift[256];
7288521634SEd Maste 
7388521634SEd Maste 	/* Computing length of needle and fill shift table */
7488521634SEd Maste 	for (l=0; n[l] && h[l]; l++)
7588521634SEd Maste 		BITOP(byteset, n[l], |=), shift[n[l]] = l+1;
7688521634SEd Maste 	if (n[l]) return 0; /* hit the end of h */
7788521634SEd Maste 
7888521634SEd Maste 	/* Compute maximal suffix */
7988521634SEd Maste 	ip = -1; jp = 0; k = p = 1;
8088521634SEd Maste 	while (jp+k<l) {
8188521634SEd Maste 		if (n[ip+k] == n[jp+k]) {
8288521634SEd Maste 			if (k == p) {
8388521634SEd Maste 				jp += p;
8488521634SEd Maste 				k = 1;
8588521634SEd Maste 			} else k++;
8688521634SEd Maste 		} else if (n[ip+k] > n[jp+k]) {
8788521634SEd Maste 			jp += k;
8888521634SEd Maste 			k = 1;
8988521634SEd Maste 			p = jp - ip;
9088521634SEd Maste 		} else {
9188521634SEd Maste 			ip = jp++;
9288521634SEd Maste 			k = p = 1;
9388521634SEd Maste 		}
9488521634SEd Maste 	}
9588521634SEd Maste 	ms = ip;
9688521634SEd Maste 	p0 = p;
9788521634SEd Maste 
9888521634SEd Maste 	/* And with the opposite comparison */
9988521634SEd Maste 	ip = -1; jp = 0; k = p = 1;
10088521634SEd Maste 	while (jp+k<l) {
10188521634SEd Maste 		if (n[ip+k] == n[jp+k]) {
10288521634SEd Maste 			if (k == p) {
10388521634SEd Maste 				jp += p;
10488521634SEd Maste 				k = 1;
10588521634SEd Maste 			} else k++;
10688521634SEd Maste 		} else if (n[ip+k] < n[jp+k]) {
10788521634SEd Maste 			jp += k;
10888521634SEd Maste 			k = 1;
10988521634SEd Maste 			p = jp - ip;
11088521634SEd Maste 		} else {
11188521634SEd Maste 			ip = jp++;
11288521634SEd Maste 			k = p = 1;
11388521634SEd Maste 		}
11488521634SEd Maste 	}
11588521634SEd Maste 	if (ip+1 > ms+1) ms = ip;
11688521634SEd Maste 	else p = p0;
11788521634SEd Maste 
11888521634SEd Maste 	/* Periodic needle? */
11988521634SEd Maste 	if (memcmp(n, n+p, ms+1)) {
12088521634SEd Maste 		mem0 = 0;
12188521634SEd Maste 		p = MAX(ms, l-ms-1) + 1;
12288521634SEd Maste 	} else mem0 = l-p;
12388521634SEd Maste 	mem = 0;
12488521634SEd Maste 
12588521634SEd Maste 	/* Initialize incremental end-of-haystack pointer */
12688521634SEd Maste 	z = h;
12788521634SEd Maste 
12888521634SEd Maste 	/* Search loop */
12988521634SEd Maste 	for (;;) {
13088521634SEd Maste 		/* Update incremental end-of-haystack pointer */
13188521634SEd Maste 		if (z-h < l) {
13288521634SEd Maste 			/* Fast estimate for MIN(l,63) */
13388521634SEd Maste 			size_t grow = l | 63;
13488521634SEd Maste 			const unsigned char *z2 = memchr(z, 0, grow);
13588521634SEd Maste 			if (z2) {
13688521634SEd Maste 				z = z2;
13788521634SEd Maste 				if (z-h < l) return 0;
13888521634SEd Maste 			} else z += grow;
13988521634SEd Maste 		}
14088521634SEd Maste 
14188521634SEd Maste 		/* Check last byte first; advance by shift on mismatch */
14288521634SEd Maste 		if (BITOP(byteset, h[l-1], &)) {
14388521634SEd Maste 			k = l-shift[h[l-1]];
14488521634SEd Maste 			//printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l);
14588521634SEd Maste 			if (k) {
14688521634SEd Maste 				if (mem0 && mem && k < p) k = l-p;
14788521634SEd Maste 				h += k;
14888521634SEd Maste 				mem = 0;
14988521634SEd Maste 				continue;
15088521634SEd Maste 			}
15188521634SEd Maste 		} else {
15288521634SEd Maste 			h += l;
15388521634SEd Maste 			mem = 0;
15488521634SEd Maste 			continue;
15588521634SEd Maste 		}
15688521634SEd Maste 
15788521634SEd Maste 		/* Compare right half */
15888521634SEd Maste 		for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
15988521634SEd Maste 		if (n[k]) {
16088521634SEd Maste 			h += k-ms;
16188521634SEd Maste 			mem = 0;
16288521634SEd Maste 			continue;
16388521634SEd Maste 		}
16488521634SEd Maste 		/* Compare left half */
16588521634SEd Maste 		for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
16688521634SEd Maste 		if (k <= mem) return (char *)h;
16788521634SEd Maste 		h += p;
16888521634SEd Maste 		mem = mem0;
16988521634SEd Maste 	}
17088521634SEd Maste }
17188521634SEd Maste 
17288521634SEd Maste char *strstr(const char *h, const char *n)
17388521634SEd Maste {
17488521634SEd Maste 	/* Return immediately on empty needle */
17588521634SEd Maste 	if (!n[0]) return (char *)h;
17688521634SEd Maste 
17788521634SEd Maste 	/* Use faster algorithms for short needles */
17888521634SEd Maste 	h = strchr(h, *n);
17988521634SEd Maste 	if (!h || !n[1]) return (char *)h;
18088521634SEd Maste 	if (!h[1]) return 0;
18188521634SEd Maste 	if (!n[2]) return twobyte_strstr((void *)h, (void *)n);
18288521634SEd Maste 	if (!h[2]) return 0;
18388521634SEd Maste 	if (!n[3]) return threebyte_strstr((void *)h, (void *)n);
18488521634SEd Maste 	if (!h[3]) return 0;
18588521634SEd Maste 	if (!n[4]) return fourbyte_strstr((void *)h, (void *)n);
18688521634SEd Maste 
18788521634SEd Maste 	return twoway_strstr((void *)h, (void *)n);
18858f0484fSRodney W. Grimes }
189