aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/string_escape.cpp
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2022-09-05 19:50:43 +0200
committerGitHub <noreply@github.com>2022-09-05 19:50:43 +0200
commit3c9c1d909476896b16ba750090f042e7824b50b2 (patch)
treebdddf72f0cce5b1b309bb61fc60a7f0bf77d8bb1 /vespalib/src/vespa/vespalib/util/string_escape.cpp
parentb81ed9944fd93513b18c48f8fc84d9aeec8615a5 (diff)
parent506f285043535af5d81fd098dfd28166930704e5 (diff)
Merge pull request #23934 from vespa-engine/vekterli/factor-out-xml-string-escapingv8.48.22
Factor out XML string escaping and use for internal legacy status pages [run-systemtest]
Diffstat (limited to 'vespalib/src/vespa/vespalib/util/string_escape.cpp')
-rw-r--r--vespalib/src/vespa/vespalib/util/string_escape.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/vespalib/src/vespa/vespalib/util/string_escape.cpp b/vespalib/src/vespa/vespalib/util/string_escape.cpp
new file mode 100644
index 00000000000..d1b38f84c3e
--- /dev/null
+++ b/vespalib/src/vespa/vespalib/util/string_escape.cpp
@@ -0,0 +1,79 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include "string_escape.h"
+#include <vespa/vespalib/stllike/asciistream.h>
+#include <vector>
+#include <ostream>
+
+namespace vespalib {
+
+namespace {
+
+std::vector<bool> precompute_escaped_xml_chars() {
+ std::vector<bool> vec(256, false);
+ for (uint32_t i=0; i<32; ++i) {
+ vec[i] = true;
+ }
+ vec['\n'] = false;
+ vec['<'] = true;
+ vec['>'] = true;
+ vec['&'] = true;
+ return vec;
+}
+
+std::vector<bool> escaped_xml_chars = precompute_escaped_xml_chars();
+
+template <typename StreamT>
+void do_write_xml_content_escaped(StreamT& out, vespalib::stringref str) {
+ for (const char s : str) {
+ if (escaped_xml_chars[static_cast<uint8_t>(s)]) {
+ if (s == '<') out << "&lt;";
+ else if (s == '>') out << "&gt;";
+ else if (s == '&') out << "&amp;";
+ else {
+ out << "&#" << static_cast<int>(s) << ";";
+ }
+ } else {
+ out << s;
+ }
+ }
+}
+
+}
+
+vespalib::string xml_attribute_escaped(vespalib::stringref str) {
+ vespalib::asciistream ost;
+ for (const char s : str) {
+ if (s == '"' || s == '\'' || s == '\n'
+ || escaped_xml_chars[static_cast<uint8_t>(s)])
+ {
+ if (s == '<') ost << "&lt;";
+ else if (s == '>') ost << "&gt;";
+ else if (s == '&') ost << "&amp;";
+ else if (s == '"') ost << "&quot;";
+ else if (s == '\'') ost << "&#39;";
+ else {
+ ost << "&#" << static_cast<int>(s) << ";";
+ }
+ } else {
+ ost << s;
+ }
+ }
+ return ost.str();
+}
+
+vespalib::string xml_content_escaped(vespalib::stringref str) {
+ vespalib::asciistream out;
+ do_write_xml_content_escaped(out, str);
+ return out.str();
+}
+
+void write_xml_content_escaped(vespalib::asciistream& out, vespalib::stringref str) {
+ do_write_xml_content_escaped(out, str);
+}
+
+void write_xml_content_escaped(std::ostream& out, vespalib::stringref str) {
+ do_write_xml_content_escaped(out, str);
+}
+
+}