// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include "string_escape.h" #include #include #include namespace vespalib { namespace { std::vector precompute_escaped_xml_chars() { std::vector 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 escaped_xml_chars = precompute_escaped_xml_chars(); template void do_write_xml_content_escaped(StreamT& out, vespalib::stringref str) { for (const char s : str) { if (escaped_xml_chars[static_cast(s)]) { if (s == '<') out << "<"; else if (s == '>') out << ">"; else if (s == '&') out << "&"; else { out << "&#" << static_cast(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(s)]) { if (s == '<') ost << "<"; else if (s == '>') ost << ">"; else if (s == '&') ost << "&"; else if (s == '"') ost << """; else if (s == '\'') ost << "'"; else { ost << "&#" << static_cast(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); } }