Commit 01e1f13b authored by Marco Bubke's avatar Marco Bubke

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's avatarTim Jenssen <tim.jenssen@qt.io>
parent 1b14ec50
......@@ -683,6 +683,46 @@ public:
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:
bool isShortString() const noexcept
{
......@@ -985,4 +1025,30 @@ std::vector<Type> clone(const std::vector<Type> &vector)
using SmallString = BasicSmallString<31>;
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
......@@ -1269,3 +1269,21 @@ TEST(SmallString, NumberToString)
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");
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment