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 13 matching lines...) Expand all Loading... |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include <stdarg.h> | 28 #include <stdarg.h> |
29 | 29 |
30 #include "v8.h" | 30 #include "v8.h" |
31 | 31 |
32 #include "conversions-inl.h" | 32 #include "conversions-inl.h" |
33 #include "factory.h" | 33 #include "factory.h" |
| 34 #include "grisu3.h" |
34 #include "scanner.h" | 35 #include "scanner.h" |
35 | 36 |
36 namespace v8 { | 37 namespace v8 { |
37 namespace internal { | 38 namespace internal { |
38 | 39 |
39 int HexValue(uc32 c) { | 40 int HexValue(uc32 c) { |
40 if ('0' <= c && c <= '9') | 41 if ('0' <= c && c <= '9') |
41 return c - '0'; | 42 return c - '0'; |
42 if ('a' <= c && c <= 'f') | 43 if ('a' <= c && c <= 'f') |
43 return c - 'a' + 10; | 44 return c - 'a' + 10; |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 break; | 376 break; |
376 | 377 |
377 case FP_ZERO: | 378 case FP_ZERO: |
378 builder.AddCharacter('0'); | 379 builder.AddCharacter('0'); |
379 break; | 380 break; |
380 | 381 |
381 default: { | 382 default: { |
382 int decimal_point; | 383 int decimal_point; |
383 int sign; | 384 int sign; |
384 | 385 |
385 char* decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL); | 386 char* decimal_rep; |
386 int length = StrLength(decimal_rep); | 387 bool used_dtoa = false; |
| 388 char grisu_buffer[kGrisu3MaximalLength + 1]; |
| 389 int length; |
| 390 if (grisu3(v, grisu_buffer, &sign, &length, &decimal_point)) { |
| 391 decimal_rep = grisu_buffer; |
| 392 } else { |
| 393 decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL); |
| 394 used_dtoa = true; |
| 395 length = StrLength(decimal_rep); |
| 396 } |
387 | 397 |
388 if (sign) builder.AddCharacter('-'); | 398 if (sign) builder.AddCharacter('-'); |
389 | 399 |
390 if (length <= decimal_point && decimal_point <= 21) { | 400 if (length <= decimal_point && decimal_point <= 21) { |
391 // ECMA-262 section 9.8.1 step 6. | 401 // ECMA-262 section 9.8.1 step 6. |
392 builder.AddString(decimal_rep); | 402 builder.AddString(decimal_rep); |
393 builder.AddPadding('0', decimal_point - length); | 403 builder.AddPadding('0', decimal_point - length); |
394 | 404 |
395 } else if (0 < decimal_point && decimal_point <= 21) { | 405 } else if (0 < decimal_point && decimal_point <= 21) { |
396 // ECMA-262 section 9.8.1 step 7. | 406 // ECMA-262 section 9.8.1 step 7. |
(...skipping 14 matching lines...) Expand all Loading... |
411 builder.AddCharacter('.'); | 421 builder.AddCharacter('.'); |
412 builder.AddString(decimal_rep + 1); | 422 builder.AddString(decimal_rep + 1); |
413 } | 423 } |
414 builder.AddCharacter('e'); | 424 builder.AddCharacter('e'); |
415 builder.AddCharacter((decimal_point >= 0) ? '+' : '-'); | 425 builder.AddCharacter((decimal_point >= 0) ? '+' : '-'); |
416 int exponent = decimal_point - 1; | 426 int exponent = decimal_point - 1; |
417 if (exponent < 0) exponent = -exponent; | 427 if (exponent < 0) exponent = -exponent; |
418 builder.AddFormatted("%d", exponent); | 428 builder.AddFormatted("%d", exponent); |
419 } | 429 } |
420 | 430 |
421 freedtoa(decimal_rep); | 431 if (used_dtoa) freedtoa(decimal_rep); |
422 } | 432 } |
423 } | 433 } |
424 return builder.Finalize(); | 434 return builder.Finalize(); |
425 } | 435 } |
426 | 436 |
427 | 437 |
428 const char* IntToCString(int n, Vector<char> buffer) { | 438 const char* IntToCString(int n, Vector<char> buffer) { |
429 bool negative = false; | 439 bool negative = false; |
430 if (n < 0) { | 440 if (n < 0) { |
431 // We must not negate the most negative int. | 441 // We must not negate the most negative int. |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 // Allocate result and fill in the parts. | 710 // Allocate result and fill in the parts. |
701 StringBuilder builder(result_size + 1); | 711 StringBuilder builder(result_size + 1); |
702 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); | 712 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); |
703 if (decimal_pos > 0) builder.AddCharacter('.'); | 713 if (decimal_pos > 0) builder.AddCharacter('.'); |
704 builder.AddSubstring(decimal_buffer, decimal_pos); | 714 builder.AddSubstring(decimal_buffer, decimal_pos); |
705 return builder.Finalize(); | 715 return builder.Finalize(); |
706 } | 716 } |
707 | 717 |
708 | 718 |
709 } } // namespace v8::internal | 719 } } // namespace v8::internal |
OLD | NEW |