Skip to content

Commit 2b58f97

Browse files
[libc] add wcsstr
1 parent 4fe47b1 commit 2b58f97

File tree

8 files changed

+87
-0
lines changed

8 files changed

+87
-0
lines changed

libc/config/gpu/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ set(TARGET_LIBC_ENTRYPOINTS
261261
libc.src.time.nanosleep
262262

263263
# wchar.h entrypoints
264+
libc.src.wchar.wcsstr
264265
libc.src.wchar.wcspbrk
265266
libc.src.wchar.wcsrchr
266267
libc.src.wchar.wcschr

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ set(TARGET_LIBC_ENTRYPOINTS
349349
libc.src.unistd.write
350350

351351
# wchar.h entrypoints
352+
libc.src.wchar.wcsstr
352353
libc.src.wchar.wcspbrk
353354
libc.src.wchar.wcsrchr
354355
libc.src.wchar.wcschr

libc/config/linux/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
346346
libc.src.unistd.write
347347

348348
# wchar.h entrypoints
349+
libc.src.wchar.wcsstr
349350
libc.src.wchar.wcspbrk
350351
libc.src.wchar.wcsrchr
351352
libc.src.wchar.wcschr

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ set(TARGET_LIBC_ENTRYPOINTS
348348
libc.src.unistd.write
349349

350350
# wchar.h entrypoints
351+
libc.src.wchar.wcsstr
351352
libc.src.wchar.wcspbrk
352353
libc.src.wchar.wcsrchr
353354
libc.src.wchar.wcschr

libc/hdrgen/yaml/wchar.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@ functions:
4949
arguments:
5050
- type: const wchar_t *
5151
- type: const wchar_t *
52+
- name: wcsstr
53+
standards:
54+
- stdc
55+
return_type: const wchar_t *
56+
arguments:
57+
- type: const wchar_t *
58+
- type: const wchar_t *

libc/src/wchar/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,14 @@ add_entrypoint_object(
8080
.wcslen
8181
libc.hdr.types.wchar_t
8282
)
83+
84+
add_entrypoint_object(
85+
wcsstr
86+
SRCS
87+
wcsstr.cpp
88+
HDRS
89+
wcsstr.h
90+
DEPENDS
91+
.wcslen
92+
libc.hdr.types.wchar_t
93+
)

libc/src/wchar/wcsstr.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===-- Implementation of wcsstr ------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/wchar/wcsstr.h"
10+
#include "src/__support/common.h"
11+
#include "src/__support/macros/config.h"
12+
#include "wcslen.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(const wchar_t *, wcsstr,
17+
(const wchar_t *s, const wchar_t *needle)) {
18+
size_t s_len = wcslen(s);
19+
size_t needle_len = wcslen(needle);
20+
21+
if (needle_len > s_len)
22+
return nullptr;
23+
24+
for (size_t i = 0; i < s_len; i++) {
25+
size_t end = needle_len + i;
26+
if (end > s_len)
27+
break;
28+
29+
bool found = true;
30+
for (size_t x = 0; x < needle_len; x++) {
31+
if (s[i + x] != needle[x]) {
32+
found = false;
33+
break;
34+
}
35+
}
36+
37+
if (!found)
38+
continue;
39+
return &s[i];
40+
}
41+
return nullptr;
42+
}
43+
44+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcsstr.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for wcsstr ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_WCHAR_WCSSTR_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSSTR_H
11+
12+
#include "hdr/types/wchar_t.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
const wchar_t *wcsstr(const wchar_t *s, const wchar_t *needle);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_WCHAR_WCSSTR_H

0 commit comments

Comments
 (0)