#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/ures.h"
#include "unicode/uchar.h"
#include "unicode/curramt.h"
#include "ucurrimp.h"
#include "util.h"
#include "digitlst.h"
#include "cmemory.h"
#include "cstring.h"
#include "umutex.h"
#include "uassert.h"
#include "putilimp.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
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormat)
#define kPatternZeroDigit ((UChar)0x0030)
#define kPatternSignificantDigit ((UChar)0x0040)
#define kPatternGroupingSeparator ((UChar)0x002C)
#define kPatternDecimalSeparator ((UChar)0x002E)
#define kPatternPerMill ((UChar)0x2030)
#define kPatternPercent ((UChar)0x0025)
#define kPatternDigit ((UChar)0x0023)
#define kPatternSeparator ((UChar)0x003B)
#define kPatternExponent ((UChar)0x0045)
#define kPatternPlus ((UChar)0x002B)
#define kPatternMinus ((UChar)0x002D)
#define kPatternPadEscape ((UChar)0x002A)
#define kQuote ((UChar)0x0027)
#define kCurrencySign ((UChar)0x00A4)
#define kDefaultPad ((UChar)0x0020)
const int32_t DecimalFormat::kDoubleIntegerDigits = 309;
const int32_t DecimalFormat::kDoubleFractionDigits = 340;
const int32_t DecimalFormat::kMaxScientificIntegerDigits = 8;
const char DecimalFormat::fgNumberPatterns[]="NumberPatterns";
inline int32_t _min(int32_t a, int32_t b) { return (a<b) ? a : b; }
inline int32_t _max(int32_t a, int32_t b) { return (a<b) ? b : a; }
DecimalFormat::DecimalFormat(UErrorCode& status)
: NumberFormat(),
fPosPrefixPattern(0),
fPosSuffixPattern(0),
fNegPrefixPattern(0),
fNegSuffixPattern(0),
fCurrencyChoice(0),
fMultiplier(0),
fGroupingSize(0),
fGroupingSize2(0),
fSymbols(0),
fUseSignificantDigits(FALSE),
fMinSignificantDigits(1),
fMaxSignificantDigits(6),
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),
fUseSignificantDigits(FALSE),
fMinSignificantDigits(1),
fMaxSignificantDigits(6),
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),
fUseSignificantDigits(FALSE),
fMinSignificantDigits(1),
fMaxSignificantDigits(6),
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),
fUseSignificantDigits(FALSE),
fMinSignificantDigits(1),
fMaxSignificantDigits(6),
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),
fUseSignificantDigits(FALSE),
fMinSignificantDigits(1),
fMaxSignificantDigits(6),
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)
{
int32_t len = 0;
UResourceBundle *resource = ures_open(NULL, Locale::getDefault().getName(), &status);
resource = ures_getByKey(resource, fgNumberPatterns, resource, &status);
const UChar *resStr = ures_getStringByIndex(resource, (int32_t)0, &len, &status);
str.setTo(TRUE, resStr, len);
pattern = &str;
ures_close(resource);
}
if (U_FAILURE(status))
{
return;
}
if (pattern->indexOf((UChar)kCurrencySign) >= 0) {
if (symbolsToAdopt == NULL) {
setCurrencyForLocale(uloc_getDefault(), status);
} else {
setCurrencyForSymbols();
}
} else {
setCurrency(NULL, status);
}
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;
UChar c[4];
ucurr_forLocale(locale, c, 4, &ec2);
}
setCurrency(c, ec);
}
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;
fMinSignificantDigits = rhs.fMinSignificantDigits;
fMaxSignificantDigits = rhs.fMaxSignificantDigits;
fUseSignificantDigits = rhs.fUseSignificantDigits;
}
return *this;
}
UBool
DecimalFormat::operator==(const Format& that) const
{
if (this == &that)
return TRUE;
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) &&
fUseSignificantDigits == other->fUseSignificantDigits &&
(!fUseSignificantDigits ||
(fMinSignificantDigits == other->fMinSignificantDigits &&
fMaxSignificantDigits == other->fMaxSignificantDigits)));
}
Format*
DecimalFormat::clone() const
{
return new DecimalFormat(*this);
}
UnicodeString&
DecimalFormat::format(int32_t number,
UnicodeString& appendTo,
FieldPosition& fieldPosition) const
{
return format((int64_t)number, appendTo, fieldPosition);
}
UnicodeString&
DecimalFormat::format(int64_t number,
UnicodeString& appendTo,
FieldPosition& fieldPosition) const
{
DigitList digits;
fieldPosition.setBeginIndex(0);
fieldPosition.setEndIndex(0);
if (fRoundingIncrement != NULL
|| (fMultiplier != 0 && (number > (U_INT64_MAX / fMultiplier)
|| number < (U_INT64_MIN / fMultiplier))))
{
digits.set(((double)number) * fMultiplier,
precision(FALSE),
!fUseExponentialNotation && !areSignificantDigitsUsed());
}
else
{
digits.set(number * fMultiplier, precision(TRUE));
}
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, precision(FALSE),
!fUseExponentialNotation && !areSignificantDigitsUsed());
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);
}
UBool useSigDig = areSignificantDigitsUsed();
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 minFracDig = 0;
if (useSigDig) {
maxIntDig = minIntDig = 1;
minFracDig = getMinimumSignificantDigits() - 1;
} else {
minFracDig = getMinimumFractionDigits();
if (maxIntDig > kMaxScientificIntegerDigits) {
maxIntDig = 1;
if (maxIntDig < minIntDig) {
maxIntDig = minIntDig;
}
}
if (maxIntDig > minIntDig) {
minIntDig = 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 || minFracDig > 0)
? minIntDig : 1;
}
int32_t minimumDigits = minIntDig + minFracDig;
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);
{
int expDig = fMinExponentDigits;
if (fUseExponentialNotation && expDig < 1) {
expDig = 1;
}
for (i=expDigits.fDecimalAt; i<expDig; ++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 sigCount = 0;
int32_t minSigDig = getMinimumSignificantDigits();
int32_t maxSigDig = getMaximumSignificantDigits();
if (!useSigDig) {
minSigDig = 0;
maxSigDig = INT32_MAX;
}
int32_t count = useSigDig ?
_max(1, digits.fDecimalAt) : minIntDig;
if (digits.fDecimalAt > 0 && count < digits.fDecimalAt) {
count = digits.fDecimalAt;
}
int32_t digitIndex = 0; if (count > maxIntDig && maxIntDig >= 0) {
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 &&
sigCount < maxSigDig) {
appendTo += ((UChar32)(digits.fDigits[digitIndex++] + zeroDelta));
++sigCount;
}
else
{
appendTo += (zero);
if (sigCount > 0) {
++sigCount;
}
}
if (isGroupingPosition(i)) {
appendTo.append(*grouping);
}
}
if (fieldPosition.getField() == NumberFormat::kIntegerField)
fieldPosition.setEndIndex(appendTo.length());
UBool fractionPresent = (!isInteger && digitIndex < digits.fCount) ||
(useSigDig ? (sigCount < minSigDig) : (getMinimumFractionDigits() > 0));
if (!fractionPresent && appendTo.length() == sizeBeforeIntegerPart)
appendTo += (zero);
if (fDecimalSeparatorAlwaysShown || fractionPresent)
appendTo += *decimal;
if (fieldPosition.getField() == NumberFormat::kFractionField)
fieldPosition.setBeginIndex(appendTo.length());
count = useSigDig ? INT32_MAX : getMaximumFractionDigits();
if (useSigDig && (sigCount == maxSigDig ||
(sigCount >= minSigDig && digitIndex == digits.fCount))) {
count = 0;
}
for (i=0; i < count; ++i) {
if (!useSigDig && i >= getMinimumFractionDigits() &&
(isInteger || digitIndex >= digits.fCount)) {
break;
}
if (-1-i > (digits.fDecimalAt-1)) {
appendTo += zero;
continue;
}
if (!isInteger && digitIndex < digits.fCount) {
appendTo += ((UChar32)(digits.fDigits[digitIndex++] + zeroDelta));
} else {
appendTo += zero;
}
++sigCount;
if (useSigDig &&
(sigCount == maxSigDig ||
(digitIndex == digits.fCount && sigCount >= minSigDig))) {
break;
}
}
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 {
parse(text, result, parsePosition, FALSE);
}
Formattable& DecimalFormat::parseCurrency(const UnicodeString& text,
Formattable& result,
ParsePosition& pos) const {
parse(text, result, pos, TRUE);
return result;
}
void DecimalFormat::parse(const UnicodeString& text,
Formattable& result,
ParsePosition& parsePosition,
UBool parseCurrency) 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];
UChar curbuf[4];
UChar* currency = parseCurrency ? curbuf : NULL;
DigitList digits;
if (!subparse(text, parsePosition, digits, status, currency)) {
parsePosition.setIndex(backup);
return;
}
if (status[fgStatusInfinite]) {
double inf = uprv_getInfinity();
result.setDouble(digits.fIsPositive ? inf : -inf);
}
else {
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);
}
else { result.setDouble(((double)n) / mult);
}
}
else if (digits.fitsIntoInt64(isParseIntegerOnly())) {
int64_t n = digits.getInt64();
if (n % mult == 0) {
result.setInt64(n / mult);
}
else { result.setDouble(((double)n) / mult);
}
}
else {
result.setDouble(digits.getDouble() / mult);
}
}
if (parseCurrency) {
UErrorCode ec = U_ZERO_ERROR;
Formattable n(result);
result.adoptObject(new CurrencyAmount(n, curbuf, ec));
U_ASSERT(U_SUCCESS(ec)); }
}
UBool DecimalFormat::subparse(const UnicodeString& text, ParsePosition& parsePosition,
DigitList& digits, UBool* status,
UChar* currency) 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, currency);
int32_t negMatch = compareAffix(text, position, TRUE, TRUE, currency);
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);
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, currency);
}
if (negMatch >= 0) {
negMatch = compareAffix(text, position, TRUE, FALSE, currency);
}
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,
UChar* currency) const {
if (fCurrencyChoice != NULL || currency != NULL) {
if (isPrefix) {
return compareComplexAffix(isNegative ? *fNegPrefixPattern : *fPosPrefixPattern,
text, pos, currency);
} else {
return compareComplexAffix(isNegative ? *fNegSuffixPattern : *fPosSuffixPattern,
text, pos, currency);
}
}
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,
UChar* currency) const {
U_ASSERT(currency != NULL ||
(fCurrencyChoice != NULL && *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 (currency != NULL) {
UErrorCode ec = U_ZERO_ERROR;
const char* loc = getLocaleID(ULOC_VALID_LOCALE, ec);
if (U_FAILURE(ec) || loc == NULL || *loc == 0) {
loc = fSymbols->getLocale().getName();
ec = U_ZERO_ERROR;
}
ParsePosition ppos(pos);
UChar curr[4];
uprv_parseCurrency(loc, text, ppos, curr, ec);
if (U_SUCCESS(ec) && ppos.getIndex() != pos) {
u_strcpy(currency, curr);
pos = ppos.getIndex();
} else {
pos = -1;
}
} else {
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 (symbolsToAdopt == NULL) {
return; }
UBool sameSymbols = FALSE;
if (fSymbols != NULL) {
sameSymbols = (UBool)(getConstSymbol(DecimalFormatSymbols::kCurrencySymbol) ==
symbolsToAdopt->getConstSymbol(DecimalFormatSymbols::kCurrencySymbol) &&
getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol) ==
symbolsToAdopt->getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol));
delete fSymbols;
}
fSymbols = symbolsToAdopt;
if (!sameSymbols) {
setCurrencyForSymbols();
}
expandAffixes();
}
void
DecimalFormat::setDecimalFormatSymbols(const DecimalFormatSymbols& symbols)
{
adoptDecimalFormatSymbols(new DecimalFormatSymbols(symbols));
}
void
DecimalFormat::setCurrencyForSymbols() {
UErrorCode ec = U_ZERO_ERROR;
const UChar* c = NULL;
const char* loc = fSymbols->getLocale().getName();
UChar intlCurrencySymbol[4];
ucurr_forLocale(loc, intlCurrencySymbol, 4, &ec);
UnicodeString currencySymbol;
uprv_getStaticCurrencyName(intlCurrencySymbol, loc, currencySymbol, ec);
if (U_SUCCESS(ec)
&& getConstSymbol(DecimalFormatSymbols::kCurrencySymbol) == currencySymbol
&& getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol) == intlCurrencySymbol)
{
c = intlCurrencySymbol;
}
ec = U_ZERO_ERROR; setCurrency(c, ec);
}
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() const {
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() const {
return fRoundingMode;
}
void DecimalFormat::setRoundingMode(ERoundingMode roundingMode) {
fRoundingMode = roundingMode;
}
int32_t DecimalFormat::getFormatWidth() const {
return fFormatWidth;
}
void DecimalFormat::setFormatWidth(int32_t width) {
fFormatWidth = (width > 0) ? width : 0;
}
UnicodeString DecimalFormat::getPadCharacterString() const {
return fPad;
}
void DecimalFormat::setPadCharacter(const UnicodeString &padChar) {
if (padChar.length() > 0) {
fPad = padChar.char32At(0);
}
else {
fPad = kDefaultPad;
}
}
DecimalFormat::EPadPosition DecimalFormat::getPadPosition() const {
return fPadPosition;
}
void DecimalFormat::setPadPosition(EPadPosition padPos) {
fPadPosition = padPos;
}
UBool DecimalFormat::isScientificNotation() {
return fUseExponentialNotation;
}
void DecimalFormat::setScientificNotation(UBool useScientific) {
fUseExponentialNotation = useScientific;
}
int8_t DecimalFormat::getMinimumExponentDigits() const {
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, sigDigit = kPatternSignificantDigit;
UnicodeString digit, group;
int32_t i;
int32_t roundingDecimalPos = 0; UnicodeString roundingDigits;
int32_t padPos = (fFormatWidth > 0) ? fPadPosition : -1;
UnicodeString padSpec;
UBool useSigDig = areSignificantDigitsUsed();
if (localized) {
digit.append(getConstSymbol(DecimalFormatSymbols::kDigitSymbol));
group.append(getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
zero = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
if (useSigDig) {
sigDigit = getConstSymbol(DecimalFormatSymbols::kSignificantDigitSymbol).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() ? _max(0, fGroupingSize) : 0;
if (g > 0 && fGroupingSize2 > 0 && fGroupingSize2 != fGroupingSize) {
g += fGroupingSize2;
}
int32_t maxDig = 0, minDig = 0, maxSigDig = 0;
if (useSigDig) {
minDig = getMinimumSignificantDigits();
maxDig = maxSigDig = getMaximumSignificantDigits();
} else {
minDig = getMinimumIntegerDigits();
maxDig = getMaximumIntegerDigits();
}
if (fUseExponentialNotation) {
if (maxDig > kMaxScientificIntegerDigits) {
maxDig = 1;
}
} else if (useSigDig) {
maxDig = _max(maxDig, g+1);
} else {
maxDig = _max(_max(g, getMinimumIntegerDigits()),
roundingDecimalPos) + 1;
}
for (i = maxDig; i > 0; --i) {
if (!fUseExponentialNotation && i<maxDig &&
isGroupingPosition(i)) {
result.append(group);
}
if (useSigDig) {
if (maxSigDig >= i && i > (maxSigDig - minDig)) {
result.append(sigDigit);
} else {
result.append(digit);
}
} else {
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<=minDig) {
result.append(zero);
} else {
result.append(digit);
}
}
}
if (!useSigDig) {
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);
++maxDig;
--add;
if (add>1 && isGroupingPosition(maxDig)) {
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; UChar32 sigDigit = kPatternSignificantDigit; 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);
sigDigit = getConstSymbol(DecimalFormatSymbols::kSignificantDigitSymbol).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, sigDigitCount = 0;
int8_t groupingCount = -1;
int8_t groupingCount2 = -1;
int32_t padPos = -1;
UChar32 padChar = 0;
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; ) {
ch = pattern.char32At(pos);
switch (subpart) {
case 0: if (pattern.compare(pos, digitLen, digit) == 0) {
if (zeroDigitCount > 0 || sigDigitCount > 0) {
++digitRightCount;
} else {
++digitLeftCount;
}
if (groupingCount >= 0 && decimalPos < 0) {
++groupingCount;
}
pos += digitLen;
} else if ((ch >= zeroDigit && ch <= nineDigit) ||
ch == sigDigit) {
if (digitRightCount > 0) {
debug("Unexpected '0'")
status = U_UNEXPECTED_TOKEN;
syntaxError(pattern,pos,parseError);
return;
}
if (ch == sigDigit) {
++sigDigitCount;
} else {
++zeroDigitCount;
if (ch != zeroDigit && roundingPos < 0) {
roundingPos = digitLeftCount + zeroDigitCount;
}
if (roundingPos >= 0) {
roundingInc.append((char)(ch - zeroDigit + '0'));
}
}
if (groupingCount >= 0 && decimalPos < 0) {
++groupingCount;
}
pos += U16_LENGTH(ch);
} 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;
}
pos += exponent.length();
if (pos < patLen
&& pattern.compare(pos, plus.length(), plus) == 0) {
expSignAlways = TRUE;
pos += plus.length();
}
expDigits = 0;
while (pos < patLen &&
pattern.char32At(pos) == zeroDigit) {
++expDigits;
pos += U16_LENGTH(zeroDigit);
}
if (((digitLeftCount + zeroDigitCount) < 1 &&
(sigDigitCount + digitRightCount) < 1) ||
(sigDigitCount > 0 && digitLeftCount > 0) ||
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) ||
!pattern.compare(pos, groupSepLen, groupingSeparator) ||
!pattern.compare(pos, decimalSepLen, decimalSeparator) ||
(ch >= zeroDigit && ch <= nineDigit) ||
ch == sigDigit) {
if (subpart == 1) { subpart = 0; sub0Start = pos; continue;
} else {
status = U_UNQUOTED_SPECIAL;
syntaxError(pattern,pos,parseError);
return;
}
} else if (ch == kCurrencySign) {
affix->append(kQuote); U_ASSERT(U16_LENGTH(kCurrencySign) == 1);
if ((pos+1) < pattern.length() && pattern[pos+1] == kCurrencySign) {
affix->append(kCurrencySign);
++pos; }
isCurrency = TRUE;
} else if (ch == kQuote) {
U_ASSERT(U16_LENGTH(kQuote) == 1);
++pos;
if (pos < pattern.length() && pattern[pos] == kQuote) {
affix->append(kQuote); } 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); affix->append(kPatternPercent); multiplier = 100;
pos += percent.length();
break;
} 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); affix->append(kPatternPerMill); multiplier = 1000;
pos += perMill.length();
break;
} 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);
break;
} else if (pattern.compare(pos, minus.length(), minus) == 0) {
affix->append(kQuote); affix->append(kPatternMinus);
pos += minus.length();
break;
} else if (pattern.compare(pos, plus.length(), plus) == 0) {
affix->append(kQuote); affix->append(kPatternPlus);
pos += plus.length();
break;
}
affix->append(ch);
pos += U16_LENGTH(ch);
break;
case 3: case 4: if (ch == kQuote) {
++pos;
if (pos < pattern.length() && pattern[pos] == kQuote) {
affix->append(kQuote); } else {
subpart -= 2; continue;
}
}
affix->append(ch);
pos += U16_LENGTH(ch);
break;
}
}
if (sub0Limit == 0) {
sub0Limit = pattern.length();
}
if (sub2Limit == 0) {
sub2Limit = pattern.length();
}
if (zeroDigitCount == 0 && sigDigitCount == 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 && sigDigitCount == 0) ||
(decimalPos >= 0 &&
(sigDigitCount > 0 ||
decimalPos < digitLeftCount ||
decimalPos > (digitLeftCount + zeroDigitCount))) ||
groupingCount == 0 || groupingCount2 == 0 ||
(sigDigitCount > 0 && zeroDigitCount > 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;
int32_t digitTotalCount = digitLeftCount + zeroDigitCount + digitRightCount;
int32_t effectiveDecimalPos = decimalPos >= 0 ? decimalPos : digitTotalCount;
UBool isSigDig = (sigDigitCount > 0);
setSignificantDigitsUsed(isSigDig);
if (isSigDig) {
setMinimumSignificantDigits(sigDigitCount);
setMaximumSignificantDigits(sigDigitCount + digitRightCount);
} else {
int32_t minInt = effectiveDecimalPos - digitLeftCount;
setMinimumIntegerDigits(minInt);
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(_min(newValue, kDoubleIntegerDigits));
}
void DecimalFormat::setMinimumIntegerDigits(int32_t newValue) {
NumberFormat::setMinimumIntegerDigits(_min(newValue, kDoubleIntegerDigits));
}
void DecimalFormat::setMaximumFractionDigits(int32_t newValue) {
NumberFormat::setMaximumFractionDigits(_min(newValue, kDoubleFractionDigits));
}
void DecimalFormat::setMinimumFractionDigits(int32_t newValue) {
NumberFormat::setMinimumFractionDigits(_min(newValue, kDoubleFractionDigits));
}
int32_t DecimalFormat::getMinimumSignificantDigits() const {
return fMinSignificantDigits;
}
int32_t DecimalFormat::getMaximumSignificantDigits() const {
return fMaxSignificantDigits;
}
void DecimalFormat::setMinimumSignificantDigits(int32_t min) {
if (min < 1) {
min = 1;
}
int32_t max = _max(fMaxSignificantDigits, min);
fMinSignificantDigits = min;
fMaxSignificantDigits = max;
}
void DecimalFormat::setMaximumSignificantDigits(int32_t max) {
if (max < 1) {
max = 1;
}
U_ASSERT(fMinSignificantDigits >= 1);
int32_t min = _min(fMinSignificantDigits, max);
fMinSignificantDigits = min;
fMaxSignificantDigits = max;
}
UBool DecimalFormat::areSignificantDigitsUsed() const {
return fUseSignificantDigits;
}
void DecimalFormat::setSignificantDigitsUsed(UBool useSignificantDigits) {
fUseSignificantDigits = useSignificantDigits;
}
void DecimalFormat::setCurrency(const UChar* theCurrency, UErrorCode& ec) {
UBool isCurr = (theCurrency && *theCurrency);
double rounding = 0.0;
int32_t frac = 0;
if (fIsCurrencyFormat && isCurr) {
rounding = ucurr_getRoundingIncrement(theCurrency, &ec);
frac = ucurr_getDefaultFractionDigits(theCurrency, &ec);
}
NumberFormat::setCurrency(theCurrency, ec);
if (U_FAILURE(ec)) return;
if (fIsCurrencyFormat) {
if (isCurr) {
setRoundingIncrement(rounding);
setMinimumFractionDigits(frac);
setMaximumFractionDigits(frac);
}
expandAffixes();
}
}
void DecimalFormat::setCurrency(const UChar* theCurrency) {
UErrorCode ec = U_ZERO_ERROR;
setCurrency(theCurrency, ec);
}
void DecimalFormat::getEffectiveCurrency(UChar* result, UErrorCode& ) const {
const UChar* c = getCurrency();
if (*c == 0) {
const UnicodeString &intl =
fSymbols->getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol);
c = intl.getBuffer(); }
u_strncpy(result, c, 3);
result[3] = 0;
}
int32_t
DecimalFormat::precision(UBool isIntegral) const {
if (areSignificantDigitsUsed()) {
return getMaximumSignificantDigits();
} else if (fUseExponentialNotation) {
return getMinimumIntegerDigits() + getMaximumFractionDigits();
} else {
return isIntegral ? 0 : getMaximumFractionDigits();
}
}
U_NAMESPACE_END
#endif