[llvm] [ADT] Add std::string_view conversion to SmallString (PR #83397)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 29 01:55:59 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-adt
Author: Aiden Grossman (boomanaiden154)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/83397.diff
2 Files Affected:
- (modified) llvm/include/llvm/ADT/SmallString.h (+5)
- (modified) llvm/unittests/ADT/SmallStringTest.cpp (+6)
``````````diff
diff --git a/llvm/include/llvm/ADT/SmallString.h b/llvm/include/llvm/ADT/SmallString.h
index a5b9eec50c8257..df03398c804a77 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
``````````
</details>
https://github.com/llvm/llvm-project/pull/83397
More information about the llvm-commits
mailing list