MCPcopy Create free account
hub / github.com/FidoProject/Fido / XmlEncode

Class XmlEncode

tests/catch.h:8854–8907  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8852namespace Catch {
8853
8854 class XmlEncode {
8855 public:
8856 enum ForWhat { ForTextNodes, ForAttributes };
8857
8858 XmlEncode( std::string const& str, ForWhat forWhat = ForTextNodes )
8859 : m_str( str ),
8860 m_forWhat( forWhat )
8861 {}
8862
8863 void encodeTo( std::ostream& os ) const {
8864
8865 // Apostrophe escaping not necessary if we always use " to write attributes
8866 // (see: http://www.w3.org/TR/xml/#syntax)
8867
8868 for( std::size_t i = 0; i < m_str.size(); ++ i ) {
8869 char c = m_str[i];
8870 switch( c ) {
8871 case '<': os << "&lt;"; break;
8872 case '&': os << "&amp;"; break;
8873
8874 case '>':
8875 // See: http://www.w3.org/TR/xml/#syntax
8876 if( i > 2 && m_str[i-1] == ']' && m_str[i-2] == ']' )
8877 os << "&gt;";
8878 else
8879 os << c;
8880 break;
8881
8882 case '\"':
8883 if( m_forWhat == ForAttributes )
8884 os << "&quot;";
8885 else
8886 os << c;
8887 break;
8888
8889 default:
8890 // Escape control chars - based on contribution by @espenalb in PR #465
8891 if ( ( c < '\x09' ) || ( c > '\x0D' && c < '\x20') || c=='\x7F' )
8892 os << "&#x" << std::uppercase << std::hex << static_cast<int>( c );
8893 else
8894 os << c;
8895 }
8896 }
8897 }
8898
8899 friend std::ostream& operator << ( std::ostream& os, XmlEncode const& xmlEncode ) {
8900 xmlEncode.encodeTo( os );
8901 return os;
8902 }
8903
8904 private:
8905 std::string m_str;
8906 ForWhat m_forWhat;
8907 };
8908
8909 class XmlWriter {
8910 public:

Callers 1

XmlWriterClass · 0.85

Calls 1

encodeToMethod · 0.80

Tested by

no test coverage detected