HexNumber.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2011 Research In Motion Limited. All rights reserved.
  3. *
  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. #ifndef HexNumber_h
  20. #define HexNumber_h
  21. #include <wtf/text/StringConcatenate.h>
  22. namespace WTF {
  23. enum HexConversionMode {
  24. Lowercase,
  25. Uppercase
  26. };
  27. namespace Internal {
  28. const LChar lowerHexDigits[17] = "0123456789abcdef";
  29. const LChar upperHexDigits[17] = "0123456789ABCDEF";
  30. inline const LChar* hexDigitsForMode(HexConversionMode mode)
  31. {
  32. return mode == Lowercase ? lowerHexDigits : upperHexDigits;
  33. }
  34. }; // namespace Internal
  35. template<typename T>
  36. inline void appendByteAsHex(unsigned char byte, T& destination, HexConversionMode mode = Uppercase)
  37. {
  38. const LChar* hexDigits = Internal::hexDigitsForMode(mode);
  39. destination.append(hexDigits[byte >> 4]);
  40. destination.append(hexDigits[byte & 0xF]);
  41. }
  42. template<typename T>
  43. inline void placeByteAsHexCompressIfPossible(unsigned char byte, T& destination, unsigned& index, HexConversionMode mode = Uppercase)
  44. {
  45. const LChar* hexDigits = Internal::hexDigitsForMode(mode);
  46. if (byte >= 0x10)
  47. destination[index++] = hexDigits[byte >> 4];
  48. destination[index++] = hexDigits[byte & 0xF];
  49. }
  50. template<typename T>
  51. inline void placeByteAsHex(unsigned char byte, T& destination, HexConversionMode mode = Uppercase)
  52. {
  53. const LChar* hexDigits = Internal::hexDigitsForMode(mode);
  54. *destination++ = hexDigits[byte >> 4];
  55. *destination++ = hexDigits[byte & 0xF];
  56. }
  57. template<typename T>
  58. inline void appendUnsignedAsHex(unsigned number, T& destination, HexConversionMode mode = Uppercase)
  59. {
  60. const LChar* hexDigits = Internal::hexDigitsForMode(mode);
  61. Vector<LChar, 8> result;
  62. do {
  63. result.append(hexDigits[number % 16]);
  64. number >>= 4;
  65. } while (number > 0);
  66. result.reverse();
  67. destination.append(result.data(), result.size());
  68. }
  69. // Same as appendUnsignedAsHex, but using exactly 'desiredDigits' for the conversion.
  70. template<typename T>
  71. inline void appendUnsignedAsHexFixedSize(unsigned number, T& destination, unsigned desiredDigits, HexConversionMode mode = Uppercase)
  72. {
  73. ASSERT(desiredDigits);
  74. const LChar* hexDigits = Internal::hexDigitsForMode(mode);
  75. Vector<LChar, 8> result;
  76. do {
  77. result.append(hexDigits[number % 16]);
  78. number >>= 4;
  79. } while (result.size() < desiredDigits);
  80. ASSERT(result.size() == desiredDigits);
  81. result.reverse();
  82. destination.append(result.data(), result.size());
  83. }
  84. } // namespace WTF
  85. using WTF::appendByteAsHex;
  86. using WTF::appendUnsignedAsHex;
  87. using WTF::appendUnsignedAsHexFixedSize;
  88. using WTF::placeByteAsHex;
  89. using WTF::placeByteAsHexCompressIfPossible;
  90. using WTF::Lowercase;
  91. #endif // HexNumber_h