Commit d7049213 authored by Jonas Devlieghere's avatar Jonas Devlieghere
Browse files

[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
parent 91618d94
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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();
+14 −0
Original line number Diff line number Diff line
@@ -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));