2013-05-05 16:23:18 -07:00
|
|
|
diff --git a/mfbt/decimal/Decimal.cpp b/mfbt/decimal/Decimal.cpp
|
|
|
|
--- a/mfbt/decimal/Decimal.cpp
|
|
|
|
+++ b/mfbt/decimal/Decimal.cpp
|
2013-09-17 05:24:35 -07:00
|
|
|
@@ -23,26 +23,23 @@
|
2013-05-05 16:23:18 -07:00
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
-#include "config.h"
|
|
|
|
#include "Decimal.h"
|
|
|
|
+#include "moz-decimal-utils.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
-#include <wtf/Assertions.h>
|
|
|
|
-#include <wtf/MathExtras.h>
|
|
|
|
-#include <wtf/Noncopyable.h>
|
|
|
|
-#include <wtf/text/StringBuilder.h>
|
2013-09-17 05:24:35 -07:00
|
|
|
+using namespace moz_decimal_utils;
|
|
|
|
|
2013-05-05 16:23:18 -07:00
|
|
|
namespace WebCore {
|
|
|
|
|
|
|
|
namespace DecimalPrivate {
|
|
|
|
|
|
|
|
static int const ExponentMax = 1023;
|
|
|
|
static int const ExponentMin = -1023;
|
|
|
|
static int const Precision = 18;
|
2013-09-17 05:24:35 -07:00
|
|
|
@@ -685,17 +682,17 @@ Decimal Decimal::floor() const
|
2013-05-05 16:23:18 -07:00
|
|
|
result += 1;
|
|
|
|
}
|
|
|
|
return Decimal(sign(), 0, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Decimal Decimal::fromDouble(double doubleValue)
|
|
|
|
{
|
|
|
|
if (std::isfinite(doubleValue))
|
|
|
|
- return fromString(String::numberToStringECMAScript(doubleValue));
|
|
|
|
+ return fromString(mozToString(doubleValue));
|
|
|
|
|
|
|
|
if (std::isinf(doubleValue))
|
|
|
|
return infinity(doubleValue < 0 ? Negative : Positive);
|
|
|
|
|
|
|
|
return nan();
|
|
|
|
}
|
|
|
|
|
|
|
|
Decimal Decimal::fromString(const String& str)
|
2013-09-17 05:24:35 -07:00
|
|
|
@@ -937,17 +934,17 @@ Decimal Decimal::round() const
|
2013-05-05 16:23:18 -07:00
|
|
|
result /= 10;
|
|
|
|
return Decimal(sign(), 0, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
double Decimal::toDouble() const
|
|
|
|
{
|
|
|
|
if (isFinite()) {
|
|
|
|
bool valid;
|
|
|
|
- const double doubleValue = toString().toDouble(&valid);
|
|
|
|
+ const double doubleValue = mozToDouble(toString(), &valid);
|
|
|
|
return valid ? doubleValue : std::numeric_limits<double>::quiet_NaN();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInfinity())
|
|
|
|
return isNegative() ? -std::numeric_limits<double>::infinity() : std::numeric_limits<double>::infinity();
|
|
|
|
|
|
|
|
return std::numeric_limits<double>::quiet_NaN();
|
|
|
|
}
|
2013-09-17 05:24:35 -07:00
|
|
|
@@ -990,17 +987,17 @@ String Decimal::toString() const
|
2013-05-05 16:23:18 -07:00
|
|
|
++coefficient;
|
|
|
|
|
|
|
|
while (originalExponent < 0 && coefficient && !(coefficient % 10)) {
|
|
|
|
coefficient /= 10;
|
|
|
|
++originalExponent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- const String digits = String::number(coefficient);
|
|
|
|
+ const String digits = mozToString(coefficient);
|
|
|
|
int coefficientLength = static_cast<int>(digits.length());
|
|
|
|
const int adjustedExponent = originalExponent + coefficientLength - 1;
|
|
|
|
if (originalExponent <= 0 && adjustedExponent >= -6) {
|
|
|
|
if (!originalExponent) {
|
|
|
|
builder.append(digits);
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
2013-09-17 05:24:35 -07:00
|
|
|
@@ -1032,15 +1029,28 @@ String Decimal::toString() const
|
2013-05-05 16:23:18 -07:00
|
|
|
if (adjustedExponent) {
|
|
|
|
builder.append(adjustedExponent < 0 ? "e" : "e+");
|
|
|
|
builder.appendNumber(adjustedExponent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return builder.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
+bool Decimal::toString(char* strBuf, size_t bufLength) const
|
|
|
|
+{
|
|
|
|
+ ASSERT(bufLength > 0);
|
|
|
|
+ String str = toString();
|
|
|
|
+ size_t length = str.copy(strBuf, bufLength);
|
|
|
|
+ if (length < bufLength) {
|
|
|
|
+ strBuf[length] = '\0';
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ strBuf[bufLength - 1] = '\0';
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
Decimal Decimal::zero(Sign sign)
|
|
|
|
{
|
|
|
|
return Decimal(EncodedData(sign, EncodedData::ClassZero));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WebCore
|
|
|
|
|
|
|
|
diff --git a/mfbt/decimal/Decimal.h b/mfbt/decimal/Decimal.h
|
|
|
|
--- a/mfbt/decimal/Decimal.h
|
|
|
|
+++ b/mfbt/decimal/Decimal.h
|
|
|
|
@@ -23,24 +23,41 @@
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Imported from:
|
|
|
|
+ * http://src.chromium.org/viewvc/blink/trunk/Source/core/platform/Decimal.h
|
|
|
|
+ * Check hg log for the svn rev of the last update from Blink core.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
#ifndef Decimal_h
|
|
|
|
#define Decimal_h
|
|
|
|
|
|
|
|
+#include "mozilla/Assertions.h"
|
2013-07-30 07:25:31 -07:00
|
|
|
+#include <stdint.h>
|
2013-05-05 16:23:18 -07:00
|
|
|
#include "mozilla/Types.h"
|
|
|
|
|
|
|
|
-#include <stdint.h>
|
|
|
|
-#include <wtf/Assertions.h>
|
|
|
|
-#include <wtf/text/WTFString.h>
|
|
|
|
+#include <string>
|
|
|
|
+
|
|
|
|
+#ifndef ASSERT
|
|
|
|
+#define DEFINED_ASSERT_FOR_DECIMAL_H 1
|
|
|
|
+#define ASSERT MOZ_ASSERT
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+// To use WTF_MAKE_FAST_ALLOCATED we'd need:
|
|
|
|
+// http://src.chromium.org/viewvc/blink/trunk/Source/wtf/FastMalloc.h
|
|
|
|
+// Since we don't allocate Decimal objects, no need.
|
|
|
|
+#define WTF_MAKE_FAST_ALLOCATED \
|
|
|
|
+ void ignore_this_dummy_method() MOZ_DELETE
|
|
|
|
|
|
|
|
namespace WebCore {
|
|
|
|
|
|
|
|
namespace DecimalPrivate {
|
|
|
|
class SpecialValueHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This class represents decimal base floating point number.
|
|
|
|
@@ -136,27 +153,28 @@ public:
|
|
|
|
MFBT_API Decimal abs() const;
|
|
|
|
MFBT_API Decimal ceiling() const;
|
|
|
|
MFBT_API Decimal floor() const;
|
|
|
|
MFBT_API Decimal remainder(const Decimal&) const;
|
|
|
|
MFBT_API Decimal round() const;
|
|
|
|
|
|
|
|
MFBT_API double toDouble() const;
|
|
|
|
// Note: toString method supports infinity and nan but fromString not.
|
|
|
|
- MFBT_API String toString() const;
|
|
|
|
+ MFBT_API std::string toString() const;
|
|
|
|
+ MFBT_API bool toString(char* strBuf, size_t bufLength) const;
|
|
|
|
|
|
|
|
static MFBT_API Decimal fromDouble(double);
|
|
|
|
// fromString supports following syntax EBNF:
|
|
|
|
// number ::= sign? digit+ ('.' digit*) (exponent-marker sign? digit+)?
|
|
|
|
// | sign? '.' digit+ (exponent-marker sign? digit+)?
|
|
|
|
// sign ::= '+' | '-'
|
|
|
|
// exponent-marker ::= 'e' | 'E'
|
|
|
|
// digit ::= '0' | '1' | ... | '9'
|
|
|
|
// Note: fromString doesn't support "infinity" and "nan".
|
|
|
|
- static MFBT_API Decimal fromString(const String&);
|
|
|
|
+ static MFBT_API Decimal fromString(const std::string& aValue);
|
|
|
|
static MFBT_API Decimal infinity(Sign);
|
|
|
|
static MFBT_API Decimal nan();
|
|
|
|
static MFBT_API Decimal zero(Sign);
|
|
|
|
|
|
|
|
// You should not use below methods. We expose them for unit testing.
|
|
|
|
MFBT_API explicit Decimal(const EncodedData&);
|
|
|
|
const EncodedData& value() const { return m_data; }
|
|
|
|
|
|
|
|
@@ -175,10 +193,21 @@ private:
|
|
|
|
|
|
|
|
Sign sign() const { return m_data.sign(); }
|
|
|
|
|
|
|
|
EncodedData m_data;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace WebCore
|
|
|
|
|
|
|
|
+namespace mozilla {
|
|
|
|
+ typedef WebCore::Decimal Decimal;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#undef WTF_MAKE_FAST_ALLOCATED
|
|
|
|
+
|
|
|
|
+#ifdef DEFINED_ASSERT_FOR_DECIMAL_H
|
|
|
|
+#undef DEFINED_ASSERT_FOR_DECIMAL_H
|
|
|
|
+#undef ASSERT
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
#endif // Decimal_h
|
|
|
|
|