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 |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
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 <math.h> |
28 #include <stdarg.h> | 29 #include <stdarg.h> |
29 | 30 |
30 #include "v8.h" | 31 #include "v8.h" |
31 | 32 |
32 #include "conversions-inl.h" | 33 #include "conversions-inl.h" |
33 #include "factory.h" | 34 #include "factory.h" |
| 35 #include "grisu3.h" |
34 #include "scanner.h" | 36 #include "scanner.h" |
35 | 37 |
36 namespace v8 { | 38 namespace v8 { |
37 namespace internal { | 39 namespace internal { |
38 | 40 |
39 int HexValue(uc32 c) { | 41 int HexValue(uc32 c) { |
40 if ('0' <= c && c <= '9') | 42 if ('0' <= c && c <= '9') |
41 return c - '0'; | 43 return c - '0'; |
42 if ('a' <= c && c <= 'f') | 44 if ('a' <= c && c <= 'f') |
43 return c - 'a' + 10; | 45 return c - 'a' + 10; |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 break; | 377 break; |
376 | 378 |
377 case FP_ZERO: | 379 case FP_ZERO: |
378 builder.AddCharacter('0'); | 380 builder.AddCharacter('0'); |
379 break; | 381 break; |
380 | 382 |
381 default: { | 383 default: { |
382 int decimal_point; | 384 int decimal_point; |
383 int sign; | 385 int sign; |
384 | 386 |
385 char* decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL); | 387 char* decimal_rep; |
386 int length = StrLength(decimal_rep); | 388 bool used_dtoa = false; |
| 389 char grisu_buffer[kGrisu3MaximalLength + 1]; |
| 390 int length; |
| 391 if (grisu3(v, grisu_buffer, &sign, &length, &decimal_point)) { |
| 392 decimal_rep = &grisu_buffer[0]; |
| 393 } else { |
| 394 decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL); |
| 395 used_dtoa = true; |
| 396 length = StrLength(decimal_rep); |
| 397 } |
387 | 398 |
388 if (sign) builder.AddCharacter('-'); | 399 if (sign) builder.AddCharacter('-'); |
389 | 400 |
390 if (length <= decimal_point && decimal_point <= 21) { | 401 if (length <= decimal_point && decimal_point <= 21) { |
391 // ECMA-262 section 9.8.1 step 6. | 402 // ECMA-262 section 9.8.1 step 6. |
392 builder.AddString(decimal_rep); | 403 builder.AddString(decimal_rep); |
393 builder.AddPadding('0', decimal_point - length); | 404 builder.AddPadding('0', decimal_point - length); |
394 | 405 |
395 } else if (0 < decimal_point && decimal_point <= 21) { | 406 } else if (0 < decimal_point && decimal_point <= 21) { |
396 // ECMA-262 section 9.8.1 step 7. | 407 // ECMA-262 section 9.8.1 step 7. |
(...skipping 14 matching lines...) Expand all Loading... |
411 builder.AddCharacter('.'); | 422 builder.AddCharacter('.'); |
412 builder.AddString(decimal_rep + 1); | 423 builder.AddString(decimal_rep + 1); |
413 } | 424 } |
414 builder.AddCharacter('e'); | 425 builder.AddCharacter('e'); |
415 builder.AddCharacter((decimal_point >= 0) ? '+' : '-'); | 426 builder.AddCharacter((decimal_point >= 0) ? '+' : '-'); |
416 int exponent = decimal_point - 1; | 427 int exponent = decimal_point - 1; |
417 if (exponent < 0) exponent = -exponent; | 428 if (exponent < 0) exponent = -exponent; |
418 builder.AddFormatted("%d", exponent); | 429 builder.AddFormatted("%d", exponent); |
419 } | 430 } |
420 | 431 |
421 freedtoa(decimal_rep); | 432 if (used_dtoa) freedtoa(decimal_rep); |
422 } | 433 } |
423 } | 434 } |
424 return builder.Finalize(); | 435 return builder.Finalize(); |
425 } | 436 } |
426 | 437 |
427 | 438 |
428 const char* IntToCString(int n, Vector<char> buffer) { | 439 const char* IntToCString(int n, Vector<char> buffer) { |
429 bool negative = false; | 440 bool negative = false; |
430 if (n < 0) { | 441 if (n < 0) { |
431 // We must not negate the most negative int. | 442 // We must not negate the most negative int. |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 bool negative = false; | 556 bool negative = false; |
546 if (value < 0) { | 557 if (value < 0) { |
547 value = -value; | 558 value = -value; |
548 negative = true; | 559 negative = true; |
549 } | 560 } |
550 | 561 |
551 // Find a sufficiently precise decimal representation of n. | 562 // Find a sufficiently precise decimal representation of n. |
552 int decimal_point; | 563 int decimal_point; |
553 int sign; | 564 int sign; |
554 char* decimal_rep = NULL; | 565 char* decimal_rep = NULL; |
| 566 bool used_dtoa = true; |
| 567 char grisu_buffer[kGrisu3MaximalLength + 1]; |
555 if (f == -1) { | 568 if (f == -1) { |
556 decimal_rep = dtoa(value, 0, 0, &decimal_point, &sign, NULL); | 569 int length; |
557 f = StrLength(decimal_rep) - 1; | 570 if (grisu3(value, grisu_buffer, &sign, &length, &decimal_point)) { |
| 571 used_dtoa = false; |
| 572 decimal_rep = &grisu_buffer[0]; |
| 573 f = length - 1; |
| 574 } else { |
| 575 decimal_rep = dtoa(value, 0, 0, &decimal_point, &sign, NULL); |
| 576 f = StrLength(decimal_rep) - 1; |
| 577 } |
558 } else { | 578 } else { |
559 decimal_rep = dtoa(value, 2, f + 1, &decimal_point, &sign, NULL); | 579 decimal_rep = dtoa(value, 2, f + 1, &decimal_point, &sign, NULL); |
560 } | 580 } |
561 int decimal_rep_length = StrLength(decimal_rep); | 581 int decimal_rep_length = StrLength(decimal_rep); |
562 ASSERT(decimal_rep_length > 0); | 582 ASSERT(decimal_rep_length > 0); |
563 ASSERT(decimal_rep_length <= f + 1); | 583 ASSERT(decimal_rep_length <= f + 1); |
564 USE(decimal_rep_length); | 584 USE(decimal_rep_length); |
565 | 585 |
566 int exponent = decimal_point - 1; | 586 int exponent = decimal_point - 1; |
567 char* result = | 587 char* result = |
568 CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1); | 588 CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1); |
569 | 589 |
570 freedtoa(decimal_rep); | 590 if (used_dtoa) freedtoa(decimal_rep); |
571 | 591 |
572 return result; | 592 return result; |
573 } | 593 } |
574 | 594 |
575 | 595 |
576 char* DoubleToPrecisionCString(double value, int p) { | 596 char* DoubleToPrecisionCString(double value, int p) { |
577 ASSERT(p >= 1 && p <= 21); | 597 ASSERT(p >= 1 && p <= 21); |
578 | 598 |
579 bool negative = false; | 599 bool negative = false; |
580 if (value < 0) { | 600 if (value < 0) { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 // Allocate result and fill in the parts. | 720 // Allocate result and fill in the parts. |
701 StringBuilder builder(result_size + 1); | 721 StringBuilder builder(result_size + 1); |
702 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); | 722 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); |
703 if (decimal_pos > 0) builder.AddCharacter('.'); | 723 if (decimal_pos > 0) builder.AddCharacter('.'); |
704 builder.AddSubstring(decimal_buffer, decimal_pos); | 724 builder.AddSubstring(decimal_buffer, decimal_pos); |
705 return builder.Finalize(); | 725 return builder.Finalize(); |
706 } | 726 } |
707 | 727 |
708 | 728 |
709 } } // namespace v8::internal | 729 } } // namespace v8::internal |
OLD | NEW |