diff options
| author | Maxime Coste <mawww@kakoune.org> | 2018-06-23 12:43:39 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2018-07-05 07:54:28 +1000 |
| commit | 18dfecfa9d112e536829aef72b073f276c32dee1 (patch) | |
| tree | 7930c9b112c9b09d21ffa15ed0bcc96ca4adc9e9 /src/string_utils.cc | |
| parent | 6a31d0ebc796ef216afb27404a0f900216c4ef2a (diff) | |
Introduce a "double_up" function for doubling up escaping
Diffstat (limited to 'src/string_utils.cc')
| -rw-r--r-- | src/string_utils.cc | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/string_utils.cc b/src/string_utils.cc index 497b53fd..742a3f19 100644 --- a/src/string_utils.cc +++ b/src/string_utils.cc @@ -339,6 +339,23 @@ String format(StringView fmt, ArrayView<const StringView> params) return res; } +String double_up(StringView s, StringView characters) +{ + String res; + auto pos = s.begin(); + for (auto it = s.begin(), end = s.end(); it != end; ++it) + { + if (contains(characters, *it)) + { + res += StringView{pos, it+1}; + res += *it; + pos = it+1; + } + } + res += StringView{pos, s.end()}; + return res; +} + UnitTest test_string{[]() { kak_assert(String("youpi ") + "matin" == "youpi matin"); @@ -382,6 +399,8 @@ UnitTest test_string{[]() kak_assert(str_to_int("00") == 0); kak_assert(str_to_int("-0") == 0); + kak_assert(double_up(R"('foo%"bar"')", "'\"%") == R"(''foo%%""bar""'')"); + kak_assert(replace("tchou/tcha/tchi", "/", "!!") == "tchou!!tcha!!tchi"); }}; |
