moz-decimal-utils.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef MOZ_DECIMAL_UTILS_H
  6. #define MOZ_DECIMAL_UTILS_H
  7. // This file contains extra includes, defines and typedefs to allow compilation
  8. // of Decimal.cpp under the Mozilla source without blink core dependencies. Do
  9. // not include it into any file other than Decimal.cpp.
  10. #include "../double-conversion/double-conversion.h"
  11. #include "mozilla/ArrayUtils.h"
  12. #include "mozilla/Casting.h"
  13. #include "mozilla/FloatingPoint.h"
  14. #include <cmath>
  15. #include <cstring>
  16. #include <iomanip>
  17. #include <limits>
  18. #include <sstream>
  19. #ifndef UINT64_C
  20. // For Android toolchain
  21. #define UINT64_C(c) (c ## ULL)
  22. #endif
  23. #ifdef ASSERT
  24. #undef ASSERT
  25. #endif
  26. #define ASSERT MOZ_ASSERT
  27. #define ASSERT_NOT_REACHED() MOZ_ASSERT_UNREACHABLE("moz-decimal-utils.h")
  28. #define STACK_ALLOCATED() DISALLOW_NEW()
  29. #define WTF_MAKE_NONCOPYABLE(ClassName) \
  30. private: \
  31. ClassName(const ClassName&) = delete; \
  32. void operator=(const ClassName&) = delete;
  33. typedef std::string String;
  34. double mozToDouble(const String &aStr, bool *valid) {
  35. double_conversion::StringToDoubleConverter converter(
  36. double_conversion::StringToDoubleConverter::NO_FLAGS,
  37. mozilla::UnspecifiedNaN<double>(), mozilla::UnspecifiedNaN<double>(), nullptr, nullptr);
  38. const char* str = aStr.c_str();
  39. int length = mozilla::AssertedCast<int>(strlen(str));
  40. int processed_char_count; // unused - NO_FLAGS requires the whole string to parse
  41. double result = converter.StringToDouble(str, length, &processed_char_count);
  42. *valid = mozilla::IsFinite(result);
  43. return result;
  44. }
  45. String mozToString(double aNum) {
  46. char buffer[64];
  47. int buffer_length = mozilla::ArrayLength(buffer);
  48. const double_conversion::DoubleToStringConverter& converter =
  49. double_conversion::DoubleToStringConverter::EcmaScriptConverter();
  50. double_conversion::StringBuilder builder(buffer, buffer_length);
  51. converter.ToShortest(aNum, &builder);
  52. return String(builder.Finalize());
  53. }
  54. String mozToString(int64_t aNum) {
  55. std::ostringstream o;
  56. o << std::setprecision(std::numeric_limits<int64_t>::digits10) << aNum;
  57. return o.str();
  58. }
  59. String mozToString(uint64_t aNum) {
  60. std::ostringstream o;
  61. o << std::setprecision(std::numeric_limits<uint64_t>::digits10) << aNum;
  62. return o.str();
  63. }
  64. namespace moz_decimal_utils {
  65. class StringBuilder
  66. {
  67. public:
  68. void append(char c) {
  69. mStr += c;
  70. }
  71. void appendLiteral(const char *aStr) {
  72. mStr += aStr;
  73. }
  74. void appendNumber(int aNum) {
  75. mStr += mozToString(int64_t(aNum));
  76. }
  77. void append(const String& aStr) {
  78. mStr += aStr;
  79. }
  80. std::string toString() const {
  81. return mStr;
  82. }
  83. private:
  84. std::string mStr;
  85. };
  86. } // namespace moz_decimal_utils
  87. #endif