This source file includes following definitions.
- bufferLengthForStringDecimal
- bufferLengthForStringExponential
- toStringDecimal
- toStringExponential
#include "config.h"
#include "DecimalNumber.h"
namespace WTF {
unsigned DecimalNumber::bufferLengthForStringDecimal() const
{
unsigned length = 0;
if (m_exponent < 0) {
if (m_sign)
++length;
length += 2;
length += -m_exponent - 1;
length += m_precision;
return length;
}
unsigned digitsBeforeDecimalPoint = m_exponent + 1;
if (m_precision <= digitsBeforeDecimalPoint) {
if (m_sign)
++length;
length += m_precision;
length += digitsBeforeDecimalPoint - m_precision;
return length;
}
if (m_sign)
++length;
length += digitsBeforeDecimalPoint;
++length;
length += m_precision - digitsBeforeDecimalPoint;
return length;
}
unsigned DecimalNumber::bufferLengthForStringExponential() const
{
unsigned length = 0;
if (m_sign)
++length;
++length;
if (m_precision > 1) {
++length;
length += m_precision - 1;
}
length += 2;
int exponent = (m_exponent >= 0) ? m_exponent : -m_exponent;
if (exponent >= 100)
++length;
if (exponent >= 10)
++length;
++length;
return length;
}
unsigned DecimalNumber::toStringDecimal(LChar* buffer, unsigned bufferLength) const
{
ASSERT_UNUSED(bufferLength, bufferLength >= bufferLengthForStringDecimal());
ASSERT(m_precision);
LChar* next = buffer;
if (m_exponent < 0) {
unsigned zeros = -m_exponent - 1;
if (m_sign)
*next++ = '-';
*next++ = '0';
*next++ = '.';
for (unsigned i = 0; i < zeros; ++i)
*next++ = '0';
for (unsigned i = 0; i < m_precision; ++i)
*next++ = m_significand[i];
return next - buffer;
}
unsigned digitsBeforeDecimalPoint = m_exponent + 1;
if (m_precision <= digitsBeforeDecimalPoint) {
if (m_sign)
*next++ = '-';
for (unsigned i = 0; i < m_precision; ++i)
*next++ = m_significand[i];
for (unsigned i = 0; i < (digitsBeforeDecimalPoint - m_precision); ++i)
*next++ = '0';
return next - buffer;
}
if (m_sign)
*next++ = '-';
for (unsigned i = 0; i < digitsBeforeDecimalPoint; ++i)
*next++ = m_significand[i];
*next++ = '.';
for (unsigned i = digitsBeforeDecimalPoint; i < m_precision; ++i)
*next++ = m_significand[i];
return next - buffer;
}
unsigned DecimalNumber::toStringExponential(LChar* buffer, unsigned bufferLength) const
{
ASSERT_UNUSED(bufferLength, bufferLength >= bufferLengthForStringExponential());
ASSERT(m_precision);
LChar* next = buffer;
if (m_sign)
*next++ = '-';
*next++ = m_significand[0];
if (m_precision > 1) {
*next++ = '.';
for (unsigned i = 1; i < m_precision; ++i)
*next++ = m_significand[i];
}
*next++ = 'e';
int exponent;
if (m_exponent >= 0) {
*next++ = '+';
exponent = m_exponent;
} else {
*next++ = '-';
exponent = -m_exponent;
}
if (exponent >= 100)
*next++ = '0' + exponent / 100;
if (exponent >= 10)
*next++ = '0' + (exponent % 100) / 10;
*next++ = '0' + exponent % 10;
return next - buffer;
}
}