| 1 | // © 2018 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | // |
| 4 | // From the double-conversion library. Original license: |
| 5 | // |
| 6 | // Copyright 2012 the V8 project authors. All rights reserved. |
| 7 | // Redistribution and use in source and binary forms, with or without |
| 8 | // modification, are permitted provided that the following conditions are |
| 9 | // met: |
| 10 | // |
| 11 | // * Redistributions of source code must retain the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer. |
| 13 | // * Redistributions in binary form must reproduce the above |
| 14 | // copyright notice, this list of conditions and the following |
| 15 | // disclaimer in the documentation and/or other materials provided |
| 16 | // with the distribution. |
| 17 | // * Neither the name of Google Inc. nor the names of its |
| 18 | // contributors may be used to endorse or promote products derived |
| 19 | // from this software without specific prior written permission. |
| 20 | // |
| 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | |
| 33 | // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING |
| 34 | #include "unicode/utypes.h" |
| 35 | #if !UCONFIG_NO_FORMATTING |
| 36 | |
| 37 | #ifndef DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_ |
| 38 | #define DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_ |
| 39 | |
| 40 | // ICU PATCH: Customize header file paths for ICU. |
| 41 | |
| 42 | #include "double-conversion-utils.h" |
| 43 | |
| 44 | // ICU PATCH: Wrap in ICU namespace |
| 45 | U_NAMESPACE_BEGIN |
| 46 | |
| 47 | namespace double_conversion { |
| 48 | |
| 49 | class DoubleToStringConverter { |
| 50 | public: |
| 51 | // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint |
| 52 | // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the |
| 53 | // function returns false. |
| 54 | static const int kMaxFixedDigitsBeforePoint = 60; |
| 55 | static const int kMaxFixedDigitsAfterPoint = 100; |
| 56 | |
| 57 | // When calling ToExponential with a requested_digits |
| 58 | // parameter > kMaxExponentialDigits then the function returns false. |
| 59 | static const int kMaxExponentialDigits = 120; |
| 60 | |
| 61 | // When calling ToPrecision with a requested_digits |
| 62 | // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits |
| 63 | // then the function returns false. |
| 64 | static const int kMinPrecisionDigits = 1; |
| 65 | static const int kMaxPrecisionDigits = 120; |
| 66 | |
| 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 |
| 90 | enum Flags { |
| 91 | NO_FLAGS = 0, |
| 92 | EMIT_POSITIVE_EXPONENT_SIGN = 1, |
| 93 | EMIT_TRAILING_DECIMAL_POINT = 2, |
| 94 | EMIT_TRAILING_ZERO_AFTER_POINT = 4, |
| 95 | UNIQUE_ZERO = 8, |
| 96 | NO_TRAILING_ZERO = 16, |
| 97 | EMIT_TRAILING_DECIMAL_POINT_IN_EXPONENTIAL = 32, |
| 98 | EMIT_TRAILING_ZERO_AFTER_POINT_IN_EXPONENTIAL = 64 |
| 99 | }; |
| 100 | |
| 101 | // Flags should be a bit-or combination of the possible Flags-enum. |
| 102 | // - NO_FLAGS: no special flags. |
| 103 | // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent |
| 104 | // form, emits a '+' for positive exponents. Example: 1.2e+2. |
| 105 | // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is |
| 106 | // converted into decimal format then a trailing decimal point is appended. |
| 107 | // Example: 2345.0 is converted to "2345.". |
| 108 | // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point |
| 109 | // emits a trailing '0'-character. This flag requires the |
| 110 | // EMIT_TRAILING_DECIMAL_POINT flag. |
| 111 | // Example: 2345.0 is converted to "2345.0". |
| 112 | // - UNIQUE_ZERO: "-0.0" is converted to "0.0". |
| 113 | // - NO_TRAILING_ZERO: Trailing zeros are removed from the fractional portion |
| 114 | // of the result in precision mode. Matches printf's %g. |
| 115 | // When EMIT_TRAILING_ZERO_AFTER_POINT is also given, one trailing zero is |
| 116 | // preserved. |
| 117 | // - EMIT_TRAILING_DECIMAL_POINT_IN_EXPONENTIAL: when the input number has |
| 118 | // exactly one significant digit and is converted into exponent form then a |
| 119 | // trailing decimal point is appended to the significand in shortest mode |
| 120 | // or in precision mode with one requested digit. |
| 121 | // - EMIT_TRAILING_ZERO_AFTER_POINT_IN_EXPONENTIAL: in addition to a trailing |
| 122 | // decimal point emits a trailing '0'-character. This flag requires the |
| 123 | // EMIT_TRAILING_DECIMAL_POINT_IN_EXPONENTIAL flag. |
| 124 | // |
| 125 | // Infinity symbol and nan_symbol provide the string representation for these |
| 126 | // special values. If the string is nullptr and the special value is encountered |
| 127 | // then the conversion functions return false. |
| 128 | // |
| 129 | // The exponent_character is used in exponential representations. It is |
| 130 | // usually 'e' or 'E'. |
| 131 | // |
| 132 | // When converting to the shortest representation the converter will |
| 133 | // represent input numbers in decimal format if they are in the interval |
| 134 | // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[ |
| 135 | // (lower boundary included, greater boundary excluded). |
| 136 | // Example: with decimal_in_shortest_low = -6 and |
| 137 | // decimal_in_shortest_high = 21: |
| 138 | // ToShortest(0.000001) -> "0.000001" |
| 139 | // ToShortest(0.0000001) -> "1e-7" |
| 140 | // ToShortest(111111111111111111111.0) -> "111111111111111110000" |
| 141 | // ToShortest(100000000000000000000.0) -> "100000000000000000000" |
| 142 | // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" |
| 143 | // |
| 144 | // When converting to precision mode the converter may add |
| 145 | // max_leading_padding_zeroes before returning the number in exponential |
| 146 | // format. |
| 147 | // Example with max_leading_padding_zeroes_in_precision_mode = 6. |
| 148 | // ToPrecision(0.0000012345, 2) -> "0.0000012" |
| 149 | // ToPrecision(0.00000012345, 2) -> "1.2e-7" |
| 150 | // Similarly the converter may add up to |
| 151 | // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid |
| 152 | // returning an exponential representation. A zero added by the |
| 153 | // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. |
| 154 | // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: |
| 155 | // ToPrecision(230.0, 2) -> "230" |
| 156 | // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. |
| 157 | // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. |
| 158 | // |
| 159 | // When converting numbers with exactly one significant digit to exponent |
| 160 | // form in shortest mode or in precision mode with one requested digit, the |
| 161 | // EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT flags have |
| 162 | // no effect. Use the EMIT_TRAILING_DECIMAL_POINT_IN_EXPONENTIAL flag to |
| 163 | // append a decimal point in this case and the |
| 164 | // EMIT_TRAILING_ZERO_AFTER_POINT_IN_EXPONENTIAL flag to also append a |
| 165 | // '0'-character in this case. |
| 166 | // Example with decimal_in_shortest_low = 0: |
| 167 | // ToShortest(0.0009) -> "9e-4" |
| 168 | // with EMIT_TRAILING_DECIMAL_POINT_IN_EXPONENTIAL deactivated. |
| 169 | // ToShortest(0.0009) -> "9.e-4" |
| 170 | // with EMIT_TRAILING_DECIMAL_POINT_IN_EXPONENTIAL activated. |
| 171 | // ToShortest(0.0009) -> "9.0e-4" |
| 172 | // with EMIT_TRAILING_DECIMAL_POINT_IN_EXPONENTIAL activated and |
| 173 | // EMIT_TRAILING_ZERO_AFTER_POINT_IN_EXPONENTIAL activated. |
| 174 | // |
| 175 | // The min_exponent_width is used for exponential representations. |
| 176 | // The converter adds leading '0's to the exponent until the exponent |
| 177 | // is at least min_exponent_width digits long. |
| 178 | // The min_exponent_width is clamped to 5. |
| 179 | // As such, the exponent may never have more than 5 digits in total. |
| 180 | DoubleToStringConverter(int flags, |
| 181 | const char* infinity_symbol, |
| 182 | const char* nan_symbol, |
| 183 | char exponent_character, |
| 184 | int decimal_in_shortest_low, |
| 185 | int decimal_in_shortest_high, |
| 186 | int max_leading_padding_zeroes_in_precision_mode, |
| 187 | int max_trailing_padding_zeroes_in_precision_mode, |
| 188 | int min_exponent_width = 0) |
| 189 | : flags_(flags), |
| 190 | infinity_symbol_(infinity_symbol), |
| 191 | nan_symbol_(nan_symbol), |
| 192 | exponent_character_(exponent_character), |
| 193 | decimal_in_shortest_low_(decimal_in_shortest_low), |
| 194 | decimal_in_shortest_high_(decimal_in_shortest_high), |
| 195 | max_leading_padding_zeroes_in_precision_mode_( |
| 196 | max_leading_padding_zeroes_in_precision_mode), |
| 197 | max_trailing_padding_zeroes_in_precision_mode_( |
| 198 | max_trailing_padding_zeroes_in_precision_mode), |
| 199 | min_exponent_width_(min_exponent_width) { |
| 200 | // When 'trailing zero after the point' is set, then 'trailing point' |
| 201 | // must be set too. |
| 202 | DOUBLE_CONVERSION_ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) || |
| 203 | !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0)); |
| 204 | } |
| 205 | |
| 206 | // Returns a converter following the EcmaScript specification. |
| 207 | // |
| 208 | // Flags: UNIQUE_ZERO and EMIT_POSITIVE_EXPONENT_SIGN. |
| 209 | // Special values: "Infinity" and "NaN". |
| 210 | // Lower case 'e' for exponential values. |
| 211 | // decimal_in_shortest_low: -6 |
| 212 | // decimal_in_shortest_high: 21 |
| 213 | // max_leading_padding_zeroes_in_precision_mode: 6 |
| 214 | // max_trailing_padding_zeroes_in_precision_mode: 0 |
| 215 | static const DoubleToStringConverter& EcmaScriptConverter(); |
| 216 | |
| 217 | // Computes the shortest string of digits that correctly represent the input |
| 218 | // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high |
| 219 | // (see constructor) it then either returns a decimal representation, or an |
| 220 | // exponential representation. |
| 221 | // Example with decimal_in_shortest_low = -6, |
| 222 | // decimal_in_shortest_high = 21, |
| 223 | // EMIT_POSITIVE_EXPONENT_SIGN activated, and |
| 224 | // EMIT_TRAILING_DECIMAL_POINT deactivated: |
| 225 | // ToShortest(0.000001) -> "0.000001" |
| 226 | // ToShortest(0.0000001) -> "1e-7" |
| 227 | // ToShortest(111111111111111111111.0) -> "111111111111111110000" |
| 228 | // ToShortest(100000000000000000000.0) -> "100000000000000000000" |
| 229 | // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" |
| 230 | // |
| 231 | // Note: the conversion may round the output if the returned string |
| 232 | // is accurate enough to uniquely identify the input-number. |
| 233 | // For example the most precise representation of the double 9e59 equals |
| 234 | // "899999999999999918767229449717619953810131273674690656206848", but |
| 235 | // the converter will return the shorter (but still correct) "9e59". |
| 236 | // |
| 237 | // Returns true if the conversion succeeds. The conversion always succeeds |
| 238 | // except when the input value is special and no infinity_symbol or |
| 239 | // nan_symbol has been given to the constructor. |
| 240 | // |
| 241 | // The length of the longest result is the maximum of the length of the |
| 242 | // following string representations (each with possible examples): |
| 243 | // - NaN and negative infinity: "NaN", "-Infinity", "-inf". |
| 244 | // - -10^(decimal_in_shortest_high - 1): |
| 245 | // "-100000000000000000000", "-1000000000000000.0" |
| 246 | // - the longest string in range [0; -10^decimal_in_shortest_low]. Generally, |
| 247 | // this string is 3 + kBase10MaximalLength - decimal_in_shortest_low. |
| 248 | // (Sign, '0', decimal point, padding zeroes for decimal_in_shortest_low, |
| 249 | // and the significant digits). |
| 250 | // "-0.0000033333333333333333", "-0.0012345678901234567" |
| 251 | // - the longest exponential representation. (A negative number with |
| 252 | // kBase10MaximalLength significant digits). |
| 253 | // "-1.7976931348623157e+308", "-1.7976931348623157E308" |
| 254 | // In addition, the buffer must be able to hold the trailing '\0' character. |
| 255 | bool ToShortest(double value, StringBuilder* result_builder) const { |
| 256 | return ToShortestIeeeNumber(value, result_builder, SHORTEST); |
| 257 | } |
| 258 | |
| 259 | // Same as ToShortest, but for single-precision floats. |
| 260 | bool ToShortestSingle(float value, StringBuilder* result_builder) const { |
| 261 | return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | // Computes a decimal representation with a fixed number of digits after the |
| 266 | // decimal point. The last emitted digit is rounded. |
| 267 | // |
| 268 | // Examples: |
| 269 | // ToFixed(3.12, 1) -> "3.1" |
| 270 | // ToFixed(3.1415, 3) -> "3.142" |
| 271 | // ToFixed(1234.56789, 4) -> "1234.5679" |
| 272 | // ToFixed(1.23, 5) -> "1.23000" |
| 273 | // ToFixed(0.1, 4) -> "0.1000" |
| 274 | // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00" |
| 275 | // ToFixed(0.1, 30) -> "0.100000000000000005551115123126" |
| 276 | // ToFixed(0.1, 17) -> "0.10000000000000001" |
| 277 | // |
| 278 | // If requested_digits equals 0, then the tail of the result depends on |
| 279 | // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT. |
| 280 | // Examples, for requested_digits == 0, |
| 281 | // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be |
| 282 | // - false and false: then 123.45 -> 123 |
| 283 | // 0.678 -> 1 |
| 284 | // - true and false: then 123.45 -> 123. |
| 285 | // 0.678 -> 1. |
| 286 | // - true and true: then 123.45 -> 123.0 |
| 287 | // 0.678 -> 1.0 |
| 288 | // |
| 289 | // Returns true if the conversion succeeds. The conversion always succeeds |
| 290 | // except for the following cases: |
| 291 | // - the input value is special and no infinity_symbol or nan_symbol has |
| 292 | // been provided to the constructor, |
| 293 | // - 'value' > 10^kMaxFixedDigitsBeforePoint, or |
| 294 | // - 'requested_digits' > kMaxFixedDigitsAfterPoint. |
| 295 | // The last two conditions imply that the result for non-special values never |
| 296 | // contains more than |
| 297 | // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters |
| 298 | // (one additional character for the sign, and one for the decimal point). |
| 299 | // In addition, the buffer must be able to hold the trailing '\0' character. |
| 300 | bool ToFixed(double value, |
| 301 | int requested_digits, |
| 302 | StringBuilder* result_builder) const; |
| 303 | |
| 304 | // Computes a representation in exponential format with requested_digits |
| 305 | // after the decimal point. The last emitted digit is rounded. |
| 306 | // If requested_digits equals -1, then the shortest exponential representation |
| 307 | // is computed. |
| 308 | // |
| 309 | // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and |
| 310 | // exponent_character set to 'e'. |
| 311 | // ToExponential(3.12, 1) -> "3.1e0" |
| 312 | // ToExponential(5.0, 3) -> "5.000e0" |
| 313 | // ToExponential(0.001, 2) -> "1.00e-3" |
| 314 | // ToExponential(3.1415, -1) -> "3.1415e0" |
| 315 | // ToExponential(3.1415, 4) -> "3.1415e0" |
| 316 | // ToExponential(3.1415, 3) -> "3.142e0" |
| 317 | // ToExponential(123456789000000, 3) -> "1.235e14" |
| 318 | // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30" |
| 319 | // ToExponential(1000000000000000019884624838656.0, 32) -> |
| 320 | // "1.00000000000000001988462483865600e30" |
| 321 | // ToExponential(1234, 0) -> "1e3" |
| 322 | // |
| 323 | // Returns true if the conversion succeeds. The conversion always succeeds |
| 324 | // except for the following cases: |
| 325 | // - the input value is special and no infinity_symbol or nan_symbol has |
| 326 | // been provided to the constructor, |
| 327 | // - 'requested_digits' > kMaxExponentialDigits. |
| 328 | // |
| 329 | // The last condition implies that the result never contains more than |
| 330 | // kMaxExponentialDigits + 8 characters (the sign, the digit before the |
| 331 | // decimal point, the decimal point, the exponent character, the |
| 332 | // exponent's sign, and at most 3 exponent digits). |
| 333 | // In addition, the buffer must be able to hold the trailing '\0' character. |
| 334 | bool ToExponential(double value, |
| 335 | int requested_digits, |
| 336 | StringBuilder* result_builder) const; |
| 337 | |
| 338 | |
| 339 | // Computes 'precision' leading digits of the given 'value' and returns them |
| 340 | // either in exponential or decimal format, depending on |
| 341 | // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the |
| 342 | // constructor). |
| 343 | // The last computed digit is rounded. |
| 344 | // |
| 345 | // Example with max_leading_padding_zeroes_in_precision_mode = 6. |
| 346 | // ToPrecision(0.0000012345, 2) -> "0.0000012" |
| 347 | // ToPrecision(0.00000012345, 2) -> "1.2e-7" |
| 348 | // Similarly the converter may add up to |
| 349 | // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid |
| 350 | // returning an exponential representation. A zero added by the |
| 351 | // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. |
| 352 | // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: |
| 353 | // ToPrecision(230.0, 2) -> "230" |
| 354 | // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. |
| 355 | // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. |
| 356 | // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no |
| 357 | // EMIT_TRAILING_ZERO_AFTER_POINT: |
| 358 | // ToPrecision(123450.0, 6) -> "123450" |
| 359 | // ToPrecision(123450.0, 5) -> "123450" |
| 360 | // ToPrecision(123450.0, 4) -> "123500" |
| 361 | // ToPrecision(123450.0, 3) -> "123000" |
| 362 | // ToPrecision(123450.0, 2) -> "1.2e5" |
| 363 | // |
| 364 | // Returns true if the conversion succeeds. The conversion always succeeds |
| 365 | // except for the following cases: |
| 366 | // - the input value is special and no infinity_symbol or nan_symbol has |
| 367 | // been provided to the constructor, |
| 368 | // - precision < kMinPericisionDigits |
| 369 | // - precision > kMaxPrecisionDigits |
| 370 | // |
| 371 | // The last condition implies that the result never contains more than |
| 372 | // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the |
| 373 | // exponent character, the exponent's sign, and at most 3 exponent digits). |
| 374 | // In addition, the buffer must be able to hold the trailing '\0' character. |
| 375 | bool ToPrecision(double value, |
| 376 | int precision, |
| 377 | StringBuilder* result_builder) const; |
| 378 | #endif // not needed for ICU |
| 379 | |
| 380 | enum DtoaMode { |
| 381 | // Produce the shortest correct representation. |
| 382 | // For example the output of 0.299999999999999988897 is (the less accurate |
| 383 | // but correct) 0.3. |
| 384 | SHORTEST, |
| 385 | // Same as SHORTEST, but for single-precision floats. |
| 386 | SHORTEST_SINGLE, |
| 387 | // Produce a fixed number of digits after the decimal point. |
| 388 | // For instance fixed(0.1, 4) becomes 0.1000 |
| 389 | // If the input number is big, the output will be big. |
| 390 | FIXED, |
| 391 | // Fixed number of digits (independent of the decimal point). |
| 392 | PRECISION |
| 393 | }; |
| 394 | |
| 395 | // Converts the given double 'v' to digit characters. 'v' must not be NaN, |
| 396 | // +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also |
| 397 | // applies to 'v' after it has been casted to a single-precision float. That |
| 398 | // is, in this mode static_cast<float>(v) must not be NaN, +Infinity or |
| 399 | // -Infinity. |
| 400 | // |
| 401 | // The result should be interpreted as buffer * 10^(point-length). |
| 402 | // |
| 403 | // The digits are written to the buffer in the platform's charset, which is |
| 404 | // often UTF-8 (with ASCII-range digits) but may be another charset, such |
| 405 | // as EBCDIC. |
| 406 | // |
| 407 | // The output depends on the given mode: |
| 408 | // - SHORTEST: produce the least amount of digits for which the internal |
| 409 | // identity requirement is still satisfied. If the digits are printed |
| 410 | // (together with the correct exponent) then reading this number will give |
| 411 | // 'v' again. The buffer will choose the representation that is closest to |
| 412 | // 'v'. If there are two at the same distance, than the one farther away |
| 413 | // from 0 is chosen (halfway cases - ending with 5 - are rounded up). |
| 414 | // In this mode the 'requested_digits' parameter is ignored. |
| 415 | // - SHORTEST_SINGLE: same as SHORTEST but with single-precision. |
| 416 | // - FIXED: produces digits necessary to print a given number with |
| 417 | // 'requested_digits' digits after the decimal point. The produced digits |
| 418 | // might be too short in which case the caller has to fill the remainder |
| 419 | // with '0's. |
| 420 | // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2. |
| 421 | // Halfway cases are rounded towards +/-Infinity (away from 0). The call |
| 422 | // toFixed(0.15, 2) thus returns buffer="2", point=0. |
| 423 | // The returned buffer may contain digits that would be truncated from the |
| 424 | // shortest representation of the input. |
| 425 | // - PRECISION: produces 'requested_digits' where the first digit is not '0'. |
| 426 | // Even though the length of produced digits usually equals |
| 427 | // 'requested_digits', the function is allowed to return fewer digits, in |
| 428 | // which case the caller has to fill the missing digits with '0's. |
| 429 | // Halfway cases are again rounded away from 0. |
| 430 | // DoubleToAscii expects the given buffer to be big enough to hold all |
| 431 | // digits and a terminating null-character. In SHORTEST-mode it expects a |
| 432 | // buffer of at least kBase10MaximalLength + 1. In all other modes the |
| 433 | // requested_digits parameter and the padding-zeroes limit the size of the |
| 434 | // output. Don't forget the decimal point, the exponent character and the |
| 435 | // terminating null-character when computing the maximal output size. |
| 436 | // The given length is only used in debug mode to ensure the buffer is big |
| 437 | // enough. |
| 438 | // ICU PATCH: Export this as U_I18N_API for unit tests. |
| 439 | static void U_I18N_API DoubleToAscii(double v, |
| 440 | DtoaMode mode, |
| 441 | int requested_digits, |
| 442 | char* buffer, |
| 443 | int buffer_length, |
| 444 | bool* sign, |
| 445 | int* length, |
| 446 | int* point); |
| 447 | |
| 448 | #if 0 // not needed for ICU |
| 449 | private: |
| 450 | // Implementation for ToShortest and ToShortestSingle. |
| 451 | bool ToShortestIeeeNumber(double value, |
| 452 | StringBuilder* result_builder, |
| 453 | DtoaMode mode) const; |
| 454 | |
| 455 | // If the value is a special value (NaN or Infinity) constructs the |
| 456 | // corresponding string using the configured infinity/nan-symbol. |
| 457 | // If either of them is nullptr or the value is not special then the |
| 458 | // function returns false. |
| 459 | bool HandleSpecialValues(double value, StringBuilder* result_builder) const; |
| 460 | // Constructs an exponential representation (i.e. 1.234e56). |
| 461 | // The given exponent assumes a decimal point after the first decimal digit. |
| 462 | void CreateExponentialRepresentation(const char* decimal_digits, |
| 463 | int length, |
| 464 | int exponent, |
| 465 | StringBuilder* result_builder) const; |
| 466 | // Creates a decimal representation (i.e 1234.5678). |
| 467 | void CreateDecimalRepresentation(const char* decimal_digits, |
| 468 | int length, |
| 469 | int decimal_point, |
| 470 | int digits_after_point, |
| 471 | StringBuilder* result_builder) const; |
| 472 | |
| 473 | const int flags_; |
| 474 | const char* const infinity_symbol_; |
| 475 | const char* const nan_symbol_; |
| 476 | const char exponent_character_; |
| 477 | const int decimal_in_shortest_low_; |
| 478 | const int decimal_in_shortest_high_; |
| 479 | const int max_leading_padding_zeroes_in_precision_mode_; |
| 480 | const int max_trailing_padding_zeroes_in_precision_mode_; |
| 481 | const int min_exponent_width_; |
| 482 | #endif // not needed for ICU |
| 483 | |
| 484 | DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter); |
| 485 | }; |
| 486 | |
| 487 | } // namespace double_conversion |
| 488 | |
| 489 | // ICU PATCH: Close ICU namespace |
| 490 | U_NAMESPACE_END |
| 491 | |
| 492 | #endif // DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_ |
| 493 | #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING |