QXmlStreamReader: use std::optional

The optional-like FastScanNameResult was used to make some previous
changes backport-able to Qt 5.15 (std::optional is C++17 whereas Qt 5.15
requires C++14).

Amends commit 6326bec46a618c72feba4a2bb994c4d475050aed.

Change-Id: I409e1da83f82927c1eb24c47c1414c0c7ab1bf5b
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit effb3bddf63eb6c53d81ce6b0a30be6e3e80e189)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ahmad Samir 2023-04-18 13:19:57 +02:00 committed by Qt Cherry-pick Bot
parent a3a5d805c9
commit eea8c04e6f
2 changed files with 8 additions and 17 deletions

View File

@ -1297,8 +1297,7 @@ inline qsizetype QXmlStreamReaderPrivate::fastScanContentCharList()
} }
// Fast scan an XML attribute name (e.g. "xml:lang"). // Fast scan an XML attribute name (e.g. "xml:lang").
inline QXmlStreamReaderPrivate::FastScanNameResult inline std::optional<qsizetype> QXmlStreamReaderPrivate::fastScanName(Value *val)
QXmlStreamReaderPrivate::fastScanName(Value *val)
{ {
qsizetype n = 0; qsizetype n = 0;
uint c; uint c;
@ -1307,7 +1306,7 @@ QXmlStreamReaderPrivate::fastScanName(Value *val)
// This is too long to be a sensible name, and // This is too long to be a sensible name, and
// can exhaust memory, or the range of decltype(*prefix) // can exhaust memory, or the range of decltype(*prefix)
raiseNamePrefixTooLongError(); raiseNamePrefixTooLongError();
return {}; return std::nullopt;
} }
switch (c) { switch (c) {
case '\n': case '\n':
@ -1341,18 +1340,18 @@ QXmlStreamReaderPrivate::fastScanName(Value *val)
putChar(':'); putChar(':');
--n; --n;
} }
return FastScanNameResult(n); return n;
case ':': case ':':
if (val) { if (val) {
if (val->prefix == 0) { if (val->prefix == 0) {
val->prefix = qint16(n + 2); val->prefix = qint16(n + 2);
} else { // only one colon allowed according to the namespace spec. } else { // only one colon allowed according to the namespace spec.
putChar(c); putChar(c);
return FastScanNameResult(n); return n;
} }
} else { } else {
putChar(c); putChar(c);
return FastScanNameResult(n); return n;
} }
Q_FALLTHROUGH(); Q_FALLTHROUGH();
default: default:
@ -1366,7 +1365,7 @@ QXmlStreamReaderPrivate::fastScanName(Value *val)
qsizetype pos = textBuffer.size() - n; qsizetype pos = textBuffer.size() - n;
putString(textBuffer, pos); putString(textBuffer, pos);
textBuffer.resize(pos); textBuffer.resize(pos);
return FastScanNameResult(0); return 0;
} }
enum NameChar { NameBeginning, NameNotBeginning, NotName }; enum NameChar { NameBeginning, NameNotBeginning, NotName };

View File

@ -21,6 +21,7 @@
#include <memory> #include <memory>
#include <optional>
#ifndef QXMLSTREAM_P_H #ifndef QXMLSTREAM_P_H
#define QXMLSTREAM_P_H #define QXMLSTREAM_P_H
@ -498,16 +499,7 @@ public:
qsizetype fastScanLiteralContent(); qsizetype fastScanLiteralContent();
qsizetype fastScanSpace(); qsizetype fastScanSpace();
qsizetype fastScanContentCharList(); qsizetype fastScanContentCharList();
std::optional<qsizetype> fastScanName(Value *val = nullptr);
struct FastScanNameResult {
FastScanNameResult() : ok(false) {}
explicit FastScanNameResult(qsizetype len) : addToLen(len), ok(true) { }
operator bool() { return ok; }
qsizetype operator*() { Q_ASSERT(ok); return addToLen; }
qsizetype addToLen;
bool ok;
};
FastScanNameResult fastScanName(Value *val = nullptr);
inline qsizetype fastScanNMTOKEN(); inline qsizetype fastScanNMTOKEN();