Bumping gaia.json for 7 gaia revision(s) a=gaia-bump
[gecko.git] / mfbt / decimal / moz-decimal-utils.h
bloba13152d5e729ab415ab7b25e9845507f8e376246
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"
18 #include <cmath>
19 #include <cstring>
20 #include <iomanip>
21 #include <limits>
22 #include <sstream>
24 #ifndef UINT64_C
25 // For Android toolchain
26 #define UINT64_C(c) (c ## ULL)
27 #endif
29 #ifdef ASSERT
30 #undef ASSERT
31 #endif
32 #define ASSERT MOZ_ASSERT
34 #define ASSERT_NOT_REACHED() MOZ_ASSERT_UNREACHABLE("moz-decimal-utils.h")
36 #define WTF_MAKE_NONCOPYABLE(ClassName) \
37 private: \
38 ClassName(const ClassName&) = delete; \
39 void operator=(const ClassName&) = delete;
41 #if defined(_MSC_VER)
42 namespace std {
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); }
47 #endif
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);
60 return result;
63 String mozToString(double aNum) {
64 char buffer[64];
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) {
74 std::ostringstream o;
75 o << std::setprecision(std::numeric_limits<int64_t>::digits10) << aNum;
76 return o.str();
79 String mozToString(uint64_t aNum) {
80 std::ostringstream o;
81 o << std::setprecision(std::numeric_limits<uint64_t>::digits10) << aNum;
82 return o.str();
85 namespace moz_decimal_utils {
87 class StringBuilder
89 public:
90 void append(char c) {
91 mStr += c;
93 void appendLiteral(const char *aStr) {
94 mStr += aStr;
96 void appendNumber(int aNum) {
97 mStr += mozToString(int64_t(aNum));
99 void append(const String& aStr) {
100 mStr += aStr;
102 std::string toString() const {
103 return mStr;
105 private:
106 std::string mStr;
109 } // namespace moz-decimal-utils
111 #endif