2 *******************************************************************************
3 * Copyright (C) 1997-2013, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Modification History:
11 * Date Name Description
12 * 02/19/97 aliu Converted from java.
13 * 03/20/97 clhuang Implemented with new APIs.
14 * 03/31/97 aliu Moved isLONG_MIN to DigitList, and fixed it.
15 * 04/3/97 aliu Rewrote parsing and formatting completely, and
16 * cleaned up and debugged. Actually works now.
17 * Implemented NAN and INF handling, for both parsing
18 * and formatting. Extensive testing & debugging.
19 * 04/10/97 aliu Modified to compile on AIX.
20 * 04/16/97 aliu Rewrote to use DigitList, which has been resurrected.
21 * Changed DigitCount to int per code review.
22 * 07/09/97 helena Made ParsePosition into a class.
23 * 08/26/97 aliu Extensive changes to applyPattern; completely
24 * rewritten from the Java.
25 * 09/09/97 aliu Ported over support for exponential formats.
26 * 07/20/98 stephen JDK 1.2 sync up.
27 * Various instances of '0' replaced with 'NULL'
28 * Check for grouping size in subFormat()
29 * Brought subParse() in line with Java 1.2
30 * Added method appendAffix()
31 * 08/24/1998 srl Removed Mutex calls. This is not a thread safe class!
32 * 02/22/99 stephen Removed character literals for EBCDIC safety
33 * 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
34 * 06/28/99 stephen Fixed bugs in toPattern().
35 * 06/29/99 stephen Fixed operator= to copy fFormatWidth, fPad,
37 ********************************************************************************
40 #include "unicode/utypes.h"
42 #if !UCONFIG_NO_FORMATTING
45 #include "unicode/decimfmt.h"
46 #include "unicode/choicfmt.h"
47 #include "unicode/ucurr.h"
48 #include "unicode/ustring.h"
49 #include "unicode/dcfmtsym.h"
50 #include "unicode/ures.h"
51 #include "unicode/uchar.h"
52 #include "unicode/uniset.h"
53 #include "unicode/curramt.h"
54 #include "unicode/currpinf.h"
55 #include "unicode/plurrule.h"
56 #include "unicode/utf16.h"
57 #include "unicode/numsys.h"
58 #include "unicode/localpointer.h"
63 #include "patternprops.h"
73 #include "plurrule_impl.h"
76 * On certain platforms, round is a macro defined in math.h
77 * This undefine is to avoid conflict between the macro and
78 * the function defined below.
89 static void _debugout(const char *f, int l, const UnicodeString& s) {
91 s.extract((int32_t) 0, s.length(), buf, "utf-8");
92 printf("%s:%d: %s\n", f,l, buf);
94 #define debugout(x) _debugout(__FILE__,__LINE__,x)
95 #define debug(x) printf("%s:%d: %s\n", __FILE__,__LINE__, x);
96 static const UnicodeString dbg_null("<NULL>","");
97 #define DEREFSTR(x) ((x!=NULL)?(*x):(dbg_null))
105 /* == Fastpath calculation. ==
107 #if UCONFIG_FORMAT_FASTPATHS_49
108 inline DecimalFormatInternal& internalData(uint8_t *reserved) {
109 return *reinterpret_cast<DecimalFormatInternal*>(reserved);
111 inline const DecimalFormatInternal& internalData(const uint8_t *reserved) {
112 return *reinterpret_cast<const DecimalFormatInternal*>(reserved);
117 /* For currency parsing purose,
118 * Need to remember all prefix patterns and suffix patterns of
119 * every currency format pattern,
120 * including the pattern of default currecny style
121 * and plural currency style. And the patterns are set through applyPattern.
123 struct AffixPatternsForCurrency : public UMemory {
124 // negative prefix pattern
125 UnicodeString negPrefixPatternForCurrency;
126 // negative suffix pattern
127 UnicodeString negSuffixPatternForCurrency;
128 // positive prefix pattern
129 UnicodeString posPrefixPatternForCurrency;
130 // positive suffix pattern
131 UnicodeString posSuffixPatternForCurrency;
134 AffixPatternsForCurrency(const UnicodeString& negPrefix,
135 const UnicodeString& negSuffix,
136 const UnicodeString& posPrefix,
137 const UnicodeString& posSuffix,
139 negPrefixPatternForCurrency = negPrefix;
140 negSuffixPatternForCurrency = negSuffix;
141 posPrefixPatternForCurrency = posPrefix;
142 posSuffixPatternForCurrency = posSuffix;
147 debugout( UnicodeString("AffixPatternsForCurrency( -=\"") +
148 negPrefixPatternForCurrency + (UnicodeString)"\"/\"" +
149 negSuffixPatternForCurrency + (UnicodeString)"\" +=\"" +
150 posPrefixPatternForCurrency + (UnicodeString)"\"/\"" +
151 posSuffixPatternForCurrency + (UnicodeString)"\" )");
156 /* affix for currency formatting when the currency sign in the pattern
157 * equals to 3, such as the pattern contains 3 currency sign or
158 * the formatter style is currency plural format style.
160 struct AffixesForCurrency : public UMemory {
162 UnicodeString negPrefixForCurrency;
164 UnicodeString negSuffixForCurrency;
166 UnicodeString posPrefixForCurrency;
168 UnicodeString posSuffixForCurrency;
172 AffixesForCurrency(const UnicodeString& negPrefix,
173 const UnicodeString& negSuffix,
174 const UnicodeString& posPrefix,
175 const UnicodeString& posSuffix) {
176 negPrefixForCurrency = negPrefix;
177 negSuffixForCurrency = negSuffix;
178 posPrefixForCurrency = posPrefix;
179 posSuffixForCurrency = posSuffix;
183 debugout( UnicodeString("AffixesForCurrency( -=\"") +
184 negPrefixForCurrency + (UnicodeString)"\"/\"" +
185 negSuffixForCurrency + (UnicodeString)"\" +=\"" +
186 posPrefixForCurrency + (UnicodeString)"\"/\"" +
187 posSuffixForCurrency + (UnicodeString)"\" )");
197 static UBool U_CALLCONV decimfmtAffixValueComparator(UHashTok val1, UHashTok val2);
202 static UBool U_CALLCONV decimfmtAffixPatternValueComparator(UHashTok val1, UHashTok val2);
206 U_CALLCONV decimfmtAffixValueComparator(UHashTok val1, UHashTok val2) {
207 const AffixesForCurrency* affix_1 =
208 (AffixesForCurrency*)val1.pointer;
209 const AffixesForCurrency* affix_2 =
210 (AffixesForCurrency*)val2.pointer;
211 return affix_1->negPrefixForCurrency == affix_2->negPrefixForCurrency &&
212 affix_1->negSuffixForCurrency == affix_2->negSuffixForCurrency &&
213 affix_1->posPrefixForCurrency == affix_2->posPrefixForCurrency &&
214 affix_1->posSuffixForCurrency == affix_2->posSuffixForCurrency;
219 U_CALLCONV decimfmtAffixPatternValueComparator(UHashTok val1, UHashTok val2) {
220 const AffixPatternsForCurrency* affix_1 =
221 (AffixPatternsForCurrency*)val1.pointer;
222 const AffixPatternsForCurrency* affix_2 =
223 (AffixPatternsForCurrency*)val2.pointer;
224 return affix_1->negPrefixPatternForCurrency ==
225 affix_2->negPrefixPatternForCurrency &&
226 affix_1->negSuffixPatternForCurrency ==
227 affix_2->negSuffixPatternForCurrency &&
228 affix_1->posPrefixPatternForCurrency ==
229 affix_2->posPrefixPatternForCurrency &&
230 affix_1->posSuffixPatternForCurrency ==
231 affix_2->posSuffixPatternForCurrency &&
232 affix_1->patternType == affix_2->patternType;
240 // *****************************************************************************
241 // class DecimalFormat
242 // *****************************************************************************
244 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormat)
246 // Constants for characters used in programmatic (unlocalized) patterns.
247 #define kPatternZeroDigit ((UChar)0x0030) /*'0'*/
248 #define kPatternSignificantDigit ((UChar)0x0040) /*'@'*/
249 #define kPatternGroupingSeparator ((UChar)0x002C) /*','*/
250 #define kPatternDecimalSeparator ((UChar)0x002E) /*'.'*/
251 #define kPatternPerMill ((UChar)0x2030)
252 #define kPatternPercent ((UChar)0x0025) /*'%'*/
253 #define kPatternDigit ((UChar)0x0023) /*'#'*/
254 #define kPatternSeparator ((UChar)0x003B) /*';'*/
255 #define kPatternExponent ((UChar)0x0045) /*'E'*/
256 #define kPatternPlus ((UChar)0x002B) /*'+'*/
257 #define kPatternMinus ((UChar)0x002D) /*'-'*/
258 #define kPatternPadEscape ((UChar)0x002A) /*'*'*/
259 #define kQuote ((UChar)0x0027) /*'\''*/
261 * The CURRENCY_SIGN is the standard Unicode symbol for currency. It
262 * is used in patterns and substitued with either the currency symbol,
263 * or if it is doubled, with the international currency symbol. If the
264 * CURRENCY_SIGN is seen in a pattern, then the decimal separator is
265 * replaced with the monetary decimal separator.
267 #define kCurrencySign ((UChar)0x00A4)
268 #define kDefaultPad ((UChar)0x0020) /* */
270 const int32_t DecimalFormat::kDoubleIntegerDigits = 309;
271 const int32_t DecimalFormat::kDoubleFractionDigits = 340;
273 const int32_t DecimalFormat::kMaxScientificIntegerDigits = 8;
276 * These are the tags we expect to see in normal resource bundle files associated
279 const char DecimalFormat::fgNumberPatterns[]="NumberPatterns"; // Deprecated - not used
280 static const char fgNumberElements[]="NumberElements";
281 static const char fgLatn[]="latn";
282 static const char fgPatterns[]="patterns";
283 static const char fgDecimalFormat[]="decimalFormat";
284 static const char fgCurrencyFormat[]="currencyFormat";
286 static const UChar fgTripleCurrencySign[] = {0xA4, 0xA4, 0xA4, 0};
288 inline int32_t _min(int32_t a, int32_t b) { return (a<b) ? a : b; }
289 inline int32_t _max(int32_t a, int32_t b) { return (a<b) ? b : a; }
291 //------------------------------------------------------------------------------
292 // Constructs a DecimalFormat instance in the default locale.
294 DecimalFormat::DecimalFormat(UErrorCode& status) {
296 UParseError parseError;
297 construct(status, parseError);
300 //------------------------------------------------------------------------------
301 // Constructs a DecimalFormat instance with the specified number format
302 // pattern in the default locale.
304 DecimalFormat::DecimalFormat(const UnicodeString& pattern,
305 UErrorCode& status) {
307 UParseError parseError;
308 construct(status, parseError, &pattern);
311 //------------------------------------------------------------------------------
312 // Constructs a DecimalFormat instance with the specified number format
313 // pattern and the number format symbols in the default locale. The
314 // created instance owns the symbols.
316 DecimalFormat::DecimalFormat(const UnicodeString& pattern,
317 DecimalFormatSymbols* symbolsToAdopt,
318 UErrorCode& status) {
320 UParseError parseError;
321 if (symbolsToAdopt == NULL)
322 status = U_ILLEGAL_ARGUMENT_ERROR;
323 construct(status, parseError, &pattern, symbolsToAdopt);
326 DecimalFormat::DecimalFormat( const UnicodeString& pattern,
327 DecimalFormatSymbols* symbolsToAdopt,
328 UParseError& parseErr,
329 UErrorCode& status) {
331 if (symbolsToAdopt == NULL)
332 status = U_ILLEGAL_ARGUMENT_ERROR;
333 construct(status,parseErr, &pattern, symbolsToAdopt);
336 //------------------------------------------------------------------------------
337 // Constructs a DecimalFormat instance with the specified number format
338 // pattern and the number format symbols in the default locale. The
339 // created instance owns the clone of the symbols.
341 DecimalFormat::DecimalFormat(const UnicodeString& pattern,
342 const DecimalFormatSymbols& symbols,
343 UErrorCode& status) {
345 UParseError parseError;
346 construct(status, parseError, &pattern, new DecimalFormatSymbols(symbols));
349 //------------------------------------------------------------------------------
350 // Constructs a DecimalFormat instance with the specified number format
351 // pattern, the number format symbols, and the number format style.
352 // The created instance owns the clone of the symbols.
354 DecimalFormat::DecimalFormat(const UnicodeString& pattern,
355 DecimalFormatSymbols* symbolsToAdopt,
356 UNumberFormatStyle style,
357 UErrorCode& status) {
360 UParseError parseError;
361 construct(status, parseError, &pattern, symbolsToAdopt);
364 //-----------------------------------------------------------------------------
365 // Common DecimalFormat initialization.
366 // Put all fields of an uninitialized object into a known state.
367 // Common code, shared by all constructors.
368 // Can not fail. Leave the object in good enough shape that the destructor
369 // or assignment operator can run successfully.
371 DecimalFormat::init() {
372 fPosPrefixPattern = 0;
373 fPosSuffixPattern = 0;
374 fNegPrefixPattern = 0;
375 fNegSuffixPattern = 0;
381 fDecimalSeparatorAlwaysShown = FALSE;
383 fUseSignificantDigits = FALSE;
384 fMinSignificantDigits = 1;
385 fMaxSignificantDigits = 6;
386 fUseExponentialNotation = FALSE;
387 fMinExponentDigits = 0;
388 fExponentSignAlwaysShown = FALSE;
390 fRoundingIncrement = 0;
391 fRoundingMode = kRoundHalfEven;
394 fPadPosition = kPadBeforePrefix;
395 fStyle = UNUM_DECIMAL;
396 fCurrencySignCount = fgCurrencySignCountZero;
397 fAffixPatternsForCurrency = NULL;
398 fAffixesForCurrency = NULL;
399 fPluralAffixesForCurrency = NULL;
400 fCurrencyPluralInfo = NULL;
401 #if UCONFIG_HAVE_PARSEALLINPUT
402 fParseAllInput = UNUM_MAYBE;
405 #if UCONFIG_FORMAT_FASTPATHS_49
406 DecimalFormatInternal &data = internalData(fReserved);
407 data.fFastFormatStatus=kFastpathUNKNOWN; // don't try to calculate the fastpath until later.
408 data.fFastParseStatus=kFastpathUNKNOWN; // don't try to calculate the fastpath until later.
413 //------------------------------------------------------------------------------
414 // Constructs a DecimalFormat instance with the specified number format
415 // pattern and the number format symbols in the desired locale. The
416 // created instance owns the symbols.
419 DecimalFormat::construct(UErrorCode& status,
420 UParseError& parseErr,
421 const UnicodeString* pattern,
422 DecimalFormatSymbols* symbolsToAdopt)
424 fSymbols = symbolsToAdopt; // Do this BEFORE aborting on status failure!!!
425 fRoundingIncrement = NULL;
426 fRoundingMode = kRoundHalfEven;
427 fPad = kPatternPadEscape;
428 fPadPosition = kPadBeforePrefix;
429 if (U_FAILURE(status))
432 fPosPrefixPattern = fPosSuffixPattern = NULL;
433 fNegPrefixPattern = fNegSuffixPattern = NULL;
437 fDecimalSeparatorAlwaysShown = FALSE;
438 fUseExponentialNotation = FALSE;
439 fMinExponentDigits = 0;
441 if (fSymbols == NULL)
443 fSymbols = new DecimalFormatSymbols(Locale::getDefault(), status);
445 status = U_MEMORY_ALLOCATION_ERROR;
449 fStaticSets = DecimalFormatStaticSets::getStaticSets(status);
450 if (U_FAILURE(status)) {
453 UErrorCode nsStatus = U_ZERO_ERROR;
454 NumberingSystem *ns = NumberingSystem::createInstance(nsStatus);
455 if (U_FAILURE(nsStatus)) {
461 // Uses the default locale's number format pattern if there isn't
466 UResourceBundle *top = ures_open(NULL, Locale::getDefault().getName(), &status);
468 UResourceBundle *resource = ures_getByKeyWithFallback(top, fgNumberElements, NULL, &status);
469 resource = ures_getByKeyWithFallback(resource, ns->getName(), resource, &status);
470 resource = ures_getByKeyWithFallback(resource, fgPatterns, resource, &status);
471 const UChar *resStr = ures_getStringByKeyWithFallback(resource, fgDecimalFormat, &len, &status);
472 if ( status == U_MISSING_RESOURCE_ERROR && uprv_strcmp(fgLatn,ns->getName())) {
473 status = U_ZERO_ERROR;
474 resource = ures_getByKeyWithFallback(top, fgNumberElements, resource, &status);
475 resource = ures_getByKeyWithFallback(resource, fgLatn, resource, &status);
476 resource = ures_getByKeyWithFallback(resource, fgPatterns, resource, &status);
477 resStr = ures_getStringByKeyWithFallback(resource, fgDecimalFormat, &len, &status);
479 str.setTo(TRUE, resStr, len);
481 ures_close(resource);
487 if (U_FAILURE(status))
492 if (pattern->indexOf((UChar)kCurrencySign) >= 0) {
493 // If it looks like we are going to use a currency pattern
494 // then do the time consuming lookup.
495 setCurrencyForSymbols();
497 setCurrencyInternally(NULL, status);
500 const UnicodeString* patternUsed;
501 UnicodeString currencyPluralPatternForOther;
503 if (fStyle == UNUM_CURRENCY_PLURAL) {
504 fCurrencyPluralInfo = new CurrencyPluralInfo(fSymbols->getLocale(), status);
505 if (U_FAILURE(status)) {
509 // the pattern used in format is not fixed until formatting,
510 // in which, the number is known and
511 // will be used to pick the right pattern based on plural count.
512 // Here, set the pattern as the pattern of plural count == "other".
513 // For most locale, the patterns are probably the same for all
514 // plural count. If not, the right pattern need to be re-applied
516 fCurrencyPluralInfo->getCurrencyPluralPattern(UNICODE_STRING("other", 5), currencyPluralPatternForOther);
517 patternUsed = ¤cyPluralPatternForOther;
519 setCurrencyForSymbols();
522 patternUsed = pattern;
525 if (patternUsed->indexOf(kCurrencySign) != -1) {
526 // initialize for currency, not only for plural format,
527 // but also for mix parsing
528 if (fCurrencyPluralInfo == NULL) {
529 fCurrencyPluralInfo = new CurrencyPluralInfo(fSymbols->getLocale(), status);
530 if (U_FAILURE(status)) {
534 // need it for mix parsing
535 setupCurrencyAffixPatterns(status);
536 // expanded affixes for plural names
537 if (patternUsed->indexOf(fgTripleCurrencySign, 3, 0) != -1) {
538 setupCurrencyAffixes(*patternUsed, TRUE, TRUE, status);
542 applyPatternWithoutExpandAffix(*patternUsed,FALSE, parseErr, status);
545 if (fCurrencySignCount != fgCurrencySignCountInPluralFormat) {
546 expandAffixAdjustWidth(NULL);
549 // If it was a currency format, apply the appropriate rounding by
550 // resetting the currency. NOTE: this copies fCurrency on top of itself.
551 if (fCurrencySignCount != fgCurrencySignCountZero) {
552 setCurrencyInternally(getCurrency(), status);
554 #if UCONFIG_FORMAT_FASTPATHS_49
555 DecimalFormatInternal &data = internalData(fReserved);
556 data.fFastFormatStatus = kFastpathNO; // allow it to be calculated
557 data.fFastParseStatus = kFastpathNO; // allow it to be calculated
564 DecimalFormat::setupCurrencyAffixPatterns(UErrorCode& status) {
565 if (U_FAILURE(status)) {
568 UParseError parseErr;
569 fAffixPatternsForCurrency = initHashForAffixPattern(status);
570 if (U_FAILURE(status)) {
574 NumberingSystem *ns = NumberingSystem::createInstance(fSymbols->getLocale(),status);
575 if (U_FAILURE(status)) {
579 // Save the default currency patterns of this locale.
580 // Here, chose onlyApplyPatternWithoutExpandAffix without
581 // expanding the affix patterns into affixes.
582 UnicodeString currencyPattern;
583 UErrorCode error = U_ZERO_ERROR;
585 UResourceBundle *resource = ures_open(NULL, fSymbols->getLocale().getName(), &error);
586 UResourceBundle *numElements = ures_getByKeyWithFallback(resource, fgNumberElements, NULL, &error);
587 resource = ures_getByKeyWithFallback(numElements, ns->getName(), resource, &error);
588 resource = ures_getByKeyWithFallback(resource, fgPatterns, resource, &error);
590 const UChar *patResStr = ures_getStringByKeyWithFallback(resource, fgCurrencyFormat, &patLen, &error);
591 if ( error == U_MISSING_RESOURCE_ERROR && uprv_strcmp(ns->getName(),fgLatn)) {
592 error = U_ZERO_ERROR;
593 resource = ures_getByKeyWithFallback(numElements, fgLatn, resource, &error);
594 resource = ures_getByKeyWithFallback(resource, fgPatterns, resource, &error);
595 patResStr = ures_getStringByKeyWithFallback(resource, fgCurrencyFormat, &patLen, &error);
597 ures_close(numElements);
598 ures_close(resource);
601 if (U_SUCCESS(error)) {
602 applyPatternWithoutExpandAffix(UnicodeString(patResStr, patLen), false,
604 AffixPatternsForCurrency* affixPtn = new AffixPatternsForCurrency(
610 fAffixPatternsForCurrency->put(UNICODE_STRING("default", 7), affixPtn, status);
613 // save the unique currency plural patterns of this locale.
614 Hashtable* pluralPtn = fCurrencyPluralInfo->fPluralCountToCurrencyUnitPattern;
615 const UHashElement* element = NULL;
617 Hashtable pluralPatternSet;
618 while ((element = pluralPtn->nextElement(pos)) != NULL) {
619 const UHashTok valueTok = element->value;
620 const UnicodeString* value = (UnicodeString*)valueTok.pointer;
621 const UHashTok keyTok = element->key;
622 const UnicodeString* key = (UnicodeString*)keyTok.pointer;
623 if (pluralPatternSet.geti(*value) != 1) {
624 pluralPatternSet.puti(*value, 1, status);
625 applyPatternWithoutExpandAffix(*value, false, parseErr, status);
626 AffixPatternsForCurrency* affixPtn = new AffixPatternsForCurrency(
632 fAffixPatternsForCurrency->put(*key, affixPtn, status);
639 DecimalFormat::setupCurrencyAffixes(const UnicodeString& pattern,
640 UBool setupForCurrentPattern,
641 UBool setupForPluralPattern,
642 UErrorCode& status) {
643 if (U_FAILURE(status)) {
646 UParseError parseErr;
647 if (setupForCurrentPattern) {
648 if (fAffixesForCurrency) {
649 deleteHashForAffix(fAffixesForCurrency);
651 fAffixesForCurrency = initHashForAffix(status);
652 if (U_SUCCESS(status)) {
653 applyPatternWithoutExpandAffix(pattern, false, parseErr, status);
654 const PluralRules* pluralRules = fCurrencyPluralInfo->getPluralRules();
655 StringEnumeration* keywords = pluralRules->getKeywords(status);
656 if (U_SUCCESS(status)) {
657 const UnicodeString* pluralCount;
658 while ((pluralCount = keywords->snext(status)) != NULL) {
659 if ( U_SUCCESS(status) ) {
660 expandAffixAdjustWidth(pluralCount);
661 AffixesForCurrency* affix = new AffixesForCurrency(
662 fNegativePrefix, fNegativeSuffix, fPositivePrefix, fPositiveSuffix);
663 fAffixesForCurrency->put(*pluralCount, affix, status);
671 if (U_FAILURE(status)) {
675 if (setupForPluralPattern) {
676 if (fPluralAffixesForCurrency) {
677 deleteHashForAffix(fPluralAffixesForCurrency);
679 fPluralAffixesForCurrency = initHashForAffix(status);
680 if (U_SUCCESS(status)) {
681 const PluralRules* pluralRules = fCurrencyPluralInfo->getPluralRules();
682 StringEnumeration* keywords = pluralRules->getKeywords(status);
683 if (U_SUCCESS(status)) {
684 const UnicodeString* pluralCount;
685 while ((pluralCount = keywords->snext(status)) != NULL) {
686 if ( U_SUCCESS(status) ) {
688 fCurrencyPluralInfo->getCurrencyPluralPattern(*pluralCount, ptn);
689 applyPatternInternally(*pluralCount, ptn, false, parseErr, status);
690 AffixesForCurrency* affix = new AffixesForCurrency(
691 fNegativePrefix, fNegativeSuffix, fPositivePrefix, fPositiveSuffix);
692 fPluralAffixesForCurrency->put(*pluralCount, affix, status);
702 //------------------------------------------------------------------------------
704 DecimalFormat::~DecimalFormat()
706 delete fPosPrefixPattern;
707 delete fPosSuffixPattern;
708 delete fNegPrefixPattern;
709 delete fNegSuffixPattern;
710 delete fCurrencyChoice;
713 delete fRoundingIncrement;
714 deleteHashForAffixPattern();
715 deleteHashForAffix(fAffixesForCurrency);
716 deleteHashForAffix(fPluralAffixesForCurrency);
717 delete fCurrencyPluralInfo;
720 //------------------------------------------------------------------------------
723 DecimalFormat::DecimalFormat(const DecimalFormat &source) :
724 NumberFormat(source) {
729 //------------------------------------------------------------------------------
730 // assignment operator
733 static void _copy_ptr(T** pdest, const T* source) {
734 if (source == NULL) {
737 } else if (*pdest == NULL) {
738 *pdest = new T(*source);
745 static void _clone_ptr(T** pdest, const T* source) {
747 if (source == NULL) {
750 *pdest = static_cast<T*>(source->clone());
755 DecimalFormat::operator=(const DecimalFormat& rhs)
758 UErrorCode status = U_ZERO_ERROR;
759 NumberFormat::operator=(rhs);
760 fStaticSets = DecimalFormatStaticSets::getStaticSets(status);
761 fPositivePrefix = rhs.fPositivePrefix;
762 fPositiveSuffix = rhs.fPositiveSuffix;
763 fNegativePrefix = rhs.fNegativePrefix;
764 fNegativeSuffix = rhs.fNegativeSuffix;
765 _copy_ptr(&fPosPrefixPattern, rhs.fPosPrefixPattern);
766 _copy_ptr(&fPosSuffixPattern, rhs.fPosSuffixPattern);
767 _copy_ptr(&fNegPrefixPattern, rhs.fNegPrefixPattern);
768 _copy_ptr(&fNegSuffixPattern, rhs.fNegSuffixPattern);
769 _clone_ptr(&fCurrencyChoice, rhs.fCurrencyChoice);
770 setRoundingIncrement(rhs.getRoundingIncrement());
771 fRoundingMode = rhs.fRoundingMode;
772 setMultiplier(rhs.getMultiplier());
773 fGroupingSize = rhs.fGroupingSize;
774 fGroupingSize2 = rhs.fGroupingSize2;
775 fDecimalSeparatorAlwaysShown = rhs.fDecimalSeparatorAlwaysShown;
776 _copy_ptr(&fSymbols, rhs.fSymbols);
777 fUseExponentialNotation = rhs.fUseExponentialNotation;
778 fExponentSignAlwaysShown = rhs.fExponentSignAlwaysShown;
779 fBoolFlags = rhs.fBoolFlags;
780 /*Bertrand A. D. Update 98.03.17*/
781 fCurrencySignCount = rhs.fCurrencySignCount;
783 fMinExponentDigits = rhs.fMinExponentDigits;
786 fFormatWidth = rhs.fFormatWidth;
788 fPadPosition = rhs.fPadPosition;
790 fMinSignificantDigits = rhs.fMinSignificantDigits;
791 fMaxSignificantDigits = rhs.fMaxSignificantDigits;
792 fUseSignificantDigits = rhs.fUseSignificantDigits;
793 fFormatPattern = rhs.fFormatPattern;
795 fCurrencySignCount = rhs.fCurrencySignCount;
796 _clone_ptr(&fCurrencyPluralInfo, rhs.fCurrencyPluralInfo);
797 deleteHashForAffixPattern();
798 if (rhs.fAffixPatternsForCurrency) {
799 UErrorCode status = U_ZERO_ERROR;
800 fAffixPatternsForCurrency = initHashForAffixPattern(status);
801 copyHashForAffixPattern(rhs.fAffixPatternsForCurrency,
802 fAffixPatternsForCurrency, status);
804 deleteHashForAffix(fAffixesForCurrency);
805 if (rhs.fAffixesForCurrency) {
806 UErrorCode status = U_ZERO_ERROR;
807 fAffixesForCurrency = initHashForAffixPattern(status);
808 copyHashForAffix(rhs.fAffixesForCurrency, fAffixesForCurrency, status);
810 deleteHashForAffix(fPluralAffixesForCurrency);
811 if (rhs.fPluralAffixesForCurrency) {
812 UErrorCode status = U_ZERO_ERROR;
813 fPluralAffixesForCurrency = initHashForAffixPattern(status);
814 copyHashForAffix(rhs.fPluralAffixesForCurrency, fPluralAffixesForCurrency, status);
817 #if UCONFIG_FORMAT_FASTPATHS_49
823 //------------------------------------------------------------------------------
826 DecimalFormat::operator==(const Format& that) const
831 // NumberFormat::operator== guarantees this cast is safe
832 const DecimalFormat* other = (DecimalFormat*)&that;
835 // This code makes it easy to determine why two format objects that should
838 if (!NumberFormat::operator==(that)) {
839 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
840 debug("NumberFormat::!=");
842 if (!((fPosPrefixPattern == other->fPosPrefixPattern && // both null
843 fPositivePrefix == other->fPositivePrefix)
844 || (fPosPrefixPattern != 0 && other->fPosPrefixPattern != 0 &&
845 *fPosPrefixPattern == *other->fPosPrefixPattern))) {
846 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
847 debug("Pos Prefix !=");
849 if (!((fPosSuffixPattern == other->fPosSuffixPattern && // both null
850 fPositiveSuffix == other->fPositiveSuffix)
851 || (fPosSuffixPattern != 0 && other->fPosSuffixPattern != 0 &&
852 *fPosSuffixPattern == *other->fPosSuffixPattern))) {
853 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
854 debug("Pos Suffix !=");
856 if (!((fNegPrefixPattern == other->fNegPrefixPattern && // both null
857 fNegativePrefix == other->fNegativePrefix)
858 || (fNegPrefixPattern != 0 && other->fNegPrefixPattern != 0 &&
859 *fNegPrefixPattern == *other->fNegPrefixPattern))) {
860 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
861 debug("Neg Prefix ");
862 if (fNegPrefixPattern == NULL) {
864 debugout(fNegativePrefix);
867 debugout(*fNegPrefixPattern);
870 if (other->fNegPrefixPattern == NULL) {
872 debugout(other->fNegativePrefix);
875 debugout(*other->fNegPrefixPattern);
878 if (!((fNegSuffixPattern == other->fNegSuffixPattern && // both null
879 fNegativeSuffix == other->fNegativeSuffix)
880 || (fNegSuffixPattern != 0 && other->fNegSuffixPattern != 0 &&
881 *fNegSuffixPattern == *other->fNegSuffixPattern))) {
882 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
883 debug("Neg Suffix ");
884 if (fNegSuffixPattern == NULL) {
886 debugout(fNegativeSuffix);
889 debugout(*fNegSuffixPattern);
892 if (other->fNegSuffixPattern == NULL) {
894 debugout(other->fNegativeSuffix);
897 debugout(*other->fNegSuffixPattern);
900 if (!((fRoundingIncrement == other->fRoundingIncrement) // both null
901 || (fRoundingIncrement != NULL &&
902 other->fRoundingIncrement != NULL &&
903 *fRoundingIncrement == *other->fRoundingIncrement))) {
904 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
905 debug("Rounding Increment !=");
907 if (getMultiplier() != other->getMultiplier()) {
908 if (first) { printf("[ "); first = FALSE; }
909 printf("Multiplier %ld != %ld", getMultiplier(), other->getMultiplier());
911 if (fGroupingSize != other->fGroupingSize) {
912 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
913 printf("Grouping Size %ld != %ld", fGroupingSize, other->fGroupingSize);
915 if (fGroupingSize2 != other->fGroupingSize2) {
916 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
917 printf("Secondary Grouping Size %ld != %ld", fGroupingSize2, other->fGroupingSize2);
919 if (fDecimalSeparatorAlwaysShown != other->fDecimalSeparatorAlwaysShown) {
920 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
921 printf("Dec Sep Always %d != %d", fDecimalSeparatorAlwaysShown, other->fDecimalSeparatorAlwaysShown);
923 if (fUseExponentialNotation != other->fUseExponentialNotation) {
924 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
927 if (!(!fUseExponentialNotation ||
928 fMinExponentDigits != other->fMinExponentDigits)) {
929 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
930 debug("Exp Digits !=");
932 if (*fSymbols != *(other->fSymbols)) {
933 if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
936 // TODO Add debug stuff for significant digits here
937 if (fUseSignificantDigits != other->fUseSignificantDigits) {
938 debug("fUseSignificantDigits !=");
940 if (fUseSignificantDigits &&
941 fMinSignificantDigits != other->fMinSignificantDigits) {
942 debug("fMinSignificantDigits !=");
944 if (fUseSignificantDigits &&
945 fMaxSignificantDigits != other->fMaxSignificantDigits) {
946 debug("fMaxSignificantDigits !=");
949 if (!first) { printf(" ]"); }
950 if (fCurrencySignCount != other->fCurrencySignCount) {
951 debug("fCurrencySignCount !=");
953 if (fCurrencyPluralInfo == other->fCurrencyPluralInfo) {
954 debug("fCurrencyPluralInfo == ");
955 if (fCurrencyPluralInfo == NULL) {
956 debug("fCurrencyPluralInfo == NULL");
959 if (fCurrencyPluralInfo != NULL && other->fCurrencyPluralInfo != NULL &&
960 *fCurrencyPluralInfo != *(other->fCurrencyPluralInfo)) {
961 debug("fCurrencyPluralInfo !=");
963 if (fCurrencyPluralInfo != NULL && other->fCurrencyPluralInfo == NULL ||
964 fCurrencyPluralInfo == NULL && other->fCurrencyPluralInfo != NULL) {
965 debug("fCurrencyPluralInfo one NULL, the other not");
967 if (fCurrencyPluralInfo == NULL && other->fCurrencyPluralInfo == NULL) {
968 debug("fCurrencyPluralInfo == ");
973 return (NumberFormat::operator==(that) &&
974 ((fCurrencySignCount == fgCurrencySignCountInPluralFormat) ?
975 (fAffixPatternsForCurrency->equals(*other->fAffixPatternsForCurrency)) :
976 (((fPosPrefixPattern == other->fPosPrefixPattern && // both null
977 fPositivePrefix == other->fPositivePrefix)
978 || (fPosPrefixPattern != 0 && other->fPosPrefixPattern != 0 &&
979 *fPosPrefixPattern == *other->fPosPrefixPattern)) &&
980 ((fPosSuffixPattern == other->fPosSuffixPattern && // both null
981 fPositiveSuffix == other->fPositiveSuffix)
982 || (fPosSuffixPattern != 0 && other->fPosSuffixPattern != 0 &&
983 *fPosSuffixPattern == *other->fPosSuffixPattern)) &&
984 ((fNegPrefixPattern == other->fNegPrefixPattern && // both null
985 fNegativePrefix == other->fNegativePrefix)
986 || (fNegPrefixPattern != 0 && other->fNegPrefixPattern != 0 &&
987 *fNegPrefixPattern == *other->fNegPrefixPattern)) &&
988 ((fNegSuffixPattern == other->fNegSuffixPattern && // both null
989 fNegativeSuffix == other->fNegativeSuffix)
990 || (fNegSuffixPattern != 0 && other->fNegSuffixPattern != 0 &&
991 *fNegSuffixPattern == *other->fNegSuffixPattern)))) &&
992 ((fRoundingIncrement == other->fRoundingIncrement) // both null
993 || (fRoundingIncrement != NULL &&
994 other->fRoundingIncrement != NULL &&
995 *fRoundingIncrement == *other->fRoundingIncrement)) &&
996 getMultiplier() == other->getMultiplier() &&
997 fGroupingSize == other->fGroupingSize &&
998 fGroupingSize2 == other->fGroupingSize2 &&
999 fDecimalSeparatorAlwaysShown == other->fDecimalSeparatorAlwaysShown &&
1000 fUseExponentialNotation == other->fUseExponentialNotation &&
1001 (!fUseExponentialNotation ||
1002 fMinExponentDigits == other->fMinExponentDigits) &&
1003 *fSymbols == *(other->fSymbols) &&
1004 fUseSignificantDigits == other->fUseSignificantDigits &&
1005 (!fUseSignificantDigits ||
1006 (fMinSignificantDigits == other->fMinSignificantDigits &&
1007 fMaxSignificantDigits == other->fMaxSignificantDigits)) &&
1008 fCurrencySignCount == other->fCurrencySignCount &&
1009 ((fCurrencyPluralInfo == other->fCurrencyPluralInfo &&
1010 fCurrencyPluralInfo == NULL) ||
1011 (fCurrencyPluralInfo != NULL && other->fCurrencyPluralInfo != NULL &&
1012 *fCurrencyPluralInfo == *(other->fCurrencyPluralInfo))));
1015 //------------------------------------------------------------------------------
1018 DecimalFormat::clone() const
1020 return new DecimalFormat(*this);
1025 DecimalFormat::getFixedDecimal(double number, UErrorCode &status) const {
1026 FixedDecimal result;
1028 if (U_FAILURE(status)) {
1032 if (uprv_isNaN(number) || uprv_isPositiveInfinity(fabs(number))) {
1033 // For NaN and Infinity the state of the formatter is ignored.
1034 result.init(number);
1038 if (fMultiplier == NULL && fScale == 0 && fRoundingIncrement == 0 && areSignificantDigitsUsed() == FALSE &&
1039 result.quickInit(number) && result.visibleDecimalDigitCount <= getMaximumFractionDigits()) {
1040 // Fast Path. Construction of an exact FixedDecimal directly from the double, without passing
1041 // through a DigitList, was successful, and the formatter is doing nothing tricky with rounding.
1042 // printf("getFixedDecimal(%g): taking fast path.\n", number);
1043 result.adjustForMinFractionDigits(getMinimumFractionDigits());
1045 // Slow path. Create a DigitList, and have this formatter round it according to the
1046 // requirements of the format, and fill the fixedDecimal from that.
1049 result = getFixedDecimal(digits, status);
1054 // MSVC optimizer bug?
1055 // turn off optimization as it causes different behavior in the int64->double->int64 conversion
1056 #if defined (_MSC_VER)
1057 #pragma optimize ( "", off )
1060 DecimalFormat::getFixedDecimal(const Formattable &number, UErrorCode &status) const {
1061 if (U_FAILURE(status)) {
1062 return FixedDecimal();
1064 if (!number.isNumeric()) {
1065 status = U_ILLEGAL_ARGUMENT_ERROR;
1066 return FixedDecimal();
1069 DigitList *dl = number.getDigitList();
1071 DigitList clonedDL(*dl);
1072 return getFixedDecimal(clonedDL, status);
1075 Formattable::Type type = number.getType();
1076 if (type == Formattable::kDouble || type == Formattable::kLong) {
1077 return getFixedDecimal(number.getDouble(status), status);
1080 if (type == Formattable::kInt64) {
1081 // "volatile" here is a workaround to avoid optimization issues.
1082 volatile double fdv = number.getDouble(status);
1083 // Note: conversion of int64_t -> double rounds with some compilers to
1084 // values beyond what can be represented as a 64 bit int. Subsequent
1085 // testing or conversion with int64_t produces bad results.
1086 // So filter the problematic values, route them to DigitList.
1087 if (fdv != (double)U_INT64_MAX && fdv != (double)U_INT64_MIN &&
1088 number.getInt64() == (int64_t)fdv) {
1089 return getFixedDecimal(number.getDouble(status), status);
1093 // The only case left is type==int64_t, with a value with more digits than a double can represent.
1094 // Any formattable originating as a big decimal will have had a pre-existing digit list.
1095 // Any originating as a double or int32 will have been handled as a double.
1097 U_ASSERT(type == Formattable::kInt64);
1099 digits.set(number.getInt64());
1100 return getFixedDecimal(digits, status);
1102 // end workaround MSVC optimizer bug
1103 #if defined (_MSC_VER)
1104 #pragma optimize ( "", on )
1108 // Create a fixed decimal from a DigitList.
1109 // The digit list may be modified.
1110 // Internal function only.
1112 DecimalFormat::getFixedDecimal(DigitList &number, UErrorCode &status) const {
1113 // Round the number according to the requirements of this Format.
1114 FixedDecimal result;
1115 _round(number, number, result.isNegative, status);
1117 // The int64_t fields in FixedDecimal can easily overflow.
1118 // In deciding what to discard in this event, consider that fixedDecimal
1119 // is being used only with PluralRules, and those rules mostly look at least significant
1120 // few digits of the integer part, and whether the fraction part is zero or not.
1122 // So, in case of overflow when filling in the fields of the FixedDecimal object,
1123 // for the integer part, discard the most significant digits.
1124 // for the fraction part, discard the least significant digits,
1125 // don't truncate the fraction value to zero.
1126 // For simplicity, the int64_t fields are limited to 18 decimal digits, even
1127 // though they could hold most (but not all) 19 digit values.
1130 int32_t di = number.getDecimalAt()-18; // Take at most 18 digits.
1134 result.intValue = 0;
1135 for (; di<number.getDecimalAt(); di++) {
1136 result.intValue = result.intValue * 10 + (number.getDigit(di) & 0x0f);
1138 if (result.intValue == 0 && number.getDecimalAt()-18 > 0) {
1139 // The number is something like 100000000000000000000000.
1140 // More than 18 digits integer digits, but the least significant 18 are all zero.
1141 // We don't want to return zero as the int part, but want to keep zeros
1142 // for several of the least significant digits.
1143 result.intValue = 100000000000000000LL;
1147 result.decimalDigits = result.decimalDigitsWithoutTrailingZeros = result.visibleDecimalDigitCount = 0;
1148 for (di = number.getDecimalAt(); di < number.getCount(); di++) {
1149 result.visibleDecimalDigitCount++;
1150 if (result.decimalDigits < 100000000000000000LL) {
1151 // 9223372036854775807 Largest 64 bit signed integer
1152 int32_t digitVal = number.getDigit(di) & 0x0f; // getDigit() returns a char, '0'-'9'.
1153 result.decimalDigits = result.decimalDigits * 10 + digitVal;
1155 result.decimalDigitsWithoutTrailingZeros = result.decimalDigits;
1160 result.hasIntegerValue = (result.decimalDigits == 0);
1162 // Trailing fraction zeros. The format specification may require more trailing
1163 // zeros than the numeric value. Add any such on now.
1165 int32_t minFractionDigits;
1166 if (areSignificantDigitsUsed()) {
1167 minFractionDigits = getMinimumSignificantDigits() - number.getDecimalAt();
1168 if (minFractionDigits < 0) {
1169 minFractionDigits = 0;
1172 minFractionDigits = getMinimumFractionDigits();
1174 result.adjustForMinFractionDigits(minFractionDigits);
1180 //------------------------------------------------------------------------------
1183 DecimalFormat::format(int32_t number,
1184 UnicodeString& appendTo,
1185 FieldPosition& fieldPosition) const
1187 return format((int64_t)number, appendTo, fieldPosition);
1191 DecimalFormat::format(int32_t number,
1192 UnicodeString& appendTo,
1193 FieldPosition& fieldPosition,
1194 UErrorCode& status) const
1196 return format((int64_t)number, appendTo, fieldPosition, status);
1200 DecimalFormat::format(int32_t number,
1201 UnicodeString& appendTo,
1202 FieldPositionIterator* posIter,
1203 UErrorCode& status) const
1205 return format((int64_t)number, appendTo, posIter, status);
1209 #if UCONFIG_FORMAT_FASTPATHS_49
1210 void DecimalFormat::handleChanged() {
1211 DecimalFormatInternal &data = internalData(fReserved);
1213 if(data.fFastFormatStatus == kFastpathUNKNOWN || data.fFastParseStatus == kFastpathUNKNOWN) {
1214 return; // still constructing. Wait.
1217 data.fFastParseStatus = data.fFastFormatStatus = kFastpathNO;
1219 #if UCONFIG_HAVE_PARSEALLINPUT
1220 if(fParseAllInput == UNUM_NO) {
1221 debug("No Parse fastpath: fParseAllInput==UNUM_NO");
1224 if (fFormatWidth!=0) {
1225 debug("No Parse fastpath: fFormatWidth");
1226 } else if(fPositivePrefix.length()>0) {
1227 debug("No Parse fastpath: positive prefix");
1228 } else if(fPositiveSuffix.length()>0) {
1229 debug("No Parse fastpath: positive suffix");
1230 } else if(fNegativePrefix.length()>1
1231 || ((fNegativePrefix.length()==1) && (fNegativePrefix.charAt(0)!=0x002D))) {
1232 debug("No Parse fastpath: negative prefix that isn't '-'");
1233 } else if(fNegativeSuffix.length()>0) {
1234 debug("No Parse fastpath: negative suffix");
1236 data.fFastParseStatus = kFastpathYES;
1237 debug("parse fastpath: YES");
1240 if (fGroupingSize!=0 && isGroupingUsed()) {
1241 debug("No format fastpath: fGroupingSize!=0 and grouping is used");
1243 printf("groupingsize=%d\n", fGroupingSize);
1245 } else if(fGroupingSize2!=0 && isGroupingUsed()) {
1246 debug("No format fastpath: fGroupingSize2!=0");
1247 } else if(fUseExponentialNotation) {
1248 debug("No format fastpath: fUseExponentialNotation");
1249 } else if(fFormatWidth!=0) {
1250 debug("No format fastpath: fFormatWidth!=0");
1251 } else if(fMinSignificantDigits!=1) {
1252 debug("No format fastpath: fMinSignificantDigits!=1");
1253 } else if(fMultiplier!=NULL) {
1254 debug("No format fastpath: fMultiplier!=NULL");
1255 } else if(fScale!=0) {
1256 debug("No format fastpath: fScale!=0");
1257 } else if(0x0030 != getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0)) {
1258 debug("No format fastpath: 0x0030 != getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0)");
1259 } else if(fDecimalSeparatorAlwaysShown) {
1260 debug("No format fastpath: fDecimalSeparatorAlwaysShown");
1261 } else if(getMinimumFractionDigits()>0) {
1262 debug("No format fastpath: fMinFractionDigits>0");
1263 } else if(fCurrencySignCount != fgCurrencySignCountZero) {
1264 debug("No format fastpath: fCurrencySignCount != fgCurrencySignCountZero");
1265 } else if(fRoundingIncrement!=0) {
1266 debug("No format fastpath: fRoundingIncrement!=0");
1268 data.fFastFormatStatus = kFastpathYES;
1269 debug("format:kFastpathYES!");
1275 //------------------------------------------------------------------------------
1278 DecimalFormat::format(int64_t number,
1279 UnicodeString& appendTo,
1280 FieldPosition& fieldPosition) const
1282 UErrorCode status = U_ZERO_ERROR; /* ignored */
1283 FieldPositionOnlyHandler handler(fieldPosition);
1284 return _format(number, appendTo, handler, status);
1288 DecimalFormat::format(int64_t number,
1289 UnicodeString& appendTo,
1290 FieldPosition& fieldPosition,
1291 UErrorCode& status) const
1293 FieldPositionOnlyHandler handler(fieldPosition);
1294 return _format(number, appendTo, handler, status);
1298 DecimalFormat::format(int64_t number,
1299 UnicodeString& appendTo,
1300 FieldPositionIterator* posIter,
1301 UErrorCode& status) const
1303 FieldPositionIteratorHandler handler(posIter, status);
1304 return _format(number, appendTo, handler, status);
1308 DecimalFormat::_format(int64_t number,
1309 UnicodeString& appendTo,
1310 FieldPositionHandler& handler,
1311 UErrorCode &status) const
1313 // Bottleneck function for formatting int64_t
1314 if (U_FAILURE(status)) {
1318 #if UCONFIG_FORMAT_FASTPATHS_49
1319 // const UnicodeString *posPrefix = fPosPrefixPattern;
1320 // const UnicodeString *posSuffix = fPosSuffixPattern;
1321 // const UnicodeString *negSuffix = fNegSuffixPattern;
1323 const DecimalFormatInternal &data = internalData(fReserved);
1327 printf("fastpath? [%d]\n", number);
1330 if( data.fFastFormatStatus==kFastpathYES) {
1332 #define kZero 0x0030
1333 const int32_t MAX_IDX = MAX_DIGITS+2;
1334 UChar outputStr[MAX_IDX];
1335 int32_t destIdx = MAX_IDX;
1336 outputStr[--destIdx] = 0; // term
1340 // Negative numbers are slightly larger than positive
1341 // output the first digit (or the leading zero)
1342 outputStr[--destIdx] = (-(n % 10) + kZero);
1345 // get any remaining digits
1347 outputStr[--destIdx] = (n % 10) + kZero;
1352 // Slide the number to the start of the output str
1353 U_ASSERT(destIdx >= 0);
1354 int32_t length = MAX_IDX - destIdx -1;
1355 /*int32_t prefixLen = */ appendAffix(appendTo, number, handler, number<0, TRUE);
1356 int32_t maxIntDig = getMaximumIntegerDigits();
1357 int32_t destlength = length<=maxIntDig?length:maxIntDig; // dest length pinned to max int digits
1359 if(length>maxIntDig && fBoolFlags.contains(UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS)) {
1360 status = U_ILLEGAL_ARGUMENT_ERROR;
1363 int32_t prependZero = getMinimumIntegerDigits() - destlength;
1366 printf("prependZero=%d, length=%d, minintdig=%d maxintdig=%d destlength=%d skip=%d\n", prependZero, length, getMinimumIntegerDigits(), maxIntDig, destlength, length-destlength);
1368 int32_t intBegin = appendTo.length();
1370 while((prependZero--)>0) {
1371 appendTo.append((UChar)0x0030); // '0'
1374 appendTo.append(outputStr+destIdx+
1375 (length-destlength), // skip any leading digits
1377 handler.addAttribute(kIntegerField, intBegin, appendTo.length());
1379 /*int32_t suffixLen =*/ appendAffix(appendTo, number, handler, number<0, FALSE);
1381 //outputStr[length]=0;
1384 printf("Writing [%s] length [%d] max %d for [%d]\n", outputStr+destIdx, length, MAX_IDX, number);
1393 // Else the slow way - via DigitList
1396 return _format(digits, appendTo, handler, status);
1399 //------------------------------------------------------------------------------
1402 DecimalFormat::format( double number,
1403 UnicodeString& appendTo,
1404 FieldPosition& fieldPosition) const
1406 UErrorCode status = U_ZERO_ERROR; /* ignored */
1407 FieldPositionOnlyHandler handler(fieldPosition);
1408 return _format(number, appendTo, handler, status);
1412 DecimalFormat::format( double number,
1413 UnicodeString& appendTo,
1414 FieldPosition& fieldPosition,
1415 UErrorCode& status) const
1417 FieldPositionOnlyHandler handler(fieldPosition);
1418 return _format(number, appendTo, handler, status);
1422 DecimalFormat::format( double number,
1423 UnicodeString& appendTo,
1424 FieldPositionIterator* posIter,
1425 UErrorCode& status) const
1427 FieldPositionIteratorHandler handler(posIter, status);
1428 return _format(number, appendTo, handler, status);
1432 DecimalFormat::_format( double number,
1433 UnicodeString& appendTo,
1434 FieldPositionHandler& handler,
1435 UErrorCode &status) const
1437 if (U_FAILURE(status)) {
1440 // Special case for NaN, sets the begin and end index to be the
1441 // the string length of localized name of NaN.
1442 // TODO: let NaNs go through DigitList.
1443 if (uprv_isNaN(number))
1445 int begin = appendTo.length();
1446 appendTo += getConstSymbol(DecimalFormatSymbols::kNaNSymbol);
1448 handler.addAttribute(kIntegerField, begin, appendTo.length());
1450 addPadding(appendTo, handler, 0, 0);
1456 _format(digits, appendTo, handler, status);
1457 // No way to return status from here.
1461 //------------------------------------------------------------------------------
1465 DecimalFormat::format(const StringPiece &number,
1466 UnicodeString &toAppendTo,
1467 FieldPositionIterator *posIter,
1468 UErrorCode &status) const
1470 #if UCONFIG_FORMAT_FASTPATHS_49
1471 // don't bother if the int64 path is not optimized
1472 int32_t len = number.length();
1474 if(len>0&&len<10) { /* 10 or more digits may not be an int64 */
1475 const char *data = number.data();
1482 if(data[start]=='+') {
1484 } else if(data[start]=='-') {
1489 int32_t place = 1; /* 1, 10, ... */
1490 for(int32_t i=len-1;i>=start;i--) {
1491 if(data[i]>='0'&&data[i]<='9') {
1492 num+=place*(int64_t)(data[i]-'0');
1502 num = -num;// add minus bit
1504 // format as int64_t
1505 return format(num, toAppendTo, posIter, status);
1507 // else fall through
1512 dnum.set(number, status);
1513 if (U_FAILURE(status)) {
1516 FieldPositionIteratorHandler handler(posIter, status);
1517 _format(dnum, toAppendTo, handler, status);
1523 DecimalFormat::format(const DigitList &number,
1524 UnicodeString &appendTo,
1525 FieldPositionIterator *posIter,
1526 UErrorCode &status) const {
1527 FieldPositionIteratorHandler handler(posIter, status);
1528 _format(number, appendTo, handler, status);
1535 DecimalFormat::format(const DigitList &number,
1536 UnicodeString& appendTo,
1538 UErrorCode &status) const {
1539 FieldPositionOnlyHandler handler(pos);
1540 _format(number, appendTo, handler, status);
1545 DecimalFormat::_round(const DigitList &number, DigitList &adjustedNum, UBool& isNegative, UErrorCode &status) const {
1546 if (U_FAILURE(status)) {
1550 // note: number and adjustedNum may refer to the same DigitList, in cases where a copy
1551 // is not needed by the caller.
1553 adjustedNum = number;
1555 if (number.isNaN()) {
1559 // Do this BEFORE checking to see if value is infinite or negative! Sets the
1560 // begin and end index to be length of the string composed of
1561 // localized name of Infinite and the positive/negative localized
1564 adjustedNum.setRoundingMode(fRoundingMode);
1565 if (fMultiplier != NULL) {
1566 adjustedNum.mult(*fMultiplier, status);
1567 if (U_FAILURE(status)) {
1574 ten.set((int32_t)10);
1576 for (int32_t i = fScale ; i > 0 ; i--) {
1577 adjustedNum.mult(ten, status);
1578 if (U_FAILURE(status)) {
1583 for (int32_t i = fScale ; i < 0 ; i++) {
1584 adjustedNum.div(ten, status);
1585 if (U_FAILURE(status)) {
1593 * Note: sign is important for zero as well as non-zero numbers.
1594 * Proper detection of -0.0 is needed to deal with the
1595 * issues raised by bugs 4106658, 4106667, and 4147706. Liu 7/6/98.
1597 isNegative = !adjustedNum.isPositive();
1599 // Apply rounding after multiplier
1601 adjustedNum.fContext.status &= ~DEC_Inexact;
1602 if (fRoundingIncrement != NULL) {
1603 adjustedNum.div(*fRoundingIncrement, status);
1604 adjustedNum.toIntegralValue();
1605 adjustedNum.mult(*fRoundingIncrement, status);
1607 if (U_FAILURE(status)) {
1611 if (fRoundingMode == kRoundUnnecessary && (adjustedNum.fContext.status & DEC_Inexact)) {
1612 status = U_FORMAT_INEXACT_ERROR;
1616 if (adjustedNum.isInfinite()) {
1620 if (fUseExponentialNotation || areSignificantDigitsUsed()) {
1621 int32_t sigDigits = precision();
1622 if (sigDigits > 0) {
1623 adjustedNum.round(sigDigits);
1626 // Fixed point format. Round to a set number of fraction digits.
1627 int32_t numFractionDigits = precision();
1628 adjustedNum.roundFixedPoint(numFractionDigits);
1630 if (fRoundingMode == kRoundUnnecessary && (adjustedNum.fContext.status & DEC_Inexact)) {
1631 status = U_FORMAT_INEXACT_ERROR;
1638 DecimalFormat::_format(const DigitList &number,
1639 UnicodeString& appendTo,
1640 FieldPositionHandler& handler,
1641 UErrorCode &status) const
1643 if (U_FAILURE(status)) {
1647 // Special case for NaN, sets the begin and end index to be the
1648 // the string length of localized name of NaN.
1651 int begin = appendTo.length();
1652 appendTo += getConstSymbol(DecimalFormatSymbols::kNaNSymbol);
1654 handler.addAttribute(kIntegerField, begin, appendTo.length());
1656 addPadding(appendTo, handler, 0, 0);
1660 DigitList adjustedNum;
1662 _round(number, adjustedNum, isNegative, status);
1663 if (U_FAILURE(status)) {
1667 // Special case for INFINITE,
1668 if (adjustedNum.isInfinite()) {
1669 int32_t prefixLen = appendAffix(appendTo, adjustedNum.getDouble(), handler, isNegative, TRUE);
1671 int begin = appendTo.length();
1672 appendTo += getConstSymbol(DecimalFormatSymbols::kInfinitySymbol);
1674 handler.addAttribute(kIntegerField, begin, appendTo.length());
1676 int32_t suffixLen = appendAffix(appendTo, adjustedNum.getDouble(), handler, isNegative, FALSE);
1678 addPadding(appendTo, handler, prefixLen, suffixLen);
1681 return subformat(appendTo, handler, adjustedNum, FALSE, status);
1685 * Return true if a grouping separator belongs at the given
1686 * position, based on whether grouping is in use and the values of
1687 * the primary and secondary grouping interval.
1688 * @param pos the number of integer digits to the right of
1689 * the current position. Zero indicates the position after the
1690 * rightmost integer digit.
1691 * @return true if a grouping character belongs at the current
1694 UBool DecimalFormat::isGroupingPosition(int32_t pos) const {
1695 UBool result = FALSE;
1696 if (isGroupingUsed() && (pos > 0) && (fGroupingSize > 0)) {
1697 if ((fGroupingSize2 > 0) && (pos > fGroupingSize)) {
1698 result = ((pos - fGroupingSize) % fGroupingSize2) == 0;
1700 result = pos % fGroupingSize == 0;
1706 //------------------------------------------------------------------------------
1709 * Complete the formatting of a finite number. On entry, the DigitList must
1710 * be filled in with the correct digits.
1713 DecimalFormat::subformat(UnicodeString& appendTo,
1714 FieldPositionHandler& handler,
1717 UErrorCode& status) const
1720 // DigitList returns digits as '0' thru '9', so we will need to
1721 // always need to subtract the character 0 to get the numeric value to use for indexing.
1723 UChar32 localizedDigits[10];
1724 localizedDigits[0] = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
1725 localizedDigits[1] = getConstSymbol(DecimalFormatSymbols::kOneDigitSymbol).char32At(0);
1726 localizedDigits[2] = getConstSymbol(DecimalFormatSymbols::kTwoDigitSymbol).char32At(0);
1727 localizedDigits[3] = getConstSymbol(DecimalFormatSymbols::kThreeDigitSymbol).char32At(0);
1728 localizedDigits[4] = getConstSymbol(DecimalFormatSymbols::kFourDigitSymbol).char32At(0);
1729 localizedDigits[5] = getConstSymbol(DecimalFormatSymbols::kFiveDigitSymbol).char32At(0);
1730 localizedDigits[6] = getConstSymbol(DecimalFormatSymbols::kSixDigitSymbol).char32At(0);
1731 localizedDigits[7] = getConstSymbol(DecimalFormatSymbols::kSevenDigitSymbol).char32At(0);
1732 localizedDigits[8] = getConstSymbol(DecimalFormatSymbols::kEightDigitSymbol).char32At(0);
1733 localizedDigits[9] = getConstSymbol(DecimalFormatSymbols::kNineDigitSymbol).char32At(0);
1735 const UnicodeString *grouping ;
1736 if(fCurrencySignCount == fgCurrencySignCountZero) {
1737 grouping = &getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol);
1739 grouping = &getConstSymbol(DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol);
1741 const UnicodeString *decimal;
1742 if(fCurrencySignCount == fgCurrencySignCountZero) {
1743 decimal = &getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
1745 decimal = &getConstSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol);
1747 UBool useSigDig = areSignificantDigitsUsed();
1748 int32_t maxIntDig = getMaximumIntegerDigits();
1749 int32_t minIntDig = getMinimumIntegerDigits();
1751 // Appends the prefix.
1752 double doubleValue = digits.getDouble();
1753 int32_t prefixLen = appendAffix(appendTo, doubleValue, handler, !digits.isPositive(), TRUE);
1755 if (fUseExponentialNotation)
1757 int currentLength = appendTo.length();
1758 int intBegin = currentLength;
1762 int32_t minFracDig = 0;
1764 maxIntDig = minIntDig = 1;
1765 minFracDig = getMinimumSignificantDigits() - 1;
1767 minFracDig = getMinimumFractionDigits();
1768 if (maxIntDig > kMaxScientificIntegerDigits) {
1770 if (maxIntDig < minIntDig) {
1771 maxIntDig = minIntDig;
1774 if (maxIntDig > minIntDig) {
1779 // Minimum integer digits are handled in exponential format by
1780 // adjusting the exponent. For example, 0.01234 with 3 minimum
1781 // integer digits is "123.4E-4".
1783 // Maximum integer digits are interpreted as indicating the
1784 // repeating range. This is useful for engineering notation, in
1785 // which the exponent is restricted to a multiple of 3. For
1786 // example, 0.01234 with 3 maximum integer digits is "12.34e-3".
1787 // If maximum integer digits are defined and are larger than
1788 // minimum integer digits, then minimum integer digits are
1790 digits.reduce(); // Removes trailing zero digits.
1791 int32_t exponent = digits.getDecimalAt();
1792 if (maxIntDig > 1 && maxIntDig != minIntDig) {
1793 // A exponent increment is defined; adjust to it.
1794 exponent = (exponent > 0) ? (exponent - 1) / maxIntDig
1795 : (exponent / maxIntDig) - 1;
1796 exponent *= maxIntDig;
1798 // No exponent increment is defined; use minimum integer digits.
1799 // If none is specified, as in "#E0", generate 1 integer digit.
1800 exponent -= (minIntDig > 0 || minFracDig > 0)
1804 // We now output a minimum number of digits, and more if there
1805 // are more digits, up to the maximum number of digits. We
1806 // place the decimal point after the "integer" digits, which
1807 // are the first (decimalAt - exponent) digits.
1808 int32_t minimumDigits = minIntDig + minFracDig;
1809 // The number of integer digits is handled specially if the number
1810 // is zero, since then there may be no digits.
1811 int32_t integerDigits = digits.isZero() ? minIntDig :
1812 digits.getDecimalAt() - exponent;
1813 int32_t totalDigits = digits.getCount();
1814 if (minimumDigits > totalDigits)
1815 totalDigits = minimumDigits;
1816 if (integerDigits > totalDigits)
1817 totalDigits = integerDigits;
1819 // totalDigits records total number of digits needs to be processed
1821 for (i=0; i<totalDigits; ++i)
1823 if (i == integerDigits)
1825 intEnd = appendTo.length();
1826 handler.addAttribute(kIntegerField, intBegin, intEnd);
1828 appendTo += *decimal;
1830 fracBegin = appendTo.length();
1831 handler.addAttribute(kDecimalSeparatorField, fracBegin - 1, fracBegin);
1833 // Restores the digit character or pads the buffer with zeros.
1834 UChar32 c = (UChar32)((i < digits.getCount()) ?
1835 localizedDigits[digits.getDigitValue(i)] :
1836 localizedDigits[0]);
1840 currentLength = appendTo.length();
1843 handler.addAttribute(kIntegerField, intBegin, currentLength);
1845 if (fracBegin > 0) {
1846 handler.addAttribute(kFractionField, fracBegin, currentLength);
1849 // The exponent is output using the pattern-specified minimum
1850 // exponent digits. There is no maximum limit to the exponent
1851 // digits, since truncating the exponent would appendTo in an
1852 // unacceptable inaccuracy.
1853 appendTo += getConstSymbol(DecimalFormatSymbols::kExponentialSymbol);
1855 handler.addAttribute(kExponentSymbolField, currentLength, appendTo.length());
1856 currentLength = appendTo.length();
1858 // For zero values, we force the exponent to zero. We
1859 // must do this here, and not earlier, because the value
1860 // is used to determine integer digit count above.
1861 if (digits.isZero())
1865 appendTo += getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
1866 handler.addAttribute(kExponentSignField, currentLength, appendTo.length());
1867 } else if (fExponentSignAlwaysShown) {
1868 appendTo += getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
1869 handler.addAttribute(kExponentSignField, currentLength, appendTo.length());
1872 currentLength = appendTo.length();
1874 DigitList expDigits;
1875 expDigits.set(exponent);
1877 int expDig = fMinExponentDigits;
1878 if (fUseExponentialNotation && expDig < 1) {
1881 for (i=expDigits.getDecimalAt(); i<expDig; ++i)
1882 appendTo += (localizedDigits[0]);
1884 for (i=0; i<expDigits.getDecimalAt(); ++i)
1886 UChar32 c = (UChar32)((i < expDigits.getCount()) ?
1887 localizedDigits[expDigits.getDigitValue(i)] :
1888 localizedDigits[0]);
1892 handler.addAttribute(kExponentField, currentLength, appendTo.length());
1894 else // Not using exponential notation
1896 int currentLength = appendTo.length();
1897 int intBegin = currentLength;
1899 int32_t sigCount = 0;
1900 int32_t minSigDig = getMinimumSignificantDigits();
1901 int32_t maxSigDig = getMaximumSignificantDigits();
1904 maxSigDig = INT32_MAX;
1907 // Output the integer portion. Here 'count' is the total
1908 // number of integer digits we will display, including both
1909 // leading zeros required to satisfy getMinimumIntegerDigits,
1910 // and actual digits present in the number.
1911 int32_t count = useSigDig ?
1912 _max(1, digits.getDecimalAt()) : minIntDig;
1913 if (digits.getDecimalAt() > 0 && count < digits.getDecimalAt()) {
1914 count = digits.getDecimalAt();
1917 // Handle the case where getMaximumIntegerDigits() is smaller
1918 // than the real number of integer digits. If this is so, we
1919 // output the least significant max integer digits. For example,
1920 // the value 1997 printed with 2 max integer digits is just "97".
1922 int32_t digitIndex = 0; // Index into digitList.fDigits[]
1923 if (count > maxIntDig && maxIntDig >= 0) {
1925 digitIndex = digits.getDecimalAt() - count;
1926 if(fBoolFlags.contains(UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS)) {
1927 status = U_ILLEGAL_ARGUMENT_ERROR;
1931 int32_t sizeBeforeIntegerPart = appendTo.length();
1934 for (i=count-1; i>=0; --i)
1936 if (i < digits.getDecimalAt() && digitIndex < digits.getCount() &&
1937 sigCount < maxSigDig) {
1938 // Output a real digit
1939 appendTo += (UChar32)localizedDigits[digits.getDigitValue(digitIndex++)];
1944 // Output a zero (leading or trailing)
1945 appendTo += localizedDigits[0];
1951 // Output grouping separator if necessary.
1952 if (isGroupingPosition(i)) {
1953 currentLength = appendTo.length();
1954 appendTo.append(*grouping);
1955 handler.addAttribute(kGroupingSeparatorField, currentLength, appendTo.length());
1959 // This handles the special case of formatting 0. For zero only, we count the
1960 // zero to the left of the decimal point as one signficant digit. Ordinarily we
1961 // do not count any leading 0's as significant. If the number we are formatting
1962 // is not zero, then either sigCount or digits.getCount() will be non-zero.
1963 if (sigCount == 0 && digits.getCount() == 0) {
1967 // TODO(dlf): this looks like it was a bug, we marked the int field as ending
1968 // before the zero was generated.
1969 // Record field information for caller.
1970 // if (fieldPosition.getField() == NumberFormat::kIntegerField)
1971 // fieldPosition.setEndIndex(appendTo.length());
1973 // Determine whether or not there are any printable fractional
1974 // digits. If we've used up the digits we know there aren't.
1975 UBool fractionPresent = (!isInteger && digitIndex < digits.getCount()) ||
1976 (useSigDig ? (sigCount < minSigDig) : (getMinimumFractionDigits() > 0));
1978 // If there is no fraction present, and we haven't printed any
1979 // integer digits, then print a zero. Otherwise we won't print
1980 // _any_ digits, and we won't be able to parse this string.
1981 if (!fractionPresent && appendTo.length() == sizeBeforeIntegerPart)
1982 appendTo += localizedDigits[0];
1984 currentLength = appendTo.length();
1985 handler.addAttribute(kIntegerField, intBegin, currentLength);
1987 // Output the decimal separator if we always do so.
1988 if (fDecimalSeparatorAlwaysShown || fractionPresent) {
1989 appendTo += *decimal;
1990 handler.addAttribute(kDecimalSeparatorField, currentLength, appendTo.length());
1991 currentLength = appendTo.length();
1994 int fracBegin = currentLength;
1996 count = useSigDig ? INT32_MAX : getMaximumFractionDigits();
1997 if (useSigDig && (sigCount == maxSigDig ||
1998 (sigCount >= minSigDig && digitIndex == digits.getCount()))) {
2002 for (i=0; i < count; ++i) {
2003 // Here is where we escape from the loop. We escape
2004 // if we've output the maximum fraction digits
2005 // (specified in the for expression above). We also
2006 // stop when we've output the minimum digits and
2007 // either: we have an integer, so there is no
2008 // fractional stuff to display, or we're out of
2009 // significant digits.
2010 if (!useSigDig && i >= getMinimumFractionDigits() &&
2011 (isInteger || digitIndex >= digits.getCount())) {
2015 // Output leading fractional zeros. These are zeros
2016 // that come after the decimal but before any
2017 // significant digits. These are only output if
2018 // abs(number being formatted) < 1.0.
2019 if (-1-i > (digits.getDecimalAt()-1)) {
2020 appendTo += localizedDigits[0];
2024 // Output a digit, if we have any precision left, or a
2025 // zero if we don't. We don't want to output noise digits.
2026 if (!isInteger && digitIndex < digits.getCount()) {
2027 appendTo += (UChar32)localizedDigits[digits.getDigitValue(digitIndex++)];
2029 appendTo += localizedDigits[0];
2032 // If we reach the maximum number of significant
2033 // digits, or if we output all the real digits and
2034 // reach the minimum, then we are done.
2037 (sigCount == maxSigDig ||
2038 (digitIndex == digits.getCount() && sigCount >= minSigDig))) {
2043 handler.addAttribute(kFractionField, fracBegin, appendTo.length());
2046 int32_t suffixLen = appendAffix(appendTo, doubleValue, handler, !digits.isPositive(), FALSE);
2048 addPadding(appendTo, handler, prefixLen, suffixLen);
2053 * Inserts the character fPad as needed to expand result to fFormatWidth.
2054 * @param result the string to be padded
2056 void DecimalFormat::addPadding(UnicodeString& appendTo,
2057 FieldPositionHandler& handler,
2059 int32_t suffixLen) const
2061 if (fFormatWidth > 0) {
2062 int32_t len = fFormatWidth - appendTo.length();
2064 UnicodeString padding;
2065 for (int32_t i=0; i<len; ++i) {
2068 switch (fPadPosition) {
2069 case kPadAfterPrefix:
2070 appendTo.insert(prefixLen, padding);
2072 case kPadBeforePrefix:
2073 appendTo.insert(0, padding);
2075 case kPadBeforeSuffix:
2076 appendTo.insert(appendTo.length() - suffixLen, padding);
2078 case kPadAfterSuffix:
2079 appendTo += padding;
2082 if (fPadPosition == kPadBeforePrefix || fPadPosition == kPadAfterPrefix) {
2083 handler.shiftLast(len);
2089 //------------------------------------------------------------------------------
2092 DecimalFormat::parse(const UnicodeString& text,
2093 Formattable& result,
2094 ParsePosition& parsePosition) const {
2095 parse(text, result, parsePosition, NULL);
2098 CurrencyAmount* DecimalFormat::parseCurrency(const UnicodeString& text,
2099 ParsePosition& pos) const {
2100 Formattable parseResult;
2101 int32_t start = pos.getIndex();
2102 UChar curbuf[4] = {};
2103 parse(text, parseResult, pos, curbuf);
2104 if (pos.getIndex() != start) {
2105 UErrorCode ec = U_ZERO_ERROR;
2106 LocalPointer<CurrencyAmount> currAmt(new CurrencyAmount(parseResult, curbuf, ec));
2107 if (U_FAILURE(ec)) {
2108 pos.setIndex(start); // indicate failure
2110 return currAmt.orphan();
2117 * Parses the given text as a number, optionally providing a currency amount.
2118 * @param text the string to parse
2119 * @param result output parameter for the numeric result.
2120 * @param parsePosition input-output position; on input, the
2121 * position within text to match; must have 0 <= pos.getIndex() <
2122 * text.length(); on output, the position after the last matched
2123 * character. If the parse fails, the position in unchanged upon
2125 * @param currency if non-NULL, it should point to a 4-UChar buffer.
2126 * In this case the text is parsed as a currency format, and the
2127 * ISO 4217 code for the parsed currency is put into the buffer.
2128 * Otherwise the text is parsed as a non-currency format.
2130 void DecimalFormat::parse(const UnicodeString& text,
2131 Formattable& result,
2132 ParsePosition& parsePosition,
2133 UChar* currency) const {
2134 int32_t startIdx, backup;
2135 int32_t i = startIdx = backup = parsePosition.getIndex();
2137 // clear any old contents in the result. In particular, clears any DigitList
2138 // that it may be holding.
2140 if (currency != NULL) {
2141 for (int32_t ci=0; ci<4; ci++) {
2146 // Handle NaN as a special case:
2148 // Skip padding characters, if around prefix
2149 if (fFormatWidth > 0 && (fPadPosition == kPadBeforePrefix ||
2150 fPadPosition == kPadAfterPrefix)) {
2151 i = skipPadding(text, i);
2155 // skip any leading whitespace
2156 i = backup = skipUWhiteSpace(text, i);
2159 // If the text is composed of the representation of NaN, returns NaN.length
2160 const UnicodeString *nan = &getConstSymbol(DecimalFormatSymbols::kNaNSymbol);
2161 int32_t nanLen = (text.compare(i, nan->length(), *nan)
2162 ? 0 : nan->length());
2165 if (fFormatWidth > 0 && (fPadPosition == kPadBeforeSuffix ||
2166 fPadPosition == kPadAfterSuffix)) {
2167 i = skipPadding(text, i);
2169 parsePosition.setIndex(i);
2170 result.setDouble(uprv_getNaN());
2174 // NaN parse failed; start over
2176 parsePosition.setIndex(i);
2178 // status is used to record whether a number is infinite.
2179 UBool status[fgStatusLength];
2181 DigitList *digits = result.getInternalDigitList(); // get one from the stack buffer
2182 if (digits == NULL) {
2183 return; // no way to report error from here.
2186 if (fCurrencySignCount != fgCurrencySignCountZero) {
2187 if (!parseForCurrency(text, parsePosition, *digits,
2188 status, currency)) {
2193 fNegPrefixPattern, fNegSuffixPattern,
2194 fPosPrefixPattern, fPosSuffixPattern,
2195 FALSE, UCURR_SYMBOL_NAME,
2196 parsePosition, *digits, status, currency)) {
2197 debug("!subparse(...) - rewind");
2198 parsePosition.setIndex(startIdx);
2204 if (status[fgStatusInfinite]) {
2205 double inf = uprv_getInfinity();
2206 result.setDouble(digits->isPositive() ? inf : -inf);
2207 // TODO: set the dl to infinity, and let it fall into the code below.
2212 if (fMultiplier != NULL) {
2213 UErrorCode ec = U_ZERO_ERROR;
2214 digits->div(*fMultiplier, ec);
2219 ten.set((int32_t)10);
2221 for (int32_t i = fScale; i > 0; i--) {
2222 UErrorCode ec = U_ZERO_ERROR;
2223 digits->div(ten,ec);
2226 for (int32_t i = fScale; i < 0; i++) {
2227 UErrorCode ec = U_ZERO_ERROR;
2228 digits->mult(ten,ec);
2233 // Negative zero special case:
2234 // if parsing integerOnly, change to +0, which goes into an int32 in a Formattable.
2235 // if not parsing integerOnly, leave as -0, which a double can represent.
2236 if (digits->isZero() && !digits->isPositive() && isParseIntegerOnly()) {
2237 digits->setPositive(TRUE);
2239 result.adoptDigitList(digits);
2246 DecimalFormat::parseForCurrency(const UnicodeString& text,
2247 ParsePosition& parsePosition,
2250 UChar* currency) const {
2251 int origPos = parsePosition.getIndex();
2252 int maxPosIndex = origPos;
2253 int maxErrorPos = -1;
2254 // First, parse against current pattern.
2255 // Since current pattern could be set by applyPattern(),
2256 // it could be an arbitrary pattern, and it may not be the one
2257 // defined in current locale.
2258 UBool tmpStatus[fgStatusLength];
2259 ParsePosition tmpPos(origPos);
2260 DigitList tmpDigitList;
2262 if (fStyle == UNUM_CURRENCY_PLURAL) {
2263 found = subparse(text,
2264 fNegPrefixPattern, fNegSuffixPattern,
2265 fPosPrefixPattern, fPosSuffixPattern,
2266 TRUE, UCURR_LONG_NAME,
2267 tmpPos, tmpDigitList, tmpStatus, currency);
2269 found = subparse(text,
2270 fNegPrefixPattern, fNegSuffixPattern,
2271 fPosPrefixPattern, fPosSuffixPattern,
2272 TRUE, UCURR_SYMBOL_NAME,
2273 tmpPos, tmpDigitList, tmpStatus, currency);
2276 if (tmpPos.getIndex() > maxPosIndex) {
2277 maxPosIndex = tmpPos.getIndex();
2278 for (int32_t i = 0; i < fgStatusLength; ++i) {
2279 status[i] = tmpStatus[i];
2281 digits = tmpDigitList;
2284 maxErrorPos = tmpPos.getErrorIndex();
2286 // Then, parse against affix patterns.
2287 // Those are currency patterns and currency plural patterns.
2289 const UHashElement* element = NULL;
2290 while ( (element = fAffixPatternsForCurrency->nextElement(pos)) != NULL ) {
2291 const UHashTok valueTok = element->value;
2292 const AffixPatternsForCurrency* affixPtn = (AffixPatternsForCurrency*)valueTok.pointer;
2293 UBool tmpStatus[fgStatusLength];
2294 ParsePosition tmpPos(origPos);
2295 DigitList tmpDigitList;
2298 debug("trying affix for currency..");
2302 UBool result = subparse(text,
2303 &affixPtn->negPrefixPatternForCurrency,
2304 &affixPtn->negSuffixPatternForCurrency,
2305 &affixPtn->posPrefixPatternForCurrency,
2306 &affixPtn->posSuffixPatternForCurrency,
2307 TRUE, affixPtn->patternType,
2308 tmpPos, tmpDigitList, tmpStatus, currency);
2311 if (tmpPos.getIndex() > maxPosIndex) {
2312 maxPosIndex = tmpPos.getIndex();
2313 for (int32_t i = 0; i < fgStatusLength; ++i) {
2314 status[i] = tmpStatus[i];
2316 digits = tmpDigitList;
2319 maxErrorPos = (tmpPos.getErrorIndex() > maxErrorPos) ?
2320 tmpPos.getErrorIndex() : maxErrorPos;
2323 // Finally, parse against simple affix to find the match.
2324 // For example, in TestMonster suite,
2325 // if the to-be-parsed text is "-\u00A40,00".
2326 // complexAffixCompare will not find match,
2327 // since there is no ISO code matches "\u00A4",
2328 // and the parse stops at "\u00A4".
2329 // We will just use simple affix comparison (look for exact match)
2332 // TODO: We should parse against simple affix first when
2333 // output currency is not requested. After the complex currency
2334 // parsing implementation was introduced, the default currency
2335 // instance parsing slowed down because of the new code flow.
2336 // I filed #10312 - Yoshito
2337 UBool tmpStatus_2[fgStatusLength];
2338 ParsePosition tmpPos_2(origPos);
2339 DigitList tmpDigitList_2;
2341 // Disable complex currency parsing and try it again.
2342 UBool result = subparse(text,
2343 &fNegativePrefix, &fNegativeSuffix,
2344 &fPositivePrefix, &fPositiveSuffix,
2345 FALSE /* disable complex currency parsing */, UCURR_SYMBOL_NAME,
2346 tmpPos_2, tmpDigitList_2, tmpStatus_2,
2349 if (tmpPos_2.getIndex() > maxPosIndex) {
2350 maxPosIndex = tmpPos_2.getIndex();
2351 for (int32_t i = 0; i < fgStatusLength; ++i) {
2352 status[i] = tmpStatus_2[i];
2354 digits = tmpDigitList_2;
2358 maxErrorPos = (tmpPos_2.getErrorIndex() > maxErrorPos) ?
2359 tmpPos_2.getErrorIndex() : maxErrorPos;
2363 //parsePosition.setIndex(origPos);
2364 parsePosition.setErrorIndex(maxErrorPos);
2366 parsePosition.setIndex(maxPosIndex);
2367 parsePosition.setErrorIndex(-1);
2374 * Parse the given text into a number. The text is parsed beginning at
2375 * parsePosition, until an unparseable character is seen.
2376 * @param text the string to parse.
2377 * @param negPrefix negative prefix.
2378 * @param negSuffix negative suffix.
2379 * @param posPrefix positive prefix.
2380 * @param posSuffix positive suffix.
2381 * @param complexCurrencyParsing whether it is complex currency parsing or not.
2382 * @param type the currency type to parse against, LONG_NAME only or not.
2383 * @param parsePosition The position at which to being parsing. Upon
2384 * return, the first unparsed character.
2385 * @param digits the DigitList to set to the parsed value.
2386 * @param status output param containing boolean status flags indicating
2387 * whether the value was infinite and whether it was positive.
2388 * @param currency return value for parsed currency, for generic
2389 * currency parsing mode, or NULL for normal parsing. In generic
2390 * currency parsing mode, any currency is parsed, not just the
2391 * currency that this formatter is set to.
2393 UBool DecimalFormat::subparse(const UnicodeString& text,
2394 const UnicodeString* negPrefix,
2395 const UnicodeString* negSuffix,
2396 const UnicodeString* posPrefix,
2397 const UnicodeString* posSuffix,
2398 UBool complexCurrencyParsing,
2400 ParsePosition& parsePosition,
2401 DigitList& digits, UBool* status,
2402 UChar* currency) const
2404 // The parsing process builds up the number as char string, in the neutral format that
2405 // will be acceptable to the decNumber library, then at the end passes that string
2406 // off for conversion to a decNumber.
2407 UErrorCode err = U_ZERO_ERROR;
2408 CharString parsedNum;
2411 int32_t position = parsePosition.getIndex();
2412 int32_t oldStart = position;
2413 int32_t textLength = text.length(); // One less pointer to follow
2414 UBool strictParse = !isLenient();
2415 UChar32 zero = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
2416 const UnicodeString *groupingString = &getConstSymbol(fCurrencySignCount == fgCurrencySignCountZero ?
2417 DecimalFormatSymbols::kGroupingSeparatorSymbol : DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol);
2418 UChar32 groupingChar = groupingString->char32At(0);
2419 int32_t groupingStringLength = groupingString->length();
2420 int32_t groupingCharLength = U16_LENGTH(groupingChar);
2421 UBool groupingUsed = isGroupingUsed();
2424 UnicodeString s(dbgbuf,0,300);;
2425 s.append((UnicodeString)"PARSE \"").append(text.tempSubString(position)).append((UnicodeString)"\" " );
2426 #define DBGAPPD(x) if(x) { s.append(UnicodeString(#x "=")); if(x->isEmpty()) { s.append(UnicodeString("<empty>")); } else { s.append(*x); } s.append(UnicodeString(" ")); } else { s.append(UnicodeString(#x "=NULL ")); }
2432 printf("currencyParsing=%d, fFormatWidth=%d, isParseIntegerOnly=%c text.length=%d negPrefLen=%d\n", currencyParsing, fFormatWidth, (isParseIntegerOnly())?'Y':'N', text.length(), negPrefix!=NULL?negPrefix->length():-1);
2435 UBool fastParseOk = false; /* TRUE iff fast parse is OK */
2436 // UBool fastParseHadDecimal = FALSE; /* true if fast parse saw a decimal point. */
2437 const DecimalFormatInternal &data = internalData(fReserved);
2438 if((data.fFastParseStatus==kFastpathYES) &&
2439 fCurrencySignCount == fgCurrencySignCountZero &&
2440 // (negPrefix!=NULL&&negPrefix->isEmpty()) ||
2443 (posPrefix==NULL||posPrefix->isEmpty()) &&
2444 (posSuffix==NULL||posSuffix->isEmpty()) &&
2445 // (negPrefix==NULL||negPrefix->isEmpty()) &&
2446 // (negSuffix==NULL||(negSuffix->isEmpty()) ) &&
2447 TRUE) { // optimized path
2449 int l=text.length();
2451 UChar32 ch = text.char32At(j);
2452 const UnicodeString *decimalString = &getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
2453 UChar32 decimalChar = 0;
2454 UBool intOnly = FALSE;
2455 UChar32 lookForGroup = (groupingUsed&&intOnly&&strictParse)?groupingChar:0;
2457 int32_t decimalCount = decimalString->countChar32(0,3);
2458 if(isParseIntegerOnly()) {
2459 decimalChar = 0; // not allowed
2460 intOnly = TRUE; // Don't look for decimals.
2461 } else if(decimalCount==1) {
2462 decimalChar = decimalString->char32At(0); // Look for this decimal
2463 } else if(decimalCount==0) {
2464 decimalChar=0; // NO decimal set
2466 j=l+1;//Set counter to end of line, so that we break. Unknown decimal situation.
2470 printf("Preparing to do fastpath parse: decimalChar=U+%04X, groupingChar=U+%04X, first ch=U+%04X intOnly=%c strictParse=%c\n",
2471 decimalChar, groupingChar, ch,
2473 (strictParse)?'y':'n');
2475 if(ch==0x002D) { // '-'
2476 j=l+1;//=break - negative number.
2479 parsedNum.append('-',err);
2481 if(j<l) ch = text.char32At(j);
2484 parsedNum.append('+',err);
2487 int32_t digit = ch - zero;
2488 if(digit >=0 && digit <= 9) {
2489 parsedNum.append((char)(digit + '0'), err);
2490 if((digitCount>0) || digit!=0 || j==(l-1)) {
2493 } else if(ch == 0) { // break out
2496 } else if(ch == decimalChar) {
2497 parsedNum.append((char)('.'), err);
2498 decimalChar=0; // no more decimals.
2499 // fastParseHadDecimal=TRUE;
2500 } else if(ch == lookForGroup) {
2501 // ignore grouping char. No decimals, so it has to be an ignorable grouping sep
2502 } else if(intOnly && (lookForGroup!=0) && !u_isdigit(ch)) {
2503 // parsing integer only and can fall through
2505 digitCount=-1; // fail - fall through to slow parse
2509 ch = text.char32At(j); // for next
2512 ((j==l)||intOnly) // end OR only parsing integer
2513 && (digitCount>0)) { // and have at least one digit
2515 printf("PP -> %d, good = [%s] digitcount=%d, fGroupingSize=%d fGroupingSize2=%d!\n", j, parsedNum.data(), digitCount, fGroupingSize, fGroupingSize2);
2517 fastParseOk=true; // Fast parse OK!
2521 /* for testing, try it the slow way. also */
2525 parsePosition.setIndex(position=j);
2526 status[fgStatusInfinite]=false;
2529 // was not OK. reset, retry
2531 printf("Fall through: j=%d, l=%d, digitCount=%d\n", j, l, digitCount);
2537 printf("Could not fastpath parse. ");
2538 printf("fFormatWidth=%d ", fFormatWidth);
2539 printf("text.length()=%d ", text.length());
2540 printf("posPrefix=%p posSuffix=%p ", posPrefix, posSuffix);
2547 #if UCONFIG_HAVE_PARSEALLINPUT
2548 && fParseAllInput!=UNUM_YES
2552 // Match padding before prefix
2553 if (fFormatWidth > 0 && fPadPosition == kPadBeforePrefix) {
2554 position = skipPadding(text, position);
2557 // Match positive and negative prefixes; prefer longest match.
2558 int32_t posMatch = compareAffix(text, position, FALSE, TRUE, posPrefix, complexCurrencyParsing, type, currency);
2559 int32_t negMatch = compareAffix(text, position, TRUE, TRUE, negPrefix, complexCurrencyParsing, type, currency);
2560 if (posMatch >= 0 && negMatch >= 0) {
2561 if (posMatch > negMatch) {
2563 } else if (negMatch > posMatch) {
2567 if (posMatch >= 0) {
2568 position += posMatch;
2569 parsedNum.append('+', err);
2570 } else if (negMatch >= 0) {
2571 position += negMatch;
2572 parsedNum.append('-', err);
2573 } else if (strictParse){
2574 parsePosition.setErrorIndex(position);
2577 // Temporary set positive. This might be changed after checking suffix
2578 parsedNum.append('+', err);
2581 // Match padding before prefix
2582 if (fFormatWidth > 0 && fPadPosition == kPadAfterPrefix) {
2583 position = skipPadding(text, position);
2586 if (! strictParse) {
2587 position = skipUWhiteSpace(text, position);
2590 // process digits or Inf, find decimal position
2591 const UnicodeString *inf = &getConstSymbol(DecimalFormatSymbols::kInfinitySymbol);
2592 int32_t infLen = (text.compare(position, inf->length(), *inf)
2593 ? 0 : inf->length());
2594 position += infLen; // infLen is non-zero when it does equal to infinity
2595 status[fgStatusInfinite] = infLen != 0;
2598 parsedNum.append("Infinity", err);
2600 // We now have a string of digits, possibly with grouping symbols,
2601 // and decimal points. We want to process these into a DigitList.
2602 // We don't want to put a bunch of leading zeros into the DigitList
2603 // though, so we keep track of the location of the decimal point,
2604 // put only significant digits into the DigitList, and adjust the
2605 // exponent as needed.
2608 UBool strictFail = FALSE; // did we exit with a strict parse failure?
2609 int32_t lastGroup = -1; // where did we last see a grouping separator?
2610 int32_t digitStart = position;
2611 int32_t gs2 = fGroupingSize2 == 0 ? fGroupingSize : fGroupingSize2;
2613 const UnicodeString *decimalString;
2614 if (fCurrencySignCount != fgCurrencySignCountZero) {
2615 decimalString = &getConstSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol);
2617 decimalString = &getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
2619 UChar32 decimalChar = decimalString->char32At(0);
2620 int32_t decimalStringLength = decimalString->length();
2621 int32_t decimalCharLength = U16_LENGTH(decimalChar);
2623 UBool sawDecimal = FALSE;
2624 UChar32 sawDecimalChar = 0xFFFF;
2625 UBool sawGrouping = FALSE;
2626 UChar32 sawGroupingChar = 0xFFFF;
2627 UBool sawDigit = FALSE;
2628 int32_t backup = -1;
2631 // equivalent grouping and decimal support
2632 const UnicodeSet *decimalSet = NULL;
2633 const UnicodeSet *groupingSet = NULL;
2635 if (decimalCharLength == decimalStringLength) {
2636 decimalSet = DecimalFormatStaticSets::getSimilarDecimals(decimalChar, strictParse);
2639 if (groupingCharLength == groupingStringLength) {
2641 groupingSet = fStaticSets->fStrictDefaultGroupingSeparators;
2643 groupingSet = fStaticSets->fDefaultGroupingSeparators;
2647 // We need to test groupingChar and decimalChar separately from groupingSet and decimalSet, if the sets are even initialized.
2648 // If sawDecimal is TRUE, only consider sawDecimalChar and NOT decimalSet
2649 // If a character matches decimalSet, don't consider it to be a member of the groupingSet.
2651 // We have to track digitCount ourselves, because digits.fCount will
2652 // pin when the maximum allowable digits is reached.
2653 int32_t digitCount = 0;
2654 int32_t integerDigitCount = 0;
2656 for (; position < textLength; )
2658 UChar32 ch = text.char32At(position);
2660 /* We recognize all digit ranges, not only the Latin digit range
2661 * '0'..'9'. We do so by using the Character.digit() method,
2662 * which converts a valid Unicode digit to the range 0..9.
2664 * The character 'ch' may be a digit. If so, place its value
2665 * from 0 to 9 in 'digit'. First try using the locale digit,
2666 * which may or MAY NOT be a standard Unicode digit range. If
2667 * this fails, try using the standard Unicode digit ranges by
2668 * calling Character.digit(). If this also fails, digit will
2669 * have a value outside the range 0..9.
2672 if (digit < 0 || digit > 9)
2674 digit = u_charDigitValue(ch);
2677 // As a last resort, look through the localized digits if the zero digit
2678 // is not a "standard" Unicode digit.
2679 if ( (digit < 0 || digit > 9) && u_charDigitValue(zero) != 0) {
2681 if ( getConstSymbol((DecimalFormatSymbols::ENumberFormatSymbol)(DecimalFormatSymbols::kZeroDigitSymbol)).char32At(0) == ch ) {
2684 for (digit = 1 ; digit < 10 ; digit++ ) {
2685 if ( getConstSymbol((DecimalFormatSymbols::ENumberFormatSymbol)(DecimalFormatSymbols::kOneDigitSymbol+digit-1)).char32At(0) == ch ) {
2691 if (digit >= 0 && digit <= 9)
2693 if (strictParse && backup != -1) {
2694 // comma followed by digit, so group before comma is a
2695 // secondary group. If there was a group separator
2696 // before that, the group must == the secondary group
2697 // length, else it can be <= the the secondary group
2699 if ((lastGroup != -1 && backup - lastGroup - 1 != gs2) ||
2700 (lastGroup == -1 && position - digitStart - 1 > gs2)) {
2708 // Cancel out backup setting (see grouping handler below)
2712 // Note: this will append leading zeros
2713 parsedNum.append((char)(digit + '0'), err);
2715 // count any digit that's not a leading zero
2716 if (digit > 0 || digitCount > 0 || sawDecimal) {
2719 // count any integer digit that's not a leading zero
2721 integerDigitCount += 1;
2725 position += U16_LENGTH(ch);
2727 else if (groupingStringLength > 0 &&
2728 matchGrouping(groupingChar, sawGrouping, sawGroupingChar, groupingSet,
2729 decimalChar, decimalSet,
2730 ch) && groupingUsed)
2737 if ((!sawDigit || backup != -1)) {
2738 // leading group, or two group separators in a row
2744 // Ignore grouping characters, if we are using them, but require
2745 // that they be followed by a digit. Otherwise we backup and
2748 position += groupingStringLength;
2750 // Once we see a grouping character, we only accept that grouping character from then on.
2753 else if (matchDecimal(decimalChar,sawDecimal,sawDecimalChar, decimalSet, ch))
2757 (lastGroup != -1 && position - lastGroup != fGroupingSize + 1)) {
2763 // If we're only parsing integers, or if we ALREADY saw the
2764 // decimal, then don't parse this one.
2765 if (isParseIntegerOnly() || sawDecimal) {
2769 parsedNum.append('.', err);
2770 position += decimalStringLength;
2772 // Once we see a decimal character, we only accept that decimal character from then on.
2774 // decimalSet is considered to consist of (ch,ch)
2778 if(!fBoolFlags.contains(UNUM_PARSE_NO_EXPONENT) || // don't parse if this is set unless..
2779 isScientificNotation()) { // .. it's an exponent format - ignore setting and parse anyways
2780 const UnicodeString *tmp;
2781 tmp = &getConstSymbol(DecimalFormatSymbols::kExponentialSymbol);
2783 if (!text.caseCompare(position, tmp->length(), *tmp, U_FOLD_CASE_DEFAULT)) // error code is set below if !sawDigit
2785 // Parse sign, if present
2786 int32_t pos = position + tmp->length();
2787 char exponentSign = '+';
2789 if (pos < textLength)
2791 tmp = &getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
2792 if (!text.compare(pos, tmp->length(), *tmp))
2794 pos += tmp->length();
2797 tmp = &getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
2798 if (!text.compare(pos, tmp->length(), *tmp))
2801 pos += tmp->length();
2806 UBool sawExponentDigit = FALSE;
2807 while (pos < textLength) {
2808 ch = text[(int32_t)pos];
2811 if (digit < 0 || digit > 9) {
2812 digit = u_charDigitValue(ch);
2814 if (0 <= digit && digit <= 9) {
2815 if (!sawExponentDigit) {
2816 parsedNum.append('E', err);
2817 parsedNum.append(exponentSign, err);
2818 sawExponentDigit = TRUE;
2821 parsedNum.append((char)(digit + '0'), err);
2827 if (sawExponentDigit) {
2828 position = pos; // Advance past the exponent
2831 break; // Whether we fail or succeed, we exit this loop
2835 } else { // not parsing exponent
2846 if (strictParse && !sawDecimal) {
2847 if (lastGroup != -1 && position - lastGroup != fGroupingSize + 1) {
2853 // only set with strictParse and a grouping separator error
2855 parsePosition.setIndex(oldStart);
2856 parsePosition.setErrorIndex(position);
2857 debug("strictFail!");
2861 // If there was no decimal point we have an integer
2863 // If none of the text string was recognized. For example, parse
2864 // "x" with pattern "#0.00" (return index and error index both 0)
2865 // parse "$" with pattern "$#0.00". (return index 0 and error index
2867 if (!sawDigit && digitCount == 0) {
2869 debug("none of text rec");
2870 printf("position=%d\n",position);
2872 parsePosition.setIndex(oldStart);
2873 parsePosition.setErrorIndex(oldStart);
2878 // Match padding before suffix
2879 if (fFormatWidth > 0 && fPadPosition == kPadBeforeSuffix) {
2880 position = skipPadding(text, position);
2883 int32_t posSuffixMatch = -1, negSuffixMatch = -1;
2885 // Match positive and negative suffixes; prefer longest match.
2886 if (posMatch >= 0 || (!strictParse && negMatch < 0)) {
2887 posSuffixMatch = compareAffix(text, position, FALSE, FALSE, posSuffix, complexCurrencyParsing, type, currency);
2889 if (negMatch >= 0) {
2890 negSuffixMatch = compareAffix(text, position, TRUE, FALSE, negSuffix, complexCurrencyParsing, type, currency);
2892 if (posSuffixMatch >= 0 && negSuffixMatch >= 0) {
2893 if (posSuffixMatch > negSuffixMatch) {
2894 negSuffixMatch = -1;
2895 } else if (negSuffixMatch > posSuffixMatch) {
2896 posSuffixMatch = -1;
2900 // Fail if neither or both
2901 if (strictParse && ((posSuffixMatch >= 0) == (negSuffixMatch >= 0))) {
2902 parsePosition.setErrorIndex(position);
2903 debug("neither or both");
2907 position += (posSuffixMatch >= 0 ? posSuffixMatch : (negSuffixMatch >= 0 ? negSuffixMatch : 0));
2909 // Match padding before suffix
2910 if (fFormatWidth > 0 && fPadPosition == kPadAfterSuffix) {
2911 position = skipPadding(text, position);
2914 parsePosition.setIndex(position);
2916 parsedNum.data()[0] = (posSuffixMatch >= 0 || (!strictParse && negMatch < 0 && negSuffixMatch < 0)) ? '+' : '-';
2918 printf("PP -> %d, SLOW = [%s]! pp=%d, os=%d, err=%s\n", position, parsedNum.data(), parsePosition.getIndex(),oldStart,u_errorName(err));
2920 } /* end SLOW parse */
2921 if(parsePosition.getIndex() == oldStart)
2924 printf(" PP didnt move, err\n");
2926 parsePosition.setErrorIndex(position);
2929 #if UCONFIG_HAVE_PARSEALLINPUT
2930 else if (fParseAllInput==UNUM_YES&&parsePosition.getIndex()!=textLength)
2933 printf(" PP didnt consume all (UNUM_YES), err\n");
2935 parsePosition.setErrorIndex(position);
2939 // uint32_t bits = (fastParseOk?kFastpathOk:0) |
2940 // (fastParseHadDecimal?0:kNoDecimal);
2941 //printf("FPOK=%d, FPHD=%d, bits=%08X\n", fastParseOk, fastParseHadDecimal, bits);
2942 digits.set(parsedNum.toStringPiece(),
2947 if (U_FAILURE(err)) {
2949 printf(" err setting %s\n", u_errorName(err));
2951 parsePosition.setErrorIndex(position);
2958 * Starting at position, advance past a run of pad characters, if any.
2959 * Return the index of the first character after position that is not a pad
2960 * character. Result is >= position.
2962 int32_t DecimalFormat::skipPadding(const UnicodeString& text, int32_t position) const {
2963 int32_t padLen = U16_LENGTH(fPad);
2964 while (position < text.length() &&
2965 text.char32At(position) == fPad) {
2972 * Return the length matched by the given affix, or -1 if none.
2973 * Runs of white space in the affix, match runs of white space in
2974 * the input. Pattern white space and input white space are
2975 * determined differently; see code.
2976 * @param text input text
2977 * @param pos offset into input at which to begin matching
2980 * @param affixPat affix pattern used for currency affix comparison.
2981 * @param complexCurrencyParsing whether it is currency parsing or not
2982 * @param type the currency type to parse against, LONG_NAME only or not.
2983 * @param currency return value for parsed currency, for generic
2984 * currency parsing mode, or null for normal parsing. In generic
2985 * currency parsing mode, any currency is parsed, not just the
2986 * currency that this formatter is set to.
2987 * @return length of input that matches, or -1 if match failure
2989 int32_t DecimalFormat::compareAffix(const UnicodeString& text,
2993 const UnicodeString* affixPat,
2994 UBool complexCurrencyParsing,
2996 UChar* currency) const
2998 const UnicodeString *patternToCompare;
2999 if (fCurrencyChoice != NULL || currency != NULL ||
3000 (fCurrencySignCount != fgCurrencySignCountZero && complexCurrencyParsing)) {
3002 if (affixPat != NULL) {
3003 return compareComplexAffix(*affixPat, text, pos, type, currency);
3009 patternToCompare = &fNegativePrefix;
3012 patternToCompare = &fNegativeSuffix;
3017 patternToCompare = &fPositivePrefix;
3020 patternToCompare = &fPositiveSuffix;
3023 return compareSimpleAffix(*patternToCompare, text, pos, isLenient());
3026 UBool DecimalFormat::equalWithSignCompatibility(UChar32 lhs, UChar32 rhs) const {
3030 U_ASSERT(fStaticSets != NULL); // should already be loaded
3031 const UnicodeSet *minusSigns = fStaticSets->fMinusSigns;
3032 const UnicodeSet *plusSigns = fStaticSets->fPlusSigns;
3033 return (minusSigns->contains(lhs) && minusSigns->contains(rhs)) ||
3034 (plusSigns->contains(lhs) && plusSigns->contains(rhs));
3037 // check for LRM 0x200E, RLM 0x200F, ALM 0x061C
3038 #define IS_BIDI_MARK(c) (c==0x200E || c==0x200F || c==0x061C)
3040 #define TRIM_BUFLEN 32
3041 UnicodeString& DecimalFormat::trimMarksFromAffix(const UnicodeString& affix, UnicodeString& trimmedAffix) {
3042 UChar trimBuf[TRIM_BUFLEN];
3043 int32_t affixLen = affix.length();
3044 int32_t affixPos, trimLen = 0;
3046 for (affixPos = 0; affixPos < affixLen; affixPos++) {
3047 UChar c = affix.charAt(affixPos);
3048 if (!IS_BIDI_MARK(c)) {
3049 if (trimLen < TRIM_BUFLEN) {
3050 trimBuf[trimLen++] = c;
3057 return (trimLen > 0)? trimmedAffix.setTo(trimBuf, trimLen): trimmedAffix.setTo(affix);
3061 * Return the length matched by the given affix, or -1 if none.
3062 * Runs of white space in the affix, match runs of white space in
3063 * the input. Pattern white space and input white space are
3064 * determined differently; see code.
3065 * @param affix pattern string, taken as a literal
3066 * @param input input text
3067 * @param pos offset into input at which to begin matching
3068 * @return length of input that matches, or -1 if match failure
3070 int32_t DecimalFormat::compareSimpleAffix(const UnicodeString& affix,
3071 const UnicodeString& input,
3073 UBool lenient) const {
3074 int32_t start = pos;
3075 UnicodeString trimmedAffix;
3076 // For more efficiency we should keep lazily-created trimmed affixes around in
3077 // instance variables instead of trimming each time they are used (the next step)
3078 trimMarksFromAffix(affix, trimmedAffix);
3079 UChar32 affixChar = trimmedAffix.char32At(0);
3080 int32_t affixLength = trimmedAffix.length();
3081 int32_t inputLength = input.length();
3082 int32_t affixCharLength = U16_LENGTH(affixChar);
3083 UnicodeSet *affixSet;
3084 UErrorCode status = U_ZERO_ERROR;
3086 U_ASSERT(fStaticSets != NULL); // should already be loaded
3088 if (U_FAILURE(status)) {
3092 affixSet = fStaticSets->fStrictDashEquivalents;
3094 // If the trimmedAffix is exactly one character long and that character
3095 // is in the dash set and the very next input character is also
3096 // in the dash set, return a match.
3097 if (affixCharLength == affixLength && affixSet->contains(affixChar)) {
3098 UChar32 ic = input.char32At(pos);
3099 if (affixSet->contains(ic)) {
3100 pos += U16_LENGTH(ic);
3101 pos = skipBidiMarks(input, pos); // skip any trailing bidi marks
3106 for (int32_t i = 0; i < affixLength; ) {
3107 UChar32 c = trimmedAffix.char32At(i);
3108 int32_t len = U16_LENGTH(c);
3109 if (PatternProps::isWhiteSpace(c)) {
3110 // We may have a pattern like: \u200F \u0020
3111 // and input text like: \u200F \u0020
3112 // Note that U+200F and U+0020 are Pattern_White_Space but only
3113 // U+0020 is UWhiteSpace. So we have to first do a direct
3114 // match of the run of Pattern_White_Space in the pattern,
3115 // then match any extra characters.
3116 UBool literalMatch = FALSE;
3117 while (pos < inputLength) {
3118 UChar32 ic = input.char32At(pos);
3120 literalMatch = TRUE;
3123 if (i == affixLength) {
3126 c = trimmedAffix.char32At(i);
3127 len = U16_LENGTH(c);
3128 if (!PatternProps::isWhiteSpace(c)) {
3131 } else if (IS_BIDI_MARK(ic)) {
3132 pos ++; // just skip over this input text
3138 // Advance over run in pattern
3139 i = skipPatternWhiteSpace(trimmedAffix, i);
3141 // Advance over run in input text
3142 // Must see at least one white space char in input,
3143 // unless we've already matched some characters literally.
3145 pos = skipUWhiteSpace(input, pos);
3146 if (pos == s && !literalMatch) {
3150 // If we skip UWhiteSpace in the input text, we need to skip it in the pattern.
3151 // Otherwise, the previous lines may have skipped over text (such as U+00A0) that
3152 // is also in the trimmedAffix.
3153 i = skipUWhiteSpace(trimmedAffix, i);
3155 UBool match = FALSE;
3156 while (pos < inputLength) {
3157 UChar32 ic = input.char32At(pos);
3158 if (!match && ic == c) {
3162 } else if (IS_BIDI_MARK(ic)) {
3163 pos++; // just skip over this input text
3174 UBool match = FALSE;
3176 affixSet = fStaticSets->fDashEquivalents;
3178 if (affixCharLength == affixLength && affixSet->contains(affixChar)) {
3179 pos = skipUWhiteSpaceAndMarks(input, pos);
3180 UChar32 ic = input.char32At(pos);
3182 if (affixSet->contains(ic)) {
3183 pos += U16_LENGTH(ic);
3184 pos = skipBidiMarks(input, pos);
3189 for (int32_t i = 0; i < affixLength; )
3191 //i = skipRuleWhiteSpace(trimmedAffix, i);
3192 i = skipUWhiteSpace(trimmedAffix, i);
3193 pos = skipUWhiteSpaceAndMarks(input, pos);
3195 if (i >= affixLength || pos >= inputLength) {
3199 UChar32 c = trimmedAffix.char32At(i);
3200 UChar32 ic = input.char32At(pos);
3202 if (!equalWithSignCompatibility(ic, c)) {
3208 pos += U16_LENGTH(ic);
3209 pos = skipBidiMarks(input, pos);
3212 if (affixLength > 0 && ! match) {
3220 * Skip over a run of zero or more Pattern_White_Space characters at
3223 int32_t DecimalFormat::skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) {
3224 const UChar* s = text.getBuffer();
3225 return (int32_t)(PatternProps::skipWhiteSpace(s + pos, text.length() - pos) - s);
3229 * Skip over a run of zero or more isUWhiteSpace() characters at pos
3232 int32_t DecimalFormat::skipUWhiteSpace(const UnicodeString& text, int32_t pos) {
3233 while (pos < text.length()) {
3234 UChar32 c = text.char32At(pos);
3235 if (!u_isUWhiteSpace(c)) {
3238 pos += U16_LENGTH(c);
3244 * Skip over a run of zero or more isUWhiteSpace() characters or bidi marks at pos
3247 int32_t DecimalFormat::skipUWhiteSpaceAndMarks(const UnicodeString& text, int32_t pos) {
3248 while (pos < text.length()) {
3249 UChar32 c = text.char32At(pos);
3250 if (!u_isUWhiteSpace(c) && !IS_BIDI_MARK(c)) { // u_isUWhiteSpace doesn't include LRM,RLM,ALM
3253 pos += U16_LENGTH(c);
3259 * Skip over a run of zero or more bidi marks at pos in text.
3261 int32_t DecimalFormat::skipBidiMarks(const UnicodeString& text, int32_t pos) {
3262 while (pos < text.length()) {
3263 UChar c = text.charAt(pos);
3264 if (!IS_BIDI_MARK(c)) {
3273 * Return the length matched by the given affix, or -1 if none.
3274 * @param affixPat pattern string
3275 * @param input input text
3276 * @param pos offset into input at which to begin matching
3277 * @param type the currency type to parse against, LONG_NAME only or not.
3278 * @param currency return value for parsed currency, for generic
3279 * currency parsing mode, or null for normal parsing. In generic
3280 * currency parsing mode, any currency is parsed, not just the
3281 * currency that this formatter is set to.
3282 * @return length of input that matches, or -1 if match failure
3284 int32_t DecimalFormat::compareComplexAffix(const UnicodeString& affixPat,
3285 const UnicodeString& text,
3288 UChar* currency) const
3290 int32_t start = pos;
3291 U_ASSERT(currency != NULL ||
3292 (fCurrencyChoice != NULL && *getCurrency() != 0) ||
3293 fCurrencySignCount != fgCurrencySignCountZero);
3296 i<affixPat.length() && pos >= 0; ) {
3297 UChar32 c = affixPat.char32At(i);
3301 U_ASSERT(i <= affixPat.length());
3302 c = affixPat.char32At(i);
3305 const UnicodeString* affix = NULL;
3308 case kCurrencySign: {
3309 // since the currency names in choice format is saved
3310 // the same way as other currency names,
3311 // do not need to do currency choice parsing here.
3312 // the general currency parsing parse against all names,
3313 // including names in choice format.
3314 UBool intl = i<affixPat.length() &&
3315 affixPat.char32At(i) == kCurrencySign;
3319 UBool plural = i<affixPat.length() &&
3320 affixPat.char32At(i) == kCurrencySign;
3325 // Parse generic currency -- anything for which we
3326 // have a display name, or any 3-letter ISO code.
3327 // Try to parse display name for our locale; first
3328 // determine our locale.
3329 const char* loc = fCurrencyPluralInfo->getLocale().getName();
3330 ParsePosition ppos(pos);
3332 UErrorCode ec = U_ZERO_ERROR;
3333 // Delegate parse of display name => ISO code to Currency
3334 uprv_parseCurrency(loc, text, ppos, type, curr, ec);
3336 // If parse succeeds, populate currency[0]
3337 if (U_SUCCESS(ec) && ppos.getIndex() != pos) {
3339 u_strcpy(currency, curr);
3341 // The formatter is currency-style but the client has not requested
3342 // the value of the parsed currency. In this case, if that value does
3343 // not match the formatter's current value, then the parse fails.
3344 UChar effectiveCurr[4];
3345 getEffectiveCurrency(effectiveCurr, ec);
3346 if ( U_FAILURE(ec) || u_strncmp(curr,effectiveCurr,4) != 0 ) {
3351 pos = ppos.getIndex();
3352 } else if (!isLenient()){
3357 case kPatternPercent:
3358 affix = &getConstSymbol(DecimalFormatSymbols::kPercentSymbol);
3360 case kPatternPerMill:
3361 affix = &getConstSymbol(DecimalFormatSymbols::kPerMillSymbol);
3364 affix = &getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
3367 affix = &getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
3370 // fall through to affix!=0 test, which will fail
3374 if (affix != NULL) {
3375 pos = match(text, pos, *affix);
3380 pos = match(text, pos, c);
3381 if (PatternProps::isWhiteSpace(c)) {
3382 i = skipPatternWhiteSpace(affixPat, i);
3389 * Match a single character at text[pos] and return the index of the
3390 * next character upon success. Return -1 on failure. If
3391 * ch is a Pattern_White_Space then match a run of white space in text.
3393 int32_t DecimalFormat::match(const UnicodeString& text, int32_t pos, UChar32 ch) {
3394 if (PatternProps::isWhiteSpace(ch)) {
3395 // Advance over run of white space in input text
3396 // Must see at least one white space char in input
3398 pos = skipPatternWhiteSpace(text, pos);
3404 return (pos >= 0 && text.char32At(pos) == ch) ?
3405 (pos + U16_LENGTH(ch)) : -1;
3409 * Match a string at text[pos] and return the index of the next
3410 * character upon success. Return -1 on failure. Match a run of
3411 * white space in str with a run of white space in text.
3413 int32_t DecimalFormat::match(const UnicodeString& text, int32_t pos, const UnicodeString& str) {
3414 for (int32_t i=0; i<str.length() && pos >= 0; ) {
3415 UChar32 ch = str.char32At(i);
3416 i += U16_LENGTH(ch);
3417 if (PatternProps::isWhiteSpace(ch)) {
3418 i = skipPatternWhiteSpace(str, i);
3420 pos = match(text, pos, ch);
3425 UBool DecimalFormat::matchSymbol(const UnicodeString &text, int32_t position, int32_t length, const UnicodeString &symbol,
3426 UnicodeSet *sset, UChar32 schar)
3429 return sset->contains(schar);
3432 return text.compare(position, length, symbol) == 0;
3435 UBool DecimalFormat::matchDecimal(UChar32 symbolChar,
3436 UBool sawDecimal, UChar32 sawDecimalChar,
3437 const UnicodeSet *sset, UChar32 schar) {
3439 return schar==sawDecimalChar;
3440 } else if(schar==symbolChar) {
3442 } else if(sset!=NULL) {
3443 return sset->contains(schar);
3449 UBool DecimalFormat::matchGrouping(UChar32 groupingChar,
3450 UBool sawGrouping, UChar32 sawGroupingChar,
3451 const UnicodeSet *sset,
3452 UChar32 /*decimalChar*/, const UnicodeSet *decimalSet,
3455 return schar==sawGroupingChar; // previously found
3456 } else if(schar==groupingChar) {
3457 return TRUE; // char from symbols
3458 } else if(sset!=NULL) {
3459 return sset->contains(schar) && // in groupingSet but...
3460 ((decimalSet==NULL) || !decimalSet->contains(schar)); // Exclude decimalSet from groupingSet
3468 //------------------------------------------------------------------------------
3469 // Gets the pointer to the localized decimal format symbols
3471 const DecimalFormatSymbols*
3472 DecimalFormat::getDecimalFormatSymbols() const
3477 //------------------------------------------------------------------------------
3478 // De-owning the current localized symbols and adopt the new symbols.
3481 DecimalFormat::adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt)
3483 if (symbolsToAdopt == NULL) {
3484 return; // do not allow caller to set fSymbols to NULL
3487 UBool sameSymbols = FALSE;
3488 if (fSymbols != NULL) {
3489 sameSymbols = (UBool)(getConstSymbol(DecimalFormatSymbols::kCurrencySymbol) ==
3490 symbolsToAdopt->getConstSymbol(DecimalFormatSymbols::kCurrencySymbol) &&
3491 getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol) ==
3492 symbolsToAdopt->getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol));
3496 fSymbols = symbolsToAdopt;
3498 // If the currency symbols are the same, there is no need to recalculate.
3499 setCurrencyForSymbols();
3501 expandAffixes(NULL);
3502 #if UCONFIG_FORMAT_FASTPATHS_49
3506 //------------------------------------------------------------------------------
3507 // Setting the symbols is equlivalent to adopting a newly created localized
3511 DecimalFormat::setDecimalFormatSymbols(const DecimalFormatSymbols& symbols)
3513 adoptDecimalFormatSymbols(new DecimalFormatSymbols(symbols));
3514 #if UCONFIG_FORMAT_FASTPATHS_49
3520 const CurrencyPluralInfo*
3521 DecimalFormat::getCurrencyPluralInfo(void) const
3523 return fCurrencyPluralInfo;
3528 DecimalFormat::adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt)
3530 if (toAdopt != NULL) {
3531 delete fCurrencyPluralInfo;
3532 fCurrencyPluralInfo = toAdopt;
3533 // re-set currency affix patterns and currency affixes.
3534 if (fCurrencySignCount != fgCurrencySignCountZero) {
3535 UErrorCode status = U_ZERO_ERROR;
3536 if (fAffixPatternsForCurrency) {
3537 deleteHashForAffixPattern();
3539 setupCurrencyAffixPatterns(status);
3540 if (fCurrencySignCount == fgCurrencySignCountInPluralFormat) {
3541 // only setup the affixes of the plural pattern.
3542 setupCurrencyAffixes(fFormatPattern, FALSE, TRUE, status);
3546 #if UCONFIG_FORMAT_FASTPATHS_49
3552 DecimalFormat::setCurrencyPluralInfo(const CurrencyPluralInfo& info)
3554 adoptCurrencyPluralInfo(info.clone());
3555 #if UCONFIG_FORMAT_FASTPATHS_49
3562 * Update the currency object to match the symbols. This method
3563 * is used only when the caller has passed in a symbols object
3564 * that may not be the default object for its locale.
3567 DecimalFormat::setCurrencyForSymbols() {
3569 Update the affix strings accroding to symbols in order to keep
3570 the affix strings up to date.
3574 // With the introduction of the Currency object, the currency
3575 // symbols in the DFS object are ignored. For backward
3576 // compatibility, we check any explicitly set DFS object. If it
3577 // is a default symbols object for its locale, we change the
3578 // currency object to one for that locale. If it is custom,
3579 // we set the currency to null.
3580 UErrorCode ec = U_ZERO_ERROR;
3581 const UChar* c = NULL;
3582 const char* loc = fSymbols->getLocale().getName();
3583 UChar intlCurrencySymbol[4];
3584 ucurr_forLocale(loc, intlCurrencySymbol, 4, &ec);
3585 UnicodeString currencySymbol;
3587 uprv_getStaticCurrencyName(intlCurrencySymbol, loc, currencySymbol, ec);
3589 && getConstSymbol(DecimalFormatSymbols::kCurrencySymbol) == currencySymbol
3590 && getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol) == UnicodeString(intlCurrencySymbol))
3592 // Trap an error in mapping locale to currency. If we can't
3593 // map, then don't fail and set the currency to "".
3594 c = intlCurrencySymbol;
3596 ec = U_ZERO_ERROR; // reset local error code!
3597 setCurrencyInternally(c, ec);
3598 #if UCONFIG_FORMAT_FASTPATHS_49
3604 //------------------------------------------------------------------------------
3605 // Gets the positive prefix of the number pattern.
3608 DecimalFormat::getPositivePrefix(UnicodeString& result) const
3610 result = fPositivePrefix;
3614 //------------------------------------------------------------------------------
3615 // Sets the positive prefix of the number pattern.
3618 DecimalFormat::setPositivePrefix(const UnicodeString& newValue)
3620 fPositivePrefix = newValue;
3621 delete fPosPrefixPattern;
3622 fPosPrefixPattern = 0;
3623 #if UCONFIG_FORMAT_FASTPATHS_49
3628 //------------------------------------------------------------------------------
3629 // Gets the negative prefix of the number pattern.
3632 DecimalFormat::getNegativePrefix(UnicodeString& result) const
3634 result = fNegativePrefix;
3638 //------------------------------------------------------------------------------
3639 // Gets the negative prefix of the number pattern.
3642 DecimalFormat::setNegativePrefix(const UnicodeString& newValue)
3644 fNegativePrefix = newValue;
3645 delete fNegPrefixPattern;
3646 fNegPrefixPattern = 0;
3647 #if UCONFIG_FORMAT_FASTPATHS_49
3652 //------------------------------------------------------------------------------
3653 // Gets the positive suffix of the number pattern.
3656 DecimalFormat::getPositiveSuffix(UnicodeString& result) const
3658 result = fPositiveSuffix;
3662 //------------------------------------------------------------------------------
3663 // Sets the positive suffix of the number pattern.
3666 DecimalFormat::setPositiveSuffix(const UnicodeString& newValue)
3668 fPositiveSuffix = newValue;
3669 delete fPosSuffixPattern;
3670 fPosSuffixPattern = 0;
3671 #if UCONFIG_FORMAT_FASTPATHS_49
3676 //------------------------------------------------------------------------------
3677 // Gets the negative suffix of the number pattern.
3680 DecimalFormat::getNegativeSuffix(UnicodeString& result) const
3682 result = fNegativeSuffix;
3686 //------------------------------------------------------------------------------
3687 // Sets the negative suffix of the number pattern.
3690 DecimalFormat::setNegativeSuffix(const UnicodeString& newValue)
3692 fNegativeSuffix = newValue;
3693 delete fNegSuffixPattern;
3694 fNegSuffixPattern = 0;
3695 #if UCONFIG_FORMAT_FASTPATHS_49
3700 //------------------------------------------------------------------------------
3701 // Gets the multiplier of the number pattern.
3702 // Multipliers are stored as decimal numbers (DigitLists) because that
3703 // is the most convenient for muliplying or dividing the numbers to be formatted.
3704 // A NULL multiplier implies one, and the scaling operations are skipped.
3707 DecimalFormat::getMultiplier() const
3709 if (fMultiplier == NULL) {
3712 return fMultiplier->getLong();
3716 //------------------------------------------------------------------------------
3717 // Sets the multiplier of the number pattern.
3719 DecimalFormat::setMultiplier(int32_t newValue)
3721 // if (newValue == 0) {
3722 // throw new IllegalArgumentException("Bad multiplier: " + newValue);
3724 if (newValue == 0) {
3725 newValue = 1; // one being the benign default value for a multiplier.
3727 if (newValue == 1) {
3731 if (fMultiplier == NULL) {
3732 fMultiplier = new DigitList;
3734 if (fMultiplier != NULL) {
3735 fMultiplier->set(newValue);
3738 #if UCONFIG_FORMAT_FASTPATHS_49
3744 * Get the rounding increment.
3745 * @return A positive rounding increment, or 0.0 if rounding
3747 * @see #setRoundingIncrement
3748 * @see #getRoundingMode
3749 * @see #setRoundingMode
3751 double DecimalFormat::getRoundingIncrement() const {
3752 if (fRoundingIncrement == NULL) {
3755 return fRoundingIncrement->getDouble();
3760 * Set the rounding increment. This method also controls whether
3761 * rounding is enabled.
3762 * @param newValue A positive rounding increment, or 0.0 to disable rounding.
3763 * Negative increments are equivalent to 0.0.
3764 * @see #getRoundingIncrement
3765 * @see #getRoundingMode
3766 * @see #setRoundingMode
3768 void DecimalFormat::setRoundingIncrement(double newValue) {
3769 if (newValue > 0.0) {
3770 if (fRoundingIncrement == NULL) {
3771 fRoundingIncrement = new DigitList();
3773 if (fRoundingIncrement != NULL) {
3774 fRoundingIncrement->set(newValue);
3778 // These statements are executed if newValue is less than 0.0
3779 // or fRoundingIncrement could not be created.
3780 delete fRoundingIncrement;
3781 fRoundingIncrement = NULL;
3782 #if UCONFIG_FORMAT_FASTPATHS_49
3788 * Get the rounding mode.
3789 * @return A rounding mode
3790 * @see #setRoundingIncrement
3791 * @see #getRoundingIncrement
3792 * @see #setRoundingMode
3794 DecimalFormat::ERoundingMode DecimalFormat::getRoundingMode() const {
3795 return fRoundingMode;
3799 * Set the rounding mode. This has no effect unless the rounding
3800 * increment is greater than zero.
3801 * @param roundingMode A rounding mode
3802 * @see #setRoundingIncrement
3803 * @see #getRoundingIncrement
3804 * @see #getRoundingMode
3806 void DecimalFormat::setRoundingMode(ERoundingMode roundingMode) {
3807 fRoundingMode = roundingMode;
3808 #if UCONFIG_FORMAT_FASTPATHS_49
3814 * Get the width to which the output of <code>format()</code> is padded.
3815 * @return the format width, or zero if no padding is in effect
3816 * @see #setFormatWidth
3817 * @see #getPadCharacter
3818 * @see #setPadCharacter
3819 * @see #getPadPosition
3820 * @see #setPadPosition
3822 int32_t DecimalFormat::getFormatWidth() const {
3823 return fFormatWidth;
3827 * Set the width to which the output of <code>format()</code> is padded.
3828 * This method also controls whether padding is enabled.
3829 * @param width the width to which to pad the result of
3830 * <code>format()</code>, or zero to disable padding. A negative
3831 * width is equivalent to 0.
3832 * @see #getFormatWidth
3833 * @see #getPadCharacter
3834 * @see #setPadCharacter
3835 * @see #getPadPosition
3836 * @see #setPadPosition
3838 void DecimalFormat::setFormatWidth(int32_t width) {
3839 fFormatWidth = (width > 0) ? width : 0;
3840 #if UCONFIG_FORMAT_FASTPATHS_49
3845 UnicodeString DecimalFormat::getPadCharacterString() const {
3846 return UnicodeString(fPad);
3849 void DecimalFormat::setPadCharacter(const UnicodeString &padChar) {
3850 if (padChar.length() > 0) {
3851 fPad = padChar.char32At(0);
3856 #if UCONFIG_FORMAT_FASTPATHS_49
3862 * Get the position at which padding will take place. This is the location
3863 * at which padding will be inserted if the result of <code>format()</code>
3864 * is shorter than the format width.
3865 * @return the pad position, one of <code>kPadBeforePrefix</code>,
3866 * <code>kPadAfterPrefix</code>, <code>kPadBeforeSuffix</code>, or
3867 * <code>kPadAfterSuffix</code>.
3868 * @see #setFormatWidth
3869 * @see #getFormatWidth
3870 * @see #setPadCharacter
3871 * @see #getPadCharacter
3872 * @see #setPadPosition
3873 * @see #kPadBeforePrefix
3874 * @see #kPadAfterPrefix
3875 * @see #kPadBeforeSuffix
3876 * @see #kPadAfterSuffix
3878 DecimalFormat::EPadPosition DecimalFormat::getPadPosition() const {
3879 return fPadPosition;
3883 * <strong><font face=helvetica color=red>NEW</font></strong>
3884 * Set the position at which padding will take place. This is the location
3885 * at which padding will be inserted if the result of <code>format()</code>
3886 * is shorter than the format width. This has no effect unless padding is
3888 * @param padPos the pad position, one of <code>kPadBeforePrefix</code>,
3889 * <code>kPadAfterPrefix</code>, <code>kPadBeforeSuffix</code>, or
3890 * <code>kPadAfterSuffix</code>.
3891 * @see #setFormatWidth
3892 * @see #getFormatWidth
3893 * @see #setPadCharacter
3894 * @see #getPadCharacter
3895 * @see #getPadPosition
3896 * @see #kPadBeforePrefix
3897 * @see #kPadAfterPrefix
3898 * @see #kPadBeforeSuffix
3899 * @see #kPadAfterSuffix
3901 void DecimalFormat::setPadPosition(EPadPosition padPos) {
3902 fPadPosition = padPos;
3903 #if UCONFIG_FORMAT_FASTPATHS_49
3909 * Return whether or not scientific notation is used.
3910 * @return TRUE if this object formats and parses scientific notation
3911 * @see #setScientificNotation
3912 * @see #getMinimumExponentDigits
3913 * @see #setMinimumExponentDigits
3914 * @see #isExponentSignAlwaysShown
3915 * @see #setExponentSignAlwaysShown
3917 UBool DecimalFormat::isScientificNotation() const {
3918 return fUseExponentialNotation;
3922 * Set whether or not scientific notation is used.
3923 * @param useScientific TRUE if this object formats and parses scientific
3925 * @see #isScientificNotation
3926 * @see #getMinimumExponentDigits
3927 * @see #setMinimumExponentDigits
3928 * @see #isExponentSignAlwaysShown
3929 * @see #setExponentSignAlwaysShown
3931 void DecimalFormat::setScientificNotation(UBool useScientific) {
3932 fUseExponentialNotation = useScientific;
3933 #if UCONFIG_FORMAT_FASTPATHS_49
3939 * Return the minimum exponent digits that will be shown.
3940 * @return the minimum exponent digits that will be shown
3941 * @see #setScientificNotation
3942 * @see #isScientificNotation
3943 * @see #setMinimumExponentDigits
3944 * @see #isExponentSignAlwaysShown
3945 * @see #setExponentSignAlwaysShown
3947 int8_t DecimalFormat::getMinimumExponentDigits() const {
3948 return fMinExponentDigits;
3952 * Set the minimum exponent digits that will be shown. This has no
3953 * effect unless scientific notation is in use.
3954 * @param minExpDig a value >= 1 indicating the fewest exponent digits
3955 * that will be shown. Values less than 1 will be treated as 1.
3956 * @see #setScientificNotation
3957 * @see #isScientificNotation
3958 * @see #getMinimumExponentDigits
3959 * @see #isExponentSignAlwaysShown
3960 * @see #setExponentSignAlwaysShown
3962 void DecimalFormat::setMinimumExponentDigits(int8_t minExpDig) {
3963 fMinExponentDigits = (int8_t)((minExpDig > 0) ? minExpDig : 1);
3964 #if UCONFIG_FORMAT_FASTPATHS_49
3970 * Return whether the exponent sign is always shown.
3971 * @return TRUE if the exponent is always prefixed with either the
3972 * localized minus sign or the localized plus sign, false if only negative
3973 * exponents are prefixed with the localized minus sign.
3974 * @see #setScientificNotation
3975 * @see #isScientificNotation
3976 * @see #setMinimumExponentDigits
3977 * @see #getMinimumExponentDigits
3978 * @see #setExponentSignAlwaysShown
3980 UBool DecimalFormat::isExponentSignAlwaysShown() const {
3981 return fExponentSignAlwaysShown;
3985 * Set whether the exponent sign is always shown. This has no effect
3986 * unless scientific notation is in use.
3987 * @param expSignAlways TRUE if the exponent is always prefixed with either
3988 * the localized minus sign or the localized plus sign, false if only
3989 * negative exponents are prefixed with the localized minus sign.
3990 * @see #setScientificNotation
3991 * @see #isScientificNotation
3992 * @see #setMinimumExponentDigits
3993 * @see #getMinimumExponentDigits
3994 * @see #isExponentSignAlwaysShown
3996 void DecimalFormat::setExponentSignAlwaysShown(UBool expSignAlways) {
3997 fExponentSignAlwaysShown = expSignAlways;
3998 #if UCONFIG_FORMAT_FASTPATHS_49
4003 //------------------------------------------------------------------------------
4004 // Gets the grouping size of the number pattern. For example, thousand or 10
4005 // thousand groupings.
4008 DecimalFormat::getGroupingSize() const
4010 return fGroupingSize;
4013 //------------------------------------------------------------------------------
4014 // Gets the grouping size of the number pattern.
4017 DecimalFormat::setGroupingSize(int32_t newValue)
4019 fGroupingSize = newValue;
4020 #if UCONFIG_FORMAT_FASTPATHS_49
4025 //------------------------------------------------------------------------------
4028 DecimalFormat::getSecondaryGroupingSize() const
4030 return fGroupingSize2;
4033 //------------------------------------------------------------------------------
4036 DecimalFormat::setSecondaryGroupingSize(int32_t newValue)
4038 fGroupingSize2 = newValue;
4039 #if UCONFIG_FORMAT_FASTPATHS_49
4044 //------------------------------------------------------------------------------
4045 // Checks if to show the decimal separator.
4048 DecimalFormat::isDecimalSeparatorAlwaysShown() const
4050 return fDecimalSeparatorAlwaysShown;
4053 //------------------------------------------------------------------------------
4054 // Sets to always show the decimal separator.
4057 DecimalFormat::setDecimalSeparatorAlwaysShown(UBool newValue)
4059 fDecimalSeparatorAlwaysShown = newValue;
4060 #if UCONFIG_FORMAT_FASTPATHS_49
4065 //------------------------------------------------------------------------------
4066 // Emits the pattern of this DecimalFormat instance.
4069 DecimalFormat::toPattern(UnicodeString& result) const
4071 return toPattern(result, FALSE);
4074 //------------------------------------------------------------------------------
4075 // Emits the localized pattern this DecimalFormat instance.
4078 DecimalFormat::toLocalizedPattern(UnicodeString& result) const
4080 return toPattern(result, TRUE);
4083 //------------------------------------------------------------------------------
4085 * Expand the affix pattern strings into the expanded affix strings. If any
4086 * affix pattern string is null, do not expand it. This method should be
4087 * called any time the symbols or the affix patterns change in order to keep
4088 * the expanded affix strings up to date.
4089 * This method also will be called before formatting if format currency
4090 * plural names, since the plural name is not a static one, it is
4091 * based on the currency plural count, the affix will be known only
4092 * after the currency plural count is know.
4093 * In which case, the parameter
4094 * 'pluralCount' will be a non-null currency plural count.
4095 * In all other cases, the 'pluralCount' is null, which means it is not needed.
4097 void DecimalFormat::expandAffixes(const UnicodeString* pluralCount) {
4098 FieldPositionHandler none;
4099 if (fPosPrefixPattern != 0) {
4100 expandAffix(*fPosPrefixPattern, fPositivePrefix, 0, none, FALSE, pluralCount);
4102 if (fPosSuffixPattern != 0) {
4103 expandAffix(*fPosSuffixPattern, fPositiveSuffix, 0, none, FALSE, pluralCount);
4105 if (fNegPrefixPattern != 0) {
4106 expandAffix(*fNegPrefixPattern, fNegativePrefix, 0, none, FALSE, pluralCount);
4108 if (fNegSuffixPattern != 0) {
4109 expandAffix(*fNegSuffixPattern, fNegativeSuffix, 0, none, FALSE, pluralCount);
4113 s.append(UnicodeString("["))
4114 .append(DEREFSTR(fPosPrefixPattern)).append((UnicodeString)"|").append(DEREFSTR(fPosSuffixPattern))
4115 .append((UnicodeString)";") .append(DEREFSTR(fNegPrefixPattern)).append((UnicodeString)"|").append(DEREFSTR(fNegSuffixPattern))
4116 .append((UnicodeString)"]->[")
4117 .append(fPositivePrefix).append((UnicodeString)"|").append(fPositiveSuffix)
4118 .append((UnicodeString)";") .append(fNegativePrefix).append((UnicodeString)"|").append(fNegativeSuffix)
4119 .append((UnicodeString)"]\n");
4125 * Expand an affix pattern into an affix string. All characters in the
4126 * pattern are literal unless prefixed by kQuote. The following characters
4127 * after kQuote are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
4128 * PATTERN_MINUS, and kCurrencySign. If kCurrencySign is doubled (kQuote +
4129 * kCurrencySign + kCurrencySign), it is interpreted as an international
4130 * currency sign. If CURRENCY_SIGN is tripled, it is interpreted as
4131 * currency plural long names, such as "US Dollars".
4132 * Any other character after a kQuote represents itself.
4133 * kQuote must be followed by another character; kQuote may not occur by
4134 * itself at the end of the pattern.
4136 * This method is used in two distinct ways. First, it is used to expand
4137 * the stored affix patterns into actual affixes. For this usage, doFormat
4138 * must be false. Second, it is used to expand the stored affix patterns
4139 * given a specific number (doFormat == true), for those rare cases in
4140 * which a currency format references a ChoiceFormat (e.g., en_IN display
4141 * name for INR). The number itself is taken from digitList.
4143 * When used in the first way, this method has a side effect: It sets
4144 * currencyChoice to a ChoiceFormat object, if the currency's display name
4145 * in this locale is a ChoiceFormat pattern (very rare). It only does this
4146 * if currencyChoice is null to start with.
4148 * @param pattern the non-null, fPossibly empty pattern
4149 * @param affix string to receive the expanded equivalent of pattern.
4150 * Previous contents are deleted.
4151 * @param doFormat if false, then the pattern will be expanded, and if a
4152 * currency symbol is encountered that expands to a ChoiceFormat, the
4153 * currencyChoice member variable will be initialized if it is null. If
4154 * doFormat is true, then it is assumed that the currencyChoice has been
4155 * created, and it will be used to format the value in digitList.
4156 * @param pluralCount the plural count. It is only used for currency
4157 * plural format. In which case, it is the plural
4158 * count of the currency amount. For example,
4159 * in en_US, it is the singular "one", or the plural
4160 * "other". For all other cases, it is null, and
4161 * is not being used.
4163 void DecimalFormat::expandAffix(const UnicodeString& pattern,
4164 UnicodeString& affix,
4166 FieldPositionHandler& handler,
4168 const UnicodeString* pluralCount) const {
4170 for (int i=0; i<pattern.length(); ) {
4171 UChar32 c = pattern.char32At(i);
4174 c = pattern.char32At(i);
4176 int beginIdx = affix.length();
4178 case kCurrencySign: {
4179 // As of ICU 2.2 we use the currency object, and
4180 // ignore the currency symbols in the DFS, unless
4181 // we have a null currency object. This occurs if
4182 // resurrecting a pre-2.2 object or if the user
4183 // sets a custom DFS.
4184 UBool intl = i<pattern.length() &&
4185 pattern.char32At(i) == kCurrencySign;
4186 UBool plural = FALSE;
4189 plural = i<pattern.length() &&
4190 pattern.char32At(i) == kCurrencySign;
4196 const UChar* currencyUChars = getCurrency();
4197 if (currencyUChars[0] != 0) {
4198 UErrorCode ec = U_ZERO_ERROR;
4199 if (plural && pluralCount != NULL) {
4200 // plural name is only needed when pluralCount != null,
4201 // which means when formatting currency plural names.
4202 // For other cases, pluralCount == null,
4203 // and plural names are not needed.
4205 CharString pluralCountChar;
4206 pluralCountChar.appendInvariantChars(*pluralCount, ec);
4207 UBool isChoiceFormat;
4208 const UChar* s = ucurr_getPluralName(currencyUChars,
4209 fSymbols != NULL ? fSymbols->getLocale().getName() :
4210 Locale::getDefault().getName(), &isChoiceFormat,
4211 pluralCountChar.data(), &len, &ec);
4212 affix += UnicodeString(s, len);
4213 handler.addAttribute(kCurrencyField, beginIdx, affix.length());
4215 affix.append(currencyUChars, -1);
4216 handler.addAttribute(kCurrencyField, beginIdx, affix.length());
4219 UBool isChoiceFormat;
4220 // If fSymbols is NULL, use default locale
4221 const UChar* s = ucurr_getName(currencyUChars,
4222 fSymbols != NULL ? fSymbols->getLocale().getName() : Locale::getDefault().getName(),
4223 UCURR_SYMBOL_NAME, &isChoiceFormat, &len, &ec);
4224 if (isChoiceFormat) {
4225 // Two modes here: If doFormat is false, we set up
4226 // currencyChoice. If doFormat is true, we use the
4227 // previously created currencyChoice to format the
4228 // value in digitList.
4230 // If the currency is handled by a ChoiceFormat,
4231 // then we're not going to use the expanded
4232 // patterns. Instantiate the ChoiceFormat and
4234 if (fCurrencyChoice == NULL) {
4235 // TODO Replace double-check with proper thread-safe code
4236 ChoiceFormat* fmt = new ChoiceFormat(UnicodeString(s), ec);
4237 if (U_SUCCESS(ec)) {
4239 if (fCurrencyChoice == NULL) {
4241 ((DecimalFormat*)this)->fCurrencyChoice = fmt;
4248 // We could almost return null or "" here, since the
4249 // expanded affixes are almost not used at all
4250 // in this situation. However, one method --
4251 // toPattern() -- still does use the expanded
4252 // affixes, in order to set up a padding
4253 // pattern. We use the CURRENCY_SIGN as a
4255 affix.append(kCurrencySign);
4257 if (fCurrencyChoice != NULL) {
4258 FieldPosition pos(0); // ignored
4262 fCurrencyChoice->format(number, affix, pos);
4264 // We only arrive here if the currency choice
4265 // format in the locale data is INVALID.
4266 affix.append(currencyUChars, -1);
4267 handler.addAttribute(kCurrencyField, beginIdx, affix.length());
4272 affix += UnicodeString(s, len);
4273 handler.addAttribute(kCurrencyField, beginIdx, affix.length());
4277 affix += getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol);
4279 affix += getConstSymbol(DecimalFormatSymbols::kCurrencySymbol);
4281 handler.addAttribute(kCurrencyField, beginIdx, affix.length());
4285 case kPatternPercent:
4286 affix += getConstSymbol(DecimalFormatSymbols::kPercentSymbol);
4287 handler.addAttribute(kPercentField, beginIdx, affix.length());
4289 case kPatternPerMill:
4290 affix += getConstSymbol(DecimalFormatSymbols::kPerMillSymbol);
4291 handler.addAttribute(kPermillField, beginIdx, affix.length());
4294 affix += getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
4295 handler.addAttribute(kSignField, beginIdx, affix.length());
4298 affix += getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
4299 handler.addAttribute(kSignField, beginIdx, affix.length());
4313 * Append an affix to the given StringBuffer.
4314 * @param buf buffer to append to
4318 int32_t DecimalFormat::appendAffix(UnicodeString& buf, double number,
4319 FieldPositionHandler& handler,
4320 UBool isNegative, UBool isPrefix) const {
4321 // plural format precedes choice format
4322 if (fCurrencyChoice != 0 &&
4323 fCurrencySignCount != fgCurrencySignCountInPluralFormat) {
4324 const UnicodeString* affixPat;
4326 affixPat = isNegative ? fNegPrefixPattern : fPosPrefixPattern;
4328 affixPat = isNegative ? fNegSuffixPattern : fPosSuffixPattern;
4331 UnicodeString affixBuf;
4332 expandAffix(*affixPat, affixBuf, number, handler, TRUE, NULL);
4333 buf.append(affixBuf);
4334 return affixBuf.length();
4336 // else someone called a function that reset the pattern.
4339 const UnicodeString* affix;
4340 if (fCurrencySignCount == fgCurrencySignCountInPluralFormat) {
4341 // TODO: get an accurate count of visible fraction digits.
4342 UnicodeString pluralCount;
4343 int32_t minFractionDigits = this->getMinimumFractionDigits();
4344 if (minFractionDigits > 0) {
4345 FixedDecimal ni(number, this->getMinimumFractionDigits());
4346 pluralCount = fCurrencyPluralInfo->getPluralRules()->select(ni);
4348 pluralCount = fCurrencyPluralInfo->getPluralRules()->select(number);
4350 AffixesForCurrency* oneSet;
4351 if (fStyle == UNUM_CURRENCY_PLURAL) {
4352 oneSet = (AffixesForCurrency*)fPluralAffixesForCurrency->get(pluralCount);
4354 oneSet = (AffixesForCurrency*)fAffixesForCurrency->get(pluralCount);
4357 affix = isNegative ? &oneSet->negPrefixForCurrency :
4358 &oneSet->posPrefixForCurrency;
4360 affix = isNegative ? &oneSet->negSuffixForCurrency :
4361 &oneSet->posSuffixForCurrency;
4365 affix = isNegative ? &fNegativePrefix : &fPositivePrefix;
4367 affix = isNegative ? &fNegativeSuffix : &fPositiveSuffix;
4371 int32_t begin = (int) buf.length();
4375 if (handler.isRecording()) {
4376 int32_t offset = (int) (*affix).indexOf(getConstSymbol(DecimalFormatSymbols::kCurrencySymbol));
4378 UnicodeString aff = getConstSymbol(DecimalFormatSymbols::kCurrencySymbol);
4379 handler.addAttribute(kCurrencyField, begin + offset, begin + offset + aff.length());
4382 offset = (int) (*affix).indexOf(getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol));
4384 UnicodeString aff = getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol);
4385 handler.addAttribute(kCurrencyField, begin + offset, begin + offset + aff.length());
4388 offset = (int) (*affix).indexOf(getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol));
4390 UnicodeString aff = getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
4391 handler.addAttribute(kSignField, begin + offset, begin + offset + aff.length());
4394 offset = (int) (*affix).indexOf(getConstSymbol(DecimalFormatSymbols::kPercentSymbol));
4396 UnicodeString aff = getConstSymbol(DecimalFormatSymbols::kPercentSymbol);
4397 handler.addAttribute(kPercentField, begin + offset, begin + offset + aff.length());
4400 offset = (int) (*affix).indexOf(getConstSymbol(DecimalFormatSymbols::kPerMillSymbol));
4402 UnicodeString aff = getConstSymbol(DecimalFormatSymbols::kPerMillSymbol);
4403 handler.addAttribute(kPermillField, begin + offset, begin + offset + aff.length());
4406 return affix->length();
4410 * Appends an affix pattern to the given StringBuffer, quoting special
4411 * characters as needed. Uses the internal affix pattern, if that exists,
4412 * or the literal affix, if the internal affix pattern is null. The
4413 * appended string will generate the same affix pattern (or literal affix)
4414 * when passed to toPattern().
4416 * @param appendTo the affix string is appended to this
4417 * @param affixPattern a pattern such as fPosPrefixPattern; may be null
4418 * @param expAffix a corresponding expanded affix, such as fPositivePrefix.
4419 * Ignored unless affixPattern is null. If affixPattern is null, then
4420 * expAffix is appended as a literal affix.
4421 * @param localized true if the appended pattern should contain localized
4422 * pattern characters; otherwise, non-localized pattern chars are appended
4424 void DecimalFormat::appendAffixPattern(UnicodeString& appendTo,
4425 const UnicodeString* affixPattern,
4426 const UnicodeString& expAffix,
4427 UBool localized) const {
4428 if (affixPattern == 0) {
4429 appendAffixPattern(appendTo, expAffix, localized);
4432 for (int pos=0; pos<affixPattern->length(); pos=i) {
4433 i = affixPattern->indexOf(kQuote, pos);
4436 affixPattern->extractBetween(pos, affixPattern->length(), s);
4437 appendAffixPattern(appendTo, s, localized);
4442 affixPattern->extractBetween(pos, i, s);
4443 appendAffixPattern(appendTo, s, localized);
4445 UChar32 c = affixPattern->char32At(++i);
4448 appendTo.append(c).append(c);
4449 // Fall through and append another kQuote below
4450 } else if (c == kCurrencySign &&
4451 i<affixPattern->length() &&
4452 affixPattern->char32At(i) == kCurrencySign) {
4454 appendTo.append(c).append(c);
4455 } else if (localized) {
4457 case kPatternPercent:
4458 appendTo += getConstSymbol(DecimalFormatSymbols::kPercentSymbol);
4460 case kPatternPerMill:
4461 appendTo += getConstSymbol(DecimalFormatSymbols::kPerMillSymbol);
4464 appendTo += getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
4467 appendTo += getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
4480 * Append an affix to the given StringBuffer, using quotes if
4481 * there are special characters. Single quotes themselves must be
4482 * escaped in either case.
4485 DecimalFormat::appendAffixPattern(UnicodeString& appendTo,
4486 const UnicodeString& affix,
4487 UBool localized) const {
4490 needQuote = affix.indexOf(getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol)) >= 0
4491 || affix.indexOf(getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol)) >= 0
4492 || affix.indexOf(getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol)) >= 0
4493 || affix.indexOf(getConstSymbol(DecimalFormatSymbols::kPercentSymbol)) >= 0
4494 || affix.indexOf(getConstSymbol(DecimalFormatSymbols::kPerMillSymbol)) >= 0
4495 || affix.indexOf(getConstSymbol(DecimalFormatSymbols::kDigitSymbol)) >= 0
4496 || affix.indexOf(getConstSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol)) >= 0
4497 || affix.indexOf(getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol)) >= 0
4498 || affix.indexOf(getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol)) >= 0
4499 || affix.indexOf(kCurrencySign) >= 0;
4502 needQuote = affix.indexOf(kPatternZeroDigit) >= 0
4503 || affix.indexOf(kPatternGroupingSeparator) >= 0
4504 || affix.indexOf(kPatternDecimalSeparator) >= 0
4505 || affix.indexOf(kPatternPercent) >= 0
4506 || affix.indexOf(kPatternPerMill) >= 0
4507 || affix.indexOf(kPatternDigit) >= 0
4508 || affix.indexOf(kPatternSeparator) >= 0
4509 || affix.indexOf(kPatternExponent) >= 0
4510 || affix.indexOf(kPatternPlus) >= 0
4511 || affix.indexOf(kPatternMinus) >= 0
4512 || affix.indexOf(kCurrencySign) >= 0;
4515 appendTo += (UChar)0x0027 /*'\''*/;
4516 if (affix.indexOf((UChar)0x0027 /*'\''*/) < 0)
4519 for (int32_t j = 0; j < affix.length(); ) {
4520 UChar32 c = affix.char32At(j);
4523 if (c == 0x0027 /*'\''*/)
4528 appendTo += (UChar)0x0027 /*'\''*/;
4531 //------------------------------------------------------------------------------
4534 DecimalFormat::toPattern(UnicodeString& result, UBool localized) const
4536 if (fStyle == UNUM_CURRENCY_PLURAL) {
4537 // the prefix or suffix pattern might not be defined yet,
4538 // so they can not be synthesized,
4539 // instead, get them directly.
4540 // but it might not be the actual pattern used in formatting.
4541 // the actual pattern used in formatting depends on the
4542 // formatted number's plural count.
4543 result = fFormatPattern;
4547 UChar32 zero, sigDigit = kPatternSignificantDigit;
4548 UnicodeString digit, group;
4550 int32_t roundingDecimalPos = 0; // Pos of decimal in roundingDigits
4551 UnicodeString roundingDigits;
4552 int32_t padPos = (fFormatWidth > 0) ? fPadPosition : -1;
4553 UnicodeString padSpec;
4554 UBool useSigDig = areSignificantDigitsUsed();
4557 digit.append(getConstSymbol(DecimalFormatSymbols::kDigitSymbol));
4558 group.append(getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
4559 zero = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
4561 sigDigit = getConstSymbol(DecimalFormatSymbols::kSignificantDigitSymbol).char32At(0);
4565 digit.append((UChar)kPatternDigit);
4566 group.append((UChar)kPatternGroupingSeparator);
4567 zero = (UChar32)kPatternZeroDigit;
4569 if (fFormatWidth > 0) {
4571 padSpec.append(getConstSymbol(DecimalFormatSymbols::kPadEscapeSymbol));
4574 padSpec.append((UChar)kPatternPadEscape);
4576 padSpec.append(fPad);
4578 if (fRoundingIncrement != NULL) {
4579 for(i=0; i<fRoundingIncrement->getCount(); ++i) {
4580 roundingDigits.append(zero+(fRoundingIncrement->getDigitValue(i))); // Convert to Unicode digit
4582 roundingDecimalPos = fRoundingIncrement->getDecimalAt();
4584 for (int32_t part=0; part<2; ++part) {
4585 if (padPos == kPadBeforePrefix) {
4586 result.append(padSpec);
4588 appendAffixPattern(result,
4589 (part==0 ? fPosPrefixPattern : fNegPrefixPattern),
4590 (part==0 ? fPositivePrefix : fNegativePrefix),
4592 if (padPos == kPadAfterPrefix && ! padSpec.isEmpty()) {
4593 result.append(padSpec);
4595 int32_t sub0Start = result.length();
4596 int32_t g = isGroupingUsed() ? _max(0, fGroupingSize) : 0;
4597 if (g > 0 && fGroupingSize2 > 0 && fGroupingSize2 != fGroupingSize) {
4598 g += fGroupingSize2;
4600 int32_t maxDig = 0, minDig = 0, maxSigDig = 0;
4602 minDig = getMinimumSignificantDigits();
4603 maxDig = maxSigDig = getMaximumSignificantDigits();
4605 minDig = getMinimumIntegerDigits();
4606 maxDig = getMaximumIntegerDigits();
4608 if (fUseExponentialNotation) {
4609 if (maxDig > kMaxScientificIntegerDigits) {
4612 } else if (useSigDig) {
4613 maxDig = _max(maxDig, g+1);
4615 maxDig = _max(_max(g, getMinimumIntegerDigits()),
4616 roundingDecimalPos) + 1;
4618 for (i = maxDig; i > 0; --i) {
4619 if (!fUseExponentialNotation && i<maxDig &&
4620 isGroupingPosition(i)) {
4621 result.append(group);
4624 // #@,@### (maxSigDig == 5, minSigDig == 2)
4625 // 65 4321 (1-based pos, count from the right)
4626 // Use # if pos > maxSigDig or 1 <= pos <= (maxSigDig - minSigDig)
4627 // Use @ if (maxSigDig - minSigDig) < pos <= maxSigDig
4628 if (maxSigDig >= i && i > (maxSigDig - minDig)) {
4629 result.append(sigDigit);
4631 result.append(digit);
4634 if (! roundingDigits.isEmpty()) {
4635 int32_t pos = roundingDecimalPos - i;
4636 if (pos >= 0 && pos < roundingDigits.length()) {
4637 result.append((UChar) (roundingDigits.char32At(pos) - kPatternZeroDigit + zero));
4642 result.append(zero);
4644 result.append(digit);
4649 if (getMaximumFractionDigits() > 0 || fDecimalSeparatorAlwaysShown) {
4651 result += getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
4654 result.append((UChar)kPatternDecimalSeparator);
4657 int32_t pos = roundingDecimalPos;
4658 for (i = 0; i < getMaximumFractionDigits(); ++i) {
4659 if (! roundingDigits.isEmpty() && pos < roundingDigits.length()) {
4661 result.append(zero);
4664 result.append((UChar)(roundingDigits.char32At(pos) - kPatternZeroDigit + zero));
4669 if (i<getMinimumFractionDigits()) {
4670 result.append(zero);
4673 result.append(digit);
4677 if (fUseExponentialNotation) {
4679 result += getConstSymbol(DecimalFormatSymbols::kExponentialSymbol);
4682 result.append((UChar)kPatternExponent);
4684 if (fExponentSignAlwaysShown) {
4686 result += getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
4689 result.append((UChar)kPatternPlus);
4692 for (i=0; i<fMinExponentDigits; ++i) {
4693 result.append(zero);
4696 if (! padSpec.isEmpty() && !fUseExponentialNotation) {
4697 int32_t add = fFormatWidth - result.length() + sub0Start
4699 ? fPositivePrefix.length() + fPositiveSuffix.length()
4700 : fNegativePrefix.length() + fNegativeSuffix.length());
4702 result.insert(sub0Start, digit);
4705 // Only add a grouping separator if we have at least
4706 // 2 additional characters to be added, so we don't
4707 // end up with ",###".
4708 if (add>1 && isGroupingPosition(maxDig)) {
4709 result.insert(sub0Start, group);
4714 if (fPadPosition == kPadBeforeSuffix && ! padSpec.isEmpty()) {
4715 result.append(padSpec);
4718 appendAffixPattern(result, fPosSuffixPattern, fPositiveSuffix, localized);
4719 if (fPadPosition == kPadAfterSuffix && ! padSpec.isEmpty()) {
4720 result.append(padSpec);
4722 UBool isDefault = FALSE;
4723 if ((fNegSuffixPattern == fPosSuffixPattern && // both null
4724 fNegativeSuffix == fPositiveSuffix)
4725 || (fNegSuffixPattern != 0 && fPosSuffixPattern != 0 &&
4726 *fNegSuffixPattern == *fPosSuffixPattern))
4728 if (fNegPrefixPattern != NULL && fPosPrefixPattern != NULL)
4730 int32_t length = fPosPrefixPattern->length();
4731 isDefault = fNegPrefixPattern->length() == (length+2) &&
4732 (*fNegPrefixPattern)[(int32_t)0] == kQuote &&
4733 (*fNegPrefixPattern)[(int32_t)1] == kPatternMinus &&
4734 fNegPrefixPattern->compare(2, length, *fPosPrefixPattern, 0, length) == 0;
4737 fNegPrefixPattern == NULL && fPosPrefixPattern == NULL)
4739 int32_t length = fPositivePrefix.length();
4740 isDefault = fNegativePrefix.length() == (length+1) &&
4741 fNegativePrefix.compare(getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol)) == 0 &&
4742 fNegativePrefix.compare(1, length, fPositivePrefix, 0, length) == 0;
4746 break; // Don't output default negative subpattern
4749 result += getConstSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol);
4752 result.append((UChar)kPatternSeparator);
4756 appendAffixPattern(result, fNegSuffixPattern, fNegativeSuffix, localized);
4757 if (fPadPosition == kPadAfterSuffix && ! padSpec.isEmpty()) {
4758 result.append(padSpec);
4766 //------------------------------------------------------------------------------
4769 DecimalFormat::applyPattern(const UnicodeString& pattern, UErrorCode& status)
4771 UParseError parseError;
4772 applyPattern(pattern, FALSE, parseError, status);
4775 //------------------------------------------------------------------------------
4778 DecimalFormat::applyPattern(const UnicodeString& pattern,
4779 UParseError& parseError,
4782 applyPattern(pattern, FALSE, parseError, status);
4784 //------------------------------------------------------------------------------
4787 DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status)
4789 UParseError parseError;
4790 applyPattern(pattern, TRUE,parseError,status);
4793 //------------------------------------------------------------------------------
4796 DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern,
4797 UParseError& parseError,
4800 applyPattern(pattern, TRUE,parseError,status);
4803 //------------------------------------------------------------------------------
4806 DecimalFormat::applyPatternWithoutExpandAffix(const UnicodeString& pattern,
4808 UParseError& parseError,
4811 if (U_FAILURE(status))
4815 // Clear error struct
4816 parseError.offset = -1;
4817 parseError.preContext[0] = parseError.postContext[0] = (UChar)0;
4819 // Set the significant pattern symbols
4820 UChar32 zeroDigit = kPatternZeroDigit; // '0'
4821 UChar32 sigDigit = kPatternSignificantDigit; // '@'
4822 UnicodeString groupingSeparator ((UChar)kPatternGroupingSeparator);
4823 UnicodeString decimalSeparator ((UChar)kPatternDecimalSeparator);
4824 UnicodeString percent ((UChar)kPatternPercent);
4825 UnicodeString perMill ((UChar)kPatternPerMill);
4826 UnicodeString digit ((UChar)kPatternDigit); // '#'
4827 UnicodeString separator ((UChar)kPatternSeparator);
4828 UnicodeString exponent ((UChar)kPatternExponent);
4829 UnicodeString plus ((UChar)kPatternPlus);
4830 UnicodeString minus ((UChar)kPatternMinus);
4831 UnicodeString padEscape ((UChar)kPatternPadEscape);
4832 // Substitute with the localized symbols if necessary
4834 zeroDigit = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
4835 sigDigit = getConstSymbol(DecimalFormatSymbols::kSignificantDigitSymbol).char32At(0);
4836 groupingSeparator. remove().append(getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
4837 decimalSeparator. remove().append(getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
4838 percent. remove().append(getConstSymbol(DecimalFormatSymbols::kPercentSymbol));
4839 perMill. remove().append(getConstSymbol(DecimalFormatSymbols::kPerMillSymbol));
4840 digit. remove().append(getConstSymbol(DecimalFormatSymbols::kDigitSymbol));
4841 separator. remove().append(getConstSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol));
4842 exponent. remove().append(getConstSymbol(DecimalFormatSymbols::kExponentialSymbol));
4843 plus. remove().append(getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol));
4844 minus. remove().append(getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol));
4845 padEscape. remove().append(getConstSymbol(DecimalFormatSymbols::kPadEscapeSymbol));
4847 UChar nineDigit = (UChar)(zeroDigit + 9);
4848 int32_t digitLen = digit.length();
4849 int32_t groupSepLen = groupingSeparator.length();
4850 int32_t decimalSepLen = decimalSeparator.length();
4853 int32_t patLen = pattern.length();
4854 // Part 0 is the positive pattern. Part 1, if present, is the negative
4856 for (int32_t part=0; part<2 && pos<patLen; ++part) {
4857 // The subpart ranges from 0 to 4: 0=pattern proper, 1=prefix,
4858 // 2=suffix, 3=prefix in quote, 4=suffix in quote. Subpart 0 is
4859 // between the prefix and suffix, and consists of pattern
4860 // characters. In the prefix and suffix, percent, perMill, and
4861 // currency symbols are recognized and translated.
4862 int32_t subpart = 1, sub0Start = 0, sub0Limit = 0, sub2Limit = 0;
4864 // It's important that we don't change any fields of this object
4865 // prematurely. We set the following variables for the multiplier,
4866 // grouping, etc., and then only change the actual object fields if
4867 // everything parses correctly. This also lets us register
4868 // the data from part 0 and ignore the part 1, except for the
4869 // prefix and suffix.
4870 UnicodeString prefix;
4871 UnicodeString suffix;
4872 int32_t decimalPos = -1;
4873 int32_t multiplier = 1;
4874 int32_t digitLeftCount = 0, zeroDigitCount = 0, digitRightCount = 0, sigDigitCount = 0;
4875 int8_t groupingCount = -1;
4876 int8_t groupingCount2 = -1;
4877 int32_t padPos = -1;
4878 UChar32 padChar = 0;
4879 int32_t roundingPos = -1;
4880 DigitList roundingInc;
4881 int8_t expDigits = -1;
4882 UBool expSignAlways = FALSE;
4884 // The affix is either the prefix or the suffix.
4885 UnicodeString* affix = &prefix;
4887 int32_t start = pos;
4888 UBool isPartDone = FALSE;
4891 for (; !isPartDone && pos < patLen; ) {
4892 // Todo: account for surrogate pairs
4893 ch = pattern.char32At(pos);
4895 case 0: // Pattern proper subpart (between prefix & suffix)
4896 // Process the digits, decimal, and grouping characters. We
4897 // record five pieces of information. We expect the digits
4898 // to occur in the pattern ####00.00####, and we record the
4899 // number of left digits, zero (central) digits, and right
4900 // digits. The position of the last grouping character is
4901 // recorded (should be somewhere within the first two blocks
4902 // of characters), as is the position of the decimal point,
4903 // if any (should be in the zero digits). If there is no
4904 // decimal point, then there should be no right digits.
4905 if (pattern.compare(pos, digitLen, digit) == 0) {
4906 if (zeroDigitCount > 0 || sigDigitCount > 0) {
4911 if (groupingCount >= 0 && decimalPos < 0) {
4915 } else if ((ch >= zeroDigit && ch <= nineDigit) ||
4917 if (digitRightCount > 0) {
4919 debug("Unexpected '0'")
4920 status = U_UNEXPECTED_TOKEN;
4921 syntaxError(pattern,pos,parseError);
4924 if (ch == sigDigit) {
4927 if (ch != zeroDigit && roundingPos < 0) {
4928 roundingPos = digitLeftCount + zeroDigitCount;
4930 if (roundingPos >= 0) {
4931 roundingInc.append((char)(ch - zeroDigit + '0'));
4935 if (groupingCount >= 0 && decimalPos < 0) {
4938 pos += U16_LENGTH(ch);
4939 } else if (pattern.compare(pos, groupSepLen, groupingSeparator) == 0) {
4940 if (decimalPos >= 0) {
4941 // Grouping separator after decimal
4942 debug("Grouping separator after decimal")
4943 status = U_UNEXPECTED_TOKEN;
4944 syntaxError(pattern,pos,parseError);
4947 groupingCount2 = groupingCount;
4950 } else if (pattern.compare(pos, decimalSepLen, decimalSeparator) == 0) {
4951 if (decimalPos >= 0) {
4952 // Multiple decimal separators
4953 debug("Multiple decimal separators")
4954 status = U_MULTIPLE_DECIMAL_SEPARATORS;
4955 syntaxError(pattern,pos,parseError);
4958 // Intentionally incorporate the digitRightCount,
4959 // even though it is illegal for this to be > 0
4960 // at this point. We check pattern syntax below.
4961 decimalPos = digitLeftCount + zeroDigitCount + digitRightCount;
4962 pos += decimalSepLen;
4964 if (pattern.compare(pos, exponent.length(), exponent) == 0) {
4965 if (expDigits >= 0) {
4966 // Multiple exponential symbols
4967 debug("Multiple exponential symbols")
4968 status = U_MULTIPLE_EXPONENTIAL_SYMBOLS;
4969 syntaxError(pattern,pos,parseError);
4972 if (groupingCount >= 0) {
4973 // Grouping separator in exponential pattern
4974 debug("Grouping separator in exponential pattern")
4975 status = U_MALFORMED_EXPONENTIAL_PATTERN;
4976 syntaxError(pattern,pos,parseError);
4979 pos += exponent.length();
4980 // Check for positive prefix
4982 && pattern.compare(pos, plus.length(), plus) == 0) {
4983 expSignAlways = TRUE;
4984 pos += plus.length();
4986 // Use lookahead to parse out the exponential part of the
4987 // pattern, then jump into suffix subpart.
4989 while (pos < patLen &&
4990 pattern.char32At(pos) == zeroDigit) {
4992 pos += U16_LENGTH(zeroDigit);
4995 // 1. Require at least one mantissa pattern digit
4996 // 2. Disallow "#+ @" in mantissa
4997 // 3. Require at least one exponent pattern digit
4998 if (((digitLeftCount + zeroDigitCount) < 1 &&
4999 (sigDigitCount + digitRightCount) < 1) ||
5000 (sigDigitCount > 0 && digitLeftCount > 0) ||
5002 // Malformed exponential pattern
5003 debug("Malformed exponential pattern")
5004 status = U_MALFORMED_EXPONENTIAL_PATTERN;
5005 syntaxError(pattern,pos,parseError);
5009 // Transition to suffix subpart
5010 subpart = 2; // suffix subpart
5016 case 1: // Prefix subpart
5017 case 2: // Suffix subpart
5018 // Process the prefix / suffix characters
5019 // Process unquoted characters seen in prefix or suffix
5022 // Several syntax characters implicitly begins the
5023 // next subpart if we are in the prefix; otherwise
5024 // they are illegal if unquoted.
5025 if (!pattern.compare(pos, digitLen, digit) ||
5026 !pattern.compare(pos, groupSepLen, groupingSeparator) ||
5027 !pattern.compare(pos, decimalSepLen, decimalSeparator) ||
5028 (ch >= zeroDigit && ch <= nineDigit) ||
5030 if (subpart == 1) { // prefix subpart
5031 subpart = 0; // pattern proper subpart
5032 sub0Start = pos; // Reprocess this character
5035 status = U_UNQUOTED_SPECIAL;
5036 syntaxError(pattern,pos,parseError);
5039 } else if (ch == kCurrencySign) {
5040 affix->append(kQuote); // Encode currency
5041 // Use lookahead to determine if the currency sign is
5043 U_ASSERT(U16_LENGTH(kCurrencySign) == 1);
5044 if ((pos+1) < pattern.length() && pattern[pos+1] == kCurrencySign) {
5045 affix->append(kCurrencySign);
5046 ++pos; // Skip over the doubled character
5047 if ((pos+1) < pattern.length() &&
5048 pattern[pos+1] == kCurrencySign) {
5049 affix->append(kCurrencySign);
5050 ++pos; // Skip over the doubled character
5051 fCurrencySignCount = fgCurrencySignCountInPluralFormat;
5053 fCurrencySignCount = fgCurrencySignCountInISOFormat;
5056 fCurrencySignCount = fgCurrencySignCountInSymbolFormat;
5058 // Fall through to append(ch)
5059 } else if (ch == kQuote) {
5060 // A quote outside quotes indicates either the opening
5061 // quote or two quotes, which is a quote literal. That is,
5062 // we have the first quote in 'do' or o''clock.
5063 U_ASSERT(U16_LENGTH(kQuote) == 1);
5065 if (pos < pattern.length() && pattern[pos] == kQuote) {
5066 affix->append(kQuote); // Encode quote
5067 // Fall through to append(ch)
5069 subpart += 2; // open quote
5072 } else if (pattern.compare(pos, separator.length(), separator) == 0) {
5073 // Don't allow separators in the prefix, and don't allow
5074 // separators in the second pattern (part == 1).
5075 if (subpart == 1 || part == 1) {
5076 // Unexpected separator
5077 debug("Unexpected separator")
5078 status = U_UNEXPECTED_TOKEN;
5079 syntaxError(pattern,pos,parseError);
5083 isPartDone = TRUE; // Go to next part
5084 pos += separator.length();
5086 } else if (pattern.compare(pos, percent.length(), percent) == 0) {
5087 // Next handle characters which are appended directly.
5088 if (multiplier != 1) {
5089 // Too many percent/perMill characters
5090 debug("Too many percent characters")
5091 status = U_MULTIPLE_PERCENT_SYMBOLS;
5092 syntaxError(pattern,pos,parseError);
5095 affix->append(kQuote); // Encode percent/perMill
5096 affix->append(kPatternPercent); // Use unlocalized pattern char
5098 pos += percent.length();
5100 } else if (pattern.compare(pos, perMill.length(), perMill) == 0) {
5101 // Next handle characters which are appended directly.
5102 if (multiplier != 1) {
5103 // Too many percent/perMill characters
5104 debug("Too many perMill characters")
5105 status = U_MULTIPLE_PERMILL_SYMBOLS;
5106 syntaxError(pattern,pos,parseError);
5109 affix->append(kQuote); // Encode percent/perMill
5110 affix->append(kPatternPerMill); // Use unlocalized pattern char
5112 pos += perMill.length();
5114 } else if (pattern.compare(pos, padEscape.length(), padEscape) == 0) {
5115 if (padPos >= 0 || // Multiple pad specifiers
5116 (pos+1) == pattern.length()) { // Nothing after padEscape
5117 debug("Multiple pad specifiers")
5118 status = U_MULTIPLE_PAD_SPECIFIERS;
5119 syntaxError(pattern,pos,parseError);
5123 pos += padEscape.length();
5124 padChar = pattern.char32At(pos);
5125 pos += U16_LENGTH(padChar);
5127 } else if (pattern.compare(pos, minus.length(), minus) == 0) {
5128 affix->append(kQuote); // Encode minus
5129 affix->append(kPatternMinus);
5130 pos += minus.length();
5132 } else if (pattern.compare(pos, plus.length(), plus) == 0) {
5133 affix->append(kQuote); // Encode plus
5134 affix->append(kPatternPlus);
5135 pos += plus.length();
5138 // Unquoted, non-special characters fall through to here, as
5139 // well as other code which needs to append something to the
5142 pos += U16_LENGTH(ch);
5144 case 3: // Prefix subpart, in quote
5145 case 4: // Suffix subpart, in quote
5146 // A quote within quotes indicates either the closing
5147 // quote or two quotes, which is a quote literal. That is,
5148 // we have the second quote in 'do' or 'don''t'.
5151 if (pos < pattern.length() && pattern[pos] == kQuote) {
5152 affix->append(kQuote); // Encode quote
5153 // Fall through to append(ch)
5155 subpart -= 2; // close quote
5160 pos += U16_LENGTH(ch);
5165 if (sub0Limit == 0) {
5166 sub0Limit = pattern.length();
5169 if (sub2Limit == 0) {
5170 sub2Limit = pattern.length();
5173 /* Handle patterns with no '0' pattern character. These patterns
5174 * are legal, but must be recodified to make sense. "##.###" ->
5175 * "#0.###". ".###" -> ".0##".
5177 * We allow patterns of the form "####" to produce a zeroDigitCount
5178 * of zero (got that?); although this seems like it might make it
5179 * possible for format() to produce empty strings, format() checks
5180 * for this condition and outputs a zero digit in this situation.
5181 * Having a zeroDigitCount of zero yields a minimum integer digits
5182 * of zero, which allows proper round-trip patterns. We don't want
5183 * "#" to become "#0" when toPattern() is called (even though that's
5184 * what it really is, semantically).
5186 if (zeroDigitCount == 0 && sigDigitCount == 0 &&
5187 digitLeftCount > 0 && decimalPos >= 0) {
5188 // Handle "###.###" and "###." and ".###"
5191 ++n; // Handle ".###"
5192 digitRightCount = digitLeftCount - n;
5193 digitLeftCount = n - 1;
5197 // Do syntax checking on the digits, decimal points, and quotes.
5198 if ((decimalPos < 0 && digitRightCount > 0 && sigDigitCount == 0) ||
5200 (sigDigitCount > 0 ||
5201 decimalPos < digitLeftCount ||
5202 decimalPos > (digitLeftCount + zeroDigitCount))) ||
5203 groupingCount == 0 || groupingCount2 == 0 ||
5204 (sigDigitCount > 0 && zeroDigitCount > 0) ||
5206 { // subpart > 2 == unmatched quote
5207 debug("Syntax error")
5208 status = U_PATTERN_SYNTAX_ERROR;
5209 syntaxError(pattern,pos,parseError);
5213 // Make sure pad is at legal position before or after affix.
5215 if (padPos == start) {
5216 padPos = kPadBeforePrefix;
5217 } else if (padPos+2 == sub0Start) {
5218 padPos = kPadAfterPrefix;
5219 } else if (padPos == sub0Limit) {
5220 padPos = kPadBeforeSuffix;
5221 } else if (padPos+2 == sub2Limit) {
5222 padPos = kPadAfterSuffix;
5224 // Illegal pad position
5225 debug("Illegal pad position")
5226 status = U_ILLEGAL_PAD_POSITION;
5227 syntaxError(pattern,pos,parseError);
5233 delete fPosPrefixPattern;
5234 delete fPosSuffixPattern;
5235 delete fNegPrefixPattern;
5236 delete fNegSuffixPattern;
5237 fPosPrefixPattern = new UnicodeString(prefix);
5239 if (fPosPrefixPattern == 0) {
5240 status = U_MEMORY_ALLOCATION_ERROR;
5243 fPosSuffixPattern = new UnicodeString(suffix);
5245 if (fPosSuffixPattern == 0) {
5246 status = U_MEMORY_ALLOCATION_ERROR;
5247 delete fPosPrefixPattern;
5250 fNegPrefixPattern = 0;
5251 fNegSuffixPattern = 0;
5253 fUseExponentialNotation = (expDigits >= 0);
5254 if (fUseExponentialNotation) {
5255 fMinExponentDigits = expDigits;
5257 fExponentSignAlwaysShown = expSignAlways;
5258 int32_t digitTotalCount = digitLeftCount + zeroDigitCount + digitRightCount;
5259 // The effectiveDecimalPos is the position the decimal is at or
5260 // would be at if there is no decimal. Note that if
5261 // decimalPos<0, then digitTotalCount == digitLeftCount +
5263 int32_t effectiveDecimalPos = decimalPos >= 0 ? decimalPos : digitTotalCount;
5264 UBool isSigDig = (sigDigitCount > 0);
5265 setSignificantDigitsUsed(isSigDig);
5267 setMinimumSignificantDigits(sigDigitCount);
5268 setMaximumSignificantDigits(sigDigitCount + digitRightCount);
5270 int32_t minInt = effectiveDecimalPos - digitLeftCount;
5271 setMinimumIntegerDigits(minInt);
5272 setMaximumIntegerDigits(fUseExponentialNotation
5273 ? digitLeftCount + getMinimumIntegerDigits()
5274 : NumberFormat::gDefaultMaxIntegerDigits);
5275 setMaximumFractionDigits(decimalPos >= 0
5276 ? (digitTotalCount - decimalPos) : 0);
5277 setMinimumFractionDigits(decimalPos >= 0
5278 ? (digitLeftCount + zeroDigitCount - decimalPos) : 0);
5280 setGroupingUsed(groupingCount > 0);
5281 fGroupingSize = (groupingCount > 0) ? groupingCount : 0;
5282 fGroupingSize2 = (groupingCount2 > 0 && groupingCount2 != groupingCount)
5283 ? groupingCount2 : 0;
5284 setMultiplier(multiplier);
5285 setDecimalSeparatorAlwaysShown(decimalPos == 0
5286 || decimalPos == digitTotalCount);
5288 fPadPosition = (EPadPosition) padPos;
5289 // To compute the format width, first set up sub0Limit -
5290 // sub0Start. Add in prefix/suffix length later.
5292 // fFormatWidth = prefix.length() + suffix.length() +
5293 // sub0Limit - sub0Start;
5294 fFormatWidth = sub0Limit - sub0Start;
5299 if (roundingPos >= 0) {
5300 roundingInc.setDecimalAt(effectiveDecimalPos - roundingPos);
5301 if (fRoundingIncrement != NULL) {
5302 *fRoundingIncrement = roundingInc;
5304 fRoundingIncrement = new DigitList(roundingInc);
5306 if (fRoundingIncrement == NULL) {
5307 status = U_MEMORY_ALLOCATION_ERROR;
5308 delete fPosPrefixPattern;
5309 delete fPosSuffixPattern;
5313 fRoundingMode = kRoundHalfEven;
5315 setRoundingIncrement(0.0);
5318 fNegPrefixPattern = new UnicodeString(prefix);
5320 if (fNegPrefixPattern == 0) {
5321 status = U_MEMORY_ALLOCATION_ERROR;
5324 fNegSuffixPattern = new UnicodeString(suffix);
5326 if (fNegSuffixPattern == 0) {
5327 delete fNegPrefixPattern;
5328 status = U_MEMORY_ALLOCATION_ERROR;
5334 if (pattern.length() == 0) {
5335 delete fNegPrefixPattern;
5336 delete fNegSuffixPattern;
5337 fNegPrefixPattern = NULL;
5338 fNegSuffixPattern = NULL;
5339 if (fPosPrefixPattern != NULL) {
5340 fPosPrefixPattern->remove();
5342 fPosPrefixPattern = new UnicodeString();
5344 if (fPosPrefixPattern == 0) {
5345 status = U_MEMORY_ALLOCATION_ERROR;
5349 if (fPosSuffixPattern != NULL) {
5350 fPosSuffixPattern->remove();
5352 fPosSuffixPattern = new UnicodeString();
5354 if (fPosSuffixPattern == 0) {
5355 delete fPosPrefixPattern;
5356 status = U_MEMORY_ALLOCATION_ERROR;
5361 setMinimumIntegerDigits(0);
5362 setMaximumIntegerDigits(kDoubleIntegerDigits);
5363 setMinimumFractionDigits(0);
5364 setMaximumFractionDigits(kDoubleFractionDigits);
5366 fUseExponentialNotation = FALSE;
5367 fCurrencySignCount = fgCurrencySignCountZero;
5368 setGroupingUsed(FALSE);
5372 setDecimalSeparatorAlwaysShown(FALSE);
5374 setRoundingIncrement(0.0);
5377 // If there was no negative pattern, or if the negative pattern is
5378 // identical to the positive pattern, then prepend the minus sign to the
5379 // positive pattern to form the negative pattern.
5380 if (fNegPrefixPattern == NULL ||
5381 (*fNegPrefixPattern == *fPosPrefixPattern
5382 && *fNegSuffixPattern == *fPosSuffixPattern)) {
5383 _copy_ptr(&fNegSuffixPattern, fPosSuffixPattern);
5384 if (fNegPrefixPattern == NULL) {
5385 fNegPrefixPattern = new UnicodeString();
5387 if (fNegPrefixPattern == 0) {
5388 status = U_MEMORY_ALLOCATION_ERROR;
5392 fNegPrefixPattern->remove();
5394 fNegPrefixPattern->append(kQuote).append(kPatternMinus)
5395 .append(*fPosPrefixPattern);
5399 s.append((UnicodeString)"\"").append(pattern).append((UnicodeString)"\"->");
5404 fFormatPattern = pattern;
5409 DecimalFormat::expandAffixAdjustWidth(const UnicodeString* pluralCount) {
5410 expandAffixes(pluralCount);
5411 if (fFormatWidth > 0) {
5412 // Finish computing format width (see above)
5413 // TODO: how to handle fFormatWidth,
5414 // need to save in f(Plural)AffixesForCurrecy?
5415 fFormatWidth += fPositivePrefix.length() + fPositiveSuffix.length();
5421 DecimalFormat::applyPattern(const UnicodeString& pattern,
5423 UParseError& parseError,
5426 // do the following re-set first. since they change private data by
5427 // apply pattern again.
5428 if (pattern.indexOf(kCurrencySign) != -1) {
5429 if (fCurrencyPluralInfo == NULL) {
5430 // initialize currencyPluralInfo if needed
5431 fCurrencyPluralInfo = new CurrencyPluralInfo(fSymbols->getLocale(), status);
5433 if (fAffixPatternsForCurrency == NULL) {
5434 setupCurrencyAffixPatterns(status);
5436 if (pattern.indexOf(fgTripleCurrencySign, 3, 0) != -1) {
5437 // only setup the affixes of the current pattern.
5438 setupCurrencyAffixes(pattern, TRUE, FALSE, status);
5441 applyPatternWithoutExpandAffix(pattern, localized, parseError, status);
5442 expandAffixAdjustWidth(NULL);
5443 #if UCONFIG_FORMAT_FASTPATHS_49
5450 DecimalFormat::applyPatternInternally(const UnicodeString& pluralCount,
5451 const UnicodeString& pattern,
5453 UParseError& parseError,
5454 UErrorCode& status) {
5455 applyPatternWithoutExpandAffix(pattern, localized, parseError, status);
5456 expandAffixAdjustWidth(&pluralCount);
5457 #if UCONFIG_FORMAT_FASTPATHS_49
5464 * Sets the maximum number of digits allowed in the integer portion of a
5466 * @see NumberFormat#setMaximumIntegerDigits
5468 void DecimalFormat::setMaximumIntegerDigits(int32_t newValue) {
5469 NumberFormat::setMaximumIntegerDigits(_min(newValue, gDefaultMaxIntegerDigits));
5470 #if UCONFIG_FORMAT_FASTPATHS_49
5476 * Sets the minimum number of digits allowed in the integer portion of a
5477 * number. This override limits the integer digit count to 309.
5478 * @see NumberFormat#setMinimumIntegerDigits
5480 void DecimalFormat::setMinimumIntegerDigits(int32_t newValue) {
5481 NumberFormat::setMinimumIntegerDigits(_min(newValue, kDoubleIntegerDigits));
5482 #if UCONFIG_FORMAT_FASTPATHS_49
5488 * Sets the maximum number of digits allowed in the fraction portion of a
5489 * number. This override limits the fraction digit count to 340.
5490 * @see NumberFormat#setMaximumFractionDigits
5492 void DecimalFormat::setMaximumFractionDigits(int32_t newValue) {
5493 NumberFormat::setMaximumFractionDigits(_min(newValue, kDoubleFractionDigits));
5494 #if UCONFIG_FORMAT_FASTPATHS_49
5500 * Sets the minimum number of digits allowed in the fraction portion of a
5501 * number. This override limits the fraction digit count to 340.
5502 * @see NumberFormat#setMinimumFractionDigits
5504 void DecimalFormat::setMinimumFractionDigits(int32_t newValue) {
5505 NumberFormat::setMinimumFractionDigits(_min(newValue, kDoubleFractionDigits));
5506 #if UCONFIG_FORMAT_FASTPATHS_49
5511 int32_t DecimalFormat::getMinimumSignificantDigits() const {
5512 return fMinSignificantDigits;
5515 int32_t DecimalFormat::getMaximumSignificantDigits() const {
5516 return fMaxSignificantDigits;
5519 void DecimalFormat::setMinimumSignificantDigits(int32_t min) {
5523 // pin max sig dig to >= min
5524 int32_t max = _max(fMaxSignificantDigits, min);
5525 fMinSignificantDigits = min;
5526 fMaxSignificantDigits = max;
5527 fUseSignificantDigits = TRUE;
5528 #if UCONFIG_FORMAT_FASTPATHS_49
5533 void DecimalFormat::setMaximumSignificantDigits(int32_t max) {
5537 // pin min sig dig to 1..max
5538 U_ASSERT(fMinSignificantDigits >= 1);
5539 int32_t min = _min(fMinSignificantDigits, max);
5540 fMinSignificantDigits = min;
5541 fMaxSignificantDigits = max;
5542 fUseSignificantDigits = TRUE;
5543 #if UCONFIG_FORMAT_FASTPATHS_49
5548 UBool DecimalFormat::areSignificantDigitsUsed() const {
5549 return fUseSignificantDigits;
5552 void DecimalFormat::setSignificantDigitsUsed(UBool useSignificantDigits) {
5553 fUseSignificantDigits = useSignificantDigits;
5554 #if UCONFIG_FORMAT_FASTPATHS_49
5559 void DecimalFormat::setCurrencyInternally(const UChar* theCurrency,
5561 // If we are a currency format, then modify our affixes to
5562 // encode the currency symbol for the given currency in our
5563 // locale, and adjust the decimal digits and rounding for the
5566 // Note: The code is ordered so that this object is *not changed*
5567 // until we are sure we are going to succeed.
5569 // NULL or empty currency is *legal* and indicates no currency.
5570 UBool isCurr = (theCurrency && *theCurrency);
5572 double rounding = 0.0;
5574 if (fCurrencySignCount != fgCurrencySignCountZero && isCurr) {
5575 rounding = ucurr_getRoundingIncrement(theCurrency, &ec);
5576 frac = ucurr_getDefaultFractionDigits(theCurrency, &ec);
5579 NumberFormat::setCurrency(theCurrency, ec);
5580 if (U_FAILURE(ec)) return;
5582 if (fCurrencySignCount != fgCurrencySignCountZero) {
5583 // NULL or empty currency is *legal* and indicates no currency.
5585 setRoundingIncrement(rounding);
5586 setMinimumFractionDigits(frac);
5587 setMaximumFractionDigits(frac);
5589 expandAffixes(NULL);
5591 #if UCONFIG_FORMAT_FASTPATHS_49
5596 void DecimalFormat::setCurrency(const UChar* theCurrency, UErrorCode& ec) {
5597 // set the currency before compute affixes to get the right currency names
5598 NumberFormat::setCurrency(theCurrency, ec);
5599 if (fFormatPattern.indexOf(fgTripleCurrencySign, 3, 0) != -1) {
5600 UnicodeString savedPtn = fFormatPattern;
5601 setupCurrencyAffixes(fFormatPattern, TRUE, TRUE, ec);
5602 UParseError parseErr;
5603 applyPattern(savedPtn, FALSE, parseErr, ec);
5605 // set the currency after apply pattern to get the correct rounding/fraction
5606 setCurrencyInternally(theCurrency, ec);
5607 #if UCONFIG_FORMAT_FASTPATHS_49
5612 // Deprecated variant with no UErrorCode parameter
5613 void DecimalFormat::setCurrency(const UChar* theCurrency) {
5614 UErrorCode ec = U_ZERO_ERROR;
5615 setCurrency(theCurrency, ec);
5616 #if UCONFIG_FORMAT_FASTPATHS_49
5621 void DecimalFormat::getEffectiveCurrency(UChar* result, UErrorCode& ec) const {
5622 if (fSymbols == NULL) {
5623 ec = U_MEMORY_ALLOCATION_ERROR;
5627 const UChar* c = getCurrency();
5629 const UnicodeString &intl =
5630 fSymbols->getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol);
5631 c = intl.getBuffer(); // ok for intl to go out of scope
5633 u_strncpy(result, c, 3);
5638 * Return the number of fraction digits to display, or the total
5639 * number of digits for significant digit formats and exponential
5643 DecimalFormat::precision() const {
5644 if (areSignificantDigitsUsed()) {
5645 return getMaximumSignificantDigits();
5646 } else if (fUseExponentialNotation) {
5647 return getMinimumIntegerDigits() + getMaximumFractionDigits();
5649 return getMaximumFractionDigits();
5654 // TODO: template algorithm
5656 DecimalFormat::initHashForAffix(UErrorCode& status) {
5657 if ( U_FAILURE(status) ) {
5661 if ( (hTable = new Hashtable(TRUE, status)) == NULL ) {
5662 status = U_MEMORY_ALLOCATION_ERROR;
5665 if ( U_FAILURE(status) ) {
5669 hTable->setValueComparator(decimfmtAffixValueComparator);
5674 DecimalFormat::initHashForAffixPattern(UErrorCode& status) {
5675 if ( U_FAILURE(status) ) {
5679 if ( (hTable = new Hashtable(TRUE, status)) == NULL ) {
5680 status = U_MEMORY_ALLOCATION_ERROR;
5683 if ( U_FAILURE(status) ) {
5687 hTable->setValueComparator(decimfmtAffixPatternValueComparator);
5692 DecimalFormat::deleteHashForAffix(Hashtable*& table)
5694 if ( table == NULL ) {
5698 const UHashElement* element = NULL;
5699 while ( (element = table->nextElement(pos)) != NULL ) {
5700 const UHashTok valueTok = element->value;
5701 const AffixesForCurrency* value = (AffixesForCurrency*)valueTok.pointer;
5711 DecimalFormat::deleteHashForAffixPattern()
5713 if ( fAffixPatternsForCurrency == NULL ) {
5717 const UHashElement* element = NULL;
5718 while ( (element = fAffixPatternsForCurrency->nextElement(pos)) != NULL ) {
5719 const UHashTok valueTok = element->value;
5720 const AffixPatternsForCurrency* value = (AffixPatternsForCurrency*)valueTok.pointer;
5723 delete fAffixPatternsForCurrency;
5724 fAffixPatternsForCurrency = NULL;
5729 DecimalFormat::copyHashForAffixPattern(const Hashtable* source,
5731 UErrorCode& status) {
5732 if ( U_FAILURE(status) ) {
5736 const UHashElement* element = NULL;
5738 while ( (element = source->nextElement(pos)) != NULL ) {
5739 const UHashTok keyTok = element->key;
5740 const UnicodeString* key = (UnicodeString*)keyTok.pointer;
5741 const UHashTok valueTok = element->value;
5742 const AffixPatternsForCurrency* value = (AffixPatternsForCurrency*)valueTok.pointer;
5743 AffixPatternsForCurrency* copy = new AffixPatternsForCurrency(
5744 value->negPrefixPatternForCurrency,
5745 value->negSuffixPatternForCurrency,
5746 value->posPrefixPatternForCurrency,
5747 value->posSuffixPatternForCurrency,
5748 value->patternType);
5749 target->put(UnicodeString(*key), copy, status);
5750 if ( U_FAILURE(status) ) {
5757 DecimalFormat& DecimalFormat::setAttribute( UNumberFormatAttribute attr,
5759 UErrorCode &status) {
5760 if(U_FAILURE(status)) return *this;
5763 case UNUM_LENIENT_PARSE:
5764 setLenient(newValue!=0);
5767 case UNUM_PARSE_INT_ONLY:
5768 setParseIntegerOnly(newValue!=0);
5771 case UNUM_GROUPING_USED:
5772 setGroupingUsed(newValue!=0);
5775 case UNUM_DECIMAL_ALWAYS_SHOWN:
5776 setDecimalSeparatorAlwaysShown(newValue!=0);
5779 case UNUM_MAX_INTEGER_DIGITS:
5780 setMaximumIntegerDigits(newValue);
5783 case UNUM_MIN_INTEGER_DIGITS:
5784 setMinimumIntegerDigits(newValue);
5787 case UNUM_INTEGER_DIGITS:
5788 setMinimumIntegerDigits(newValue);
5789 setMaximumIntegerDigits(newValue);
5792 case UNUM_MAX_FRACTION_DIGITS:
5793 setMaximumFractionDigits(newValue);
5796 case UNUM_MIN_FRACTION_DIGITS:
5797 setMinimumFractionDigits(newValue);
5800 case UNUM_FRACTION_DIGITS:
5801 setMinimumFractionDigits(newValue);
5802 setMaximumFractionDigits(newValue);
5805 case UNUM_SIGNIFICANT_DIGITS_USED:
5806 setSignificantDigitsUsed(newValue!=0);
5809 case UNUM_MAX_SIGNIFICANT_DIGITS:
5810 setMaximumSignificantDigits(newValue);
5813 case UNUM_MIN_SIGNIFICANT_DIGITS:
5814 setMinimumSignificantDigits(newValue);
5817 case UNUM_MULTIPLIER:
5818 setMultiplier(newValue);
5821 case UNUM_GROUPING_SIZE:
5822 setGroupingSize(newValue);
5825 case UNUM_ROUNDING_MODE:
5826 setRoundingMode((DecimalFormat::ERoundingMode)newValue);
5829 case UNUM_FORMAT_WIDTH:
5830 setFormatWidth(newValue);
5833 case UNUM_PADDING_POSITION:
5834 /** The position at which padding will take place. */
5835 setPadPosition((DecimalFormat::EPadPosition)newValue);
5838 case UNUM_SECONDARY_GROUPING_SIZE:
5839 setSecondaryGroupingSize(newValue);
5842 #if UCONFIG_HAVE_PARSEALLINPUT
5843 case UNUM_PARSE_ALL_INPUT:
5844 setParseAllInput((UNumberFormatAttributeValue)newValue);
5848 /* These are stored in fBoolFlags */
5849 case UNUM_PARSE_NO_EXPONENT:
5850 case UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS:
5851 if(!fBoolFlags.isValidValue(newValue)) {
5852 status = U_ILLEGAL_ARGUMENT_ERROR;
5854 fBoolFlags.set(attr, newValue);
5863 status = U_UNSUPPORTED_ERROR;
5869 int32_t DecimalFormat::getAttribute( UNumberFormatAttribute attr,
5870 UErrorCode &status ) const {
5871 if(U_FAILURE(status)) return -1;
5873 case UNUM_LENIENT_PARSE:
5876 case UNUM_PARSE_INT_ONLY:
5877 return isParseIntegerOnly();
5879 case UNUM_GROUPING_USED:
5880 return isGroupingUsed();
5882 case UNUM_DECIMAL_ALWAYS_SHOWN:
5883 return isDecimalSeparatorAlwaysShown();
5885 case UNUM_MAX_INTEGER_DIGITS:
5886 return getMaximumIntegerDigits();
5888 case UNUM_MIN_INTEGER_DIGITS:
5889 return getMinimumIntegerDigits();
5891 case UNUM_INTEGER_DIGITS:
5892 // TBD: what should this return?
5893 return getMinimumIntegerDigits();
5895 case UNUM_MAX_FRACTION_DIGITS:
5896 return getMaximumFractionDigits();
5898 case UNUM_MIN_FRACTION_DIGITS:
5899 return getMinimumFractionDigits();
5901 case UNUM_FRACTION_DIGITS:
5902 // TBD: what should this return?
5903 return getMinimumFractionDigits();
5905 case UNUM_SIGNIFICANT_DIGITS_USED:
5906 return areSignificantDigitsUsed();
5908 case UNUM_MAX_SIGNIFICANT_DIGITS:
5909 return getMaximumSignificantDigits();
5911 case UNUM_MIN_SIGNIFICANT_DIGITS:
5912 return getMinimumSignificantDigits();
5914 case UNUM_MULTIPLIER:
5915 return getMultiplier();
5917 case UNUM_GROUPING_SIZE:
5918 return getGroupingSize();
5920 case UNUM_ROUNDING_MODE:
5921 return getRoundingMode();
5923 case UNUM_FORMAT_WIDTH:
5924 return getFormatWidth();
5926 case UNUM_PADDING_POSITION:
5927 return getPadPosition();
5929 case UNUM_SECONDARY_GROUPING_SIZE:
5930 return getSecondaryGroupingSize();
5932 /* These are stored in fBoolFlags */
5933 case UNUM_PARSE_NO_EXPONENT:
5934 case UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS:
5935 return fBoolFlags.get(attr);
5941 status = U_UNSUPPORTED_ERROR;
5945 return -1; /* undefined */
5948 #if UCONFIG_HAVE_PARSEALLINPUT
5949 void DecimalFormat::setParseAllInput(UNumberFormatAttributeValue value) {
5950 fParseAllInput = value;
5951 #if UCONFIG_FORMAT_FASTPATHS_49
5958 DecimalFormat::copyHashForAffix(const Hashtable* source,
5960 UErrorCode& status) {
5961 if ( U_FAILURE(status) ) {
5965 const UHashElement* element = NULL;
5967 while ( (element = source->nextElement(pos)) != NULL ) {
5968 const UHashTok keyTok = element->key;
5969 const UnicodeString* key = (UnicodeString*)keyTok.pointer;
5971 const UHashTok valueTok = element->value;
5972 const AffixesForCurrency* value = (AffixesForCurrency*)valueTok.pointer;
5973 AffixesForCurrency* copy = new AffixesForCurrency(
5974 value->negPrefixForCurrency,
5975 value->negSuffixForCurrency,
5976 value->posPrefixForCurrency,
5977 value->posSuffixForCurrency);
5978 target->put(UnicodeString(*key), copy, status);
5979 if ( U_FAILURE(status) ) {
5988 #endif /* #if !UCONFIG_NO_FORMATTING */