2 * Copyright (C) 2012 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2012 Patrick Gansterer <paroga@paroga.com>
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.
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.
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.
22 #ifndef IntegerToStringConversion_h
23 #define IntegerToStringConversion_h
25 #include "wtf/text/StringBuilder.h"
26 #include "wtf/text/StringImpl.h"
30 enum PositiveOrNegativeNumber
{
35 template<typename T
> struct ConversionTrait
;
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
); }
42 template<> struct ConversionTrait
<StringBuilder
> {
43 typedef void ReturnType
;
44 typedef StringBuilder AdditionalArgumentType
;
45 static inline ReturnType
flush(LChar
* characters
, unsigned length
, StringBuilder
* stringBuilder
) { stringBuilder
->append(characters
, length
); }
47 template<> struct ConversionTrait
<AtomicString
> {
48 typedef AtomicString ReturnType
;
49 typedef void AdditionalArgumentType
;
50 static inline ReturnType
flush(LChar
* characters
, unsigned length
, void*) { return AtomicString(characters
, length
); }
53 template<typename T
> struct UnsignedIntegerTrait
;
55 template<> struct UnsignedIntegerTrait
<int> {
56 typedef unsigned Type
;
58 template<> struct UnsignedIntegerTrait
<long> {
59 typedef unsigned long Type
;
61 template<> struct UnsignedIntegerTrait
<long long> {
62 typedef unsigned long long Type
;
65 template<typename T
, typename UnsignedIntegerType
, PositiveOrNegativeNumber NumberType
>
66 static typename ConversionTrait
<T
>::ReturnType
numberToStringImpl(UnsignedIntegerType number
, typename ConversionTrait
<T
>::AdditionalArgumentType
* additionalArgument
)
68 LChar buf
[sizeof(UnsignedIntegerType
) * 3 + 1];
69 LChar
* end
= buf
+ WTF_ARRAY_LENGTH(buf
);
73 *--p
= static_cast<LChar
>((number
% 10) + '0');
77 if (NumberType
== NegativeNumber
)
80 return ConversionTrait
<T
>::flush(p
, static_cast<unsigned>(end
- p
), additionalArgument
);
83 template<typename T
, typename SignedIntegerType
>
84 inline typename ConversionTrait
<T
>::ReturnType
numberToStringSigned(SignedIntegerType number
, typename ConversionTrait
<T
>::AdditionalArgumentType
* additionalArgument
= 0)
87 return numberToStringImpl
<T
, typename UnsignedIntegerTrait
<SignedIntegerType
>::Type
, NegativeNumber
>(-number
, additionalArgument
);
88 return numberToStringImpl
<T
, typename UnsignedIntegerTrait
<SignedIntegerType
>::Type
, PositiveNumber
>(number
, additionalArgument
);
91 template<typename T
, typename UnsignedIntegerType
>
92 inline typename ConversionTrait
<T
>::ReturnType
numberToStringUnsigned(UnsignedIntegerType number
, typename ConversionTrait
<T
>::AdditionalArgumentType
* additionalArgument
= 0)
94 return numberToStringImpl
<T
, UnsignedIntegerType
, PositiveNumber
>(number
, additionalArgument
);
99 #endif // IntegerToStringConversion_h