| OLD | NEW |
| (Empty) |
| 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 | |
| 22 #ifndef IntegerToStringConversion_h | |
| 23 #define IntegerToStringConversion_h | |
| 24 | |
| 25 #include <wtf/text/StringBuilder.h> | |
| 26 #include <wtf/text/StringImpl.h> | |
| 27 | |
| 28 namespace WTF { | |
| 29 | |
| 30 enum PositiveOrNegativeNumber { | |
| 31 PositiveNumber, | |
| 32 NegativeNumber | |
| 33 }; | |
| 34 | |
| 35 template<typename T> struct ConversionTrait; | |
| 36 | |
| 37 template<> struct ConversionTrait<String> { | |
| 38 typedef PassRefPtr<StringImpl> ReturnType; | |
| 39 typedef void AdditionalArgumentType; | |
| 40 static inline ReturnType flush(LChar* characters, unsigned length, void*) {
return StringImpl::create(characters, length); } | |
| 41 }; | |
| 42 template<> struct ConversionTrait<StringBuilder> { | |
| 43 typedef void ReturnType; | |
| 44 typedef StringBuilder AdditionalArgumentType; | |
| 45 static inline ReturnType flush(LChar* characters, unsigned length, StringBui
lder* stringBuilder) { stringBuilder->append(characters, length); } | |
| 46 }; | |
| 47 | |
| 48 template<typename T> struct UnsignedIntegerTrait; | |
| 49 | |
| 50 template<> struct UnsignedIntegerTrait<int> { | |
| 51 typedef unsigned int Type; | |
| 52 }; | |
| 53 template<> struct UnsignedIntegerTrait<long> { | |
| 54 typedef unsigned long Type; | |
| 55 }; | |
| 56 template<> struct UnsignedIntegerTrait<long long> { | |
| 57 typedef unsigned long long Type; | |
| 58 }; | |
| 59 | |
| 60 template<typename T, typename UnsignedIntegerType, PositiveOrNegativeNumber Numb
erType> | |
| 61 static typename ConversionTrait<T>::ReturnType numberToStringImpl(UnsignedIntege
rType number, typename ConversionTrait<T>::AdditionalArgumentType* additionalArg
ument) | |
| 62 { | |
| 63 LChar buf[sizeof(UnsignedIntegerType) * 3 + 1]; | |
| 64 LChar* end = buf + WTF_ARRAY_LENGTH(buf); | |
| 65 LChar* p = end; | |
| 66 | |
| 67 do { | |
| 68 *--p = static_cast<LChar>((number % 10) + '0'); | |
| 69 number /= 10; | |
| 70 } while (number); | |
| 71 | |
| 72 if (NumberType == NegativeNumber) | |
| 73 *--p = '-'; | |
| 74 | |
| 75 return ConversionTrait<T>::flush(p, static_cast<unsigned>(end - p), addition
alArgument); | |
| 76 } | |
| 77 | |
| 78 template<typename T, typename SignedIntegerType> | |
| 79 inline typename ConversionTrait<T>::ReturnType numberToStringSigned(SignedIntege
rType number, typename ConversionTrait<T>::AdditionalArgumentType* additionalArg
ument = 0) | |
| 80 { | |
| 81 if (number < 0) | |
| 82 return numberToStringImpl<T, typename UnsignedIntegerTrait<SignedInteger
Type>::Type, NegativeNumber>(-number, additionalArgument); | |
| 83 return numberToStringImpl<T, typename UnsignedIntegerTrait<SignedIntegerType
>::Type, PositiveNumber>(number, additionalArgument); | |
| 84 } | |
| 85 | |
| 86 template<typename T, typename UnsignedIntegerType> | |
| 87 inline typename ConversionTrait<T>::ReturnType numberToStringUnsigned(UnsignedIn
tegerType number, typename ConversionTrait<T>::AdditionalArgumentType* additiona
lArgument = 0) | |
| 88 { | |
| 89 return numberToStringImpl<T, UnsignedIntegerType, PositiveNumber>(number, ad
ditionalArgument); | |
| 90 } | |
| 91 | |
| 92 } // namespace WTF | |
| 93 | |
| 94 #endif // IntegerToStringConversion_h | |
| OLD | NEW |