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

View File

@ -222,9 +222,10 @@ String escape(StringView str, StringView characters, char escape)
{ {
String res; String res;
res.reserve(str.length()); res.reserve(str.length());
auto cbeg = characters.begin(), cend = characters.end();
for (auto it = str.begin(), end = str.end(); it != 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) if (next != end)
{ {
res += StringView{it, next+1}; res += StringView{it, next+1};