forked from qt-creator/qt-creator
Utils: Add + operator to SmallString
It should not be used for chained concatenation. Use initializer list
instead.
auto text = in + 'x';
auto text = Utils::SmallString{in, "x", other, ", "};
Change-Id: I453d986913eae89fd5e1f525b9f4e0c4bd089467
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -683,6 +683,46 @@ public:
|
|||||||
return comparison < 0;
|
return comparison < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
friend BasicSmallString operator+(const BasicSmallString &first, const BasicSmallString &second)
|
||||||
|
{
|
||||||
|
BasicSmallString text;
|
||||||
|
text.reserve(first.size() + second.size());
|
||||||
|
|
||||||
|
text.append(first);
|
||||||
|
text.append(second);
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend BasicSmallString operator+(const BasicSmallString &first, SmallStringView second)
|
||||||
|
{
|
||||||
|
BasicSmallString text;
|
||||||
|
text.reserve(first.size() + second.size());
|
||||||
|
|
||||||
|
text.append(first);
|
||||||
|
text.append(second);
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
friend BasicSmallString operator+(SmallStringView first, const BasicSmallString &second)
|
||||||
|
{
|
||||||
|
return operator+(second, first);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<size_type ArraySize>
|
||||||
|
friend BasicSmallString operator+(const BasicSmallString &first, const char(&second)[ArraySize])
|
||||||
|
{
|
||||||
|
|
||||||
|
return operator+(first, SmallStringView(second));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<size_type ArraySize>
|
||||||
|
friend BasicSmallString operator+(const char(&first)[ArraySize], const BasicSmallString &second)
|
||||||
|
{
|
||||||
|
return operator+(SmallStringView(first), second);
|
||||||
|
}
|
||||||
|
|
||||||
unittest_public:
|
unittest_public:
|
||||||
bool isShortString() const noexcept
|
bool isShortString() const noexcept
|
||||||
{
|
{
|
||||||
@@ -985,4 +1025,30 @@ std::vector<Type> clone(const std::vector<Type> &vector)
|
|||||||
using SmallString = BasicSmallString<31>;
|
using SmallString = BasicSmallString<31>;
|
||||||
using PathString = BasicSmallString<190>;
|
using PathString = BasicSmallString<190>;
|
||||||
|
|
||||||
|
inline
|
||||||
|
SmallString operator+(SmallStringView first, SmallStringView second)
|
||||||
|
{
|
||||||
|
SmallString text;
|
||||||
|
text.reserve(first.size() + second.size());
|
||||||
|
|
||||||
|
text.append(first);
|
||||||
|
text.append(second);
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::size_t Size>
|
||||||
|
inline
|
||||||
|
SmallString operator+(SmallStringView first, const char(&second)[Size])
|
||||||
|
{
|
||||||
|
return operator+(first, SmallStringView(second));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::size_t Size>
|
||||||
|
inline
|
||||||
|
SmallString operator+(const char(&first)[Size], SmallStringView second)
|
||||||
|
{
|
||||||
|
return operator+(SmallStringView(first), second);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Utils
|
} // namespace Utils
|
||||||
|
|||||||
@@ -1269,3 +1269,21 @@ TEST(SmallString, NumberToString)
|
|||||||
ASSERT_THAT(SmallString::number(1.2), "1.200000");
|
ASSERT_THAT(SmallString::number(1.2), "1.200000");
|
||||||
ASSERT_THAT(SmallString::number(-1.2), "-1.200000");
|
ASSERT_THAT(SmallString::number(-1.2), "-1.200000");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(SmallString, StringViewPlusOperator)
|
||||||
|
{
|
||||||
|
SmallStringView text = "text";
|
||||||
|
|
||||||
|
auto result = text + " and more text";
|
||||||
|
|
||||||
|
ASSERT_THAT(result, "text and more text");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(SmallString, StringPlusOperator)
|
||||||
|
{
|
||||||
|
SmallString text = "text";
|
||||||
|
|
||||||
|
auto result = text + " and more text";
|
||||||
|
|
||||||
|
ASSERT_THAT(result, "text and more text");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user