6 #define MAX(a,b) ((a)>(b)?(a):(b))
7 #define MIN(a,b) ((a)<(b)?(a):(b))
9 static wchar_t *twoway_wcsstr(const wchar_t *h, const wchar_t *n)
12 size_t l, ip, jp, k, p, ms, p0, mem, mem0;
14 /* Computing length of needle */
15 for (l=0; n[l] && h[l]; l++);
16 if (n[l]) return 0; /* hit the end of h */
18 /* Compute maximal suffix */
19 ip = -1; jp = 0; k = p = 1;
21 if (n[ip+k] == n[jp+k]) {
26 } else if (n[ip+k] > n[jp+k]) {
38 /* And with the opposite comparison */
39 ip = -1; jp = 0; k = p = 1;
41 if (n[ip+k] == n[jp+k]) {
46 } else if (n[ip+k] < n[jp+k]) {
55 if (ip+1 > ms+1) ms = ip;
58 /* Periodic needle? */
59 if (wmemcmp(n, n+p, ms+1)) {
61 p = MAX(ms, l-ms-1) + 1;
65 /* Initialize incremental end-of-haystack pointer */
70 /* Update incremental end-of-haystack pointer */
72 /* Fast estimate for MIN(l,63) */
74 const wchar_t *z2 = wmemchr(z, 0, grow);
77 if (z-h < l) return 0;
81 /* Compare right half */
82 for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
88 /* Compare left half */
89 for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
90 if (k == mem) return (wchar_t *)h;
96 wchar_t *wcsstr(const wchar_t *h, const wchar_t *n)
98 /* Return immediately on empty needle or haystack */
99 if (!n[0]) return (wchar_t *)h;
102 /* Use faster algorithms for short needles */
104 if (!h || !n[1]) return (wchar_t *)h;
107 return twoway_wcsstr(h, n);