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>
This commit is contained in:
parent
50916edd9d
commit
ff0d02eb2f
@ -972,6 +972,25 @@ QString QUtf32::convertToUnicode(const char *chars, int len, QTextCodec::Convert
|
|||||||
return result;
|
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)
|
#if QT_CONFIG(textcodec)
|
||||||
|
|
||||||
|
@ -54,6 +54,7 @@
|
|||||||
|
|
||||||
#include <QtCore/qstring.h>
|
#include <QtCore/qstring.h>
|
||||||
#include <QtCore/qlist.h>
|
#include <QtCore/qlist.h>
|
||||||
|
#include <QtCore/qendian.h>
|
||||||
|
|
||||||
#if QT_CONFIG(textcodec)
|
#if QT_CONFIG(textcodec)
|
||||||
#include "QtCore/qtextcodec.h"
|
#include "QtCore/qtextcodec.h"
|
||||||
@ -317,6 +318,13 @@ struct QUtf32
|
|||||||
static QByteArray convertFromUnicode(const QChar *, int, QTextCodec::ConverterState *, DataEndianness = DetectEndianness);
|
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)
|
#if QT_CONFIG(textcodec)
|
||||||
|
|
||||||
class QUtf8Codec : public QTextCodec {
|
class QUtf8Codec : public QTextCodec {
|
||||||
|
@ -47,7 +47,7 @@
|
|||||||
#include "qbuffer.h"
|
#include "qbuffer.h"
|
||||||
#include "qimage.h"
|
#include "qimage.h"
|
||||||
#if QT_CONFIG(textcodec)
|
#if QT_CONFIG(textcodec)
|
||||||
#include "qtextcodec.h"
|
#include "private/qutfcodec_p.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "private/qguiapplication_p.h"
|
#include "private/qguiapplication_p.h"
|
||||||
@ -301,14 +301,9 @@ QString QClipboard::text(QString &subtype, Mode mode) const
|
|||||||
const QByteArray rawData = data->data(QLatin1String("text/") + subtype);
|
const QByteArray rawData = data->data(QLatin1String("text/") + subtype);
|
||||||
|
|
||||||
#if QT_CONFIG(textcodec)
|
#if QT_CONFIG(textcodec)
|
||||||
QTextCodec* codec = QTextCodec::codecForMib(106); // utf-8 is default
|
return qFromUtfEncoded(rawData);
|
||||||
if (subtype == QLatin1String("html"))
|
|
||||||
codec = QTextCodec::codecForHtml(rawData, codec);
|
|
||||||
else
|
|
||||||
codec = QTextCodec::codecForUtfText(rawData, codec);
|
|
||||||
return codec->toUnicode(rawData);
|
|
||||||
#else // textcodec
|
#else // textcodec
|
||||||
return rawData;
|
return QString::fromUtf8(rawData);
|
||||||
#endif // textcodec
|
#endif // textcodec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user