diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2013-07-24 22:37:17 +0200 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2013-07-24 22:37:17 +0200 |
| commit | b5db256384f981833f85ad9019be6e212fffcc6b (patch) | |
| tree | fa2f6048c408a7fa885786f236cd3c3f604bea97 /src/string.cc | |
| parent | d6425f1d5090c3225665fff9dcf36e64ff54fb06 (diff) | |
string escaping support functions
the split function now takes an additional escape parameter and
does not split on separators that have the escaper before it.
An utility escape function that adds escape before separators
is also added.
Diffstat (limited to 'src/string.cc')
| -rw-r--r-- | src/string.cc | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/src/string.cc b/src/string.cc index 2c6bb140..dcfc1037 100644 --- a/src/string.cc +++ b/src/string.cc @@ -5,7 +5,7 @@ namespace Kakoune { -std::vector<String> split(const String& str, char separator) +std::vector<String> split(const String& str, char separator, char escape) { auto begin = str.begin(); auto end = str.begin(); @@ -13,12 +13,40 @@ std::vector<String> split(const String& str, char separator) std::vector<String> res; while (end != str.end()) { - while (end != str.end() and *end != separator) - ++end; - res.push_back(String(begin, end)); - if (end == str.end()) - break; - begin = ++end; + res.emplace_back(); + String& element = res.back(); + while (end != str.end()) + { + auto c = *end; + if (c == escape and end + 1 != end and *(end+1) == separator) + { + element += separator; + end += 2; + } + else if (c == separator) + { + ++end; + break; + } + else + { + element += c; + ++end; + } + } + begin = end; + } + return res; +} + +String escape(const String& str, char character, char escape) +{ + String res; + for (auto& c : str) + { + if (c == character) + res += escape; + res += c; } return res; } |
