[llvm] d704921 - [SmallString] Add explicit conversion to std::string
Jonas Devlieghere via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 29 10:17:24 PST 2020
Author: Jonas Devlieghere
Date: 2020-01-29T10:17:10-08:00
New Revision: d7049213d0fcda691c9e79f9b41e357198d99738
URL: https://github.com/llvm/llvm-project/commit/d7049213d0fcda691c9e79f9b41e357198d99738
DIFF: https://github.com/llvm/llvm-project/commit/d7049213d0fcda691c9e79f9b41e357198d99738.diff
LOG: [SmallString] Add explicit conversion to std::string
With the conversion between StringRef and std::string now being
explicit, converting SmallStrings becomes more tedious. This patch adds
an explicit operator so you can write std::string(Str) instead of
Str.str().str().
Differential revision: https://reviews.llvm.org/D73640
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 898be80d0324..ad0115e8c89a 100644
--- a/llvm/include/llvm/ADT/SmallString.h
+++ b/llvm/include/llvm/ADT/SmallString.h
@@ -275,6 +275,8 @@ class SmallString : public SmallVector<char, InternalLen> {
/// Implicit conversion to StringRef.
operator StringRef() const { return str(); }
+ explicit operator std::string() const { return str().str(); }
+
// Extra operators.
const SmallString &operator=(StringRef RHS) {
this->clear();
diff --git a/llvm/unittests/ADT/SmallStringTest.cpp b/llvm/unittests/ADT/SmallStringTest.cpp
index 686215fd2238..e78da9fe5289 100644
--- a/llvm/unittests/ADT/SmallStringTest.cpp
+++ b/llvm/unittests/ADT/SmallStringTest.cpp
@@ -96,6 +96,20 @@ TEST_F(SmallStringTest, AppendSmallVector) {
EXPECT_STREQ("abcabc", theString.c_str());
}
+TEST_F(SmallStringTest, StringRefConversion) {
+ StringRef abc = "abc";
+ theString.assign(abc.begin(), abc.end());
+ StringRef theStringRef = theString;
+ EXPECT_EQ("abc", theStringRef);
+}
+
+TEST_F(SmallStringTest, StdStringConversion) {
+ StringRef abc = "abc";
+ theString.assign(abc.begin(), abc.end());
+ std::string theStdString = std::string(theString);
+ EXPECT_EQ("abc", theStdString);
+}
+
TEST_F(SmallStringTest, Substr) {
theString = "hello";
EXPECT_EQ("lo", theString.substr(3));
More information about the llvm-commits
mailing list