summaryrefslogtreecommitdiffstats
path: root/src/corelib/codecs/qutfcodec.cpp
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/codecs/qutfcodec.cpp
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/codecs/qutfcodec.cpp')
-rw-r--r--src/corelib/codecs/qutfcodec.cpp19
1 files changed, 19 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)