| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Research In Motion Limited. All rights reserved. | 2 * Copyright (C) 2011 Research In Motion Limited. All rights reserved. |
| 3 * | 3 * |
| 4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
| 6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
| 7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
| 8 * | 8 * |
| 9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU Library General Public License | 14 * You should have received a copy of the GNU Library General Public License |
| 15 * along with this library; see the file COPYING.LIB. If not, write to | 15 * along with this library; see the file COPYING.LIB. If not, write to |
| 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
| 18 */ | 18 */ |
| 19 | 19 |
| 20 #ifndef HexNumber_h | 20 #ifndef HexNumber_h |
| 21 #define HexNumber_h | 21 #define HexNumber_h |
| 22 | 22 |
| 23 #include "wtf/text/StringConcatenate.h" | 23 #include "wtf/text/StringConcatenate.h" |
| 24 | 24 |
| 25 namespace WTF { | 25 namespace WTF { |
| 26 | 26 |
| 27 enum HexConversionMode { | 27 enum HexConversionMode { Lowercase, Uppercase }; |
| 28 Lowercase, | |
| 29 Uppercase | |
| 30 }; | |
| 31 | 28 |
| 32 namespace Internal { | 29 namespace Internal { |
| 33 | 30 |
| 34 const LChar lowerHexDigits[17] = "0123456789abcdef"; | 31 const LChar lowerHexDigits[17] = "0123456789abcdef"; |
| 35 const LChar upperHexDigits[17] = "0123456789ABCDEF"; | 32 const LChar upperHexDigits[17] = "0123456789ABCDEF"; |
| 36 inline const LChar* hexDigitsForMode(HexConversionMode mode) | 33 inline const LChar* hexDigitsForMode(HexConversionMode mode) { |
| 37 { | 34 return mode == Lowercase ? lowerHexDigits : upperHexDigits; |
| 38 return mode == Lowercase ? lowerHexDigits : upperHexDigits; | |
| 39 } | 35 } |
| 40 | 36 |
| 41 } // namespace Internal | 37 } // namespace Internal |
| 42 | 38 |
| 43 template<typename T> | 39 template <typename T> |
| 44 inline void appendByteAsHex(unsigned char byte, T& destination, HexConversionMod
e mode = Uppercase) | 40 inline void appendByteAsHex(unsigned char byte, |
| 45 { | 41 T& destination, |
| 46 const LChar* hexDigits = Internal::hexDigitsForMode(mode); | 42 HexConversionMode mode = Uppercase) { |
| 47 destination.append(hexDigits[byte >> 4]); | 43 const LChar* hexDigits = Internal::hexDigitsForMode(mode); |
| 48 destination.append(hexDigits[byte & 0xF]); | 44 destination.append(hexDigits[byte >> 4]); |
| 45 destination.append(hexDigits[byte & 0xF]); |
| 49 } | 46 } |
| 50 | 47 |
| 51 template<typename T> | 48 template <typename T> |
| 52 inline void placeByteAsHexCompressIfPossible(unsigned char byte, T& destination,
unsigned& index, HexConversionMode mode = Uppercase) | 49 inline void placeByteAsHexCompressIfPossible( |
| 53 { | 50 unsigned char byte, |
| 54 const LChar* hexDigits = Internal::hexDigitsForMode(mode); | 51 T& destination, |
| 55 if (byte >= 0x10) | 52 unsigned& index, |
| 56 destination[index++] = hexDigits[byte >> 4]; | 53 HexConversionMode mode = Uppercase) { |
| 57 destination[index++] = hexDigits[byte & 0xF]; | 54 const LChar* hexDigits = Internal::hexDigitsForMode(mode); |
| 55 if (byte >= 0x10) |
| 56 destination[index++] = hexDigits[byte >> 4]; |
| 57 destination[index++] = hexDigits[byte & 0xF]; |
| 58 } | 58 } |
| 59 | 59 |
| 60 template<typename T> | 60 template <typename T> |
| 61 inline void placeByteAsHex(unsigned char byte, T& destination, HexConversionMode
mode = Uppercase) | 61 inline void placeByteAsHex(unsigned char byte, |
| 62 { | 62 T& destination, |
| 63 const LChar* hexDigits = Internal::hexDigitsForMode(mode); | 63 HexConversionMode mode = Uppercase) { |
| 64 *destination++ = hexDigits[byte >> 4]; | 64 const LChar* hexDigits = Internal::hexDigitsForMode(mode); |
| 65 *destination++ = hexDigits[byte & 0xF]; | 65 *destination++ = hexDigits[byte >> 4]; |
| 66 *destination++ = hexDigits[byte & 0xF]; |
| 66 } | 67 } |
| 67 | 68 |
| 68 template<typename T> | 69 template <typename T> |
| 69 inline void appendUnsignedAsHex(unsigned number, T& destination, HexConversionMo
de mode = Uppercase) | 70 inline void appendUnsignedAsHex(unsigned number, |
| 70 { | 71 T& destination, |
| 71 const LChar* hexDigits = Internal::hexDigitsForMode(mode); | 72 HexConversionMode mode = Uppercase) { |
| 72 Vector<LChar, 8> result; | 73 const LChar* hexDigits = Internal::hexDigitsForMode(mode); |
| 73 do { | 74 Vector<LChar, 8> result; |
| 74 result.prepend(hexDigits[number % 16]); | 75 do { |
| 75 number >>= 4; | 76 result.prepend(hexDigits[number % 16]); |
| 76 } while (number > 0); | 77 number >>= 4; |
| 78 } while (number > 0); |
| 77 | 79 |
| 78 destination.append(result.data(), result.size()); | 80 destination.append(result.data(), result.size()); |
| 79 } | 81 } |
| 80 | 82 |
| 81 // Same as appendUnsignedAsHex, but using exactly 'desiredDigits' for the conver
sion. | 83 // Same as appendUnsignedAsHex, but using exactly 'desiredDigits' for the conver
sion. |
| 82 template<typename T> | 84 template <typename T> |
| 83 inline void appendUnsignedAsHexFixedSize(unsigned number, T& destination, unsign
ed desiredDigits, HexConversionMode mode = Uppercase) | 85 inline void appendUnsignedAsHexFixedSize(unsigned number, |
| 84 { | 86 T& destination, |
| 85 ASSERT(desiredDigits); | 87 unsigned desiredDigits, |
| 88 HexConversionMode mode = Uppercase) { |
| 89 ASSERT(desiredDigits); |
| 86 | 90 |
| 87 const LChar* hexDigits = Internal::hexDigitsForMode(mode); | 91 const LChar* hexDigits = Internal::hexDigitsForMode(mode); |
| 88 Vector<LChar, 8> result; | 92 Vector<LChar, 8> result; |
| 89 do { | 93 do { |
| 90 result.prepend(hexDigits[number % 16]); | 94 result.prepend(hexDigits[number % 16]); |
| 91 number >>= 4; | 95 number >>= 4; |
| 92 } while (result.size() < desiredDigits); | 96 } while (result.size() < desiredDigits); |
| 93 | 97 |
| 94 ASSERT(result.size() == desiredDigits); | 98 ASSERT(result.size() == desiredDigits); |
| 95 destination.append(result.data(), result.size()); | 99 destination.append(result.data(), result.size()); |
| 96 } | 100 } |
| 97 | 101 |
| 98 } // namespace WTF | 102 } // namespace WTF |
| 99 | 103 |
| 100 using WTF::appendByteAsHex; | 104 using WTF::appendByteAsHex; |
| 101 using WTF::appendUnsignedAsHex; | 105 using WTF::appendUnsignedAsHex; |
| 102 using WTF::appendUnsignedAsHexFixedSize; | 106 using WTF::appendUnsignedAsHexFixedSize; |
| 103 using WTF::placeByteAsHex; | 107 using WTF::placeByteAsHex; |
| 104 using WTF::placeByteAsHexCompressIfPossible; | 108 using WTF::placeByteAsHexCompressIfPossible; |
| 105 using WTF::Lowercase; | 109 using WTF::Lowercase; |
| 106 | 110 |
| 107 #endif // HexNumber_h | 111 #endif // HexNumber_h |
| OLD | NEW |