#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/decimfmt.h"
#include "unicode/choicfmt.h"
#include "unicode/ucurr.h"
#include "unicode/ustring.h"
#include "unicode/dcfmtsym.h"
#include "unicode/resbund.h"
#include "unicode/uchar.h"
#include "uprops.h"
#include "digitlst.h"
#include "cmemory.h"
#include "cstring.h"
#include "umutex.h"
#include "uassert.h"
U_NAMESPACE_BEGIN
#ifdef FMT_DEBUG
#include <stdio.h>
static void debugout(UnicodeString s) {
char buf[2000];
s.extract((int32_t) 0, s.length(), buf);
printf("%s", buf);
}
#define debug(x) printf("%s", x);
#else
#define debugout(x)
#define debug(x)
#endif
const char DecimalFormat::fgClassID = 0;
const UChar DecimalFormat::kPatternZeroDigit = 0x0030 ;
const UChar DecimalFormat::kPatternGroupingSeparator = 0x002C ;
const UChar DecimalFormat::kPatternDecimalSeparator = 0x002E ;
const UChar DecimalFormat::kPatternPerMill = 0x2030;
const UChar DecimalFormat::kPatternPercent = 0x0025 ;
const UChar DecimalFormat::kPatternDigit = 0x0023 ;
const UChar DecimalFormat::kPatternSeparator = 0x003B ;
const UChar DecimalFormat::kPatternExponent = 0x0045 ;
const UChar DecimalFormat::kPatternPlus = 0x002B ;
const UChar DecimalFormat::kPatternMinus = 0x002D ;
const UChar DecimalFormat::kPatternPadEscape = 0x002A ;
const UChar DecimalFormat::kCurrencySign = 0x00A4;
const UChar DecimalFormat::kQuote = 0x0027 ;
const int32_t DecimalFormat::kDoubleIntegerDigits = 309;
const int32_t DecimalFormat::kDoubleFractionDigits = 340;
const char DecimalFormat::fgNumberPatterns[]="NumberPatterns";
static const UChar kDefaultPad = 0x0020;
DecimalFormat::DecimalFormat(UErrorCode& status)
: NumberFormat(),
fPosPrefixPattern(0),
fPosSuffixPattern(0),
fNegPrefixPattern(0),
fNegSuffixPattern(0),
fCurrencyChoice(0),
fMultiplier(0),
fGroupingSize(0),
fGroupingSize2(0),
fSymbols(0),
fMinExponentDigits(0),
fRoundingIncrement(0),
fPad(0),
fFormatWidth(0)
{
UParseError parseError;
construct(status, parseError);
}
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
UErrorCode& status)
: NumberFormat(),
fPosPrefixPattern(0),
fPosSuffixPattern(0),
fNegPrefixPattern(0),
fNegSuffixPattern(0),
fCurrencyChoice(0),
fMultiplier(0),
fGroupingSize(0),
fGroupingSize2(0),
fSymbols(0),
fMinExponentDigits(0),
fRoundingIncrement(0),
fPad(0),
fFormatWidth(0)
{
UParseError parseError;
construct(status, parseError, &pattern);
}
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
DecimalFormatSymbols* symbolsToAdopt,
UErrorCode& status)
: NumberFormat(),
fPosPrefixPattern(0),
fPosSuffixPattern(0),
fNegPrefixPattern(0),
fNegSuffixPattern(0),
fCurrencyChoice(0),
fMultiplier(0),
fGroupingSize(0),
fGroupingSize2(0),
fSymbols(0),
fMinExponentDigits(0),
fRoundingIncrement(0),
fPad(0),
fFormatWidth(0)
{
UParseError parseError;
if (symbolsToAdopt == NULL)
status = U_ILLEGAL_ARGUMENT_ERROR;
construct(status, parseError, &pattern, symbolsToAdopt);
}
DecimalFormat::DecimalFormat( const UnicodeString& pattern,
DecimalFormatSymbols* symbolsToAdopt,
UParseError& parseErr,
UErrorCode& status)
: NumberFormat(),
fPosPrefixPattern(0),
fPosSuffixPattern(0),
fNegPrefixPattern(0),
fNegSuffixPattern(0),
fCurrencyChoice(0),
fMultiplier(0),
fGroupingSize(0),
fGroupingSize2(0),
fSymbols(0),
fMinExponentDigits(0),
fRoundingIncrement(0),
fPad(0),
fFormatWidth(0)
{
if (symbolsToAdopt == NULL)
status = U_ILLEGAL_ARGUMENT_ERROR;
construct(status,parseErr, &pattern, symbolsToAdopt);
}
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
const DecimalFormatSymbols& symbols,
UErrorCode& status)
: NumberFormat(),
fPosPrefixPattern(0),
fPosSuffixPattern(0),
fNegPrefixPattern(0),
fNegSuffixPattern(0),
fCurrencyChoice(0),
fMultiplier(0),
fGroupingSize(0),
fGroupingSize2(0),
fSymbols(0),
fMinExponentDigits(0),
fRoundingIncrement(0),
fPad(0),
fFormatWidth(0)
{
UParseError parseError;
construct(status, parseError, &pattern, new DecimalFormatSymbols(symbols));
}
void
DecimalFormat::construct(UErrorCode& status,
UParseError& parseErr,
const UnicodeString* pattern,
DecimalFormatSymbols* symbolsToAdopt)
{
fSymbols = symbolsToAdopt; fRoundingIncrement = NULL;
fRoundingDouble = 0.0;
fRoundingMode = kRoundHalfEven;
fPad = kPatternPadEscape;
fPadPosition = kPadBeforePrefix;
if (U_FAILURE(status))
return;
fPosPrefixPattern = fPosSuffixPattern = NULL;
fNegPrefixPattern = fNegSuffixPattern = NULL;
fMultiplier = 1;
fGroupingSize = 3;
fGroupingSize2 = 0;
fDecimalSeparatorAlwaysShown = FALSE;
fIsCurrencyFormat = FALSE;
fUseExponentialNotation = FALSE;
fMinExponentDigits = 0;
if (fSymbols == NULL)
{
fSymbols = new DecimalFormatSymbols(Locale::getDefault(), status);
if (fSymbols == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
}
UnicodeString str;
if (pattern == NULL)
{
ResourceBundle resource((char *)0, Locale::getDefault(), status);
str = resource.get(fgNumberPatterns, status).getStringEx((int32_t)0, status);
pattern = &str;
}
if (U_FAILURE(status))
{
return;
}
if (symbolsToAdopt == NULL) {
setCurrencyForLocale(uloc_getDefault(), status);
} else {
setCurrencyForSymbols();
}
applyPattern(*pattern, FALSE ,parseErr, status);
}
void DecimalFormat::setCurrencyForLocale(const char* locale, UErrorCode& ec) {
const UChar* c = NULL;
if (U_SUCCESS(ec)) {
UErrorCode ec2 = U_ZERO_ERROR;
c = ucurr_forLocale(locale, &ec2);
}
setCurrency(c);
}
DecimalFormat::~DecimalFormat()
{
delete fPosPrefixPattern;
delete fPosSuffixPattern;
delete fNegPrefixPattern;
delete fNegSuffixPattern;
delete fCurrencyChoice;
delete fSymbols;
delete fRoundingIncrement;
}
DecimalFormat::DecimalFormat(const DecimalFormat &source)
: NumberFormat(source),
fPosPrefixPattern(NULL),
fPosSuffixPattern(NULL),
fNegPrefixPattern(NULL),
fNegSuffixPattern(NULL),
fCurrencyChoice(NULL),
fSymbols(NULL),
fRoundingIncrement(NULL)
{
*this = source;
}
static void _copy_us_ptr(UnicodeString** pdest, const UnicodeString* source) {
if (source == NULL) {
delete *pdest;
*pdest = NULL;
} else if (*pdest == NULL) {
*pdest = new UnicodeString(*source);
} else {
**pdest = *source;
}
}
DecimalFormat&
DecimalFormat::operator=(const DecimalFormat& rhs)
{
if(this != &rhs) {
NumberFormat::operator=(rhs);
fPositivePrefix = rhs.fPositivePrefix;
fPositiveSuffix = rhs.fPositiveSuffix;
fNegativePrefix = rhs.fNegativePrefix;
fNegativeSuffix = rhs.fNegativeSuffix;
_copy_us_ptr(&fPosPrefixPattern, rhs.fPosPrefixPattern);
_copy_us_ptr(&fPosSuffixPattern, rhs.fPosSuffixPattern);
_copy_us_ptr(&fNegPrefixPattern, rhs.fNegPrefixPattern);
_copy_us_ptr(&fNegSuffixPattern, rhs.fNegSuffixPattern);
if (rhs.fCurrencyChoice == 0) {
delete fCurrencyChoice;
fCurrencyChoice = 0;
} else {
fCurrencyChoice = (ChoiceFormat*) rhs.fCurrencyChoice->clone();
}
if(rhs.fRoundingIncrement == NULL) {
delete fRoundingIncrement;
fRoundingIncrement = NULL;
}
else if(fRoundingIncrement == NULL) {
fRoundingIncrement = new DigitList(*rhs.fRoundingIncrement);
}
else {
*fRoundingIncrement = *rhs.fRoundingIncrement;
}
fRoundingDouble = rhs.fRoundingDouble;
fMultiplier = rhs.fMultiplier;
fGroupingSize = rhs.fGroupingSize;
fGroupingSize2 = rhs.fGroupingSize2;
fDecimalSeparatorAlwaysShown = rhs.fDecimalSeparatorAlwaysShown;
if(fSymbols == NULL)
fSymbols = new DecimalFormatSymbols(*rhs.fSymbols);
else
*fSymbols = *rhs.fSymbols;
fUseExponentialNotation = rhs.fUseExponentialNotation;
fExponentSignAlwaysShown = rhs.fExponentSignAlwaysShown;
fIsCurrencyFormat = rhs.fIsCurrencyFormat;
fMinExponentDigits = rhs.fMinExponentDigits;
fFormatWidth = rhs.fFormatWidth;
fPad = rhs.fPad;
fPadPosition = rhs.fPadPosition;
}
return *this;
}
UBool
DecimalFormat::operator==(const Format& that) const
{
if (this == &that)
return TRUE;
if (getDynamicClassID() != that.getDynamicClassID())
return FALSE;
const DecimalFormat* other = (DecimalFormat*)&that;
#ifdef FMT_DEBUG
UBool first = TRUE;
if (!NumberFormat::operator==(that)) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("NumberFormat::!=");
}
if (!((fPosPrefixPattern == other->fPosPrefixPattern && fPositivePrefix == other->fPositivePrefix)
|| (fPosPrefixPattern != 0 && other->fPosPrefixPattern != 0 &&
*fPosPrefixPattern == *other->fPosPrefixPattern))) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("Pos Prefix !=");
}
if (!((fPosSuffixPattern == other->fPosSuffixPattern && fPositiveSuffix == other->fPositiveSuffix)
|| (fPosSuffixPattern != 0 && other->fPosSuffixPattern != 0 &&
*fPosSuffixPattern == *other->fPosSuffixPattern))) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("Pos Suffix !=");
}
if (!((fNegPrefixPattern == other->fNegPrefixPattern && fNegativePrefix == other->fNegativePrefix)
|| (fNegPrefixPattern != 0 && other->fNegPrefixPattern != 0 &&
*fNegPrefixPattern == *other->fNegPrefixPattern))) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("Neg Prefix ");
if (fNegPrefixPattern == NULL) {
debug("NULL(");
debugout(fNegativePrefix);
debug(")");
} else {
debugout(*fNegPrefixPattern);
}
debug(" != ");
if (other->fNegPrefixPattern == NULL) {
debug("NULL(");
debugout(other->fNegativePrefix);
debug(")");
} else {
debugout(*other->fNegPrefixPattern);
}
}
if (!((fNegSuffixPattern == other->fNegSuffixPattern && fNegativeSuffix == other->fNegativeSuffix)
|| (fNegSuffixPattern != 0 && other->fNegSuffixPattern != 0 &&
*fNegSuffixPattern == *other->fNegSuffixPattern))) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("Neg Suffix ");
if (fNegSuffixPattern == NULL) {
debug("NULL(");
debugout(fNegativeSuffix);
debug(")");
} else {
debugout(*fNegSuffixPattern);
}
debug(" != ");
if (other->fNegSuffixPattern == NULL) {
debug("NULL(");
debugout(other->fNegativeSuffix);
debug(")");
} else {
debugout(*other->fNegSuffixPattern);
}
}
if (!((fRoundingIncrement == other->fRoundingIncrement) || (fRoundingIncrement != NULL &&
other->fRoundingIncrement != NULL &&
*fRoundingIncrement == *other->fRoundingIncrement))) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("Rounding Increment !=");
}
if (fMultiplier != other->fMultiplier) {
if (first) { printf("[ "); first = FALSE; }
printf("Multiplier %ld != %ld", fMultiplier, other->fMultiplier);
}
if (fGroupingSize != other->fGroupingSize) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
printf("Grouping Size %ld != %ld", fGroupingSize, other->fGroupingSize);
}
if (fGroupingSize2 != other->fGroupingSize2) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
printf("Secondary Grouping Size %ld != %ld", fGroupingSize2, other->fGroupingSize2);
}
if (fDecimalSeparatorAlwaysShown != other->fDecimalSeparatorAlwaysShown) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
printf("Dec Sep Always %d != %d", fDecimalSeparatorAlwaysShown, other->fDecimalSeparatorAlwaysShown);
}
if (fUseExponentialNotation != other->fUseExponentialNotation) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("Use Exp !=");
}
if (!(!fUseExponentialNotation ||
fMinExponentDigits != other->fMinExponentDigits)) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("Exp Digits !=");
}
if (*fSymbols != *(other->fSymbols)) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("Symbols !=");
}
if (!first) { printf(" ]"); }
#endif
return (NumberFormat::operator==(that) &&
((fPosPrefixPattern == other->fPosPrefixPattern && fPositivePrefix == other->fPositivePrefix)
|| (fPosPrefixPattern != 0 && other->fPosPrefixPattern != 0 &&
*fPosPrefixPattern == *other->fPosPrefixPattern)) &&
((fPosSuffixPattern == other->fPosSuffixPattern && fPositiveSuffix == other->fPositiveSuffix)
|| (fPosSuffixPattern != 0 && other->fPosSuffixPattern != 0 &&
*fPosSuffixPattern == *other->fPosSuffixPattern)) &&
((fNegPrefixPattern == other->fNegPrefixPattern && fNegativePrefix == other->fNegativePrefix)
|| (fNegPrefixPattern != 0 && other->fNegPrefixPattern != 0 &&
*fNegPrefixPattern == *other->fNegPrefixPattern)) &&
((fNegSuffixPattern == other->fNegSuffixPattern && fNegativeSuffix == other->fNegativeSuffix)
|| (fNegSuffixPattern != 0 && other->fNegSuffixPattern != 0 &&
*fNegSuffixPattern == *other->fNegSuffixPattern)) &&
((fRoundingIncrement == other->fRoundingIncrement) || (fRoundingIncrement != NULL &&
other->fRoundingIncrement != NULL &&
*fRoundingIncrement == *other->fRoundingIncrement)) &&
fMultiplier == other->fMultiplier &&
fGroupingSize == other->fGroupingSize &&
fGroupingSize2 == other->fGroupingSize2 &&
fDecimalSeparatorAlwaysShown == other->fDecimalSeparatorAlwaysShown &&
fUseExponentialNotation == other->fUseExponentialNotation &&
(!fUseExponentialNotation ||
fMinExponentDigits == other->fMinExponentDigits) &&
*fSymbols == *(other->fSymbols));
}
Format*
DecimalFormat::clone() const
{
return new DecimalFormat(*this);
}
UnicodeString&
DecimalFormat::format(int32_t number,
UnicodeString& appendTo,
FieldPosition& fieldPosition) const
{
DigitList digits;
fieldPosition.setBeginIndex(0);
fieldPosition.setEndIndex(0);
if (fRoundingIncrement != NULL
|| (fMultiplier != 0 && (number > (INT32_MAX / fMultiplier)
|| number < (INT32_MIN / fMultiplier))))
{
digits.set(((double)number) * fMultiplier,
fUseExponentialNotation ?
getMinimumIntegerDigits() + getMaximumFractionDigits() : 0,
!fUseExponentialNotation);
}
else
{
digits.set(number * fMultiplier,
fUseExponentialNotation ?
getMinimumIntegerDigits() + getMaximumFractionDigits() : 0);
}
return subformat(appendTo, fieldPosition, digits, TRUE);
}
UnicodeString&
DecimalFormat::format( double number,
UnicodeString& appendTo,
FieldPosition& fieldPosition) const
{
fieldPosition.setBeginIndex(0);
fieldPosition.setEndIndex(0);
if (uprv_isNaN(number))
{
if (fieldPosition.getField() == NumberFormat::kIntegerField)
fieldPosition.setBeginIndex(appendTo.length());
appendTo += getConstSymbol(DecimalFormatSymbols::kNaNSymbol);
if (fieldPosition.getField() == NumberFormat::kIntegerField)
fieldPosition.setEndIndex(appendTo.length());
addPadding(appendTo, fieldPosition, 0, 0);
return appendTo;
}
UBool isNegative = uprv_isNegative(number);
number *= fMultiplier;
if (fRoundingIncrement != NULL) {
if (isNegative) number = -number;
number = fRoundingDouble
* round(number / fRoundingDouble, fRoundingMode, isNegative);
if (isNegative)
number = -number;
}
if (uprv_isInfinite(number))
{
int32_t prefixLen = appendAffix(appendTo, number, isNegative, TRUE);
if (fieldPosition.getField() == NumberFormat::kIntegerField)
fieldPosition.setBeginIndex(appendTo.length());
appendTo += getConstSymbol(DecimalFormatSymbols::kInfinitySymbol);
if (fieldPosition.getField() == NumberFormat::kIntegerField)
fieldPosition.setEndIndex(appendTo.length());
int32_t suffixLen = appendAffix(appendTo, number, isNegative, FALSE);
addPadding(appendTo, fieldPosition, prefixLen, suffixLen);
return appendTo;
}
DigitList digits;
digits.set(number, fUseExponentialNotation ?
getMinimumIntegerDigits() + getMaximumFractionDigits() :
getMaximumFractionDigits(),
!fUseExponentialNotation);
return subformat(appendTo, fieldPosition, digits, FALSE);
}
double DecimalFormat::round(double a, ERoundingMode mode, UBool isNegative) {
switch (mode) {
case kRoundCeiling:
return isNegative ? uprv_floor(a) : uprv_ceil(a);
case kRoundFloor:
return isNegative ? uprv_ceil(a) : uprv_floor(a);
case kRoundDown:
return uprv_floor(a);
case kRoundUp:
return uprv_ceil(a);
case kRoundHalfEven:
{
double f = uprv_floor(a);
if ((a - f) != 0.5) {
return uprv_floor(a + 0.5);
}
double g = f / 2.0;
return (g == uprv_floor(g)) ? f : (f + 1.0);
}
case kRoundHalfDown:
return ((a - uprv_floor(a)) <= 0.5) ? uprv_floor(a) : uprv_ceil(a);
case kRoundHalfUp:
return ((a - uprv_floor(a)) < 0.5) ? uprv_floor(a) : uprv_ceil(a);
}
return 1.0;
}
UnicodeString&
DecimalFormat::format( const Formattable& obj,
UnicodeString& appendTo,
FieldPosition& fieldPosition,
UErrorCode& status) const
{
return NumberFormat::format(obj, appendTo, fieldPosition, status);
}
UBool DecimalFormat::isGroupingPosition(int32_t pos) const {
UBool result = FALSE;
if (isGroupingUsed() && (pos > 0) && (fGroupingSize > 0)) {
if ((fGroupingSize2 > 0) && (pos > fGroupingSize)) {
result = ((pos - fGroupingSize) % fGroupingSize2) == 0;
} else {
result = pos % fGroupingSize == 0;
}
}
return result;
}
UnicodeString&
DecimalFormat::subformat(UnicodeString& appendTo,
FieldPosition& fieldPosition,
DigitList& digits,
UBool isInteger) const
{
UChar32 zero = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
int32_t zeroDelta = zero - '0'; const UnicodeString *grouping = &getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol);
const UnicodeString *decimal;
if(fIsCurrencyFormat) {
decimal = &getConstSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol);
} else {
decimal = &getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
}
int32_t maxIntDig = getMaximumIntegerDigits();
int32_t minIntDig = getMinimumIntegerDigits();
if (digits.isZero())
{
digits.fDecimalAt = digits.fCount = 0; }
double doubleValue = digits.getDouble();
int32_t prefixLen = appendAffix(appendTo, doubleValue, !digits.fIsPositive, TRUE);
if (fUseExponentialNotation)
{
if (fieldPosition.getField() == NumberFormat::kIntegerField)
{
fieldPosition.setBeginIndex(appendTo.length());
fieldPosition.setEndIndex(-1);
}
else if (fieldPosition.getField() == NumberFormat::kFractionField)
{
fieldPosition.setBeginIndex(-1);
}
int32_t exponent = digits.fDecimalAt;
if (maxIntDig > 1 && maxIntDig != minIntDig) {
exponent = (exponent > 0) ? (exponent - 1) / maxIntDig
: (exponent / maxIntDig) - 1;
exponent *= maxIntDig;
} else {
exponent -= (minIntDig > 0 || getMinimumFractionDigits() > 0)
? minIntDig : 1;
}
int32_t minimumDigits = minIntDig + getMinimumFractionDigits();
int32_t integerDigits = digits.isZero() ? minIntDig :
digits.fDecimalAt - exponent;
int32_t totalDigits = digits.fCount;
if (minimumDigits > totalDigits)
totalDigits = minimumDigits;
if (integerDigits > totalDigits)
totalDigits = integerDigits;
int32_t i;
for (i=0; i<totalDigits; ++i)
{
if (i == integerDigits)
{
if (fieldPosition.getField() == NumberFormat::kIntegerField)
fieldPosition.setEndIndex(appendTo.length());
appendTo += *decimal;
if (fieldPosition.getField() == NumberFormat::kFractionField)
fieldPosition.setBeginIndex(appendTo.length());
}
UChar32 c = (UChar32)((i < digits.fCount) ?
(digits.fDigits[i] + zeroDelta) :
zero);
appendTo += c;
}
if (fieldPosition.getField() == NumberFormat::kIntegerField)
{
if (fieldPosition.getEndIndex() < 0)
fieldPosition.setEndIndex(appendTo.length());
}
else if (fieldPosition.getField() == NumberFormat::kFractionField)
{
if (fieldPosition.getBeginIndex() < 0)
fieldPosition.setBeginIndex(appendTo.length());
fieldPosition.setEndIndex(appendTo.length());
}
appendTo += getConstSymbol(DecimalFormatSymbols::kExponentialSymbol);
if (digits.isZero())
exponent = 0;
if (exponent < 0) {
appendTo += getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
} else if (fExponentSignAlwaysShown) {
appendTo += getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
}
DigitList expDigits;
expDigits.set(exponent);
for (i=expDigits.fDecimalAt; i<fMinExponentDigits; ++i)
appendTo += (zero);
for (i=0; i<expDigits.fDecimalAt; ++i)
{
UChar32 c = (UChar32)((i < expDigits.fCount) ?
(expDigits.fDigits[i] + zeroDelta) : zero);
appendTo += c;
}
}
else {
if (fieldPosition.getField() == NumberFormat::kIntegerField)
fieldPosition.setBeginIndex(appendTo.length());
int32_t count = minIntDig;
int32_t digitIndex = 0; if (digits.fDecimalAt > 0 && count < digits.fDecimalAt)
count = digits.fDecimalAt;
if (count > maxIntDig)
{
count = maxIntDig;
digitIndex = digits.fDecimalAt - count;
}
int32_t sizeBeforeIntegerPart = appendTo.length();
int32_t i;
for (i=count-1; i>=0; --i)
{
if (i < digits.fDecimalAt && digitIndex < digits.fCount)
{
appendTo += ((UChar32)(digits.fDigits[digitIndex++] + zeroDelta));
}
else
{
appendTo += (zero);
}
if (isGroupingPosition(i)) {
appendTo.append(*grouping);
}
}
if (fieldPosition.getField() == NumberFormat::kIntegerField)
fieldPosition.setEndIndex(appendTo.length());
UBool fractionPresent = (getMinimumFractionDigits() > 0) ||
(!isInteger && digitIndex < digits.fCount);
if (!fractionPresent && appendTo.length() == sizeBeforeIntegerPart)
appendTo += (zero);
if (fDecimalSeparatorAlwaysShown || fractionPresent)
appendTo += *decimal;
if (fieldPosition.getField() == NumberFormat::kFractionField)
fieldPosition.setBeginIndex(appendTo.length());
int32_t maxFracDigits = getMaximumFractionDigits();
int32_t negDecimalAt = -digits.fDecimalAt;
for (i=0; i < maxFracDigits; ++i)
{
if (!isInteger && digitIndex < digits.fCount)
{
if (i >= negDecimalAt)
{
appendTo += ((UChar32)(digits.fDigits[digitIndex++] + zeroDelta));
}
else
{
appendTo += zero;
}
}
else
{
if (i >= getMinimumFractionDigits())
break;
appendTo += zero;
}
}
if (fieldPosition.getField() == NumberFormat::kFractionField)
fieldPosition.setEndIndex(appendTo.length());
}
int32_t suffixLen = appendAffix(appendTo, doubleValue, !digits.fIsPositive, FALSE);
addPadding(appendTo, fieldPosition, prefixLen, suffixLen);
return appendTo;
}
void DecimalFormat::addPadding(UnicodeString& appendTo,
FieldPosition& fieldPosition,
int32_t prefixLen,
int32_t suffixLen) const
{
if (fFormatWidth > 0) {
int32_t len = fFormatWidth - appendTo.length();
if (len > 0) {
UnicodeString padding;
for (int32_t i=0; i<len; ++i) {
padding += fPad;
}
switch (fPadPosition) {
case kPadAfterPrefix:
appendTo.insert(prefixLen, padding);
break;
case kPadBeforePrefix:
appendTo.insert(0, padding);
break;
case kPadBeforeSuffix:
appendTo.insert(appendTo.length() - suffixLen, padding);
break;
case kPadAfterSuffix:
appendTo += padding;
break;
}
if (fPadPosition == kPadBeforePrefix ||
fPadPosition == kPadAfterPrefix) {
fieldPosition.setBeginIndex(len + fieldPosition.getBeginIndex());
fieldPosition.setEndIndex(len + fieldPosition.getEndIndex());
}
}
}
}
void
DecimalFormat::parse(const UnicodeString& text,
Formattable& result,
UErrorCode& status) const
{
NumberFormat::parse(text, result, status);
}
void
DecimalFormat::parse(const UnicodeString& text,
Formattable& result,
ParsePosition& parsePosition) const
{
int32_t backup;
int32_t i = backup = parsePosition.getIndex();
if (fFormatWidth > 0 && (fPadPosition == kPadBeforePrefix ||
fPadPosition == kPadAfterPrefix)) {
i = skipPadding(text, i);
}
const UnicodeString *nan = &getConstSymbol(DecimalFormatSymbols::kNaNSymbol);
int32_t nanLen = (text.compare(i, nan->length(), *nan)
? 0 : nan->length());
if (nanLen) {
i += nanLen;
if (fFormatWidth > 0 && (fPadPosition == kPadBeforeSuffix ||
fPadPosition == kPadAfterSuffix)) {
i = skipPadding(text, i);
}
parsePosition.setIndex(i);
result.setDouble(uprv_getNaN());
return;
}
i = backup;
UBool status[fgStatusLength];
DigitList digits;
if (!subparse(text, parsePosition, digits, status)) {
parsePosition.setIndex(backup);
return;
}
if (status[fgStatusInfinite]) {
double inf = uprv_getInfinity();
result.setDouble(digits.fIsPositive ? inf : -inf);
return;
}
int32_t mult = fMultiplier; while (mult % 10 == 0) {
mult /= 10;
--digits.fDecimalAt;
}
if (digits.fitsIntoLong(isParseIntegerOnly())) {
int32_t n = digits.getLong();
if (n % mult == 0) {
result.setLong(n / mult);
return;
}
else { result.setDouble(((double)n) / mult);
return;
}
}
else {
result.setDouble(digits.getDouble() / mult);
return;
}
}
UBool DecimalFormat::subparse(const UnicodeString& text, ParsePosition& parsePosition,
DigitList& digits, UBool* status) const
{
int32_t position = parsePosition.getIndex();
int32_t oldStart = position;
if (fFormatWidth > 0 && fPadPosition == kPadBeforePrefix) {
position = skipPadding(text, position);
}
int32_t posMatch = compareAffix(text, position, FALSE, TRUE);
int32_t negMatch = compareAffix(text, position, TRUE, TRUE);
if (posMatch >= 0 && negMatch >= 0) {
if (posMatch > negMatch) {
negMatch = -1;
} else if (negMatch > posMatch) {
posMatch = -1;
}
}
if (posMatch >= 0) {
position += posMatch;
} else if (negMatch >= 0) {
position += negMatch;
} else {
parsePosition.setErrorIndex(position);
return FALSE;
}
if (fFormatWidth > 0 && fPadPosition == kPadAfterPrefix) {
position = skipPadding(text, position);
}
const UnicodeString *inf = &getConstSymbol(DecimalFormatSymbols::kInfinitySymbol);
int32_t infLen = (text.compare(position, inf->length(), *inf)
? 0 : inf->length());
position += infLen; status[fgStatusInfinite] = (UBool)infLen;
if (!infLen)
{
digits.fDecimalAt = digits.fCount = 0;
UChar32 zero = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
const UnicodeString *decimal;
if(fIsCurrencyFormat) {
decimal = &getConstSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol);
} else {
decimal = &getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
}
const UnicodeString *grouping = &getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol);
const UnicodeString *exponentChar = &getConstSymbol(DecimalFormatSymbols::kExponentialSymbol);
UBool sawDecimal = FALSE;
UBool sawDigit = FALSE;
int32_t backup = -1;
int32_t digit;
int32_t textLength = text.length(); int32_t groupingLen = grouping->length();
int32_t decimalLen = decimal->length();
int32_t digitCount = 0;
for (; position < textLength; )
{
UChar32 ch = text.char32At(position);
digit = ch - zero;
if (digit < 0 || digit > 9)
{
digit = u_charDigitValue(ch);
}
if (digit > 0 && digit <= 9)
{
backup = -1;
sawDigit = TRUE;
++digitCount;
digits.append((char)(digit + '0'));
position += U16_LENGTH(ch);
}
else if (digit == 0)
{
backup = -1;
sawDigit = TRUE;
if (digits.fCount != 0)
{
++digitCount;
digits.append((char)(digit + '0'));
}
else if (sawDecimal)
{
--digits.fDecimalAt;
}
position += U16_LENGTH(ch);
}
else if (!text.compare(position, groupingLen, *grouping) && isGroupingUsed())
{
backup = position;
position += groupingLen;
}
else if (!text.compare(position, decimalLen, *decimal) && !isParseIntegerOnly() && !sawDecimal)
{
digits.fDecimalAt = digitCount; sawDecimal = TRUE;
position += decimalLen;
}
else {
const UnicodeString *tmp;
tmp = &getConstSymbol(DecimalFormatSymbols::kExponentialSymbol);
if (!text.caseCompare(position, tmp->length(), *tmp, U_FOLD_CASE_DEFAULT)) {
int32_t pos = position + tmp->length();
DigitList exponentDigits;
if (pos < textLength)
{
tmp = &getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
if (!text.compare(pos, tmp->length(), *tmp))
{
pos += tmp->length();
}
else {
tmp = &getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
if (!text.compare(pos, tmp->length(), *tmp))
{
pos += tmp->length();
exponentDigits.fIsPositive = FALSE;
}
}
}
while (pos < textLength) {
ch = text[(int32_t)pos];
digit = ch - zero;
if (digit < 0 || digit > 9) {
digit = u_charDigitValue(ch);
}
if (0 <= digit && digit <= 9) {
++pos;
exponentDigits.append((char)(digit + '0'));
} else {
break;
}
}
if (exponentDigits.fCount > 0) {
exponentDigits.fDecimalAt = exponentDigits.fCount;
digits.fDecimalAt += exponentDigits.getLong();
position = pos; }
break; }
else {
break;
}
}
}
if (backup != -1)
{
position = backup;
}
if (!sawDecimal)
{
digits.fDecimalAt += digitCount; }
if (!sawDigit && digitCount == 0) {
parsePosition.setIndex(oldStart);
parsePosition.setErrorIndex(oldStart);
return FALSE;
}
}
if (fFormatWidth > 0 && fPadPosition == kPadBeforeSuffix) {
position = skipPadding(text, position);
}
if (posMatch >= 0) {
posMatch = compareAffix(text, position, FALSE, FALSE);
}
if (negMatch >= 0) {
negMatch = compareAffix(text, position, TRUE, FALSE);
}
if (posMatch >= 0 && negMatch >= 0) {
if (posMatch > negMatch) {
negMatch = -1;
} else if (negMatch > posMatch) {
posMatch = -1;
}
}
if ((posMatch >= 0) == (negMatch >= 0)) {
parsePosition.setErrorIndex(position);
return FALSE;
}
position += (posMatch>=0 ? posMatch : negMatch);
if (fFormatWidth > 0 && fPadPosition == kPadAfterSuffix) {
position = skipPadding(text, position);
}
parsePosition.setIndex(position);
digits.fIsPositive = (posMatch >= 0);
if(parsePosition.getIndex() == oldStart)
{
parsePosition.setErrorIndex(position);
return FALSE;
}
return TRUE;
}
int32_t DecimalFormat::skipPadding(const UnicodeString& text, int32_t position) const {
int32_t padLen = U16_LENGTH(fPad);
while (position < text.length() &&
text.char32At(position) == fPad) {
position += padLen;
}
return position;
}
int32_t DecimalFormat::compareAffix(const UnicodeString& text,
int32_t pos,
UBool isNegative,
UBool isPrefix) const {
if (fCurrencyChoice != NULL) {
if (isPrefix) {
return compareComplexAffix(isNegative ? *fNegPrefixPattern : *fPosPrefixPattern,
text, pos);
} else {
return compareComplexAffix(isNegative ? *fNegSuffixPattern : *fPosSuffixPattern,
text, pos);
}
}
if (isPrefix) {
return compareSimpleAffix(isNegative ? fNegativePrefix : fPositivePrefix,
text, pos);
} else {
return compareSimpleAffix(isNegative ? fNegativeSuffix : fPositiveSuffix,
text, pos);
}
}
int32_t DecimalFormat::compareSimpleAffix(const UnicodeString& affix,
const UnicodeString& input,
int32_t pos) {
int32_t start = pos;
for (int32_t i=0; i<affix.length(); ) {
UChar32 c = affix.char32At(i);
int32_t len = U16_LENGTH(c);
if (uprv_isRuleWhiteSpace(c)) {
UBool literalMatch = FALSE;
while (pos < input.length() &&
input.char32At(pos) == c) {
literalMatch = TRUE;
i += len;
pos += len;
if (i == affix.length()) {
break;
}
c = affix.char32At(i);
len = U16_LENGTH(c);
if (!uprv_isRuleWhiteSpace(c)) {
break;
}
}
i = skipRuleWhiteSpace(affix, i);
int32_t s = pos;
pos = skipUWhiteSpace(input, pos);
if (pos == s && !literalMatch) {
return -1;
}
} else {
if (pos < input.length() &&
input.char32At(pos) == c) {
i += len;
pos += len;
} else {
return -1;
}
}
}
return pos - start;
}
int32_t DecimalFormat::skipRuleWhiteSpace(const UnicodeString& text, int32_t pos) {
while (pos < text.length()) {
UChar32 c = text.char32At(pos);
if (!uprv_isRuleWhiteSpace(c)) {
break;
}
pos += U16_LENGTH(c);
}
return pos;
}
int32_t DecimalFormat::skipUWhiteSpace(const UnicodeString& text, int32_t pos) {
while (pos < text.length()) {
UChar32 c = text.char32At(pos);
if (!u_isUWhiteSpace(c)) {
break;
}
pos += U16_LENGTH(c);
}
return pos;
}
int32_t DecimalFormat::compareComplexAffix(const UnicodeString& affixPat,
const UnicodeString& text,
int32_t pos) const {
U_ASSERT(fCurrencyChoice != NULL);
U_ASSERT(*getCurrency() != 0);
for (int32_t i=0; i<affixPat.length() && pos >= 0; ) {
UChar32 c = affixPat.char32At(i);
i += U16_LENGTH(c);
if (c == kQuote) {
U_ASSERT(i <= affixPat.length());
c = affixPat.char32At(i);
i += U16_LENGTH(c);
const UnicodeString* affix = NULL;
switch (c) {
case kCurrencySign: {
UBool intl = i<affixPat.length() &&
affixPat.char32At(i) == kCurrencySign;
if (intl) {
++i;
pos = match(text, pos, getCurrency());
} else {
ParsePosition ppos(pos);
Formattable result;
fCurrencyChoice->parse(text, result, ppos);
pos = (ppos.getIndex() == pos) ? -1 : ppos.getIndex();
}
continue;
}
case kPatternPercent:
affix = &getConstSymbol(DecimalFormatSymbols::kPercentSymbol);
break;
case kPatternPerMill:
affix = &getConstSymbol(DecimalFormatSymbols::kPerMillSymbol);
break;
case kPatternPlus:
affix = &getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
break;
case kPatternMinus:
affix = &getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
break;
default:
break;
}
if (affix != NULL) {
pos = match(text, pos, *affix);
continue;
}
}
pos = match(text, pos, c);
if (uprv_isRuleWhiteSpace(c)) {
i = skipRuleWhiteSpace(affixPat, i);
}
}
return pos;
}
int32_t DecimalFormat::match(const UnicodeString& text, int32_t pos, UChar32 ch) {
if (uprv_isRuleWhiteSpace(ch)) {
int32_t s = pos;
pos = skipUWhiteSpace(text, pos);
if (pos == s) {
return -1;
}
return pos;
}
return (pos >= 0 && text.char32At(pos) == ch) ?
(pos + U16_LENGTH(ch)) : -1;
}
int32_t DecimalFormat::match(const UnicodeString& text, int32_t pos, const UnicodeString& str) {
for (int32_t i=0; i<str.length() && pos >= 0; ) {
UChar32 ch = str.char32At(i);
i += U16_LENGTH(ch);
if (uprv_isRuleWhiteSpace(ch)) {
i = skipRuleWhiteSpace(str, i);
}
pos = match(text, pos, ch);
}
return pos;
}
const DecimalFormatSymbols*
DecimalFormat::getDecimalFormatSymbols() const
{
return fSymbols;
}
void
DecimalFormat::adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt)
{
if (fSymbols != NULL)
delete fSymbols;
fSymbols = symbolsToAdopt;
setCurrencyForSymbols();
expandAffixes();
}
void
DecimalFormat::setDecimalFormatSymbols(const DecimalFormatSymbols& symbols)
{
adoptDecimalFormatSymbols(new DecimalFormatSymbols(symbols));
}
void
DecimalFormat::setCurrencyForSymbols() {
UErrorCode ec = U_ZERO_ERROR;
DecimalFormatSymbols def(fSymbols->getLocale(), ec);
if (getConstSymbol(DecimalFormatSymbols::kCurrencySymbol) ==
def.getConstSymbol(DecimalFormatSymbols::kCurrencySymbol) &&
getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol) ==
def.getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol)
) {
setCurrencyForLocale(fSymbols->getLocale().getName(), ec);
} else {
setCurrency(NULL); }
}
UnicodeString&
DecimalFormat::getPositivePrefix(UnicodeString& result) const
{
result = fPositivePrefix;
return result;
}
void
DecimalFormat::setPositivePrefix(const UnicodeString& newValue)
{
fPositivePrefix = newValue;
delete fPosPrefixPattern;
fPosPrefixPattern = 0;
}
UnicodeString&
DecimalFormat::getNegativePrefix(UnicodeString& result) const
{
result = fNegativePrefix;
return result;
}
void
DecimalFormat::setNegativePrefix(const UnicodeString& newValue)
{
fNegativePrefix = newValue;
delete fNegPrefixPattern;
fNegPrefixPattern = 0;
}
UnicodeString&
DecimalFormat::getPositiveSuffix(UnicodeString& result) const
{
result = fPositiveSuffix;
return result;
}
void
DecimalFormat::setPositiveSuffix(const UnicodeString& newValue)
{
fPositiveSuffix = newValue;
delete fPosSuffixPattern;
fPosSuffixPattern = 0;
}
UnicodeString&
DecimalFormat::getNegativeSuffix(UnicodeString& result) const
{
result = fNegativeSuffix;
return result;
}
void
DecimalFormat::setNegativeSuffix(const UnicodeString& newValue)
{
fNegativeSuffix = newValue;
delete fNegSuffixPattern;
fNegSuffixPattern = 0;
}
int32_t DecimalFormat::getMultiplier() const
{
return fMultiplier;
}
void
DecimalFormat::setMultiplier(int32_t newValue)
{
fMultiplier = newValue;
}
double DecimalFormat::getRoundingIncrement() {
return fRoundingDouble;
}
void DecimalFormat::setRoundingIncrement(double newValue) {
if (newValue > 0.0) {
if (fRoundingIncrement == NULL) {
fRoundingIncrement = new DigitList();
}
fRoundingIncrement->set((int32_t)newValue);
fRoundingDouble = newValue;
} else {
delete fRoundingIncrement;
fRoundingIncrement = NULL;
fRoundingDouble = 0.0;
}
}
DecimalFormat::ERoundingMode DecimalFormat::getRoundingMode() {
return fRoundingMode;
}
void DecimalFormat::setRoundingMode(ERoundingMode roundingMode) {
fRoundingMode = roundingMode;
}
int32_t DecimalFormat::getFormatWidth() {
return fFormatWidth;
}
void DecimalFormat::setFormatWidth(int32_t width) {
fFormatWidth = (width > 0) ? width : 0;
}
UnicodeString DecimalFormat::getPadCharacterString() {
return fPad;
}
void DecimalFormat::setPadCharacter(const UnicodeString &padChar) {
if (padChar.length() > 0) {
fPad = padChar.char32At(0);
}
else {
fPad = kDefaultPad;
}
}
DecimalFormat::EPadPosition DecimalFormat::getPadPosition() {
return fPadPosition;
}
void DecimalFormat::setPadPosition(EPadPosition padPos) {
fPadPosition = padPos;
}
UBool DecimalFormat::isScientificNotation() {
return fUseExponentialNotation;
}
void DecimalFormat::setScientificNotation(UBool useScientific) {
fUseExponentialNotation = useScientific;
if (fUseExponentialNotation && fMinExponentDigits < 1) {
fMinExponentDigits = 1;
}
}
int8_t DecimalFormat::getMinimumExponentDigits() {
return fMinExponentDigits;
}
void DecimalFormat::setMinimumExponentDigits(int8_t minExpDig) {
fMinExponentDigits = (int8_t)((minExpDig > 0) ? minExpDig : 1);
}
UBool DecimalFormat::isExponentSignAlwaysShown() {
return fExponentSignAlwaysShown;
}
void DecimalFormat::setExponentSignAlwaysShown(UBool expSignAlways) {
fExponentSignAlwaysShown = expSignAlways;
}
int32_t
DecimalFormat::getGroupingSize() const
{
return fGroupingSize;
}
void
DecimalFormat::setGroupingSize(int32_t newValue)
{
fGroupingSize = newValue;
}
int32_t
DecimalFormat::getSecondaryGroupingSize() const
{
return fGroupingSize2;
}
void
DecimalFormat::setSecondaryGroupingSize(int32_t newValue)
{
fGroupingSize2 = newValue;
}
UBool
DecimalFormat::isDecimalSeparatorAlwaysShown() const
{
return fDecimalSeparatorAlwaysShown;
}
void
DecimalFormat::setDecimalSeparatorAlwaysShown(UBool newValue)
{
fDecimalSeparatorAlwaysShown = newValue;
}
UnicodeString&
DecimalFormat::toPattern(UnicodeString& result) const
{
return toPattern(result, FALSE);
}
UnicodeString&
DecimalFormat::toLocalizedPattern(UnicodeString& result) const
{
return toPattern(result, TRUE);
}
void DecimalFormat::expandAffixes() {
if (fPosPrefixPattern != 0) {
expandAffix(*fPosPrefixPattern, fPositivePrefix, 0, FALSE);
}
if (fPosSuffixPattern != 0) {
expandAffix(*fPosSuffixPattern, fPositiveSuffix, 0, FALSE);
}
if (fNegPrefixPattern != 0) {
expandAffix(*fNegPrefixPattern, fNegativePrefix, 0, FALSE);
}
if (fNegSuffixPattern != 0) {
expandAffix(*fNegSuffixPattern, fNegativeSuffix, 0, FALSE);
}
#ifdef FMT_DEBUG
UnicodeString s;
s.append("[")
.append(*fPosPrefixPattern).append("|").append(*fPosSuffixPattern)
.append(";") .append(*fNegPrefixPattern).append("|").append(*fNegSuffixPattern)
.append("]->[")
.append(fPositivePrefix).append("|").append(fPositiveSuffix)
.append(";") .append(fNegativePrefix).append("|").append(fNegativeSuffix)
.append("]\n");
debugout(s);
#endif
}
void DecimalFormat::expandAffix(const UnicodeString& pattern,
UnicodeString& affix,
double number,
UBool doFormat) const {
affix.remove();
for (int i=0; i<pattern.length(); ) {
UChar32 c = pattern.char32At(i);
i += U16_LENGTH(c);
if (c == kQuote) {
c = pattern.char32At(i);
i += U16_LENGTH(c);
switch (c) {
case kCurrencySign: {
UBool intl = i<pattern.length() &&
pattern.char32At(i) == kCurrencySign;
if (intl) {
++i;
}
const UChar* currencyUChars = getCurrency();
if (currencyUChars[0] != 0) {
UErrorCode ec = U_ZERO_ERROR;
if(intl) {
affix += currencyUChars;
} else {
int32_t len;
UBool isChoiceFormat;
const UChar* s = ucurr_getName(currencyUChars, fSymbols->getLocale().getName(),
UCURR_SYMBOL_NAME, &isChoiceFormat, &len, &ec);
if (isChoiceFormat) {
if (!doFormat) {
if (fCurrencyChoice == NULL) {
ChoiceFormat* fmt = new ChoiceFormat(s, ec);
if (U_SUCCESS(ec)) {
umtx_lock(NULL);
if (fCurrencyChoice == NULL) {
((DecimalFormat*)this)->fCurrencyChoice = fmt;
fmt = NULL;
}
umtx_unlock(NULL);
delete fmt;
}
}
affix.append(kCurrencySign);
} else {
if (fCurrencyChoice != NULL) {
FieldPosition pos(0); if (number < 0) {
number = -number;
}
fCurrencyChoice->format(number, affix, pos);
} else {
affix += currencyUChars;
}
}
continue;
}
affix += UnicodeString(s, len);
}
} else {
if(intl) {
affix += getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol);
} else {
affix += getConstSymbol(DecimalFormatSymbols::kCurrencySymbol);
}
}
break;
}
case kPatternPercent:
affix += getConstSymbol(DecimalFormatSymbols::kPercentSymbol);
break;
case kPatternPerMill:
affix += getConstSymbol(DecimalFormatSymbols::kPerMillSymbol);
break;
case kPatternPlus:
affix += getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
break;
case kPatternMinus:
affix += getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
break;
default:
affix.append(c);
break;
}
}
else {
affix.append(c);
}
}
}
int32_t DecimalFormat::appendAffix(UnicodeString& buf, double number,
UBool isNegative, UBool isPrefix) const {
if (fCurrencyChoice != 0) {
const UnicodeString* affixPat = 0;
if (isPrefix) {
affixPat = isNegative ? fNegPrefixPattern : fPosPrefixPattern;
} else {
affixPat = isNegative ? fNegSuffixPattern : fPosSuffixPattern;
}
UnicodeString affixBuf;
expandAffix(*affixPat, affixBuf, number, TRUE);
buf.append(affixBuf);
return affixBuf.length();
}
const UnicodeString* affix = NULL;
if (isPrefix) {
affix = isNegative ? &fNegativePrefix : &fPositivePrefix;
} else {
affix = isNegative ? &fNegativeSuffix : &fPositiveSuffix;
}
buf.append(*affix);
return affix->length();
}
void DecimalFormat::appendAffixPattern(UnicodeString& appendTo,
const UnicodeString* affixPattern,
const UnicodeString& expAffix,
UBool localized) const {
if (affixPattern == 0) {
appendAffixPattern(appendTo, expAffix, localized);
} else {
int i;
for (int pos=0; pos<affixPattern->length(); pos=i) {
i = affixPattern->indexOf(kQuote, pos);
if (i < 0) {
UnicodeString s;
affixPattern->extractBetween(pos, affixPattern->length(), s);
appendAffixPattern(appendTo, s, localized);
break;
}
if (i > pos) {
UnicodeString s;
affixPattern->extractBetween(pos, i, s);
appendAffixPattern(appendTo, s, localized);
}
UChar32 c = affixPattern->char32At(++i);
++i;
if (c == kQuote) {
appendTo.append(c).append(c);
} else if (c == kCurrencySign &&
i<affixPattern->length() &&
affixPattern->char32At(i) == kCurrencySign) {
++i;
appendTo.append(c).append(c);
} else if (localized) {
switch (c) {
case kPatternPercent:
appendTo += getConstSymbol(DecimalFormatSymbols::kPercentSymbol);
break;
case kPatternPerMill:
appendTo += getConstSymbol(DecimalFormatSymbols::kPerMillSymbol);
break;
case kPatternPlus:
appendTo += getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
break;
case kPatternMinus:
appendTo += getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
break;
default:
appendTo.append(c);
}
} else {
appendTo.append(c);
}
}
}
}
void
DecimalFormat::appendAffixPattern(UnicodeString& appendTo,
const UnicodeString& affix,
UBool localized) const {
UBool needQuote;
if(localized) {
needQuote = affix.indexOf(getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol)) >= 0
|| affix.indexOf(getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol)) >= 0
|| affix.indexOf(getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol)) >= 0
|| affix.indexOf(getConstSymbol(DecimalFormatSymbols::kPercentSymbol)) >= 0
|| affix.indexOf(getConstSymbol(DecimalFormatSymbols::kPerMillSymbol)) >= 0
|| affix.indexOf(getConstSymbol(DecimalFormatSymbols::kDigitSymbol)) >= 0
|| affix.indexOf(getConstSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol)) >= 0
|| affix.indexOf(getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol)) >= 0
|| affix.indexOf(getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol)) >= 0
|| affix.indexOf(kCurrencySign) >= 0;
}
else {
needQuote = affix.indexOf(kPatternZeroDigit) >= 0
|| affix.indexOf(kPatternGroupingSeparator) >= 0
|| affix.indexOf(kPatternDecimalSeparator) >= 0
|| affix.indexOf(kPatternPercent) >= 0
|| affix.indexOf(kPatternPerMill) >= 0
|| affix.indexOf(kPatternDigit) >= 0
|| affix.indexOf(kPatternSeparator) >= 0
|| affix.indexOf(kPatternExponent) >= 0
|| affix.indexOf(kPatternPlus) >= 0
|| affix.indexOf(kPatternMinus) >= 0
|| affix.indexOf(kCurrencySign) >= 0;
}
if (needQuote)
appendTo += (UChar)0x0027 ;
if (affix.indexOf((UChar)0x0027 ) < 0)
appendTo += affix;
else {
for (int32_t j = 0; j < affix.length(); ) {
UChar32 c = affix.char32At(j);
j += U16_LENGTH(c);
appendTo += c;
if (c == 0x0027 )
appendTo += c;
}
}
if (needQuote)
appendTo += (UChar)0x0027 ;
}
UnicodeString&
DecimalFormat::toPattern(UnicodeString& result, UBool localized) const
{
result.remove();
UChar32 zero;
UnicodeString digit, group;
int32_t i;
int32_t roundingDecimalPos = 0; UnicodeString roundingDigits;
int32_t padPos = (fFormatWidth > 0) ? fPadPosition : -1;
UnicodeString padSpec;
if (localized) {
digit.append(getConstSymbol(DecimalFormatSymbols::kDigitSymbol));
group.append(getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
zero = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
}
else {
digit.append((UChar)kPatternDigit);
group.append((UChar)kPatternGroupingSeparator);
zero = (UChar32)kPatternZeroDigit;
}
if (fFormatWidth > 0) {
if (localized) {
padSpec.append(getConstSymbol(DecimalFormatSymbols::kPadEscapeSymbol));
}
else {
padSpec.append((UChar)kPatternPadEscape);
}
padSpec.append(fPad);
}
if (fRoundingIncrement != NULL) {
for(i=0; i<fRoundingIncrement->fCount; ++i) {
roundingDigits.append((UChar)fRoundingIncrement->fDigits[i]);
}
roundingDecimalPos = fRoundingIncrement->fDecimalAt;
}
for (int32_t part=0; part<2; ++part) {
if (padPos == kPadBeforePrefix) {
result.append(padSpec);
}
appendAffixPattern(result,
(part==0 ? fPosPrefixPattern : fNegPrefixPattern),
(part==0 ? fPositivePrefix : fNegativePrefix),
localized);
if (padPos == kPadAfterPrefix && ! padSpec.isEmpty()) {
result.append(padSpec);
}
int32_t sub0Start = result.length();
int32_t g = isGroupingUsed() ? uprv_max(0, fGroupingSize) : 0;
if (g > 0 && fGroupingSize2 > 0 && fGroupingSize2 != fGroupingSize) {
g += fGroupingSize2;
}
int32_t maxIntDig = fUseExponentialNotation ? getMaximumIntegerDigits() :
(uprv_max(uprv_max(g, getMinimumIntegerDigits()),
roundingDecimalPos) + 1);
for (i = maxIntDig; i > 0; --i) {
if (!fUseExponentialNotation && i<maxIntDig &&
isGroupingPosition(i)) {
result.append(group);
}
if (! roundingDigits.isEmpty()) {
int32_t pos = roundingDecimalPos - i;
if (pos >= 0 && pos < roundingDigits.length()) {
result.append((UChar) (roundingDigits.char32At(pos) - kPatternZeroDigit + zero));
continue;
}
}
if (i<=getMinimumIntegerDigits()) {
result.append(zero);
}
else {
result.append(digit);
}
}
if (getMaximumFractionDigits() > 0 || fDecimalSeparatorAlwaysShown) {
if (localized) {
result += getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
}
else {
result.append((UChar)kPatternDecimalSeparator);
}
}
int32_t pos = roundingDecimalPos;
for (i = 0; i < getMaximumFractionDigits(); ++i) {
if (! roundingDigits.isEmpty() && pos < roundingDigits.length()) {
if (pos < 0) {
result.append(zero);
}
else {
result.append((UChar)(roundingDigits.char32At(pos) - kPatternZeroDigit + zero));
}
++pos;
continue;
}
if (i<getMinimumFractionDigits()) {
result.append(zero);
}
else {
result.append(digit);
}
}
if (fUseExponentialNotation) {
if (localized) {
result += getConstSymbol(DecimalFormatSymbols::kExponentialSymbol);
}
else {
result.append((UChar)kPatternExponent);
}
if (fExponentSignAlwaysShown) {
if (localized) {
result += getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
}
else {
result.append((UChar)kPatternPlus);
}
}
for (i=0; i<fMinExponentDigits; ++i) {
result.append(zero);
}
}
if (! padSpec.isEmpty() && !fUseExponentialNotation) {
int32_t add = fFormatWidth - result.length() + sub0Start
- ((part == 0)
? fPositivePrefix.length() + fPositiveSuffix.length()
: fNegativePrefix.length() + fNegativeSuffix.length());
while (add > 0) {
result.insert(sub0Start, digit);
++maxIntDig;
--add;
if (add>1 && isGroupingPosition(maxIntDig)) {
result.insert(sub0Start, group);
--add;
}
}
}
if (fPadPosition == kPadBeforeSuffix && ! padSpec.isEmpty()) {
result.append(padSpec);
}
if (part == 0) {
appendAffixPattern(result, fPosSuffixPattern, fPositiveSuffix, localized);
if (fPadPosition == kPadAfterSuffix && ! padSpec.isEmpty()) {
result.append(padSpec);
}
UBool isDefault = FALSE;
if ((fNegSuffixPattern == fPosSuffixPattern && fNegativeSuffix == fPositiveSuffix)
|| (fNegSuffixPattern != 0 && fPosSuffixPattern != 0 &&
*fNegSuffixPattern == *fPosSuffixPattern))
{
if (fNegPrefixPattern != NULL && fPosPrefixPattern != NULL)
{
int32_t length = fPosPrefixPattern->length();
isDefault = fNegPrefixPattern->length() == (length+2) &&
(*fNegPrefixPattern)[(int32_t)0] == kQuote &&
(*fNegPrefixPattern)[(int32_t)1] == kPatternMinus &&
fNegPrefixPattern->compare(2, length, *fPosPrefixPattern, 0, length) == 0;
}
if (!isDefault &&
fNegPrefixPattern == NULL && fPosPrefixPattern == NULL)
{
int32_t length = fPositivePrefix.length();
isDefault = fNegativePrefix.length() == (length+1) &&
fNegativePrefix.compare(getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol)) == 0 &&
fNegativePrefix.compare(1, length, fPositivePrefix, 0, length) == 0;
}
}
if (isDefault) {
break; } else {
if (localized) {
result += getConstSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol);
}
else {
result.append((UChar)kPatternSeparator);
}
}
} else {
appendAffixPattern(result, fNegSuffixPattern, fNegativeSuffix, localized);
if (fPadPosition == kPadAfterSuffix && ! padSpec.isEmpty()) {
result.append(padSpec);
}
}
}
return result;
}
void
DecimalFormat::applyPattern(const UnicodeString& pattern, UErrorCode& status)
{
UParseError parseError;
applyPattern(pattern, FALSE, parseError, status);
}
void
DecimalFormat::applyPattern(const UnicodeString& pattern,
UParseError& parseError,
UErrorCode& status)
{
applyPattern(pattern, FALSE, parseError, status);
}
void
DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status)
{
UParseError parseError;
applyPattern(pattern, TRUE,parseError,status);
}
void
DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern,
UParseError& parseError,
UErrorCode& status)
{
applyPattern(pattern, TRUE,parseError,status);
}
void
DecimalFormat::applyPattern(const UnicodeString& pattern,
UBool localized,
UParseError& parseError,
UErrorCode& status)
{
if (U_FAILURE(status))
{
return;
}
parseError.offset = -1;
parseError.preContext[0] = parseError.postContext[0] = (UChar)0;
UChar32 zeroDigit = kPatternZeroDigit;
UnicodeString groupingSeparator ((UChar)kPatternGroupingSeparator);
UnicodeString decimalSeparator ((UChar)kPatternDecimalSeparator);
UnicodeString percent ((UChar)kPatternPercent);
UnicodeString perMill ((UChar)kPatternPerMill);
UnicodeString digit ((UChar)kPatternDigit);
UnicodeString separator ((UChar)kPatternSeparator);
UnicodeString exponent ((UChar)kPatternExponent);
UnicodeString plus ((UChar)kPatternPlus);
UnicodeString minus ((UChar)kPatternMinus);
UnicodeString padEscape ((UChar)kPatternPadEscape);
if (localized) {
zeroDigit = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
groupingSeparator. remove().append(getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
decimalSeparator. remove().append(getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
percent. remove().append(getConstSymbol(DecimalFormatSymbols::kPercentSymbol));
perMill. remove().append(getConstSymbol(DecimalFormatSymbols::kPerMillSymbol));
digit. remove().append(getConstSymbol(DecimalFormatSymbols::kDigitSymbol));
separator. remove().append(getConstSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol));
exponent. remove().append(getConstSymbol(DecimalFormatSymbols::kExponentialSymbol));
plus. remove().append(getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol));
minus. remove().append(getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol));
padEscape. remove().append(getConstSymbol(DecimalFormatSymbols::kPadEscapeSymbol));
}
UChar nineDigit = (UChar)(zeroDigit + 9);
int32_t digitLen = digit.length();
int32_t groupSepLen = groupingSeparator.length();
int32_t decimalSepLen = decimalSeparator.length();
int32_t pos = 0;
int32_t patLen = pattern.length();
for (int32_t part=0; part<2 && pos<patLen; ++part) {
int32_t subpart = 1, sub0Start = 0, sub0Limit = 0, sub2Limit = 0;
UnicodeString prefix;
UnicodeString suffix;
int32_t decimalPos = -1;
int32_t multiplier = 1;
int32_t digitLeftCount = 0, zeroDigitCount = 0, digitRightCount = 0;
int8_t groupingCount = -1;
int8_t groupingCount2 = -1;
int32_t padPos = -1;
UChar32 padChar;
int32_t roundingPos = -1;
DigitList roundingInc;
int8_t expDigits = -1;
UBool expSignAlways = FALSE;
UBool isCurrency = FALSE;
UnicodeString* affix = &prefix;
int32_t start = pos;
UBool isPartDone = FALSE;
UChar32 ch;
for (; !isPartDone && pos < patLen; pos += UTF_NEED_MULTIPLE_UCHAR(ch)) {
ch = pattern.char32At(pos);
switch (subpart) {
case 0: if (pattern.compare(pos, digitLen, digit) == 0) {
if (zeroDigitCount > 0) {
++digitRightCount;
} else {
++digitLeftCount;
}
if (groupingCount >= 0 && decimalPos < 0) {
++groupingCount;
}
pos += digitLen;
} else if (ch >= zeroDigit && ch <= nineDigit) {
if (digitRightCount > 0) {
debug("Unexpected '0'")
status = U_UNEXPECTED_TOKEN;
syntaxError(pattern,pos,parseError);
return;
}
++zeroDigitCount;
if (groupingCount >= 0 && decimalPos < 0) {
++groupingCount;
}
if (ch != zeroDigit && roundingPos < 0) {
roundingPos = digitLeftCount + zeroDigitCount;
}
if (roundingPos >= 0) {
roundingInc.append((char)(ch - zeroDigit + '0'));
}
pos++;
} else if (pattern.compare(pos, groupSepLen, groupingSeparator) == 0) {
if (decimalPos >= 0) {
debug("Grouping separator after decimal")
status = U_UNEXPECTED_TOKEN;
syntaxError(pattern,pos,parseError);
return;
}
groupingCount2 = groupingCount;
groupingCount = 0;
pos += groupSepLen;
} else if (pattern.compare(pos, decimalSepLen, decimalSeparator) == 0) {
if (decimalPos >= 0) {
debug("Multiple decimal separators")
status = U_MULTIPLE_DECIMAL_SEPARATORS;
syntaxError(pattern,pos,parseError);
return;
}
decimalPos = digitLeftCount + zeroDigitCount + digitRightCount;
pos += decimalSepLen;
} else {
if (pattern.compare(pos, exponent.length(), exponent) == 0) {
if (expDigits >= 0) {
debug("Multiple exponential symbols")
status = U_MULTIPLE_EXPONENTIAL_SYMBOLS;
syntaxError(pattern,pos,parseError);
return;
}
if (groupingCount >= 0) {
debug("Grouping separator in exponential pattern")
status = U_MALFORMED_EXPONENTIAL_PATTERN;
syntaxError(pattern,pos,parseError);
return;
}
if ((pos+1) < patLen
&& pattern.compare((int32_t) (pos+1), plus.length(), plus) == 0)
{
expSignAlways = TRUE;
pos += plus.length();
}
expDigits = 0;
pos += exponent.length() - 1;
while (++pos < patLen &&
pattern[(int32_t) pos] == zeroDigit)
{
++expDigits;
}
if ((digitLeftCount + zeroDigitCount) < 1 ||
expDigits < 1) {
debug("Malformed exponential pattern")
status = U_MALFORMED_EXPONENTIAL_PATTERN;
syntaxError(pattern,pos,parseError);
return;
}
}
subpart = 2; affix = &suffix;
sub0Limit = pos;
continue;
}
break;
case 1: case 2: if (pattern.compare(pos, digitLen, digit) == 0) {
if (subpart == 1) { subpart = 0; sub0Start = pos; continue;
}
pos += digitLen;
} else if (pattern.compare(pos, groupSepLen, groupingSeparator) == 0) {
if (subpart == 1) { subpart = 0; sub0Start = pos; continue;
}
pos += groupSepLen;
} else if (pattern.compare(pos, decimalSepLen, decimalSeparator) == 0) {
if (subpart == 1) { subpart = 0; sub0Start = pos; continue;
}
pos += decimalSepLen;
} else if (ch >= zeroDigit && ch <= nineDigit) {
if (subpart == 1) { subpart = 0; sub0Start = pos; continue;
}
pos++;
} else if (ch == kCurrencySign) {
pos++;
affix->append(kQuote); if (pos < pattern.length() && pattern[pos] == kCurrencySign)
{
affix->append(kCurrencySign);
++pos; }
isCurrency = TRUE;
} else if (ch == kQuote) {
++pos;
if (pos < pattern.length() && pattern[pos] == kQuote) {
affix->append(kQuote); ++pos;
} else {
subpart += 2; continue;
}
} else if (pattern.compare(pos, separator.length(), separator) == 0) {
if (subpart == 1 || part == 1) {
debug("Unexpected separator")
status = U_UNEXPECTED_TOKEN;
syntaxError(pattern,pos,parseError);
return;
}
sub2Limit = pos;
isPartDone = TRUE; pos += separator.length();
break;
} else if (pattern.compare(pos, percent.length(), percent) == 0) {
if (multiplier != 1) {
debug("Too many percent characters")
status = U_MULTIPLE_PERCENT_SYMBOLS;
syntaxError(pattern,pos,parseError);
return;
}
affix->append(kQuote); multiplier = 100;
ch = kPatternPercent; pos += percent.length();
} else if (pattern.compare(pos, perMill.length(), perMill) == 0) {
if (multiplier != 1) {
debug("Too many perMill characters")
status = U_MULTIPLE_PERMILL_SYMBOLS;
syntaxError(pattern,pos,parseError);
return;
}
affix->append(kQuote); multiplier = 1000;
ch = kPatternPerMill; pos += perMill.length();
} else if (pattern.compare(pos, padEscape.length(), padEscape) == 0) {
if (padPos >= 0 || (pos+1) == pattern.length()) { debug("Multiple pad specifiers")
status = U_MULTIPLE_PAD_SPECIFIERS;
syntaxError(pattern,pos,parseError);
return;
}
padPos = pos;
pos += padEscape.length();
padChar = pattern.char32At(pos);
pos += U16_LENGTH(padChar);
continue;
} else if (pattern.compare(pos, minus.length(), minus) == 0) {
affix->append(kQuote); ch = kPatternMinus;
pos += minus.length();
} else if (pattern.compare(pos, plus.length(), plus) == 0) {
affix->append(kQuote); ch = kPatternPlus;
pos += plus.length();
} else {
pos++;
}
affix->append(ch);
break;
case 3: case 4: pos++;
if (ch == kQuote) {
if (pos < pattern.length() && pattern[pos] == kQuote) {
++pos;
affix->append(kQuote); } else {
subpart -= 2; continue;
}
}
affix->append(ch);
break;
}
}
if (sub0Limit == 0) {
sub0Limit = pattern.length();
}
if (sub2Limit == 0) {
sub2Limit = pattern.length();
}
if (zeroDigitCount == 0 && digitLeftCount > 0 && decimalPos >= 0) {
int n = decimalPos;
if (n == 0)
++n; digitRightCount = digitLeftCount - n;
digitLeftCount = n - 1;
zeroDigitCount = 1;
}
if ((decimalPos < 0 && digitRightCount > 0) ||
(decimalPos >= 0 &&
(decimalPos < digitLeftCount ||
decimalPos > (digitLeftCount + zeroDigitCount))) ||
groupingCount == 0 || groupingCount2 == 0 ||
subpart > 2)
{ debug("Syntax error")
status = U_PATTERN_SYNTAX_ERROR;
syntaxError(pattern,pos,parseError);
return;
}
if (padPos >= 0) {
if (padPos == start) {
padPos = kPadBeforePrefix;
} else if (padPos+2 == sub0Start) {
padPos = kPadAfterPrefix;
} else if (padPos == sub0Limit) {
padPos = kPadBeforeSuffix;
} else if (padPos+2 == sub2Limit) {
padPos = kPadAfterSuffix;
} else {
debug("Illegal pad position")
status = U_ILLEGAL_PAD_POSITION;
syntaxError(pattern,pos,parseError);
return;
}
}
if (part == 0) {
delete fPosPrefixPattern;
delete fPosSuffixPattern;
delete fNegPrefixPattern;
delete fNegSuffixPattern;
fPosPrefixPattern = new UnicodeString(prefix);
if (fPosPrefixPattern == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
fPosSuffixPattern = new UnicodeString(suffix);
if (fPosSuffixPattern == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
delete fPosPrefixPattern;
return;
}
fNegPrefixPattern = 0;
fNegSuffixPattern = 0;
fUseExponentialNotation = (expDigits >= 0);
if (fUseExponentialNotation) {
fMinExponentDigits = expDigits;
}
fExponentSignAlwaysShown = expSignAlways;
fIsCurrencyFormat = isCurrency;
int digitTotalCount = digitLeftCount + zeroDigitCount + digitRightCount;
int effectiveDecimalPos = decimalPos >= 0 ? decimalPos : digitTotalCount;
setMinimumIntegerDigits(effectiveDecimalPos - digitLeftCount);
setMaximumIntegerDigits(fUseExponentialNotation
? digitLeftCount + getMinimumIntegerDigits()
: kDoubleIntegerDigits);
setMaximumFractionDigits(decimalPos >= 0
? (digitTotalCount - decimalPos) : 0);
setMinimumFractionDigits(decimalPos >= 0
? (digitLeftCount + zeroDigitCount - decimalPos) : 0);
setGroupingUsed(groupingCount > 0);
fGroupingSize = (groupingCount > 0) ? groupingCount : 0;
fGroupingSize2 = (groupingCount2 > 0 && groupingCount2 != groupingCount)
? groupingCount2 : 0;
fMultiplier = multiplier;
setDecimalSeparatorAlwaysShown(decimalPos == 0
|| decimalPos == digitTotalCount);
if (padPos >= 0) {
fPadPosition = (EPadPosition) padPos;
fFormatWidth = sub0Limit - sub0Start;
fPad = padChar;
} else {
fFormatWidth = 0;
}
if (roundingPos >= 0) {
roundingInc.fDecimalAt = effectiveDecimalPos - roundingPos;
if (fRoundingIncrement != NULL) {
*fRoundingIncrement = roundingInc;
} else {
fRoundingIncrement = new DigitList(roundingInc);
if (fRoundingIncrement == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
delete fPosPrefixPattern;
delete fPosSuffixPattern;
return;
}
}
fRoundingDouble = fRoundingIncrement->getDouble();
fRoundingMode = kRoundHalfEven;
} else {
setRoundingIncrement(0.0);
}
} else {
fNegPrefixPattern = new UnicodeString(prefix);
if (fNegPrefixPattern == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
fNegSuffixPattern = new UnicodeString(suffix);
if (fNegSuffixPattern == 0) {
delete fNegPrefixPattern;
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
}
}
if (pattern.length() == 0) {
delete fNegPrefixPattern;
delete fNegSuffixPattern;
fNegPrefixPattern = NULL;
fNegSuffixPattern = NULL;
if (fPosPrefixPattern != NULL) {
fPosPrefixPattern->remove();
} else {
fPosPrefixPattern = new UnicodeString();
if (fPosPrefixPattern == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
}
if (fPosSuffixPattern != NULL) {
fPosSuffixPattern->remove();
} else {
fPosSuffixPattern = new UnicodeString();
if (fPosSuffixPattern == 0) {
delete fPosPrefixPattern;
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
}
setMinimumIntegerDigits(0);
setMaximumIntegerDigits(kDoubleIntegerDigits);
setMinimumFractionDigits(0);
setMaximumFractionDigits(kDoubleFractionDigits);
fUseExponentialNotation = FALSE;
fIsCurrencyFormat = FALSE;
setGroupingUsed(FALSE);
fGroupingSize = 0;
fGroupingSize2 = 0;
fMultiplier = 1;
setDecimalSeparatorAlwaysShown(FALSE);
fFormatWidth = 0;
setRoundingIncrement(0.0);
}
if (fNegPrefixPattern == NULL ||
(*fNegPrefixPattern == *fPosPrefixPattern
&& *fNegSuffixPattern == *fPosSuffixPattern)) {
_copy_us_ptr(&fNegSuffixPattern, fPosSuffixPattern);
if (fNegPrefixPattern == NULL) {
fNegPrefixPattern = new UnicodeString();
if (fNegPrefixPattern == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
} else {
fNegPrefixPattern->remove();
}
fNegPrefixPattern->append(kQuote).append(kPatternMinus)
.append(*fPosPrefixPattern);
}
#ifdef FMT_DEBUG
UnicodeString s;
s.append("\"").append(pattern).append("\"->");
debugout(s);
#endif
expandAffixes();
if (fFormatWidth > 0) {
fFormatWidth += fPositivePrefix.length() + fPositiveSuffix.length();
}
}
void DecimalFormat::setMaximumIntegerDigits(int32_t newValue) {
NumberFormat::setMaximumIntegerDigits(uprv_min(newValue, kDoubleIntegerDigits));
}
void DecimalFormat::setMinimumIntegerDigits(int32_t newValue) {
NumberFormat::setMinimumIntegerDigits(uprv_min(newValue, kDoubleIntegerDigits));
}
void DecimalFormat::setMaximumFractionDigits(int32_t newValue) {
NumberFormat::setMaximumFractionDigits(uprv_min(newValue, kDoubleFractionDigits));
}
void DecimalFormat::setMinimumFractionDigits(int32_t newValue) {
NumberFormat::setMinimumFractionDigits(uprv_min(newValue, kDoubleFractionDigits));
}
void DecimalFormat::setCurrency(const UChar* theCurrency) {
NumberFormat::setCurrency(theCurrency);
if (fIsCurrencyFormat) {
if (theCurrency && *theCurrency) {
setRoundingIncrement(ucurr_getRoundingIncrement(theCurrency));
int32_t d = ucurr_getDefaultFractionDigits(theCurrency);
setMinimumFractionDigits(d);
setMaximumFractionDigits(d);
}
expandAffixes();
}
}
U_NAMESPACE_END
#endif