| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 // must be rounded to the bigger one unless the tail consists of zeros, so | 102 // must be rounded to the bigger one unless the tail consists of zeros, so |
| 103 // we don't need to preserve all the digits. | 103 // we don't need to preserve all the digits. |
| 104 const int kMaxSignificantDigits = 772; | 104 const int kMaxSignificantDigits = 772; |
| 105 | 105 |
| 106 | 106 |
| 107 static const double JUNK_STRING_VALUE = OS::nan_value(); | 107 static const double JUNK_STRING_VALUE = OS::nan_value(); |
| 108 | 108 |
| 109 | 109 |
| 110 // Returns true if a nonspace found and false if the end has reached. | 110 // Returns true if a nonspace found and false if the end has reached. |
| 111 template <class Iterator, class EndMark> | 111 template <class Iterator, class EndMark> |
| 112 static inline bool AdvanceToNonspace(Iterator* current, EndMark end) { | 112 static inline bool AdvanceToNonspace(ScannerConstants* scanner_constants, |
| 113 Iterator* current, |
| 114 EndMark end) { |
| 113 while (*current != end) { | 115 while (*current != end) { |
| 114 if (!ScannerConstants::kIsWhiteSpace.get(**current)) return true; | 116 if (!scanner_constants->IsWhiteSpace(**current)) return true; |
| 115 ++*current; | 117 ++*current; |
| 116 } | 118 } |
| 117 return false; | 119 return false; |
| 118 } | 120 } |
| 119 | 121 |
| 120 | 122 |
| 121 static bool isDigit(int x, int radix) { | 123 static bool isDigit(int x, int radix) { |
| 122 return (x >= '0' && x <= '9' && x < '0' + radix) | 124 return (x >= '0' && x <= '9' && x < '0' + radix) |
| 123 || (radix > 10 && x >= 'a' && x < 'a' + radix - 10) | 125 || (radix > 10 && x >= 'a' && x < 'a' + radix - 10) |
| 124 || (radix > 10 && x >= 'A' && x < 'A' + radix - 10); | 126 || (radix > 10 && x >= 'A' && x < 'A' + radix - 10); |
| 125 } | 127 } |
| 126 | 128 |
| 127 | 129 |
| 128 static double SignedZero(bool negative) { | 130 static double SignedZero(bool negative) { |
| 129 return negative ? -0.0 : 0.0; | 131 return negative ? -0.0 : 0.0; |
| 130 } | 132 } |
| 131 | 133 |
| 132 | 134 |
| 133 // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end. | 135 // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end. |
| 134 template <int radix_log_2, class Iterator, class EndMark> | 136 template <int radix_log_2, class Iterator, class EndMark> |
| 135 static double InternalStringToIntDouble(Iterator current, | 137 static double InternalStringToIntDouble(ScannerConstants* scanner_constants, |
| 138 Iterator current, |
| 136 EndMark end, | 139 EndMark end, |
| 137 bool negative, | 140 bool negative, |
| 138 bool allow_trailing_junk) { | 141 bool allow_trailing_junk) { |
| 139 ASSERT(current != end); | 142 ASSERT(current != end); |
| 140 | 143 |
| 141 // Skip leading 0s. | 144 // Skip leading 0s. |
| 142 while (*current == '0') { | 145 while (*current == '0') { |
| 143 ++current; | 146 ++current; |
| 144 if (current == end) return SignedZero(negative); | 147 if (current == end) return SignedZero(negative); |
| 145 } | 148 } |
| 146 | 149 |
| 147 int64_t number = 0; | 150 int64_t number = 0; |
| 148 int exponent = 0; | 151 int exponent = 0; |
| 149 const int radix = (1 << radix_log_2); | 152 const int radix = (1 << radix_log_2); |
| 150 | 153 |
| 151 do { | 154 do { |
| 152 int digit; | 155 int digit; |
| 153 if (*current >= '0' && *current <= '9' && *current < '0' + radix) { | 156 if (*current >= '0' && *current <= '9' && *current < '0' + radix) { |
| 154 digit = static_cast<char>(*current) - '0'; | 157 digit = static_cast<char>(*current) - '0'; |
| 155 } else if (radix > 10 && *current >= 'a' && *current < 'a' + radix - 10) { | 158 } else if (radix > 10 && *current >= 'a' && *current < 'a' + radix - 10) { |
| 156 digit = static_cast<char>(*current) - 'a' + 10; | 159 digit = static_cast<char>(*current) - 'a' + 10; |
| 157 } else if (radix > 10 && *current >= 'A' && *current < 'A' + radix - 10) { | 160 } else if (radix > 10 && *current >= 'A' && *current < 'A' + radix - 10) { |
| 158 digit = static_cast<char>(*current) - 'A' + 10; | 161 digit = static_cast<char>(*current) - 'A' + 10; |
| 159 } else { | 162 } else { |
| 160 if (allow_trailing_junk || !AdvanceToNonspace(¤t, end)) { | 163 if (allow_trailing_junk || |
| 164 !AdvanceToNonspace(scanner_constants, ¤t, end)) { |
| 161 break; | 165 break; |
| 162 } else { | 166 } else { |
| 163 return JUNK_STRING_VALUE; | 167 return JUNK_STRING_VALUE; |
| 164 } | 168 } |
| 165 } | 169 } |
| 166 | 170 |
| 167 number = number * radix + digit; | 171 number = number * radix + digit; |
| 168 int overflow = static_cast<int>(number >> 53); | 172 int overflow = static_cast<int>(number >> 53); |
| 169 if (overflow != 0) { | 173 if (overflow != 0) { |
| 170 // Overflow occurred. Need to determine which direction to round the | 174 // Overflow occurred. Need to determine which direction to round the |
| (...skipping 10 matching lines...) Expand all Loading... |
| 181 exponent = overflow_bits_count; | 185 exponent = overflow_bits_count; |
| 182 | 186 |
| 183 bool zero_tail = true; | 187 bool zero_tail = true; |
| 184 while (true) { | 188 while (true) { |
| 185 ++current; | 189 ++current; |
| 186 if (current == end || !isDigit(*current, radix)) break; | 190 if (current == end || !isDigit(*current, radix)) break; |
| 187 zero_tail = zero_tail && *current == '0'; | 191 zero_tail = zero_tail && *current == '0'; |
| 188 exponent += radix_log_2; | 192 exponent += radix_log_2; |
| 189 } | 193 } |
| 190 | 194 |
| 191 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 195 if (!allow_trailing_junk && |
| 196 AdvanceToNonspace(scanner_constants, ¤t, end)) { |
| 192 return JUNK_STRING_VALUE; | 197 return JUNK_STRING_VALUE; |
| 193 } | 198 } |
| 194 | 199 |
| 195 int middle_value = (1 << (overflow_bits_count - 1)); | 200 int middle_value = (1 << (overflow_bits_count - 1)); |
| 196 if (dropped_bits > middle_value) { | 201 if (dropped_bits > middle_value) { |
| 197 number++; // Rounding up. | 202 number++; // Rounding up. |
| 198 } else if (dropped_bits == middle_value) { | 203 } else if (dropped_bits == middle_value) { |
| 199 // Rounding to even to consistency with decimals: half-way case rounds | 204 // Rounding to even to consistency with decimals: half-way case rounds |
| 200 // up if significant part is odd and down otherwise. | 205 // up if significant part is odd and down otherwise. |
| 201 if ((number & 1) != 0 || !zero_tail) { | 206 if ((number & 1) != 0 || !zero_tail) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 225 } | 230 } |
| 226 | 231 |
| 227 ASSERT(number != 0); | 232 ASSERT(number != 0); |
| 228 // The double could be constructed faster from number (mantissa), exponent | 233 // The double could be constructed faster from number (mantissa), exponent |
| 229 // and sign. Assuming it's a rare case more simple code is used. | 234 // and sign. Assuming it's a rare case more simple code is used. |
| 230 return static_cast<double>(negative ? -number : number) * pow(2.0, exponent); | 235 return static_cast<double>(negative ? -number : number) * pow(2.0, exponent); |
| 231 } | 236 } |
| 232 | 237 |
| 233 | 238 |
| 234 template <class Iterator, class EndMark> | 239 template <class Iterator, class EndMark> |
| 235 static double InternalStringToInt(Iterator current, EndMark end, int radix) { | 240 static double InternalStringToInt(ScannerConstants* scanner_constants, |
| 241 Iterator current, |
| 242 EndMark end, |
| 243 int radix) { |
| 236 const bool allow_trailing_junk = true; | 244 const bool allow_trailing_junk = true; |
| 237 const double empty_string_val = JUNK_STRING_VALUE; | 245 const double empty_string_val = JUNK_STRING_VALUE; |
| 238 | 246 |
| 239 if (!AdvanceToNonspace(¤t, end)) return empty_string_val; | 247 if (!AdvanceToNonspace(scanner_constants, ¤t, end)) { |
| 248 return empty_string_val; |
| 249 } |
| 240 | 250 |
| 241 bool negative = false; | 251 bool negative = false; |
| 242 bool leading_zero = false; | 252 bool leading_zero = false; |
| 243 | 253 |
| 244 if (*current == '+') { | 254 if (*current == '+') { |
| 245 // Ignore leading sign; skip following spaces. | 255 // Ignore leading sign; skip following spaces. |
| 246 ++current; | 256 ++current; |
| 247 if (!AdvanceToNonspace(¤t, end)) return JUNK_STRING_VALUE; | 257 if (!AdvanceToNonspace(scanner_constants, ¤t, end)) { |
| 258 return JUNK_STRING_VALUE; |
| 259 } |
| 248 } else if (*current == '-') { | 260 } else if (*current == '-') { |
| 249 ++current; | 261 ++current; |
| 250 if (!AdvanceToNonspace(¤t, end)) return JUNK_STRING_VALUE; | 262 if (!AdvanceToNonspace(scanner_constants, ¤t, end)) { |
| 263 return JUNK_STRING_VALUE; |
| 264 } |
| 251 negative = true; | 265 negative = true; |
| 252 } | 266 } |
| 253 | 267 |
| 254 if (radix == 0) { | 268 if (radix == 0) { |
| 255 // Radix detection. | 269 // Radix detection. |
| 256 if (*current == '0') { | 270 if (*current == '0') { |
| 257 ++current; | 271 ++current; |
| 258 if (current == end) return SignedZero(negative); | 272 if (current == end) return SignedZero(negative); |
| 259 if (*current == 'x' || *current == 'X') { | 273 if (*current == 'x' || *current == 'X') { |
| 260 radix = 16; | 274 radix = 16; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 291 } | 305 } |
| 292 | 306 |
| 293 if (!leading_zero && !isDigit(*current, radix)) { | 307 if (!leading_zero && !isDigit(*current, radix)) { |
| 294 return JUNK_STRING_VALUE; | 308 return JUNK_STRING_VALUE; |
| 295 } | 309 } |
| 296 | 310 |
| 297 if (IsPowerOf2(radix)) { | 311 if (IsPowerOf2(radix)) { |
| 298 switch (radix) { | 312 switch (radix) { |
| 299 case 2: | 313 case 2: |
| 300 return InternalStringToIntDouble<1>( | 314 return InternalStringToIntDouble<1>( |
| 301 current, end, negative, allow_trailing_junk); | 315 scanner_constants, current, end, negative, allow_trailing_junk); |
| 302 case 4: | 316 case 4: |
| 303 return InternalStringToIntDouble<2>( | 317 return InternalStringToIntDouble<2>( |
| 304 current, end, negative, allow_trailing_junk); | 318 scanner_constants, current, end, negative, allow_trailing_junk); |
| 305 case 8: | 319 case 8: |
| 306 return InternalStringToIntDouble<3>( | 320 return InternalStringToIntDouble<3>( |
| 307 current, end, negative, allow_trailing_junk); | 321 scanner_constants, current, end, negative, allow_trailing_junk); |
| 308 | 322 |
| 309 case 16: | 323 case 16: |
| 310 return InternalStringToIntDouble<4>( | 324 return InternalStringToIntDouble<4>( |
| 311 current, end, negative, allow_trailing_junk); | 325 scanner_constants, current, end, negative, allow_trailing_junk); |
| 312 | 326 |
| 313 case 32: | 327 case 32: |
| 314 return InternalStringToIntDouble<5>( | 328 return InternalStringToIntDouble<5>( |
| 315 current, end, negative, allow_trailing_junk); | 329 scanner_constants, current, end, negative, allow_trailing_junk); |
| 316 default: | 330 default: |
| 317 UNREACHABLE(); | 331 UNREACHABLE(); |
| 318 } | 332 } |
| 319 } | 333 } |
| 320 | 334 |
| 321 if (radix == 10) { | 335 if (radix == 10) { |
| 322 // Parsing with strtod. | 336 // Parsing with strtod. |
| 323 const int kMaxSignificantDigits = 309; // Doubles are less than 1.8e308. | 337 const int kMaxSignificantDigits = 309; // Doubles are less than 1.8e308. |
| 324 // The buffer may contain up to kMaxSignificantDigits + 1 digits and a zero | 338 // The buffer may contain up to kMaxSignificantDigits + 1 digits and a zero |
| 325 // end. | 339 // end. |
| 326 const int kBufferSize = kMaxSignificantDigits + 2; | 340 const int kBufferSize = kMaxSignificantDigits + 2; |
| 327 char buffer[kBufferSize]; | 341 char buffer[kBufferSize]; |
| 328 int buffer_pos = 0; | 342 int buffer_pos = 0; |
| 329 while (*current >= '0' && *current <= '9') { | 343 while (*current >= '0' && *current <= '9') { |
| 330 if (buffer_pos <= kMaxSignificantDigits) { | 344 if (buffer_pos <= kMaxSignificantDigits) { |
| 331 // If the number has more than kMaxSignificantDigits it will be parsed | 345 // If the number has more than kMaxSignificantDigits it will be parsed |
| 332 // as infinity. | 346 // as infinity. |
| 333 ASSERT(buffer_pos < kBufferSize); | 347 ASSERT(buffer_pos < kBufferSize); |
| 334 buffer[buffer_pos++] = static_cast<char>(*current); | 348 buffer[buffer_pos++] = static_cast<char>(*current); |
| 335 } | 349 } |
| 336 ++current; | 350 ++current; |
| 337 if (current == end) break; | 351 if (current == end) break; |
| 338 } | 352 } |
| 339 | 353 |
| 340 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 354 if (!allow_trailing_junk && |
| 355 AdvanceToNonspace(scanner_constants, ¤t, end)) { |
| 341 return JUNK_STRING_VALUE; | 356 return JUNK_STRING_VALUE; |
| 342 } | 357 } |
| 343 | 358 |
| 344 ASSERT(buffer_pos < kBufferSize); | 359 ASSERT(buffer_pos < kBufferSize); |
| 345 buffer[buffer_pos] = '\0'; | 360 buffer[buffer_pos] = '\0'; |
| 346 Vector<const char> buffer_vector(buffer, buffer_pos); | 361 Vector<const char> buffer_vector(buffer, buffer_pos); |
| 347 return negative ? -Strtod(buffer_vector, 0) : Strtod(buffer_vector, 0); | 362 return negative ? -Strtod(buffer_vector, 0) : Strtod(buffer_vector, 0); |
| 348 } | 363 } |
| 349 | 364 |
| 350 // The following code causes accumulating rounding error for numbers greater | 365 // The following code causes accumulating rounding error for numbers greater |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 if (current == end) { | 410 if (current == end) { |
| 396 done = true; | 411 done = true; |
| 397 break; | 412 break; |
| 398 } | 413 } |
| 399 } | 414 } |
| 400 | 415 |
| 401 // Update the value and skip the part in the string. | 416 // Update the value and skip the part in the string. |
| 402 v = v * multiplier + part; | 417 v = v * multiplier + part; |
| 403 } while (!done); | 418 } while (!done); |
| 404 | 419 |
| 405 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 420 if (!allow_trailing_junk && |
| 421 AdvanceToNonspace(scanner_constants, ¤t, end)) { |
| 406 return JUNK_STRING_VALUE; | 422 return JUNK_STRING_VALUE; |
| 407 } | 423 } |
| 408 | 424 |
| 409 return negative ? -v : v; | 425 return negative ? -v : v; |
| 410 } | 426 } |
| 411 | 427 |
| 412 | 428 |
| 413 // Converts a string to a double value. Assumes the Iterator supports | 429 // Converts a string to a double value. Assumes the Iterator supports |
| 414 // the following operations: | 430 // the following operations: |
| 415 // 1. current == end (other ops are not allowed), current != end. | 431 // 1. current == end (other ops are not allowed), current != end. |
| 416 // 2. *current - gets the current character in the sequence. | 432 // 2. *current - gets the current character in the sequence. |
| 417 // 3. ++current (advances the position). | 433 // 3. ++current (advances the position). |
| 418 template <class Iterator, class EndMark> | 434 template <class Iterator, class EndMark> |
| 419 static double InternalStringToDouble(Iterator current, | 435 static double InternalStringToDouble(ScannerConstants* scanner_constants, |
| 436 Iterator current, |
| 420 EndMark end, | 437 EndMark end, |
| 421 int flags, | 438 int flags, |
| 422 double empty_string_val) { | 439 double empty_string_val) { |
| 423 // To make sure that iterator dereferencing is valid the following | 440 // To make sure that iterator dereferencing is valid the following |
| 424 // convention is used: | 441 // convention is used: |
| 425 // 1. Each '++current' statement is followed by check for equality to 'end'. | 442 // 1. Each '++current' statement is followed by check for equality to 'end'. |
| 426 // 2. If AdvanceToNonspace returned false then current == end. | 443 // 2. If AdvanceToNonspace returned false then current == end. |
| 427 // 3. If 'current' becomes be equal to 'end' the function returns or goes to | 444 // 3. If 'current' becomes be equal to 'end' the function returns or goes to |
| 428 // 'parsing_done'. | 445 // 'parsing_done'. |
| 429 // 4. 'current' is not dereferenced after the 'parsing_done' label. | 446 // 4. 'current' is not dereferenced after the 'parsing_done' label. |
| 430 // 5. Code before 'parsing_done' may rely on 'current != end'. | 447 // 5. Code before 'parsing_done' may rely on 'current != end'. |
| 431 if (!AdvanceToNonspace(¤t, end)) return empty_string_val; | 448 if (!AdvanceToNonspace(scanner_constants, ¤t, end)) { |
| 449 return empty_string_val; |
| 450 } |
| 432 | 451 |
| 433 const bool allow_trailing_junk = (flags & ALLOW_TRAILING_JUNK) != 0; | 452 const bool allow_trailing_junk = (flags & ALLOW_TRAILING_JUNK) != 0; |
| 434 | 453 |
| 435 // The longest form of simplified number is: "-<significant digits>'.1eXXX\0". | 454 // The longest form of simplified number is: "-<significant digits>'.1eXXX\0". |
| 436 const int kBufferSize = kMaxSignificantDigits + 10; | 455 const int kBufferSize = kMaxSignificantDigits + 10; |
| 437 char buffer[kBufferSize]; // NOLINT: size is known at compile time. | 456 char buffer[kBufferSize]; // NOLINT: size is known at compile time. |
| 438 int buffer_pos = 0; | 457 int buffer_pos = 0; |
| 439 | 458 |
| 440 // Exponent will be adjusted if insignificant digits of the integer part | 459 // Exponent will be adjusted if insignificant digits of the integer part |
| 441 // or insignificant leading zeros of the fractional part are dropped. | 460 // or insignificant leading zeros of the fractional part are dropped. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 456 if (current == end) return JUNK_STRING_VALUE; | 475 if (current == end) return JUNK_STRING_VALUE; |
| 457 negative = true; | 476 negative = true; |
| 458 } | 477 } |
| 459 | 478 |
| 460 static const char kInfinitySymbol[] = "Infinity"; | 479 static const char kInfinitySymbol[] = "Infinity"; |
| 461 if (*current == kInfinitySymbol[0]) { | 480 if (*current == kInfinitySymbol[0]) { |
| 462 if (!SubStringEquals(¤t, end, kInfinitySymbol)) { | 481 if (!SubStringEquals(¤t, end, kInfinitySymbol)) { |
| 463 return JUNK_STRING_VALUE; | 482 return JUNK_STRING_VALUE; |
| 464 } | 483 } |
| 465 | 484 |
| 466 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 485 if (!allow_trailing_junk && |
| 486 AdvanceToNonspace(scanner_constants, ¤t, end)) { |
| 467 return JUNK_STRING_VALUE; | 487 return JUNK_STRING_VALUE; |
| 468 } | 488 } |
| 469 | 489 |
| 470 ASSERT(buffer_pos == 0); | 490 ASSERT(buffer_pos == 0); |
| 471 return negative ? -V8_INFINITY : V8_INFINITY; | 491 return negative ? -V8_INFINITY : V8_INFINITY; |
| 472 } | 492 } |
| 473 | 493 |
| 474 bool leading_zero = false; | 494 bool leading_zero = false; |
| 475 if (*current == '0') { | 495 if (*current == '0') { |
| 476 ++current; | 496 ++current; |
| 477 if (current == end) return SignedZero(negative); | 497 if (current == end) return SignedZero(negative); |
| 478 | 498 |
| 479 leading_zero = true; | 499 leading_zero = true; |
| 480 | 500 |
| 481 // It could be hexadecimal value. | 501 // It could be hexadecimal value. |
| 482 if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) { | 502 if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) { |
| 483 ++current; | 503 ++current; |
| 484 if (current == end || !isDigit(*current, 16)) { | 504 if (current == end || !isDigit(*current, 16)) { |
| 485 return JUNK_STRING_VALUE; // "0x". | 505 return JUNK_STRING_VALUE; // "0x". |
| 486 } | 506 } |
| 487 | 507 |
| 488 return InternalStringToIntDouble<4>(current, | 508 return InternalStringToIntDouble<4>(scanner_constants, |
| 509 current, |
| 489 end, | 510 end, |
| 490 negative, | 511 negative, |
| 491 allow_trailing_junk); | 512 allow_trailing_junk); |
| 492 } | 513 } |
| 493 | 514 |
| 494 // Ignore leading zeros in the integer part. | 515 // Ignore leading zeros in the integer part. |
| 495 while (*current == '0') { | 516 while (*current == '0') { |
| 496 ++current; | 517 ++current; |
| 497 if (current == end) return SignedZero(negative); | 518 if (current == end) return SignedZero(negative); |
| 498 } | 519 } |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 num = max_exponent; | 635 num = max_exponent; |
| 615 } else { | 636 } else { |
| 616 num = num * 10 + digit; | 637 num = num * 10 + digit; |
| 617 } | 638 } |
| 618 ++current; | 639 ++current; |
| 619 } while (current != end && *current >= '0' && *current <= '9'); | 640 } while (current != end && *current >= '0' && *current <= '9'); |
| 620 | 641 |
| 621 exponent += (sign == '-' ? -num : num); | 642 exponent += (sign == '-' ? -num : num); |
| 622 } | 643 } |
| 623 | 644 |
| 624 if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { | 645 if (!allow_trailing_junk && |
| 646 AdvanceToNonspace(scanner_constants, ¤t, end)) { |
| 625 return JUNK_STRING_VALUE; | 647 return JUNK_STRING_VALUE; |
| 626 } | 648 } |
| 627 | 649 |
| 628 parsing_done: | 650 parsing_done: |
| 629 exponent += insignificant_digits; | 651 exponent += insignificant_digits; |
| 630 | 652 |
| 631 if (octal) { | 653 if (octal) { |
| 632 return InternalStringToIntDouble<3>(buffer, | 654 return InternalStringToIntDouble<3>(scanner_constants, |
| 655 buffer, |
| 633 buffer + buffer_pos, | 656 buffer + buffer_pos, |
| 634 negative, | 657 negative, |
| 635 allow_trailing_junk); | 658 allow_trailing_junk); |
| 636 } | 659 } |
| 637 | 660 |
| 638 if (nonzero_digit_dropped) { | 661 if (nonzero_digit_dropped) { |
| 639 buffer[buffer_pos++] = '1'; | 662 buffer[buffer_pos++] = '1'; |
| 640 exponent--; | 663 exponent--; |
| 641 } | 664 } |
| 642 | 665 |
| 643 ASSERT(buffer_pos < kBufferSize); | 666 ASSERT(buffer_pos < kBufferSize); |
| 644 buffer[buffer_pos] = '\0'; | 667 buffer[buffer_pos] = '\0'; |
| 645 | 668 |
| 646 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); | 669 double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); |
| 647 return negative ? -converted : converted; | 670 return negative ? -converted : converted; |
| 648 } | 671 } |
| 649 | 672 |
| 650 | 673 |
| 651 double StringToDouble(String* str, int flags, double empty_string_val) { | 674 double StringToDouble(String* str, int flags, double empty_string_val) { |
| 675 ScannerConstants* scanner_constants = |
| 676 Isolate::Current()->scanner_constants(); |
| 652 StringShape shape(str); | 677 StringShape shape(str); |
| 653 if (shape.IsSequentialAscii()) { | 678 if (shape.IsSequentialAscii()) { |
| 654 const char* begin = SeqAsciiString::cast(str)->GetChars(); | 679 const char* begin = SeqAsciiString::cast(str)->GetChars(); |
| 655 const char* end = begin + str->length(); | 680 const char* end = begin + str->length(); |
| 656 return InternalStringToDouble(begin, end, flags, empty_string_val); | 681 return InternalStringToDouble(scanner_constants, begin, end, flags, |
| 682 empty_string_val); |
| 657 } else if (shape.IsSequentialTwoByte()) { | 683 } else if (shape.IsSequentialTwoByte()) { |
| 658 const uc16* begin = SeqTwoByteString::cast(str)->GetChars(); | 684 const uc16* begin = SeqTwoByteString::cast(str)->GetChars(); |
| 659 const uc16* end = begin + str->length(); | 685 const uc16* end = begin + str->length(); |
| 660 return InternalStringToDouble(begin, end, flags, empty_string_val); | 686 return InternalStringToDouble(scanner_constants, begin, end, flags, |
| 687 empty_string_val); |
| 661 } else { | 688 } else { |
| 662 StringInputBuffer buffer(str); | 689 StringInputBuffer buffer(str); |
| 663 return InternalStringToDouble(StringInputBufferIterator(&buffer), | 690 return InternalStringToDouble(scanner_constants, |
| 691 StringInputBufferIterator(&buffer), |
| 664 StringInputBufferIterator::EndMarker(), | 692 StringInputBufferIterator::EndMarker(), |
| 665 flags, | 693 flags, |
| 666 empty_string_val); | 694 empty_string_val); |
| 667 } | 695 } |
| 668 } | 696 } |
| 669 | 697 |
| 670 | 698 |
| 671 double StringToInt(String* str, int radix) { | 699 double StringToInt(String* str, int radix) { |
| 700 ScannerConstants* scanner_constants = |
| 701 Isolate::Current()->scanner_constants(); |
| 672 StringShape shape(str); | 702 StringShape shape(str); |
| 673 if (shape.IsSequentialAscii()) { | 703 if (shape.IsSequentialAscii()) { |
| 674 const char* begin = SeqAsciiString::cast(str)->GetChars(); | 704 const char* begin = SeqAsciiString::cast(str)->GetChars(); |
| 675 const char* end = begin + str->length(); | 705 const char* end = begin + str->length(); |
| 676 return InternalStringToInt(begin, end, radix); | 706 return InternalStringToInt(scanner_constants, begin, end, radix); |
| 677 } else if (shape.IsSequentialTwoByte()) { | 707 } else if (shape.IsSequentialTwoByte()) { |
| 678 const uc16* begin = SeqTwoByteString::cast(str)->GetChars(); | 708 const uc16* begin = SeqTwoByteString::cast(str)->GetChars(); |
| 679 const uc16* end = begin + str->length(); | 709 const uc16* end = begin + str->length(); |
| 680 return InternalStringToInt(begin, end, radix); | 710 return InternalStringToInt(scanner_constants, begin, end, radix); |
| 681 } else { | 711 } else { |
| 682 StringInputBuffer buffer(str); | 712 StringInputBuffer buffer(str); |
| 683 return InternalStringToInt(StringInputBufferIterator(&buffer), | 713 return InternalStringToInt(scanner_constants, |
| 714 StringInputBufferIterator(&buffer), |
| 684 StringInputBufferIterator::EndMarker(), | 715 StringInputBufferIterator::EndMarker(), |
| 685 radix); | 716 radix); |
| 686 } | 717 } |
| 687 } | 718 } |
| 688 | 719 |
| 689 | 720 |
| 690 double StringToDouble(const char* str, int flags, double empty_string_val) { | 721 double StringToDouble(const char* str, int flags, double empty_string_val) { |
| 722 ScannerConstants* scanner_constants = |
| 723 Isolate::Current()->scanner_constants(); |
| 691 const char* end = str + StrLength(str); | 724 const char* end = str + StrLength(str); |
| 692 return InternalStringToDouble(str, end, flags, empty_string_val); | 725 return InternalStringToDouble(scanner_constants, str, end, flags, |
| 726 empty_string_val); |
| 693 } | 727 } |
| 694 | 728 |
| 695 | 729 |
| 696 double StringToDouble(Vector<const char> str, | 730 double StringToDouble(Vector<const char> str, |
| 697 int flags, | 731 int flags, |
| 698 double empty_string_val) { | 732 double empty_string_val) { |
| 733 ScannerConstants* scanner_constants = |
| 734 Isolate::Current()->scanner_constants(); |
| 699 const char* end = str.start() + str.length(); | 735 const char* end = str.start() + str.length(); |
| 700 return InternalStringToDouble(str.start(), end, flags, empty_string_val); | 736 return InternalStringToDouble(scanner_constants, str.start(), end, flags, |
| 737 empty_string_val); |
| 701 } | 738 } |
| 702 | 739 |
| 703 | 740 |
| 704 const char* DoubleToCString(double v, Vector<char> buffer) { | 741 const char* DoubleToCString(double v, Vector<char> buffer) { |
| 705 switch (fpclassify(v)) { | 742 switch (fpclassify(v)) { |
| 706 case FP_NAN: return "NaN"; | 743 case FP_NAN: return "NaN"; |
| 707 case FP_INFINITE: return (v < 0.0 ? "-Infinity" : "Infinity"); | 744 case FP_INFINITE: return (v < 0.0 ? "-Infinity" : "Infinity"); |
| 708 case FP_ZERO: return "0"; | 745 case FP_ZERO: return "0"; |
| 709 default: { | 746 default: { |
| 710 StringBuilder builder(buffer.start(), buffer.length()); | 747 StringBuilder builder(buffer.start(), buffer.length()); |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1059 if (decimal_pos > 0) result_size++; | 1096 if (decimal_pos > 0) result_size++; |
| 1060 // Allocate result and fill in the parts. | 1097 // Allocate result and fill in the parts. |
| 1061 StringBuilder builder(result_size + 1); | 1098 StringBuilder builder(result_size + 1); |
| 1062 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); | 1099 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); |
| 1063 if (decimal_pos > 0) builder.AddCharacter('.'); | 1100 if (decimal_pos > 0) builder.AddCharacter('.'); |
| 1064 builder.AddSubstring(decimal_buffer, decimal_pos); | 1101 builder.AddSubstring(decimal_buffer, decimal_pos); |
| 1065 return builder.Finalize(); | 1102 return builder.Finalize(); |
| 1066 } | 1103 } |
| 1067 | 1104 |
| 1068 | 1105 |
| 1106 static Mutex* dtoa_lock_one = OS::CreateMutex(); |
| 1107 static Mutex* dtoa_lock_zero = OS::CreateMutex(); |
| 1108 |
| 1109 |
| 1069 } } // namespace v8::internal | 1110 } } // namespace v8::internal |
| 1111 |
| 1112 |
| 1113 extern "C" { |
| 1114 void ACQUIRE_DTOA_LOCK(int n) { |
| 1115 ASSERT(n == 0 || n == 1); |
| 1116 (n == 0 ? v8::internal::dtoa_lock_zero : v8::internal::dtoa_lock_one)->Lock(); |
| 1117 } |
| 1118 |
| 1119 |
| 1120 void FREE_DTOA_LOCK(int n) { |
| 1121 ASSERT(n == 0 || n == 1); |
| 1122 (n == 0 ? v8::internal::dtoa_lock_zero : v8::internal::dtoa_lock_one)-> |
| 1123 Unlock(); |
| 1124 } |
| 1125 } |
| OLD | NEW |