]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/string/strstr.c
Import Amazon Elastic Network Adapter (ENA) HAL to sys/contrib/
[FreeBSD/FreeBSD.git] / lib / libc / string / strstr.c
1 /*-
2  * Copyright (c) 2005-2014 Rich Felker, et al.
3  *
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:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
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.
22  */
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
25
26 #include <string.h>
27 #include <stdint.h>
28
29 static char *twobyte_strstr(const unsigned char *h, const unsigned char *n)
30 {
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;
34 }
35
36 static char *threebyte_strstr(const unsigned char *h, const unsigned char *n)
37 {
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;
42 }
43
44 static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n)
45 {
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;
50 }
51
52 #define MAX(a,b) ((a)>(b)?(a):(b))
53 #define MIN(a,b) ((a)<(b)?(a):(b))
54
55 #define BITOP(a,b,op) \
56  ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
57
58 /*
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.
62  *
63  * Reference: CROCHEMORE M., PERRIN D., 1991, Two-way string-matching,
64  * Journal of the ACM 38(3):651-675
65  */
66 static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
67 {
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 };
71         size_t shift[256];
72
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 */
77
78         /* Compute maximal suffix */
79         ip = -1; jp = 0; k = p = 1;
80         while (jp+k<l) {
81                 if (n[ip+k] == n[jp+k]) {
82                         if (k == p) {
83                                 jp += p;
84                                 k = 1;
85                         } else k++;
86                 } else if (n[ip+k] > n[jp+k]) {
87                         jp += k;
88                         k = 1;
89                         p = jp - ip;
90                 } else {
91                         ip = jp++;
92                         k = p = 1;
93                 }
94         }
95         ms = ip;
96         p0 = p;
97
98         /* And with the opposite comparison */
99         ip = -1; jp = 0; k = p = 1;
100         while (jp+k<l) {
101                 if (n[ip+k] == n[jp+k]) {
102                         if (k == p) {
103                                 jp += p;
104                                 k = 1;
105                         } else k++;
106                 } else if (n[ip+k] < n[jp+k]) {
107                         jp += k;
108                         k = 1;
109                         p = jp - ip;
110                 } else {
111                         ip = jp++;
112                         k = p = 1;
113                 }
114         }
115         if (ip+1 > ms+1) ms = ip;
116         else p = p0;
117
118         /* Periodic needle? */
119         if (memcmp(n, n+p, ms+1)) {
120                 mem0 = 0;
121                 p = MAX(ms, l-ms-1) + 1;
122         } else mem0 = l-p;
123         mem = 0;
124
125         /* Initialize incremental end-of-haystack pointer */
126         z = h;
127
128         /* Search loop */
129         for (;;) {
130                 /* Update incremental end-of-haystack pointer */
131                 if (z-h < l) {
132                         /* Fast estimate for MIN(l,63) */
133                         size_t grow = l | 63;
134                         const unsigned char *z2 = memchr(z, 0, grow);
135                         if (z2) {
136                                 z = z2;
137                                 if (z-h < l) return 0;
138                         } else z += grow;
139                 }
140
141                 /* Check last byte first; advance by shift on mismatch */
142                 if (BITOP(byteset, h[l-1], &)) {
143                         k = l-shift[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);
145                         if (k) {
146                                 if (mem0 && mem && k < p) k = l-p;
147                                 h += k;
148                                 mem = 0;
149                                 continue;
150                         }
151                 } else {
152                         h += l;
153                         mem = 0;
154                         continue;
155                 }
156
157                 /* Compare right half */
158                 for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
159                 if (n[k]) {
160                         h += k-ms;
161                         mem = 0;
162                         continue;
163                 }
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;
167                 h += p;
168                 mem = mem0;
169         }
170 }
171
172 char *strstr(const char *h, const char *n)
173 {
174         /* Return immediately on empty needle */
175         if (!n[0]) return (char *)h;
176
177         /* Use faster algorithms for short needles */
178         h = strchr(h, *n);
179         if (!h || !n[1]) return (char *)h;
180         if (!h[1]) return 0;
181         if (!n[2]) return twobyte_strstr((void *)h, (void *)n);
182         if (!h[2]) return 0;
183         if (!n[3]) return threebyte_strstr((void *)h, (void *)n);
184         if (!h[3]) return 0;
185         if (!n[4]) return fourbyte_strstr((void *)h, (void *)n);
186
187         return twoway_strstr((void *)h, (void *)n);
188 }