diff options
author | Jan Arve Saether <jan-arve.saether@digia.com> | 2012-11-20 10:11:44 (GMT) |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-11-23 03:53:10 (GMT) |
commit | 169ba759c536fcd807ee061e1831e4502a5f1416 (patch) | |
tree | 1e81cda11d7161ce1b4d2a3464e3c85cf4ae1129 /src/corelib | |
parent | 96311def2466dd44de64d77a1c815b22fbf68f71 (diff) | |
download | Qt-169ba759c536fcd807ee061e1831e4502a5f1416.zip Qt-169ba759c536fcd807ee061e1831e4502a5f1416.tar.gz Qt-169ba759c536fcd807ee061e1831e4502a5f1416.tar.bz2 |
Fix regression in QXmlStreamWriter with codec set to UTF-16
Actually, this broke using QXmlStreamWriter with any codec
where characters in the ASCII range have a different encoding
than the ASCII standard.
This was a regression from 558fe9383ba0aecbec09cc411c0ebab132aac137
Backported from Qt 5 (035d93a6e1cbde76d34866ffd9b39633572e6236),
and changed QStringLiteral to QLatin1String
Task-number: QTBUG-23310
Change-Id: Iaaf25463737382ace1bdb83138c7dd634d825b30
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
Diffstat (limited to 'src/corelib')
-rw-r--r-- | src/corelib/xml/qxmlstream.cpp | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/src/corelib/xml/qxmlstream.cpp b/src/corelib/xml/qxmlstream.cpp index 2f414e4..0841813 100644 --- a/src/corelib/xml/qxmlstream.cpp +++ b/src/corelib/xml/qxmlstream.cpp @@ -2986,6 +2986,7 @@ public: uint wroteSomething :1; uint hasError :1; uint autoFormatting :1; + uint isCodecASCIICompatible :1; QByteArray autoFormattingIndent; NamespaceDeclaration emptyNamespace; int lastNamespaceDeclaration; @@ -2994,6 +2995,7 @@ public: QTextCodec *codec; QTextEncoder *encoder; #endif + void checkIfASCIICompatibleCodec(); NamespaceDeclaration &findNamespace(const QString &namespaceUri, bool writeDeclaration = false, bool noDefault = false); void writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration); @@ -3015,6 +3017,7 @@ QXmlStreamWriterPrivate::QXmlStreamWriterPrivate(QXmlStreamWriter *q) codec = QTextCodec::codecForMib(106); // utf8 encoder = codec->makeEncoder(QTextCodec::IgnoreHeader); // no byte order mark for utf8 #endif + checkIfASCIICompatibleCodec(); inStartElement = inEmptyElement = false; wroteSomething = false; hasError = false; @@ -3024,6 +3027,18 @@ QXmlStreamWriterPrivate::QXmlStreamWriterPrivate(QXmlStreamWriter *q) namespacePrefixCount = 0; } +void QXmlStreamWriterPrivate::checkIfASCIICompatibleCodec() +{ +#ifndef QT_NO_TEXTCODEC + Q_ASSERT(encoder); + // assumes ASCII-compatibility for all 8-bit encodings + const QByteArray bytes = encoder->fromUnicode(QLatin1String(" ")); + isCodecASCIICompatible = (bytes.count() == 1); +#else + isCodecASCIICompatible = true; +#endif +} + void QXmlStreamWriterPrivate::write(const QStringRef &s) { if (device) { @@ -3092,18 +3107,20 @@ void QXmlStreamWriterPrivate::writeEscaped(const QString &s, bool escapeWhitespa write(escaped); } -// ASCII only! +// Converts from ASCII to output encoding void QXmlStreamWriterPrivate::write(const char *s, int len) { if (device) { if (hasError) return; - if (device->write(s, len) != len) - hasError = true; - } else if (stringDevice) { - stringDevice->append(QString::fromLatin1(s, len)); - } else - qWarning("QXmlStreamWriter: No device"); + if (isCodecASCIICompatible) { + if (device->write(s, len) != len) + hasError = true; + return; + } + } + + write(QString::fromLatin1(s, len)); } void QXmlStreamWriterPrivate::writeNamespaceDeclaration(const NamespaceDeclaration &namespaceDeclaration) { @@ -3285,6 +3302,7 @@ void QXmlStreamWriter::setCodec(QTextCodec *codec) d->codec = codec; delete d->encoder; d->encoder = codec->makeEncoder(QTextCodec::IgnoreHeader); // no byte order mark for utf8 + d->checkIfASCIICompatibleCodec(); } } |