IntegerToStringConversion.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All Rights Reserved.
  3. * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
  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. #ifndef IntegerToStringConversion_h
  22. #define IntegerToStringConversion_h
  23. #include <wtf/text/StringBuilder.h>
  24. #include <wtf/text/StringImpl.h>
  25. namespace WTF {
  26. enum PositiveOrNegativeNumber {
  27. PositiveNumber,
  28. NegativeNumber
  29. };
  30. template<typename T> struct ConversionTrait;
  31. template<> struct ConversionTrait<String> {
  32. typedef PassRefPtr<StringImpl> ReturnType;
  33. typedef void AdditionalArgumentType;
  34. static inline ReturnType flush(LChar* characters, unsigned length, void*) { return StringImpl::create(characters, length); }
  35. };
  36. template<> struct ConversionTrait<StringBuilder> {
  37. typedef void ReturnType;
  38. typedef StringBuilder AdditionalArgumentType;
  39. static inline ReturnType flush(LChar* characters, unsigned length, StringBuilder* stringBuilder) { stringBuilder->append(characters, length); }
  40. };
  41. template<typename T> struct UnsignedIntegerTrait;
  42. template<> struct UnsignedIntegerTrait<int> {
  43. typedef unsigned int Type;
  44. };
  45. template<> struct UnsignedIntegerTrait<long> {
  46. typedef unsigned long Type;
  47. };
  48. template<> struct UnsignedIntegerTrait<long long> {
  49. typedef unsigned long long Type;
  50. };
  51. template<typename T, typename UnsignedIntegerType, PositiveOrNegativeNumber NumberType>
  52. static typename ConversionTrait<T>::ReturnType numberToStringImpl(UnsignedIntegerType number, typename ConversionTrait<T>::AdditionalArgumentType* additionalArgument)
  53. {
  54. LChar buf[sizeof(UnsignedIntegerType) * 3 + 1];
  55. LChar* end = buf + WTF_ARRAY_LENGTH(buf);
  56. LChar* p = end;
  57. do {
  58. *--p = static_cast<LChar>((number % 10) + '0');
  59. number /= 10;
  60. } while (number);
  61. if (NumberType == NegativeNumber)
  62. *--p = '-';
  63. return ConversionTrait<T>::flush(p, static_cast<unsigned>(end - p), additionalArgument);
  64. }
  65. template<typename T, typename SignedIntegerType>
  66. inline typename ConversionTrait<T>::ReturnType numberToStringSigned(SignedIntegerType number, typename ConversionTrait<T>::AdditionalArgumentType* additionalArgument = 0)
  67. {
  68. if (number < 0)
  69. return numberToStringImpl<T, typename UnsignedIntegerTrait<SignedIntegerType>::Type, NegativeNumber>(-number, additionalArgument);
  70. return numberToStringImpl<T, typename UnsignedIntegerTrait<SignedIntegerType>::Type, PositiveNumber>(number, additionalArgument);
  71. }
  72. template<typename T, typename UnsignedIntegerType>
  73. inline typename ConversionTrait<T>::ReturnType numberToStringUnsigned(UnsignedIntegerType number, typename ConversionTrait<T>::AdditionalArgumentType* additionalArgument = 0)
  74. {
  75. return numberToStringImpl<T, UnsignedIntegerType, PositiveNumber>(number, additionalArgument);
  76. }
  77. } // namespace WTF
  78. #endif // IntegerToStringConversion_h