-
Notifications
You must be signed in to change notification settings - Fork 14k
[libc] Implemented wcspbrk #142040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc] Implemented wcspbrk #142040
Changes from 2 commits
7dd7eda
1729afa
379e86e
d787008
85dec85
d07884b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===-- Implementation of wcspbrk -----------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/wchar/wcspbrk.h" | ||
|
||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/common.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
LLVM_LIBC_FUNCTION(const wchar_t *, wcspbrk, | ||
(const wchar_t *src, const wchar_t *breakset)) { | ||
// currently O(n * m), can be further optimized to O(n + m) with a hash set | ||
for (int src_idx = 0; src[src_idx] != 0; src_idx++) | ||
for (int breakset_idx = 0; breakset[breakset_idx] != 0; breakset_idx++) | ||
if (src[src_idx] == breakset[breakset_idx]) | ||
return src + src_idx; | ||
|
||
return nullptr; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE_DECL |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//===-- Implementation header for wcspbrk ---------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_WCHAR_WCSPBRK_H | ||
#define LLVM_LIBC_SRC_WCHAR_WCSPBRK_H | ||
|
||
#include "hdr/types/wchar_t.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
|
||
const wchar_t *wcspbrk(const wchar_t *src, const wchar_t *breakset); | ||
|
||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_WCHAR_WCSPBRK_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//===-- Unittests for wcspbrk | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: fix formatting |
||
//----------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "hdr/types/wchar_t.h" | ||
#include "src/wchar/wcspbrk.h" | ||
#include "test/UnitTest/Test.h" | ||
|
||
TEST(LlvmLibcWCSPBrkTest, EmptyStringShouldReturnNullptr) { | ||
// The search should not include the null terminator. | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(L"", L""), nullptr); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(L"_", L""), nullptr); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(L"", L"_"), nullptr); | ||
} | ||
|
||
TEST(LlvmLibcWCSPBrkTest, ShouldNotFindAnythingAfterNullTerminator) { | ||
const wchar_t src[4] = {'a', 'b', '\0', 'c'}; | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"c"), nullptr); | ||
} | ||
|
||
TEST(LlvmLibcWCSPBrkTest, ShouldReturnNullptrIfNoCharactersFound) { | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(L"12345", L"abcdef"), nullptr); | ||
} | ||
|
||
TEST(LlvmLibcWCSPBrkTest, FindsFirstCharacter) { | ||
const wchar_t *src = L"12345"; | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"1"), src); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"-1"), src); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"1_"), src); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"f1_"), src); | ||
|
||
EXPECT_TRUE(src[0] == L'1'); | ||
EXPECT_TRUE(src[1] == L'2'); | ||
EXPECT_TRUE(src[2] == L'3'); | ||
EXPECT_TRUE(src[3] == L'4'); | ||
EXPECT_TRUE(src[4] == L'5'); | ||
EXPECT_TRUE(src[5] == 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since |
||
} | ||
|
||
TEST(LlvmLibcWCSPBrkTest, FindsMiddleCharacter) { | ||
const wchar_t *src = L"12345"; | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"3"), src + 2); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"?3"), src + 2); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"3F"), src + 2); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"z3_"), src + 2); | ||
|
||
EXPECT_TRUE(src[0] == L'1'); | ||
EXPECT_TRUE(src[1] == L'2'); | ||
EXPECT_TRUE(src[2] == L'3'); | ||
EXPECT_TRUE(src[3] == L'4'); | ||
EXPECT_TRUE(src[4] == L'5'); | ||
EXPECT_TRUE(src[5] == 0); | ||
} | ||
|
||
TEST(LlvmLibcWCSPBrkTest, FindsLastCharacter) { | ||
const wchar_t *src = L"12345"; | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"5"), src + 4); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"r5"), src + 4); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"59"), src + 4); | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"n5_"), src + 4); | ||
|
||
EXPECT_TRUE(src[0] == L'1'); | ||
EXPECT_TRUE(src[1] == L'2'); | ||
EXPECT_TRUE(src[2] == L'3'); | ||
EXPECT_TRUE(src[3] == L'4'); | ||
EXPECT_TRUE(src[4] == L'5'); | ||
EXPECT_TRUE(src[5] == 0); | ||
} | ||
|
||
TEST(LlvmLibcWCSPBrkTest, FindsFirstOfRepeated) { | ||
const wchar_t *src = L"A,B,C,D"; | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L","), src + 1); | ||
} | ||
|
||
TEST(LlvmLibcWCSPBrkTest, FindsFirstInBreakset) { | ||
const wchar_t *src = L"12345"; | ||
EXPECT_EQ(LIBC_NAMESPACE::wcspbrk(src, L"34"), src + 2); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it might be useful to also check that it works correctly when the breakset is not in the same order as the string being searched. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you break the check if
src[src_idx]
is inbreakset
out into a helper function this will be easier to read