Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions libc/src/wchar/wcstok.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,22 @@ LLVM_LIBC_FUNCTION(wchar_t *, wcstok,
wchar_t *tok_start = str;
while (*tok_start != L'\0' && internal::wcschr(delims, *tok_start))
++tok_start;
if (*tok_start == L'\0') {
*context = nullptr;
return nullptr;
}

wchar_t *tok_end = tok_start;
while (*tok_end != L'\0' && !internal::wcschr(delims, *tok_end))
++tok_end;

if (*tok_end != L'\0') {
if (*tok_end == L'\0') {
*context = nullptr;
} else {
*tok_end = L'\0';
++tok_end;
*context = tok_end + 1;
}
*context = tok_end;
return *tok_start == L'\0' ? nullptr : tok_start;
return tok_start;
}

} // namespace LIBC_NAMESPACE_DECL
8 changes: 8 additions & 0 deletions libc/test/src/wchar/wcstok_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ TEST(LlvmLibcWCSTokReentrantTest, NoTokenFound) {
// Another call to ensure that 'reserve' is not in a bad state.
ASSERT_EQ(LIBC_NAMESPACE::wcstok(empty, L"", &reserve), nullptr);
ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"", &reserve), nullptr);
// Subsequent searches still return nullptr.
ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"", &reserve), nullptr);
}
{ // Empty source and single character delimiter string.
wchar_t empty[] = L"";
Expand All @@ -27,6 +29,8 @@ TEST(LlvmLibcWCSTokReentrantTest, NoTokenFound) {
// Another call to ensure that 'reserve' is not in a bad state.
ASSERT_EQ(LIBC_NAMESPACE::wcstok(empty, L"_", &reserve), nullptr);
ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"_", &reserve), nullptr);
// Subsequent searches still return nullptr.
ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"_", &reserve), nullptr);
}
{ // Same character source and delimiter string.
wchar_t single[] = L"_";
Expand All @@ -35,6 +39,8 @@ TEST(LlvmLibcWCSTokReentrantTest, NoTokenFound) {
// Another call to ensure that 'reserve' is not in a bad state.
ASSERT_EQ(LIBC_NAMESPACE::wcstok(single, L"_", &reserve), nullptr);
ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"_", &reserve), nullptr);
// Subsequent searches still return nullptr.
ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L"_", &reserve), nullptr);
}
{ // Multiple character source and single character delimiter string.
wchar_t multiple[] = L"1,2";
Expand All @@ -51,6 +57,8 @@ TEST(LlvmLibcWCSTokReentrantTest, NoTokenFound) {
ASSERT_TRUE(tok[2] == L'2');
ASSERT_TRUE(tok[3] == L'\0');
ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L":", &reserve), nullptr);
// Subsequent searches still return nullptr.
ASSERT_EQ(LIBC_NAMESPACE::wcstok(nullptr, L":", &reserve), nullptr);
}
}

Expand Down
Loading