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 }; 98 99 // Flags should be a bit-or combination of the possible Flags-enum. 100 // - NO_FLAGS: no special flags. 101 // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent 102 // form, emits a '+' for positive exponents. Example: 1.2e+2. 103 // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is 104 // converted into decimal format then a trailing decimal point is appended. 105 // Example: 2345.0 is converted to "2345.". 106 // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point 107 // emits a trailing '0'-character. This flag requires the 108 // EMIT_TRAILING_DECIMAL_POINT flag. 109 // Example: 2345.0 is converted to "2345.0". 110 // - 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. 115 // 116 // Infinity symbol and nan_symbol provide the string representation for these 117 // special values. If the string is NULL and the special value is encountered 118 // then the conversion functions return false. 119 // 120 // The exponent_character is used in exponential representations. It is 121 // usually 'e' or 'E'. 122 // 123 // When converting to the shortest representation the converter will 124 // represent input numbers in decimal format if they are in the interval 125 // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[ 126 // (lower boundary included, greater boundary excluded). 127 // Example: with decimal_in_shortest_low = -6 and 128 // decimal_in_shortest_high = 21: 129 // ToShortest(0.000001) -> "0.000001" 130 // ToShortest(0.0000001) -> "1e-7" 131 // ToShortest(111111111111111111111.0) -> "111111111111111110000" 132 // ToShortest(100000000000000000000.0) -> "100000000000000000000" 133 // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" 134 // 135 // When converting to precision mode the converter may add 136 // max_leading_padding_zeroes before returning the number in exponential 137 // format. 138 // Example with max_leading_padding_zeroes_in_precision_mode = 6. 139 // ToPrecision(0.0000012345, 2) -> "0.0000012" 140 // ToPrecision(0.00000012345, 2) -> "1.2e-7" 141 // Similarily the converter may add up to 142 // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid 143 // returning an exponential representation. A zero added by the 144 // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. 145 // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: 146 // ToPrecision(230.0, 2) -> "230" 147 // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. 148 // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. 149 // 150 // The min_exponent_width is used for exponential representations. 151 // The converter adds leading '0's to the exponent until the exponent 152 // is at least min_exponent_width digits long. 153 // The min_exponent_width is clamped to 5. 154 // As such, the exponent may never have more than 5 digits in total. 155 DoubleToStringConverter(int flags, 156 const char* infinity_symbol, 157 const char* nan_symbol, 158 char exponent_character, 159 int decimal_in_shortest_low, 160 int decimal_in_shortest_high, 161 int max_leading_padding_zeroes_in_precision_mode, 162 int max_trailing_padding_zeroes_in_precision_mode, 163 int min_exponent_width = 0) 164 : flags_(flags), 165 infinity_symbol_(infinity_symbol), 166 nan_symbol_(nan_symbol), 167 exponent_character_(exponent_character), 168 decimal_in_shortest_low_(decimal_in_shortest_low), 169 decimal_in_shortest_high_(decimal_in_shortest_high), 170 max_leading_padding_zeroes_in_precision_mode_( 171 max_leading_padding_zeroes_in_precision_mode), 172 max_trailing_padding_zeroes_in_precision_mode_( 173 max_trailing_padding_zeroes_in_precision_mode), 174 min_exponent_width_(min_exponent_width) { 175 // When 'trailing zero after the point' is set, then 'trailing point' 176 // must be set too. 177 DOUBLE_CONVERSION_ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) || 178 !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0)); 179 } 180 181 // 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 190 static const DoubleToStringConverter& EcmaScriptConverter(); 191 192 // Computes the shortest string of digits that correctly represent the input 193 // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high 194 // (see constructor) it then either returns a decimal representation, or an 195 // exponential representation. 196 // Example with decimal_in_shortest_low = -6, 197 // decimal_in_shortest_high = 21, 198 // EMIT_POSITIVE_EXPONENT_SIGN activated, and 199 // EMIT_TRAILING_DECIMAL_POINT deactived: 200 // ToShortest(0.000001) -> "0.000001" 201 // ToShortest(0.0000001) -> "1e-7" 202 // ToShortest(111111111111111111111.0) -> "111111111111111110000" 203 // ToShortest(100000000000000000000.0) -> "100000000000000000000" 204 // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" 205 // 206 // Note: the conversion may round the output if the returned string 207 // is accurate enough to uniquely identify the input-number. 208 // For example the most precise representation of the double 9e59 equals 209 // "899999999999999918767229449717619953810131273674690656206848", but 210 // the converter will return the shorter (but still correct) "9e59". 211 // 212 // Returns true if the conversion succeeds. The conversion always succeeds 213 // except when the input value is special and no infinity_symbol or 214 // 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. 230 bool ToShortest(double value, StringBuilder* result_builder) const { 231 return ToShortestIeeeNumber(value, result_builder, SHORTEST); 232 } 233 234 // Same as ToShortest, but for single-precision floats. 235 bool ToShortestSingle(float value, StringBuilder* result_builder) const { 236 return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE); 237 } 238 239 240 // Computes a decimal representation with a fixed number of digits after the 241 // decimal point. The last emitted digit is rounded. 242 // 243 // Examples: 244 // ToFixed(3.12, 1) -> "3.1" 245 // ToFixed(3.1415, 3) -> "3.142" 246 // ToFixed(1234.56789, 4) -> "1234.5679" 247 // ToFixed(1.23, 5) -> "1.23000" 248 // ToFixed(0.1, 4) -> "0.1000" 249 // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00" 250 // ToFixed(0.1, 30) -> "0.100000000000000005551115123126" 251 // ToFixed(0.1, 17) -> "0.10000000000000001" 252 // 253 // If requested_digits equals 0, then the tail of the result depends on 254 // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT. 255 // Examples, for requested_digits == 0, 256 // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be 257 // - false and false: then 123.45 -> 123 258 // 0.678 -> 1 259 // - true and false: then 123.45 -> 123. 260 // 0.678 -> 1. 261 // - true and true: then 123.45 -> 123.0 262 // 0.678 -> 1.0 263 // 264 // Returns true if the conversion succeeds. The conversion always succeeds 265 // except for the following cases: 266 // - the input value is special and no infinity_symbol or nan_symbol has 267 // been provided to the constructor, 268 // - 'value' > 10^kMaxFixedDigitsBeforePoint, or 269 // - 'requested_digits' > kMaxFixedDigitsAfterPoint. 270 // The last two conditions imply that the result for non-special values never 271 // contains more than 272 // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters 273 // (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. 275 bool ToFixed(double value, 276 int requested_digits, 277 StringBuilder* result_builder) const; 278 279 // Computes a representation in exponential format with requested_digits 280 // after the decimal point. The last emitted digit is rounded. 281 // If requested_digits equals -1, then the shortest exponential representation 282 // is computed. 283 // 284 // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and 285 // exponent_character set to 'e'. 286 // ToExponential(3.12, 1) -> "3.1e0" 287 // ToExponential(5.0, 3) -> "5.000e0" 288 // ToExponential(0.001, 2) -> "1.00e-3" 289 // ToExponential(3.1415, -1) -> "3.1415e0" 290 // ToExponential(3.1415, 4) -> "3.1415e0" 291 // ToExponential(3.1415, 3) -> "3.142e0" 292 // ToExponential(123456789000000, 3) -> "1.235e14" 293 // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30" 294 // ToExponential(1000000000000000019884624838656.0, 32) -> 295 // "1.00000000000000001988462483865600e30" 296 // ToExponential(1234, 0) -> "1e3" 297 // 298 // Returns true if the conversion succeeds. The conversion always succeeds 299 // except for the following cases: 300 // - the input value is special and no infinity_symbol or nan_symbol has 301 // been provided to the constructor, 302 // - 'requested_digits' > kMaxExponentialDigits. 303 // 304 // The last condition implies that the result never contains more than 305 // kMaxExponentialDigits + 8 characters (the sign, the digit before the 306 // decimal point, the decimal point, the exponent character, the 307 // exponent's sign, and at most 3 exponent digits). 308 // In addition, the buffer must be able to hold the trailing '\0' character. 309 bool ToExponential(double value, 310 int requested_digits, 311 StringBuilder* result_builder) const; 312 313 314 // Computes 'precision' leading digits of the given 'value' and returns them 315 // either in exponential or decimal format, depending on 316 // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the 317 // constructor). 318 // The last computed digit is rounded. 319 // 320 // Example with max_leading_padding_zeroes_in_precision_mode = 6. 321 // ToPrecision(0.0000012345, 2) -> "0.0000012" 322 // ToPrecision(0.00000012345, 2) -> "1.2e-7" 323 // Similarily the converter may add up to 324 // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid 325 // returning an exponential representation. A zero added by the 326 // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. 327 // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: 328 // ToPrecision(230.0, 2) -> "230" 329 // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. 330 // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT. 331 // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no 332 // EMIT_TRAILING_ZERO_AFTER_POINT: 333 // ToPrecision(123450.0, 6) -> "123450" 334 // ToPrecision(123450.0, 5) -> "123450" 335 // ToPrecision(123450.0, 4) -> "123500" 336 // ToPrecision(123450.0, 3) -> "123000" 337 // ToPrecision(123450.0, 2) -> "1.2e5" 338 // 339 // Returns true if the conversion succeeds. The conversion always succeeds 340 // except for the following cases: 341 // - the input value is special and no infinity_symbol or nan_symbol has 342 // been provided to the constructor, 343 // - precision < kMinPericisionDigits 344 // - precision > kMaxPrecisionDigits 345 // 346 // The last condition implies that the result never contains more than 347 // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the 348 // 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. 350 bool ToPrecision(double value, 351 int precision, 352 StringBuilder* result_builder) const; 353 #endif // not needed for ICU 354 355 enum DtoaMode { 356 // Produce the shortest correct representation. 357 // For example the output of 0.299999999999999988897 is (the less accurate 358 // but correct) 0.3. 359 SHORTEST, 360 // Same as SHORTEST, but for single-precision floats. 361 SHORTEST_SINGLE, 362 // Produce a fixed number of digits after the decimal point. 363 // For instance fixed(0.1, 4) becomes 0.1000 364 // If the input number is big, the output will be big. 365 FIXED, 366 // Fixed number of digits (independent of the decimal point). 367 PRECISION 368 }; 369 370 // Converts the given double 'v' to digit characters. 'v' must not be NaN, 371 // +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also 372 // applies to 'v' after it has been casted to a single-precision float. That 373 // is, in this mode static_cast<float>(v) must not be NaN, +Infinity or 374 // -Infinity. 375 // 376 // The result should be interpreted as buffer * 10^(point-length). 377 // 378 // The digits are written to the buffer in the platform's charset, which is 379 // often UTF-8 (with ASCII-range digits) but may be another charset, such 380 // as EBCDIC. 381 // 382 // The output depends on the given mode: 383 // - SHORTEST: produce the least amount of digits for which the internal 384 // identity requirement is still satisfied. If the digits are printed 385 // (together with the correct exponent) then reading this number will give 386 // 'v' again. The buffer will choose the representation that is closest to 387 // 'v'. If there are two at the same distance, than the one farther away 388 // from 0 is chosen (halfway cases - ending with 5 - are rounded up). 389 // In this mode the 'requested_digits' parameter is ignored. 390 // - SHORTEST_SINGLE: same as SHORTEST but with single-precision. 391 // - FIXED: produces digits necessary to print a given number with 392 // 'requested_digits' digits after the decimal point. The produced digits 393 // might be too short in which case the caller has to fill the remainder 394 // with '0's. 395 // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2. 396 // Halfway cases are rounded towards +/-Infinity (away from 0). The call 397 // toFixed(0.15, 2) thus returns buffer="2", point=0. 398 // The returned buffer may contain digits that would be truncated from the 399 // shortest representation of the input. 400 // - PRECISION: produces 'requested_digits' where the first digit is not '0'. 401 // Even though the length of produced digits usually equals 402 // 'requested_digits', the function is allowed to return fewer digits, in 403 // which case the caller has to fill the missing digits with '0's. 404 // Halfway cases are again rounded away from 0. 405 // DoubleToAscii expects the given buffer to be big enough to hold all 406 // digits and a terminating null-character. In SHORTEST-mode it expects a 407 // buffer of at least kBase10MaximalLength + 1. In all other modes the 408 // requested_digits parameter and the padding-zeroes limit the size of the 409 // output. Don't forget the decimal point, the exponent character and the 410 // terminating null-character when computing the maximal output size. 411 // The given length is only used in debug mode to ensure the buffer is big 412 // enough. 413 // ICU PATCH: Export this as U_I18N_API for unit tests. 414 static void U_I18N_API DoubleToAscii(double v, 415 DtoaMode mode, 416 int requested_digits, 417 char* buffer, 418 int buffer_length, 419 bool* sign, 420 int* length, 421 int* point); 422 423 #if 0 // not needed for ICU 424 private: 425 // Implementation for ToShortest and ToShortestSingle. 426 bool ToShortestIeeeNumber(double value, 427 StringBuilder* result_builder, 428 DtoaMode mode) const; 429 430 // If the value is a special value (NaN or Infinity) constructs the 431 // corresponding string using the configured infinity/nan-symbol. 432 // If either of them is NULL or the value is not special then the 433 // function returns false. 434 bool HandleSpecialValues(double value, StringBuilder* result_builder) const; 435 // Constructs an exponential representation (i.e. 1.234e56). 436 // The given exponent assumes a decimal point after the first decimal digit. 437 void CreateExponentialRepresentation(const char* decimal_digits, 438 int length, 439 int exponent, 440 StringBuilder* result_builder) const; 441 // Creates a decimal representation (i.e 1234.5678). 442 void CreateDecimalRepresentation(const char* decimal_digits, 443 int length, 444 int decimal_point, 445 int digits_after_point, 446 StringBuilder* result_builder) const; 447 448 const int flags_; 449 const char* const infinity_symbol_; 450 const char* const nan_symbol_; 451 const char exponent_character_; 452 const int decimal_in_shortest_low_; 453 const int decimal_in_shortest_high_; 454 const int max_leading_padding_zeroes_in_precision_mode_; 455 const int max_trailing_padding_zeroes_in_precision_mode_; 456 const int min_exponent_width_; 457 #endif // not needed for ICU 458 459 DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter); 460 }; 461 462 } // namespace double_conversion 463 464 // ICU PATCH: Close ICU namespace 465 U_NAMESPACE_END 466 467 #endif // DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_ 468 #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING 469