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 #include <limits.h> | 29 #include <limits.h> |
30 | 30 |
31 #include "v8.h" | 31 #include "v8.h" |
32 | 32 |
33 #include "conversions-inl.h" | 33 #include "conversions-inl.h" |
| 34 #include "dtoa.h" |
34 #include "factory.h" | 35 #include "factory.h" |
35 #include "fast-dtoa.h" | |
36 #include "scanner.h" | 36 #include "scanner.h" |
37 | 37 |
38 namespace v8 { | 38 namespace v8 { |
39 namespace internal { | 39 namespace internal { |
40 | 40 |
41 int HexValue(uc32 c) { | 41 int HexValue(uc32 c) { |
42 if ('0' <= c && c <= '9') | 42 if ('0' <= c && c <= '9') |
43 return c - '0'; | 43 return c - '0'; |
44 if ('a' <= c && c <= 'f') | 44 if ('a' <= c && c <= 'f') |
45 return c - 'a' + 10; | 45 return c - 'a' + 10; |
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 } | 759 } |
760 break; | 760 break; |
761 | 761 |
762 case FP_ZERO: | 762 case FP_ZERO: |
763 builder.AddCharacter('0'); | 763 builder.AddCharacter('0'); |
764 break; | 764 break; |
765 | 765 |
766 default: { | 766 default: { |
767 int decimal_point; | 767 int decimal_point; |
768 int sign; | 768 int sign; |
769 | |
770 char* decimal_rep; | 769 char* decimal_rep; |
771 bool used_gay_dtoa = false; | 770 bool used_gay_dtoa = false; |
772 const int kFastDtoaBufferCapacity = kFastDtoaMaximalLength + 1; | 771 const int kV8DtoaBufferCapacity = kBase10MaximalLength + 1; |
773 char fast_dtoa_buffer[kFastDtoaBufferCapacity]; | 772 char v8_dtoa_buffer[kV8DtoaBufferCapacity]; |
774 int length; | 773 int length; |
775 if (FastDtoa(v, Vector<char>(fast_dtoa_buffer, kFastDtoaBufferCapacity), | 774 |
776 &sign, &length, &decimal_point)) { | 775 if (DoubleToAscii(v, DTOA_SHORTEST, 0, |
777 decimal_rep = fast_dtoa_buffer; | 776 Vector<char>(v8_dtoa_buffer, kV8DtoaBufferCapacity), |
| 777 &sign, &length, &decimal_point)) { |
| 778 decimal_rep = v8_dtoa_buffer; |
778 } else { | 779 } else { |
779 decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL); | 780 decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL); |
780 used_gay_dtoa = true; | 781 used_gay_dtoa = true; |
781 length = StrLength(decimal_rep); | 782 length = StrLength(decimal_rep); |
782 } | 783 } |
783 | 784 |
784 if (sign) builder.AddCharacter('-'); | 785 if (sign) builder.AddCharacter('-'); |
785 | 786 |
786 if (length <= decimal_point && decimal_point <= 21) { | 787 if (length <= decimal_point && decimal_point <= 21) { |
787 // ECMA-262 section 9.8.1 step 6. | 788 // ECMA-262 section 9.8.1 step 6. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
835 do { | 836 do { |
836 buffer[--i] = '0' + (n % 10); | 837 buffer[--i] = '0' + (n % 10); |
837 n /= 10; | 838 n /= 10; |
838 } while (n); | 839 } while (n); |
839 if (negative) buffer[--i] = '-'; | 840 if (negative) buffer[--i] = '-'; |
840 return buffer.start() + i; | 841 return buffer.start() + i; |
841 } | 842 } |
842 | 843 |
843 | 844 |
844 char* DoubleToFixedCString(double value, int f) { | 845 char* DoubleToFixedCString(double value, int f) { |
| 846 const int kMaxDigitsBeforePoint = 20; |
| 847 const double kFirstNonFixed = 1e21; |
| 848 const int kMaxDigitsAfterPoint = 20; |
845 ASSERT(f >= 0); | 849 ASSERT(f >= 0); |
| 850 ASSERT(f <= kMaxDigitsAfterPoint); |
846 | 851 |
847 bool negative = false; | 852 bool negative = false; |
848 double abs_value = value; | 853 double abs_value = value; |
849 if (value < 0) { | 854 if (value < 0) { |
850 abs_value = -value; | 855 abs_value = -value; |
851 negative = true; | 856 negative = true; |
852 } | 857 } |
853 | 858 |
854 if (abs_value >= 1e21) { | 859 // If abs_value has more than kMaxDigitsBeforePoint digits before the point |
| 860 // use the non-fixed conversion routine. |
| 861 if (abs_value >= kFirstNonFixed) { |
855 char arr[100]; | 862 char arr[100]; |
856 Vector<char> buffer(arr, ARRAY_SIZE(arr)); | 863 Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
857 return StrDup(DoubleToCString(value, buffer)); | 864 return StrDup(DoubleToCString(value, buffer)); |
858 } | 865 } |
859 | 866 |
860 // Find a sufficiently precise decimal representation of n. | 867 // Find a sufficiently precise decimal representation of n. |
861 int decimal_point; | 868 int decimal_point; |
862 int sign; | 869 int sign; |
863 char* decimal_rep = dtoa(abs_value, 3, f, &decimal_point, &sign, NULL); | 870 // Add space for the '.' and the '\0' byte. |
864 int decimal_rep_length = StrLength(decimal_rep); | 871 const int kDecimalRepCapacity = |
| 872 kMaxDigitsBeforePoint + kMaxDigitsAfterPoint + 2; |
| 873 char decimal_rep[kDecimalRepCapacity]; |
| 874 int decimal_rep_length; |
| 875 bool status = DoubleToAscii(value, DTOA_FIXED, f, |
| 876 Vector<char>(decimal_rep, kDecimalRepCapacity), |
| 877 &sign, &decimal_rep_length, &decimal_point); |
| 878 USE(status); |
| 879 ASSERT(status); |
865 | 880 |
866 // Create a representation that is padded with zeros if needed. | 881 // Create a representation that is padded with zeros if needed. |
867 int zero_prefix_length = 0; | 882 int zero_prefix_length = 0; |
868 int zero_postfix_length = 0; | 883 int zero_postfix_length = 0; |
869 | 884 |
870 if (decimal_point <= 0) { | 885 if (decimal_point <= 0) { |
871 zero_prefix_length = -decimal_point + 1; | 886 zero_prefix_length = -decimal_point + 1; |
872 decimal_point = 1; | 887 decimal_point = 1; |
873 } | 888 } |
874 | 889 |
875 if (zero_prefix_length + decimal_rep_length < decimal_point + f) { | 890 if (zero_prefix_length + decimal_rep_length < decimal_point + f) { |
876 zero_postfix_length = decimal_point + f - decimal_rep_length - | 891 zero_postfix_length = decimal_point + f - decimal_rep_length - |
877 zero_prefix_length; | 892 zero_prefix_length; |
878 } | 893 } |
879 | 894 |
880 unsigned rep_length = | 895 unsigned rep_length = |
881 zero_prefix_length + decimal_rep_length + zero_postfix_length; | 896 zero_prefix_length + decimal_rep_length + zero_postfix_length; |
882 StringBuilder rep_builder(rep_length + 1); | 897 StringBuilder rep_builder(rep_length + 1); |
883 rep_builder.AddPadding('0', zero_prefix_length); | 898 rep_builder.AddPadding('0', zero_prefix_length); |
884 rep_builder.AddString(decimal_rep); | 899 rep_builder.AddString(decimal_rep); |
885 rep_builder.AddPadding('0', zero_postfix_length); | 900 rep_builder.AddPadding('0', zero_postfix_length); |
886 char* rep = rep_builder.Finalize(); | 901 char* rep = rep_builder.Finalize(); |
887 freedtoa(decimal_rep); | |
888 | 902 |
889 // Create the result string by appending a minus and putting in a | 903 // Create the result string by appending a minus and putting in a |
890 // decimal point if needed. | 904 // decimal point if needed. |
891 unsigned result_size = decimal_point + f + 2; | 905 unsigned result_size = decimal_point + f + 2; |
892 StringBuilder builder(result_size + 1); | 906 StringBuilder builder(result_size + 1); |
893 if (negative) builder.AddCharacter('-'); | 907 if (negative) builder.AddCharacter('-'); |
894 builder.AddSubstring(rep, decimal_point); | 908 builder.AddSubstring(rep, decimal_point); |
895 if (f > 0) { | 909 if (f > 0) { |
896 builder.AddCharacter('.'); | 910 builder.AddCharacter('.'); |
897 builder.AddSubstring(rep + decimal_point, f); | 911 builder.AddSubstring(rep + decimal_point, f); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1096 // Allocate result and fill in the parts. | 1110 // Allocate result and fill in the parts. |
1097 StringBuilder builder(result_size + 1); | 1111 StringBuilder builder(result_size + 1); |
1098 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); | 1112 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); |
1099 if (decimal_pos > 0) builder.AddCharacter('.'); | 1113 if (decimal_pos > 0) builder.AddCharacter('.'); |
1100 builder.AddSubstring(decimal_buffer, decimal_pos); | 1114 builder.AddSubstring(decimal_buffer, decimal_pos); |
1101 return builder.Finalize(); | 1115 return builder.Finalize(); |
1102 } | 1116 } |
1103 | 1117 |
1104 | 1118 |
1105 } } // namespace v8::internal | 1119 } } // namespace v8::internal |
OLD | NEW |