From 99baa0d4401516cff978cab8a7102fc0a25731a9 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Sat, 20 Oct 2018 21:45:10 -0700 Subject: Optimize QTextCodec::codecForUtfText a little Instead of doing byte comparisons, let the compiler do 16- and 32-bit comparisons on its own. Change-Id: If7e743cf8476463880ccfffd155f8629991b0b87 Reviewed-by: Olivier Goffart (Woboq GmbH) Reviewed-by: Tobias Hunger --- src/corelib/codecs/qtextcodec.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/corelib/codecs') diff --git a/src/corelib/codecs/qtextcodec.cpp b/src/corelib/codecs/qtextcodec.cpp index a2a128158e..e5c33d9d59 100644 --- a/src/corelib/codecs/qtextcodec.cpp +++ b/src/corelib/codecs/qtextcodec.cpp @@ -45,8 +45,9 @@ #ifndef QT_NO_TEXTCODEC #include "qbytearraymatcher.h" -#include "qlist.h" +#include "qendian.h" #include "qfile.h" +#include "qlist.h" #include "qstringlist.h" #include "qvarlengtharray.h" #if !defined(QT_BOOTSTRAPPED) @@ -1183,32 +1184,31 @@ QTextCodec *QTextCodec::codecForHtml(const QByteArray &ba) QTextCodec *QTextCodec::codecForUtfText(const QByteArray &ba, QTextCodec *defaultCodec) { const int arraySize = ba.size(); + const uchar *buf = reinterpret_cast(ba.constData()); + const uint bom = 0xfeff; if (arraySize > 3) { - if ((uchar)ba[0] == 0x00 - && (uchar)ba[1] == 0x00 - && (uchar)ba[2] == 0xFE - && (uchar)ba[3] == 0xFF) + uint uc = qFromUnaligned(buf); + if (uc == qToBigEndian(bom)) return QTextCodec::codecForMib(1018); // utf-32 be - else if ((uchar)ba[0] == 0xFF - && (uchar)ba[1] == 0xFE - && (uchar)ba[2] == 0x00 - && (uchar)ba[3] == 0x00) + else if (uc == qToLittleEndian(bom)) return QTextCodec::codecForMib(1019); // utf-32 le } if (arraySize < 2) return defaultCodec; - if ((uchar)ba[0] == 0xfe && (uchar)ba[1] == 0xff) + + ushort uc = qFromUnaligned(buf); + if (uc == qToBigEndian(ushort(bom))) return QTextCodec::codecForMib(1013); // utf16 be - else if ((uchar)ba[0] == 0xff && (uchar)ba[1] == 0xfe) + else if (uc == qToLittleEndian(ushort(bom))) return QTextCodec::codecForMib(1014); // utf16 le if (arraySize < 3) return defaultCodec; - if ((uchar)ba[0] == 0xef - && (uchar)ba[1] == 0xbb - && (uchar)ba[2] == 0xbf) + + static const char utf8bom[] = "\xef\xbb\xbf"; + if (memcmp(buf, utf8bom, sizeof(utf8bom) - 1) == 0) return QTextCodec::codecForMib(106); // utf-8 return defaultCodec; -- cgit v1.2.3