src/html.cpp

changeset 71
39644afa3808
parent 63
9ed068855450
--- a/src/html.cpp	Sun Nov 30 22:55:34 2025 +0100
+++ b/src/html.cpp	Tue Dec 16 21:23:17 2025 +0100
@@ -394,15 +394,19 @@
         static_cast<unsigned>(ymd.day()));
 
     // Utility function to escape strings in JSON
-    auto escape_json = [](std::string str) static {
-        size_t pos = str.find('\"');
-        if (pos == std::string::npos) return str;
-        std::string escaped = std::move(str);
-        do {
-            escaped.replace(pos, 1, "\\\"");
-            pos += 2;
-        } while ((pos = escaped.find('\"', pos)) != std::string::npos);
-        return escaped;
+    auto escape_json = [](std::string raw) static {
+        using std::string_view_literals::operator ""sv;
+        auto replace_all = [](std::string str, char chr, std::string_view repl) static {
+            size_t pos = str.find(chr);
+            if (pos == std::string::npos) return str;
+            std::string result = std::move(str);
+            do {
+                result.replace(pos, 1, repl);
+                pos += repl.length();
+            } while ((pos = result.find(chr, pos)) != std::string::npos);
+            return result;
+        };
+        return replace_all(replace_all(std::move(raw), '\\', "\\\\"), '\"', "\\\""sv);
     };
 
     // Build a JSON object of commit summaries

mercurial