MCPcopy Create free account
hub / github.com/awslabs/aws-lambda-cpp / EscapeXml

Method EscapeXml

tests/gtest/gtest-all.cc:5086–5127  ·  view source on GitHub ↗

Returns an XML-escaped copy of the input string str. If is_attribute is true, the text is meant to appear as an attribute value, and normalizable whitespace is preserved by replacing it with character references. Invalid XML characters in str, if any, are stripped from the output. It is expected that most, if not all, of the text processed by this module will consist of ordinary English text. If

Source from the content-addressed store, hash-verified

5084// If this module is ever modified to produce version 1.1 XML output,
5085// most invalid characters can be retained using character references.
5086std::string XmlUnitTestResultPrinter::EscapeXml(
5087 const std::string& str, bool is_attribute) {
5088 Message m;
5089
5090 for (size_t i = 0; i < str.size(); ++i) {
5091 const char ch = str[i];
5092 switch (ch) {
5093 case '<':
5094 m << "&lt;";
5095 break;
5096 case '>':
5097 m << "&gt;";
5098 break;
5099 case '&':
5100 m << "&amp;";
5101 break;
5102 case '\'':
5103 if (is_attribute)
5104 m << "&apos;";
5105 else
5106 m << '\'';
5107 break;
5108 case '"':
5109 if (is_attribute)
5110 m << "&quot;";
5111 else
5112 m << '"';
5113 break;
5114 default:
5115 if (IsValidXmlCharacter(ch)) {
5116 if (is_attribute && IsNormalizableWhitespace(ch))
5117 m << "&#x" << String::FormatByte(static_cast<unsigned char>(ch))
5118 << ";";
5119 else
5120 m << ch;
5121 }
5122 break;
5123 }
5124 }
5125
5126 return m.GetString();
5127}
5128
5129// Returns the given string with all characters invalid in XML removed.
5130// Currently invalid characters are dropped from the string. An

Callers

nothing calls this directly

Calls 2

GetStringMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected