-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc] wcscspn implementation #146158
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
Merged
Merged
[libc] wcscspn implementation #146158
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| //===-- Implementation of wcscspn -----------------------------------------===// | ||
| // | ||
| // 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/wcscspn.h" | ||
|
|
||
| #include "hdr/types/size_t.h" | ||
| #include "hdr/types/wchar_t.h" | ||
| #include "src/__support/common.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| bool check(wchar_t c, const wchar_t *s2) { | ||
| for (int n = 0; s2[n]; ++n) { | ||
| if (s2[n] == c) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
| LLVM_LIBC_FUNCTION(size_t, wcscspn, (const wchar_t *s1, const wchar_t *s2)) { | ||
| size_t i = 0; | ||
| for (; s1[i]; ++i) { | ||
| if (!check(s1[i], s2)) | ||
| return i; | ||
| } | ||
| return i; | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| //===-- Implementation header for wcscspn ---------------------------------===// | ||
| // | ||
| // 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_WCSCSPN_H | ||
| #define LLVM_LIBC_SRC_WCHAR_WCSCSPN_H | ||
|
|
||
| #include "hdr/types/size_t.h" | ||
| #include "hdr/types/wchar_t.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| size_t wcscspn(const wchar_t *s1, const wchar_t *s2); | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC_WCHAR_WCSCSPN_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| //===-- Unittests for wcscspn | ||
| //----------------------------------------------===// | ||
| // | ||
| // 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/size_t.h" | ||
| #include "hdr/types/wchar_t.h" | ||
| #include "src/wchar/wcscspn.h" | ||
| #include "test/UnitTest/Test.h" | ||
|
|
||
| TEST(LlvmLibcWCSCSpnTest, EmptyStringShouldReturnZeroLengthSpan) { | ||
| // The search should not include the null terminator. | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"", L""), size_t{0}); | ||
sribee8 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"_", L""), size_t{1}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"", L"_"), size_t{0}); | ||
| } | ||
|
|
||
| TEST(LlvmLibcWCSCSpnTest, ShouldNotSpanAnythingAfterNullTerminator) { | ||
| const wchar_t src[4] = {L'a', L'b', L'\0', L'c'}; | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"de"), size_t{2}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"c"), size_t{2}); | ||
|
|
||
| // Same goes for the segment to be searched for. | ||
| const wchar_t segment[4] = {L'1', L'2', L'\0', L'3'}; | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"3", segment), size_t{1}); | ||
| } | ||
|
|
||
| TEST(LlvmLibcWCSCSpnTest, SpanEachIndividualCharacter) { | ||
| const wchar_t *src = L"12345"; | ||
| // These are all in the segment. | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"1"), size_t{0}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"2"), size_t{1}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"3"), size_t{2}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"4"), size_t{3}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"5"), size_t{4}); | ||
| } | ||
|
|
||
| TEST(LlvmLibcWCSCSpnTest, UnmatchedCharacterShouldReturnLength) { | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"a", L"b"), size_t{1}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"abcdef", L"1"), size_t{6}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"123", L"4"), size_t{3}); | ||
| } | ||
|
|
||
| TEST(LlvmLibcWCSCSpnTest, NonSequentialCharactersShouldNotSpan) { | ||
| const wchar_t *src = L"abc456789"; | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"_1_abc_2_def_3_"), size_t{0}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(src, L"67__34xyz12"), size_t{3}); | ||
| } | ||
|
|
||
| TEST(LlvmLibcWCSCSpnTest, ReverseCharacters) { | ||
| // These are all in the string. | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"12345", L"54321"), size_t{0}); | ||
| // 1 is not in the span. | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"12345", L"432"), size_t{1}); | ||
| // 1 is in the span. | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"12345", L"51"), size_t{0}); | ||
| } | ||
|
|
||
| TEST(LlvmLibcWCSCSpnTest, DuplicatedCharactersToBeSearchedForShouldStillMatch) { | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"a", L"aa"), size_t{0}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"aa", L"aa"), size_t{0}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"aaa", L"bb"), size_t{3}); | ||
| EXPECT_EQ(LIBC_NAMESPACE::wcscspn(L"aaaa", L"bb"), size_t{4}); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.