summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-04-07 11:14:06 +0200
committerLars Knoll <lars.knoll@qt.io>2020-04-21 15:46:43 +0200
commitff0d02eb2f048730ccbf0cdf30af64c5c5552044 (patch)
treea650f2440af8e8a6529c49f093f1fb60eb087132 /src/corelib
parent50916edd9d1707774c597abe1b7237e1a798fc53 (diff)
Remove QTextCodec dependency from QClipboard
QClipboard used QTextCodec to convert the war clipboard data to a QString. HTML is nowadays always encoded as utf8, and we were only supporting utf based encodings for other text. Add a qFromUtfEncoded() to our UTF helpers that auto detects utf16 and utf32 byte order marks, and assumes utf8 otherwise, to keep this compatible with what we have been doing in Qt 5. Change-Id: I5a9fccb67a88dff27cbbdecff9bd548d31aa1c6c Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/codecs/qutfcodec.cpp19
-rw-r--r--src/corelib/codecs/qutfcodec_p.h8
2 files changed, 27 insertions, 0 deletions
diff --git a/src/corelib/codecs/qutfcodec.cpp b/src/corelib/codecs/qutfcodec.cpp
index 8561f908b9..9419ce8d84 100644
--- a/src/corelib/codecs/qutfcodec.cpp
+++ b/src/corelib/codecs/qutfcodec.cpp
@@ -972,6 +972,25 @@ QString QUtf32::convertToUnicode(const char *chars, int len, QTextCodec::Convert
return result;
}
+QString qFromUtfEncoded(const QByteArray &ba)
+{
+ const int arraySize = ba.size();
+ const uchar *buf = reinterpret_cast<const uchar *>(ba.constData());
+ const uint bom = 0xfeff;
+
+ if (arraySize > 3) {
+ uint uc = qFromUnaligned<uint>(buf);
+ if (uc == qToBigEndian(bom) || uc == qToLittleEndian(bom))
+ return QUtf32::convertToUnicode(ba.constData(), ba.length(), nullptr); // utf-32
+ }
+
+ if (arraySize > 1) {
+ ushort uc = qFromUnaligned<ushort>(buf);
+ if (uc == qToBigEndian(ushort(bom)) || qToLittleEndian(ushort(bom)))
+ return QUtf16::convertToUnicode(ba.constData(), ba.length(), nullptr); // utf-16
+ }
+ return QUtf8::convertToUnicode(ba.constData(), ba.length());
+}
#if QT_CONFIG(textcodec)
diff --git a/src/corelib/codecs/qutfcodec_p.h b/src/corelib/codecs/qutfcodec_p.h
index 083e16317a..b1c7a23d4f 100644
--- a/src/corelib/codecs/qutfcodec_p.h
+++ b/src/corelib/codecs/qutfcodec_p.h
@@ -54,6 +54,7 @@
#include <QtCore/qstring.h>
#include <QtCore/qlist.h>
+#include <QtCore/qendian.h>
#if QT_CONFIG(textcodec)
#include "QtCore/qtextcodec.h"
@@ -317,6 +318,13 @@ struct QUtf32
static QByteArray convertFromUnicode(const QChar *, int, QTextCodec::ConverterState *, DataEndianness = DetectEndianness);
};
+/*
+ Converts from different utf encodings looking at a possible byte order mark at the
+ beginning of the string. If no BOM exists, utf-8 is assumed.
+ */
+QString Q_CORE_EXPORT qFromUtfEncoded(const QByteArray &ba);
+
+
#if QT_CONFIG(textcodec)
class QUtf8Codec : public QTextCodec {