| 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 2010 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 | // ICU PATCH: Do not include std::locale. | 
|---|
| 38 |  | 
|---|
| 39 | #include <climits> | 
|---|
| 40 | // #include <locale> | 
|---|
| 41 | #include <cmath> | 
|---|
| 42 |  | 
|---|
| 43 | // ICU PATCH: Customize header file paths for ICU. | 
|---|
| 44 |  | 
|---|
| 45 | #include "double-conversion-string-to-double.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include "double-conversion-ieee.h" | 
|---|
| 48 | #include "double-conversion-strtod.h" | 
|---|
| 49 | #include "double-conversion-utils.h" | 
|---|
| 50 |  | 
|---|
| 51 | // ICU PATCH: Wrap in ICU namespace | 
|---|
| 52 | U_NAMESPACE_BEGIN | 
|---|
| 53 |  | 
|---|
| 54 | namespace double_conversion { | 
|---|
| 55 |  | 
|---|
| 56 | namespace { | 
|---|
| 57 |  | 
|---|
| 58 | inline char ToLower(char ch) { | 
|---|
| 59 | #if 0  // do not include std::locale in ICU | 
|---|
| 60 | static const std::ctype<char>& cType = | 
|---|
| 61 | std::use_facet<std::ctype<char> >(std::locale::classic()); | 
|---|
| 62 | return cType.tolower(ch); | 
|---|
| 63 | #else | 
|---|
| 64 | (void)ch; | 
|---|
| 65 | DOUBLE_CONVERSION_UNREACHABLE(); | 
|---|
| 66 | #endif | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | inline char Pass(char ch) { | 
|---|
| 70 | return ch; | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | template <class Iterator, class Converter> | 
|---|
| 74 | static inline bool ConsumeSubStringImpl(Iterator* current, | 
|---|
| 75 | Iterator end, | 
|---|
| 76 | const char* substring, | 
|---|
| 77 | Converter converter) { | 
|---|
| 78 | DOUBLE_CONVERSION_ASSERT(converter(**current) == *substring); | 
|---|
| 79 | for (substring++; *substring != '\0'; substring++) { | 
|---|
| 80 | ++*current; | 
|---|
| 81 | if (*current == end || converter(**current) != *substring) { | 
|---|
| 82 | return false; | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 | ++*current; | 
|---|
| 86 | return true; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | // Consumes the given substring from the iterator. | 
|---|
| 90 | // Returns false, if the substring does not match. | 
|---|
| 91 | template <class Iterator> | 
|---|
| 92 | static bool ConsumeSubString(Iterator* current, | 
|---|
| 93 | Iterator end, | 
|---|
| 94 | const char* substring, | 
|---|
| 95 | bool allow_case_insensitivity) { | 
|---|
| 96 | if (allow_case_insensitivity) { | 
|---|
| 97 | return ConsumeSubStringImpl(current, end, substring, ToLower); | 
|---|
| 98 | } else { | 
|---|
| 99 | return ConsumeSubStringImpl(current, end, substring, Pass); | 
|---|
| 100 | } | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | // Consumes first character of the str is equal to ch | 
|---|
| 104 | inline bool ConsumeFirstCharacter(char ch, | 
|---|
| 105 | const char* str, | 
|---|
| 106 | bool case_insensitivity) { | 
|---|
| 107 | return case_insensitivity ? ToLower(ch) == str[0] : ch == str[0]; | 
|---|
| 108 | } | 
|---|
| 109 | }  // namespace | 
|---|
| 110 |  | 
|---|
| 111 | // Maximum number of significant digits in decimal representation. | 
|---|
| 112 | // The longest possible double in decimal representation is | 
|---|
| 113 | // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074 | 
|---|
| 114 | // (768 digits). If we parse a number whose first digits are equal to a | 
|---|
| 115 | // mean of 2 adjacent doubles (that could have up to 769 digits) the result | 
|---|
| 116 | // must be rounded to the bigger one unless the tail consists of zeros, so | 
|---|
| 117 | // we don't need to preserve all the digits. | 
|---|
| 118 | const int kMaxSignificantDigits = 772; | 
|---|
| 119 |  | 
|---|
| 120 |  | 
|---|
| 121 | static const char kWhitespaceTable7[] = { 32, 13, 10, 9, 11, 12 }; | 
|---|
| 122 | static const int kWhitespaceTable7Length = DOUBLE_CONVERSION_ARRAY_SIZE(kWhitespaceTable7); | 
|---|
| 123 |  | 
|---|
| 124 |  | 
|---|
| 125 | static const uc16 kWhitespaceTable16[] = { | 
|---|
| 126 | 160, 8232, 8233, 5760, 6158, 8192, 8193, 8194, 8195, | 
|---|
| 127 | 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8239, 8287, 12288, 65279 | 
|---|
| 128 | }; | 
|---|
| 129 | static const int kWhitespaceTable16Length = DOUBLE_CONVERSION_ARRAY_SIZE(kWhitespaceTable16); | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 | static bool isWhitespace(int x) { | 
|---|
| 133 | if (x < 128) { | 
|---|
| 134 | for (int i = 0; i < kWhitespaceTable7Length; i++) { | 
|---|
| 135 | if (kWhitespaceTable7[i] == x) return true; | 
|---|
| 136 | } | 
|---|
| 137 | } else { | 
|---|
| 138 | for (int i = 0; i < kWhitespaceTable16Length; i++) { | 
|---|
| 139 | if (kWhitespaceTable16[i] == x) return true; | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 | return false; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 | // Returns true if a nonspace found and false if the end has reached. | 
|---|
| 147 | template <class Iterator> | 
|---|
| 148 | static inline bool AdvanceToNonspace(Iterator* current, Iterator end) { | 
|---|
| 149 | while (*current != end) { | 
|---|
| 150 | if (!isWhitespace(**current)) return true; | 
|---|
| 151 | ++*current; | 
|---|
| 152 | } | 
|---|
| 153 | return false; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|
| 157 | static bool isDigit(int x, int radix) { | 
|---|
| 158 | return (x >= '0' && x <= '9' && x < '0' + radix) | 
|---|
| 159 | || (radix > 10 && x >= 'a' && x < 'a' + radix - 10) | 
|---|
| 160 | || (radix > 10 && x >= 'A' && x < 'A' + radix - 10); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 |  | 
|---|
| 164 | static double SignedZero(bool sign) { | 
|---|
| 165 | return sign ? -0.0 : 0.0; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 | // Returns true if 'c' is a decimal digit that is valid for the given radix. | 
|---|
| 170 | // | 
|---|
| 171 | // The function is small and could be inlined, but VS2012 emitted a warning | 
|---|
| 172 | // because it constant-propagated the radix and concluded that the last | 
|---|
| 173 | // condition was always true. By moving it into a separate function the | 
|---|
| 174 | // compiler wouldn't warn anymore. | 
|---|
| 175 | #ifdef _MSC_VER | 
|---|
| 176 | #pragma optimize("",off) | 
|---|
| 177 | static bool IsDecimalDigitForRadix(int c, int radix) { | 
|---|
| 178 | return '0' <= c && c <= '9' && (c - '0') < radix; | 
|---|
| 179 | } | 
|---|
| 180 | #pragma optimize("",on) | 
|---|
| 181 | #else | 
|---|
| 182 | static bool inline IsDecimalDigitForRadix(int c, int radix) { | 
|---|
| 183 | return '0' <= c && c <= '9' && (c - '0') < radix; | 
|---|
| 184 | } | 
|---|
| 185 | #endif | 
|---|
| 186 | // Returns true if 'c' is a character digit that is valid for the given radix. | 
|---|
| 187 | // The 'a_character' should be 'a' or 'A'. | 
|---|
| 188 | // | 
|---|
| 189 | // The function is small and could be inlined, but VS2012 emitted a warning | 
|---|
| 190 | // because it constant-propagated the radix and concluded that the first | 
|---|
| 191 | // condition was always false. By moving it into a separate function the | 
|---|
| 192 | // compiler wouldn't warn anymore. | 
|---|
| 193 | static bool IsCharacterDigitForRadix(int c, int radix, char a_character) { | 
|---|
| 194 | return radix > 10 && c >= a_character && c < a_character + radix - 10; | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | // Returns true, when the iterator is equal to end. | 
|---|
| 198 | template<class Iterator> | 
|---|
| 199 | static bool Advance (Iterator* it, uc16 separator, int base, Iterator& end) { | 
|---|
| 200 | if (separator == StringToDoubleConverter::kNoSeparator) { | 
|---|
| 201 | ++(*it); | 
|---|
| 202 | return *it == end; | 
|---|
| 203 | } | 
|---|
| 204 | if (!isDigit(**it, base)) { | 
|---|
| 205 | ++(*it); | 
|---|
| 206 | return *it == end; | 
|---|
| 207 | } | 
|---|
| 208 | ++(*it); | 
|---|
| 209 | if (*it == end) return true; | 
|---|
| 210 | if (*it + 1 == end) return false; | 
|---|
| 211 | if (**it == separator && isDigit(*(*it + 1), base)) { | 
|---|
| 212 | ++(*it); | 
|---|
| 213 | } | 
|---|
| 214 | return *it == end; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | // Checks whether the string in the range start-end is a hex-float string. | 
|---|
| 218 | // This function assumes that the leading '0x'/'0X' is already consumed. | 
|---|
| 219 | // | 
|---|
| 220 | // Hex float strings are of one of the following forms: | 
|---|
| 221 | //   - hex_digits+ 'p' ('+'|'-')? exponent_digits+ | 
|---|
| 222 | //   - hex_digits* '.' hex_digits+ 'p' ('+'|'-')? exponent_digits+ | 
|---|
| 223 | //   - hex_digits+ '.' 'p' ('+'|'-')? exponent_digits+ | 
|---|
| 224 | template<class Iterator> | 
|---|
| 225 | static bool IsHexFloatString(Iterator start, | 
|---|
| 226 | Iterator end, | 
|---|
| 227 | uc16 separator, | 
|---|
| 228 | bool allow_trailing_junk) { | 
|---|
| 229 | DOUBLE_CONVERSION_ASSERT(start != end); | 
|---|
| 230 |  | 
|---|
| 231 | Iterator current = start; | 
|---|
| 232 |  | 
|---|
| 233 | bool saw_digit = false; | 
|---|
| 234 | while (isDigit(*current, 16)) { | 
|---|
| 235 | saw_digit = true; | 
|---|
| 236 | if (Advance(¤t, separator, 16, end)) return false; | 
|---|
| 237 | } | 
|---|
| 238 | if (*current == '.') { | 
|---|
| 239 | if (Advance(¤t, separator, 16, end)) return false; | 
|---|
| 240 | while (isDigit(*current, 16)) { | 
|---|
| 241 | saw_digit = true; | 
|---|
| 242 | if (Advance(¤t, separator, 16, end)) return false; | 
|---|
| 243 | } | 
|---|
| 244 | } | 
|---|
| 245 | if (!saw_digit) return false; | 
|---|
| 246 | if (*current != 'p' && *current != 'P') return false; | 
|---|
| 247 | if (Advance(¤t, separator, 16, end)) return false; | 
|---|
| 248 | if (*current == '+' || *current == '-') { | 
|---|
| 249 | if (Advance(¤t, separator, 16, end)) return false; | 
|---|
| 250 | } | 
|---|
| 251 | if (!isDigit(*current, 10)) return false; | 
|---|
| 252 | if (Advance(¤t, separator, 16, end)) return true; | 
|---|
| 253 | while (isDigit(*current, 10)) { | 
|---|
| 254 | if (Advance(¤t, separator, 16, end)) return true; | 
|---|
| 255 | } | 
|---|
| 256 | return allow_trailing_junk || !AdvanceToNonspace(¤t, end); | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 |  | 
|---|
| 260 | // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end. | 
|---|
| 261 | // | 
|---|
| 262 | // If parse_as_hex_float is true, then the string must be a valid | 
|---|
| 263 | // hex-float. | 
|---|
| 264 | template <int radix_log_2, class Iterator> | 
|---|
| 265 | static double RadixStringToIeee(Iterator* current, | 
|---|
| 266 | Iterator end, | 
|---|
| 267 | bool sign, | 
|---|
| 268 | uc16 separator, | 
|---|
| 269 | bool parse_as_hex_float, | 
|---|
| 270 | bool allow_trailing_junk, | 
|---|
| 271 | double junk_string_value, | 
|---|
| 272 | bool read_as_double, | 
|---|
| 273 | bool* result_is_junk) { | 
|---|
| 274 | DOUBLE_CONVERSION_ASSERT(*current != end); | 
|---|
| 275 | DOUBLE_CONVERSION_ASSERT(!parse_as_hex_float || | 
|---|
| 276 | IsHexFloatString(*current, end, separator, allow_trailing_junk)); | 
|---|
| 277 |  | 
|---|
| 278 | const int kDoubleSize = Double::kSignificandSize; | 
|---|
| 279 | const int kSingleSize = Single::kSignificandSize; | 
|---|
| 280 | const int kSignificandSize = read_as_double? kDoubleSize: kSingleSize; | 
|---|
| 281 |  | 
|---|
| 282 | *result_is_junk = true; | 
|---|
| 283 |  | 
|---|
| 284 | int64_t number = 0; | 
|---|
| 285 | int exponent = 0; | 
|---|
| 286 | const int radix = (1 << radix_log_2); | 
|---|
| 287 | // Whether we have encountered a '.' and are parsing the decimal digits. | 
|---|
| 288 | // Only relevant if parse_as_hex_float is true. | 
|---|
| 289 | bool post_decimal = false; | 
|---|
| 290 |  | 
|---|
| 291 | // Skip leading 0s. | 
|---|
| 292 | while (**current == '0') { | 
|---|
| 293 | if (Advance(current, separator, radix, end)) { | 
|---|
| 294 | *result_is_junk = false; | 
|---|
| 295 | return SignedZero(sign); | 
|---|
| 296 | } | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 | while (true) { | 
|---|
| 300 | int digit; | 
|---|
| 301 | if (IsDecimalDigitForRadix(**current, radix)) { | 
|---|
| 302 | digit = static_cast<char>(**current) - '0'; | 
|---|
| 303 | if (post_decimal) exponent -= radix_log_2; | 
|---|
| 304 | } else if (IsCharacterDigitForRadix(**current, radix, 'a')) { | 
|---|
| 305 | digit = static_cast<char>(**current) - 'a' + 10; | 
|---|
| 306 | if (post_decimal) exponent -= radix_log_2; | 
|---|
| 307 | } else if (IsCharacterDigitForRadix(**current, radix, 'A')) { | 
|---|
| 308 | digit = static_cast<char>(**current) - 'A' + 10; | 
|---|
| 309 | if (post_decimal) exponent -= radix_log_2; | 
|---|
| 310 | } else if (parse_as_hex_float && **current == '.') { | 
|---|
| 311 | post_decimal = true; | 
|---|
| 312 | Advance(current, separator, radix, end); | 
|---|
| 313 | DOUBLE_CONVERSION_ASSERT(*current != end); | 
|---|
| 314 | continue; | 
|---|
| 315 | } else if (parse_as_hex_float && (**current == 'p' || **current == 'P')) { | 
|---|
| 316 | break; | 
|---|
| 317 | } else { | 
|---|
| 318 | if (allow_trailing_junk || !AdvanceToNonspace(current, end)) { | 
|---|
| 319 | break; | 
|---|
| 320 | } else { | 
|---|
| 321 | return junk_string_value; | 
|---|
| 322 | } | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | number = number * radix + digit; | 
|---|
| 326 | int overflow = static_cast<int>(number >> kSignificandSize); | 
|---|
| 327 | if (overflow != 0) { | 
|---|
| 328 | // Overflow occurred. Need to determine which direction to round the | 
|---|
| 329 | // result. | 
|---|
| 330 | int overflow_bits_count = 1; | 
|---|
| 331 | while (overflow > 1) { | 
|---|
| 332 | overflow_bits_count++; | 
|---|
| 333 | overflow >>= 1; | 
|---|
| 334 | } | 
|---|
| 335 |  | 
|---|
| 336 | int dropped_bits_mask = ((1 << overflow_bits_count) - 1); | 
|---|
| 337 | int dropped_bits = static_cast<int>(number) & dropped_bits_mask; | 
|---|
| 338 | number >>= overflow_bits_count; | 
|---|
| 339 | exponent += overflow_bits_count; | 
|---|
| 340 |  | 
|---|
| 341 | bool zero_tail = true; | 
|---|
| 342 | for (;;) { | 
|---|
| 343 | if (Advance(current, separator, radix, end)) break; | 
|---|
| 344 | if (parse_as_hex_float && **current == '.') { | 
|---|
| 345 | // Just run over the '.'. We are just trying to see whether there is | 
|---|
| 346 | // a non-zero digit somewhere. | 
|---|
| 347 | Advance(current, separator, radix, end); | 
|---|
| 348 | DOUBLE_CONVERSION_ASSERT(*current != end); | 
|---|
| 349 | post_decimal = true; | 
|---|
| 350 | } | 
|---|
| 351 | if (!isDigit(**current, radix)) break; | 
|---|
| 352 | zero_tail = zero_tail && **current == '0'; | 
|---|
| 353 | if (!post_decimal) exponent += radix_log_2; | 
|---|
| 354 | } | 
|---|
| 355 |  | 
|---|
| 356 | if (!parse_as_hex_float && | 
|---|
| 357 | !allow_trailing_junk && | 
|---|
| 358 | AdvanceToNonspace(current, end)) { | 
|---|
| 359 | return junk_string_value; | 
|---|
| 360 | } | 
|---|
| 361 |  | 
|---|
| 362 | int middle_value = (1 << (overflow_bits_count - 1)); | 
|---|
| 363 | if (dropped_bits > middle_value) { | 
|---|
| 364 | number++;  // Rounding up. | 
|---|
| 365 | } else if (dropped_bits == middle_value) { | 
|---|
| 366 | // Rounding to even to consistency with decimals: half-way case rounds | 
|---|
| 367 | // up if significant part is odd and down otherwise. | 
|---|
| 368 | if ((number & 1) != 0 || !zero_tail) { | 
|---|
| 369 | number++;  // Rounding up. | 
|---|
| 370 | } | 
|---|
| 371 | } | 
|---|
| 372 |  | 
|---|
| 373 | // Rounding up may cause overflow. | 
|---|
| 374 | if ((number & ((int64_t)1 << kSignificandSize)) != 0) { | 
|---|
| 375 | exponent++; | 
|---|
| 376 | number >>= 1; | 
|---|
| 377 | } | 
|---|
| 378 | break; | 
|---|
| 379 | } | 
|---|
| 380 | if (Advance(current, separator, radix, end)) break; | 
|---|
| 381 | } | 
|---|
| 382 |  | 
|---|
| 383 | DOUBLE_CONVERSION_ASSERT(number < ((int64_t)1 << kSignificandSize)); | 
|---|
| 384 | DOUBLE_CONVERSION_ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number); | 
|---|
| 385 |  | 
|---|
| 386 | *result_is_junk = false; | 
|---|
| 387 |  | 
|---|
| 388 | if (parse_as_hex_float) { | 
|---|
| 389 | DOUBLE_CONVERSION_ASSERT(**current == 'p' || **current == 'P'); | 
|---|
| 390 | Advance(current, separator, radix, end); | 
|---|
| 391 | DOUBLE_CONVERSION_ASSERT(*current != end); | 
|---|
| 392 | bool is_negative = false; | 
|---|
| 393 | if (**current == '+') { | 
|---|
| 394 | Advance(current, separator, radix, end); | 
|---|
| 395 | DOUBLE_CONVERSION_ASSERT(*current != end); | 
|---|
| 396 | } else if (**current == '-') { | 
|---|
| 397 | is_negative = true; | 
|---|
| 398 | Advance(current, separator, radix, end); | 
|---|
| 399 | DOUBLE_CONVERSION_ASSERT(*current != end); | 
|---|
| 400 | } | 
|---|
| 401 | int written_exponent = 0; | 
|---|
| 402 | while (IsDecimalDigitForRadix(**current, 10)) { | 
|---|
| 403 | // No need to read exponents if they are too big. That could potentially overflow | 
|---|
| 404 | // the `written_exponent` variable. | 
|---|
| 405 | if (abs(written_exponent) <= 100 * Double::kMaxExponent) { | 
|---|
| 406 | written_exponent = 10 * written_exponent + **current - '0'; | 
|---|
| 407 | } | 
|---|
| 408 | if (Advance(current, separator, radix, end)) break; | 
|---|
| 409 | } | 
|---|
| 410 | if (is_negative) written_exponent = -written_exponent; | 
|---|
| 411 | exponent += written_exponent; | 
|---|
| 412 | } | 
|---|
| 413 |  | 
|---|
| 414 | if (exponent == 0 || number == 0) { | 
|---|
| 415 | if (sign) { | 
|---|
| 416 | if (number == 0) return -0.0; | 
|---|
| 417 | number = -number; | 
|---|
| 418 | } | 
|---|
| 419 | return static_cast<double>(number); | 
|---|
| 420 | } | 
|---|
| 421 |  | 
|---|
| 422 | DOUBLE_CONVERSION_ASSERT(number != 0); | 
|---|
| 423 | double result = Double(DiyFp(number, exponent)).value(); | 
|---|
| 424 | return sign ? -result : result; | 
|---|
| 425 | } | 
|---|
| 426 |  | 
|---|
| 427 | template <class Iterator> | 
|---|
| 428 | double StringToDoubleConverter::StringToIeee( | 
|---|
| 429 | Iterator input, | 
|---|
| 430 | int length, | 
|---|
| 431 | bool read_as_double, | 
|---|
| 432 | int* processed_characters_count) const { | 
|---|
| 433 | Iterator current = input; | 
|---|
| 434 | Iterator end = input + length; | 
|---|
| 435 |  | 
|---|
| 436 | *processed_characters_count = 0; | 
|---|
| 437 |  | 
|---|
| 438 | const bool allow_trailing_junk = (flags_ & ALLOW_TRAILING_JUNK) != 0; | 
|---|
| 439 | const bool allow_leading_spaces = (flags_ & ALLOW_LEADING_SPACES) != 0; | 
|---|
| 440 | const bool allow_trailing_spaces = (flags_ & ALLOW_TRAILING_SPACES) != 0; | 
|---|
| 441 | const bool allow_spaces_after_sign = (flags_ & ALLOW_SPACES_AFTER_SIGN) != 0; | 
|---|
| 442 | const bool allow_case_insensitivity = (flags_ & ALLOW_CASE_INSENSITIVITY) != 0; | 
|---|
| 443 |  | 
|---|
| 444 | // To make sure that iterator dereferencing is valid the following | 
|---|
| 445 | // convention is used: | 
|---|
| 446 | // 1. Each '++current' statement is followed by check for equality to 'end'. | 
|---|
| 447 | // 2. If AdvanceToNonspace returned false then current == end. | 
|---|
| 448 | // 3. If 'current' becomes equal to 'end' the function returns or goes to | 
|---|
| 449 | // 'parsing_done'. | 
|---|
| 450 | // 4. 'current' is not dereferenced after the 'parsing_done' label. | 
|---|
| 451 | // 5. Code before 'parsing_done' may rely on 'current != end'. | 
|---|
| 452 | if (current == end) return empty_string_value_; | 
|---|
| 453 |  | 
|---|
| 454 | if (allow_leading_spaces || allow_trailing_spaces) { | 
|---|
| 455 | if (!AdvanceToNonspace(¤t, end)) { | 
|---|
| 456 | *processed_characters_count = static_cast<int>(current - input); | 
|---|
| 457 | return empty_string_value_; | 
|---|
| 458 | } | 
|---|
| 459 | if (!allow_leading_spaces && (input != current)) { | 
|---|
| 460 | // No leading spaces allowed, but AdvanceToNonspace moved forward. | 
|---|
| 461 | return junk_string_value_; | 
|---|
| 462 | } | 
|---|
| 463 | } | 
|---|
| 464 |  | 
|---|
| 465 | // Exponent will be adjusted if insignificant digits of the integer part | 
|---|
| 466 | // or insignificant leading zeros of the fractional part are dropped. | 
|---|
| 467 | int exponent = 0; | 
|---|
| 468 | int significant_digits = 0; | 
|---|
| 469 | int insignificant_digits = 0; | 
|---|
| 470 | bool nonzero_digit_dropped = false; | 
|---|
| 471 |  | 
|---|
| 472 | bool sign = false; | 
|---|
| 473 |  | 
|---|
| 474 | if (*current == '+' || *current == '-') { | 
|---|
| 475 | sign = (*current == '-'); | 
|---|
| 476 | ++current; | 
|---|
| 477 | Iterator next_non_space = current; | 
|---|
| 478 | // Skip following spaces (if allowed). | 
|---|
| 479 | if (!AdvanceToNonspace(&next_non_space, end)) return junk_string_value_; | 
|---|
| 480 | if (!allow_spaces_after_sign && (current != next_non_space)) { | 
|---|
| 481 | return junk_string_value_; | 
|---|
| 482 | } | 
|---|
| 483 | current = next_non_space; | 
|---|
| 484 | } | 
|---|
| 485 |  | 
|---|
| 486 | if (infinity_symbol_ != NULL) { | 
|---|
| 487 | if (ConsumeFirstCharacter(*current, infinity_symbol_, allow_case_insensitivity)) { | 
|---|
| 488 | if (!ConsumeSubString(¤t, end, infinity_symbol_, allow_case_insensitivity)) { | 
|---|
| 489 | return junk_string_value_; | 
|---|
| 490 | } | 
|---|
| 491 |  | 
|---|
| 492 | if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { | 
|---|
| 493 | return junk_string_value_; | 
|---|
| 494 | } | 
|---|
| 495 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 
|---|
| 496 | return junk_string_value_; | 
|---|
| 497 | } | 
|---|
| 498 |  | 
|---|
| 499 | *processed_characters_count = static_cast<int>(current - input); | 
|---|
| 500 | return sign ? -Double::Infinity() : Double::Infinity(); | 
|---|
| 501 | } | 
|---|
| 502 | } | 
|---|
| 503 |  | 
|---|
| 504 | if (nan_symbol_ != NULL) { | 
|---|
| 505 | if (ConsumeFirstCharacter(*current, nan_symbol_, allow_case_insensitivity)) { | 
|---|
| 506 | if (!ConsumeSubString(¤t, end, nan_symbol_, allow_case_insensitivity)) { | 
|---|
| 507 | return junk_string_value_; | 
|---|
| 508 | } | 
|---|
| 509 |  | 
|---|
| 510 | if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { | 
|---|
| 511 | return junk_string_value_; | 
|---|
| 512 | } | 
|---|
| 513 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 
|---|
| 514 | return junk_string_value_; | 
|---|
| 515 | } | 
|---|
| 516 |  | 
|---|
| 517 | *processed_characters_count = static_cast<int>(current - input); | 
|---|
| 518 | return sign ? -Double::NaN() : Double::NaN(); | 
|---|
| 519 | } | 
|---|
| 520 | } | 
|---|
| 521 |  | 
|---|
| 522 | bool leading_zero = false; | 
|---|
| 523 | if (*current == '0') { | 
|---|
| 524 | if (Advance(¤t, separator_, 10, end)) { | 
|---|
| 525 | *processed_characters_count = static_cast<int>(current - input); | 
|---|
| 526 | return SignedZero(sign); | 
|---|
| 527 | } | 
|---|
| 528 |  | 
|---|
| 529 | leading_zero = true; | 
|---|
| 530 |  | 
|---|
| 531 | // It could be hexadecimal value. | 
|---|
| 532 | if (((flags_ & ALLOW_HEX) || (flags_ & ALLOW_HEX_FLOATS)) && | 
|---|
| 533 | (*current == 'x' || *current == 'X')) { | 
|---|
| 534 | ++current; | 
|---|
| 535 |  | 
|---|
| 536 | if (current == end) return junk_string_value_;  // "0x" | 
|---|
| 537 |  | 
|---|
| 538 | bool parse_as_hex_float = (flags_ & ALLOW_HEX_FLOATS) && | 
|---|
| 539 | IsHexFloatString(current, end, separator_, allow_trailing_junk); | 
|---|
| 540 |  | 
|---|
| 541 | if (!parse_as_hex_float && !isDigit(*current, 16)) { | 
|---|
| 542 | return junk_string_value_; | 
|---|
| 543 | } | 
|---|
| 544 |  | 
|---|
| 545 | bool result_is_junk; | 
|---|
| 546 | double result = RadixStringToIeee<4>(¤t, | 
|---|
| 547 | end, | 
|---|
| 548 | sign, | 
|---|
| 549 | separator_, | 
|---|
| 550 | parse_as_hex_float, | 
|---|
| 551 | allow_trailing_junk, | 
|---|
| 552 | junk_string_value_, | 
|---|
| 553 | read_as_double, | 
|---|
| 554 | &result_is_junk); | 
|---|
| 555 | if (!result_is_junk) { | 
|---|
| 556 | if (allow_trailing_spaces) AdvanceToNonspace(¤t, end); | 
|---|
| 557 | *processed_characters_count = static_cast<int>(current - input); | 
|---|
| 558 | } | 
|---|
| 559 | return result; | 
|---|
| 560 | } | 
|---|
| 561 |  | 
|---|
| 562 | // Ignore leading zeros in the integer part. | 
|---|
| 563 | while (*current == '0') { | 
|---|
| 564 | if (Advance(¤t, separator_, 10, end)) { | 
|---|
| 565 | *processed_characters_count = static_cast<int>(current - input); | 
|---|
| 566 | return SignedZero(sign); | 
|---|
| 567 | } | 
|---|
| 568 | } | 
|---|
| 569 | } | 
|---|
| 570 |  | 
|---|
| 571 | bool octal = leading_zero && (flags_ & ALLOW_OCTALS) != 0; | 
|---|
| 572 |  | 
|---|
| 573 | // The longest form of simplified number is: "-<significant digits>.1eXXX\0". | 
|---|
| 574 | const int kBufferSize = kMaxSignificantDigits + 10; | 
|---|
| 575 | DOUBLE_CONVERSION_STACK_UNINITIALIZED char | 
|---|
| 576 | buffer[kBufferSize];  // NOLINT: size is known at compile time. | 
|---|
| 577 | int buffer_pos = 0; | 
|---|
| 578 |  | 
|---|
| 579 | // Copy significant digits of the integer part (if any) to the buffer. | 
|---|
| 580 | while (*current >= '0' && *current <= '9') { | 
|---|
| 581 | if (significant_digits < kMaxSignificantDigits) { | 
|---|
| 582 | DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize); | 
|---|
| 583 | buffer[buffer_pos++] = static_cast<char>(*current); | 
|---|
| 584 | significant_digits++; | 
|---|
| 585 | // Will later check if it's an octal in the buffer. | 
|---|
| 586 | } else { | 
|---|
| 587 | insignificant_digits++;  // Move the digit into the exponential part. | 
|---|
| 588 | nonzero_digit_dropped = nonzero_digit_dropped || *current != '0'; | 
|---|
| 589 | } | 
|---|
| 590 | octal = octal && *current < '8'; | 
|---|
| 591 | if (Advance(¤t, separator_, 10, end)) goto parsing_done; | 
|---|
| 592 | } | 
|---|
| 593 |  | 
|---|
| 594 | if (significant_digits == 0) { | 
|---|
| 595 | octal = false; | 
|---|
| 596 | } | 
|---|
| 597 |  | 
|---|
| 598 | if (*current == '.') { | 
|---|
| 599 | if (octal && !allow_trailing_junk) return junk_string_value_; | 
|---|
| 600 | if (octal) goto parsing_done; | 
|---|
| 601 |  | 
|---|
| 602 | if (Advance(¤t, separator_, 10, end)) { | 
|---|
| 603 | if (significant_digits == 0 && !leading_zero) { | 
|---|
| 604 | return junk_string_value_; | 
|---|
| 605 | } else { | 
|---|
| 606 | goto parsing_done; | 
|---|
| 607 | } | 
|---|
| 608 | } | 
|---|
| 609 |  | 
|---|
| 610 | if (significant_digits == 0) { | 
|---|
| 611 | // octal = false; | 
|---|
| 612 | // Integer part consists of 0 or is absent. Significant digits start after | 
|---|
| 613 | // leading zeros (if any). | 
|---|
| 614 | while (*current == '0') { | 
|---|
| 615 | if (Advance(¤t, separator_, 10, end)) { | 
|---|
| 616 | *processed_characters_count = static_cast<int>(current - input); | 
|---|
| 617 | return SignedZero(sign); | 
|---|
| 618 | } | 
|---|
| 619 | exponent--;  // Move this 0 into the exponent. | 
|---|
| 620 | } | 
|---|
| 621 | } | 
|---|
| 622 |  | 
|---|
| 623 | // There is a fractional part. | 
|---|
| 624 | // We don't emit a '.', but adjust the exponent instead. | 
|---|
| 625 | while (*current >= '0' && *current <= '9') { | 
|---|
| 626 | if (significant_digits < kMaxSignificantDigits) { | 
|---|
| 627 | DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize); | 
|---|
| 628 | buffer[buffer_pos++] = static_cast<char>(*current); | 
|---|
| 629 | significant_digits++; | 
|---|
| 630 | exponent--; | 
|---|
| 631 | } else { | 
|---|
| 632 | // Ignore insignificant digits in the fractional part. | 
|---|
| 633 | nonzero_digit_dropped = nonzero_digit_dropped || *current != '0'; | 
|---|
| 634 | } | 
|---|
| 635 | if (Advance(¤t, separator_, 10, end)) goto parsing_done; | 
|---|
| 636 | } | 
|---|
| 637 | } | 
|---|
| 638 |  | 
|---|
| 639 | if (!leading_zero && exponent == 0 && significant_digits == 0) { | 
|---|
| 640 | // If leading_zeros is true then the string contains zeros. | 
|---|
| 641 | // If exponent < 0 then string was [+-]\.0*... | 
|---|
| 642 | // If significant_digits != 0 the string is not equal to 0. | 
|---|
| 643 | // Otherwise there are no digits in the string. | 
|---|
| 644 | return junk_string_value_; | 
|---|
| 645 | } | 
|---|
| 646 |  | 
|---|
| 647 | // Parse exponential part. | 
|---|
| 648 | if (*current == 'e' || *current == 'E') { | 
|---|
| 649 | if (octal && !allow_trailing_junk) return junk_string_value_; | 
|---|
| 650 | if (octal) goto parsing_done; | 
|---|
| 651 | Iterator junk_begin = current; | 
|---|
| 652 | ++current; | 
|---|
| 653 | if (current == end) { | 
|---|
| 654 | if (allow_trailing_junk) { | 
|---|
| 655 | current = junk_begin; | 
|---|
| 656 | goto parsing_done; | 
|---|
| 657 | } else { | 
|---|
| 658 | return junk_string_value_; | 
|---|
| 659 | } | 
|---|
| 660 | } | 
|---|
| 661 | char exponen_sign = '+'; | 
|---|
| 662 | if (*current == '+' || *current == '-') { | 
|---|
| 663 | exponen_sign = static_cast<char>(*current); | 
|---|
| 664 | ++current; | 
|---|
| 665 | if (current == end) { | 
|---|
| 666 | if (allow_trailing_junk) { | 
|---|
| 667 | current = junk_begin; | 
|---|
| 668 | goto parsing_done; | 
|---|
| 669 | } else { | 
|---|
| 670 | return junk_string_value_; | 
|---|
| 671 | } | 
|---|
| 672 | } | 
|---|
| 673 | } | 
|---|
| 674 |  | 
|---|
| 675 | if (current == end || *current < '0' || *current > '9') { | 
|---|
| 676 | if (allow_trailing_junk) { | 
|---|
| 677 | current = junk_begin; | 
|---|
| 678 | goto parsing_done; | 
|---|
| 679 | } else { | 
|---|
| 680 | return junk_string_value_; | 
|---|
| 681 | } | 
|---|
| 682 | } | 
|---|
| 683 |  | 
|---|
| 684 | const int max_exponent = INT_MAX / 2; | 
|---|
| 685 | DOUBLE_CONVERSION_ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2); | 
|---|
| 686 | int num = 0; | 
|---|
| 687 | do { | 
|---|
| 688 | // Check overflow. | 
|---|
| 689 | int digit = *current - '0'; | 
|---|
| 690 | if (num >= max_exponent / 10 | 
|---|
| 691 | && !(num == max_exponent / 10 && digit <= max_exponent % 10)) { | 
|---|
| 692 | num = max_exponent; | 
|---|
| 693 | } else { | 
|---|
| 694 | num = num * 10 + digit; | 
|---|
| 695 | } | 
|---|
| 696 | ++current; | 
|---|
| 697 | } while (current != end && *current >= '0' && *current <= '9'); | 
|---|
| 698 |  | 
|---|
| 699 | exponent += (exponen_sign == '-' ? -num : num); | 
|---|
| 700 | } | 
|---|
| 701 |  | 
|---|
| 702 | if (!(allow_trailing_spaces || allow_trailing_junk) && (current != end)) { | 
|---|
| 703 | return junk_string_value_; | 
|---|
| 704 | } | 
|---|
| 705 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 
|---|
| 706 | return junk_string_value_; | 
|---|
| 707 | } | 
|---|
| 708 | if (allow_trailing_spaces) { | 
|---|
| 709 | AdvanceToNonspace(¤t, end); | 
|---|
| 710 | } | 
|---|
| 711 |  | 
|---|
| 712 | parsing_done: | 
|---|
| 713 | exponent += insignificant_digits; | 
|---|
| 714 |  | 
|---|
| 715 | if (octal) { | 
|---|
| 716 | double result; | 
|---|
| 717 | bool result_is_junk; | 
|---|
| 718 | char* start = buffer; | 
|---|
| 719 | result = RadixStringToIeee<3>(&start, | 
|---|
| 720 | buffer + buffer_pos, | 
|---|
| 721 | sign, | 
|---|
| 722 | separator_, | 
|---|
| 723 | false, // Don't parse as hex_float. | 
|---|
| 724 | allow_trailing_junk, | 
|---|
| 725 | junk_string_value_, | 
|---|
| 726 | read_as_double, | 
|---|
| 727 | &result_is_junk); | 
|---|
| 728 | DOUBLE_CONVERSION_ASSERT(!result_is_junk); | 
|---|
| 729 | *processed_characters_count = static_cast<int>(current - input); | 
|---|
| 730 | return result; | 
|---|
| 731 | } | 
|---|
| 732 |  | 
|---|
| 733 | if (nonzero_digit_dropped) { | 
|---|
| 734 | buffer[buffer_pos++] = '1'; | 
|---|
| 735 | exponent--; | 
|---|
| 736 | } | 
|---|
| 737 |  | 
|---|
| 738 | DOUBLE_CONVERSION_ASSERT(buffer_pos < kBufferSize); | 
|---|
| 739 | buffer[buffer_pos] = '\0'; | 
|---|
| 740 |  | 
|---|
| 741 | double converted; | 
|---|
| 742 | if (read_as_double) { | 
|---|
| 743 | converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); | 
|---|
| 744 | } else { | 
|---|
| 745 | converted = Strtof(Vector<const char>(buffer, buffer_pos), exponent); | 
|---|
| 746 | } | 
|---|
| 747 | *processed_characters_count = static_cast<int>(current - input); | 
|---|
| 748 | return sign? -converted: converted; | 
|---|
| 749 | } | 
|---|
| 750 |  | 
|---|
| 751 |  | 
|---|
| 752 | double StringToDoubleConverter::StringToDouble( | 
|---|
| 753 | const char* buffer, | 
|---|
| 754 | int length, | 
|---|
| 755 | int* processed_characters_count) const { | 
|---|
| 756 | return StringToIeee(buffer, length, true, processed_characters_count); | 
|---|
| 757 | } | 
|---|
| 758 |  | 
|---|
| 759 |  | 
|---|
| 760 | double StringToDoubleConverter::StringToDouble( | 
|---|
| 761 | const uc16* buffer, | 
|---|
| 762 | int length, | 
|---|
| 763 | int* processed_characters_count) const { | 
|---|
| 764 | return StringToIeee(buffer, length, true, processed_characters_count); | 
|---|
| 765 | } | 
|---|
| 766 |  | 
|---|
| 767 |  | 
|---|
| 768 | float StringToDoubleConverter::StringToFloat( | 
|---|
| 769 | const char* buffer, | 
|---|
| 770 | int length, | 
|---|
| 771 | int* processed_characters_count) const { | 
|---|
| 772 | return static_cast<float>(StringToIeee(buffer, length, false, | 
|---|
| 773 | processed_characters_count)); | 
|---|
| 774 | } | 
|---|
| 775 |  | 
|---|
| 776 |  | 
|---|
| 777 | float StringToDoubleConverter::StringToFloat( | 
|---|
| 778 | const uc16* buffer, | 
|---|
| 779 | int length, | 
|---|
| 780 | int* processed_characters_count) const { | 
|---|
| 781 | return static_cast<float>(StringToIeee(buffer, length, false, | 
|---|
| 782 | processed_characters_count)); | 
|---|
| 783 | } | 
|---|
| 784 |  | 
|---|
| 785 | }  // namespace double_conversion | 
|---|
| 786 |  | 
|---|
| 787 | // ICU PATCH: Close ICU namespace | 
|---|
| 788 | U_NAMESPACE_END | 
|---|
| 789 | #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING | 
|---|
| 790 |  | 
|---|