diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2015-08-13 22:04:21 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2015-08-13 22:04:21 +0100 |
| commit | d2b82f507ffcc7e6b04fcab3a6df3504f8832424 (patch) | |
| tree | 0e52d44a3454037d727a0123c154d5790baa90a3 /src/string.cc | |
| parent | 7a36a4644ef72fc85ff8bec690a7e83205f2b8ad (diff) | |
More performant escape/unescape
Diffstat (limited to 'src/string.cc')
| -rw-r--r-- | src/string.cc | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/src/string.cc b/src/string.cc index e4b5c0e2..d9689938 100644 --- a/src/string.cc +++ b/src/string.cc @@ -61,11 +61,21 @@ String escape(StringView str, StringView characters, char escape) { String res; res.reserve(str.length()); - for (auto& c : str) + for (auto it = str.begin(), end = str.end(); it != end; ) { - if (contains(characters, c)) - res += escape; - res += c; + auto next = std::find_if(it, end, [&characters](char c) { return contains(characters, c); }); + if (next != end) + { + res += StringView{it, next+1}; + res.back() = escape; + res += *next; + it = next+1; + } + else + { + res += StringView{it, next}; + break; + } } return res; } @@ -74,12 +84,17 @@ String unescape(StringView str, StringView characters, char escape) { String res; res.reserve(str.length()); - for (auto& c : str) + for (auto it = str.begin(), end = str.end(); it != end; ) { - if (contains(characters, c) and not res.empty() and res.back() == escape) - res.back() = c; + auto next = std::find(it, end, escape); + if (next != end and next+1 != end and contains(characters, *(next+1))) + { + res += StringView{it, next+1}; + res.back() = *(next+1); + } else - res += c; + res += StringView{it, next == end ? next : next + 1}; + it = next == end ? next : next + 1; } return res; } |
