| 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 { Lowercase, Uppercase }; | |
| 28 | |
| 29 namespace Internal { | 27 namespace Internal { |
| 30 | 28 |
| 31 const LChar lowerHexDigits[17] = "0123456789abcdef"; | 29 const LChar lowerHexDigits[17] = "0123456789abcdef"; |
| 32 const LChar upperHexDigits[17] = "0123456789ABCDEF"; | 30 const LChar upperHexDigits[17] = "0123456789ABCDEF"; |
| 33 inline const LChar* hexDigitsForMode(HexConversionMode mode) { | |
| 34 return mode == Lowercase ? lowerHexDigits : upperHexDigits; | |
| 35 } | |
| 36 | 31 |
| 37 } // namespace Internal | 32 } // namespace Internal |
| 38 | 33 |
| 39 template <typename T> | 34 class HexNumber final { |
| 40 inline void appendByteAsHex(unsigned char byte, | 35 STATIC_ONLY(HexNumber); |
| 41 T& destination, | |
| 42 HexConversionMode mode = Uppercase) { | |
| 43 const LChar* hexDigits = Internal::hexDigitsForMode(mode); | |
| 44 destination.append(hexDigits[byte >> 4]); | |
| 45 destination.append(hexDigits[byte & 0xF]); | |
| 46 } | |
| 47 | 36 |
| 48 template <typename T> | 37 public: |
| 49 inline void appendUnsignedAsHex(unsigned number, | 38 enum HexConversionMode { Lowercase, Uppercase }; |
| 50 T& destination, | |
| 51 HexConversionMode mode = Uppercase) { | |
| 52 const LChar* hexDigits = Internal::hexDigitsForMode(mode); | |
| 53 Vector<LChar, 8> result; | |
| 54 do { | |
| 55 result.prepend(hexDigits[number % 16]); | |
| 56 number >>= 4; | |
| 57 } while (number > 0); | |
| 58 | 39 |
| 59 destination.append(result.data(), result.size()); | 40 template <typename T> |
| 60 } | 41 static inline void appendByteAsHex(unsigned char byte, |
| 42 T& destination, |
| 43 HexConversionMode mode = Uppercase) { |
| 44 const LChar* hexDigits = hexDigitsForMode(mode); |
| 45 destination.append(hexDigits[byte >> 4]); |
| 46 destination.append(hexDigits[byte & 0xF]); |
| 47 } |
| 61 | 48 |
| 62 // Same as appendUnsignedAsHex, but using exactly 'desiredDigits' for the | 49 static inline void appendByteAsHex(unsigned char byte, |
| 63 // conversion. | 50 Vector<LChar>& destination, |
| 64 template <typename T> | 51 HexConversionMode mode = Uppercase) { |
| 65 inline void appendUnsignedAsHexFixedSize(unsigned number, | 52 const LChar* hexDigits = hexDigitsForMode(mode); |
| 53 destination.push_back(hexDigits[byte >> 4]); |
| 54 destination.push_back(hexDigits[byte & 0xF]); |
| 55 } |
| 56 |
| 57 template <typename T> |
| 58 static inline void appendUnsignedAsHex(unsigned number, |
| 66 T& destination, | 59 T& destination, |
| 67 unsigned desiredDigits, | |
| 68 HexConversionMode mode = Uppercase) { | 60 HexConversionMode mode = Uppercase) { |
| 69 DCHECK(desiredDigits); | 61 const LChar* hexDigits = hexDigitsForMode(mode); |
| 62 Vector<LChar, 8> result; |
| 63 do { |
| 64 result.prepend(hexDigits[number % 16]); |
| 65 number >>= 4; |
| 66 } while (number > 0); |
| 70 | 67 |
| 71 const LChar* hexDigits = Internal::hexDigitsForMode(mode); | 68 destination.append(result.data(), result.size()); |
| 72 Vector<LChar, 8> result; | 69 } |
| 73 do { | |
| 74 result.prepend(hexDigits[number % 16]); | |
| 75 number >>= 4; | |
| 76 } while (result.size() < desiredDigits); | |
| 77 | 70 |
| 78 DCHECK_EQ(result.size(), desiredDigits); | 71 // Same as appendUnsignedAsHex, but using exactly 'desiredDigits' for the |
| 79 destination.append(result.data(), result.size()); | 72 // conversion. |
| 80 } | 73 template <typename T> |
| 74 static inline void appendUnsignedAsHexFixedSize( |
| 75 unsigned number, |
| 76 T& destination, |
| 77 unsigned desiredDigits, |
| 78 HexConversionMode mode = Uppercase) { |
| 79 DCHECK(desiredDigits); |
| 80 |
| 81 const LChar* hexDigits = hexDigitsForMode(mode); |
| 82 Vector<LChar, 8> result; |
| 83 do { |
| 84 result.prepend(hexDigits[number % 16]); |
| 85 number >>= 4; |
| 86 } while (result.size() < desiredDigits); |
| 87 |
| 88 DCHECK_EQ(result.size(), desiredDigits); |
| 89 destination.append(result.data(), result.size()); |
| 90 } |
| 91 |
| 92 private: |
| 93 static inline const LChar* hexDigitsForMode(HexConversionMode mode) { |
| 94 return mode == Lowercase ? Internal::lowerHexDigits |
| 95 : Internal::upperHexDigits; |
| 96 } |
| 97 }; |
| 81 | 98 |
| 82 } // namespace WTF | 99 } // namespace WTF |
| 83 | 100 |
| 84 using WTF::appendByteAsHex; | 101 using WTF::HexNumber; |
| 85 using WTF::appendUnsignedAsHex; | |
| 86 using WTF::appendUnsignedAsHexFixedSize; | |
| 87 using WTF::Lowercase; | |
| 88 | 102 |
| 89 #endif // HexNumber_h | 103 #endif // HexNumber_h |
| OLD | NEW |