MCPcopy Create free account
hub / github.com/Predelnik/DSpellCheck / encodeTo

Method encodeTo

deps/Catch/catch.hpp:15361–15453  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15359 {}
15360
15361 void XmlEncode::encodeTo( std::ostream& os ) const {
15362 // Apostrophe escaping not necessary if we always use " to write attributes
15363 // (see: http://www.w3.org/TR/xml/#syntax)
15364
15365 for( std::size_t idx = 0; idx < m_str.size(); ++ idx ) {
15366 unsigned char c = m_str[idx];
15367 switch (c) {
15368 case '<': os << "&lt;"; break;
15369 case '&': os << "&amp;"; break;
15370
15371 case '>':
15372 // See: http://www.w3.org/TR/xml/#syntax
15373 if (idx > 2 && m_str[idx - 1] == ']' && m_str[idx - 2] == ']')
15374 os << "&gt;";
15375 else
15376 os << c;
15377 break;
15378
15379 case '\"':
15380 if (m_forWhat == ForAttributes)
15381 os << "&quot;";
15382 else
15383 os << c;
15384 break;
15385
15386 default:
15387 // Check for control characters and invalid utf-8
15388
15389 // Escape control characters in standard ascii
15390 // see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0
15391 if (c < 0x09 || (c > 0x0D && c < 0x20) || c == 0x7F) {
15392 hexEscapeChar(os, c);
15393 break;
15394 }
15395
15396 // Plain ASCII: Write it to stream
15397 if (c < 0x7F) {
15398 os << c;
15399 break;
15400 }
15401
15402 // UTF-8 territory
15403 // Check if the encoding is valid and if it is not, hex escape bytes.
15404 // Important: We do not check the exact decoded values for validity, only the encoding format
15405 // First check that this bytes is a valid lead byte:
15406 // This means that it is not encoded as 1111 1XXX
15407 // Or as 10XX XXXX
15408 if (c < 0xC0 ||
15409 c >= 0xF8) {
15410 hexEscapeChar(os, c);
15411 break;
15412 }
15413
15414 auto encBytes = trailingBytes(c);
15415 // Are there enough bytes left to avoid accessing out-of-bounds memory?
15416 if (idx + encBytes - 1 >= m_str.size()) {
15417 hexEscapeChar(os, c);
15418 break;

Callers 1

catch.hppFile · 0.80

Calls 4

hexEscapeCharFunction · 0.85
trailingBytesFunction · 0.85
headerValueFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected