michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZ_DECIMAL_UTILS_H michael@0: #define MOZ_DECIMAL_UTILS_H michael@0: michael@0: // This file contains extra includes, defines and typedefs to allow compilation michael@0: // of Decimal.cpp under the Mozilla source without blink core dependencies. Do michael@0: // not include it into any file other than Decimal.cpp. michael@0: michael@0: #include "../double-conversion/double-conversion.h" michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/Casting.h" michael@0: #include "mozilla/FloatingPoint.h" michael@0: #include "mozilla/NullPtr.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifndef UINT64_C michael@0: // For Android toolchain michael@0: #define UINT64_C(c) (c ## ULL) michael@0: #endif michael@0: michael@0: #ifdef ASSERT michael@0: #undef ASSERT michael@0: #endif michael@0: #define ASSERT MOZ_ASSERT michael@0: michael@0: #define ASSERT_NOT_REACHED() MOZ_ASSUME_UNREACHABLE("moz-decimal-utils.h") michael@0: michael@0: #define WTF_MAKE_NONCOPYABLE(ClassName) \ michael@0: private: \ michael@0: ClassName(const ClassName&) MOZ_DELETE; \ michael@0: void operator=(const ClassName&) MOZ_DELETE; michael@0: michael@0: #if defined(_MSC_VER) michael@0: namespace std { michael@0: inline bool isinf(double num) { return mozilla::IsInfinite(num); } michael@0: inline bool isnan(double num) { return mozilla::IsNaN(num); } michael@0: inline bool isfinite(double num) { return mozilla::IsFinite(num); } michael@0: } michael@0: #endif michael@0: michael@0: typedef std::string String; michael@0: michael@0: double mozToDouble(const String &aStr, bool *valid) { michael@0: double_conversion::StringToDoubleConverter converter( michael@0: double_conversion::StringToDoubleConverter::NO_FLAGS, michael@0: mozilla::UnspecifiedNaN(), mozilla::UnspecifiedNaN(), nullptr, nullptr); michael@0: const char* str = aStr.c_str(); michael@0: int length = mozilla::SafeCast(strlen(str)); michael@0: int processed_char_count; // unused - NO_FLAGS requires the whole string to parse michael@0: double result = converter.StringToDouble(str, length, &processed_char_count); michael@0: *valid = mozilla::IsFinite(result); michael@0: return result; michael@0: } michael@0: michael@0: String mozToString(double aNum) { michael@0: char buffer[64]; michael@0: int buffer_length = mozilla::ArrayLength(buffer); michael@0: const double_conversion::DoubleToStringConverter& converter = michael@0: double_conversion::DoubleToStringConverter::EcmaScriptConverter(); michael@0: double_conversion::StringBuilder builder(buffer, buffer_length); michael@0: converter.ToShortest(aNum, &builder); michael@0: return String(builder.Finalize()); michael@0: } michael@0: michael@0: String mozToString(int64_t aNum) { michael@0: std::ostringstream o; michael@0: o << std::setprecision(std::numeric_limits::digits10) << aNum; michael@0: return o.str(); michael@0: } michael@0: michael@0: String mozToString(uint64_t aNum) { michael@0: std::ostringstream o; michael@0: o << std::setprecision(std::numeric_limits::digits10) << aNum; michael@0: return o.str(); michael@0: } michael@0: michael@0: namespace moz_decimal_utils { michael@0: michael@0: class StringBuilder michael@0: { michael@0: public: michael@0: void append(char c) { michael@0: mStr += c; michael@0: } michael@0: void appendLiteral(const char *aStr) { michael@0: mStr += aStr; michael@0: } michael@0: void appendNumber(int aNum) { michael@0: mStr += mozToString(int64_t(aNum)); michael@0: } michael@0: void append(const String& aStr) { michael@0: mStr += aStr; michael@0: } michael@0: std::string toString() const { michael@0: return mStr; michael@0: } michael@0: private: michael@0: std::string mStr; michael@0: }; michael@0: michael@0: } // namespace moz-decimal-utils michael@0: michael@0: #endif michael@0: