MCPcopy Create free account
hub / github.com/apache/singa / EscapeXml

Method EscapeXml

test/gtest/gtest-all.cc:4623–4664  ·  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

4621// TODO(wan): It might be nice to have a minimally invasive, human-readable
4622// escaping scheme for invalid characters, rather than dropping them.
4623std::string XmlUnitTestResultPrinter::EscapeXml(
4624 const std::string& str, bool is_attribute) {
4625 Message m;
4626
4627 for (size_t i = 0; i < str.size(); ++i) {
4628 const char ch = str[i];
4629 switch (ch) {
4630 case '<':
4631 m << "&lt;";
4632 break;
4633 case '>':
4634 m << "&gt;";
4635 break;
4636 case '&':
4637 m << "&amp;";
4638 break;
4639 case '\'':
4640 if (is_attribute)
4641 m << "&apos;";
4642 else
4643 m << '\'';
4644 break;
4645 case '"':
4646 if (is_attribute)
4647 m << "&quot;";
4648 else
4649 m << '"';
4650 break;
4651 default:
4652 if (IsValidXmlCharacter(ch)) {
4653 if (is_attribute && IsNormalizableWhitespace(ch))
4654 m << "&#x" << String::FormatByte(static_cast<unsigned char>(ch))
4655 << ";";
4656 else
4657 m << ch;
4658 }
4659 break;
4660 }
4661 }
4662
4663 return m.GetString();
4664}
4665
4666// Returns the given string with all characters invalid in XML removed.
4667// 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