683cdd0143e2f90f6d0849c1becede3fc4ad0b82
[musl] / src / string / strstr.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4
5 static char *twobyte_strstr(const unsigned char *h, const unsigned char *n)
6 {
7         uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1];
8         for (h++; *h && hw != nw; hw = hw<<8 | *++h);
9         return *h ? (char *)h-1 : 0;
10 }
11
12 static char *threebyte_strstr(const unsigned char *h, const unsigned char *n)
13 {
14         uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8;
15         uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8;
16         for (h+=2; *h && hw != nw; hw = (hw|*++h)<<8);
17         return *h ? (char *)h-2 : 0;
18 }
19
20 static char *fourbyte_strstr(const unsigned char *h, const unsigned char *n)
21 {
22         uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3];
23         uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3];
24         for (h+=3; *h && hw != nw; hw = hw<<8 | *++h);
25         return *h ? (char *)h-3 : 0;
26 }
27
28 #if 0
29 static char *naive_strstr(const char *h, const char *n)
30 {
31         size_t i;
32         for (i=0; n[i] && h[i]; i++)
33         for (   ; n[i] != h[i]; h++, i=0);
34         return n[i] ? 0 : (char *)h;
35 }
36 #endif
37
38 #define MAX(a,b) ((a)>(b)?(a):(b))
39 #define MIN(a,b) ((a)<(b)?(a):(b))
40
41 #define BITOP(a,b,op) \
42  ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a))))
43
44 static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
45 {
46         const unsigned char *z;
47         size_t l, ip, jp, k, p, ms, p0, mem, mem0;
48         size_t byteset[32 / sizeof(size_t)] = { 0 };
49         size_t shift[256];
50
51         /* Computing length of needle and fill shift table */
52         for (l=0; n[l] && h[l]; l++)
53                 BITOP(byteset, n[l], |=), shift[n[l]] = l+1;
54         if (n[l]) return 0; /* hit the end of h */
55
56         /* Compute maximal suffix */
57         ip = -1; jp = 0; k = p = 1;
58         while (jp+k<l) {
59                 if (n[ip+k] == n[jp+k]) {
60                         if (k == p) {
61                                 jp += p;
62                                 k = 1;
63                         } else k++;
64                 } else if (n[ip+k] > n[jp+k]) {
65                         jp += k;
66                         k = 1;
67                         p = jp - ip;
68                 } else {
69                         ip = jp++;
70                         k = p = 1;
71                 }
72         }
73         ms = ip;
74         p0 = p;
75
76         /* And with the opposite comparison */
77         ip = -1; jp = 0; k = p = 1;
78         while (jp+k<l) {
79                 if (n[ip+k] == n[jp+k]) {
80                         if (k == p) {
81                                 jp += p;
82                                 k = 1;
83                         } else k++;
84                 } else if (n[ip+k] < n[jp+k]) {
85                         jp += k;
86                         k = 1;
87                         p = jp - ip;
88                 } else {
89                         ip = jp++;
90                         k = p = 1;
91                 }
92         }
93         if (ip+1 > ms+1) ms = ip;
94         else p = p0;
95
96         /* Periodic needle? */
97         if (memcmp(n, n+p, ms+1)) {
98                 mem0 = 0;
99                 p = MAX(ms, l-ms-1) + 1;
100         } else mem0 = l-p;
101         mem = 0;
102
103         /* Initialize incremental end-of-haystack pointer */
104         z = h;
105
106         /* Search loop */
107         for (;;) {
108                 /* Update incremental end-of-haystack pointer */
109                 if (z-h < l) {
110                         /* Fast estimate for MIN(l,63) */
111                         size_t grow = l | 63;
112                         const unsigned char *z2 = memchr(z, 0, grow);
113                         if (z2) {
114                                 z = z2;
115                                 if (z-h < l) return 0;
116                         } else z += grow;
117                 }
118
119                 /* Check last byte first; advance by shift on mismatch */
120                 if (BITOP(byteset, h[l-1], &)) {
121                         k = l-shift[h[l-1]];
122                         //printf("adv by %zu (on %c) at [%s] (%zu;l=%zu)\n", k, h[l-1], h, shift[h[l-1]], l);
123                         if (k) {
124                                 if (mem0 && mem && k < p) k = l-p;
125                                 h += k;
126                                 mem = 0;
127                                 continue;
128                         }
129                 } else {
130                         h += l;
131                         mem = 0;
132                         continue;
133                 }
134
135                 /* Compare right half */
136                 for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
137                 if (n[k]) {
138                         h += k-ms;
139                         mem = 0;
140                         continue;
141                 }
142                 /* Compare left half */
143                 for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
144                 if (k == mem) return (char *)h;
145                 h += p;
146                 mem = mem0;
147         }
148 }
149
150 char *strstr(const char *h, const char *n)
151 {
152         /* Return immediately on empty needle */
153         if (!n[0]) return (char *)h;
154
155         /* Use faster algorithms for short needles */
156         h = strchr(h, *n);
157         if (!h || !n[1]) return (char *)h;
158         if (!h[1]) return 0;
159         if (!n[2]) return twobyte_strstr((void *)h, (void *)n);
160         if (!h[2]) return 0;
161         if (!n[3]) return threebyte_strstr((void *)h, (void *)n);
162         if (!h[3]) return 0;
163         if (!n[4]) return fourbyte_strstr((void *)h, (void *)n);
164
165         return twoway_strstr((void *)h, (void *)n);
166 }