6 static wchar_t *naive_wcsstr(const wchar_t *h, const wchar_t *n)
9 for (i=0; n[i] && h[i]; i++)
10 for ( ; n[i] != h[i]; h++, i=0);
11 return n[i] ? 0 : (wchar_t *)h;
14 #define MAX(a,b) ((a)>(b)?(a):(b))
15 #define MIN(a,b) ((a)<(b)?(a):(b))
17 static wchar_t *twoway_wcsstr(const wchar_t *h, const wchar_t *n)
20 size_t l, ip, jp, k, p, ms, p0, mem, mem0;
22 /* Computing length of needle */
23 for (l=0; n[l] && h[l]; l++);
24 if (n[l]) return 0; /* hit the end of h */
26 /* Compute maximal suffix */
27 ip = -1; jp = 0; k = p = 1;
29 if (n[ip+k] == n[jp+k]) {
34 } else if (n[ip+k] > n[jp+k]) {
46 /* And with the opposite comparison */
47 ip = -1; jp = 0; k = p = 1;
49 if (n[ip+k] == n[jp+k]) {
54 } else if (n[ip+k] < n[jp+k]) {
63 if (ip+1 > ms+1) ms = ip;
66 /* Periodic needle? */
67 if (wmemcmp(n, n+p, ms+1)) {
69 p = MAX(ms, l-ms-1) + 1;
73 /* Initialize incremental end-of-haystack pointer */
78 /* Update incremental end-of-haystack pointer */
80 /* Fast estimate for MIN(l,63) */
82 const wchar_t *z2 = wmemchr(z, 0, grow);
85 if (z-h < l) return 0;
89 /* Compare right half */
90 for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
96 /* Compare left half */
97 for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
98 if (k == mem) return (wchar_t *)h;
104 wchar_t *wcsstr(const wchar_t *h, const wchar_t *n)
106 /* Return immediately on empty needle or haystack */
107 if (!n[0]) return (wchar_t *)h;
110 /* Use faster algorithms for short needles */
112 if (!h || !n[1]) return (wchar_t *)h;
114 if (!n[2] || !n[3] || !n[4]) return naive_wcsstr(h, n);
116 return twoway_wcsstr(h, n);