1/*
2 * Copyright (C) 2003-2019 Apple Inc. 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 */
20
21#pragma once
22
23#include <array>
24#include <wtf/ASCIICType.h>
25#include <wtf/dtoa/double-conversion.h>
26#include <wtf/text/StringView.h>
27
28namespace WTF {
29
30using NumberToStringBuffer = std::array<char, 96>;
31
32WTF_EXPORT_PRIVATE const char* numberToString(float, NumberToStringBuffer&);
33WTF_EXPORT_PRIVATE const char* numberToFixedPrecisionString(float, unsigned significantFigures, NumberToStringBuffer&, bool truncateTrailingZeros = false);
34WTF_EXPORT_PRIVATE const char* numberToFixedWidthString(float, unsigned decimalPlaces, NumberToStringBuffer&);
35
36WTF_EXPORT_PRIVATE const char* numberToString(double, NumberToStringBuffer&);
37WTF_EXPORT_PRIVATE const char* numberToFixedPrecisionString(double, unsigned significantFigures, NumberToStringBuffer&, bool truncateTrailingZeros = false);
38WTF_EXPORT_PRIVATE const char* numberToFixedWidthString(double, unsigned decimalPlaces, NumberToStringBuffer&);
39
40double parseDouble(const LChar* string, size_t length, size_t& parsedLength);
41double parseDouble(const UChar* string, size_t length, size_t& parsedLength);
42double parseDouble(StringView, size_t& parsedLength);
43
44namespace Internal {
45 WTF_EXPORT_PRIVATE double parseDoubleFromLongString(const UChar* string, size_t length, size_t& parsedLength);
46}
47
48inline double parseDouble(const LChar* string, size_t length, size_t& parsedLength)
49{
50 return double_conversion::StringToDoubleConverter::StringToDouble(reinterpret_cast<const char*>(string), length, &parsedLength);
51}
52
53inline double parseDouble(const UChar* string, size_t length, size_t& parsedLength)
54{
55 const size_t conversionBufferSize = 64;
56 if (length > conversionBufferSize)
57 return Internal::parseDoubleFromLongString(string, length, parsedLength);
58 LChar conversionBuffer[conversionBufferSize];
59 for (int i = 0; i < static_cast<int>(length); ++i)
60 conversionBuffer[i] = isASCII(string[i]) ? string[i] : 0;
61 return parseDouble(conversionBuffer, length, parsedLength);
62}
63
64inline double parseDouble(StringView string, size_t& parsedLength)
65{
66 if (string.is8Bit())
67 return parseDouble(string.characters8(), string.length(), parsedLength);
68 return parseDouble(string.characters16(), string.length(), parsedLength);
69}
70
71} // namespace WTF
72
73using WTF::NumberToStringBuffer;
74using WTF::numberToString;
75using WTF::numberToFixedPrecisionString;
76using WTF::numberToFixedWidthString;
77using WTF::parseDouble;
78