summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2018-10-20 21:45:10 -0700
committerThiago Macieira <thiago.macieira@intel.com>2018-11-08 15:19:55 +0000
commit99baa0d4401516cff978cab8a7102fc0a25731a9 (patch)
tree9627c8ee0151aa8777732d2f4816cd97c8bbe65f
parent66a2d159073bfbeec53a0b9e09575c3fe9beac8d (diff)
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) <ogoffart@woboq.com> Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
-rw-r--r--src/corelib/codecs/qtextcodec.cpp28
1 files changed, 14 insertions, 14 deletions
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<const uchar *>(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<uint>(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<ushort>(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;