|
1 diff --git a/mfbt/decimal/Decimal.cpp b/mfbt/decimal/Decimal.cpp |
|
2 --- a/mfbt/decimal/Decimal.cpp |
|
3 +++ b/mfbt/decimal/Decimal.cpp |
|
4 @@ -23,26 +23,23 @@ |
|
5 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
6 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
7 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
8 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
9 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
10 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
11 */ |
|
12 |
|
13 -#include "config.h" |
|
14 #include "Decimal.h" |
|
15 +#include "moz-decimal-utils.h" |
|
16 |
|
17 #include <algorithm> |
|
18 #include <float.h> |
|
19 |
|
20 -#include <wtf/Assertions.h> |
|
21 -#include <wtf/MathExtras.h> |
|
22 -#include <wtf/Noncopyable.h> |
|
23 -#include <wtf/text/StringBuilder.h> |
|
24 +using namespace moz_decimal_utils; |
|
25 |
|
26 namespace WebCore { |
|
27 |
|
28 namespace DecimalPrivate { |
|
29 |
|
30 static int const ExponentMax = 1023; |
|
31 static int const ExponentMin = -1023; |
|
32 static int const Precision = 18; |
|
33 @@ -685,17 +682,17 @@ Decimal Decimal::floor() const |
|
34 result += 1; |
|
35 } |
|
36 return Decimal(sign(), 0, result); |
|
37 } |
|
38 |
|
39 Decimal Decimal::fromDouble(double doubleValue) |
|
40 { |
|
41 if (std::isfinite(doubleValue)) |
|
42 - return fromString(String::numberToStringECMAScript(doubleValue)); |
|
43 + return fromString(mozToString(doubleValue)); |
|
44 |
|
45 if (std::isinf(doubleValue)) |
|
46 return infinity(doubleValue < 0 ? Negative : Positive); |
|
47 |
|
48 return nan(); |
|
49 } |
|
50 |
|
51 Decimal Decimal::fromString(const String& str) |
|
52 @@ -937,17 +934,17 @@ Decimal Decimal::round() const |
|
53 result /= 10; |
|
54 return Decimal(sign(), 0, result); |
|
55 } |
|
56 |
|
57 double Decimal::toDouble() const |
|
58 { |
|
59 if (isFinite()) { |
|
60 bool valid; |
|
61 - const double doubleValue = toString().toDouble(&valid); |
|
62 + const double doubleValue = mozToDouble(toString(), &valid); |
|
63 return valid ? doubleValue : std::numeric_limits<double>::quiet_NaN(); |
|
64 } |
|
65 |
|
66 if (isInfinity()) |
|
67 return isNegative() ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity(); |
|
68 |
|
69 return std::numeric_limits<double>::quiet_NaN(); |
|
70 } |
|
71 @@ -990,17 +987,17 @@ String Decimal::toString() const |
|
72 ++coefficient; |
|
73 |
|
74 while (originalExponent < 0 && coefficient && !(coefficient % 10)) { |
|
75 coefficient /= 10; |
|
76 ++originalExponent; |
|
77 } |
|
78 } |
|
79 |
|
80 - const String digits = String::number(coefficient); |
|
81 + const String digits = mozToString(coefficient); |
|
82 int coefficientLength = static_cast<int>(digits.length()); |
|
83 const int adjustedExponent = originalExponent + coefficientLength - 1; |
|
84 if (originalExponent <= 0 && adjustedExponent >= -6) { |
|
85 if (!originalExponent) { |
|
86 builder.append(digits); |
|
87 return builder.toString(); |
|
88 } |
|
89 |
|
90 @@ -1032,15 +1029,28 @@ String Decimal::toString() const |
|
91 if (adjustedExponent) { |
|
92 builder.append(adjustedExponent < 0 ? "e" : "e+"); |
|
93 builder.appendNumber(adjustedExponent); |
|
94 } |
|
95 } |
|
96 return builder.toString(); |
|
97 } |
|
98 |
|
99 +bool Decimal::toString(char* strBuf, size_t bufLength) const |
|
100 +{ |
|
101 + ASSERT(bufLength > 0); |
|
102 + String str = toString(); |
|
103 + size_t length = str.copy(strBuf, bufLength); |
|
104 + if (length < bufLength) { |
|
105 + strBuf[length] = '\0'; |
|
106 + return true; |
|
107 + } |
|
108 + strBuf[bufLength - 1] = '\0'; |
|
109 + return false; |
|
110 +} |
|
111 + |
|
112 Decimal Decimal::zero(Sign sign) |
|
113 { |
|
114 return Decimal(EncodedData(sign, EncodedData::ClassZero)); |
|
115 } |
|
116 |
|
117 } // namespace WebCore |
|
118 |
|
119 diff --git a/mfbt/decimal/Decimal.h b/mfbt/decimal/Decimal.h |
|
120 --- a/mfbt/decimal/Decimal.h |
|
121 +++ b/mfbt/decimal/Decimal.h |
|
122 @@ -23,24 +23,41 @@ |
|
123 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
124 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
125 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
126 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
127 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
128 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
129 */ |
|
130 |
|
131 +/** |
|
132 + * Imported from: |
|
133 + * http://src.chromium.org/viewvc/blink/trunk/Source/core/platform/Decimal.h |
|
134 + * Check hg log for the svn rev of the last update from Blink core. |
|
135 + */ |
|
136 + |
|
137 #ifndef Decimal_h |
|
138 #define Decimal_h |
|
139 |
|
140 +#include "mozilla/Assertions.h" |
|
141 +#include <stdint.h> |
|
142 #include "mozilla/Types.h" |
|
143 |
|
144 -#include <stdint.h> |
|
145 -#include <wtf/Assertions.h> |
|
146 -#include <wtf/text/WTFString.h> |
|
147 +#include <string> |
|
148 + |
|
149 +#ifndef ASSERT |
|
150 +#define DEFINED_ASSERT_FOR_DECIMAL_H 1 |
|
151 +#define ASSERT MOZ_ASSERT |
|
152 +#endif |
|
153 + |
|
154 +// To use WTF_MAKE_FAST_ALLOCATED we'd need: |
|
155 +// http://src.chromium.org/viewvc/blink/trunk/Source/wtf/FastMalloc.h |
|
156 +// Since we don't allocate Decimal objects, no need. |
|
157 +#define WTF_MAKE_FAST_ALLOCATED \ |
|
158 + void ignore_this_dummy_method() MOZ_DELETE |
|
159 |
|
160 namespace WebCore { |
|
161 |
|
162 namespace DecimalPrivate { |
|
163 class SpecialValueHandler; |
|
164 } |
|
165 |
|
166 // This class represents decimal base floating point number. |
|
167 @@ -136,27 +153,28 @@ public: |
|
168 MFBT_API Decimal abs() const; |
|
169 MFBT_API Decimal ceiling() const; |
|
170 MFBT_API Decimal floor() const; |
|
171 MFBT_API Decimal remainder(const Decimal&) const; |
|
172 MFBT_API Decimal round() const; |
|
173 |
|
174 MFBT_API double toDouble() const; |
|
175 // Note: toString method supports infinity and nan but fromString not. |
|
176 - MFBT_API String toString() const; |
|
177 + MFBT_API std::string toString() const; |
|
178 + MFBT_API bool toString(char* strBuf, size_t bufLength) const; |
|
179 |
|
180 static MFBT_API Decimal fromDouble(double); |
|
181 // fromString supports following syntax EBNF: |
|
182 // number ::= sign? digit+ ('.' digit*) (exponent-marker sign? digit+)? |
|
183 // | sign? '.' digit+ (exponent-marker sign? digit+)? |
|
184 // sign ::= '+' | '-' |
|
185 // exponent-marker ::= 'e' | 'E' |
|
186 // digit ::= '0' | '1' | ... | '9' |
|
187 // Note: fromString doesn't support "infinity" and "nan". |
|
188 - static MFBT_API Decimal fromString(const String&); |
|
189 + static MFBT_API Decimal fromString(const std::string& aValue); |
|
190 static MFBT_API Decimal infinity(Sign); |
|
191 static MFBT_API Decimal nan(); |
|
192 static MFBT_API Decimal zero(Sign); |
|
193 |
|
194 // You should not use below methods. We expose them for unit testing. |
|
195 MFBT_API explicit Decimal(const EncodedData&); |
|
196 const EncodedData& value() const { return m_data; } |
|
197 |
|
198 @@ -175,10 +193,21 @@ private: |
|
199 |
|
200 Sign sign() const { return m_data.sign(); } |
|
201 |
|
202 EncodedData m_data; |
|
203 }; |
|
204 |
|
205 } // namespace WebCore |
|
206 |
|
207 +namespace mozilla { |
|
208 + typedef WebCore::Decimal Decimal; |
|
209 +} |
|
210 + |
|
211 +#undef WTF_MAKE_FAST_ALLOCATED |
|
212 + |
|
213 +#ifdef DEFINED_ASSERT_FOR_DECIMAL_H |
|
214 +#undef DEFINED_ASSERT_FOR_DECIMAL_H |
|
215 +#undef ASSERT |
|
216 +#endif |
|
217 + |
|
218 #endif // Decimal_h |
|
219 |