@@ -48,12 +48,11 @@ namespace double_conversion {
4848
4949class DoubleToStringConverter {
5050 public:
51- #if 0 // not needed for ICU
5251 // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint
5352 // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the
5453 // function returns false.
5554 static const int kMaxFixedDigitsBeforePoint = 60 ;
56- static const int kMaxFixedDigitsAfterPoint = 60 ;
55+ static const int kMaxFixedDigitsAfterPoint = 100 ;
5756
5857 // When calling ToExponential with a requested_digits
5958 // parameter > kMaxExponentialDigits then the function returns false.
@@ -65,12 +64,36 @@ class DoubleToStringConverter {
6564 static const int kMinPrecisionDigits = 1 ;
6665 static const int kMaxPrecisionDigits = 120 ;
6766
67+ // The maximal number of digits that are needed to emit a double in base 10.
68+ // A higher precision can be achieved by using more digits, but the shortest
69+ // accurate representation of any double will never use more digits than
70+ // kBase10MaximalLength.
71+ // Note that DoubleToAscii null-terminates its input. So the given buffer
72+ // should be at least kBase10MaximalLength + 1 characters long.
73+ static const int kBase10MaximalLength = 17 ;
74+
75+ // The maximal number of digits that are needed to emit a single in base 10.
76+ // A higher precision can be achieved by using more digits, but the shortest
77+ // accurate representation of any single will never use more digits than
78+ // kBase10MaximalLengthSingle.
79+ static const int kBase10MaximalLengthSingle = 9 ;
80+
81+ // The length of the longest string that 'ToShortest' can produce when the
82+ // converter is instantiated with EcmaScript defaults (see
83+ // 'EcmaScriptConverter')
84+ // This value does not include the trailing '\0' character.
85+ // This amount of characters is needed for negative values that hit the
86+ // 'decimal_in_shortest_low' limit. For example: "-0.0000033333333333333333"
87+ static const int kMaxCharsEcmaScriptShortest = 25 ;
88+
89+ #if 0 // not needed for ICU
6890 enum Flags {
6991 NO_FLAGS = 0,
7092 EMIT_POSITIVE_EXPONENT_SIGN = 1,
7193 EMIT_TRAILING_DECIMAL_POINT = 2,
7294 EMIT_TRAILING_ZERO_AFTER_POINT = 4,
73- UNIQUE_ZERO = 8
95+ UNIQUE_ZERO = 8,
96+ NO_TRAILING_ZERO = 16
7497 };
7598
7699 // Flags should be a bit-or combination of the possible Flags-enum.
@@ -82,9 +105,13 @@ class DoubleToStringConverter {
82105 // Example: 2345.0 is converted to "2345.".
83106 // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point
84107 // emits a trailing '0'-character. This flag requires the
85- // EXMIT_TRAILING_DECIMAL_POINT flag.
108+ // EMIT_TRAILING_DECIMAL_POINT flag.
86109 // Example: 2345.0 is converted to "2345.0".
87110 // - UNIQUE_ZERO: "-0.0" is converted to "0.0".
111+ // - NO_TRAILING_ZERO: Trailing zeros are removed from the fractional portion
112+ // of the result in precision mode. Matches printf's %g.
113+ // When EMIT_TRAILING_ZERO_AFTER_POINT is also given, one trailing zero is
114+ // preserved.
88115 //
89116 // Infinity symbol and nan_symbol provide the string representation for these
90117 // special values. If the string is NULL and the special value is encountered
@@ -152,6 +179,14 @@ class DoubleToStringConverter {
152179 }
153180
154181 // Returns a converter following the EcmaScript specification.
182+ //
183+ // Flags: UNIQUE_ZERO and EMIT_POSITIVE_EXPONENT_SIGN.
184+ // Special values: "Infinity" and "NaN".
185+ // Lower case 'e' for exponential values.
186+ // decimal_in_shortest_low: -6
187+ // decimal_in_shortest_high: 21
188+ // max_leading_padding_zeroes_in_precision_mode: 6
189+ // max_trailing_padding_zeroes_in_precision_mode: 0
155190 static const DoubleToStringConverter& EcmaScriptConverter();
156191
157192 // Computes the shortest string of digits that correctly represent the input
@@ -177,6 +212,21 @@ class DoubleToStringConverter {
177212 // Returns true if the conversion succeeds. The conversion always succeeds
178213 // except when the input value is special and no infinity_symbol or
179214 // nan_symbol has been given to the constructor.
215+ //
216+ // The length of the longest result is the maximum of the length of the
217+ // following string representations (each with possible examples):
218+ // - NaN and negative infinity: "NaN", "-Infinity", "-inf".
219+ // - -10^(decimal_in_shortest_high - 1):
220+ // "-100000000000000000000", "-1000000000000000.0"
221+ // - the longest string in range [0; -10^decimal_in_shortest_low]. Generally,
222+ // this string is 3 + kBase10MaximalLength - decimal_in_shortest_low.
223+ // (Sign, '0', decimal point, padding zeroes for decimal_in_shortest_low,
224+ // and the significant digits).
225+ // "-0.0000033333333333333333", "-0.0012345678901234567"
226+ // - the longest exponential representation. (A negative number with
227+ // kBase10MaximalLength significant digits).
228+ // "-1.7976931348623157e+308", "-1.7976931348623157E308"
229+ // In addition, the buffer must be able to hold the trailing '\0' character.
180230 bool ToShortest(double value, StringBuilder* result_builder) const {
181231 return ToShortestIeeeNumber(value, result_builder, SHORTEST);
182232 }
@@ -217,9 +267,11 @@ class DoubleToStringConverter {
217267 // been provided to the constructor,
218268 // - 'value' > 10^kMaxFixedDigitsBeforePoint, or
219269 // - 'requested_digits' > kMaxFixedDigitsAfterPoint.
220- // The last two conditions imply that the result will never contain more than
221- // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters
270+ // The last two conditions imply that the result for non-special values never
271+ // contains more than
272+ // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters
222273 // (one additional character for the sign, and one for the decimal point).
274+ // In addition, the buffer must be able to hold the trailing '\0' character.
223275 bool ToFixed(double value,
224276 int requested_digits,
225277 StringBuilder* result_builder) const;
@@ -248,14 +300,17 @@ class DoubleToStringConverter {
248300 // - the input value is special and no infinity_symbol or nan_symbol has
249301 // been provided to the constructor,
250302 // - 'requested_digits' > kMaxExponentialDigits.
251- // The last condition implies that the result will never contain more than
303+ //
304+ // The last condition implies that the result never contains more than
252305 // kMaxExponentialDigits + 8 characters (the sign, the digit before the
253306 // decimal point, the decimal point, the exponent character, the
254307 // exponent's sign, and at most 3 exponent digits).
308+ // In addition, the buffer must be able to hold the trailing '\0' character.
255309 bool ToExponential(double value,
256310 int requested_digits,
257311 StringBuilder* result_builder) const;
258312
313+
259314 // Computes 'precision' leading digits of the given 'value' and returns them
260315 // either in exponential or decimal format, depending on
261316 // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the
@@ -287,9 +342,11 @@ class DoubleToStringConverter {
287342 // been provided to the constructor,
288343 // - precision < kMinPericisionDigits
289344 // - precision > kMaxPrecisionDigits
290- // The last condition implies that the result will never contain more than
345+ //
346+ // The last condition implies that the result never contains more than
291347 // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the
292348 // exponent character, the exponent's sign, and at most 3 exponent digits).
349+ // In addition, the buffer must be able to hold the trailing '\0' character.
293350 bool ToPrecision(double value,
294351 int precision,
295352 StringBuilder* result_builder) const;
@@ -310,14 +367,6 @@ class DoubleToStringConverter {
310367 PRECISION
311368 };
312369
313- // The maximal number of digits that are needed to emit a double in base 10.
314- // A higher precision can be achieved by using more digits, but the shortest
315- // accurate representation of any double will never use more digits than
316- // kBase10MaximalLength.
317- // Note that DoubleToAscii null-terminates its input. So the given buffer
318- // should be at least kBase10MaximalLength + 1 characters long.
319- static const int kBase10MaximalLength = 17 ;
320-
321370 // Converts the given double 'v' to digit characters. 'v' must not be NaN,
322371 // +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also
323372 // applies to 'v' after it has been casted to a single-precision float. That
0 commit comments