forked from qt-creator/qt-creator
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 <tim.jenssen@qt.io>
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
Reference in New Issue
Block a user