forked from OSchip/llvm-project
[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:
parent
91618d940e
commit
d7049213d0
|
@ -275,6 +275,8 @@ public:
|
||||||
/// Implicit conversion to StringRef.
|
/// Implicit conversion to StringRef.
|
||||||
operator StringRef() const { return str(); }
|
operator StringRef() const { return str(); }
|
||||||
|
|
||||||
|
explicit operator std::string() const { return str().str(); }
|
||||||
|
|
||||||
// Extra operators.
|
// Extra operators.
|
||||||
const SmallString &operator=(StringRef RHS) {
|
const SmallString &operator=(StringRef RHS) {
|
||||||
this->clear();
|
this->clear();
|
||||||
|
|
|
@ -96,6 +96,20 @@ TEST_F(SmallStringTest, AppendSmallVector) {
|
||||||
EXPECT_STREQ("abcabc", theString.c_str());
|
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) {
|
TEST_F(SmallStringTest, Substr) {
|
||||||
theString = "hello";
|
theString = "hello";
|
||||||
EXPECT_EQ("lo", theString.substr(3));
|
EXPECT_EQ("lo", theString.substr(3));
|
||||||
|
|
Loading…
Reference in New Issue