[llvm] cff36bb - [ADT] Add std::string_view conversion to SmallString (#83397)

via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 29 12:27:55 PST 2024


Author: Aiden Grossman
Date: 2024-02-29T12:27:52-08:00
New Revision: cff36bb198759c4fe557adc594eabc097cf7d565

URL: https://github.com/llvm/llvm-project/commit/cff36bb198759c4fe557adc594eabc097cf7d565
DIFF: https://github.com/llvm/llvm-project/commit/cff36bb198759c4fe557adc594eabc097cf7d565.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/SmallString.h
    llvm/unittests/ADT/SmallStringTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/SmallString.h b/llvm/include/llvm/ADT/SmallString.h
index a5b9eec50c8257..ab5d483558cac8 100644
--- a/llvm/include/llvm/ADT/SmallString.h
+++ b/llvm/include/llvm/ADT/SmallString.h
@@ -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());
   }

diff  --git a/llvm/unittests/ADT/SmallStringTest.cpp b/llvm/unittests/ADT/SmallStringTest.cpp
index 2f4df8afeafa59..d68d73157988ab 100644
--- a/llvm/unittests/ADT/SmallStringTest.cpp
+++ b/llvm/unittests/ADT/SmallStringTest.cpp
@@ -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


        


More information about the llvm-commits mailing list