1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-20 18:11:36 +03:00

Optimize split implementation, avoid growing strings char by char

This commit is contained in:
Maxime Coste 2016-03-24 23:25:58 +00:00
parent 7b52b00b94
commit e5afacba70

View File

@ -135,6 +135,7 @@ Vector<String> split(StringView str, char separator, char escape)
{ {
Vector<String> res; Vector<String> res;
auto it = str.begin(); auto it = str.begin();
auto start = it;
while (it != str.end()) while (it != str.end())
{ {
res.emplace_back(); res.emplace_back();
@ -144,21 +145,24 @@ Vector<String> split(StringView str, char separator, char escape)
auto c = *it; auto c = *it;
if (c == escape and it + 1 != str.end() and *(it+1) == separator) if (c == escape and it + 1 != str.end() and *(it+1) == separator)
{ {
element += separator; element += StringView{start, it+1};
element.back() = separator;
it += 2; it += 2;
start = it;
} }
else if (c == separator) else if (c == separator)
{ {
element += StringView{start, it};
++it; ++it;
start = it;
break; break;
} }
else else
{
element += c;
++it; ++it;
}
} }
} }
if (start != str.end())
res.back() += StringView{start, str.end()};
return res; return res;
} }