diff options
author | Jan Arve Saether <jan-arve.saether@digia.com> | 2012-11-20 11:11:44 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-11-20 18:53:49 +0100 |
commit | 035d93a6e1cbde76d34866ffd9b39633572e6236 (patch) | |
tree | 5f131ef5550f30eabf823b4dd2daf535a0608355 /src/corelib | |
parent | 4532a1399e10a677083300648cf18af9addab656 (diff) | |
download | qtbase-035d93a6e1cbde76d34866ffd9b39633572e6236.tar.gz |
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
Task-number: QTBUG-23310
Change-Id: I75bd013e9d5de53da564a76c2f06e95ff35303a8
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 46c0e5d297..2345cb1881 100644 --- a/src/corelib/xml/qxmlstream.cpp +++ b/src/corelib/xml/qxmlstream.cpp @@ -2980,6 +2980,7 @@ public: uint wroteSomething :1; uint hasError :1; uint autoFormatting :1; + uint isCodecASCIICompatible :1; QByteArray autoFormattingIndent; NamespaceDeclaration emptyNamespace; int lastNamespaceDeclaration; @@ -2988,6 +2989,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); @@ -3009,6 +3011,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; @@ -3018,6 +3021,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(QStringLiteral(" ")); + isCodecASCIICompatible = (bytes.count() == 1); +#else + isCodecASCIICompatible = true; +#endif +} + void QXmlStreamWriterPrivate::write(const QStringRef &s) { if (device) { @@ -3086,18 +3101,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) { @@ -3279,6 +3296,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(); } } |