2 * Copyright (C) 2011 Research In Motion Limited. All rights reserved.
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.
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.
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.
23 #include <wtf/text/StringConcatenate.h>
27 enum HexConversionMode {
34 static const char* hexDigitsForMode(HexConversionMode mode)
36 static const char lowerHexDigits[17] = "0123456789abcdef";
37 static const char upperHexDigits[17] = "0123456789ABCDEF";
38 return mode == Lowercase ? lowerHexDigits : upperHexDigits;
41 }; // namespace Internal
44 inline void appendByteAsHex(unsigned char byte, T& destination, HexConversionMode mode = Uppercase)
46 const char* hexDigits = Internal::hexDigitsForMode(mode);
47 destination.append(hexDigits[byte >> 4]);
48 destination.append(hexDigits[byte & 0xF]);
52 inline void placeByteAsHexCompressIfPossible(unsigned char byte, T& destination, unsigned& index, HexConversionMode mode = Uppercase)
54 const char* hexDigits = Internal::hexDigitsForMode(mode);
56 destination[index++] = hexDigits[byte >> 4];
57 destination[index++] = hexDigits[byte & 0xF];
61 inline void placeByteAsHex(unsigned char byte, T& destination, HexConversionMode mode = Uppercase)
63 const char* hexDigits = Internal::hexDigitsForMode(mode);
64 *destination++ = hexDigits[byte >> 4];
65 *destination++ = hexDigits[byte & 0xF];
69 inline void appendUnsignedAsHex(unsigned number, T& destination, HexConversionMode mode = Uppercase)
71 const char* hexDigits = Internal::hexDigitsForMode(mode);
72 Vector<UChar, 8> result;
74 result.prepend(hexDigits[number % 16]);
78 destination.append(result.data(), result.size());
81 // Same as appendUnsignedAsHex, but using exactly 'desiredDigits' for the conversion.
83 inline void appendUnsignedAsHexFixedSize(unsigned number, T& destination, unsigned desiredDigits, HexConversionMode mode = Uppercase)
85 ASSERT(desiredDigits);
87 const char* hexDigits = Internal::hexDigitsForMode(mode);
88 Vector<UChar, 8> result;
90 result.prepend(hexDigits[number % 16]);
92 } while (result.size() < desiredDigits);
94 ASSERT(result.size() == desiredDigits);
95 destination.append(result.data(), result.size());
100 using WTF::appendByteAsHex;
101 using WTF::appendUnsignedAsHex;
102 using WTF::appendUnsignedAsHexFixedSize;
103 using WTF::placeByteAsHex;
104 using WTF::placeByteAsHexCompressIfPossible;
105 using WTF::Lowercase;
107 #endif // HexNumber_h