- appendAffixPattern(result, fNegSuffixPattern, fNegativeSuffix, localized);
- if (fPadPosition == kPadAfterSuffix && ! padSpec.isEmpty()) {
- result.append(padSpec);
- }
- }
- }
-
- return result;
-}
-
-//------------------------------------------------------------------------------
-
-void
-DecimalFormat::applyPattern(const UnicodeString& pattern, UErrorCode& status)
-{
- UParseError parseError;
- applyPattern(pattern, FALSE, parseError, status);
-}
-
-//------------------------------------------------------------------------------
-
-void
-DecimalFormat::applyPattern(const UnicodeString& pattern,
- UParseError& parseError,
- UErrorCode& status)
-{
- applyPattern(pattern, FALSE, parseError, status);
-}
-//------------------------------------------------------------------------------
-
-void
-DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status)
-{
- UParseError parseError;
- applyPattern(pattern, TRUE,parseError,status);
-}
-
-//------------------------------------------------------------------------------
-
-void
-DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern,
- UParseError& parseError,
- UErrorCode& status)
-{
- applyPattern(pattern, TRUE,parseError,status);
-}
-
-//------------------------------------------------------------------------------
-
-void
-DecimalFormat::applyPattern(const UnicodeString& pattern,
- UBool localized,
- UParseError& parseError,
- UErrorCode& status)
-{
- if (U_FAILURE(status))
- {
- return;
- }
- // Clear error struct
- parseError.offset = -1;
- parseError.preContext[0] = parseError.postContext[0] = (UChar)0;
-
- // Set the significant pattern symbols
- UChar32 zeroDigit = kPatternZeroDigit; // '0'
- UChar32 sigDigit = kPatternSignificantDigit; // '@'
- UnicodeString groupingSeparator ((UChar)kPatternGroupingSeparator);
- UnicodeString decimalSeparator ((UChar)kPatternDecimalSeparator);
- UnicodeString percent ((UChar)kPatternPercent);
- UnicodeString perMill ((UChar)kPatternPerMill);
- UnicodeString digit ((UChar)kPatternDigit); // '#'
- UnicodeString separator ((UChar)kPatternSeparator);
- UnicodeString exponent ((UChar)kPatternExponent);
- UnicodeString plus ((UChar)kPatternPlus);
- UnicodeString minus ((UChar)kPatternMinus);
- UnicodeString padEscape ((UChar)kPatternPadEscape);
- // Substitute with the localized symbols if necessary
- if (localized) {
- zeroDigit = getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
- sigDigit = getConstSymbol(DecimalFormatSymbols::kSignificantDigitSymbol).char32At(0);
- groupingSeparator. remove().append(getConstSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
- decimalSeparator. remove().append(getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
- percent. remove().append(getConstSymbol(DecimalFormatSymbols::kPercentSymbol));
- perMill. remove().append(getConstSymbol(DecimalFormatSymbols::kPerMillSymbol));
- digit. remove().append(getConstSymbol(DecimalFormatSymbols::kDigitSymbol));
- separator. remove().append(getConstSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol));
- exponent. remove().append(getConstSymbol(DecimalFormatSymbols::kExponentialSymbol));
- plus. remove().append(getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol));
- minus. remove().append(getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol));
- padEscape. remove().append(getConstSymbol(DecimalFormatSymbols::kPadEscapeSymbol));
- }
- UChar nineDigit = (UChar)(zeroDigit + 9);
- int32_t digitLen = digit.length();
- int32_t groupSepLen = groupingSeparator.length();
- int32_t decimalSepLen = decimalSeparator.length();
-
- int32_t pos = 0;
- int32_t patLen = pattern.length();
- // Part 0 is the positive pattern. Part 1, if present, is the negative
- // pattern.
- for (int32_t part=0; part<2 && pos<patLen; ++part) {
- // The subpart ranges from 0 to 4: 0=pattern proper, 1=prefix,
- // 2=suffix, 3=prefix in quote, 4=suffix in quote. Subpart 0 is
- // between the prefix and suffix, and consists of pattern
- // characters. In the prefix and suffix, percent, perMill, and
- // currency symbols are recognized and translated.
- int32_t subpart = 1, sub0Start = 0, sub0Limit = 0, sub2Limit = 0;
-
- // It's important that we don't change any fields of this object
- // prematurely. We set the following variables for the multiplier,
- // grouping, etc., and then only change the actual object fields if
- // everything parses correctly. This also lets us register
- // the data from part 0 and ignore the part 1, except for the
- // prefix and suffix.
- UnicodeString prefix;
- UnicodeString suffix;
- int32_t decimalPos = -1;
- int32_t multiplier = 1;
- int32_t digitLeftCount = 0, zeroDigitCount = 0, digitRightCount = 0, sigDigitCount = 0;
- int8_t groupingCount = -1;
- int8_t groupingCount2 = -1;
- int32_t padPos = -1;
- UChar32 padChar = 0;
- int32_t roundingPos = -1;
- DigitList roundingInc;
- int8_t expDigits = -1;
- UBool expSignAlways = FALSE;
- UBool isCurrency = FALSE;
-
- // The affix is either the prefix or the suffix.
- UnicodeString* affix = &prefix;
-
- int32_t start = pos;
- UBool isPartDone = FALSE;
- UChar32 ch;
-
- for (; !isPartDone && pos < patLen; ) {
- // Todo: account for surrogate pairs
- ch = pattern.char32At(pos);
- switch (subpart) {
- case 0: // Pattern proper subpart (between prefix & suffix)
- // Process the digits, decimal, and grouping characters. We
- // record five pieces of information. We expect the digits
- // to occur in the pattern ####00.00####, and we record the
- // number of left digits, zero (central) digits, and right
- // digits. The position of the last grouping character is
- // recorded (should be somewhere within the first two blocks
- // of characters), as is the position of the decimal point,
- // if any (should be in the zero digits). If there is no
- // decimal point, then there should be no right digits.
- if (pattern.compare(pos, digitLen, digit) == 0) {
- if (zeroDigitCount > 0 || sigDigitCount > 0) {
- ++digitRightCount;
- } else {
- ++digitLeftCount;
- }
- if (groupingCount >= 0 && decimalPos < 0) {
- ++groupingCount;
- }
- pos += digitLen;
- } else if ((ch >= zeroDigit && ch <= nineDigit) ||
- ch == sigDigit) {
- if (digitRightCount > 0) {
- // Unexpected '0'
- debug("Unexpected '0'")
- status = U_UNEXPECTED_TOKEN;
- syntaxError(pattern,pos,parseError);
- return;
- }
- if (ch == sigDigit) {
- ++sigDigitCount;
- } else {
- ++zeroDigitCount;
- if (ch != zeroDigit && roundingPos < 0) {
- roundingPos = digitLeftCount + zeroDigitCount;
- }
- if (roundingPos >= 0) {
- roundingInc.append((char)(ch - zeroDigit + '0'));
- }
- }
- if (groupingCount >= 0 && decimalPos < 0) {
- ++groupingCount;
- }
- pos += U16_LENGTH(ch);
- } else if (pattern.compare(pos, groupSepLen, groupingSeparator) == 0) {
- if (decimalPos >= 0) {
- // Grouping separator after decimal
- debug("Grouping separator after decimal")
- status = U_UNEXPECTED_TOKEN;
- syntaxError(pattern,pos,parseError);
- return;
- }
- groupingCount2 = groupingCount;
- groupingCount = 0;
- pos += groupSepLen;
- } else if (pattern.compare(pos, decimalSepLen, decimalSeparator) == 0) {
- if (decimalPos >= 0) {
- // Multiple decimal separators
- debug("Multiple decimal separators")
- status = U_MULTIPLE_DECIMAL_SEPARATORS;
- syntaxError(pattern,pos,parseError);
- return;
- }
- // Intentionally incorporate the digitRightCount,
- // even though it is illegal for this to be > 0
- // at this point. We check pattern syntax below.
- decimalPos = digitLeftCount + zeroDigitCount + digitRightCount;
- pos += decimalSepLen;
- } else {
- if (pattern.compare(pos, exponent.length(), exponent) == 0) {
- if (expDigits >= 0) {
- // Multiple exponential symbols
- debug("Multiple exponential symbols")
- status = U_MULTIPLE_EXPONENTIAL_SYMBOLS;
- syntaxError(pattern,pos,parseError);
- return;
- }
- if (groupingCount >= 0) {
- // Grouping separator in exponential pattern
- debug("Grouping separator in exponential pattern")
- status = U_MALFORMED_EXPONENTIAL_PATTERN;
- syntaxError(pattern,pos,parseError);
- return;
- }
- pos += exponent.length();
- // Check for positive prefix
- if (pos < patLen
- && pattern.compare(pos, plus.length(), plus) == 0) {
- expSignAlways = TRUE;
- pos += plus.length();
- }
- // Use lookahead to parse out the exponential part of the
- // pattern, then jump into suffix subpart.
- expDigits = 0;
- while (pos < patLen &&
- pattern.char32At(pos) == zeroDigit) {
- ++expDigits;
- pos += U16_LENGTH(zeroDigit);
- }
-
- // 1. Require at least one mantissa pattern digit
- // 2. Disallow "#+ @" in mantissa
- // 3. Require at least one exponent pattern digit
- if (((digitLeftCount + zeroDigitCount) < 1 &&
- (sigDigitCount + digitRightCount) < 1) ||
- (sigDigitCount > 0 && digitLeftCount > 0) ||
- expDigits < 1) {
- // Malformed exponential pattern
- debug("Malformed exponential pattern")
- status = U_MALFORMED_EXPONENTIAL_PATTERN;
- syntaxError(pattern,pos,parseError);
- return;
- }
- }
- // Transition to suffix subpart
- subpart = 2; // suffix subpart
- affix = &suffix;
- sub0Limit = pos;
- continue;
- }
- break;
- case 1: // Prefix subpart
- case 2: // Suffix subpart
- // Process the prefix / suffix characters
- // Process unquoted characters seen in prefix or suffix
- // subpart.
-
- // Several syntax characters implicitly begins the
- // next subpart if we are in the prefix; otherwise
- // they are illegal if unquoted.
- if (!pattern.compare(pos, digitLen, digit) ||
- !pattern.compare(pos, groupSepLen, groupingSeparator) ||
- !pattern.compare(pos, decimalSepLen, decimalSeparator) ||
- (ch >= zeroDigit && ch <= nineDigit) ||
- ch == sigDigit) {
- if (subpart == 1) { // prefix subpart
- subpart = 0; // pattern proper subpart
- sub0Start = pos; // Reprocess this character
- continue;
- } else {
- status = U_UNQUOTED_SPECIAL;
- syntaxError(pattern,pos,parseError);
- return;
- }
- } else if (ch == kCurrencySign) {
- affix->append(kQuote); // Encode currency
- // Use lookahead to determine if the currency sign is
- // doubled or not.
- U_ASSERT(U16_LENGTH(kCurrencySign) == 1);
- if ((pos+1) < pattern.length() && pattern[pos+1] == kCurrencySign) {
- affix->append(kCurrencySign);
- ++pos; // Skip over the doubled character
- }
- isCurrency = TRUE;
- // Fall through to append(ch)
- } else if (ch == kQuote) {
- // A quote outside quotes indicates either the opening
- // quote or two quotes, which is a quote literal. That is,
- // we have the first quote in 'do' or o''clock.
- U_ASSERT(U16_LENGTH(kQuote) == 1);
- ++pos;
- if (pos < pattern.length() && pattern[pos] == kQuote) {
- affix->append(kQuote); // Encode quote
- // Fall through to append(ch)
- } else {
- subpart += 2; // open quote
- continue;
- }
- } else if (pattern.compare(pos, separator.length(), separator) == 0) {
- // Don't allow separators in the prefix, and don't allow
- // separators in the second pattern (part == 1).
- if (subpart == 1 || part == 1) {
- // Unexpected separator
- debug("Unexpected separator")
- status = U_UNEXPECTED_TOKEN;
- syntaxError(pattern,pos,parseError);
- return;
- }
- sub2Limit = pos;
- isPartDone = TRUE; // Go to next part
- pos += separator.length();
- break;
- } else if (pattern.compare(pos, percent.length(), percent) == 0) {
- // Next handle characters which are appended directly.
- if (multiplier != 1) {
- // Too many percent/perMill characters
- debug("Too many percent characters")
- status = U_MULTIPLE_PERCENT_SYMBOLS;
- syntaxError(pattern,pos,parseError);
- return;
- }
- affix->append(kQuote); // Encode percent/perMill
- affix->append(kPatternPercent); // Use unlocalized pattern char
- multiplier = 100;
- pos += percent.length();
- break;
- } else if (pattern.compare(pos, perMill.length(), perMill) == 0) {
- // Next handle characters which are appended directly.
- if (multiplier != 1) {
- // Too many percent/perMill characters
- debug("Too many perMill characters")
- status = U_MULTIPLE_PERMILL_SYMBOLS;
- syntaxError(pattern,pos,parseError);
- return;
- }
- affix->append(kQuote); // Encode percent/perMill
- affix->append(kPatternPerMill); // Use unlocalized pattern char
- multiplier = 1000;
- pos += perMill.length();
- break;
- } else if (pattern.compare(pos, padEscape.length(), padEscape) == 0) {
- if (padPos >= 0 || // Multiple pad specifiers
- (pos+1) == pattern.length()) { // Nothing after padEscape
- debug("Multiple pad specifiers")
- status = U_MULTIPLE_PAD_SPECIFIERS;
- syntaxError(pattern,pos,parseError);
- return;
- }
- padPos = pos;
- pos += padEscape.length();
- padChar = pattern.char32At(pos);
- pos += U16_LENGTH(padChar);
- break;
- } else if (pattern.compare(pos, minus.length(), minus) == 0) {
- affix->append(kQuote); // Encode minus
- affix->append(kPatternMinus);
- pos += minus.length();
- break;
- } else if (pattern.compare(pos, plus.length(), plus) == 0) {
- affix->append(kQuote); // Encode plus
- affix->append(kPatternPlus);
- pos += plus.length();
- break;
- }
- // Unquoted, non-special characters fall through to here, as
- // well as other code which needs to append something to the
- // affix.
- affix->append(ch);
- pos += U16_LENGTH(ch);
- break;
- case 3: // Prefix subpart, in quote
- case 4: // Suffix subpart, in quote
- // A quote within quotes indicates either the closing
- // quote or two quotes, which is a quote literal. That is,
- // we have the second quote in 'do' or 'don''t'.
- if (ch == kQuote) {
- ++pos;
- if (pos < pattern.length() && pattern[pos] == kQuote) {
- affix->append(kQuote); // Encode quote
- // Fall through to append(ch)
- } else {
- subpart -= 2; // close quote
- continue;
- }
- }
- affix->append(ch);
- pos += U16_LENGTH(ch);
- break;
- }
- }
-
- if (sub0Limit == 0) {
- sub0Limit = pattern.length();
- }
-
- if (sub2Limit == 0) {
- sub2Limit = pattern.length();
- }
-
- /* Handle patterns with no '0' pattern character. These patterns
- * are legal, but must be recodified to make sense. "##.###" ->
- * "#0.###". ".###" -> ".0##".
- *
- * We allow patterns of the form "####" to produce a zeroDigitCount
- * of zero (got that?); although this seems like it might make it
- * possible for format() to produce empty strings, format() checks
- * for this condition and outputs a zero digit in this situation.
- * Having a zeroDigitCount of zero yields a minimum integer digits
- * of zero, which allows proper round-trip patterns. We don't want
- * "#" to become "#0" when toPattern() is called (even though that's
- * what it really is, semantically).
- */
- if (zeroDigitCount == 0 && sigDigitCount == 0 &&
- digitLeftCount > 0 && decimalPos >= 0) {
- // Handle "###.###" and "###." and ".###"
- int n = decimalPos;
- if (n == 0)
- ++n; // Handle ".###"
- digitRightCount = digitLeftCount - n;
- digitLeftCount = n - 1;
- zeroDigitCount = 1;
- }
-
- // Do syntax checking on the digits, decimal points, and quotes.
- if ((decimalPos < 0 && digitRightCount > 0 && sigDigitCount == 0) ||
- (decimalPos >= 0 &&
- (sigDigitCount > 0 ||
- decimalPos < digitLeftCount ||
- decimalPos > (digitLeftCount + zeroDigitCount))) ||
- groupingCount == 0 || groupingCount2 == 0 ||
- (sigDigitCount > 0 && zeroDigitCount > 0) ||
- subpart > 2)
- { // subpart > 2 == unmatched quote
- debug("Syntax error")
- status = U_PATTERN_SYNTAX_ERROR;
- syntaxError(pattern,pos,parseError);
- return;
- }
-
- // Make sure pad is at legal position before or after affix.
- if (padPos >= 0) {
- if (padPos == start) {
- padPos = kPadBeforePrefix;
- } else if (padPos+2 == sub0Start) {
- padPos = kPadAfterPrefix;
- } else if (padPos == sub0Limit) {
- padPos = kPadBeforeSuffix;
- } else if (padPos+2 == sub2Limit) {
- padPos = kPadAfterSuffix;
- } else {
- // Illegal pad position
- debug("Illegal pad position")
- status = U_ILLEGAL_PAD_POSITION;
- syntaxError(pattern,pos,parseError);
- return;
- }
- }
-
- if (part == 0) {
- delete fPosPrefixPattern;
- delete fPosSuffixPattern;
- delete fNegPrefixPattern;
- delete fNegSuffixPattern;
- fPosPrefixPattern = new UnicodeString(prefix);
- /* test for NULL */
- if (fPosPrefixPattern == 0) {
- status = U_MEMORY_ALLOCATION_ERROR;
- return;
- }
- fPosSuffixPattern = new UnicodeString(suffix);
- /* test for NULL */
- if (fPosSuffixPattern == 0) {
- status = U_MEMORY_ALLOCATION_ERROR;
- delete fPosPrefixPattern;
- return;
- }
- fNegPrefixPattern = 0;
- fNegSuffixPattern = 0;
-
- fUseExponentialNotation = (expDigits >= 0);
- if (fUseExponentialNotation) {
- fMinExponentDigits = expDigits;
- }
- fExponentSignAlwaysShown = expSignAlways;
- fIsCurrencyFormat = isCurrency;
- int32_t digitTotalCount = digitLeftCount + zeroDigitCount + digitRightCount;
- // The effectiveDecimalPos is the position the decimal is at or
- // would be at if there is no decimal. Note that if
- // decimalPos<0, then digitTotalCount == digitLeftCount +
- // zeroDigitCount.
- int32_t effectiveDecimalPos = decimalPos >= 0 ? decimalPos : digitTotalCount;
- UBool isSigDig = (sigDigitCount > 0);
- setSignificantDigitsUsed(isSigDig);
- if (isSigDig) {
- setMinimumSignificantDigits(sigDigitCount);
- setMaximumSignificantDigits(sigDigitCount + digitRightCount);
- } else {
- int32_t minInt = effectiveDecimalPos - digitLeftCount;
- setMinimumIntegerDigits(minInt);
- setMaximumIntegerDigits(fUseExponentialNotation
- ? digitLeftCount + getMinimumIntegerDigits()
- : kDoubleIntegerDigits);
- setMaximumFractionDigits(decimalPos >= 0
- ? (digitTotalCount - decimalPos) : 0);
- setMinimumFractionDigits(decimalPos >= 0
- ? (digitLeftCount + zeroDigitCount - decimalPos) : 0);
- }
- setGroupingUsed(groupingCount > 0);
- fGroupingSize = (groupingCount > 0) ? groupingCount : 0;
- fGroupingSize2 = (groupingCount2 > 0 && groupingCount2 != groupingCount)
- ? groupingCount2 : 0;
- fMultiplier = multiplier;
- setDecimalSeparatorAlwaysShown(decimalPos == 0
- || decimalPos == digitTotalCount);
- if (padPos >= 0) {
- fPadPosition = (EPadPosition) padPos;
- // To compute the format width, first set up sub0Limit -
- // sub0Start. Add in prefix/suffix length later.
-
- // fFormatWidth = prefix.length() + suffix.length() +
- // sub0Limit - sub0Start;
- fFormatWidth = sub0Limit - sub0Start;
- fPad = padChar;
- } else {
- fFormatWidth = 0;
- }
- if (roundingPos >= 0) {
- roundingInc.fDecimalAt = effectiveDecimalPos - roundingPos;
- if (fRoundingIncrement != NULL) {
- *fRoundingIncrement = roundingInc;
- } else {
- fRoundingIncrement = new DigitList(roundingInc);
- /* test for NULL */
- if (fRoundingIncrement == 0) {
- status = U_MEMORY_ALLOCATION_ERROR;
- delete fPosPrefixPattern;
- delete fPosSuffixPattern;
- return;
- }
- }
- fRoundingDouble = fRoundingIncrement->getDouble();
- fRoundingMode = kRoundHalfEven;
- } else {
- setRoundingIncrement(0.0);
- }
- } else {
- fNegPrefixPattern = new UnicodeString(prefix);
- /* test for NULL */
- if (fNegPrefixPattern == 0) {
- status = U_MEMORY_ALLOCATION_ERROR;
- return;
- }
- fNegSuffixPattern = new UnicodeString(suffix);
- /* test for NULL */
- if (fNegSuffixPattern == 0) {
- delete fNegPrefixPattern;
- status = U_MEMORY_ALLOCATION_ERROR;
- return;