Skip to content

Commit 4a610c0

Browse files
author
Sriya Pratipati
committed
[libc] wcscspn implementation
Implemented wcscspn and tests.
1 parent 526701f commit 4a610c0

File tree

7 files changed

+153
-0
lines changed

7 files changed

+153
-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
@@ -203,6 +203,17 @@ add_entrypoint_object(
203203
libc.hdr.types.size_t
204204
)
205205

206+
add_entrypoint_object(
207+
wcscspn
208+
SRCS
209+
wcscspn.cpp
210+
HDRS
211+
wcscspn.h
212+
DEPENDS
213+
libc.hdr.wchar_macros
214+
libc.hdr.types.size_t
215+
)
216+
206217
add_entrypoint_object(
207218
wmemcmp
208219
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
@@ -173,6 +173,16 @@ add_libc_test(
173173
libc.src.wchar.wcsspn
174174
)
175175

176+
add_libc_test(
177+
wcscspn_test
178+
SUITE
179+
libc_wchar_unittests
180+
SRCS
181+
wcscspn_test.cpp
182+
DEPENDS
183+
libc.src.wchar.wcscspn
184+
)
185+
176186
add_libc_test(
177187
wmemchr_test
178188
SUITE
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===-- Unittests for wcscspn
2+
//----------------------------------------------===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#include "hdr/types/size_t.h"
11+
#include "hdr/types/wchar_t.h"
12+
#include "src/wchar/wcscspn.h"
13+
#include "test/UnitTest/Test.h"
14+
15+
TEST(LlvmLibcWCSCSpnTest, EmptyStringShouldReturnZeroLengthSpan) {
16+
// The search should not include the null terminator.
17+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"", L""), size_t{0});
18+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"_", L""), size_t{1});
19+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"", L"_"), size_t{0});
20+
}
21+
22+
TEST(LlvmLibcWCSCSpnTest, ShouldNotSpanAnythingAfterNullTerminator) {
23+
const wchar_t src[4] = {L'a', L'b', L'\0', L'c'};
24+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"de"), size_t{2});
25+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"c"), size_t{2});
26+
27+
// Same goes for the segment to be searched for.
28+
const wchar_t segment[4] = {L'1', L'2', L'\0', L'3'};
29+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"3", segment), size_t{1});
30+
}
31+
32+
TEST(LlvmLibcWCSCSpnTest, SpanEachIndividualCharacter) {
33+
const wchar_t *src = L"12345";
34+
// These are all in the segment.
35+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"1"), size_t{0});
36+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"2"), size_t{1});
37+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"3"), size_t{2});
38+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"4"), size_t{3});
39+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"5"), size_t{4});
40+
}
41+
42+
TEST(LlvmLibcWCSCSpnTest, UnmatchedCharacterShouldReturnLength) {
43+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"a", L"b"), size_t{1});
44+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"abcdef", L"1"), size_t{6});
45+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"123", L"4"), size_t{3});
46+
}
47+
48+
TEST(LlvmLibcWCSCSpnTest, NonSequentialCharactersShouldNotSpan) {
49+
const wchar_t *src = L"abc456789";
50+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"_1_abc_2_def_3_"), size_t{0});
51+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"67__34xyz12"), size_t{3});
52+
}
53+
54+
TEST(LlvmLibcWCSCSpnTest, ReverseCharacters) {
55+
// These are all in the string.
56+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"12345", L"54321"), size_t{0});
57+
// 1 is not in the span.
58+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"12345", L"432"), size_t{1});
59+
// 1 is in the span.
60+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"12345", L"51"), size_t{0});
61+
}
62+
63+
TEST(LlvmLibcWCSCSpnTest, DuplicatedCharactersToBeSearchedForShouldStillMatch) {
64+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"a", L"aa"), size_t{0});
65+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"aa", L"aa"), size_t{0});
66+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"aaa", L"bb"), size_t{3});
67+
EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"aaaa", L"bb"), size_t{4});
68+
}

0 commit comments

Comments
 (0)