OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #include "vm/double_conversion.h" | |
6 | |
7 #include "third_party/double-conversion/src/double-conversion.h" | |
8 | |
9 #include "vm/exceptions.h" | |
10 #include "vm/globals.h" | |
11 #include "vm/object.h" | |
12 | |
13 namespace dart { | |
14 | |
15 static const char kDoubleToStringCommonExponentChar = 'e'; | |
16 static const char* kDoubleToStringCommonInfinitySymbol = "Infinity"; | |
17 static const char* kDoubleToStringCommonNaNSymbol = "NaN"; | |
18 | |
19 bool DoubleToString(double d, String& result) { | |
20 static const int kDecimalLow = -6; | |
21 static const int kDecimalHigh = 21; | |
22 static const int kConversionFlags = | |
23 double_conversion::DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN | | |
24 double_conversion::DoubleToStringConverter::EMIT_TRAILING_DECIMAL_POINT | | |
25 double_conversion::DoubleToStringConverter::EMIT_TRAILING_ZERO_AFTER_POINT; | |
26 const int kBufferSize = 128; | |
27 // The output contains the sign, at most kDecimalHigh - 1 digits, | |
28 // the decimal point followed by a 0 plus the \0. | |
29 ASSERT(kBufferSize >= 1 + (kDecimalHigh - 1) + 1 + 1 + 1); | |
30 // Or it contains the sign, a 0, the decimal point, kDecimalLow '0's, | |
31 // 17 digits (the precision needed for doubles), plus the \0. | |
32 ASSERT(kBufferSize >= 1 + 1 + 1 + kDecimalLow + 17 + 1); | |
33 // Alternatively it contains a sign, at most 17 digits (precision needed for | |
34 // any double), the decimal point, the exponent character, the exponent's | |
35 // sign, at most three exponent digits, plus the \0. | |
36 ASSERT(kBufferSize >= 1 + 17 + 1 + 1 + 1 + 3 + 1); | |
37 | |
38 const double_conversion::DoubleToStringConverter converter( | |
39 kConversionFlags, | |
40 kDoubleToStringCommonInfinitySymbol, | |
41 kDoubleToStringCommonNaNSymbol, | |
42 kDoubleToStringCommonExponentChar, | |
43 kDecimalLow, | |
44 kDecimalHigh, | |
45 0, 0); // Last two values are ignored in shortest mode. | |
46 | |
47 UNIMPLEMENTED(); | |
48 return false; | |
49 } | |
50 | |
51 bool DoubleToStringAsFixed(double d, int fraction_digits, String& result) { | |
52 static const int kMinFractionDigits = 0; | |
53 static const int kMaxFractionDigits = 20; | |
54 static const int kMaxDigitsBeforePoint = 20; | |
55 // The boundaries are exclusive. | |
56 static const double kLowerBoundary = -1e21; | |
57 static const double kUpperBoundary = 1e21; | |
58 static const int kConversionFlags = | |
59 double_conversion::DoubleToStringConverter::NO_FLAGS; | |
60 const int kBufferSize = 128; | |
61 // The output contains the sign, at most kMaxDigitsBeforePoint digits, | |
62 // the decimal point followed by at most fraction_digits digits plus the \0. | |
63 ASSERT(kBufferSize >= 1 + kMaxDigitsBeforePoint + 1 + kMaxFractionDigits + 1); | |
64 | |
65 if (!(isnan(d) || (kLowerBoundary < d && d < kUpperBoundary))) { | |
Ivan Posva
2011/12/20 00:30:03
You could add an early bailout for NaN and return
floitsch
2011/12/22 21:53:20
Added early bailout in dart-land.
Done.
| |
66 return false; | |
67 } | |
68 if (!(kMinFractionDigits <= fraction_digits && | |
Ivan Posva
2011/12/20 00:30:03
Might be personal style, but I prefer not to have
floitsch
2011/12/22 21:53:20
Done.
| |
69 fraction_digits <= kMaxFractionDigits)) { | |
70 return false; | |
71 } | |
72 | |
73 const double_conversion::DoubleToStringConverter converter( | |
74 kConversionFlags, | |
75 kDoubleToStringCommonInfinitySymbol, | |
76 kDoubleToStringCommonNaNSymbol, | |
77 kDoubleToStringCommonExponentChar, | |
78 0, 0, 0, 0); // Last four values are ignored in fixed mode. | |
79 | |
80 char buffer[kBufferSize]; | |
81 double_conversion::StringBuilder builder(buffer, kBufferSize); | |
82 bool status = converter.ToFixed(d, fraction_digits, &builder); | |
83 if (!status) return false; | |
84 int length = builder.position(); | |
85 result ^= String::New(reinterpret_cast<uint8_t*>(builder.Finalize()), length); | |
86 return true; | |
87 } | |
88 | |
89 | |
90 bool DoubleToStringAsExponential(double d, | |
91 int fraction_digits, | |
92 String& result) { | |
93 static const int kMinFractionDigits = 0; | |
94 static const int kMaxFractionDigits = 20; | |
95 static const int kConversionFlags = | |
96 double_conversion::DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN; | |
97 const int kBufferSize = 128; | |
98 // The output contains the sign, at most 1 digits, the decimal point followed | |
99 // by at most kMaxFractionDigits digits, the exponent-character, the | |
100 // exponent-sign and three exponent digits plus \0. | |
101 ASSERT(kBufferSize >= 1 + 1 + kMaxFractionDigits + 1 + 1 + 3 + 1); | |
102 | |
103 if (!(kMinFractionDigits <= fraction_digits && | |
104 fraction_digits <= kMaxFractionDigits)) { | |
105 return false; | |
106 } | |
107 | |
108 const double_conversion::DoubleToStringConverter converter( | |
109 kConversionFlags, | |
110 kDoubleToStringCommonInfinitySymbol, | |
111 kDoubleToStringCommonNaNSymbol, | |
112 kDoubleToStringCommonExponentChar, | |
113 0, 0, 0, 0); // Last four values are ignored in exponential mode. | |
114 | |
115 UNIMPLEMENTED(); | |
116 return false; | |
117 } | |
118 | |
119 | |
120 bool DoubleToStringAsPrecision(double d, int precision, String& result) { | |
121 static const int kMinPrecisionDigits = 1; | |
122 static const int kMaxPrecisionDigits = 21; | |
123 static const int kMaxLeadingPaddingZeroes = 6; | |
124 static const int kMaxTrailingPaddingZeroes = 0; | |
125 static const int kConversionFlags = | |
126 double_conversion::DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN; | |
127 const int kBufferSize = 128; | |
128 // The output contains the sign, the decimal point, precision digits, | |
129 // the exponent-character, the exponent-sign, three exponent digits | |
130 // plus the \0. | |
131 ASSERT(kBufferSize >= 1 + 1 + kMaxPrecisionDigits + 1 + 1 + 3 + 1); | |
132 | |
133 if (!(kMinPrecisionDigits <= precision && precision <= kMaxPrecisionDigits)) { | |
134 return false; | |
135 } | |
136 | |
137 const double_conversion::DoubleToStringConverter converter( | |
138 kConversionFlags, | |
139 kDoubleToStringCommonInfinitySymbol, | |
140 kDoubleToStringCommonNaNSymbol, | |
141 kDoubleToStringCommonExponentChar, | |
142 0, 0, // Ignored in precision mode. | |
143 kMaxLeadingPaddingZeroes, | |
144 kMaxTrailingPaddingZeroes); | |
145 | |
146 UNIMPLEMENTED(); | |
147 return false; | |
148 } | |
149 | |
150 } // namespace dart | |
OLD | NEW |