diff options
| author | Thiago Macieira <thiago.macieira@intel.com> | 2014-11-18 13:46:21 -0800 |
|---|---|---|
| committer | Thiago Macieira <thiago.macieira@intel.com> | 2014-12-13 05:10:20 +0100 |
| commit | cfe12f716b0e138b51e3d8f5e481c4d9624459dc (patch) | |
| tree | 26232a23b4985ba8984a139e03ac30403a38089d /src/corelib | |
| parent | 0bc6c4f7ecfa332de57500fe722872eff4009b9b (diff) | |
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>
Diffstat (limited to 'src/corelib')
| -rw-r--r-- | src/corelib/io/qipaddress.cpp | 3 | ||||
| -rw-r--r-- | src/corelib/io/qurlrecode.cpp | 4 | ||||
| -rw-r--r-- | src/corelib/plugin/quuid.cpp | 9 | ||||
| -rw-r--r-- | src/corelib/tools/qbytearray.cpp | 22 | ||||
| -rw-r--r-- | src/corelib/tools/qtools_p.h | 14 |
5 files changed, 25 insertions, 27 deletions
diff --git a/src/corelib/io/qipaddress.cpp b/src/corelib/io/qipaddress.cpp index 44da22489d9..d436cd2dd69 100644 --- a/src/corelib/io/qipaddress.cpp +++ b/src/corelib/io/qipaddress.cpp @@ -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) diff --git a/src/corelib/io/qurlrecode.cpp b/src/corelib/io/qurlrecode.cpp index 9aad2a12bd3..4cf471a248a 100644 --- a/src/corelib/io/qurlrecode.cpp +++ b/src/corelib/io/qurlrecode.cpp @@ -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, diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index 4a5f4e791d3..f7c569f1239 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -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)); } } diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 26bf7d047d8..bf29ebcd6dc 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -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) diff --git a/src/corelib/tools/qtools_p.h b/src/corelib/tools/qtools_p.h index 1e72db1d87e..2acf3427f0d 100644 --- a/src/corelib/tools/qtools_p.h +++ b/src/corelib/tools/qtools_p.h @@ -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 |
