Skip to content

Commit 9070743

Browse files
extinguishxiaoxiang781216
authored andcommitted
libs/libc/wchar: add wcsstr implementation
Signed-off-by: guoshichao <[email protected]>
1 parent 20802fc commit 9070743

File tree

3 files changed

+216
-1
lines changed

3 files changed

+216
-1
lines changed

libs/libc/wchar/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ target_sources(
5959
lib_wcsncmp.c
6060
lib_wcscspn.c
6161
lib_wcspbrk.c
62-
lib_wcsspn.c)
62+
lib_wcsspn.c
63+
lib_wcsstr.c)

libs/libc/wchar/Make.defs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ CSRCS += lib_swprintf.c lib_mbsnrtowcs.c lib_wcsnrtombs.c lib_mbsinit.c
2929
CSRCS += lib_mbrlen.c lib_mbsrtowcs.c lib_wcsrtombs.c lib_wcscpy.c
3030
CSRCS += lib_wcscat.c lib_wcslcat.c lib_wcsncat.c lib_wcsrchr.c lib_wcschr.c
3131
CSRCS += lib_wcsncpy.c lib_wcsncmp.c lib_wcscspn.c lib_wcspbrk.c lib_wcsspn.c
32+
CSRCS += lib_wcsstr.c
3233

3334
# Add the wchar directory to the build
3435

libs/libc/wchar/lib_wcsstr.c

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/****************************************************************************
2+
* libs/libc/wchar/lib_wcsstr.c
3+
*
4+
* Copyright © 2005-2014 Rich Felker, et al.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining
7+
* a copy of this software and associated documentation files (the
8+
* "Software"), to deal in the Software without restriction, including
9+
* without limitation the rights to use, copy, modify, merge, publish,
10+
* distribute, sublicense, and/or sell copies of the Software, and to
11+
* permit persons to whom the Software is furnished to do so, subject to
12+
* the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be
15+
* included in all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24+
*
25+
****************************************************************************/
26+
27+
/****************************************************************************
28+
* Included Files
29+
****************************************************************************/
30+
31+
#include <wchar.h>
32+
#include <sys/param.h>
33+
34+
/****************************************************************************
35+
* Private Functions
36+
****************************************************************************/
37+
38+
static FAR wchar_t *twoway_wcsstr(FAR const wchar_t *h, FAR const wchar_t *n)
39+
{
40+
FAR const wchar_t *z;
41+
size_t l;
42+
size_t ip;
43+
size_t jp;
44+
size_t k;
45+
size_t p;
46+
size_t ms;
47+
size_t p0;
48+
size_t mem;
49+
size_t mem0;
50+
51+
/* Computing length of needle */
52+
53+
for (l = 0; n[l] && h[l]; l++);
54+
55+
/* hit the end of h */
56+
57+
if (n[l]) return 0;
58+
59+
/* Compute maximal suffix */
60+
61+
ip = -1; jp = 0; k = p = 1;
62+
63+
while (jp + k < l)
64+
{
65+
if (n[ip + k] == n[jp + k])
66+
{
67+
if (k == p)
68+
{
69+
jp += p;
70+
k = 1;
71+
}
72+
else k++;
73+
}
74+
else if (n[ip + k] > n[jp + k])
75+
{
76+
jp += k;
77+
k = 1;
78+
p = jp - ip;
79+
}
80+
else
81+
{
82+
ip = jp++;
83+
k = p = 1;
84+
}
85+
}
86+
87+
ms = ip;
88+
p0 = p;
89+
90+
/* And with the opposite comparison */
91+
92+
ip = -1; jp = 0; k = p = 1;
93+
while (jp + k < l)
94+
{
95+
if (n[ip + k] == n[jp + k])
96+
{
97+
if (k == p)
98+
{
99+
jp += p;
100+
k = 1;
101+
}
102+
else k++;
103+
}
104+
else if (n[ip + k] < n[jp + k])
105+
{
106+
jp += k;
107+
k = 1;
108+
p = jp - ip;
109+
}
110+
else
111+
{
112+
ip = jp++;
113+
k = p = 1;
114+
}
115+
}
116+
117+
if (ip + 1 > ms + 1) ms = ip;
118+
else p = p0;
119+
120+
/* Periodic needle? */
121+
122+
if (wmemcmp(n, n + p, ms + 1))
123+
{
124+
mem0 = 0;
125+
p = MAX(ms, l - ms - 1) + 1;
126+
}
127+
else
128+
{
129+
mem0 = l - p;
130+
}
131+
132+
mem = 0;
133+
134+
/* Initialize incremental end-of-haystack pointer */
135+
136+
z = h;
137+
138+
/* Search loop */
139+
140+
for (; ; )
141+
{
142+
/* Update incremental end-of-haystack pointer */
143+
144+
if (z - h < l)
145+
{
146+
/* Fast estimate for MIN(l,63) */
147+
148+
size_t grow = l | 63;
149+
FAR const wchar_t *z2 = wmemchr(z, 0, grow);
150+
if (z2)
151+
{
152+
z = z2;
153+
if (z - h < l) return 0;
154+
}
155+
else z += grow;
156+
}
157+
158+
/* Compare right half */
159+
160+
for (k = MAX(ms + 1, mem); n[k] && n[k] == h[k]; k++);
161+
if (n[k])
162+
{
163+
h += k - ms;
164+
mem = 0;
165+
continue;
166+
}
167+
168+
/* Compare left half */
169+
170+
for (k = ms + 1; k > mem && n[k - 1] == h[k - 1]; k--);
171+
172+
if (k <= mem) return (FAR wchar_t *)h;
173+
h += p;
174+
mem = mem0;
175+
}
176+
}
177+
178+
/****************************************************************************
179+
* Public Functions
180+
****************************************************************************/
181+
182+
/****************************************************************************
183+
* Name: wcsstr
184+
*
185+
* Description:
186+
* Locate substring of wide string
187+
*
188+
* Input Parameters:
189+
* h - the wide string to be scanned
190+
* n - the wide string containing the sequence of characters to match
191+
*
192+
* Returned Value:
193+
* Return a pointer to the first occurrence in "h" of the entire sequence
194+
* of characters specified in "n";
195+
* Return null pointer if the sequence is not present in "h"
196+
*
197+
****************************************************************************/
198+
199+
FAR wchar_t *wcsstr(FAR const wchar_t *h, FAR const wchar_t *n)
200+
{
201+
/* Return immediately on empty needle or haystack */
202+
203+
if (!n[0]) return (FAR wchar_t *)h;
204+
if (!h[0]) return 0;
205+
206+
/* Use faster algorithms for short needles */
207+
208+
h = wcschr(h, *n);
209+
if (!h || !n[1]) return (FAR wchar_t *)h;
210+
if (!h[1]) return 0;
211+
212+
return twoway_wcsstr(h, n);
213+
}

0 commit comments

Comments
 (0)