[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
This commit is contained in:
Jonas Devlieghere 2020-01-29 09:39:46 -08:00
parent f5970fd81b
commit 59878130bd
2 changed files with 16 additions and 0 deletions

View File

@ -275,6 +275,8 @@ public:
/// 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();

View File

@ -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));