1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef MOZ_DECIMAL_UTILS_H
7 #define MOZ_DECIMAL_UTILS_H
9 // This file contains extra includes, defines and typedefs to allow compilation
10 // of Decimal.cpp under the Mozilla source without blink core dependencies. Do
11 // not include it into any file other than Decimal.cpp.
13 #include "../double-conversion/double-conversion.h"
14 #include "mozilla/ArrayUtils.h"
15 #include "mozilla/Casting.h"
16 #include "mozilla/FloatingPoint.h"
25 // For Android toolchain
26 #define UINT64_C(c) (c ## ULL)
32 #define ASSERT MOZ_ASSERT
34 #define ASSERT_NOT_REACHED() MOZ_ASSERT_UNREACHABLE("moz-decimal-utils.h")
36 #define WTF_MAKE_NONCOPYABLE(ClassName) \
38 ClassName(const ClassName&) = delete; \
39 void operator=(const ClassName&) = delete;
43 inline bool isinf(double num
) { return mozilla::IsInfinite(num
); }
44 inline bool isnan(double num
) { return mozilla::IsNaN(num
); }
45 inline bool isfinite(double num
) { return mozilla::IsFinite(num
); }
49 typedef std::string String
;
51 double mozToDouble(const String
&aStr
, bool *valid
) {
52 double_conversion::StringToDoubleConverter
converter(
53 double_conversion::StringToDoubleConverter::NO_FLAGS
,
54 mozilla::UnspecifiedNaN
<double>(), mozilla::UnspecifiedNaN
<double>(), nullptr, nullptr);
55 const char* str
= aStr
.c_str();
56 int length
= mozilla::AssertedCast
<int>(strlen(str
));
57 int processed_char_count
; // unused - NO_FLAGS requires the whole string to parse
58 double result
= converter
.StringToDouble(str
, length
, &processed_char_count
);
59 *valid
= mozilla::IsFinite(result
);
63 String
mozToString(double aNum
) {
65 int buffer_length
= mozilla::ArrayLength(buffer
);
66 const double_conversion::DoubleToStringConverter
& converter
=
67 double_conversion::DoubleToStringConverter::EcmaScriptConverter();
68 double_conversion::StringBuilder
builder(buffer
, buffer_length
);
69 converter
.ToShortest(aNum
, &builder
);
70 return String(builder
.Finalize());
73 String
mozToString(int64_t aNum
) {
75 o
<< std::setprecision(std::numeric_limits
<int64_t>::digits10
) << aNum
;
79 String
mozToString(uint64_t aNum
) {
81 o
<< std::setprecision(std::numeric_limits
<uint64_t>::digits10
) << aNum
;
85 namespace moz_decimal_utils
{
93 void appendLiteral(const char *aStr
) {
96 void appendNumber(int aNum
) {
97 mStr
+= mozToString(int64_t(aNum
));
99 void append(const String
& aStr
) {
102 std::string
toString() const {
109 } // namespace moz-decimal-utils