mfbt/decimal/moz-decimal-utils.h

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef MOZ_DECIMAL_UTILS_H
michael@0 7 #define MOZ_DECIMAL_UTILS_H
michael@0 8
michael@0 9 // This file contains extra includes, defines and typedefs to allow compilation
michael@0 10 // of Decimal.cpp under the Mozilla source without blink core dependencies. Do
michael@0 11 // not include it into any file other than Decimal.cpp.
michael@0 12
michael@0 13 #include "../double-conversion/double-conversion.h"
michael@0 14 #include "mozilla/ArrayUtils.h"
michael@0 15 #include "mozilla/Casting.h"
michael@0 16 #include "mozilla/FloatingPoint.h"
michael@0 17 #include "mozilla/NullPtr.h"
michael@0 18
michael@0 19 #include <cmath>
michael@0 20 #include <cstring>
michael@0 21 #include <iomanip>
michael@0 22 #include <limits>
michael@0 23 #include <sstream>
michael@0 24
michael@0 25 #ifndef UINT64_C
michael@0 26 // For Android toolchain
michael@0 27 #define UINT64_C(c) (c ## ULL)
michael@0 28 #endif
michael@0 29
michael@0 30 #ifdef ASSERT
michael@0 31 #undef ASSERT
michael@0 32 #endif
michael@0 33 #define ASSERT MOZ_ASSERT
michael@0 34
michael@0 35 #define ASSERT_NOT_REACHED() MOZ_ASSUME_UNREACHABLE("moz-decimal-utils.h")
michael@0 36
michael@0 37 #define WTF_MAKE_NONCOPYABLE(ClassName) \
michael@0 38 private: \
michael@0 39 ClassName(const ClassName&) MOZ_DELETE; \
michael@0 40 void operator=(const ClassName&) MOZ_DELETE;
michael@0 41
michael@0 42 #if defined(_MSC_VER)
michael@0 43 namespace std {
michael@0 44 inline bool isinf(double num) { return mozilla::IsInfinite(num); }
michael@0 45 inline bool isnan(double num) { return mozilla::IsNaN(num); }
michael@0 46 inline bool isfinite(double num) { return mozilla::IsFinite(num); }
michael@0 47 }
michael@0 48 #endif
michael@0 49
michael@0 50 typedef std::string String;
michael@0 51
michael@0 52 double mozToDouble(const String &aStr, bool *valid) {
michael@0 53 double_conversion::StringToDoubleConverter converter(
michael@0 54 double_conversion::StringToDoubleConverter::NO_FLAGS,
michael@0 55 mozilla::UnspecifiedNaN<double>(), mozilla::UnspecifiedNaN<double>(), nullptr, nullptr);
michael@0 56 const char* str = aStr.c_str();
michael@0 57 int length = mozilla::SafeCast<int>(strlen(str));
michael@0 58 int processed_char_count; // unused - NO_FLAGS requires the whole string to parse
michael@0 59 double result = converter.StringToDouble(str, length, &processed_char_count);
michael@0 60 *valid = mozilla::IsFinite(result);
michael@0 61 return result;
michael@0 62 }
michael@0 63
michael@0 64 String mozToString(double aNum) {
michael@0 65 char buffer[64];
michael@0 66 int buffer_length = mozilla::ArrayLength(buffer);
michael@0 67 const double_conversion::DoubleToStringConverter& converter =
michael@0 68 double_conversion::DoubleToStringConverter::EcmaScriptConverter();
michael@0 69 double_conversion::StringBuilder builder(buffer, buffer_length);
michael@0 70 converter.ToShortest(aNum, &builder);
michael@0 71 return String(builder.Finalize());
michael@0 72 }
michael@0 73
michael@0 74 String mozToString(int64_t aNum) {
michael@0 75 std::ostringstream o;
michael@0 76 o << std::setprecision(std::numeric_limits<int64_t>::digits10) << aNum;
michael@0 77 return o.str();
michael@0 78 }
michael@0 79
michael@0 80 String mozToString(uint64_t aNum) {
michael@0 81 std::ostringstream o;
michael@0 82 o << std::setprecision(std::numeric_limits<uint64_t>::digits10) << aNum;
michael@0 83 return o.str();
michael@0 84 }
michael@0 85
michael@0 86 namespace moz_decimal_utils {
michael@0 87
michael@0 88 class StringBuilder
michael@0 89 {
michael@0 90 public:
michael@0 91 void append(char c) {
michael@0 92 mStr += c;
michael@0 93 }
michael@0 94 void appendLiteral(const char *aStr) {
michael@0 95 mStr += aStr;
michael@0 96 }
michael@0 97 void appendNumber(int aNum) {
michael@0 98 mStr += mozToString(int64_t(aNum));
michael@0 99 }
michael@0 100 void append(const String& aStr) {
michael@0 101 mStr += aStr;
michael@0 102 }
michael@0 103 std::string toString() const {
michael@0 104 return mStr;
michael@0 105 }
michael@0 106 private:
michael@0 107 std::string mStr;
michael@0 108 };
michael@0 109
michael@0 110 } // namespace moz-decimal-utils
michael@0 111
michael@0 112 #endif
michael@0 113

mercurial