Skip to content

Commit 47e28d9

Browse files
sribee8Sriya Pratipati
andauthored
[libc] wcscspn implementation (#146158)
Implemented wcscspn and tests. --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent 04c6143 commit 47e28d9

File tree

9 files changed

+174
-0
lines changed

9 files changed

+174
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ set(TARGET_LIBC_ENTRYPOINTS
376376
libc.src.wchar.wcspbrk
377377
libc.src.wchar.wcsrchr
378378
libc.src.wchar.wcsspn
379+
libc.src.wchar.wcscspn
379380
libc.src.wchar.wmemcmp
380381
libc.src.wchar.wmempcpy
381382
libc.src.wchar.wmemcpy

libc/include/wchar.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ functions:
104104
arguments:
105105
- type: const wchar_t *
106106
- type: const wchar_t *
107+
- name: wcscspn
108+
standards:
109+
- stdc
110+
return_type: size_t
111+
arguments:
112+
- type: const wchar_t *
113+
- type: const wchar_t *
107114
- name: wmemcmp
108115
standards:
109116
- stdc

libc/src/wchar/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,17 @@ add_entrypoint_object(
213213
libc.hdr.types.size_t
214214
)
215215

216+
add_entrypoint_object(
217+
wcscspn
218+
SRCS
219+
wcscspn.cpp
220+
HDRS
221+
wcscspn.h
222+
DEPENDS
223+
libc.hdr.wchar_macros
224+
libc.hdr.types.size_t
225+
)
226+
216227
add_entrypoint_object(
217228
wmemcmp
218229
SRCS

libc/src/wchar/wcscspn.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//===-- Implementation of wcscspn -----------------------------------------===//
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/wcscspn.h"
10+
11+
#include "hdr/types/size_t.h"
12+
#include "hdr/types/wchar_t.h"
13+
#include "src/__support/common.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
bool check(wchar_t c, const wchar_t *s2) {
19+
for (int n = 0; s2[n]; ++n) {
20+
if (s2[n] == c)
21+
return false;
22+
}
23+
return true;
24+
}
25+
LLVM_LIBC_FUNCTION(size_t, wcscspn, (const wchar_t *s1, const wchar_t *s2)) {
26+
size_t i = 0;
27+
for (; s1[i]; ++i) {
28+
if (!check(s1[i], s2))
29+
return i;
30+
}
31+
return i;
32+
}
33+
34+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcscspn.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- Implementation header for wcscspn ---------------------------------===//
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_WCSCSPN_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSCSPN_H
11+
12+
#include "hdr/types/size_t.h"
13+
#include "hdr/types/wchar_t.h"
14+
#include "src/__support/macros/config.h"
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
18+
size_t wcscspn(const wchar_t *s1, const wchar_t *s2);
19+
20+
} // namespace LIBC_NAMESPACE_DECL
21+
22+
#endif // LLVM_LIBC_SRC_WCHAR_WCSCSPN_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ add_libc_test(
183183
libc.src.wchar.wcsspn
184184
)
185185

186+
add_libc_test(
187+
wcscspn_test
188+
SUITE
189+
libc_wchar_unittests
190+
SRCS
191+
wcscspn_test.cpp
192+
DEPENDS
193+
libc.src.wchar.wcscspn
194+
)
195+
186196
add_libc_test(
187197
wmemchr_test
188198
SUITE

libc/test/src/wchar/wcscspn_test.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//===-- Unittests for wcscspn ---------------------------------------------===//
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 "hdr/types/size_t.h"
10+
#include "hdr/types/wchar_t.h"
11+
#include "src/wchar/wcscspn.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
TEST(LlvmLibcWCSCSpnTest, EmptyStringShouldReturnZeroLengthSpan) {
15+
// The search should not include the null terminator.
16+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"", L""), size_t(0));
17+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"_", L""), size_t(1));
18+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"", L"_"), size_t(0));
19+
}
20+
21+
TEST(LlvmLibcWCSCSpnTest, ShouldNotSpanAnythingAfterNullTerminator) {
22+
const wchar_t src[4] = {L'a', L'b', L'\0', L'c'};
23+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"de"), size_t(2));
24+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"c"), size_t(2));
25+
26+
// Same goes for the segment to be searched for.
27+
const wchar_t segment[4] = {L'1', L'2', L'\0', L'3'};
28+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"3", segment), size_t(1));
29+
}
30+
31+
TEST(LlvmLibcWCSCSpnTest, SpanEachIndividualCharacter) {
32+
const wchar_t *src = L"12345";
33+
// These are all in the segment.
34+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"1"), size_t(0));
35+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"2"), size_t(1));
36+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"3"), size_t(2));
37+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"4"), size_t(3));
38+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"5"), size_t(4));
39+
}
40+
41+
TEST(LlvmLibcWCSCSpnTest, UnmatchedCharacterShouldReturnLength) {
42+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"a", L"b"), size_t(1));
43+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"abcdef", L"1"), size_t(6));
44+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"123", L"4"), size_t(3));
45+
}
46+
47+
TEST(LlvmLibcWCSCSpnTest, NonSequentialCharactersShouldNotSpan) {
48+
const wchar_t *src = L"abc456789";
49+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"_1_abc_2_def_3_"), size_t(0));
50+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"67__34xyz12"), size_t(3));
51+
}
52+
53+
TEST(LlvmLibcWCSCSpnTest, ReverseCharacters) {
54+
// These are all in the string.
55+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"12345", L"54321"), size_t(0));
56+
// 1 is not in the span.
57+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"12345", L"432"), size_t(1));
58+
// 1 is in the span.
59+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"12345", L"51"), size_t(0));
60+
}
61+
62+
TEST(LlvmLibcWCSCSpnTest, DuplicatedCharactersToBeSearchedForShouldStillMatch) {
63+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"a", L"aa"), size_t(0));
64+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"aa", L"aa"), size_t(0));
65+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"aaa", L"bb"), size_t(3));
66+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"aaaa", L"bb"), size_t(4));
67+
}

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5815,6 +5815,18 @@ libc_function(
58155815
],
58165816
)
58175817

5818+
libc_function(
5819+
name = "wcscspn",
5820+
srcs = ["src/wchar/wcscspn.cpp"],
5821+
hdrs = ["src/wchar/wcscspn.h"],
5822+
deps = [
5823+
":__support_common",
5824+
":__support_macros_config",
5825+
":types_size_t",
5826+
":types_wchar_t",
5827+
],
5828+
)
5829+
58185830
libc_function(
58195831
name = "wcslen",
58205832
srcs = ["src/wchar/wcslen.cpp"],

utils/bazel/llvm-project-overlay/libc/test/src/wchar/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ libc_test(
7373
],
7474
)
7575

76+
libc_test(
77+
name = "wcscspn_test",
78+
srcs = ["wcscspn_test.cpp"],
79+
deps = [
80+
"//libc:types_size_t",
81+
"//libc:types_wchar_t",
82+
"//libc:wcscspn",
83+
],
84+
)
85+
7686
libc_test(
7787
name = "wcslen_test",
7888
srcs = ["wcslen_test.cpp"],

0 commit comments

Comments
 (0)