mfbt/decimal/moz-decimal-utils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/decimal/moz-decimal-utils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZ_DECIMAL_UTILS_H
    1.10 +#define MOZ_DECIMAL_UTILS_H
    1.11 +
    1.12 +// This file contains extra includes, defines and typedefs to allow compilation
    1.13 +// of Decimal.cpp under the Mozilla source without blink core dependencies. Do
    1.14 +// not include it into any file other than Decimal.cpp.
    1.15 +
    1.16 +#include "../double-conversion/double-conversion.h"
    1.17 +#include "mozilla/ArrayUtils.h"
    1.18 +#include "mozilla/Casting.h"
    1.19 +#include "mozilla/FloatingPoint.h"
    1.20 +#include "mozilla/NullPtr.h"
    1.21 +
    1.22 +#include <cmath>
    1.23 +#include <cstring>
    1.24 +#include <iomanip>
    1.25 +#include <limits>
    1.26 +#include <sstream>
    1.27 +
    1.28 +#ifndef UINT64_C
    1.29 +// For Android toolchain
    1.30 +#define UINT64_C(c) (c ## ULL)
    1.31 +#endif
    1.32 +
    1.33 +#ifdef ASSERT
    1.34 +#undef ASSERT
    1.35 +#endif
    1.36 +#define ASSERT MOZ_ASSERT
    1.37 +
    1.38 +#define ASSERT_NOT_REACHED() MOZ_ASSUME_UNREACHABLE("moz-decimal-utils.h")
    1.39 +
    1.40 +#define WTF_MAKE_NONCOPYABLE(ClassName) \
    1.41 +  private: \
    1.42 +    ClassName(const ClassName&) MOZ_DELETE; \
    1.43 +    void operator=(const ClassName&) MOZ_DELETE;
    1.44 +
    1.45 +#if defined(_MSC_VER)
    1.46 +namespace std {
    1.47 +  inline bool isinf(double num) { return mozilla::IsInfinite(num); }
    1.48 +  inline bool isnan(double num) { return mozilla::IsNaN(num); }
    1.49 +  inline bool isfinite(double num) { return mozilla::IsFinite(num); }
    1.50 +}
    1.51 +#endif
    1.52 +
    1.53 +typedef std::string String;
    1.54 +
    1.55 +double mozToDouble(const String &aStr, bool *valid) {
    1.56 +  double_conversion::StringToDoubleConverter converter(
    1.57 +    double_conversion::StringToDoubleConverter::NO_FLAGS,
    1.58 +    mozilla::UnspecifiedNaN<double>(), mozilla::UnspecifiedNaN<double>(), nullptr, nullptr);
    1.59 +  const char* str = aStr.c_str();
    1.60 +  int length = mozilla::SafeCast<int>(strlen(str));
    1.61 +  int processed_char_count; // unused - NO_FLAGS requires the whole string to parse
    1.62 +  double result = converter.StringToDouble(str, length, &processed_char_count);
    1.63 +  *valid = mozilla::IsFinite(result);
    1.64 +  return result;
    1.65 +}
    1.66 +
    1.67 +String mozToString(double aNum) {
    1.68 +  char buffer[64];
    1.69 +  int buffer_length = mozilla::ArrayLength(buffer);
    1.70 +  const double_conversion::DoubleToStringConverter& converter =
    1.71 +    double_conversion::DoubleToStringConverter::EcmaScriptConverter();
    1.72 +  double_conversion::StringBuilder builder(buffer, buffer_length);
    1.73 +  converter.ToShortest(aNum, &builder);
    1.74 +  return String(builder.Finalize());
    1.75 +}
    1.76 +
    1.77 +String mozToString(int64_t aNum) {
    1.78 +  std::ostringstream o;
    1.79 +  o << std::setprecision(std::numeric_limits<int64_t>::digits10) << aNum;
    1.80 +  return o.str();
    1.81 +}
    1.82 +
    1.83 +String mozToString(uint64_t aNum) {
    1.84 +  std::ostringstream o;
    1.85 +  o << std::setprecision(std::numeric_limits<uint64_t>::digits10) << aNum;
    1.86 +  return o.str();
    1.87 +}
    1.88 +
    1.89 +namespace moz_decimal_utils {
    1.90 +
    1.91 +class StringBuilder
    1.92 +{
    1.93 +public:
    1.94 +  void append(char c) {
    1.95 +    mStr += c;
    1.96 +  }
    1.97 +  void appendLiteral(const char *aStr) {
    1.98 +    mStr += aStr;
    1.99 +  }
   1.100 +  void appendNumber(int aNum) {
   1.101 +    mStr += mozToString(int64_t(aNum));
   1.102 +  }
   1.103 +  void append(const String& aStr) {
   1.104 +    mStr += aStr;
   1.105 +  }
   1.106 +  std::string toString() const {
   1.107 +    return mStr;
   1.108 +  }
   1.109 +private:
   1.110 +  std::string mStr;
   1.111 +};
   1.112 +
   1.113 +} // namespace moz-decimal-utils
   1.114 +
   1.115 +#endif
   1.116 +

mercurial