summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2018-06-23 12:43:39 +1000
committerMaxime Coste <mawww@kakoune.org>2018-07-05 07:54:28 +1000
commit18dfecfa9d112e536829aef72b073f276c32dee1 (patch)
tree7930c9b112c9b09d21ffa15ed0bcc96ca4adc9e9
parent6a31d0ebc796ef216afb27404a0f900216c4ef2a (diff)
Introduce a "double_up" function for doubling up escaping
-rw-r--r--src/string_utils.cc19
-rw-r--r--src/string_utils.hh4
2 files changed, 22 insertions, 1 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");
}};
diff --git a/src/string_utils.hh b/src/string_utils.hh
index 61a28cad..558ba62f 100644
--- a/src/string_utils.hh
+++ b/src/string_utils.hh
@@ -128,9 +128,11 @@ StringView format_to(ArrayView<char> buffer, StringView fmt, Types&&... params)
return format_to(buffer, fmt, ArrayView<const StringView>{detail::format_param(std::forward<Types>(params))...});
}
+String double_up(StringView s, StringView characters);
+
inline String quote(StringView s)
{
- return format("'{}'", replace(s, "'", "''"));
+ return format("'{}'", double_up(s, "'"));
}
}