| OLD | NEW |
| 1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2011 Research In Motion Limited. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * | 3 // found in the LICENSE file. |
| 4 * This library is free software; you can redistribute it and/or | |
| 5 * modify it under the terms of the GNU Library General Public | |
| 6 * License as published by the Free Software Foundation; either | |
| 7 * version 2 of the License, or (at your option) any later version. | |
| 8 * | |
| 9 * This library is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 12 * Library General Public License for more details. | |
| 13 * | |
| 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 | |
| 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
| 17 * Boston, MA 02110-1301, USA. | |
| 18 */ | |
| 19 | 4 |
| 20 #ifndef HexNumber_h | 5 #include "platform/wtf/HexNumber.h" |
| 21 #define HexNumber_h | |
| 22 | 6 |
| 23 #include "wtf/text/StringConcatenate.h" | 7 // The contents of this header was moved to platform/wtf as part of |
| 24 | 8 // WTF migration project. See the following post for details: |
| 25 namespace WTF { | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
| 26 | |
| 27 namespace Internal { | |
| 28 | |
| 29 const LChar lowerHexDigits[17] = "0123456789abcdef"; | |
| 30 const LChar upperHexDigits[17] = "0123456789ABCDEF"; | |
| 31 | |
| 32 } // namespace Internal | |
| 33 | |
| 34 class HexNumber final { | |
| 35 STATIC_ONLY(HexNumber); | |
| 36 | |
| 37 public: | |
| 38 enum HexConversionMode { Lowercase, Uppercase }; | |
| 39 | |
| 40 template <typename T> | |
| 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 } | |
| 48 | |
| 49 static inline void appendByteAsHex(unsigned char byte, | |
| 50 Vector<LChar>& destination, | |
| 51 HexConversionMode mode = Uppercase) { | |
| 52 const LChar* hexDigits = hexDigitsForMode(mode); | |
| 53 destination.push_back(hexDigits[byte >> 4]); | |
| 54 destination.push_back(hexDigits[byte & 0xF]); | |
| 55 } | |
| 56 | |
| 57 static inline void appendByteAsHex(unsigned char byte, | |
| 58 Vector<char>& destination, | |
| 59 HexConversionMode mode = Uppercase) { | |
| 60 const LChar* hexDigits = hexDigitsForMode(mode); | |
| 61 destination.push_back(hexDigits[byte >> 4]); | |
| 62 destination.push_back(hexDigits[byte & 0xF]); | |
| 63 } | |
| 64 | |
| 65 template <typename T> | |
| 66 static inline void appendUnsignedAsHex(unsigned number, | |
| 67 T& destination, | |
| 68 HexConversionMode mode = Uppercase) { | |
| 69 const LChar* hexDigits = hexDigitsForMode(mode); | |
| 70 Vector<LChar, 8> result; | |
| 71 do { | |
| 72 result.push_front(hexDigits[number % 16]); | |
| 73 number >>= 4; | |
| 74 } while (number > 0); | |
| 75 | |
| 76 destination.append(result.data(), result.size()); | |
| 77 } | |
| 78 | |
| 79 // Same as appendUnsignedAsHex, but using exactly 'desiredDigits' for the | |
| 80 // conversion. | |
| 81 template <typename T> | |
| 82 static inline void appendUnsignedAsHexFixedSize( | |
| 83 unsigned number, | |
| 84 T& destination, | |
| 85 unsigned desiredDigits, | |
| 86 HexConversionMode mode = Uppercase) { | |
| 87 DCHECK(desiredDigits); | |
| 88 | |
| 89 const LChar* hexDigits = hexDigitsForMode(mode); | |
| 90 Vector<LChar, 8> result; | |
| 91 do { | |
| 92 result.push_front(hexDigits[number % 16]); | |
| 93 number >>= 4; | |
| 94 } while (result.size() < desiredDigits); | |
| 95 | |
| 96 DCHECK_EQ(result.size(), desiredDigits); | |
| 97 destination.append(result.data(), result.size()); | |
| 98 } | |
| 99 | |
| 100 private: | |
| 101 static inline const LChar* hexDigitsForMode(HexConversionMode mode) { | |
| 102 return mode == Lowercase ? Internal::lowerHexDigits | |
| 103 : Internal::upperHexDigits; | |
| 104 } | |
| 105 }; | |
| 106 | |
| 107 } // namespace WTF | |
| 108 | |
| 109 using WTF::HexNumber; | |
| 110 | |
| 111 #endif // HexNumber_h | |
| OLD | NEW |