MCPcopy Create free account
hub / github.com/EmbeddedRPC/erpc / EscapeXml

Method EscapeXml

test/common/gtest/gtest.cpp:4916–4957  ·  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

4914// TODO(wan): It might be nice to have a minimally invasive, human-readable
4915// escaping scheme for invalid characters, rather than dropping them.
4916std::string XmlUnitTestResultPrinter::EscapeXml(
4917 const std::string& str, bool is_attribute) {
4918 Message m;
4919
4920 for (size_t i = 0; i < str.size(); ++i) {
4921 const char ch = str[i];
4922 switch (ch) {
4923 case '<':
4924 m << "&lt;";
4925 break;
4926 case '>':
4927 m << "&gt;";
4928 break;
4929 case '&':
4930 m << "&amp;";
4931 break;
4932 case '\'':
4933 if (is_attribute)
4934 m << "&apos;";
4935 else
4936 m << '\'';
4937 break;
4938 case '"':
4939 if (is_attribute)
4940 m << "&quot;";
4941 else
4942 m << '"';
4943 break;
4944 default:
4945 if (IsValidXmlCharacter(ch)) {
4946 if (is_attribute && IsNormalizableWhitespace(ch))
4947 m << "&#x" << String::FormatByte(static_cast<unsigned char>(ch))
4948 << ";";
4949 else
4950 m << ch;
4951 }
4952 break;
4953 }
4954 }
4955
4956 return m.GetString();
4957}
4958
4959// Returns the given string with all characters invalid in XML removed.
4960// 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