Skip to content

Commit 7dd7eda

Browse files
committed
implemented wcspbrk; added tests
1 parent 9553514 commit 7dd7eda

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ set(TARGET_LIBC_ENTRYPOINTS
365365
libc.src.wchar.wcslen
366366
libc.src.wchar.wctob
367367
libc.src.wchar.wcschr
368+
libc.src.wchar.wcspbrk
368369

369370
# sys/uio.h entrypoints
370371
libc.src.sys.uio.writev

libc/include/wchar.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,10 @@ functions:
3434
arguments:
3535
- type: const wchar_t *
3636
- type: wchar_t
37+
- name: wcspbrk
38+
standards:
39+
- stdc
40+
return_type: const wchar_t *
41+
arguments:
42+
- type: const wchar_t *
43+
- type: const wchar_t *

libc/src/wchar/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ add_entrypoint_object(
4444
libc.hdr.wchar_macros
4545
libc.src.__support.wctype_utils
4646
)
47+
48+
add_entrypoint_object(
49+
wcspbrk
50+
SRCS
51+
wcspbrk.cpp
52+
HDRS
53+
wcspbrk.h
54+
DEPENDS
55+
libc.hdr.wchar_macros
56+
libc.src.__support.wctype_utils
57+
)

libc/src/wchar/wcspbrk.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation of wcspbrk -----------------------------------------===//
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/wcspbrk.h"
10+
11+
#include "hdr/types/wchar_t.h"
12+
#include "src/__support/common.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
LLVM_LIBC_FUNCTION(const wchar_t *, wcspbrk,
17+
(const wchar_t *src, const wchar_t *breakset)) {
18+
// currently O(n * m), can be further optimized to O(n + m) with a hash set
19+
for (int src_idx = 0; src[src_idx] != 0; src_idx++)
20+
for (int breakset_idx = 0; breakset[breakset_idx] != 0; breakset_idx++)
21+
if (src[src_idx] == breakset[breakset_idx])
22+
return src + src_idx;
23+
24+
return nullptr;
25+
}
26+
27+
} // namespace LIBC_NAMESPACE_DECL

libc/src/wchar/wcspbrk.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===-- Implementation header for wcspbrk ---------------------------------===//
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_WCSPBRK_H
10+
#define LLVM_LIBC_SRC_WCHAR_WCSPBRK_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 *wcspbrk(const wchar_t *src, const wchar_t *breakset);
18+
19+
} // namespace LIBC_NAMESPACE_DECL
20+
21+
#endif // LLVM_LIBC_SRC_WCHAR_WCSPBRK_H

libc/test/src/wchar/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ add_libc_test(
4242
DEPENDS
4343
libc.src.wchar.wcschr
4444
)
45+
46+
add_libc_test(
47+
wcspbrk_test
48+
SUITE
49+
libc_wchar_unittests
50+
SRCS
51+
wcspbrk_test.cpp
52+
DEPENDS
53+
libc.src.wchar.wcspbrk
54+
)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===-- Unittests for wcspbrk
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/wchar_t.h"
11+
#include "src/wchar/wcspbrk.h"
12+
#include "test/UnitTest/Test.h"
13+
14+
TEST(LlvmLibcWCSPBrkTest, EmptyStringShouldReturnNullptr) {
15+
// The search should not include the null terminator.
16+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(L"", L""), nullptr);
17+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(L"_", L""), nullptr);
18+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(L"", L"_"), nullptr);
19+
}
20+
21+
TEST(LlvmLibcWCSPBrkTest, ShouldNotFindAnythingAfterNullTerminator) {
22+
const wchar_t src[4] = {'a', 'b', '\0', 'c'};
23+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"c"), nullptr);
24+
}
25+
26+
TEST(LlvmLibcWCSPBrkTest, ShouldReturnNullptrIfNoCharactersFound) {
27+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(L"12345", L"abcdef"), nullptr);
28+
}
29+
30+
TEST(LlvmLibcWCSPBrkTest, FindsFirstCharacter) {
31+
const wchar_t *src = L"12345";
32+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"1"), src);
33+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"-1"), src);
34+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"1_"), src);
35+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"f1_"), src);
36+
37+
EXPECT_TRUE(src[0] == L'1');
38+
EXPECT_TRUE(src[1] == L'2');
39+
EXPECT_TRUE(src[2] == L'3');
40+
EXPECT_TRUE(src[3] == L'4');
41+
EXPECT_TRUE(src[4] == L'5');
42+
EXPECT_TRUE(src[5] == 0);
43+
}
44+
45+
TEST(LlvmLibcWCSPBrkTest, FindsMiddleCharacter) {
46+
const wchar_t *src = L"12345";
47+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"3"), src + 2);
48+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"?3"), src + 2);
49+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"3F"), src + 2);
50+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"z3_"), src + 2);
51+
52+
EXPECT_TRUE(src[0] == L'1');
53+
EXPECT_TRUE(src[1] == L'2');
54+
EXPECT_TRUE(src[2] == L'3');
55+
EXPECT_TRUE(src[3] == L'4');
56+
EXPECT_TRUE(src[4] == L'5');
57+
EXPECT_TRUE(src[5] == 0);
58+
}
59+
60+
TEST(LlvmLibcWCSPBrkTest, FindsLastCharacter) {
61+
const wchar_t *src = L"12345";
62+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"5"), src + 4);
63+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"r5"), src + 4);
64+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"59"), src + 4);
65+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"n5_"), src + 4);
66+
67+
EXPECT_TRUE(src[0] == L'1');
68+
EXPECT_TRUE(src[1] == L'2');
69+
EXPECT_TRUE(src[2] == L'3');
70+
EXPECT_TRUE(src[3] == L'4');
71+
EXPECT_TRUE(src[4] == L'5');
72+
EXPECT_TRUE(src[5] == 0);
73+
}
74+
75+
TEST(LlvmLibcWCSPBrkTest, FindsFirstOfRepeated) {
76+
const wchar_t *src = L"A,B,C,D";
77+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L","), src + 1);
78+
}
79+
80+
TEST(LlvmLibcWCSPBrkTest, FindsFirstInBreakset) {
81+
const wchar_t *src = L"12345";
82+
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"34"), src + 2);
83+
}

0 commit comments

Comments
 (0)