Commit 729c5353 authored by Marco Bubke's avatar Marco Bubke

Utils: Add intializer_list constructor in SmallString

You can now write

SmallString text = {"Oh ", women[4], " how much I miss you"};

if it can be casted to SmallStringView.

Change-Id: I86b69ee8d735017cac4391e7c4e68355eb5f227b
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 9b4afa01
......@@ -40,6 +40,8 @@
#include <cstdlib>
#include <climits>
#include <cstring>
#include <initializer_list>
#include <numeric>
#include <string>
#include <unordered_map>
#include <utility>
......@@ -135,6 +137,28 @@ public:
{
}
BasicSmallString(std::initializer_list<Utils::SmallStringView> list)
{
std::size_t size = std::accumulate(list.begin(),
list.end(),
std::size_t(0),
[] (std::size_t size, Utils::SmallStringView string) {
return size + string.size();
});
reserve(size);
setSize(size);
char *currentData = data();
for (Utils::SmallStringView string : list) {
std::memcpy(currentData, string.data(), string.size());
currentData += string.size();
}
at(size) = 0;
}
~BasicSmallString() noexcept
{
......
......@@ -1186,3 +1186,45 @@ TEST(SmallString, ManipulateNonConstSubscriptOperator)
ASSERT_THAT(text, SmallString{"some qext"});
}
TEST(SmallString, EmptyInitializerListContent)
{
SmallString text = {};
ASSERT_THAT(text, SmallString());
}
TEST(SmallString, EmptyInitializerListSize)
{
SmallString text = {};
ASSERT_THAT(text, SizeIs(0));
}
TEST(SmallString, EmptyInitializerListNullTerminated)
{
auto end = SmallString{{}}[0];
ASSERT_THAT(end, '\0');
}
TEST(SmallString, InitializerListContent)
{
SmallString text = {"some", " ", "text"};
ASSERT_THAT(text, SmallString("some text"));
}
TEST(SmallString, InitializerListSize)
{
SmallString text = {"some", " ", "text"};
ASSERT_THAT(text, SizeIs(9));
}
TEST(SmallString, InitializerListNullTerminated)
{
auto end = SmallString{"some", " ", "text"}[9];
ASSERT_THAT(end, '\0');
}
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