mfbt/decimal/moz-decimal-utils.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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"
    17 #include "mozilla/NullPtr.h"
    19 #include <cmath>
    20 #include <cstring>
    21 #include <iomanip>
    22 #include <limits>
    23 #include <sstream>
    25 #ifndef UINT64_C
    26 // For Android toolchain
    27 #define UINT64_C(c) (c ## ULL)
    28 #endif
    30 #ifdef ASSERT
    31 #undef ASSERT
    32 #endif
    33 #define ASSERT MOZ_ASSERT
    35 #define ASSERT_NOT_REACHED() MOZ_ASSUME_UNREACHABLE("moz-decimal-utils.h")
    37 #define WTF_MAKE_NONCOPYABLE(ClassName) \
    38   private: \
    39     ClassName(const ClassName&) MOZ_DELETE; \
    40     void operator=(const ClassName&) MOZ_DELETE;
    42 #if defined(_MSC_VER)
    43 namespace std {
    44   inline bool isinf(double num) { return mozilla::IsInfinite(num); }
    45   inline bool isnan(double num) { return mozilla::IsNaN(num); }
    46   inline bool isfinite(double num) { return mozilla::IsFinite(num); }
    47 }
    48 #endif
    50 typedef std::string String;
    52 double mozToDouble(const String &aStr, bool *valid) {
    53   double_conversion::StringToDoubleConverter converter(
    54     double_conversion::StringToDoubleConverter::NO_FLAGS,
    55     mozilla::UnspecifiedNaN<double>(), mozilla::UnspecifiedNaN<double>(), nullptr, nullptr);
    56   const char* str = aStr.c_str();
    57   int length = mozilla::SafeCast<int>(strlen(str));
    58   int processed_char_count; // unused - NO_FLAGS requires the whole string to parse
    59   double result = converter.StringToDouble(str, length, &processed_char_count);
    60   *valid = mozilla::IsFinite(result);
    61   return result;
    62 }
    64 String mozToString(double aNum) {
    65   char buffer[64];
    66   int buffer_length = mozilla::ArrayLength(buffer);
    67   const double_conversion::DoubleToStringConverter& converter =
    68     double_conversion::DoubleToStringConverter::EcmaScriptConverter();
    69   double_conversion::StringBuilder builder(buffer, buffer_length);
    70   converter.ToShortest(aNum, &builder);
    71   return String(builder.Finalize());
    72 }
    74 String mozToString(int64_t aNum) {
    75   std::ostringstream o;
    76   o << std::setprecision(std::numeric_limits<int64_t>::digits10) << aNum;
    77   return o.str();
    78 }
    80 String mozToString(uint64_t aNum) {
    81   std::ostringstream o;
    82   o << std::setprecision(std::numeric_limits<uint64_t>::digits10) << aNum;
    83   return o.str();
    84 }
    86 namespace moz_decimal_utils {
    88 class StringBuilder
    89 {
    90 public:
    91   void append(char c) {
    92     mStr += c;
    93   }
    94   void appendLiteral(const char *aStr) {
    95     mStr += aStr;
    96   }
    97   void appendNumber(int aNum) {
    98     mStr += mozToString(int64_t(aNum));
    99   }
   100   void append(const String& aStr) {
   101     mStr += aStr;
   102   }
   103   std::string toString() const {
   104     return mStr;
   105   }
   106 private:
   107   std::string mStr;
   108 };
   110 } // namespace moz-decimal-utils
   112 #endif

mercurial