diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2016-03-24 23:25:58 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2016-03-24 23:25:58 +0000 |
| commit | e5afacba700b4404e3bd97534d32390a5ef3ee82 (patch) | |
| tree | a251cb9a85d233fe2c116a4555c7761688c434d1 /src/string.cc | |
| parent | 7b52b00b94f87a15430408390033a3e8c9492b1e (diff) | |
Optimize split implementation, avoid growing strings char by char
Diffstat (limited to 'src/string.cc')
| -rw-r--r-- | src/string.cc | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/string.cc b/src/string.cc index 2e09be2e..15abcb38 100644 --- a/src/string.cc +++ b/src/string.cc @@ -135,6 +135,7 @@ Vector<String> split(StringView str, char separator, char escape) { Vector<String> res; auto it = str.begin(); + auto start = it; while (it != str.end()) { res.emplace_back(); @@ -144,21 +145,24 @@ Vector<String> split(StringView str, char separator, char escape) auto c = *it; if (c == escape and it + 1 != str.end() and *(it+1) == separator) { - element += separator; + element += StringView{start, it+1}; + element.back() = separator; it += 2; + start = it; } else if (c == separator) { + element += StringView{start, it}; ++it; + start = it; break; } else - { - element += c; ++it; - } } } + if (start != str.end()) + res.back() += StringView{start, str.end()}; return res; } |
