diff options
| author | Magdalena Stojek <magdalena.stojek@qt.io> | 2025-05-08 12:36:56 +0200 |
|---|---|---|
| committer | Magdalena Stojek <magdalena.stojek@qt.io> | 2025-05-10 13:03:17 +0200 |
| commit | c08766abf2267f4eeda159c5688309d22d319722 (patch) | |
| tree | c56bf39061b0331c591df23e53844341bbabae90 /src/corelib/serialization/qxmlstream.cpp | |
| parent | f95805670161b38da60602ada6128f03f7f9ceb2 (diff) | |
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>
Diffstat (limited to 'src/corelib/serialization/qxmlstream.cpp')
| -rw-r--r-- | src/corelib/serialization/qxmlstream.cpp | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/src/corelib/serialization/qxmlstream.cpp b/src/corelib/serialization/qxmlstream.cpp index 99a99273ade..9949da2d3af 100644 --- a/src/corelib/serialization/qxmlstream.cpp +++ b/src/corelib/serialization/qxmlstream.cpp @@ -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 = "<"_L1; break; - } else if (uc == u'>') { + case u'>': replacement = ">"_L1; break; - } else if (uc == u'&') { + case u'&': replacement = "&"_L1; break; - } else if (uc == u'\"') { + case u'\"': replacement = """_L1; break; - } else if (uc == u'\t') { - if (escapeWhitespace) { + case u'\t': + if (escapeWhitespace) replacement = "	"_L1; - break; - } - } else if (uc == u'\n') { - if (escapeWhitespace) { + break; + case u'\n': + if (escapeWhitespace) replacement = " "_L1; - break; - } - } else if (uc == u'\v' || uc == u'\f') { - raiseError(QXmlStreamWriter::Error::InvalidCharacter); break; - } else if (uc == u'\r') { - if (escapeWhitespace) { + case u'\r': + if (escapeWhitespace) replacement = " "_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; } |
