Skip to content

Commit cff36bb

Browse files
[ADT] Add std::string_view conversion to SmallString (#83397)
This patch adds a std::string_view conversion to SmallString which we've recently found a use for downstream in a project that is using c++ standard library IO.
1 parent 22f5e30 commit cff36bb

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

llvm/include/llvm/ADT/SmallString.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ class SmallString : public SmallVector<char, InternalLen> {
265265
/// Implicit conversion to StringRef.
266266
operator StringRef() const { return str(); }
267267

268+
/// Implicit conversion to std::string_view.
269+
operator std::string_view() const {
270+
return std::string_view(this->data(), this->size());
271+
}
272+
268273
explicit operator std::string() const {
269274
return std::string(this->data(), this->size());
270275
}

llvm/unittests/ADT/SmallStringTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,10 @@ TEST_F(SmallStringTest, GTestPrinter) {
244244
EXPECT_EQ(R"("foo")", ::testing::PrintToString(ErasedSmallString));
245245
}
246246

247+
TEST_F(SmallStringTest, StringView) {
248+
theString = "hello from std::string_view";
249+
EXPECT_EQ("hello from std::string_view",
250+
static_cast<std::string_view>(theString));
251+
}
252+
247253
} // namespace

0 commit comments

Comments
 (0)