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