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