QXmlStreamWriter: Refactor writeEscaped() to use switch

The character escaping logic in writeEscaped() previously relied on a
long if/else cascade to handle escaping and error detection.
This change replaces the cascade with a switch statement, improving the
control flow. The loop now exits when a non-null replacement is set.
To support this, cases that previously raised an error without assigning
a replacement now assign an empty, non-null string to trigger the break.

Fixes: QTBUG-136681
Change-Id: I4a584738bcda741f881712547af12abd35e2fd0a
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Magdalena Stojek 2025-05-08 12:36:56 +02:00
parent f958056701
commit c08766abf2

View File

@ -3208,43 +3208,53 @@ void QXmlStreamWriterPrivate::writeEscaped(QAnyStringView s, bool escapeWhitespa
while (it != end) {
auto next_it = it;
auto [uc, encodingError] = decoder(next_it, end);
if (uc == u'<') {
switch (uc) {
case u'<':
replacement = "&lt;"_L1;
break;
} else if (uc == u'>') {
case u'>':
replacement = "&gt;"_L1;
break;
} else if (uc == u'&') {
case u'&':
replacement = "&amp;"_L1;
break;
} else if (uc == u'\"') {
case u'\"':
replacement = "&quot;"_L1;
break;
} else if (uc == u'\t') {
if (escapeWhitespace) {
case u'\t':
if (escapeWhitespace)
replacement = "&#9;"_L1;
break;
}
} else if (uc == u'\n') {
if (escapeWhitespace) {
replacement = "&#10;"_L1;
break;
}
} else if (uc == u'\v' || uc == u'\f') {
raiseError(QXmlStreamWriter::Error::InvalidCharacter);
break;
} else if (uc == u'\r') {
if (escapeWhitespace) {
case u'\n':
if (escapeWhitespace)
replacement = "&#10;"_L1;
break;
case u'\r':
if (escapeWhitespace)
replacement = "&#13;"_L1;
break;
case u'\v':
case u'\f':
raiseError(QXmlStreamWriter::Error::InvalidCharacter);
replacement = ""_L1;
Q_ASSERT(!replacement.isNull());
break;
default:
if (uc > 0x1F)
break;
}
} else if (uc <= u'\x1F' || uc == u'\uFFFE' || uc == u'\uFFFF') {
if (encodingError)
raiseError(QXmlStreamWriter::Error::EncodingError);
else
raiseError(QXmlStreamWriter::Error::InvalidCharacter);
// ASCII control characters
Q_FALLTHROUGH();
case 0xFFFE:
case 0xFFFF:
raiseError(encodingError
? QXmlStreamWriter::Error::EncodingError
: QXmlStreamWriter::Error::InvalidCharacter);
replacement = ""_L1;
Q_ASSERT(!replacement.isNull());
break;
}
if (!replacement.isNull())
break;
it = next_it;
}