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