summaryrefslogtreecommitdiff
path: root/src/json_ui.cc
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2016-03-07 22:38:37 +0000
committerMaxime Coste <frrrwww@gmail.com>2016-03-07 22:38:37 +0000
commita15cdeae6ee61a4304890400c647137db6afc049 (patch)
tree1ac767c26139fa389273c872a33ade5aa72fa015 /src/json_ui.cc
parent2626ce858f7316a86c28d406161500234c71fc1f (diff)
Fix json escaping of strings
Diffstat (limited to 'src/json_ui.cc')
-rw-r--r--src/json_ui.cc26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/json_ui.cc b/src/json_ui.cc
index eb576094..ee90eaa0 100644
--- a/src/json_ui.cc
+++ b/src/json_ui.cc
@@ -29,7 +29,31 @@ template<typename T, MemoryDomain D>
String to_json(const Vector<T, D>& vec) { return to_json(ArrayView<const T>{vec}); }
String to_json(int i) { return to_string(i); }
-String to_json(StringView s) { return format(R"("{}")", escape(s, "\"", '\\')); }
+String to_json(StringView str)
+{
+ String res;
+ res.reserve(str.length() + 4);
+ res += '"';
+ for (auto it = str.begin(), end = str.end(); it != end; )
+ {
+ auto next = std::find_if(it, end, [](char c) {
+ return c == '\\' or c == '"' or (c >= 0 and c <= 0x1F);
+ });
+
+ res += StringView{it, next};
+ if (next == end)
+ break;
+
+ char buf[7] = {'\\', *next, 0};
+ if (*next >= 0 and *next <= 0x1F)
+ sprintf(buf, "\\u%04x", *next);
+
+ res += buf;
+ it = next+1;
+ }
+ res += '"';
+ return res;
+}
String to_json(Color color)
{