| 1 | /* |
| 2 | * Copyright (C) 2011 Research In Motion Limited. All rights reserved. |
| 3 | * Copyright (C) 2016-2019 Apple Inc. All rights reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Library General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Library General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Library General Public License |
| 16 | * along with this library; see the file COPYING.LIB. If not, write to |
| 17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 | * Boston, MA 02110-1301, USA. |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include <array> |
| 24 | #include <wtf/text/StringConcatenate.h> |
| 25 | |
| 26 | namespace WTF { |
| 27 | |
| 28 | enum HexConversionMode { Lowercase, Uppercase }; |
| 29 | |
| 30 | namespace Internal { |
| 31 | |
| 32 | inline const LChar* hexDigitsForMode(HexConversionMode mode) |
| 33 | { |
| 34 | static const LChar lowercaseHexDigits[17] = "0123456789abcdef" ; |
| 35 | static const LChar uppercaseHexDigits[17] = "0123456789ABCDEF" ; |
| 36 | return mode == Lowercase ? lowercaseHexDigits : uppercaseHexDigits; |
| 37 | } |
| 38 | |
| 39 | WTF_EXPORT std::pair<LChar*, unsigned> appendHex(LChar* buffer, unsigned bufferSize, std::uintmax_t number, unsigned minimumDigits, HexConversionMode); |
| 40 | |
| 41 | template<size_t arraySize, typename NumberType> |
| 42 | inline std::pair<LChar*, unsigned> appendHex(std::array<LChar, arraySize>& buffer, NumberType number, unsigned minimumDigits, HexConversionMode mode) |
| 43 | { |
| 44 | return appendHex(&buffer.front(), buffer.size(), static_cast<typename std::make_unsigned<NumberType>::type>(number), minimumDigits, mode); |
| 45 | } |
| 46 | |
| 47 | } // namespace Internal |
| 48 | |
| 49 | template<typename T> |
| 50 | inline void appendByteAsHex(unsigned char byte, T& destination, HexConversionMode mode = Uppercase) |
| 51 | { |
| 52 | auto* hexDigits = Internal::hexDigitsForMode(mode); |
| 53 | destination.append(hexDigits[byte >> 4]); |
| 54 | destination.append(hexDigits[byte & 0xF]); |
| 55 | } |
| 56 | |
| 57 | // FIXME: Consider renaming to appendHex. |
| 58 | template<typename NumberType, typename DestinationType> |
| 59 | inline void appendUnsignedAsHex(NumberType number, DestinationType& destination, HexConversionMode mode = Uppercase) |
| 60 | { |
| 61 | appendUnsignedAsHexFixedSize(number, destination, 0, mode); |
| 62 | } |
| 63 | |
| 64 | // FIXME: Consider renaming to appendHex. |
| 65 | // Same as appendUnsignedAsHex, but zero-padding to get at least the desired number of digits. |
| 66 | template<typename NumberType, typename DestinationType> |
| 67 | inline void appendUnsignedAsHexFixedSize(NumberType number, DestinationType& destination, unsigned minimumDigits, HexConversionMode mode = Uppercase) |
| 68 | { |
| 69 | // Each byte can generate up to two digits. |
| 70 | std::array<LChar, sizeof(NumberType) * 2> buffer; |
| 71 | auto result = Internal::appendHex(buffer, number, minimumDigits, mode); |
| 72 | destination.append(result.first, result.second); |
| 73 | } |
| 74 | |
| 75 | struct HexNumberBuffer { |
| 76 | std::array<LChar, 16> characters; |
| 77 | unsigned length; |
| 78 | }; |
| 79 | |
| 80 | template<typename NumberType> HexNumberBuffer hex(NumberType number, unsigned minimumDigits = 0, HexConversionMode mode = Uppercase) |
| 81 | { |
| 82 | // Each byte can generate up to two digits. |
| 83 | HexNumberBuffer buffer; |
| 84 | static_assert(sizeof(buffer.characters) >= sizeof(NumberType) * 2, "number too large for hexNumber" ); |
| 85 | auto result = Internal::appendHex(buffer.characters, number, minimumDigits, mode); |
| 86 | buffer.length = result.second; |
| 87 | return buffer; |
| 88 | } |
| 89 | |
| 90 | template<typename NumberType> HexNumberBuffer hex(NumberType number, HexConversionMode mode) |
| 91 | { |
| 92 | return hex(number, 0, mode); |
| 93 | } |
| 94 | |
| 95 | template<> class StringTypeAdapter<HexNumberBuffer> { |
| 96 | public: |
| 97 | StringTypeAdapter(const HexNumberBuffer& buffer) |
| 98 | : m_buffer { buffer } |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | unsigned length() const { return m_buffer.length; } |
| 103 | bool is8Bit() const { return true; } |
| 104 | template<typename CharacterType> void writeTo(CharacterType* destination) const { StringImpl::copyCharacters(destination, characters(), length()); } |
| 105 | |
| 106 | private: |
| 107 | const LChar* characters() const { return &*(m_buffer.characters.end() - length()); } |
| 108 | |
| 109 | const HexNumberBuffer& m_buffer; |
| 110 | }; |
| 111 | |
| 112 | } // namespace WTF |
| 113 | |
| 114 | using WTF::appendByteAsHex; |
| 115 | using WTF::appendUnsignedAsHex; |
| 116 | using WTF::appendUnsignedAsHexFixedSize; |
| 117 | using WTF::hex; |
| 118 | using WTF::Lowercase; |
| 119 | |