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 798 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
809 do { | 809 do { |
810 buffer[--i] = '0' + (n % 10); | 810 buffer[--i] = '0' + (n % 10); |
811 n /= 10; | 811 n /= 10; |
812 } while (n); | 812 } while (n); |
813 if (negative) buffer[--i] = '-'; | 813 if (negative) buffer[--i] = '-'; |
814 return buffer.start() + i; | 814 return buffer.start() + i; |
815 } | 815 } |
816 | 816 |
817 | 817 |
818 char* DoubleToFixedCString(double value, int f) { | 818 char* DoubleToFixedCString(double value, int f) { |
819 const int kMaxDigitsBeforePoint = 20; | 819 const int kMaxDigitsBeforePoint = 21; |
820 const double kFirstNonFixed = 1e21; | 820 const double kFirstNonFixed = 1e21; |
821 const int kMaxDigitsAfterPoint = 20; | 821 const int kMaxDigitsAfterPoint = 20; |
822 ASSERT(f >= 0); | 822 ASSERT(f >= 0); |
823 ASSERT(f <= kMaxDigitsAfterPoint); | 823 ASSERT(f <= kMaxDigitsAfterPoint); |
824 | 824 |
825 bool negative = false; | 825 bool negative = false; |
826 double abs_value = value; | 826 double abs_value = value; |
827 if (value < 0) { | 827 if (value < 0) { |
828 abs_value = -value; | 828 abs_value = -value; |
829 negative = true; | 829 negative = true; |
830 } | 830 } |
831 | 831 |
832 // If abs_value has more than kMaxDigitsBeforePoint digits before the point | 832 // If abs_value has more than kMaxDigitsBeforePoint digits before the point |
833 // use the non-fixed conversion routine. | 833 // use the non-fixed conversion routine. |
834 if (abs_value >= kFirstNonFixed) { | 834 if (abs_value >= kFirstNonFixed) { |
835 char arr[100]; | 835 char arr[100]; |
836 Vector<char> buffer(arr, ARRAY_SIZE(arr)); | 836 Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
837 return StrDup(DoubleToCString(value, buffer)); | 837 return StrDup(DoubleToCString(value, buffer)); |
838 } | 838 } |
839 | 839 |
840 // Find a sufficiently precise decimal representation of n. | 840 // Find a sufficiently precise decimal representation of n. |
841 int decimal_point; | 841 int decimal_point; |
842 int sign; | 842 int sign; |
843 // Add space for the '.' and the '\0' byte. | 843 // Add space for the '\0' byte. |
844 const int kDecimalRepCapacity = | 844 const int kDecimalRepCapacity = |
845 kMaxDigitsBeforePoint + kMaxDigitsAfterPoint + 2; | 845 kMaxDigitsBeforePoint + kMaxDigitsAfterPoint + 1; |
846 char decimal_rep[kDecimalRepCapacity]; | 846 char decimal_rep[kDecimalRepCapacity]; |
847 int decimal_rep_length; | 847 int decimal_rep_length; |
848 bool status = DoubleToAscii(value, DTOA_FIXED, f, | 848 bool status = DoubleToAscii(value, DTOA_FIXED, f, |
849 Vector<char>(decimal_rep, kDecimalRepCapacity), | 849 Vector<char>(decimal_rep, kDecimalRepCapacity), |
850 &sign, &decimal_rep_length, &decimal_point); | 850 &sign, &decimal_rep_length, &decimal_point); |
851 USE(status); | 851 USE(status); |
852 ASSERT(status); | 852 ASSERT(status); |
853 | 853 |
854 // Create a representation that is padded with zeros if needed. | 854 // Create a representation that is padded with zeros if needed. |
855 int zero_prefix_length = 0; | 855 int zero_prefix_length = 0; |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1131 // Allocate result and fill in the parts. | 1131 // Allocate result and fill in the parts. |
1132 StringBuilder builder(result_size + 1); | 1132 StringBuilder builder(result_size + 1); |
1133 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); | 1133 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); |
1134 if (decimal_pos > 0) builder.AddCharacter('.'); | 1134 if (decimal_pos > 0) builder.AddCharacter('.'); |
1135 builder.AddSubstring(decimal_buffer, decimal_pos); | 1135 builder.AddSubstring(decimal_buffer, decimal_pos); |
1136 return builder.Finalize(); | 1136 return builder.Finalize(); |
1137 } | 1137 } |
1138 | 1138 |
1139 | 1139 |
1140 } } // namespace v8::internal | 1140 } } // namespace v8::internal |
OLD | NEW |