Port QXmlStreamWriterPrivate::write to QAnyStringView

Replace the const QString &s overload with a QASV
overload.

Remove the const char (&s)[N] and const XmlStringRef&
overloads, with the QASV overload they are redundant.

Leave the const char *s, qsizetype len overload for
now but make it call the QASV overload.

Task-number: QTBUG-103302
Change-Id: I4f92d76248d5b7531472056a51ca06aa25dbac01
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Mate Barany 2022-11-17 18:26:32 +01:00
parent 1523714bfa
commit 7741665a9c

View File

@ -20,6 +20,7 @@
#include <iterator> #include <iterator>
#include "qxmlstream_p.h" #include "qxmlstream_p.h"
#include "qxmlstreamparser_p.h" #include "qxmlstreamparser_p.h"
#include <private/qstringconverter_p.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -2807,11 +2808,9 @@ public:
delete device; delete device;
} }
void write(const XmlStringRef &); void write(QAnyStringView s);
void write(const QString &);
void writeEscaped(const QString &, bool escapeWhitespace = false); void writeEscaped(const QString &, bool escapeWhitespace = false);
void write(const char *s, qsizetype len); void write(const char *s, qsizetype len);
template <int N> void write(const char (&s)[N]) { write(s, N - 1); }
bool finishStartElement(bool contents = true); bool finishStartElement(bool contents = true);
void writeStartElement(const QString &namespaceUri, const QString &name); void writeStartElement(const QString &namespaceUri, const QString &name);
QIODevice *device; QIODevice *device;
@ -2827,7 +2826,6 @@ public:
QByteArray autoFormattingIndent; QByteArray autoFormattingIndent;
NamespaceDeclaration emptyNamespace; NamespaceDeclaration emptyNamespace;
qsizetype lastNamespaceDeclaration; qsizetype lastNamespaceDeclaration;
QStringEncoder toUtf8;
NamespaceDeclaration &findNamespace(const QString &namespaceUri, bool writeDeclaration = false, bool noDefault = false); NamespaceDeclaration &findNamespace(const QString &namespaceUri, bool writeDeclaration = false, bool noDefault = false);
void writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration); void writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration);
@ -2835,12 +2833,15 @@ public:
int namespacePrefixCount; int namespacePrefixCount;
void indent(int level); void indent(int level);
private:
void doWriteToDevice(QStringView s);
void doWriteToDevice(QUtf8StringView s);
void doWriteToDevice(QLatin1StringView s);
}; };
QXmlStreamWriterPrivate::QXmlStreamWriterPrivate(QXmlStreamWriter *q) QXmlStreamWriterPrivate::QXmlStreamWriterPrivate(QXmlStreamWriter *q)
: autoFormattingIndent(4, ' '), : autoFormattingIndent(4, ' ')
toUtf8(QStringEncoder::Utf8, QStringEncoder::Flag::Stateless)
{ {
q_ptr = q; q_ptr = q;
device = nullptr; device = nullptr;
@ -2856,42 +2857,18 @@ QXmlStreamWriterPrivate::QXmlStreamWriterPrivate(QXmlStreamWriter *q)
namespacePrefixCount = 0; namespacePrefixCount = 0;
} }
void QXmlStreamWriterPrivate::write(const XmlStringRef &s) void QXmlStreamWriterPrivate::write(QAnyStringView s)
{ {
if (device) { if (device) {
if (hasIoError) if (hasIoError)
return; return;
QByteArray bytes = toUtf8(s);
if (toUtf8.hasError()) {
hasEncodingError = true;
return;
}
if (device->write(bytes) != bytes.size())
hasIoError = true;
}
else if (stringDevice)
stringDevice->append(s);
else
qWarning("QXmlStreamWriter: No device");
}
void QXmlStreamWriterPrivate::write(const QString &s) s.visit([&] (auto s) { doWriteToDevice(s); });
{ } else if (stringDevice) {
if (device) { s.visit([&] (auto s) { stringDevice->append(s); });
if (hasIoError) } else {
return;
QByteArray bytes = toUtf8(s);
if (toUtf8.hasError()) {
hasEncodingError = true;
return;
}
if (device->write(bytes) != bytes.size())
hasIoError = true;
}
else if (stringDevice)
stringDevice->append(s);
else
qWarning("QXmlStreamWriter: No device"); qWarning("QXmlStreamWriter: No device");
}
} }
void QXmlStreamWriterPrivate::writeEscaped(const QString &s, bool escapeWhitespace) void QXmlStreamWriterPrivate::writeEscaped(const QString &s, bool escapeWhitespace)
@ -2945,18 +2922,9 @@ void QXmlStreamWriterPrivate::writeEscaped(const QString &s, bool escapeWhitespa
write(escaped); write(escaped);
} }
// Writes utf8
void QXmlStreamWriterPrivate::write(const char *s, qsizetype len) void QXmlStreamWriterPrivate::write(const char *s, qsizetype len)
{ {
if (device) { write(QUtf8StringView(s, len));
if (hasIoError)
return;
if (device->write(s, len) != len)
hasIoError = true;
return;
}
write(QString::fromUtf8(s, len));
} }
void QXmlStreamWriterPrivate::writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration) { void QXmlStreamWriterPrivate::writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration) {
@ -3034,6 +3002,31 @@ void QXmlStreamWriterPrivate::indent(int level)
write(autoFormattingIndent.constData(), autoFormattingIndent.size()); write(autoFormattingIndent.constData(), autoFormattingIndent.size());
} }
void QXmlStreamWriterPrivate::doWriteToDevice(QStringView s)
{
QStringEncoder toUtf8(QStringEncoder::Utf8, QStringEncoder::Flag::Stateless);
QByteArray bytes = toUtf8(s);
if (toUtf8.hasError()) {
hasEncodingError = true;
return;
}
doWriteToDevice(QUtf8StringView{bytes});
}
void QXmlStreamWriterPrivate::doWriteToDevice(QUtf8StringView s)
{
QByteArrayView bytes = s;
if (device->write(bytes.data(), bytes.size()) != bytes.size())
hasIoError = true;
}
void QXmlStreamWriterPrivate::doWriteToDevice(QLatin1StringView s)
{
QByteArray utf8(s.size() * 2, Qt::Uninitialized);
char *end = QUtf8::convertFromLatin1(utf8.data(), s);
utf8.truncate(end - utf8.data());
doWriteToDevice(QUtf8StringView{utf8});
}
/*! /*!
Constructs a stream writer. Constructs a stream writer.