Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/mfbt/decimal/moz-decimal-utils.h
Line
Count
Source (jump to first uncovered line)
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/ArrayUtils.h"
15
#include "mozilla/Casting.h"
16
#include "mozilla/FloatingPoint.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
0
#define ASSERT MOZ_ASSERT
33
34
0
#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
0
double mozToDouble(const String &aStr, bool *valid) {
46
0
  double_conversion::StringToDoubleConverter converter(
47
0
    double_conversion::StringToDoubleConverter::NO_FLAGS,
48
0
    mozilla::UnspecifiedNaN<double>(), mozilla::UnspecifiedNaN<double>(), nullptr, nullptr);
49
0
  const char* str = aStr.c_str();
50
0
  int length = mozilla::AssertedCast<int>(strlen(str));
51
0
  int processed_char_count; // unused - NO_FLAGS requires the whole string to parse
52
0
  double result = converter.StringToDouble(str, length, &processed_char_count);
53
0
  *valid = mozilla::IsFinite(result);
54
0
  return result;
55
0
}
56
57
0
String mozToString(double aNum) {
58
0
  char buffer[64];
59
0
  int buffer_length = mozilla::ArrayLength(buffer);
60
0
  const double_conversion::DoubleToStringConverter& converter =
61
0
    double_conversion::DoubleToStringConverter::EcmaScriptConverter();
62
0
  double_conversion::StringBuilder builder(buffer, buffer_length);
63
0
  converter.ToShortest(aNum, &builder);
64
0
  return String(builder.Finalize());
65
0
}
66
67
0
String mozToString(int64_t aNum) {
68
0
  std::ostringstream o;
69
0
  o << std::setprecision(std::numeric_limits<int64_t>::digits10) << aNum;
70
0
  return o.str();
71
0
}
72
73
0
String mozToString(uint64_t aNum) {
74
0
  std::ostringstream o;
75
0
  o << std::setprecision(std::numeric_limits<uint64_t>::digits10) << aNum;
76
0
  return o.str();
77
0
}
78
79
namespace moz_decimal_utils {
80
81
class StringBuilder
82
{
83
public:
84
0
  void append(char c) {
85
0
    mStr += c;
86
0
  }
87
0
  void appendLiteral(const char *aStr) {
88
0
    mStr += aStr;
89
0
  }
90
0
  void appendNumber(int aNum) {
91
0
    mStr += mozToString(int64_t(aNum));
92
0
  }
93
0
  void append(const String& aStr) {
94
0
    mStr += aStr;
95
0
  }
96
0
  std::string toString() const {
97
0
    return mStr;
98
0
  }
99
private:
100
  std::string mStr;
101
};
102
103
} // namespace moz_decimal_utils
104
105
#endif
106