2005-11-02 10:42:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2012-01-23 15:43:16 +04:00
|
|
|
#include "mozilla/FloatingPoint.h"
|
|
|
|
|
2005-11-02 10:39:22 +03:00
|
|
|
#include "nsString.h"
|
2005-11-02 10:42:16 +03:00
|
|
|
#include "txCore.h"
|
2005-11-02 20:34:14 +03:00
|
|
|
#include "txXMLUtils.h"
|
2005-11-02 10:37:29 +03:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2005-11-02 10:36:06 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <float.h>
|
|
|
|
#endif
|
2005-11-02 10:37:16 +03:00
|
|
|
#include "prdtoa.h"
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2005-11-02 10:37:16 +03:00
|
|
|
/*
|
|
|
|
* Utility class for doubles
|
|
|
|
*/
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2005-11-02 10:37:16 +03:00
|
|
|
/*
|
1999-08-28 12:19:56 +04:00
|
|
|
* Converts the given String to a double, if the String value does not
|
|
|
|
* represent a double, NaN will be returned
|
2005-11-02 10:37:16 +03:00
|
|
|
*/
|
2005-11-02 10:38:11 +03:00
|
|
|
class txStringToDouble
|
2005-11-02 10:37:16 +03:00
|
|
|
{
|
2005-11-02 10:38:11 +03:00
|
|
|
public:
|
2014-01-04 19:02:17 +04:00
|
|
|
typedef char16_t input_type;
|
|
|
|
typedef char16_t value_type;
|
2005-11-02 10:38:11 +03:00
|
|
|
txStringToDouble(): mState(eWhitestart), mSign(ePositive) {}
|
2005-11-02 10:37:16 +03:00
|
|
|
|
2008-01-04 03:07:06 +03:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
write(const input_type* aSource, uint32_t aSourceLength)
|
2005-11-02 10:38:11 +03:00
|
|
|
{
|
|
|
|
if (mState == eIllegal) {
|
2008-01-04 03:07:06 +03:00
|
|
|
return;
|
1999-08-28 12:19:56 +04:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t c;
|
2005-11-02 10:38:11 +03:00
|
|
|
for ( ; i < aSourceLength; ++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;
|
2008-01-04 03:07:06 +03:00
|
|
|
return;
|
2005-11-02 10:38:11 +03:00
|
|
|
}
|
|
|
|
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;
|
2008-01-04 03:07:06 +03:00
|
|
|
return;
|
2005-11-02 10:38:11 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case eMantissa:
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
mBuffer.Append((char)c);
|
|
|
|
}
|
|
|
|
else if (XMLUtils::isWhitespace(c)) {
|
|
|
|
mState = eWhiteend;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mState = eIllegal;
|
2008-01-04 03:17:52 +03:00
|
|
|
return;
|
2005-11-02 10:38:11 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case eWhiteend:
|
|
|
|
if (!XMLUtils::isWhitespace(c)) {
|
|
|
|
mState = eIllegal;
|
2008-01-04 03:07:06 +03:00
|
|
|
return;
|
2005-11-02 10:38:11 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-08-28 12:19:56 +04:00
|
|
|
}
|
|
|
|
|
2005-11-02 10:38:11 +03:00
|
|
|
double
|
|
|
|
getDouble()
|
|
|
|
{
|
|
|
|
if (mState == eIllegal || mBuffer.IsEmpty() ||
|
|
|
|
(mBuffer.Length() == 1 && mBuffer[0] == '.')) {
|
2014-02-27 19:23:16 +04:00
|
|
|
return mozilla::UnspecifiedNaN<double>();
|
2005-11-02 10:38:11 +03:00
|
|
|
}
|
|
|
|
return mSign*PR_strtod(mBuffer.get(), 0);
|
1999-08-28 12:19:56 +04:00
|
|
|
}
|
2005-11-02 10:38:11 +03:00
|
|
|
private:
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString mBuffer;
|
2005-11-02 10:38:11 +03:00
|
|
|
enum {
|
|
|
|
eWhitestart,
|
|
|
|
eDecimal,
|
|
|
|
eMantissa,
|
|
|
|
eWhiteend,
|
|
|
|
eIllegal
|
|
|
|
} mState;
|
|
|
|
enum {
|
|
|
|
eNegative = -1,
|
|
|
|
ePositive = 1
|
|
|
|
} mSign;
|
|
|
|
};
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2011-11-20 15:18:26 +04:00
|
|
|
double txDouble::toDouble(const nsAString& aSrc)
|
2005-11-02 10:38:11 +03:00
|
|
|
{
|
|
|
|
txStringToDouble sink;
|
|
|
|
nsAString::const_iterator fromBegin, fromEnd;
|
2005-11-02 10:39:22 +03:00
|
|
|
copy_string(aSrc.BeginReading(fromBegin), aSrc.EndReading(fromEnd), sink);
|
2005-11-02 10:38:11 +03:00
|
|
|
return sink.getDouble();
|
2005-11-02 10:37:16 +03:00
|
|
|
}
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2005-11-02 10:37:16 +03:00
|
|
|
/*
|
1999-08-28 12:19:56 +04:00
|
|
|
* Converts the value of the given double to a String, and places
|
|
|
|
* The result into the destination String.
|
|
|
|
* @return the given dest string
|
2005-11-02 10:37:16 +03:00
|
|
|
*/
|
2011-11-20 15:18:26 +04:00
|
|
|
void txDouble::toString(double aValue, nsAString& aDest)
|
2005-11-02 10:37:16 +03:00
|
|
|
{
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2005-11-02 10:37:16 +03:00
|
|
|
// check for special cases
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2013-05-02 00:55:13 +04:00
|
|
|
if (mozilla::IsNaN(aValue)) {
|
2005-11-02 10:42:33 +03:00
|
|
|
aDest.AppendLiteral("NaN");
|
2005-11-02 10:39:22 +03:00
|
|
|
return;
|
1999-08-28 12:19:56 +04:00
|
|
|
}
|
2013-05-02 00:55:13 +04:00
|
|
|
if (mozilla::IsInfinite(aValue)) {
|
2005-11-02 10:37:16 +03:00
|
|
|
if (aValue < 0)
|
2014-01-04 19:02:17 +04:00
|
|
|
aDest.Append(char16_t('-'));
|
2005-11-02 10:42:33 +03:00
|
|
|
aDest.AppendLiteral("Infinity");
|
2005-11-02 10:39:22 +03:00
|
|
|
return;
|
1999-08-28 12:19:56 +04:00
|
|
|
}
|
|
|
|
|
2005-11-02 10:39:31 +03:00
|
|
|
// Mantissa length is 17, so this is plenty
|
|
|
|
const int buflen = 20;
|
|
|
|
char buf[buflen];
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2012-08-09 11:09:40 +04:00
|
|
|
int intDigits, sign;
|
2005-11-02 10:37:16 +03:00
|
|
|
char* endp;
|
2005-11-02 10:39:31 +03:00
|
|
|
PR_dtoa(aValue, 0, 0, &intDigits, &sign, &endp, buf, buflen - 1);
|
1999-08-28 12:19:56 +04:00
|
|
|
|
2005-11-02 10:39:31 +03:00
|
|
|
// compute length
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t length = endp - buf;
|
2005-11-02 10:39:31 +03:00
|
|
|
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;
|
2006-06-22 23:07:30 +04:00
|
|
|
// grow the string
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t oldlength = aDest.Length();
|
2013-05-07 19:52:52 +04:00
|
|
|
if (!aDest.SetLength(oldlength + length, mozilla::fallible_t()))
|
2006-06-22 23:07:30 +04:00
|
|
|
return; // out of memory
|
2005-11-02 10:39:31 +03:00
|
|
|
nsAString::iterator dest;
|
2012-08-22 19:56:38 +04:00
|
|
|
aDest.BeginWriting(dest).advance(int32_t(oldlength));
|
2005-11-02 10:39:31 +03:00
|
|
|
if (aValue < 0) {
|
|
|
|
*dest = '-'; ++dest;
|
|
|
|
}
|
2005-11-02 10:37:16 +03:00
|
|
|
int i;
|
2005-11-02 10:39:31 +03:00
|
|
|
// leading zeros
|
|
|
|
if (intDigits < 1) {
|
|
|
|
*dest = '0'; ++dest;
|
|
|
|
*dest = '.'; ++dest;
|
|
|
|
for (i = 0; i > intDigits; --i) {
|
|
|
|
*dest = '0'; ++dest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// mantissa
|
2013-01-15 16:22:03 +04:00
|
|
|
int firstlen = std::min<size_t>(intDigits, endp - buf);
|
2005-11-02 10:39:31 +03:00
|
|
|
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;
|
2005-11-02 10:37:16 +03:00
|
|
|
}
|
|
|
|
}
|