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#include "config.h"
22#include <wtf/dtoa.h>
23
24namespace WTF {
25
26const char* numberToString(float number, NumberToStringBuffer& buffer)
27{
28 double_conversion::StringBuilder builder(&buffer[0], sizeof(buffer));
29 double_conversion::DoubleToStringConverter::EcmaScriptConverter().ToShortestSingle(number, &builder);
30 return builder.Finalize();
31}
32
33const char* numberToString(double d, NumberToStringBuffer& buffer)
34{
35 double_conversion::StringBuilder builder(&buffer[0], sizeof(buffer));
36 auto& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
37 converter.ToShortest(d, &builder);
38 return builder.Finalize();
39}
40
41static inline void truncateTrailingZeros(NumberToStringBuffer& buffer, double_conversion::StringBuilder& builder)
42{
43 size_t length = builder.position();
44 size_t decimalPointPosition = 0;
45 for (; decimalPointPosition < length; ++decimalPointPosition) {
46 if (buffer[decimalPointPosition] == '.')
47 break;
48 }
49
50 // No decimal separator found, early exit.
51 if (decimalPointPosition == length)
52 return;
53
54 size_t pastMantissa = decimalPointPosition + 1;
55 for (; pastMantissa < length; ++pastMantissa) {
56 if (buffer[pastMantissa] == 'e')
57 break;
58 }
59
60 size_t truncatedLength = pastMantissa;
61 for (; truncatedLength > decimalPointPosition + 1; --truncatedLength) {
62 if (buffer[truncatedLength - 1] != '0')
63 break;
64 }
65
66 // No trailing zeros found to strip.
67 if (truncatedLength == pastMantissa)
68 return;
69
70 // If we removed all trailing zeros, remove the decimal point as well.
71 if (truncatedLength == decimalPointPosition + 1)
72 truncatedLength = decimalPointPosition;
73
74 // Truncate the mantissa, and return the final result.
75 builder.RemoveCharacters(truncatedLength, pastMantissa);
76}
77
78const char* numberToFixedPrecisionString(float number, unsigned significantFigures, NumberToStringBuffer& buffer, bool shouldTruncateTrailingZeros)
79{
80 // For now, just call the double precision version.
81 // Do that here instead of at callers to pave the way to add a more efficient code path later.
82 return numberToFixedPrecisionString(static_cast<double>(number), significantFigures, buffer, shouldTruncateTrailingZeros);
83}
84
85const char* numberToFixedPrecisionString(double d, unsigned significantFigures, NumberToStringBuffer& buffer, bool shouldTruncateTrailingZeros)
86{
87 // Mimic sprintf("%.[precision]g", ...).
88 // "g": Signed value printed in f or e format, whichever is more compact for the given value and precision.
89 // The e format is used only when the exponent of the value is less than –4 or greater than or equal to the
90 // precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it.
91 // "precision": The precision specifies the maximum number of significant digits printed.
92 double_conversion::StringBuilder builder(&buffer[0], sizeof(buffer));
93 auto& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
94 converter.ToPrecision(d, significantFigures, &builder);
95 if (shouldTruncateTrailingZeros)
96 truncateTrailingZeros(buffer, builder);
97 return builder.Finalize();
98}
99
100const char* numberToFixedWidthString(float number, unsigned decimalPlaces, NumberToStringBuffer& buffer)
101{
102 // For now, just call the double precision version.
103 // Do that here instead of at callers to pave the way to add a more efficient code path later.
104 return numberToFixedWidthString(static_cast<double>(number), decimalPlaces, buffer);
105}
106
107const char* numberToFixedWidthString(double d, unsigned decimalPlaces, NumberToStringBuffer& buffer)
108{
109 // Mimic sprintf("%.[precision]f", ...).
110 // "f": Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits.
111 // The number of digits before the decimal point depends on the magnitude of the number, and
112 // the number of digits after the decimal point depends on the requested precision.
113 // "precision": The precision value specifies the number of digits after the decimal point.
114 // If a decimal point appears, at least one digit appears before it.
115 // The value is rounded to the appropriate number of digits.
116 double_conversion::StringBuilder builder(&buffer[0], sizeof(buffer));
117 auto& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
118 converter.ToFixed(d, decimalPlaces, &builder);
119 return builder.Finalize();
120}
121
122namespace Internal {
123
124double parseDoubleFromLongString(const UChar* string, size_t length, size_t& parsedLength)
125{
126 Vector<LChar> conversionBuffer(length);
127 for (size_t i = 0; i < length; ++i)
128 conversionBuffer[i] = isASCII(string[i]) ? string[i] : 0;
129 return parseDouble(conversionBuffer.data(), length, parsedLength);
130}
131
132} // namespace Internal
133
134} // namespace WTF
135