blob: 85b9085eefd07c56df7ee01821dfa43cf10cdcbb [file] [log] [blame]
/*
*******************************************************************************
* Copyright (C) 1997-2010, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* File DECIMFMT.CPP
*
* Modification History:
*
* Date Name Description
* 02/19/97 aliu Converted from java.
* 03/20/97 clhuang Implemented with new APIs.
* 03/31/97 aliu Moved isLONG_MIN to DigitList, and fixed it.
* 04/3/97 aliu Rewrote parsing and formatting completely, and
* cleaned up and debugged. Actually works now.
* Implemented NAN and INF handling, for both parsing
* and formatting. Extensive testing & debugging.
* 04/10/97 aliu Modified to compile on AIX.
* 04/16/97 aliu Rewrote to use DigitList, which has been resurrected.
* Changed DigitCount to int per code review.
* 07/09/97 helena Made ParsePosition into a class.
* 08/26/97 aliu Extensive changes to applyPattern; completely
* rewritten from the Java.
* 09/09/97 aliu Ported over support for exponential formats.
* 07/20/98 stephen JDK 1.2 sync up.
* Various instances of '0' replaced with 'NULL'
* Check for grouping size in subFormat()
* Brought subParse() in line with Java 1.2
* Added method appendAffix()
* 08/24/1998 srl Removed Mutex calls. This is not a thread safe class!
* 02/22/99 stephen Removed character literals for EBCDIC safety
* 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
* 06/28/99 stephen Fixed bugs in toPattern().
* 06/29/99 stephen Fixed operator= to copy fFormatWidth, fPad,
* fPadPosition
********************************************************************************
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "fphdlimp.h"
#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 "unicode/currpinf.h"
#include "unicode/plurrule.h"
#include "ucurrimp.h"
#include "charstr.h"
#include "cmemory.h"
#include "util.h"
#include "digitlst.h"
#include "cstring.h"
#include "umutex.h"
#include "uassert.h"
#include "putilimp.h"
#include <math.h>
#include "hash.h"
U_NAMESPACE_BEGIN
/* For currency parsing purose,
* Need to remember all prefix patterns and suffix patterns of
* every currency format pattern,
* including the pattern of default currecny style
* and plural currency style. And the patterns are set through applyPattern.
*/
struct AffixPatternsForCurrency : public UMemory {
// negative prefix pattern
UnicodeString negPrefixPatternForCurrency;
// negative suffix pattern
UnicodeString negSuffixPatternForCurrency;
// positive prefix pattern
UnicodeString posPrefixPatternForCurrency;
// positive suffix pattern
UnicodeString posSuffixPatternForCurrency;
int8_t patternType;
AffixPatternsForCurrency(const UnicodeString& negPrefix,
const UnicodeString& negSuffix,
const UnicodeString& posPrefix,
const UnicodeString& posSuffix,
int8_t type) {
negPrefixPatternForCurrency = negPrefix;
negSuffixPatternForCurrency = negSuffix;
posPrefixPatternForCurrency = posPrefix;
posSuffixPatternForCurrency = posSuffix;
patternType = type;
}
};
/* affix for currency formatting when the currency sign in the pattern
* equals to 3, such as the pattern contains 3 currency sign or
* the formatter style is currency plural format style.
*/
struct AffixesForCurrency : public UMemory {
// negative prefix
UnicodeString negPrefixForCurrency;
// negative suffix
UnicodeString negSuffixForCurrency;
// positive prefix
UnicodeString posPrefixForCurrency;
// positive suffix
UnicodeString posSuffixForCurrency;
int32_t formatWidth;
AffixesForCurrency(const UnicodeString& negPrefix,
const UnicodeString& negSuffix,
const UnicodeString& posPrefix,
const UnicodeString& posSuffix) {
negPrefixForCurrency = negPrefix;
negSuffixForCurrency = negSuffix;
posPrefixForCurrency = posPrefix;
posSuffixForCurrency = posSuffix;
}
};
U_CDECL_BEGIN
/**
* @internal ICU 4.2
*/
static UBool U_CALLCONV decimfmtAffixValueComparator(UHashTok val1, UHashTok val2);
/**
* @internal ICU 4.2
*/
static UBool U_CALLCONV decimfmtAffixPatternValueComparator(UHashTok val1, UHashTok val2);
static UBool
U_CALLCONV decimfmtAffixValueComparator(UHashTok val1, UHashTok val2) {
const AffixesForCurrency* affix_1 =
(AffixesForCurrency*)val1.pointer;
const AffixesForCurrency* affix_2 =
(AffixesForCurrency*)val2.pointer;
return affix_1->negPrefixForCurrency == affix_2->negPrefixForCurrency &&
affix_1->negSuffixForCurrency == affix_2->negSuffixForCurrency &&
affix_1->posPrefixForCurrency == affix_2->posPrefixForCurrency &&
affix_1->posSuffixForCurrency == affix_2->posSuffixForCurrency;
}
static UBool
U_CALLCONV decimfmtAffixPatternValueComparator(UHashTok val1, UHashTok val2) {
const AffixPatternsForCurrency* affix_1 =
(AffixPatternsForCurrency*)val1.pointer;
const AffixPatternsForCurrency* affix_2 =
(AffixPatternsForCurrency*)val2.pointer;
return affix_1->negPrefixPatternForCurrency ==
affix_2->negPrefixPatternForCurrency &&
affix_1->negSuffixPatternForCurrency ==
affix_2->negSuffixPatternForCurrency &&
affix_1->posPrefixPatternForCurrency ==
affix_2->posPrefixPatternForCurrency &&
affix_1->posSuffixPatternForCurrency ==
affix_2->posSuffixPatternForCurrency &&
affix_1->patternType == affix_2->patternType;
}
U_CDECL_END
//#define FMT_DEBUG
#ifdef FMT_DEBUG
#include <stdio.h>
static void debugout(UnicodeString s) {
char buf[2000];
s.extract((int32_t) 0, s.length(), buf);
printf("%s\n", buf);
}
#define debug(x) printf("%s\n", x);
#else
#define debugout(x)
#define debug(x)
#endif
// *****************************************************************************
// class DecimalFormat
// *****************************************************************************
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormat)
// Constants for characters used in programmatic (unlocalized) patterns.
#define kPatternZeroDigit ((UChar)0x0030) /*'0'*/
#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) /*'E'*/
#define kPatternPlus ((UChar)0x002B) /*'+'*/
#define kPatternMinus ((UChar)0x002D) /*'-'*/
#define kPatternPadEscape ((UChar)0x002A) /*'*'*/
#define kQuote ((UChar)0x0027) /*'\''*/
/**
* The CURRENCY_SIGN is the standard Unicode symbol for currency. It
* is used in patterns and substitued with either the currency symbol,
* or if it is doubled, with the international currency symbol. If the
* CURRENCY_SIGN is seen in a pattern, then the decimal separator is
* replaced with the monetary decimal separator.
*/
#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;
/**
* These are the tags we expect to see in normal resource bundle files associated
* with a locale.
*/
const char DecimalFormat::fgNumberPatterns[]="NumberPatterns"; // Deprecated - not used
static const char fgNumberElements[]="NumberElements";
static const char fgLatn[]="latn";
static const char fgPatterns[]="patterns";
static const char fgDecimalFormat[]="decimalFormat";
static const char fgCurrencyFormat[]="currencyFormat";
static const UChar fgTripleCurrencySign[] = {0xA4, 0xA4, 0xA4, 0};
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; }
//------------------------------------------------------------------------------
// Constructs a DecimalFormat instance in the default locale.
DecimalFormat::DecimalFormat(UErrorCode& status) {
init();
UParseError parseError;
construct(status, parseError);
}
//------------------------------------------------------------------------------
// Constructs a DecimalFormat instance with the specified number format
// pattern in the default locale.
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
UErrorCode& status) {
init();
UParseError parseError;
construct(status, parseError, &pattern);
}
//------------------------------------------------------------------------------
// Constructs a DecimalFormat instance with the specified number format
// pattern and the number format symbols in the default locale. The
// created instance owns the symbols.
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
DecimalFormatSymbols* symbolsToAdopt,
UErrorCode& status) {
init();
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) {
init();
if (symbolsToAdopt == NULL)
status = U_ILLEGAL_ARGUMENT_ERROR;
construct(status,parseErr, &pattern, symbolsToAdopt);
}
//------------------------------------------------------------------------------
// Constructs a DecimalFormat instance with the specified number format
// pattern and the number format symbols in the default locale. The
// created instance owns the clone of the symbols.
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
const DecimalFormatSymbols& symbols,
UErrorCode& status) {
init();
UParseError parseError;
construct(status, parseError, &pattern, new DecimalFormatSymbols(symbols));
}
//------------------------------------------------------------------------------
// Constructs a DecimalFormat instance with the specified number format
// pattern, the number format symbols, and the number format style.
// The created instance owns the clone of the symbols.
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
DecimalFormatSymbols* symbolsToAdopt,
NumberFormat::EStyles style,
UErrorCode& status) {
init();
fStyle = style;
UParseError parseError;
construct(status, parseError, &pattern, symbolsToAdopt);
}
//-----------------------------------------------------------------------------
// Common DecimalFormat initialization.
// Put all fields of an uninitialized object into a known state.
// Common code, shared by all constructors.
void
DecimalFormat::init() {
fPosPrefixPattern = 0;
fPosSuffixPattern = 0;
fNegPrefixPattern = 0;
fNegSuffixPattern = 0;
fCurrencyChoice = 0;
fMultiplier = NULL;
fGroupingSize = 0;
fGroupingSize2 = 0;
fDecimalSeparatorAlwaysShown = FALSE;
fSymbols = NULL;
fUseSignificantDigits = FALSE;
fMinSignificantDigits = 1;
fMaxSignificantDigits = 6;
fUseExponentialNotation = FALSE;
fMinExponentDigits = 0;
fExponentSignAlwaysShown = FALSE;
fRoundingIncrement = 0;
fRoundingMode = kRoundHalfEven;
fPad = 0;
fFormatWidth = 0;
fPadPosition = kPadBeforePrefix;
fStyle = NumberFormat::kNumberStyle;
fCurrencySignCount = 0;
fAffixPatternsForCurrency = NULL;
fAffixesForCurrency = NULL;
fPluralAffixesForCurrency = NULL;
fCurrencyPluralInfo = NULL;
}
//------------------------------------------------------------------------------
// Constructs a DecimalFormat instance with the specified number format
// pattern and the number format symbols in the desired locale. The
// created instance owns the symbols.
void
DecimalFormat::construct(UErrorCode& status,
UParseError& parseErr,
const UnicodeString* pattern,
DecimalFormatSymbols* symbolsToAdopt)
{
fSymbols = symbolsToAdopt; // Do this BEFORE aborting on status failure!!!
fRoundingIncrement = NULL;
fRoundingMode = kRoundHalfEven;
fPad = kPatternPadEscape;
fPadPosition = kPadBeforePrefix;
if (U_FAILURE(status))
return;
fPosPrefixPattern = fPosSuffixPattern = NULL;
fNegPrefixPattern = fNegSuffixPattern = NULL;
setMultiplier(1);
fGroupingSize = 3;
fGroupingSize2 = 0;
fDecimalSeparatorAlwaysShown = FALSE;
fUseExponentialNotation = FALSE;
fMinExponentDigits = 0;
if (fSymbols == NULL)
{
fSymbols = new DecimalFormatSymbols(Locale::getDefault(), status);
/* test for NULL */
if (fSymbols == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
}
}
UnicodeString str;
// Uses the default locale's number format pattern if there isn't
// one specified.
if (pattern == NULL)
{
int32_t len = 0;
UResourceBundle *resource = ures_open(NULL, Locale::getDefault().getName(), &status);
resource = ures_getByKey(resource, fgNumberElements, resource, &status);
// TODO : Get the pattern based on the active numbering system for the locale. Right now assumes "latn".
resource = ures_getByKey(resource, fgLatn, resource, &status);
resource = ures_getByKey(resource, fgPatterns, resource, &status);
const UChar *resStr = ures_getStringByKey(resource, fgDecimalFormat, &len, &status);
str.setTo(TRUE, resStr, len);
pattern = &str;
ures_close(resource);
}
if (U_FAILURE(status))
{
return;
}
if (pattern->indexOf((UChar)kCurrencySign) >= 0) {
// If it looks like we are going to use a currency pattern
// then do the time consuming lookup.
setCurrencyForSymbols();
} else {
setCurrencyInternally(NULL, status);
}
const UnicodeString* patternUsed;
UnicodeString currencyPluralPatternForOther;
// apply pattern
if (fStyle == NumberFormat::kPluralCurrencyStyle) {
fCurrencyPluralInfo = new CurrencyPluralInfo(fSymbols->getLocale(), status);
if (U_FAILURE(status)) {
return;
}
// the pattern used in format is not fixed until formatting,
// in which, the number is known and
// will be used to pick the right pattern based on plural count.
// Here, set the pattern as the pattern of plural count == "other".
// For most locale, the patterns are probably the same for all
// plural count. If not, the right pattern need to be re-applied
// during format.
fCurrencyPluralInfo->getCurrencyPluralPattern("other", currencyPluralPatternForOther);
patternUsed = &currencyPluralPatternForOther;
// TODO: not needed?
setCurrencyForSymbols();
} else {
patternUsed = pattern;
}
if (patternUsed->indexOf(kCurrencySign) != -1) {
// initialize for currency, not only for plural format,
// but also for mix parsing
if (fCurrencyPluralInfo == NULL) {
fCurrencyPluralInfo = new CurrencyPluralInfo(fSymbols->getLocale(), status);
if (U_FAILURE(status)) {
return;
}
}
// need it for mix parsing
setupCurrencyAffixPatterns(status);
// expanded affixes for plural names
if (patternUsed->indexOf(fgTripleCurrencySign) != -1) {
setupCurrencyAffixes(*patternUsed, TRUE, TRUE, status);
}
}
applyPatternWithoutExpandAffix(*patternUsed,FALSE, parseErr, status);
// expand affixes
if (fCurrencySignCount != fgCurrencySignCountInPluralFormat) {
expandAffixAdjustWidth(NULL);
}
// If it was a currency format, apply the appropriate rounding by
// resetting the currency. NOTE: this copies fCurrency on top of itself.
if (fCurrencySignCount > fgCurrencySignCountZero) {
setCurrencyInternally(getCurrency(), status);
}
}
void
DecimalFormat::setupCurrencyAffixPatterns(UErrorCode& status) {
if (U_FAILURE(status)) {
return;
}
UParseError parseErr;
fAffixPatternsForCurrency = initHashForAffixPattern(status);
if (U_FAILURE(status)) {
return;
}
// Save the default currency patterns of this locale.
// Here, chose onlyApplyPatternWithoutExpandAffix without
// expanding the affix patterns into affixes.
UnicodeString currencyPattern;
UErrorCode error = U_ZERO_ERROR;
UResourceBundle *resource = ures_open(NULL, fSymbols->getLocale().getName(), &error);
resource = ures_getByKey(resource, fgNumberElements, resource, &error);
// TODO : Get the pattern based on the active numbering system for the locale. Right now assumes "latn".
resource = ures_getByKey(resource, fgLatn, resource, &error);
resource = ures_getByKey(resource, fgPatterns, resource, &error);
int32_t patLen = 0;
const UChar *patResStr = ures_getStringByKey(resource, fgCurrencyFormat, &patLen, &error);
ures_close(resource);
if (U_SUCCESS(error)) {
applyPatternWithoutExpandAffix(UnicodeString(patResStr, patLen), false,
parseErr, status);
AffixPatternsForCurrency* affixPtn = new AffixPatternsForCurrency(
*fNegPrefixPattern,
*fNegSuffixPattern,
*fPosPrefixPattern,
*fPosSuffixPattern,
UCURR_SYMBOL_NAME);
fAffixPatternsForCurrency->put("default", affixPtn, status);
}
// save the unique currency plural patterns of this locale.
Hashtable* pluralPtn = fCurrencyPluralInfo->fPluralCountToCurrencyUnitPattern;
const UHashElement* element = NULL;
int32_t pos = -1;
Hashtable pluralPatternSet;
while ((element = pluralPtn->nextElement(pos)) != NULL) {
const UHashTok valueTok = element->value;
const UnicodeString* value = (UnicodeString*)valueTok.pointer;
const UHashTok keyTok = element->key;
const UnicodeString* key = (UnicodeString*)keyTok.pointer;
if (pluralPatternSet.geti(*value) != 1) {
pluralPatternSet.puti(*value, 1, status);
applyPatternWithoutExpandAffix(*value, false, parseErr, status);
AffixPatternsForCurrency* affixPtn = new AffixPatternsForCurrency(
*fNegPrefixPattern,
*fNegSuffixPattern,
*fPosPrefixPattern,
*fPosSuffixPattern,
UCURR_LONG_NAME);
fAffixPatternsForCurrency->put(*key, affixPtn, status);
}
}
}
void
DecimalFormat::setupCurrencyAffixes(const UnicodeString& pattern,
UBool setupForCurrentPattern,
UBool setupForPluralPattern,
UErrorCode& status) {
if (U_FAILURE(status)) {
return;
}
UParseError parseErr;
if (setupForCurrentPattern) {
if (fAffixesForCurrency) {
deleteHashForAffix(fAffixesForCurrency);
}
fAffixesForCurrency = initHashForAffix(status);
if (U_SUCCESS(status)) {
applyPatternWithoutExpandAffix(pattern, false, parseErr, status);
const PluralRules* pluralRules = fCurrencyPluralInfo->getPluralRules();
StringEnumeration* keywords = pluralRules->getKeywords(status);
if (U_SUCCESS(status)) {
const char* pluralCountCh;
while ((pluralCountCh = keywords->next(NULL, status)) != NULL) {
if ( U_SUCCESS(status) ) {
UnicodeString pluralCount = UnicodeString(pluralCountCh);
expandAffixAdjustWidth(&pluralCount);
AffixesForCurrency* affix = new AffixesForCurrency(
fNegativePrefix, fNegativeSuffix, fPositivePrefix, fPositiveSuffix);
fAffixesForCurrency->put(pluralCount, affix, status);
}
}
}
delete keywords;
}
}
if (U_FAILURE(status)) {
return;
}
if (setupForPluralPattern) {
if (fPluralAffixesForCurrency) {
deleteHashForAffix(fPluralAffixesForCurrency);
}
fPluralAffixesForCurrency = initHashForAffix(status);
if (U_SUCCESS(status)) {
const PluralRules* pluralRules = fCurrencyPluralInfo->getPluralRules();
StringEnumeration* keywords = pluralRules->getKeywords(status);
if (U_SUCCESS(status)) {
const char* pluralCountCh;
while ((pluralCountCh = keywords->next(NULL, status)) != NULL) {
if ( U_SUCCESS(status) ) {
UnicodeString pluralCount = UnicodeString(pluralCountCh);
UnicodeString ptn;
fCurrencyPluralInfo->getCurrencyPluralPattern(pluralCount, ptn);
applyPatternInternally(pluralCount, ptn, false, parseErr, status);
AffixesForCurrency* affix = new AffixesForCurrency(
fNegativePrefix, fNegativeSuffix, fPositivePrefix, fPositiveSuffix);
fPluralAffixesForCurrency->put(pluralCount, affix, status);
}
}
}
delete keywords;
}
}
}
//------------------------------------------------------------------------------
DecimalFormat::~DecimalFormat()
{
delete fPosPrefixPattern;
delete fPosSuffixPattern;
delete fNegPrefixPattern;
delete fNegSuffixPattern;
delete fCurrencyChoice;
delete fMultiplier;
delete fSymbols;
delete fRoundingIncrement;
deleteHashForAffixPattern();
deleteHashForAffix(fAffixesForCurrency);
deleteHashForAffix(fPluralAffixesForCurrency);
delete fCurrencyPluralInfo;
}
//------------------------------------------------------------------------------
// copy constructor
DecimalFormat::DecimalFormat(const DecimalFormat &source) :
NumberFormat(source) {
init();
*this = source;
}
//------------------------------------------------------------------------------
// assignment operator
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();
}
setRoundingIncrement(rhs.getRoundingIncrement());
fRoundingMode = rhs.fRoundingMode;
setMultiplier(rhs.getMultiplier());
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;
/*Bertrand A. D. Update 98.03.17*/
fCurrencySignCount = rhs.fCurrencySignCount;
/*end of Update*/
fMinExponentDigits = rhs.fMinExponentDigits;
/* sfb 990629 */
fFormatWidth = rhs.fFormatWidth;
fPad = rhs.fPad;
fPadPosition = rhs.fPadPosition;
/* end sfb */
fMinSignificantDigits = rhs.fMinSignificantDigits;
fMaxSignificantDigits = rhs.fMaxSignificantDigits;
fUseSignificantDigits = rhs.fUseSignificantDigits;
fFormatPattern = rhs.fFormatPattern;
fStyle = rhs.fStyle;
fCurrencySignCount = rhs.fCurrencySignCount;
if (rhs.fCurrencyPluralInfo) {
delete fCurrencyPluralInfo;
fCurrencyPluralInfo = rhs.fCurrencyPluralInfo->clone();
}
if (rhs.fAffixPatternsForCurrency) {
UErrorCode status = U_ZERO_ERROR;
deleteHashForAffixPattern();
fAffixPatternsForCurrency = initHashForAffixPattern(status);
copyHashForAffixPattern(rhs.fAffixPatternsForCurrency,
fAffixPatternsForCurrency, status);
}
if (rhs.fAffixesForCurrency) {
UErrorCode status = U_ZERO_ERROR;
deleteHashForAffix(fAffixesForCurrency);
fAffixesForCurrency = initHashForAffixPattern(status);
copyHashForAffix(rhs.fAffixesForCurrency, fAffixesForCurrency, status);
}
if (rhs.fPluralAffixesForCurrency) {
UErrorCode status = U_ZERO_ERROR;
deleteHashForAffix(fPluralAffixesForCurrency);
fPluralAffixesForCurrency = initHashForAffixPattern(status);
copyHashForAffix(rhs.fPluralAffixesForCurrency, fPluralAffixesForCurrency, status);
}
}
return *this;
}
//------------------------------------------------------------------------------
UBool
DecimalFormat::operator==(const Format& that) const
{
if (this == &that)
return TRUE;
// NumberFormat::operator== guarantees this cast is safe
const DecimalFormat* other = (DecimalFormat*)&that;
#ifdef FMT_DEBUG
// This code makes it easy to determine why two format objects that should
// be equal aren't.
UBool first = TRUE;
if (!NumberFormat::operator==(that)) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("NumberFormat::!=");
} else {
if (!((fPosPrefixPattern == other->fPosPrefixPattern && // both null
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 && // both null
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 && // both null
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 && // both null
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) // both null
|| (fRoundingIncrement != NULL &&
other->fRoundingIncrement != NULL &&
*fRoundingIncrement == *other->fRoundingIncrement))) {
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
debug("Rounding Increment !=");
}
if (getMultiplier() != other->getMultiplier()) {
if (first) { printf("[ "); first = FALSE; }
printf("Multiplier %ld != %ld", getMultiplier(), other->getMultiplier());
}
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 !=");
}
// TODO Add debug stuff for significant digits here
if (fUseSignificantDigits != other->fUseSignificantDigits) {
debug("fUseSignificantDigits !=");
}
if (fUseSignificantDigits &&
fMinSignificantDigits != other->fMinSignificantDigits) {
debug("fMinSignificantDigits !=");
}
if (fUseSignificantDigits &&
fMaxSignificantDigits != other->fMaxSignificantDigits) {
debug("fMaxSignificantDigits !=");
}
if (!first) { printf(" ]"); }
if (fCurrencySignCount != other->fCurrencySignCount) {
debug("fCurrencySignCount !=");
}
if (fCurrencyPluralInfo == other->fCurrencyPluralInfo) {
debug("fCurrencyPluralInfo == ");
if (fCurrencyPluralInfo == NULL) {
debug("fCurrencyPluralInfo == NULL");
}
}
if (fCurrencyPluralInfo != NULL && other->fCurrencyPluralInfo != NULL &&
*fCurrencyPluralInfo != *(other->fCurrencyPluralInfo)) {
debug("fCurrencyPluralInfo !=");
}
if (fCurrencyPluralInfo != NULL && other->fCurrencyPluralInfo == NULL ||
fCurrencyPluralInfo == NULL && other->fCurrencyPluralInfo != NULL) {
debug("fCurrencyPluralInfo one NULL, the other not");
}
if (fCurrencyPluralInfo == NULL && other->fCurrencyPluralInfo == NULL) {
debug("fCurrencyPluralInfo == ");
}
}
#endif
return (NumberFormat::operator==(that) &&
((fCurrencySignCount == fgCurrencySignCountInPluralFormat) ?
(fAffixPatternsForCurrency->equals(*other->fAffixPatternsForCurrency)) :
(((fPosPrefixPattern == other->fPosPrefixPattern && // both null
fPositivePrefix == other->fPositivePrefix)
|| (fPosPrefixPattern != 0 && other->fPosPrefixPattern != 0 &&
*fPosPrefixPattern == *other->fPosPrefixPattern)) &&
((fPosSuffixPattern == other->fPosSuffixPattern && // both null
fPositiveSuffix == other->fPositiveSuffix)
|| (fPosSuffixPattern != 0 && other->fPosSuffixPattern != 0 &&
*fPosSuffixPattern == *other->fPosSuffixPattern)) &&
((fNegPrefixPattern == other->fNegPrefixPattern && // both null
fNegativePrefix == other->fNegativePrefix)
|| (fNegPrefixPattern != 0 && other->fNegPrefixPattern != 0 &&
*fNegPrefixPattern == *other->fNegPrefixPattern)) &&
((fNegSuffixPattern == other->fNegSuffixPattern && // both null
fNegativeSuffix == other->fNegativeSuffix)
|| (fNegSuffixPattern != 0 && other->fNegSuffixPattern != 0 &&
*fNegSuffixPattern == *other->fNegSuffixPattern)))) &&
((fRoundingIncrement == other->fRoundingIncrement) // both null
|| (fRoundingIncrement != NULL &&
other->fRoundingIncrement != NULL &&
*fRoundingIncrement == *other->fRoundingIncrement)) &&
getMultiplier() == other->getMultiplier() &&
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)) &&
fCurrencySignCount == other->fCurrencySignCount &&
((fCurrencyPluralInfo == other->fCurrencyPluralInfo &&
fCurrencyPluralInfo == NULL) ||
(fCurrencyPluralInfo != NULL && other->fCurrencyPluralInfo != NULL &&
*fCurrencyPluralInfo == *(other->fCurrencyPluralInfo))));
}
//------------------------------------------------------------------------------
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(int32_t number,
UnicodeString& appendTo,
FieldPositionIterator* posIter,
UErrorCode& status) const
{
return format((int64_t)number, appendTo, posIter, status);
}
//------------------------------------------------------------------------------
UnicodeString&
DecimalFormat::format(int64_t number,
UnicodeString& appendTo,
FieldPosition& fieldPosition) const
{
FieldPositionOnlyHandler handler(fieldPosition);
return _format(number, appendTo, handler);
}
UnicodeString&
DecimalFormat::format(int64_t number,
UnicodeString& appendTo,
FieldPositionIterator* posIter,
UErrorCode& status) const
{
FieldPositionIteratorHandler handler(posIter, status);
return _format(number, appendTo, handler);
}
UnicodeString&
DecimalFormat::_format(int64_t number,
UnicodeString& appendTo,
FieldPositionHandler& handler) const
{
UErrorCode status = U_ZERO_ERROR;
DigitList digits;
digits.set(number);
return _format(digits, appendTo, handler, status);
}
//------------------------------------------------------------------------------
UnicodeString&
DecimalFormat::format( double number,
UnicodeString& appendTo,
FieldPosition& fieldPosition) const
{
FieldPositionOnlyHandler handler(fieldPosition);
return _format(number, appendTo, handler);
}
UnicodeString&
DecimalFormat::format( double number,
UnicodeString& appendTo,
FieldPositionIterator* posIter,
UErrorCode& status) const
{
FieldPositionIteratorHandler handler(posIter, status);
return _format(number, appendTo, handler);
}
UnicodeString&
DecimalFormat::_format( double number,
UnicodeString& appendTo,
FieldPositionHandler& handler) const
{
// Special case for NaN, sets the begin and end index to be the
// the string length of localized name of NaN.
// TODO: let NaNs go through DigitList.
if (uprv_isNaN(number))
{
int begin = appendTo.length();
appendTo += getConstSymbol(DecimalFormatSymbols::kNaNSymbol);
handler.addAttribute(kIntegerField, begin, appendTo.length());
addPadding(appendTo, handler, 0, 0);
return appendTo;
}
UErrorCode status = U_ZERO_ERROR;
DigitList digits;
digits.set(number);
_format(digits, appendTo, handler, status);
// No way to return status from here.
return appendTo;
}
//------------------------------------------------------------------------------
UnicodeString&
DecimalFormat::format(const StringPiece &number,
UnicodeString &toAppendTo,
FieldPositionIterator *posIter,
UErrorCode &status) const
{
DigitList dnum;
dnum.set(number, status);
if (U_FAILURE(status)) {
return toAppendTo;
}
FieldPositionIteratorHandler handler(posIter, status);
_format(dnum, toAppendTo, handler, status);
return toAppendTo;
}
UnicodeString&
DecimalFormat::format(const DigitList &number,
UnicodeString &appendTo,
FieldPositionIterator *posIter,
UErrorCode &status) const {
FieldPositionIteratorHandler handler(posIter, status);
_format(number, appendTo, handler, status);
return appendTo;
}
UnicodeString&
DecimalFormat::format(const DigitList &number,
UnicodeString& appendTo,
FieldPosition& pos,
UErrorCode &status) const {
FieldPositionOnlyHandler handler(pos);
_format(number, appendTo, handler, status);
return appendTo;
}
UnicodeString&
DecimalFormat::_format(const DigitList &number,
UnicodeString& appendTo,
FieldPositionHandler& handler,
UErrorCode &status) const
{
// Special case for NaN, sets the begin and end index to be the
// the string length of localized name of NaN.
if (number.isNaN())
{
int begin = appendTo.length();
appendTo += getConstSymbol(DecimalFormatSymbols::kNaNSymbol);
handler.addAttribute(kIntegerField, begin, appendTo.length());
addPadding(appendTo, handler, 0, 0);
return appendTo;
}
// Do this BEFORE checking to see if value is infinite or negative! Sets the
// begin and end index to be length of the string composed of
// localized name of Infinite and the positive/negative localized
// signs.
DigitList adjustedNum(number); // Copy, so we do not alter the original.
adjustedNum.setRoundingMode(fRoundingMode);
if (fMultiplier != NULL) {
adjustedNum.mult(*fMultiplier, status);
}
/*
* Note: sign is important for zero as well as non-zero numbers.
* Proper detection of -0.0 is needed to deal with the
* issues raised by bugs 4106658, 4106667, and 4147706. Liu 7/6/98.
*/
UBool isNegative = !adjustedNum.isPositive();
// Apply rounding after multiplier
if (fRoundingIncrement != NULL) {
adjustedNum.div(*fRoundingIncrement, status);
adjustedNum.toIntegralValue();
adjustedNum.mult(*fRoundingIncrement, status);
adjustedNum.trim();
}
// Special case for INFINITE,
if (adjustedNum.isInfinite()) {
int32_t prefixLen = appendAffix(appendTo, adjustedNum.getDouble(), handler, isNegative, TRUE);
int begin = appendTo.length();
appendTo += getConstSymbol(DecimalFormatSymbols::kInfinitySymbol);
handler.addAttribute(kIntegerField, begin, appendTo.length());
int32_t suffixLen = appendAffix(appendTo, adjustedNum.getDouble(), handler, isNegative, FALSE);
addPadding(appendTo, handler, prefixLen, suffixLen);
return appendTo;
}
if (fUseExponentialNotation || areSignificantDigitsUsed()) {
int32_t sigDigits = precision();
if (sigDigits > 0) {
adjustedNum.round(sigDigits);
}
} else {
// Fixed point format. Round to a set number of fraction digits.
int32_t numFractionDigits = precision();
adjustedNum.roundFixedPoint(numFractionDigits);
}
return subformat(appendTo, handler, adjustedNum, FALSE);
}
UnicodeString&
DecimalFormat::format( const Formattable& obj,
UnicodeString& appendTo,
FieldPosition& fieldPosition,
UErrorCode& status) const
{
return NumberFormat::format(obj, appendTo, fieldPosition, status);
}
/**
* Return true if a grouping separator belongs at the given
* position, based on whether grouping is in use and the values of
* the primary and secondary grouping interval.
* @param pos the number of integer digits to the right of
* the current position. Zero indicates the position after the
* rightmost integer digit.
* @return true if a grouping character belongs at the current
* position.
*/
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;
}
//------------------------------------------------------------------------------
/**
* Complete the formatting of a finite number. On entry, the DigitList must
* be filled in with the correct digits.
*/
UnicodeString&
DecimalFormat::subformat(UnicodeString& appendTo,
FieldPositionHandler& handler,
DigitList& digits,
UBool isInteger) const
{
// char zero = '0';
// DigitList returns digits as '0' thru '9', so we will need to
// always need to subtract the character 0 to get the numeric value to use for indexing.
UChar32 localizedDigits[10];
localizedDigits[0] = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
localizedDigits[1] = getConstSymbol(DecimalFormatSymbols::kOneDigitSymbol).char32At(0);
localizedDigits[2] = getConstSymbol(DecimalFormatSymbols::kTwoDigitSymbol).char32At(0);
localizedDigits[3] = getConstSymbol(DecimalFormatSymbols::kThreeDigitSymbol).char32At(0);
localizedDigits[4] = getConstSymbol(DecimalFormatSymbols::kFourDigitSymbol).char32At(0);
localizedDigits[5] = getConstSymbol(DecimalFormatSymbols::kFiveDigitSymbol).char32At(0);
localizedDigits[6] = getConstSymbol(DecimalFormatSymbols::kSixDigitSymbol).char32At(0);
localizedDigits[7] = getConstSymbol(DecimalFormatSymbols::kSevenDigitSymbol).char32At(0);
localizedDigits[8] = getConstSymbol(DecimalFormatSymbols::kEightDigitSymbol).char32At(0);
localizedDigits[9] = getConstSymbol(DecimalFormatSymbols::kNineDigitSymbol).char32At(0);
const UnicodeString *grouping ;
if(fCurrencySignCount > fgCurrencySignCountZero) {
grouping = &getConstSymbol(DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol);
}else{
grouping = &getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol);
}
const UnicodeString *decimal;
if(fCurrencySignCount > fgCurrencySignCountZero) {
decimal = &getConstSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol);
} else {
decimal = &getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
}
UBool useSigDig = areSignificantDigitsUsed();
int32_t maxIntDig = getMaximumIntegerDigits();
int32_t minIntDig = getMinimumIntegerDigits();
// Appends the prefix.
double doubleValue = digits.getDouble();
int32_t prefixLen = appendAffix(appendTo, doubleValue, handler, !digits.isPositive(), TRUE);
if (fUseExponentialNotation)
{
int currentLength = appendTo.length();
int intBegin = currentLength;
int intEnd = -1;
int fracBegin = -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;
}
}
// Minimum integer digits are handled in exponential format by
// adjusting the exponent. For example, 0.01234 with 3 minimum
// integer digits is "123.4E-4".
// Maximum integer digits are interpreted as indicating the
// repeating range. This is useful for engineering notation, in
// which the exponent is restricted to a multiple of 3. For
// example, 0.01234 with 3 maximum integer digits is "12.34e-3".
// If maximum integer digits are defined and are larger than
// minimum integer digits, then minimum integer digits are
// ignored.
digits.reduce(); // Removes trailing zero digits.
int32_t exponent = digits.getDecimalAt();
if (maxIntDig > 1 && maxIntDig != minIntDig) {
// A exponent increment is defined; adjust to it.
exponent = (exponent > 0) ? (exponent - 1) / maxIntDig
: (exponent / maxIntDig) - 1;
exponent *= maxIntDig;
} else {
// No exponent increment is defined; use minimum integer digits.
// If none is specified, as in "#E0", generate 1 integer digit.
exponent -= (minIntDig > 0 || minFracDig > 0)
? minIntDig : 1;
}
// We now output a minimum number of digits, and more if there
// are more digits, up to the maximum number of digits. We
// place the decimal point after the "integer" digits, which
// are the first (decimalAt - exponent) digits.
int32_t minimumDigits = minIntDig + minFracDig;
// The number of integer digits is handled specially if the number
// is zero, since then there may be no digits.
int32_t integerDigits = digits.isZero() ? minIntDig :
digits.getDecimalAt() - exponent;
int32_t totalDigits = digits.getCount();
if (minimumDigits > totalDigits)
totalDigits = minimumDigits;
if (integerDigits > totalDigits)
totalDigits = integerDigits;
// totalDigits records total number of digits needs to be processed
int32_t i;
for (i=0; i<totalDigits; ++i)
{
if (i == integerDigits)
{
intEnd = appendTo.length();
handler.addAttribute(kIntegerField, intBegin, intEnd);
appendTo += *decimal;
fracBegin = appendTo.length();
handler.addAttribute(kDecimalSeparatorField, fracBegin - 1, fracBegin);
}
// Restores the digit character or pads the buffer with zeros.
UChar32 c = (UChar32)((i < digits.getCount()) ?
localizedDigits[digits.getDigitValue(i)] :
localizedDigits[0]);
appendTo += c;
}
currentLength = appendTo.length();
if (intEnd < 0) {
handler.addAttribute(kIntegerField, intBegin, currentLength);
}
if (fracBegin > 0) {
handler.addAttribute(kFractionField, fracBegin, currentLength);
}
// The exponent is output using the pattern-specified minimum
// exponent digits. There is no maximum limit to the exponent
// digits, since truncating the exponent would appendTo in an
// unacceptable inaccuracy.
appendTo += getConstSymbol(DecimalFormatSymbols::kExponentialSymbol);
handler.addAttribute(kExponentSymbolField, currentLength, appendTo.length());
currentLength = appendTo.length();
// For zero values, we force the exponent to zero. We
// must do this here, and not earlier, because the value
// is used to determine integer digit count above.
if (digits.isZero())
exponent = 0;
if (exponent < 0) {
appendTo += getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
handler.addAttribute(kExponentSignField, currentLength, appendTo.length());
} else if (fExponentSignAlwaysShown) {
appendTo += getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
handler.addAttribute(kExponentSignField, currentLength, appendTo.length());
}
currentLength = appendTo.length();
DigitList expDigits;
expDigits.set(exponent);
{
int expDig = fMinExponentDigits;
if (fUseExponentialNotation && expDig < 1) {
expDig = 1;
}
for (i=expDigits.getDecimalAt(); i<expDig; ++i)
appendTo += (localizedDigits[0]);
}
for (i=0; i<expDigits.getDecimalAt(); ++i)
{
UChar32 c = (UChar32)((i < expDigits.getCount()) ?
localizedDigits[expDigits.getDigitValue(i)] :
localizedDigits[0]);
appendTo += c;
}
handler.addAttribute(kExponentField, currentLength, appendTo.length());
}
else // Not using exponential notation
{
int currentLength = appendTo.length();
int intBegin = currentLength;
int32_t sigCount = 0;
int32_t minSigDig = getMinimumSignificantDigits();
int32_t maxSigDig = getMaximumSignificantDigits();
if (!useSigDig) {
minSigDig = 0;
maxSigDig = INT32_MAX;
}
// Output the integer portion. Here 'count' is the total
// number of integer digits we will display, including both
// leading zeros required to satisfy getMinimumIntegerDigits,
// and actual digits present in the number.
int32_t count = useSigDig ?
_max(1, digits.getDecimalAt()) : minIntDig;
if (digits.getDecimalAt() > 0 && count < digits.getDecimalAt()) {
count = digits.getDecimalAt();
}
// Handle the case where getMaximumIntegerDigits() is smaller
// than the real number of integer digits. If this is so, we
// output the least significant max integer digits. For example,
// the value 1997 printed with 2 max integer digits is just "97".
int32_t digitIndex = 0; // Index into digitList.fDigits[]
if (count > maxIntDig && maxIntDig >= 0) {
count = maxIntDig;
digitIndex = digits.getDecimalAt() - count;
}
int32_t sizeBeforeIntegerPart = appendTo.length();
int32_t i;
for (i=count-1; i>=0; --i)
{
if (i < digits.getDecimalAt() && digitIndex < digits.getCount() &&
sigCount < maxSigDig) {
// Output a real digit
appendTo += (UChar32)localizedDigits[digits.getDigitValue(digitIndex++)];
++sigCount;
}
else
{
// Output a zero (leading or trailing)
appendTo += localizedDigits[0];
if (sigCount > 0) {
++sigCount;
}
}
// Output grouping separator if necessary.
if (isGroupingPosition(i)) {
currentLength = appendTo.length();
appendTo.append(*grouping);
handler.addAttribute(kGroupingSeparatorField, currentLength, appendTo.length());
}
}
// TODO(dlf): this looks like it was a bug, we marked the int field as ending
// before the zero was generated.
// Record field information for caller.
// if (fieldPosition.getField() == NumberFormat::kIntegerField)
// fieldPosition.setEndIndex(appendTo.length());
// Determine whether or not there are any printable fractional
// digits. If we've used up the digits we know there aren't.
UBool fractionPresent = (!isInteger && digitIndex < digits.getCount()) ||
(useSigDig ? (sigCount < minSigDig) : (getMinimumFractionDigits() > 0));
// If there is no fraction present, and we haven't printed any
// integer digits, then print a zero. Otherwise we won't print
// _any_ digits, and we won't be able to parse this string.
if (!fractionPresent && appendTo.length() == sizeBeforeIntegerPart)
appendTo += localizedDigits[0];
currentLength = appendTo.length();
handler.addAttribute(kIntegerField, intBegin, currentLength);
// Output the decimal separator if we always do so.
if (fDecimalSeparatorAlwaysShown || fractionPresent) {
appendTo += *decimal;
handler.addAttribute(kDecimalSeparatorField, currentLength, appendTo.length());
currentLength = appendTo.length();
}
int fracBegin = currentLength;
count = useSigDig ? INT32_MAX : getMaximumFractionDigits();
if (useSigDig && (sigCount == maxSigDig ||
(sigCount >= minSigDig && digitIndex == digits.getCount()))) {
count = 0;
}
for (i=0; i < count; ++i) {
// Here is where we escape from the loop. We escape
// if we've output the maximum fraction digits
// (specified in the for expression above). We also
// stop when we've output the minimum digits and
// either: we have an integer, so there is no
// fractional stuff to display, or we're out of
// significant digits.
if (!useSigDig && i >= getMinimumFractionDigits() &&
(isInteger || digitIndex >= digits.getCount())) {
break;
}
// Output leading fractional zeros. These are zeros
// that come after the decimal but before any
// significant digits. These are only output if
// abs(number being formatted) < 1.0.
if (-1-i > (digits.getDecimalAt()-1)) {
appendTo += localizedDigits[0];
continue;
}
// Output a digit, if we have any precision left, or a
// zero if we don't. We don't want to output noise digits.
if (!isInteger && digitIndex < digits.getCount()) {
appendTo += (UChar32)localizedDigits[digits.getDigitValue(digitIndex++)];
} else {
appendTo += localizedDigits[0];
}
// If we reach the maximum number of significant
// digits, or if we output all the real digits and
// reach the minimum, then we are done.
++sigCount;
if (useSigDig &&
(sigCount == maxSigDig ||
(digitIndex == digits.getCount() && sigCount >= minSigDig))) {
break;
}
}
handler.addAttribute(kFractionField, fracBegin, appendTo.length());
}
int32_t suffixLen = appendAffix(appendTo, doubleValue, handler, !digits.isPositive(), FALSE);
addPadding(appendTo, handler, prefixLen, suffixLen);
return appendTo;
}
/**
* Inserts the character fPad as needed to expand result to fFormatWidth.
* @param result the string to be padded
*/
void DecimalFormat::addPadding(UnicodeString& appendTo,
FieldPositionHandler& handler,
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) {
handler.shiftLast(len);
}
}
}
}
//------------------------------------------------------------------------------
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;
}
/**
* Parses the given text as either a number or a currency amount.
* @param text the string to parse
* @param result output parameter for the result
* @param parsePosition input-output position; on input, the
* position within text to match; must have 0 <= pos.getIndex() <
* text.length(); on output, the position after the last matched
* character. If the parse fails, the position in unchanged upon
* output.
* @param parseCurrency if true, a currency amount is parsed;
* otherwise a Number is parsed
*/
void DecimalFormat::parse(const UnicodeString& text,
Formattable& result,
ParsePosition& parsePosition,
UBool parseCurrency) const {
int32_t backup;
int32_t i = backup = parsePosition.getIndex();
// clear any old contents in the result. In particular, clears any DigitList
// that it may be holding.
result.setLong(0);
// Handle NaN as a special case:
// Skip padding characters, if around prefix
if (fFormatWidth > 0 && (fPadPosition == kPadBeforePrefix ||
fPadPosition == kPadAfterPrefix)) {
i = skipPadding(text, i);
}
// If the text is composed of the representation of NaN, returns NaN.length
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;
}
// NaN parse failed; start over
i = backup;
// status is used to record whether a number is infinite.
UBool status[fgStatusLength];
UChar curbuf[4];
UChar* currency = parseCurrency ? curbuf : NULL;
DigitList *digits = new DigitList;
if (digits == NULL) {
return; // no way to report error from here.
}
if (fCurrencySignCount > fgCurrencySignCountZero) {
if (!parseForCurrency(text, parsePosition, *digits,
status, currency)) {
delete digits;
return;
}
} else {
if (!subparse(text,
fNegPrefixPattern, fNegSuffixPattern,
fPosPrefixPattern, fPosSuffixPattern,
FALSE, UCURR_SYMBOL_NAME,
parsePosition, *digits, status, currency)) {
parsePosition.setIndex(backup);
delete digits;
return;
}
}
// Handle infinity
if (status[fgStatusInfinite]) {
double inf = uprv_getInfinity();
result.setDouble(digits->isPositive() ? inf : -inf);
delete digits; // TODO: set the dl to infinity, and let it fall into the code below.
}
else {
if (fMultiplier != NULL) {
UErrorCode ec = U_ZERO_ERROR;
digits->div(*fMultiplier, ec);
}
// Negative zero special case:
// if parsing integerOnly, change to +0, which goes into an int32 in a Formattable.
// if not parsing integerOnly, leave as -0, which a double can represent.
if (digits->isZero() && !digits->isPositive() && isParseIntegerOnly()) {
digits->setPositive(TRUE);
}
result.adoptDigitList(digits);
}
if (parseCurrency) {
UErrorCode ec = U_ZERO_ERROR;
Formattable n(result);
result.adoptObject(new CurrencyAmount(n, curbuf, ec));
U_ASSERT(U_SUCCESS(ec)); // should always succeed
}
}
UBool
DecimalFormat::parseForCurrency(const UnicodeString& text,
ParsePosition& parsePosition,
DigitList& digits,
UBool* status,
UChar* currency) const {
int origPos = parsePosition.getIndex();
int maxPosIndex = origPos;
int maxErrorPos = -1;
// First, parse against current pattern.
// Since current pattern could be set by applyPattern(),
// it could be an arbitrary pattern, and it may not be the one
// defined in current locale.
UBool tmpStatus[fgStatusLength];
ParsePosition tmpPos(origPos);
DigitList tmpDigitList;
UBool found;
if (fStyle == NumberFormat::kPluralCurrencyStyle) {
found = subparse(text,
fNegPrefixPattern, fNegSuffixPattern,
fPosPrefixPattern, fPosSuffixPattern,
TRUE, UCURR_LONG_NAME,
tmpPos, tmpDigitList, tmpStatus, currency);
} else {
found = subparse(text,
fNegPrefixPattern, fNegSuffixPattern,
fPosPrefixPattern, fPosSuffixPattern,
TRUE, UCURR_SYMBOL_NAME,
tmpPos, tmpDigitList, tmpStatus, currency);
}
if (found) {
if (tmpPos.getIndex() > maxPosIndex) {
maxPosIndex = tmpPos.getIndex();
for (int32_t i = 0; i < fgStatusLength; ++i) {
status[i] = tmpStatus[i];
}
digits = tmpDigitList;
}
} else {
maxErrorPos = tmpPos.getErrorIndex();
}
// Then, parse against affix patterns.
// Those are currency patterns and currency plural patterns.
int32_t pos = -1;
const UHashElement* element = NULL;
while ( (element = fAffixPatternsForCurrency->nextElement(pos)) != NULL ) {
const UHashTok keyTok = element->key;
const UHashTok valueTok = element->value;
const AffixPatternsForCurrency* affixPtn = (AffixPatternsForCurrency*)valueTok.pointer;
UBool tmpStatus[fgStatusLength];
ParsePosition tmpPos(origPos);
DigitList tmpDigitList;
UBool result = subparse(text,
&affixPtn->negPrefixPatternForCurrency,
&affixPtn->negSuffixPatternForCurrency,
&affixPtn->posPrefixPatternForCurrency,
&affixPtn->posSuffixPatternForCurrency,
TRUE, affixPtn->patternType,
tmpPos, tmpDigitList, tmpStatus, currency);
if (result) {
found = true;
if (tmpPos.getIndex() > maxPosIndex) {
maxPosIndex = tmpPos.getIndex();
for (int32_t i = 0; i < fgStatusLength; ++i) {
status[i] = tmpStatus[i];
}
digits = tmpDigitList;
}
} else {
maxErrorPos = (tmpPos.getErrorIndex() > maxErrorPos) ?
tmpPos.getErrorIndex() : maxErrorPos;
}
}
// Finally, parse against simple affix to find the match.
// For example, in TestMonster suite,
// if the to-be-parsed text is "-\u00A40,00".
// complexAffixCompare will not find match,
// since there is no ISO code matches "\u00A4",
// and the parse stops at "\u00A4".
// We will just use simple affix comparison (look for exact match)
// to pass it.
UBool tmpStatus_2[fgStatusLength];
ParsePosition tmpPos_2(origPos);
DigitList tmpDigitList_2;
// set currencySignCount to 0 so that compareAffix function will
// fall to compareSimpleAffix path, not compareComplexAffix path.
// ?? TODO: is it right? need "false"?
UBool result = subparse(text,
&fNegativePrefix, &fNegativeSuffix,
&fPositivePrefix, &fPositiveSuffix,
FALSE, UCURR_SYMBOL_NAME,
tmpPos_2, tmpDigitList_2, tmpStatus_2,
currency);
if (result) {
if (tmpPos_2.getIndex() > maxPosIndex) {
maxPosIndex = tmpPos_2.getIndex();
for (int32_t i = 0; i < fgStatusLength; ++i) {
status[i] = tmpStatus_2[i];
}
digits = tmpDigitList_2;
}
found = true;
} else {
maxErrorPos = (tmpPos_2.getErrorIndex() > maxErrorPos) ?
tmpPos_2.getErrorIndex() : maxErrorPos;
}
if (!found) {
//parsePosition.setIndex(origPos);
parsePosition.setErrorIndex(maxErrorPos);
} else {
parsePosition.setIndex(maxPosIndex);
parsePosition.setErrorIndex(-1);
}
return found;
}
/**
* Parse the given text into a number. The text is parsed beginning at
* parsePosition, until an unparseable character is seen.
* @param text the string to parse.
* @param negPrefix negative prefix.
* @param negSuffix negative suffix.
* @param posPrefix positive prefix.
* @param posSuffix positive suffix.
* @param currencyParsing whether it is currency parsing or not.
* @param type the currency type to parse against, LONG_NAME only or not.
* @param parsePosition The position at which to being parsing. Upon
* return, the first unparsed character.
* @param digits the DigitList to set to the parsed value.
* @param status output param containing boolean status flags indicating
* whether the value was infinite and whether it was positive.
* @param currency return value for parsed currency, for generic
* currency parsing mode, or NULL for normal parsing. In generic
* currency parsing mode, any currency is parsed, not just the
* currency that this formatter is set to.
*/
UBool DecimalFormat::subparse(const UnicodeString& text,
const UnicodeString* negPrefix,
const UnicodeString* negSuffix,
const UnicodeString* posPrefix,
const UnicodeString* posSuffix,
UBool currencyParsing,
int8_t type,
ParsePosition& parsePosition,
DigitList& digits, UBool* status,
UChar* currency) const
{
// The parsing process builds up the number as char string, in the neutral format that
// will be acceptable to the decNumber library, then at the end passes that string
// off for conversion to a decNumber.
UErrorCode err = U_ZERO_ERROR;
CharString parsedNum;
digits.setToZero();
int32_t position = parsePosition.getIndex();
int32_t oldStart = position;
// Match padding before prefix
if (fFormatWidth > 0 && fPadPosition == kPadBeforePrefix) {
position = skipPadding(text, position);
}
// Match positive and negative prefixes; prefer longest match.
int32_t posMatch = compareAffix(text, position, FALSE, TRUE, posPrefix, currencyParsing, type, currency);
int32_t negMatch = compareAffix(text, position, TRUE, TRUE, negPrefix,currencyParsing, type, currency);
if (posMatch >= 0 && negMatch >= 0) {
if (posMatch > negMatch) {
negMatch = -1;
} else if (negMatch > posMatch) {
posMatch = -1;
}
}
if (posMatch >= 0) {
position += posMatch;
parsedNum.append('+', err);
} else if (negMatch >= 0) {
position += negMatch;
parsedNum.append('-', err);
} else {
parsePosition.setErrorIndex(position);
return FALSE;
}
// Match padding before prefix
if (fFormatWidth > 0 && fPadPosition == kPadAfterPrefix) {
position = skipPadding(text, position);
}
// process digits or Inf, find decimal position
const UnicodeString *inf = &getConstSymbol(DecimalFormatSymbols::kInfinitySymbol);
int32_t infLen = (text.compare(position, inf->length(), *inf)
? 0 : inf->length());
position += infLen; // infLen is non-zero when it does equal to infinity
status[fgStatusInfinite] = (UBool)infLen;
if (infLen) {
parsedNum.append("Infinity", err);
} else {
// We now have a string of digits, possibly with grouping symbols,
// and decimal points. We want to process these into a DigitList.
// We don't want to put a bunch of leading zeros into the DigitList
// though, so we keep track of the location of the decimal point,
// put only significant digits into the DigitList, and adjust the
// exponent as needed.
UChar32 zero = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
const UnicodeString *decimal;
if(fCurrencySignCount > fgCurrencySignCountZero) {
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(); // One less pointer to follow
int32_t groupingLen = grouping->length();
int32_t decimalLen = decimal->length();
// We have to track digitCount ourselves, because digits.fCount will
// pin when the maximum allowable digits is reached.
int32_t digitCount = 0;
for (; position < textLength; )
{
UChar32 ch = text.char32At(position);
/* We recognize all digit ranges, not only the Latin digit range
* '0'..'9'. We do so by using the Character.digit() method,
* which converts a valid Unicode digit to the range 0..9.
*
* The character 'ch' may be a digit. If so, place its value
* from 0 to 9 in 'digit'. First try using the locale digit,
* which may or MAY NOT be a standard Unicode digit range. If
* this fails, try using the standard Unicode digit ranges by
* calling Character.digit(). If this also fails, digit will
* have a value outside the range 0..9.
*/
digit = ch - zero;
if (digit < 0 || digit > 9)
{
digit = u_charDigitValue(ch);
}
// As a last resort, look through the localized digits if the zero digit
// is not a "standard" Unicode digit.
if ( (digit < 0 || digit > 9) && u_charDigitValue(zero) != 0) {
digit = 0;
if ( getConstSymbol((DecimalFormatSymbols::ENumberFormatSymbol)(DecimalFormatSymbols::kZeroDigitSymbol)).char32At(0) == ch ) {
break;
}
for (digit = 1 ; digit < 10 ; digit++ ) {
if ( getConstSymbol((DecimalFormatSymbols::ENumberFormatSymbol)(DecimalFormatSymbols::kOneDigitSymbol+digit-1)).char32At(0) == ch ) {
break;
}
}
}
if (digit >= 0 && digit <= 9)
{
// Cancel out backup setting (see grouping handler below)
backup = -1;
sawDigit = TRUE;
// output a regular non-zero digit.
++digitCount;
parsedNum.append((char)(digit + '0'), err);
position += U16_LENGTH(ch);
}
else if (groupingLen > 0 && !text.compare(position, groupingLen, *grouping) && isGroupingUsed())
{
// Ignore grouping characters, if we are using them, but require
// that they be followed by a digit. Otherwise we backup and
// reprocess them.
backup = position;
position += groupingLen;
}
else if (!text.compare(position, decimalLen, *decimal) && !isParseIntegerOnly() && !sawDecimal)
{
// If we're only parsing integers, or if we ALREADY saw the
// decimal, then don't parse this one.
parsedNum.append('.', err);
sawDecimal = TRUE;
position += decimalLen;
}
else {
const UnicodeString *tmp;
tmp = &getConstSymbol(DecimalFormatSymbols::kExponentialSymbol);
if (!text.compare(position, tmp->length(), *tmp)) // error code is set below if !sawDigit
{
// Parse sign, if present
int32_t pos = position + tmp->length();
char exponentSign = '+';
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))
{
exponentSign = '-';
pos += tmp->length();
}
}
}
UBool sawExponentDigit = 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) {
if (!sawExponentDigit) {
parsedNum.append('E', err);
parsedNum.append(exponentSign, err);
sawExponentDigit = TRUE;
}
++pos;
parsedNum.append((char)(digit + '0'), err);
} else {
break;
}
}
if (sawExponentDigit) {
position = pos; // Advance past the exponent
}
break; // Whether we fail or succeed, we exit this loop
}
else {
break;
}
}
}
if (backup != -1)
{
position = backup;
}
// If there was no decimal point we have an integer
// If none of the text string was recognized. For example, parse
// "x" with pattern "#0.00" (return index and error index both 0)
// parse "$" with pattern "$#0.00". (return index 0 and error index
// 1).
if (!sawDigit && digitCount == 0) {
parsePosition.setIndex(oldStart);
parsePosition.setErrorIndex(oldStart);
return FALSE;
}
}
// Match padding before suffix
if (fFormatWidth > 0 && fPadPosition == kPadBeforeSuffix) {
position = skipPadding(text, position);
}
// Match positive and negative suffixes; prefer longest match.
if (posMatch >= 0) {
posMatch = compareAffix(text, position, FALSE, FALSE, posSuffix, currencyParsing, type, currency);
}
if (negMatch >= 0) {
negMatch = compareAffix(text, position, TRUE, FALSE, negSuffix, currencyParsing, type, currency);
}
if (posMatch >= 0 && negMatch >= 0) {
if (posMatch > negMatch) {
negMatch = -1;
} else if (negMatch > posMatch) {
posMatch = -1;
}
}
// Fail if neither or both
if ((posMatch >= 0) == (negMatch >= 0)) {
parsePosition.setErrorIndex(position);
return FALSE;
}
position += (posMatch>=0 ? posMatch : negMatch);
// Match padding before suffix
if (fFormatWidth > 0 && fPadPosition == kPadAfterSuffix) {
position = skipPadding(text, position);
}
parsePosition.setIndex(position);
parsedNum.data()[0] = (posMatch >= 0) ? '+' : '-';
if(parsePosition.getIndex() == oldStart)
{
parsePosition.setErrorIndex(position);
return FALSE;
}
digits.set(parsedNum.toStringPiece(), err);
if (U_FAILURE(err)) {
parsePosition.setErrorIndex(position);
return FALSE;
}
return TRUE;
}
/**
* Starting at position, advance past a run of pad characters, if any.
* Return the index of the first character after position that is not a pad
* character. Result is >= position.
*/
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;
}
/**
* Return the length matched by the given affix, or -1 if none.
* Runs of white space in the affix, match runs of white space in
* the input. Pattern white space and input white space are
* determined differently; see code.
* @param text input text
* @param pos offset into input at which to begin matching
* @param isNegative
* @param isPrefix
* @param affixPat affix pattern used for currency affix comparison.
* @param currencyParsing whether it is currency parsing or not
* @param type the currency type to parse against, LONG_NAME only or not.
* @param currency return value for parsed currency, for generic
* currency parsing mode, or null for normal parsing. In generic
* currency parsing mode, any currency is parsed, not just the
* currency that this formatter is set to.
* @return length of input that matches, or -1 if match failure
*/
int32_t DecimalFormat::compareAffix(const UnicodeString& text,
int32_t pos,
UBool isNegative,
UBool isPrefix,
const UnicodeString* affixPat,
UBool currencyParsing,
int8_t type,
UChar* currency) const
{
const UnicodeString *patternToCompare;
if (fCurrencyChoice != NULL || currency != NULL ||
(fCurrencySignCount > fgCurrencySignCountZero && currencyParsing)) {
if (affixPat != NULL) {
return compareComplexAffix(*affixPat, text, pos, type, currency);
}
}
if (isNegative) {
if (isPrefix) {
patternToCompare = &fNegativePrefix;
}
else {
patternToCompare = &fNegativeSuffix;
}
}
else {
if (isPrefix) {
patternToCompare = &fPositivePrefix;
}
else {
patternToCompare = &fPositiveSuffix;
}
}
return compareSimpleAffix(*patternToCompare, text, pos);
}
/**
* Return the length matched by the given affix, or -1 if none.
* Runs of white space in the affix, match runs of white space in
* the input. Pattern white space and input white space are
* determined differently; see code.
* @param affix pattern string, taken as a literal
* @param input input text
* @param pos offset into input at which to begin matching
* @return length of input that matches, or -1 if match failure
*/
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)) {
// We may have a pattern like: \u200F \u0020
// and input text like: \u200F \u0020
// Note that U+200F and U+0020 are RuleWhiteSpace but only
// U+0020 is UWhiteSpace. So we have to first do a direct
// match of the run of RULE whitespace in the pattern,
// then match any extra characters.
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;
}
}
// Advance over run in pattern
i = skipRuleWhiteSpace(affix, i);
// Advance over run in input text
// Must see at least one white space char in input,
// unless we've already matched some characters literally.
int32_t s = pos;
pos = skipUWhiteSpace(input, pos);
if (pos == s && !literalMatch) {
return -1;
}
// If we skip UWhiteSpace in the input text, we need to skip it in the pattern.
// Otherwise, the previous lines may have skipped over text (such as U+00A0) that
// is also in the affix.
i = skipUWhiteSpace(affix, i);
} else {
if (pos < input.length() &&
input.char32At(pos) == c) {
i += len;
pos += len;
} else {
return -1;
}
}
}
return pos - start;
}
/**
* Skip over a run of zero or more isRuleWhiteSpace() characters at
* pos in text.
*/
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;
}
/**
* Skip over a run of zero or more isUWhiteSpace() characters at pos
* in text.
*/
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;
}
/**
* Return the length matched by the given affix, or -1 if none.
* @param affixPat pattern string
* @param input input text
* @param pos offset into input at which to begin matching
* @param type the currency type to parse against, LONG_NAME only or not.
* @param currency return value for parsed currency, for generic
* currency parsing mode, or null for normal parsing. In generic
* currency parsing mode, any currency is parsed, not just the
* currency that this formatter is set to.
* @return length of input that matches, or -1 if match failure
*/
int32_t DecimalFormat::compareComplexAffix(const UnicodeString& affixPat,
const UnicodeString& text,
int32_t pos,
int8_t type,
UChar* currency) const
{
int32_t start = pos;
U_ASSERT(currency != NULL ||
(fCurrencyChoice != NULL && *getCurrency() != 0) ||
fCurrencySignCount > fgCurrencySignCountZero);
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: {
// since the currency names in choice format is saved
// the same way as other currency names,
// do not need to do currency choice parsing here.
// the general currency parsing parse against all names,
// including names in choice format.
UBool intl = i<affixPat.length() &&
affixPat.char32At(i) == kCurrencySign;
if (intl) {
++i;
}
UBool plural = i<affixPat.length() &&
affixPat.char32At(i) == kCurrencySign;
if (plural) {
++i;
intl = FALSE;
}
// Parse generic currency -- anything for which we
// have a display name, or any 3-letter ISO code.
// Try to parse display name for our locale; first
// determine our locale.
const char* loc = fCurrencyPluralInfo->getLocale().getName();
ParsePosition ppos(pos);
UChar curr[4];
UErrorCode ec = U_ZERO_ERROR;
// Delegate parse of display name => ISO code to Currency
uprv_parseCurrency(loc, text, ppos, type, curr, ec);
// If parse succeeds, populate currency[0]
if (U_SUCCESS(ec) && ppos.getIndex() != pos) {
if (currency) {
u_strcpy(currency, curr);
}
pos = ppos.getIndex();
} else {
pos = -1;
}
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:
// fall through to affix!=0 test, which will fail
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 - start;
}
/**
* Match a single character at text[pos] and return the index of the
* next character upon success. Return -1 on failure. If
* isRuleWhiteSpace(ch) then match a run of white space in text.
*/
int32_t DecimalFormat::match(const UnicodeString& text, int32_t pos, UChar32 ch) {
if (uprv_isRuleWhiteSpace(ch)) {
// Advance over run of white space in input text
// Must see at least one white space char in input
int32_t s = pos;
pos = skipRuleWhiteSpace(text, pos);
if (pos == s) {
return -1;
}
return pos;
}
return (pos >= 0 && text.char32At(pos) == ch) ?
(pos + U16_LENGTH(ch)) : -1;
}
/**
* Match a string at text[pos] and return the index of the next
* character upon success. Return -1 on failure. Match a run of
* white space in str with a run of white space in text.
*/
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;
}
//------------------------------------------------------------------------------
// Gets the pointer to the localized decimal format symbols
const DecimalFormatSymbols*
DecimalFormat::getDecimalFormatSymbols() const
{
return fSymbols;
}
//------------------------------------------------------------------------------
// De-owning the current localized symbols and adopt the new symbols.
void
DecimalFormat::adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt)
{
if (symbolsToAdopt == NULL) {
return; // do not allow caller to set fSymbols to NULL
}
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) {
// If the currency symbols are the same, there is no need to recalculate.
setCurrencyForSymbols();
}
expandAffixes(NULL);
}
//------------------------------------------------------------------------------
// Setting the symbols is equlivalent to adopting a newly created localized
// symbols.
void
DecimalFormat::setDecimalFormatSymbols(const DecimalFormatSymbols& symbols)
{
adoptDecimalFormatSymbols(new DecimalFormatSymbols(symbols));
}
const CurrencyPluralInfo*
DecimalFormat::getCurrencyPluralInfo(void) const
{
return fCurrencyPluralInfo;
}
void
DecimalFormat::adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt)
{
if (toAdopt != NULL) {
delete fCurrencyPluralInfo;
fCurrencyPluralInfo = toAdopt;
// re-set currency affix patterns and currency affixes.
if (fCurrencySignCount > fgCurrencySignCountZero) {
UErrorCode status = U_ZERO_ERROR;
if (fAffixPatternsForCurrency) {
deleteHashForAffixPattern();
}
setupCurrencyAffixPatterns(status);
if (fCurrencySignCount == fgCurrencySignCountInPluralFormat) {
// only setup the affixes of the plural pattern.
setupCurrencyAffixes(fFormatPattern, FALSE, TRUE, status);
}
}
}
}
void
DecimalFormat::setCurrencyPluralInfo(const CurrencyPluralInfo& info)
{
adoptCurrencyPluralInfo(info.clone());
}
/**
* Update the currency object to match the symbols. This method
* is used only when the caller has passed in a symbols object
* that may not be the default object for its locale.
*/
void
DecimalFormat::setCurrencyForSymbols() {
/*Bug 4212072
Update the affix strings accroding to symbols in order to keep
the affix strings up to date.
[Richard/GCL]
*/
// With the introduction of the Currency object, the currency
// symbols in the DFS object are ignored. For backward
// compatibility, we check any explicitly set DFS object. If it
// is a default symbols object for its locale, we change the
// currency object to one for that locale. If it is custom,
// we set the currency to null.
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)
{
// Trap an error in mapping locale to currency. If we can't
// map, then don't fail and set the currency to "".
c = intlCurrencySymbol;
}
ec = U_ZERO_ERROR; // reset local error code!
setCurrencyInternally(c, ec);
}
//------------------------------------------------------------------------------
// Gets the positive prefix of the number pattern.
UnicodeString&
DecimalFormat::getPositivePrefix(UnicodeString& result) const
{
result = fPositivePrefix;
return result;
}
//------------------------------------------------------------------------------
// Sets the positive prefix of the number pattern.
void
DecimalFormat::setPositivePrefix(const UnicodeString& newValue)
{
fPositivePrefix = newValue;
delete fPosPrefixPattern;
fPosPrefixPattern = 0;
}
//------------------------------------------------------------------------------
// Gets the negative prefix of the number pattern.
UnicodeString&
DecimalFormat::getNegativePrefix(UnicodeString& result) const
{
result = fNegativePrefix;
return result;
}
//------------------------------------------------------------------------------
// Gets the negative prefix of the number pattern.
void
DecimalFormat::setNegativePrefix(const UnicodeString& newValue)
{
fNegativePrefix = newValue;
delete fNegPrefixPattern;
fNegPrefixPattern = 0;
}
//------------------------------------------------------------------------------
// Gets the positive suffix of the number pattern.
UnicodeString&
DecimalFormat::getPositiveSuffix(UnicodeString& result) const
{
result = fPositiveSuffix;
return result;
}
//------------------------------------------------------------------------------
// Sets the positive suffix of the number pattern.
void
DecimalFormat::setPositiveSuffix(const UnicodeString& newValue)
{
fPositiveSuffix = newValue;
delete fPosSuffixPattern;
fPosSuffixPattern = 0;
}
//------------------------------------------------------------------------------
// Gets the negative suffix of the number pattern.
UnicodeString&
DecimalFormat::getNegativeSuffix(UnicodeString& result) const
{
result = fNegativeSuffix;
return result;
}
//------------------------------------------------------------------------------
// Sets the negative suffix of the number pattern.
void
DecimalFormat::setNegativeSuffix(const UnicodeString& newValue)
{
fNegativeSuffix = newValue;
delete fNegSuffixPattern;
fNegSuffixPattern = 0;
}
//------------------------------------------------------------------------------
// Gets the multiplier of the number pattern.
// Multipliers are stored as decimal numbers (DigitLists) because that
// is the most convenient for muliplying or dividing the numbers to be formatted.
// A NULL multiplier implies one, and the scaling operations are skipped.
int32_t
DecimalFormat::getMultiplier() const
{
if (fMultiplier == NULL) {
return 1;
} else {
return fMultiplier->getLong();
}
}
//------------------------------------------------------------------------------
// Sets the multiplier of the number pattern.
void
DecimalFormat::setMultiplier(int32_t newValue)
{
// if (newValue == 0) {
// throw new IllegalArgumentException("Bad multiplier: " + newValue);
// }
if (newValue == 0) {
newValue = 1; // one being the benign default value for a multiplier.
}
if (newValue == 1) {
delete fMultiplier;
fMultiplier = NULL;
} else {
if (fMultiplier == NULL) {
fMultiplier = new DigitList;
}
if (fMultiplier != NULL) {
fMultiplier->set(newValue);
}
}
}
/**
* Get the rounding increment.
* @return A positive rounding increment, or 0.0 if rounding
* is not in effect.
* @see #setRoundingIncrement
* @see #getRoundingMode
* @see #setRoundingMode
*/
double DecimalFormat::getRoundingIncrement() const {
if (fRoundingIncrement == NULL) {
return 0.0;
} else {
return fRoundingIncrement->getDouble();
}
}
/**
* Set the rounding increment. This method also controls whether
* rounding is enabled.
* @param newValue A positive rounding increment, or 0.0 to disable rounding.
* Negative increments are equivalent to 0.0.
* @see #getRoundingIncrement
* @see #getRoundingMode
* @see #setRoundingMode
*/
void DecimalFormat::setRoundingIncrement(double newValue) {
if (newValue > 0.0) {
if (fRoundingIncrement == NULL) {
fRoundingIncrement = new DigitList();
}
if (fRoundingIncrement != NULL) {
fRoundingIncrement->set(newValue);
return;
}