Merge the different implementations of toHex in one central place

It's a simple enough function, but we don't need to duplicate those 17
bytes all over the place. Now they'll be duplicated at most once per
library.

Change-Id: Ic995e2a934b005e7e996e70f2ee644bfa948eb38
Reviewed-by: Jason McDonald <macadder1@gmail.com>
This commit is contained in:
Thiago Macieira 2014-11-18 13:46:21 -08:00
parent 0bc6c4f7ec
commit cfe12f716b
6 changed files with 34 additions and 39 deletions

View File

@ -33,6 +33,7 @@
#include "qipaddress_p.h"
#include "private/qlocale_tools_p.h"
#include "private/qtools_p.h"
#include "qvarlengtharray.h"
QT_BEGIN_NAMESPACE
@ -240,7 +241,7 @@ const QChar *parseIp6(IPv6Address &address, const QChar *begin, const QChar *end
static inline QChar toHex(uchar c)
{
return ushort(c > 9 ? c + 'a' - 0xA : c + '0');
return QtMiscUtils::toHexLower(c);
}
void toString(QString &appendTo, IPv6Address address)

View File

@ -33,6 +33,7 @@
#include "qurl.h"
#include "private/qutfcodec_p.h"
#include "private/qtools_p.h"
QT_BEGIN_NAMESPACE
@ -197,8 +198,7 @@ static inline ushort decodePercentEncoding(const ushort *input)
static inline ushort encodeNibble(ushort c)
{
static const uchar hexnumbers[] = "0123456789ABCDEF";
return hexnumbers[c & 0xf];
return ushort(QtMiscUtils::toHexUpper(c));
}
static void ensureDetached(QString &result, ushort *&output, const ushort *begin, const ushort *input, const ushort *end,

View File

@ -36,14 +36,13 @@
#include "qdatastream.h"
#include "qendian.h"
#include "qdebug.h"
#include "private/qtools_p.h"
#ifndef QT_BOOTSTRAPPED
#include "qcryptographichash.h"
#endif
QT_BEGIN_NAMESPACE
static const char digits[] = "0123456789abcdef";
template <class Char, class Integral>
void _q_toHex(Char *&dst, Integral value)
{
@ -52,10 +51,8 @@ void _q_toHex(Char *&dst, Integral value)
const char* p = reinterpret_cast<const char*>(&value);
for (uint i = 0; i < sizeof(Integral); ++i, dst += 2) {
uint j = (p[i] >> 4) & 0xf;
dst[0] = Char(digits[j]);
j = p[i] & 0xf;
dst[1] = Char(digits[j]);
dst[0] = Char(QtMiscUtils::toHexLower((p[i] >> 4) & 0xf));
dst[1] = Char(QtMiscUtils::toHexLower(p[i] & 0xf));
}
}

View File

@ -4161,16 +4161,8 @@ QByteArray QByteArray::toHex() const
char *hexData = hex.data();
const uchar *data = (const uchar *)d->data();
for (int i = 0; i < d->size; ++i) {
int j = (data[i] >> 4) & 0xf;
if (j <= 9)
hexData[i*2] = (j + '0');
else
hexData[i*2] = (j + 'a' - 10);
j = data[i] & 0xf;
if (j <= 9)
hexData[i*2+1] = (j + '0');
else
hexData[i*2+1] = (j + 'a' - 10);
hexData[i*2] = QtMiscUtils::toHexLower(data[i] >> 4);
hexData[i*2+1] = QtMiscUtils::toHexLower(data[i] & 0xf);
}
return hex;
}
@ -4365,12 +4357,6 @@ static inline bool q_strchr(const char str[], char chr)
return false;
}
static inline char toHexHelper(char c)
{
static const char hexnumbers[] = "0123456789ABCDEF";
return hexnumbers[c & 0xf];
}
static void q_toPercentEncoding(QByteArray *ba, const char *dontEncode, const char *alsoEncode, char percent)
{
if (ba->isEmpty())
@ -4403,8 +4389,8 @@ static void q_toPercentEncoding(QByteArray *ba, const char *dontEncode, const ch
output = ba->data();
}
output[length++] = percent;
output[length++] = toHexHelper((c & 0xf0) >> 4);
output[length++] = toHexHelper(c & 0xf);
output[length++] = QtMiscUtils::toHexUpper((c & 0xf0) >> 4);
output[length++] = QtMiscUtils::toHexUpper(c & 0xf);
}
}
if (output)

View File

@ -50,6 +50,20 @@
QT_BEGIN_NAMESPACE
namespace QtMiscUtils {
inline char toHexUpper(uint value)
{
static const char hexdigits[] = "0123456789ABCDEF";
return hexdigits[value & 0xF];
}
inline char toHexLower(uint value)
{
static const char hexdigits[] = "0123456789abcdef";
return hexdigits[value & 0xF];
}
}
// We typically need an extra bit for qNextPowerOfTwo when determining the next allocation size.
enum {
MaxAllocSize = (1 << (std::numeric_limits<int>::digits - 1)) - 1

View File

@ -47,6 +47,7 @@
#include <QtCore/qprocess.h>
#include <QtCore/qdebug.h>
#include <QtCore/qlibraryinfo.h>
#include <QtCore/private/qtools_p.h>
#include <QtTest/private/qtestlog_p.h>
#include <QtTest/private/qtesttable_p.h>
@ -84,6 +85,8 @@
QT_BEGIN_NAMESPACE
using QtMiscUtils::toHexUpper;
/*!
\namespace QTest
\inmodule QtTest
@ -2060,12 +2063,6 @@ void *fetchData(QTestData *data, const char *tagName, int typeId)
return data->data(idx);
}
static char toHex(ushort value)
{
static const char hexdigits[] = "0123456789ABCDEF";
return hexdigits[value & 0xF];
}
/*!
\fn char* QTest::toHexRepresentation(const char *ba, int length)
@ -2115,9 +2112,9 @@ char *toHexRepresentation(const char *ba, int length)
while (true) {
const char at = ba[i];
result[o] = toHex(at >> 4);
result[o] = toHexUpper(at >> 4);
++o;
result[o] = toHex(at);
result[o] = toHexUpper(at);
++i;
++o;
@ -2183,10 +2180,10 @@ char *toPrettyUnicode(const ushort *p, int length)
break;
default:
*dst++ = 'u';
*dst++ = toHex(*p >> 12);
*dst++ = toHex(*p >> 8);
*dst++ = toHex(*p >> 4);
*dst++ = toHex(*p);
*dst++ = toHexUpper(*p >> 12);
*dst++ = toHexUpper(*p >> 8);
*dst++ = toHexUpper(*p >> 4);
*dst++ = toHexUpper(*p);
}
}