summaryrefslogtreecommitdiffstats
path: root/dom/xslt/base/txDouble.cpp
blob: ad7fa690be8c84ee815c77d751215c82035dfac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/FloatingPoint.h"

#include "nsString.h"
#include "txCore.h"
#include "txXMLUtils.h"
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#ifdef WIN32
#  include <float.h>
#endif
#include "prdtoa.h"

/*
 * Utility class for doubles
 */

/*
 * Converts the given String to a double, if the String value does not
 * represent a double, NaN will be returned
 */
class txStringToDouble {
 public:
  txStringToDouble() : mState(eWhitestart), mSign(ePositive) {}

  void Parse(const nsAString& aSource) {
    if (mState == eIllegal) {
      return;
    }
    uint32_t i = 0;
    char16_t c;
    auto len = aSource.Length();
    for (; i < len; ++i) {
      c = aSource[i];
      switch (mState) {
        case eWhitestart:
          if (c == '-') {
            mState = eDecimal;
            mSign = eNegative;
          } else if (c >= '0' && c <= '9') {
            mState = eDecimal;
            mBuffer.Append((char)c);
          } else if (c == '.') {
            mState = eMantissa;
            mBuffer.Append((char)c);
          } else if (!XMLUtils::isWhitespace(c)) {
            mState = eIllegal;
            return;
          }
          break;
        case eDecimal:
          if (c >= '0' && c <= '9') {
            mBuffer.Append((char)c);
          } else if (c == '.') {
            mState = eMantissa;
            mBuffer.Append((char)c);
          } else if (XMLUtils::isWhitespace(c)) {
            mState = eWhiteend;
          } else {
            mState = eIllegal;
            return;
          }
          break;
        case eMantissa:
          if (c >= '0' && c <= '9') {
            mBuffer.Append((char)c);
          } else if (XMLUtils::isWhitespace(c)) {
            mState = eWhiteend;
          } else {
            mState = eIllegal;
            return;
          }
          break;
        case eWhiteend:
          if (!XMLUtils::isWhitespace(c)) {
            mState = eIllegal;
            return;
          }
          break;
        default:
          break;
      }
    }
  }

  double getDouble() {
    if (mState == eIllegal || mBuffer.IsEmpty() ||
        (mBuffer.Length() == 1 && mBuffer[0] == '.')) {
      return mozilla::UnspecifiedNaN<double>();
    }
    return static_cast<double>(mSign) * PR_strtod(mBuffer.get(), nullptr);
  }

 private:
  nsAutoCString mBuffer;
  enum { eWhitestart, eDecimal, eMantissa, eWhiteend, eIllegal } mState;
  enum { eNegative = -1, ePositive = 1 } mSign;
};

double txDouble::toDouble(const nsAString& aSrc) {
  txStringToDouble sink;
  sink.Parse(aSrc);
  return sink.getDouble();
}

/*
 * Converts the value of the given double to a String, and places
 * The result into the destination String.
 * @return the given dest string
 */
void txDouble::toString(double aValue, nsAString& aDest) {
  // check for special cases

  if (std::isnan(aValue)) {
    aDest.AppendLiteral("NaN");
    return;
  }
  if (std::isinf(aValue)) {
    if (aValue < 0) aDest.Append(char16_t('-'));
    aDest.AppendLiteral("Infinity");
    return;
  }

  // Mantissa length is 17, so this is plenty
  const int buflen = 20;
  char buf[buflen];

  int intDigits, sign;
  char* endp;
  PR_dtoa(aValue, 0, 0, &intDigits, &sign, &endp, buf, buflen - 1);

  // compute length
  int32_t length = endp - buf;
  if (length > intDigits) {
    // decimal point needed
    ++length;
    if (intDigits < 1) {
      // leading zeros, -intDigits + 1
      length += 1 - intDigits;
    }
  } else {
    // trailing zeros, total length given by intDigits
    length = intDigits;
  }
  if (aValue < 0) ++length;
  // grow the string
  uint32_t oldlength = aDest.Length();
  if (!aDest.SetLength(oldlength + length, mozilla::fallible))
    return;  // out of memory
  auto dest = aDest.BeginWriting();
  std::advance(dest, oldlength);
  if (aValue < 0) {
    *dest = '-';
    ++dest;
  }
  int i;
  // leading zeros
  if (intDigits < 1) {
    *dest = '0';
    ++dest;
    *dest = '.';
    ++dest;
    for (i = 0; i > intDigits; --i) {
      *dest = '0';
      ++dest;
    }
  }
  // mantissa
  int firstlen = std::min<size_t>(intDigits, endp - buf);
  for (i = 0; i < firstlen; i++) {
    *dest = buf[i];
    ++dest;
  }
  if (i < endp - buf) {
    if (i > 0) {
      *dest = '.';
      ++dest;
    }
    for (; i < endp - buf; i++) {
      *dest = buf[i];
      ++dest;
    }
  }
  // trailing zeros
  for (; i < intDigits; i++) {
    *dest = '0';
    ++dest;
  }
}