12e5b6d6dSopenharmony_ci// © 2018 and later: Unicode, Inc. and others. 22e5b6d6dSopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html 32e5b6d6dSopenharmony_ci// 42e5b6d6dSopenharmony_ci// From the double-conversion library. Original license: 52e5b6d6dSopenharmony_ci// 62e5b6d6dSopenharmony_ci// Copyright 2012 the V8 project authors. All rights reserved. 72e5b6d6dSopenharmony_ci// Redistribution and use in source and binary forms, with or without 82e5b6d6dSopenharmony_ci// modification, are permitted provided that the following conditions are 92e5b6d6dSopenharmony_ci// met: 102e5b6d6dSopenharmony_ci// 112e5b6d6dSopenharmony_ci// * Redistributions of source code must retain the above copyright 122e5b6d6dSopenharmony_ci// notice, this list of conditions and the following disclaimer. 132e5b6d6dSopenharmony_ci// * Redistributions in binary form must reproduce the above 142e5b6d6dSopenharmony_ci// copyright notice, this list of conditions and the following 152e5b6d6dSopenharmony_ci// disclaimer in the documentation and/or other materials provided 162e5b6d6dSopenharmony_ci// with the distribution. 172e5b6d6dSopenharmony_ci// * Neither the name of Google Inc. nor the names of its 182e5b6d6dSopenharmony_ci// contributors may be used to endorse or promote products derived 192e5b6d6dSopenharmony_ci// from this software without specific prior written permission. 202e5b6d6dSopenharmony_ci// 212e5b6d6dSopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 222e5b6d6dSopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 232e5b6d6dSopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 242e5b6d6dSopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 252e5b6d6dSopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 262e5b6d6dSopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 272e5b6d6dSopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 282e5b6d6dSopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 292e5b6d6dSopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 302e5b6d6dSopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 312e5b6d6dSopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 322e5b6d6dSopenharmony_ci 332e5b6d6dSopenharmony_ci// ICU PATCH: ifdef around UCONFIG_NO_FORMATTING 342e5b6d6dSopenharmony_ci#include "unicode/utypes.h" 352e5b6d6dSopenharmony_ci#if !UCONFIG_NO_FORMATTING 362e5b6d6dSopenharmony_ci 372e5b6d6dSopenharmony_ci#ifndef DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_ 382e5b6d6dSopenharmony_ci#define DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_ 392e5b6d6dSopenharmony_ci 402e5b6d6dSopenharmony_ci// ICU PATCH: Customize header file paths for ICU. 412e5b6d6dSopenharmony_ci 422e5b6d6dSopenharmony_ci#include "double-conversion-utils.h" 432e5b6d6dSopenharmony_ci 442e5b6d6dSopenharmony_ci// ICU PATCH: Wrap in ICU namespace 452e5b6d6dSopenharmony_ciU_NAMESPACE_BEGIN 462e5b6d6dSopenharmony_ci 472e5b6d6dSopenharmony_cinamespace double_conversion { 482e5b6d6dSopenharmony_ci 492e5b6d6dSopenharmony_ciclass DoubleToStringConverter { 502e5b6d6dSopenharmony_ci public: 512e5b6d6dSopenharmony_ci // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint 522e5b6d6dSopenharmony_ci // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the 532e5b6d6dSopenharmony_ci // function returns false. 542e5b6d6dSopenharmony_ci static const int kMaxFixedDigitsBeforePoint = 60; 552e5b6d6dSopenharmony_ci static const int kMaxFixedDigitsAfterPoint = 100; 562e5b6d6dSopenharmony_ci 572e5b6d6dSopenharmony_ci // When calling ToExponential with a requested_digits 582e5b6d6dSopenharmony_ci // parameter > kMaxExponentialDigits then the function returns false. 592e5b6d6dSopenharmony_ci static const int kMaxExponentialDigits = 120; 602e5b6d6dSopenharmony_ci 612e5b6d6dSopenharmony_ci // When calling ToPrecision with a requested_digits 622e5b6d6dSopenharmony_ci // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits 632e5b6d6dSopenharmony_ci // then the function returns false. 642e5b6d6dSopenharmony_ci static const int kMinPrecisionDigits = 1; 652e5b6d6dSopenharmony_ci static const int kMaxPrecisionDigits = 120; 662e5b6d6dSopenharmony_ci 672e5b6d6dSopenharmony_ci // The maximal number of digits that are needed to emit a double in base 10. 682e5b6d6dSopenharmony_ci // A higher precision can be achieved by using more digits, but the shortest 692e5b6d6dSopenharmony_ci // accurate representation of any double will never use more digits than 702e5b6d6dSopenharmony_ci // kBase10MaximalLength. 712e5b6d6dSopenharmony_ci // Note that DoubleToAscii null-terminates its input. So the given buffer 722e5b6d6dSopenharmony_ci // should be at least kBase10MaximalLength + 1 characters long. 732e5b6d6dSopenharmony_ci static const int kBase10MaximalLength = 17; 742e5b6d6dSopenharmony_ci 752e5b6d6dSopenharmony_ci // The maximal number of digits that are needed to emit a single in base 10. 762e5b6d6dSopenharmony_ci // A higher precision can be achieved by using more digits, but the shortest 772e5b6d6dSopenharmony_ci // accurate representation of any single will never use more digits than 782e5b6d6dSopenharmony_ci // kBase10MaximalLengthSingle. 792e5b6d6dSopenharmony_ci static const int kBase10MaximalLengthSingle = 9; 802e5b6d6dSopenharmony_ci 812e5b6d6dSopenharmony_ci // The length of the longest string that 'ToShortest' can produce when the 822e5b6d6dSopenharmony_ci // converter is instantiated with EcmaScript defaults (see 832e5b6d6dSopenharmony_ci // 'EcmaScriptConverter') 842e5b6d6dSopenharmony_ci // This value does not include the trailing '\0' character. 852e5b6d6dSopenharmony_ci // This amount of characters is needed for negative values that hit the 862e5b6d6dSopenharmony_ci // 'decimal_in_shortest_low' limit. For example: "-0.0000033333333333333333" 872e5b6d6dSopenharmony_ci static const int kMaxCharsEcmaScriptShortest = 25; 882e5b6d6dSopenharmony_ci 892e5b6d6dSopenharmony_ci#if 0 // not needed for ICU 902e5b6d6dSopenharmony_ci enum Flags { 912e5b6d6dSopenharmony_ci NO_FLAGS = 0, 922e5b6d6dSopenharmony_ci EMIT_POSITIVE_EXPONENT_SIGN = 1, 932e5b6d6dSopenharmony_ci EMIT_TRAILING_DECIMAL_POINT = 2, 942e5b6d6dSopenharmony_ci EMIT_TRAILING_ZERO_AFTER_POINT = 4, 952e5b6d6dSopenharmony_ci UNIQUE_ZERO = 8, 962e5b6d6dSopenharmony_ci NO_TRAILING_ZERO = 16 972e5b6d6dSopenharmony_ci }; 982e5b6d6dSopenharmony_ci 992e5b6d6dSopenharmony_ci // Flags should be a bit-or combination of the possible Flags-enum. 1002e5b6d6dSopenharmony_ci // - NO_FLAGS: no special flags. 1012e5b6d6dSopenharmony_ci // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent 1022e5b6d6dSopenharmony_ci // form, emits a '+' for positive exponents. Example: 1.2e+2. 1032e5b6d6dSopenharmony_ci // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is 1042e5b6d6dSopenharmony_ci // converted into decimal format then a trailing decimal point is appended. 1052e5b6d6dSopenharmony_ci // Example: 2345.0 is converted to "2345.". 1062e5b6d6dSopenharmony_ci // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point 1072e5b6d6dSopenharmony_ci // emits a trailing '0'-character. This flag requires the 1082e5b6d6dSopenharmony_ci // EMIT_TRAILING_DECIMAL_POINT flag. 1092e5b6d6dSopenharmony_ci // Example: 2345.0 is converted to "2345.0". 1102e5b6d6dSopenharmony_ci // - UNIQUE_ZERO: "-0.0" is converted to "0.0". 1112e5b6d6dSopenharmony_ci // - NO_TRAILING_ZERO: Trailing zeros are removed from the fractional portion 1122e5b6d6dSopenharmony_ci // of the result in precision mode. Matches printf's %g. 1132e5b6d6dSopenharmony_ci // When EMIT_TRAILING_ZERO_AFTER_POINT is also given, one trailing zero is 1142e5b6d6dSopenharmony_ci // preserved. 1152e5b6d6dSopenharmony_ci // 1162e5b6d6dSopenharmony_ci // Infinity symbol and nan_symbol provide the string representation for these 1172e5b6d6dSopenharmony_ci // special values. If the string is NULL and the special value is encountered 1182e5b6d6dSopenharmony_ci // then the conversion functions return false. 1192e5b6d6dSopenharmony_ci // 1202e5b6d6dSopenharmony_ci // The exponent_character is used in exponential representations. It is 1212e5b6d6dSopenharmony_ci // usually 'e' or 'E'. 1222e5b6d6dSopenharmony_ci // 1232e5b6d6dSopenharmony_ci // When converting to the shortest representation the converter will 1242e5b6d6dSopenharmony_ci // represent input numbers in decimal format if they are in the interval 1252e5b6d6dSopenharmony_ci // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[ 1262e5b6d6dSopenharmony_ci // (lower boundary included, greater boundary excluded). 1272e5b6d6dSopenharmony_ci // Example: with decimal_in_shortest_low = -6 and 1282e5b6d6dSopenharmony_ci // decimal_in_shortest_high = 21: 1292e5b6d6dSopenharmony_ci // ToShortest(0.000001) -> "0.000001" 1302e5b6d6dSopenharmony_ci // ToShortest(0.0000001) -> "1e-7" 1312e5b6d6dSopenharmony_ci // ToShortest(111111111111111111111.0) -> "111111111111111110000" 1322e5b6d6dSopenharmony_ci // ToShortest(100000000000000000000.0) -> "100000000000000000000" 1332e5b6d6dSopenharmony_ci // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" 1342e5b6d6dSopenharmony_ci // 1352e5b6d6dSopenharmony_ci // When converting to precision mode the converter may add 1362e5b6d6dSopenharmony_ci // max_leading_padding_zeroes before returning the number in exponential 1372e5b6d6dSopenharmony_ci // format. 1382e5b6d6dSopenharmony_ci // Example with max_leading_padding_zeroes_in_precision_mode = 6. 1392e5b6d6dSopenharmony_ci // ToPrecision(0.0000012345, 2) -> "0.0000012" 1402e5b6d6dSopenharmony_ci // ToPrecision(0.00000012345, 2) -> "1.2e-7" 1412e5b6d6dSopenharmony_ci // Similarly the converter may add up to 1422e5b6d6dSopenharmony_ci // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid 1432e5b6d6dSopenharmony_ci // returning an exponential representation. A zero added by the 1442e5b6d6dSopenharmony_ci // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. 1452e5b6d6dSopenharmony_ci // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: 1462e5b6d6dSopenharmony_ci // ToPrecision(230.0, 2) -> "230" 1472e5b6d6dSopenharmony_ci // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. 1482e5b6d6dSopenharmony_ci // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. 1492e5b6d6dSopenharmony_ci // 1502e5b6d6dSopenharmony_ci // The min_exponent_width is used for exponential representations. 1512e5b6d6dSopenharmony_ci // The converter adds leading '0's to the exponent until the exponent 1522e5b6d6dSopenharmony_ci // is at least min_exponent_width digits long. 1532e5b6d6dSopenharmony_ci // The min_exponent_width is clamped to 5. 1542e5b6d6dSopenharmony_ci // As such, the exponent may never have more than 5 digits in total. 1552e5b6d6dSopenharmony_ci DoubleToStringConverter(int flags, 1562e5b6d6dSopenharmony_ci const char* infinity_symbol, 1572e5b6d6dSopenharmony_ci const char* nan_symbol, 1582e5b6d6dSopenharmony_ci char exponent_character, 1592e5b6d6dSopenharmony_ci int decimal_in_shortest_low, 1602e5b6d6dSopenharmony_ci int decimal_in_shortest_high, 1612e5b6d6dSopenharmony_ci int max_leading_padding_zeroes_in_precision_mode, 1622e5b6d6dSopenharmony_ci int max_trailing_padding_zeroes_in_precision_mode, 1632e5b6d6dSopenharmony_ci int min_exponent_width = 0) 1642e5b6d6dSopenharmony_ci : flags_(flags), 1652e5b6d6dSopenharmony_ci infinity_symbol_(infinity_symbol), 1662e5b6d6dSopenharmony_ci nan_symbol_(nan_symbol), 1672e5b6d6dSopenharmony_ci exponent_character_(exponent_character), 1682e5b6d6dSopenharmony_ci decimal_in_shortest_low_(decimal_in_shortest_low), 1692e5b6d6dSopenharmony_ci decimal_in_shortest_high_(decimal_in_shortest_high), 1702e5b6d6dSopenharmony_ci max_leading_padding_zeroes_in_precision_mode_( 1712e5b6d6dSopenharmony_ci max_leading_padding_zeroes_in_precision_mode), 1722e5b6d6dSopenharmony_ci max_trailing_padding_zeroes_in_precision_mode_( 1732e5b6d6dSopenharmony_ci max_trailing_padding_zeroes_in_precision_mode), 1742e5b6d6dSopenharmony_ci min_exponent_width_(min_exponent_width) { 1752e5b6d6dSopenharmony_ci // When 'trailing zero after the point' is set, then 'trailing point' 1762e5b6d6dSopenharmony_ci // must be set too. 1772e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) || 1782e5b6d6dSopenharmony_ci !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0)); 1792e5b6d6dSopenharmony_ci } 1802e5b6d6dSopenharmony_ci 1812e5b6d6dSopenharmony_ci // Returns a converter following the EcmaScript specification. 1822e5b6d6dSopenharmony_ci // 1832e5b6d6dSopenharmony_ci // Flags: UNIQUE_ZERO and EMIT_POSITIVE_EXPONENT_SIGN. 1842e5b6d6dSopenharmony_ci // Special values: "Infinity" and "NaN". 1852e5b6d6dSopenharmony_ci // Lower case 'e' for exponential values. 1862e5b6d6dSopenharmony_ci // decimal_in_shortest_low: -6 1872e5b6d6dSopenharmony_ci // decimal_in_shortest_high: 21 1882e5b6d6dSopenharmony_ci // max_leading_padding_zeroes_in_precision_mode: 6 1892e5b6d6dSopenharmony_ci // max_trailing_padding_zeroes_in_precision_mode: 0 1902e5b6d6dSopenharmony_ci static const DoubleToStringConverter& EcmaScriptConverter(); 1912e5b6d6dSopenharmony_ci 1922e5b6d6dSopenharmony_ci // Computes the shortest string of digits that correctly represent the input 1932e5b6d6dSopenharmony_ci // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high 1942e5b6d6dSopenharmony_ci // (see constructor) it then either returns a decimal representation, or an 1952e5b6d6dSopenharmony_ci // exponential representation. 1962e5b6d6dSopenharmony_ci // Example with decimal_in_shortest_low = -6, 1972e5b6d6dSopenharmony_ci // decimal_in_shortest_high = 21, 1982e5b6d6dSopenharmony_ci // EMIT_POSITIVE_EXPONENT_SIGN activated, and 1992e5b6d6dSopenharmony_ci // EMIT_TRAILING_DECIMAL_POINT deactivated: 2002e5b6d6dSopenharmony_ci // ToShortest(0.000001) -> "0.000001" 2012e5b6d6dSopenharmony_ci // ToShortest(0.0000001) -> "1e-7" 2022e5b6d6dSopenharmony_ci // ToShortest(111111111111111111111.0) -> "111111111111111110000" 2032e5b6d6dSopenharmony_ci // ToShortest(100000000000000000000.0) -> "100000000000000000000" 2042e5b6d6dSopenharmony_ci // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" 2052e5b6d6dSopenharmony_ci // 2062e5b6d6dSopenharmony_ci // Note: the conversion may round the output if the returned string 2072e5b6d6dSopenharmony_ci // is accurate enough to uniquely identify the input-number. 2082e5b6d6dSopenharmony_ci // For example the most precise representation of the double 9e59 equals 2092e5b6d6dSopenharmony_ci // "899999999999999918767229449717619953810131273674690656206848", but 2102e5b6d6dSopenharmony_ci // the converter will return the shorter (but still correct) "9e59". 2112e5b6d6dSopenharmony_ci // 2122e5b6d6dSopenharmony_ci // Returns true if the conversion succeeds. The conversion always succeeds 2132e5b6d6dSopenharmony_ci // except when the input value is special and no infinity_symbol or 2142e5b6d6dSopenharmony_ci // nan_symbol has been given to the constructor. 2152e5b6d6dSopenharmony_ci // 2162e5b6d6dSopenharmony_ci // The length of the longest result is the maximum of the length of the 2172e5b6d6dSopenharmony_ci // following string representations (each with possible examples): 2182e5b6d6dSopenharmony_ci // - NaN and negative infinity: "NaN", "-Infinity", "-inf". 2192e5b6d6dSopenharmony_ci // - -10^(decimal_in_shortest_high - 1): 2202e5b6d6dSopenharmony_ci // "-100000000000000000000", "-1000000000000000.0" 2212e5b6d6dSopenharmony_ci // - the longest string in range [0; -10^decimal_in_shortest_low]. Generally, 2222e5b6d6dSopenharmony_ci // this string is 3 + kBase10MaximalLength - decimal_in_shortest_low. 2232e5b6d6dSopenharmony_ci // (Sign, '0', decimal point, padding zeroes for decimal_in_shortest_low, 2242e5b6d6dSopenharmony_ci // and the significant digits). 2252e5b6d6dSopenharmony_ci // "-0.0000033333333333333333", "-0.0012345678901234567" 2262e5b6d6dSopenharmony_ci // - the longest exponential representation. (A negative number with 2272e5b6d6dSopenharmony_ci // kBase10MaximalLength significant digits). 2282e5b6d6dSopenharmony_ci // "-1.7976931348623157e+308", "-1.7976931348623157E308" 2292e5b6d6dSopenharmony_ci // In addition, the buffer must be able to hold the trailing '\0' character. 2302e5b6d6dSopenharmony_ci bool ToShortest(double value, StringBuilder* result_builder) const { 2312e5b6d6dSopenharmony_ci return ToShortestIeeeNumber(value, result_builder, SHORTEST); 2322e5b6d6dSopenharmony_ci } 2332e5b6d6dSopenharmony_ci 2342e5b6d6dSopenharmony_ci // Same as ToShortest, but for single-precision floats. 2352e5b6d6dSopenharmony_ci bool ToShortestSingle(float value, StringBuilder* result_builder) const { 2362e5b6d6dSopenharmony_ci return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE); 2372e5b6d6dSopenharmony_ci } 2382e5b6d6dSopenharmony_ci 2392e5b6d6dSopenharmony_ci 2402e5b6d6dSopenharmony_ci // Computes a decimal representation with a fixed number of digits after the 2412e5b6d6dSopenharmony_ci // decimal point. The last emitted digit is rounded. 2422e5b6d6dSopenharmony_ci // 2432e5b6d6dSopenharmony_ci // Examples: 2442e5b6d6dSopenharmony_ci // ToFixed(3.12, 1) -> "3.1" 2452e5b6d6dSopenharmony_ci // ToFixed(3.1415, 3) -> "3.142" 2462e5b6d6dSopenharmony_ci // ToFixed(1234.56789, 4) -> "1234.5679" 2472e5b6d6dSopenharmony_ci // ToFixed(1.23, 5) -> "1.23000" 2482e5b6d6dSopenharmony_ci // ToFixed(0.1, 4) -> "0.1000" 2492e5b6d6dSopenharmony_ci // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00" 2502e5b6d6dSopenharmony_ci // ToFixed(0.1, 30) -> "0.100000000000000005551115123126" 2512e5b6d6dSopenharmony_ci // ToFixed(0.1, 17) -> "0.10000000000000001" 2522e5b6d6dSopenharmony_ci // 2532e5b6d6dSopenharmony_ci // If requested_digits equals 0, then the tail of the result depends on 2542e5b6d6dSopenharmony_ci // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT. 2552e5b6d6dSopenharmony_ci // Examples, for requested_digits == 0, 2562e5b6d6dSopenharmony_ci // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be 2572e5b6d6dSopenharmony_ci // - false and false: then 123.45 -> 123 2582e5b6d6dSopenharmony_ci // 0.678 -> 1 2592e5b6d6dSopenharmony_ci // - true and false: then 123.45 -> 123. 2602e5b6d6dSopenharmony_ci // 0.678 -> 1. 2612e5b6d6dSopenharmony_ci // - true and true: then 123.45 -> 123.0 2622e5b6d6dSopenharmony_ci // 0.678 -> 1.0 2632e5b6d6dSopenharmony_ci // 2642e5b6d6dSopenharmony_ci // Returns true if the conversion succeeds. The conversion always succeeds 2652e5b6d6dSopenharmony_ci // except for the following cases: 2662e5b6d6dSopenharmony_ci // - the input value is special and no infinity_symbol or nan_symbol has 2672e5b6d6dSopenharmony_ci // been provided to the constructor, 2682e5b6d6dSopenharmony_ci // - 'value' > 10^kMaxFixedDigitsBeforePoint, or 2692e5b6d6dSopenharmony_ci // - 'requested_digits' > kMaxFixedDigitsAfterPoint. 2702e5b6d6dSopenharmony_ci // The last two conditions imply that the result for non-special values never 2712e5b6d6dSopenharmony_ci // contains more than 2722e5b6d6dSopenharmony_ci // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters 2732e5b6d6dSopenharmony_ci // (one additional character for the sign, and one for the decimal point). 2742e5b6d6dSopenharmony_ci // In addition, the buffer must be able to hold the trailing '\0' character. 2752e5b6d6dSopenharmony_ci bool ToFixed(double value, 2762e5b6d6dSopenharmony_ci int requested_digits, 2772e5b6d6dSopenharmony_ci StringBuilder* result_builder) const; 2782e5b6d6dSopenharmony_ci 2792e5b6d6dSopenharmony_ci // Computes a representation in exponential format with requested_digits 2802e5b6d6dSopenharmony_ci // after the decimal point. The last emitted digit is rounded. 2812e5b6d6dSopenharmony_ci // If requested_digits equals -1, then the shortest exponential representation 2822e5b6d6dSopenharmony_ci // is computed. 2832e5b6d6dSopenharmony_ci // 2842e5b6d6dSopenharmony_ci // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and 2852e5b6d6dSopenharmony_ci // exponent_character set to 'e'. 2862e5b6d6dSopenharmony_ci // ToExponential(3.12, 1) -> "3.1e0" 2872e5b6d6dSopenharmony_ci // ToExponential(5.0, 3) -> "5.000e0" 2882e5b6d6dSopenharmony_ci // ToExponential(0.001, 2) -> "1.00e-3" 2892e5b6d6dSopenharmony_ci // ToExponential(3.1415, -1) -> "3.1415e0" 2902e5b6d6dSopenharmony_ci // ToExponential(3.1415, 4) -> "3.1415e0" 2912e5b6d6dSopenharmony_ci // ToExponential(3.1415, 3) -> "3.142e0" 2922e5b6d6dSopenharmony_ci // ToExponential(123456789000000, 3) -> "1.235e14" 2932e5b6d6dSopenharmony_ci // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30" 2942e5b6d6dSopenharmony_ci // ToExponential(1000000000000000019884624838656.0, 32) -> 2952e5b6d6dSopenharmony_ci // "1.00000000000000001988462483865600e30" 2962e5b6d6dSopenharmony_ci // ToExponential(1234, 0) -> "1e3" 2972e5b6d6dSopenharmony_ci // 2982e5b6d6dSopenharmony_ci // Returns true if the conversion succeeds. The conversion always succeeds 2992e5b6d6dSopenharmony_ci // except for the following cases: 3002e5b6d6dSopenharmony_ci // - the input value is special and no infinity_symbol or nan_symbol has 3012e5b6d6dSopenharmony_ci // been provided to the constructor, 3022e5b6d6dSopenharmony_ci // - 'requested_digits' > kMaxExponentialDigits. 3032e5b6d6dSopenharmony_ci // 3042e5b6d6dSopenharmony_ci // The last condition implies that the result never contains more than 3052e5b6d6dSopenharmony_ci // kMaxExponentialDigits + 8 characters (the sign, the digit before the 3062e5b6d6dSopenharmony_ci // decimal point, the decimal point, the exponent character, the 3072e5b6d6dSopenharmony_ci // exponent's sign, and at most 3 exponent digits). 3082e5b6d6dSopenharmony_ci // In addition, the buffer must be able to hold the trailing '\0' character. 3092e5b6d6dSopenharmony_ci bool ToExponential(double value, 3102e5b6d6dSopenharmony_ci int requested_digits, 3112e5b6d6dSopenharmony_ci StringBuilder* result_builder) const; 3122e5b6d6dSopenharmony_ci 3132e5b6d6dSopenharmony_ci 3142e5b6d6dSopenharmony_ci // Computes 'precision' leading digits of the given 'value' and returns them 3152e5b6d6dSopenharmony_ci // either in exponential or decimal format, depending on 3162e5b6d6dSopenharmony_ci // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the 3172e5b6d6dSopenharmony_ci // constructor). 3182e5b6d6dSopenharmony_ci // The last computed digit is rounded. 3192e5b6d6dSopenharmony_ci // 3202e5b6d6dSopenharmony_ci // Example with max_leading_padding_zeroes_in_precision_mode = 6. 3212e5b6d6dSopenharmony_ci // ToPrecision(0.0000012345, 2) -> "0.0000012" 3222e5b6d6dSopenharmony_ci // ToPrecision(0.00000012345, 2) -> "1.2e-7" 3232e5b6d6dSopenharmony_ci // Similarly the converter may add up to 3242e5b6d6dSopenharmony_ci // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid 3252e5b6d6dSopenharmony_ci // returning an exponential representation. A zero added by the 3262e5b6d6dSopenharmony_ci // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. 3272e5b6d6dSopenharmony_ci // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: 3282e5b6d6dSopenharmony_ci // ToPrecision(230.0, 2) -> "230" 3292e5b6d6dSopenharmony_ci // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. 3302e5b6d6dSopenharmony_ci // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. 3312e5b6d6dSopenharmony_ci // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no 3322e5b6d6dSopenharmony_ci // EMIT_TRAILING_ZERO_AFTER_POINT: 3332e5b6d6dSopenharmony_ci // ToPrecision(123450.0, 6) -> "123450" 3342e5b6d6dSopenharmony_ci // ToPrecision(123450.0, 5) -> "123450" 3352e5b6d6dSopenharmony_ci // ToPrecision(123450.0, 4) -> "123500" 3362e5b6d6dSopenharmony_ci // ToPrecision(123450.0, 3) -> "123000" 3372e5b6d6dSopenharmony_ci // ToPrecision(123450.0, 2) -> "1.2e5" 3382e5b6d6dSopenharmony_ci // 3392e5b6d6dSopenharmony_ci // Returns true if the conversion succeeds. The conversion always succeeds 3402e5b6d6dSopenharmony_ci // except for the following cases: 3412e5b6d6dSopenharmony_ci // - the input value is special and no infinity_symbol or nan_symbol has 3422e5b6d6dSopenharmony_ci // been provided to the constructor, 3432e5b6d6dSopenharmony_ci // - precision < kMinPericisionDigits 3442e5b6d6dSopenharmony_ci // - precision > kMaxPrecisionDigits 3452e5b6d6dSopenharmony_ci // 3462e5b6d6dSopenharmony_ci // The last condition implies that the result never contains more than 3472e5b6d6dSopenharmony_ci // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the 3482e5b6d6dSopenharmony_ci // exponent character, the exponent's sign, and at most 3 exponent digits). 3492e5b6d6dSopenharmony_ci // In addition, the buffer must be able to hold the trailing '\0' character. 3502e5b6d6dSopenharmony_ci bool ToPrecision(double value, 3512e5b6d6dSopenharmony_ci int precision, 3522e5b6d6dSopenharmony_ci StringBuilder* result_builder) const; 3532e5b6d6dSopenharmony_ci#endif // not needed for ICU 3542e5b6d6dSopenharmony_ci 3552e5b6d6dSopenharmony_ci enum DtoaMode { 3562e5b6d6dSopenharmony_ci // Produce the shortest correct representation. 3572e5b6d6dSopenharmony_ci // For example the output of 0.299999999999999988897 is (the less accurate 3582e5b6d6dSopenharmony_ci // but correct) 0.3. 3592e5b6d6dSopenharmony_ci SHORTEST, 3602e5b6d6dSopenharmony_ci // Same as SHORTEST, but for single-precision floats. 3612e5b6d6dSopenharmony_ci SHORTEST_SINGLE, 3622e5b6d6dSopenharmony_ci // Produce a fixed number of digits after the decimal point. 3632e5b6d6dSopenharmony_ci // For instance fixed(0.1, 4) becomes 0.1000 3642e5b6d6dSopenharmony_ci // If the input number is big, the output will be big. 3652e5b6d6dSopenharmony_ci FIXED, 3662e5b6d6dSopenharmony_ci // Fixed number of digits (independent of the decimal point). 3672e5b6d6dSopenharmony_ci PRECISION 3682e5b6d6dSopenharmony_ci }; 3692e5b6d6dSopenharmony_ci 3702e5b6d6dSopenharmony_ci // Converts the given double 'v' to digit characters. 'v' must not be NaN, 3712e5b6d6dSopenharmony_ci // +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also 3722e5b6d6dSopenharmony_ci // applies to 'v' after it has been casted to a single-precision float. That 3732e5b6d6dSopenharmony_ci // is, in this mode static_cast<float>(v) must not be NaN, +Infinity or 3742e5b6d6dSopenharmony_ci // -Infinity. 3752e5b6d6dSopenharmony_ci // 3762e5b6d6dSopenharmony_ci // The result should be interpreted as buffer * 10^(point-length). 3772e5b6d6dSopenharmony_ci // 3782e5b6d6dSopenharmony_ci // The digits are written to the buffer in the platform's charset, which is 3792e5b6d6dSopenharmony_ci // often UTF-8 (with ASCII-range digits) but may be another charset, such 3802e5b6d6dSopenharmony_ci // as EBCDIC. 3812e5b6d6dSopenharmony_ci // 3822e5b6d6dSopenharmony_ci // The output depends on the given mode: 3832e5b6d6dSopenharmony_ci // - SHORTEST: produce the least amount of digits for which the internal 3842e5b6d6dSopenharmony_ci // identity requirement is still satisfied. If the digits are printed 3852e5b6d6dSopenharmony_ci // (together with the correct exponent) then reading this number will give 3862e5b6d6dSopenharmony_ci // 'v' again. The buffer will choose the representation that is closest to 3872e5b6d6dSopenharmony_ci // 'v'. If there are two at the same distance, than the one farther away 3882e5b6d6dSopenharmony_ci // from 0 is chosen (halfway cases - ending with 5 - are rounded up). 3892e5b6d6dSopenharmony_ci // In this mode the 'requested_digits' parameter is ignored. 3902e5b6d6dSopenharmony_ci // - SHORTEST_SINGLE: same as SHORTEST but with single-precision. 3912e5b6d6dSopenharmony_ci // - FIXED: produces digits necessary to print a given number with 3922e5b6d6dSopenharmony_ci // 'requested_digits' digits after the decimal point. The produced digits 3932e5b6d6dSopenharmony_ci // might be too short in which case the caller has to fill the remainder 3942e5b6d6dSopenharmony_ci // with '0's. 3952e5b6d6dSopenharmony_ci // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2. 3962e5b6d6dSopenharmony_ci // Halfway cases are rounded towards +/-Infinity (away from 0). The call 3972e5b6d6dSopenharmony_ci // toFixed(0.15, 2) thus returns buffer="2", point=0. 3982e5b6d6dSopenharmony_ci // The returned buffer may contain digits that would be truncated from the 3992e5b6d6dSopenharmony_ci // shortest representation of the input. 4002e5b6d6dSopenharmony_ci // - PRECISION: produces 'requested_digits' where the first digit is not '0'. 4012e5b6d6dSopenharmony_ci // Even though the length of produced digits usually equals 4022e5b6d6dSopenharmony_ci // 'requested_digits', the function is allowed to return fewer digits, in 4032e5b6d6dSopenharmony_ci // which case the caller has to fill the missing digits with '0's. 4042e5b6d6dSopenharmony_ci // Halfway cases are again rounded away from 0. 4052e5b6d6dSopenharmony_ci // DoubleToAscii expects the given buffer to be big enough to hold all 4062e5b6d6dSopenharmony_ci // digits and a terminating null-character. In SHORTEST-mode it expects a 4072e5b6d6dSopenharmony_ci // buffer of at least kBase10MaximalLength + 1. In all other modes the 4082e5b6d6dSopenharmony_ci // requested_digits parameter and the padding-zeroes limit the size of the 4092e5b6d6dSopenharmony_ci // output. Don't forget the decimal point, the exponent character and the 4102e5b6d6dSopenharmony_ci // terminating null-character when computing the maximal output size. 4112e5b6d6dSopenharmony_ci // The given length is only used in debug mode to ensure the buffer is big 4122e5b6d6dSopenharmony_ci // enough. 4132e5b6d6dSopenharmony_ci // ICU PATCH: Export this as U_I18N_API for unit tests. 4142e5b6d6dSopenharmony_ci static void U_I18N_API DoubleToAscii(double v, 4152e5b6d6dSopenharmony_ci DtoaMode mode, 4162e5b6d6dSopenharmony_ci int requested_digits, 4172e5b6d6dSopenharmony_ci char* buffer, 4182e5b6d6dSopenharmony_ci int buffer_length, 4192e5b6d6dSopenharmony_ci bool* sign, 4202e5b6d6dSopenharmony_ci int* length, 4212e5b6d6dSopenharmony_ci int* point); 4222e5b6d6dSopenharmony_ci 4232e5b6d6dSopenharmony_ci#if 0 // not needed for ICU 4242e5b6d6dSopenharmony_ci private: 4252e5b6d6dSopenharmony_ci // Implementation for ToShortest and ToShortestSingle. 4262e5b6d6dSopenharmony_ci bool ToShortestIeeeNumber(double value, 4272e5b6d6dSopenharmony_ci StringBuilder* result_builder, 4282e5b6d6dSopenharmony_ci DtoaMode mode) const; 4292e5b6d6dSopenharmony_ci 4302e5b6d6dSopenharmony_ci // If the value is a special value (NaN or Infinity) constructs the 4312e5b6d6dSopenharmony_ci // corresponding string using the configured infinity/nan-symbol. 4322e5b6d6dSopenharmony_ci // If either of them is NULL or the value is not special then the 4332e5b6d6dSopenharmony_ci // function returns false. 4342e5b6d6dSopenharmony_ci bool HandleSpecialValues(double value, StringBuilder* result_builder) const; 4352e5b6d6dSopenharmony_ci // Constructs an exponential representation (i.e. 1.234e56). 4362e5b6d6dSopenharmony_ci // The given exponent assumes a decimal point after the first decimal digit. 4372e5b6d6dSopenharmony_ci void CreateExponentialRepresentation(const char* decimal_digits, 4382e5b6d6dSopenharmony_ci int length, 4392e5b6d6dSopenharmony_ci int exponent, 4402e5b6d6dSopenharmony_ci StringBuilder* result_builder) const; 4412e5b6d6dSopenharmony_ci // Creates a decimal representation (i.e 1234.5678). 4422e5b6d6dSopenharmony_ci void CreateDecimalRepresentation(const char* decimal_digits, 4432e5b6d6dSopenharmony_ci int length, 4442e5b6d6dSopenharmony_ci int decimal_point, 4452e5b6d6dSopenharmony_ci int digits_after_point, 4462e5b6d6dSopenharmony_ci StringBuilder* result_builder) const; 4472e5b6d6dSopenharmony_ci 4482e5b6d6dSopenharmony_ci const int flags_; 4492e5b6d6dSopenharmony_ci const char* const infinity_symbol_; 4502e5b6d6dSopenharmony_ci const char* const nan_symbol_; 4512e5b6d6dSopenharmony_ci const char exponent_character_; 4522e5b6d6dSopenharmony_ci const int decimal_in_shortest_low_; 4532e5b6d6dSopenharmony_ci const int decimal_in_shortest_high_; 4542e5b6d6dSopenharmony_ci const int max_leading_padding_zeroes_in_precision_mode_; 4552e5b6d6dSopenharmony_ci const int max_trailing_padding_zeroes_in_precision_mode_; 4562e5b6d6dSopenharmony_ci const int min_exponent_width_; 4572e5b6d6dSopenharmony_ci#endif // not needed for ICU 4582e5b6d6dSopenharmony_ci 4592e5b6d6dSopenharmony_ci DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter); 4602e5b6d6dSopenharmony_ci}; 4612e5b6d6dSopenharmony_ci 4622e5b6d6dSopenharmony_ci} // namespace double_conversion 4632e5b6d6dSopenharmony_ci 4642e5b6d6dSopenharmony_ci// ICU PATCH: Close ICU namespace 4652e5b6d6dSopenharmony_ciU_NAMESPACE_END 4662e5b6d6dSopenharmony_ci 4672e5b6d6dSopenharmony_ci#endif // DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_ 4682e5b6d6dSopenharmony_ci#endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING 469