Skip to content

[ADT] Add std::string_view conversion to SmallString #83397

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 2 commits into from
Feb 29, 2024
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
5 changes: 5 additions & 0 deletions llvm/include/llvm/ADT/SmallString.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ class SmallString : public SmallVector<char, InternalLen> {
/// Implicit conversion to StringRef.
operator StringRef() const { return str(); }

/// Implicit conversion to std::string_view.
operator std::string_view() const {
return std::string_view(this->data(), this->size());
}

explicit operator std::string() const {
return std::string(this->data(), this->size());
}
Expand Down
6 changes: 6 additions & 0 deletions llvm/unittests/ADT/SmallStringTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,10 @@ TEST_F(SmallStringTest, GTestPrinter) {
EXPECT_EQ(R"("foo")", ::testing::PrintToString(ErasedSmallString));
}

TEST_F(SmallStringTest, StringView) {
theString = "hello from std::string_view";
EXPECT_EQ("hello from std::string_view",
static_cast<std::string_view>(theString));
}

} // namespace