Slight code refactoring and perf improvement in vector option to string

This commit is contained in:
Maxime Coste 2017-06-24 12:24:24 +01:00
parent dc1b039282
commit c6eddefb0d
2 changed files with 8 additions and 14 deletions

View File

@ -62,22 +62,15 @@ constexpr char list_separator = ':';
template<typename T, MemoryDomain domain>
String option_to_string(const Vector<T, domain>& opt)
{
String res;
for (size_t i = 0; i < opt.size(); ++i)
{
res += escape(option_to_string(opt[i]), list_separator, '\\');
if (i != opt.size() - 1)
res += list_separator;
}
return res;
return join(opt | transform([](const T& t) { return option_to_string(t); }),
list_separator);
}
template<typename T, MemoryDomain domain>
void option_from_string(StringView str, Vector<T, domain>& opt)
{
opt.clear();
Vector<String> elems = split(str, list_separator, '\\');
for (auto& elem: elems)
for (auto& elem : split(str, list_separator, '\\'))
{
T opt_elem;
option_from_string(elem, opt_elem);
@ -90,9 +83,9 @@ bool option_add(Vector<T, domain>& opt, StringView str)
{
Vector<T, domain> vec;
option_from_string(str, vec);
std::copy(std::make_move_iterator(vec.begin()),
std::make_move_iterator(vec.end()),
back_inserter(opt));
opt.insert(opt.end(),
std::make_move_iterator(vec.begin()),
std::make_move_iterator(vec.end()));
return not vec.empty();
}

View File

@ -222,9 +222,10 @@ String escape(StringView str, StringView characters, char escape)
{
String res;
res.reserve(str.length());
auto cbeg = characters.begin(), cend = characters.end();
for (auto it = str.begin(), end = str.end(); it != end; )
{
auto next = std::find_if(it, end, [&characters](char c) { return contains(characters, c); });
auto next = std::find_first_of(it, end, cbeg, cend);
if (next != end)
{
res += StringView{it, next+1};