/* * Copyright (C) 2012 Apple Inc. All Rights Reserved. * Copyright (C) 2012 Patrick Gansterer * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #ifndef IntegerToStringConversion_h #define IntegerToStringConversion_h #include "StringBuilder.h" namespace WTF { enum PositiveOrNegativeNumber { PositiveNumber, NegativeNumber }; template struct IntegerToStringConversionTrait; template<> struct IntegerToStringConversionTrait { typedef AtomicString ReturnType; typedef void AdditionalArgumentType; static ReturnType flush(LChar* characters, unsigned length, void*) { return AtomicString(characters, length); } }; template<> struct IntegerToStringConversionTrait { typedef String ReturnType; typedef void AdditionalArgumentType; static ReturnType flush(LChar* characters, unsigned length, void*) { return String(characters, length); } }; template<> struct IntegerToStringConversionTrait { typedef void ReturnType; typedef StringBuilder AdditionalArgumentType; static ReturnType flush(LChar* characters, unsigned length, StringBuilder* stringBuilder) { stringBuilder->append(characters, length); } }; template static typename IntegerToStringConversionTrait::ReturnType numberToStringImpl(UnsignedIntegerType number, AdditionalArgumentType additionalArgument) { LChar buf[sizeof(UnsignedIntegerType) * 3 + 1]; LChar* end = buf + WTF_ARRAY_LENGTH(buf); LChar* p = end; do { *--p = static_cast((number % 10) + '0'); number /= 10; } while (number); if (NumberType == NegativeNumber) *--p = '-'; return IntegerToStringConversionTrait::flush(p, static_cast(end - p), additionalArgument); } template inline typename IntegerToStringConversionTrait::ReturnType numberToStringSigned(SignedIntegerType number, typename IntegerToStringConversionTrait::AdditionalArgumentType* additionalArgument = nullptr) { if (number < 0) return numberToStringImpl::type, NegativeNumber>(-number, additionalArgument); return numberToStringImpl::type, PositiveNumber>(number, additionalArgument); } template inline typename IntegerToStringConversionTrait::ReturnType numberToStringUnsigned(UnsignedIntegerType number, typename IntegerToStringConversionTrait::AdditionalArgumentType* additionalArgument = nullptr) { return numberToStringImpl(number, additionalArgument); } } // namespace WTF #endif // IntegerToStringConversion_h