moz-decimal-utils.h (3189B)
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/. */ 5 6 #ifndef MOZ_DECIMAL_UTILS_H 7 #define MOZ_DECIMAL_UTILS_H 8 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. 12 13 #include "double-conversion/double-conversion.h" 14 #include "mozilla/Casting.h" 15 #include "mozilla/FloatingPoint.h" 16 #include "mozilla/Span.h" 17 18 #include <cmath> 19 #include <cstring> 20 #include <iomanip> 21 #include <limits> 22 #include <sstream> 23 24 #ifndef UINT64_C 25 // For Android toolchain 26 #define UINT64_C(c) (c ## ULL) 27 #endif 28 29 #ifdef ASSERT 30 #undef ASSERT 31 #endif 32 #define ASSERT MOZ_ASSERT 33 34 #define ASSERT_NOT_REACHED() MOZ_ASSERT_UNREACHABLE("moz-decimal-utils.h") 35 36 #define STACK_ALLOCATED() DISALLOW_NEW() 37 38 #define WTF_MAKE_NONCOPYABLE(ClassName) \ 39 private: \ 40 ClassName(const ClassName&) = delete; \ 41 void operator=(const ClassName&) = delete; 42 43 typedef std::string String; 44 45 double mozToDouble(mozilla::Span<const char> aStr, bool *valid) { 46 double_conversion::StringToDoubleConverter converter( 47 double_conversion::StringToDoubleConverter::NO_FLAGS, 48 mozilla::UnspecifiedNaN<double>(), mozilla::UnspecifiedNaN<double>(), nullptr, nullptr); 49 const char* str = aStr.Elements(); 50 int length = mozilla::AssertedCast<int>(aStr.Length()); 51 int processed_char_count; // unused - NO_FLAGS requires the whole string to parse 52 double result = converter.StringToDouble(str, length, &processed_char_count); 53 *valid = std::isfinite(result); 54 return result; 55 } 56 57 double mozToDouble(const String &aStr, bool *valid) { 58 return mozToDouble(mozilla::MakeStringSpan(aStr.c_str()), valid); 59 } 60 61 String mozToString(double aNum) { 62 char buffer[64]; 63 int buffer_length = std::size(buffer); 64 const double_conversion::DoubleToStringConverter& converter = 65 double_conversion::DoubleToStringConverter::EcmaScriptConverter(); 66 double_conversion::StringBuilder builder(buffer, buffer_length); 67 converter.ToShortest(aNum, &builder); 68 return String(builder.Finalize()); 69 } 70 71 String mozToString(int64_t aNum) { 72 #ifdef _LIBCPP_VERSION 73 std::ostringstream o; 74 o << std::setprecision(std::numeric_limits<int64_t>::digits10) << aNum; 75 return o.str(); 76 #else 77 return std::to_string(aNum); 78 #endif 79 } 80 81 String mozToString(uint64_t aNum) { 82 #ifdef _LIBCPP_VERSION 83 std::ostringstream o; 84 o << std::setprecision(std::numeric_limits<uint64_t>::digits10) << aNum; 85 return o.str(); 86 #else 87 return std::to_string(aNum); 88 #endif 89 } 90 91 namespace moz_decimal_utils { 92 93 class StringBuilder 94 { 95 public: 96 void append(char c) { 97 mStr += c; 98 } 99 void appendLiteral(const char *aStr) { 100 mStr += aStr; 101 } 102 void appendNumber(int aNum) { 103 mStr += mozToString(int64_t(aNum)); 104 } 105 void append(const String& aStr) { 106 mStr += aStr; 107 } 108 std::string toString() const { 109 return mStr; 110 } 111 private: 112 std::string mStr; 113 }; 114 115 } // namespace moz_decimal_utils 116 117 #endif