1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-11-22 20:25:35 +00:00
|
|
|
*******************************************************************************
|
2001-03-21 20:31:13 +00:00
|
|
|
* Copyright (C) 1997-2001, International Business Machines Corporation and *
|
1999-11-22 20:25:35 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File DECIMFMT.CPP
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 02/19/97 aliu Converted from java.
|
|
|
|
* 03/20/97 clhuang Implemented with new APIs.
|
|
|
|
* 03/31/97 aliu Moved isLONG_MIN to DigitList, and fixed it.
|
|
|
|
* 04/3/97 aliu Rewrote parsing and formatting completely, and
|
|
|
|
* cleaned up and debugged. Actually works now.
|
|
|
|
* Implemented NAN and INF handling, for both parsing
|
|
|
|
* and formatting. Extensive testing & debugging.
|
|
|
|
* 04/10/97 aliu Modified to compile on AIX.
|
|
|
|
* 04/16/97 aliu Rewrote to use DigitList, which has been resurrected.
|
|
|
|
* Changed DigitCount to int per code review.
|
|
|
|
* 07/09/97 helena Made ParsePosition into a class.
|
|
|
|
* 08/26/97 aliu Extensive changes to applyPattern; completely
|
|
|
|
* rewritten from the Java.
|
|
|
|
* 09/09/97 aliu Ported over support for exponential formats.
|
|
|
|
* 07/20/98 stephen JDK 1.2 sync up.
|
|
|
|
* Various instances of '0' replaced with 'NULL'
|
|
|
|
* Check for grouping size in subFormat()
|
|
|
|
* Brought subParse() in line with Java 1.2
|
|
|
|
* Added method appendAffix()
|
|
|
|
* 08/24/1998 srl Removed Mutex calls. This is not a thread safe class!
|
|
|
|
* 02/22/99 stephen Removed character literals for EBCDIC safety
|
|
|
|
* 06/24/99 helena Integrated Alan's NF enhancements and Java2 bug fixes
|
|
|
|
* 06/28/99 stephen Fixed bugs in toPattern().
|
|
|
|
* 06/29/99 stephen Fixed operator= to copy fFormatWidth, fPad,
|
|
|
|
* fPadPosition
|
|
|
|
********************************************************************************
|
|
|
|
*/
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/decimfmt.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "digitlst.h"
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/dcfmtsym.h"
|
|
|
|
#include "unicode/resbund.h"
|
|
|
|
#include "unicode/unicode.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include "cmemory.h"
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2001-09-17 21:50:19 +00:00
|
|
|
//#define FMT_DEBUG
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-09-17 21:50:19 +00:00
|
|
|
#ifdef FMT_DEBUG
|
1999-08-16 21:50:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
static void debugout(UnicodeString s) {
|
|
|
|
char buf[2000];
|
|
|
|
s.extract((UTextOffset) 0, s.length(), buf);
|
|
|
|
printf("%s", buf);
|
|
|
|
}
|
|
|
|
#define debug(x) printf("%s", x);
|
|
|
|
#else
|
|
|
|
#define debugout(x)
|
|
|
|
#define debug(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// *****************************************************************************
|
|
|
|
// class DecimalFormat
|
|
|
|
// *****************************************************************************
|
|
|
|
|
2001-09-17 21:50:19 +00:00
|
|
|
const char DecimalFormat::fgClassID = 0; // Value is irrelevant
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Constants for characters used in programmatic (unlocalized) patterns.
|
|
|
|
const UChar DecimalFormat::kPatternZeroDigit = 0x0030 /*'0'*/;
|
|
|
|
const UChar DecimalFormat::kPatternGroupingSeparator = 0x002C /*','*/;
|
|
|
|
const UChar DecimalFormat::kPatternDecimalSeparator = 0x002E /*'.'*/;
|
|
|
|
const UChar DecimalFormat::kPatternPerMill = 0x2030;
|
|
|
|
const UChar DecimalFormat::kPatternPercent = 0x0025 /*'%'*/;
|
|
|
|
const UChar DecimalFormat::kPatternDigit = 0x0023 /*'#'*/;
|
|
|
|
const UChar DecimalFormat::kPatternSeparator = 0x003B /*';'*/;
|
|
|
|
const UChar DecimalFormat::kPatternExponent = 0x0045 /*'E'*/;
|
|
|
|
const UChar DecimalFormat::kPatternPlus = 0x002B /*'+'*/;
|
|
|
|
const UChar DecimalFormat::kPatternMinus = 0x002D /*'-'*/;
|
|
|
|
const UChar DecimalFormat::kPatternPadEscape = 0x002A /*'*'*/;
|
|
|
|
const UChar DecimalFormat::kCurrencySign = 0x00A4;
|
|
|
|
const UChar DecimalFormat::kQuote = 0x0027 /*'\''*/;
|
|
|
|
|
2000-10-24 16:09:39 +00:00
|
|
|
//const int8_t DecimalFormat::fgMaxDigit = 9;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
const int32_t DecimalFormat::kDoubleIntegerDigits = 309;
|
|
|
|
const int32_t DecimalFormat::kDoubleFractionDigits = 340;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* These are the tags we expect to see in normal resource bundle files associated
|
|
|
|
* with a locale.
|
|
|
|
*/
|
2001-09-28 19:13:03 +00:00
|
|
|
const char DecimalFormat::fgNumberPatterns[]="NumberPatterns";
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Constructs a DecimalFormat instance in the default locale.
|
|
|
|
|
|
|
|
DecimalFormat::DecimalFormat(UErrorCode& status)
|
|
|
|
: NumberFormat(),
|
|
|
|
fPosPrefixPattern(0),
|
|
|
|
fPosSuffixPattern(0),
|
2000-08-14 23:23:20 +00:00
|
|
|
fNegPrefixPattern(0),
|
1999-08-16 21:50:52 +00:00
|
|
|
fNegSuffixPattern(0),
|
|
|
|
fSymbols(0)
|
|
|
|
{
|
2001-09-28 21:41:48 +00:00
|
|
|
UParseError parseError;
|
|
|
|
construct(status, parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Constructs a DecimalFormat instance with the specified number format
|
|
|
|
// pattern in the default locale.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
|
|
|
|
UErrorCode& status)
|
|
|
|
: NumberFormat(),
|
|
|
|
fPosPrefixPattern(0),
|
|
|
|
fPosSuffixPattern(0),
|
2000-08-14 23:23:20 +00:00
|
|
|
fNegPrefixPattern(0),
|
1999-08-16 21:50:52 +00:00
|
|
|
fNegSuffixPattern(0),
|
|
|
|
fSymbols(0)
|
|
|
|
{
|
2001-09-28 21:41:48 +00:00
|
|
|
UParseError parseError;
|
|
|
|
construct(status, parseError, &pattern);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Constructs a DecimalFormat instance with the specified number format
|
|
|
|
// pattern and the number format symbols in the default locale. The
|
|
|
|
// created instance owns the symbols.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
|
|
|
|
DecimalFormatSymbols* symbolsToAdopt,
|
|
|
|
UErrorCode& status)
|
|
|
|
: NumberFormat(),
|
|
|
|
fPosPrefixPattern(0),
|
|
|
|
fPosSuffixPattern(0),
|
2000-08-14 23:23:20 +00:00
|
|
|
fNegPrefixPattern(0),
|
1999-08-16 21:50:52 +00:00
|
|
|
fNegSuffixPattern(0),
|
|
|
|
fSymbols(0)
|
|
|
|
{
|
2001-09-28 21:41:48 +00:00
|
|
|
UParseError parseError;
|
2000-10-12 19:04:06 +00:00
|
|
|
if (symbolsToAdopt == NULL)
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2001-09-28 21:41:48 +00:00
|
|
|
construct(status, parseError, &pattern, symbolsToAdopt);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
DecimalFormat::DecimalFormat( const UnicodeString& pattern,
|
|
|
|
DecimalFormatSymbols* symbolsToAdopt,
|
|
|
|
UParseError& parseErr,
|
|
|
|
UErrorCode& status)
|
|
|
|
: NumberFormat(),
|
|
|
|
fPosPrefixPattern(0),
|
|
|
|
fPosSuffixPattern(0),
|
|
|
|
fNegPrefixPattern(0),
|
|
|
|
fNegSuffixPattern(0),
|
|
|
|
fSymbols(0)
|
|
|
|
{
|
|
|
|
if (symbolsToAdopt == NULL)
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
construct(status,parseErr, &pattern, symbolsToAdopt);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Constructs a DecimalFormat instance with the specified number format
|
|
|
|
// pattern and the number format symbols in the default locale. The
|
|
|
|
// created instance owns the clone of the symbols.
|
|
|
|
|
|
|
|
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
|
|
|
|
const DecimalFormatSymbols& symbols,
|
|
|
|
UErrorCode& status)
|
|
|
|
: NumberFormat(),
|
|
|
|
fPosPrefixPattern(0),
|
|
|
|
fPosSuffixPattern(0),
|
2000-08-14 23:23:20 +00:00
|
|
|
fNegPrefixPattern(0),
|
1999-08-16 21:50:52 +00:00
|
|
|
fNegSuffixPattern(0),
|
|
|
|
fSymbols(0)
|
|
|
|
{
|
2001-09-28 21:41:48 +00:00
|
|
|
UParseError parseError;
|
|
|
|
construct(status, parseError, &pattern, new DecimalFormatSymbols(symbols));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Constructs a DecimalFormat instance with the specified number format
|
|
|
|
// pattern and the number format symbols in the desired locale. The
|
|
|
|
// created instance owns the symbols.
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::construct(UErrorCode& status,
|
2001-08-16 00:55:16 +00:00
|
|
|
UParseError& parseErr,
|
1999-08-16 21:50:52 +00:00
|
|
|
const UnicodeString* pattern,
|
|
|
|
DecimalFormatSymbols* symbolsToAdopt,
|
|
|
|
const Locale& locale)
|
|
|
|
{
|
|
|
|
fSymbols = symbolsToAdopt; // Do this BEFORE aborting on status failure!!!
|
2000-11-02 02:42:40 +00:00
|
|
|
// fDigitList = new DigitList(); // Do this BEFORE aborting on status failure!!!
|
1999-08-16 21:50:52 +00:00
|
|
|
fRoundingIncrement = NULL;
|
|
|
|
fRoundingDouble = 0.0;
|
|
|
|
fRoundingMode = kRoundHalfEven;
|
1999-10-27 01:08:39 +00:00
|
|
|
fPad = kPatternPadEscape;
|
|
|
|
fPadPosition = kPadBeforePrefix;
|
2000-10-12 19:04:06 +00:00
|
|
|
if (U_FAILURE(status))
|
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
fPosPrefixPattern = fPosSuffixPattern = NULL;
|
|
|
|
fNegPrefixPattern = fNegSuffixPattern = NULL;
|
|
|
|
fMultiplier = 1;
|
|
|
|
fGroupingSize = 3;
|
2000-06-07 00:15:59 +00:00
|
|
|
fGroupingSize2 = 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
fDecimalSeparatorAlwaysShown = FALSE;
|
|
|
|
fIsCurrencyFormat = FALSE;
|
|
|
|
fUseExponentialNotation = FALSE;
|
|
|
|
fMinExponentDigits = 0;
|
|
|
|
|
2000-10-12 19:04:06 +00:00
|
|
|
if (fSymbols == NULL)
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
2000-10-12 19:04:06 +00:00
|
|
|
fSymbols = new DecimalFormatSymbols(locale, status);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
UnicodeString str;
|
|
|
|
// Uses the default locale's number format pattern if there isn't
|
|
|
|
// one specified.
|
|
|
|
if (pattern == NULL)
|
|
|
|
{
|
2000-06-30 20:31:39 +00:00
|
|
|
ResourceBundle resource((char *)0, Locale::getDefault(), status);
|
2000-05-15 18:39:17 +00:00
|
|
|
|
|
|
|
str = resource.get(fgNumberPatterns, status).getStringEx((int32_t)0, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
pattern = &str;
|
|
|
|
}
|
|
|
|
|
2000-10-12 19:04:06 +00:00
|
|
|
if (U_FAILURE(status))
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
2000-10-12 19:04:06 +00:00
|
|
|
return;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
applyPattern(*pattern, FALSE /*not localized*/,parseErr, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
DecimalFormat::~DecimalFormat()
|
|
|
|
{
|
2000-11-02 02:42:40 +00:00
|
|
|
// delete fDigitList;
|
1999-08-16 21:50:52 +00:00
|
|
|
delete fPosPrefixPattern;
|
|
|
|
delete fPosSuffixPattern;
|
|
|
|
delete fNegPrefixPattern;
|
|
|
|
delete fNegSuffixPattern;
|
2000-08-24 20:56:13 +00:00
|
|
|
delete fSymbols;
|
1999-08-16 21:50:52 +00:00
|
|
|
delete fRoundingIncrement;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// copy constructor
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
DecimalFormat::DecimalFormat(const DecimalFormat &source)
|
|
|
|
: NumberFormat(source),
|
2000-11-02 02:42:40 +00:00
|
|
|
// fDigitList(NULL),
|
1999-08-16 21:50:52 +00:00
|
|
|
fPosPrefixPattern(NULL),
|
|
|
|
fPosSuffixPattern(NULL),
|
|
|
|
fNegPrefixPattern(NULL),
|
|
|
|
fNegSuffixPattern(NULL),
|
2000-08-24 20:56:13 +00:00
|
|
|
fSymbols(NULL),
|
1999-08-16 21:50:52 +00:00
|
|
|
fRoundingIncrement(NULL)
|
|
|
|
{
|
|
|
|
*this = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// assignment operator
|
|
|
|
// Note that fDigitList is not considered a significant part of the
|
|
|
|
// DecimalFormat because it's used as a buffer to process the numbers.
|
|
|
|
|
|
|
|
static void _copy_us_ptr(UnicodeString** pdest, const UnicodeString* source) {
|
|
|
|
if (source == NULL) {
|
|
|
|
delete *pdest;
|
|
|
|
*pdest = NULL;
|
|
|
|
} else if (*pdest == NULL) {
|
|
|
|
*pdest = new UnicodeString(*source);
|
|
|
|
} else {
|
|
|
|
**pdest = *source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DecimalFormat&
|
|
|
|
DecimalFormat::operator=(const DecimalFormat& rhs)
|
|
|
|
{
|
|
|
|
if(this != &rhs) {
|
|
|
|
NumberFormat::operator=(rhs);
|
|
|
|
fPositivePrefix = rhs.fPositivePrefix;
|
|
|
|
fPositiveSuffix = rhs.fPositiveSuffix;
|
|
|
|
fNegativePrefix = rhs.fNegativePrefix;
|
|
|
|
fNegativeSuffix = rhs.fNegativeSuffix;
|
|
|
|
_copy_us_ptr(&fPosPrefixPattern, rhs.fPosPrefixPattern);
|
|
|
|
_copy_us_ptr(&fPosSuffixPattern, rhs.fPosSuffixPattern);
|
|
|
|
_copy_us_ptr(&fNegPrefixPattern, rhs.fNegPrefixPattern);
|
|
|
|
_copy_us_ptr(&fNegSuffixPattern, rhs.fNegSuffixPattern);
|
|
|
|
if(rhs.fRoundingIncrement == NULL) {
|
|
|
|
delete fRoundingIncrement;
|
|
|
|
fRoundingIncrement = NULL;
|
|
|
|
}
|
|
|
|
else if(fRoundingIncrement == NULL) {
|
|
|
|
fRoundingIncrement = new DigitList(*rhs.fRoundingIncrement);
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
else {
|
|
|
|
*fRoundingIncrement = *rhs.fRoundingIncrement;
|
|
|
|
}
|
|
|
|
fRoundingDouble = rhs.fRoundingDouble;
|
|
|
|
fMultiplier = rhs.fMultiplier;
|
|
|
|
fGroupingSize = rhs.fGroupingSize;
|
2000-06-07 00:15:59 +00:00
|
|
|
fGroupingSize2 = rhs.fGroupingSize2;
|
1999-08-16 21:50:52 +00:00
|
|
|
fDecimalSeparatorAlwaysShown = rhs.fDecimalSeparatorAlwaysShown;
|
|
|
|
if(fSymbols == NULL)
|
2000-10-12 19:04:06 +00:00
|
|
|
fSymbols = new DecimalFormatSymbols(*rhs.fSymbols);
|
1999-08-16 21:50:52 +00:00
|
|
|
else
|
2000-10-12 19:04:06 +00:00
|
|
|
*fSymbols = *rhs.fSymbols;
|
1999-08-16 21:50:52 +00:00
|
|
|
fUseExponentialNotation = rhs.fUseExponentialNotation;
|
|
|
|
/*Bertrand A. D. Update 98.03.17*/
|
|
|
|
fIsCurrencyFormat = rhs.fIsCurrencyFormat;
|
|
|
|
/*end of Update*/
|
|
|
|
fMinExponentDigits = rhs.fMinExponentDigits;
|
2000-11-02 02:42:40 +00:00
|
|
|
// if (fDigitList == NULL)
|
|
|
|
// fDigitList = new DigitList();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* sfb 990629 */
|
|
|
|
fFormatWidth = rhs.fFormatWidth;
|
|
|
|
fPad = rhs.fPad;
|
|
|
|
fPadPosition = rhs.fPadPosition;
|
|
|
|
/* end sfb */
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
DecimalFormat::operator==(const Format& that) const
|
|
|
|
{
|
2000-08-07 23:06:28 +00:00
|
|
|
if (this == &that)
|
|
|
|
return TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-08-07 23:06:28 +00:00
|
|
|
if (getDynamicClassID() != that.getDynamicClassID())
|
|
|
|
return FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
const DecimalFormat* other = (DecimalFormat*)&that;
|
|
|
|
|
2001-09-17 21:50:19 +00:00
|
|
|
#ifdef FMT_DEBUG
|
1999-08-16 21:50:52 +00:00
|
|
|
// This code makes it easy to determine why two format objects that should
|
|
|
|
// be equal aren't.
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool first = TRUE;
|
1999-08-16 21:50:52 +00:00
|
|
|
if (!NumberFormat::operator==(that)) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
debug("NumberFormat::!=");
|
|
|
|
}
|
|
|
|
if (!((fPosPrefixPattern == other->fPosPrefixPattern && // both null
|
|
|
|
fPositivePrefix == other->fPositivePrefix)
|
|
|
|
|| (fPosPrefixPattern != 0 && other->fPosPrefixPattern != 0 &&
|
|
|
|
*fPosPrefixPattern == *other->fPosPrefixPattern))) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
debug("Pos Prefix !=");
|
|
|
|
}
|
|
|
|
if (!((fPosSuffixPattern == other->fPosSuffixPattern && // both null
|
|
|
|
fPositiveSuffix == other->fPositiveSuffix)
|
|
|
|
|| (fPosSuffixPattern != 0 && other->fPosSuffixPattern != 0 &&
|
|
|
|
*fPosSuffixPattern == *other->fPosSuffixPattern))) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
debug("Pos Suffix !=");
|
|
|
|
}
|
|
|
|
if (!((fNegPrefixPattern == other->fNegPrefixPattern && // both null
|
|
|
|
fNegativePrefix == other->fNegativePrefix)
|
|
|
|
|| (fNegPrefixPattern != 0 && other->fNegPrefixPattern != 0 &&
|
|
|
|
*fNegPrefixPattern == *other->fNegPrefixPattern))) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
debug("Neg Prefix ");
|
|
|
|
if (fNegPrefixPattern == NULL) {
|
|
|
|
debug("NULL(");
|
|
|
|
debugout(fNegativePrefix);
|
|
|
|
debug(")");
|
|
|
|
} else {
|
|
|
|
debugout(*fNegPrefixPattern);
|
|
|
|
}
|
|
|
|
debug(" != ");
|
|
|
|
if (other->fNegPrefixPattern == NULL) {
|
|
|
|
debug("NULL(");
|
|
|
|
debugout(other->fNegativePrefix);
|
|
|
|
debug(")");
|
|
|
|
} else {
|
|
|
|
debugout(*other->fNegPrefixPattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!((fNegSuffixPattern == other->fNegSuffixPattern && // both null
|
|
|
|
fNegativeSuffix == other->fNegativeSuffix)
|
|
|
|
|| (fNegSuffixPattern != 0 && other->fNegSuffixPattern != 0 &&
|
|
|
|
*fNegSuffixPattern == *other->fNegSuffixPattern))) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
debug("Neg Suffix ");
|
|
|
|
if (fNegSuffixPattern == NULL) {
|
|
|
|
debug("NULL(");
|
|
|
|
debugout(fNegativeSuffix);
|
|
|
|
debug(")");
|
|
|
|
} else {
|
|
|
|
debugout(*fNegSuffixPattern);
|
|
|
|
}
|
|
|
|
debug(" != ");
|
|
|
|
if (other->fNegSuffixPattern == NULL) {
|
|
|
|
debug("NULL(");
|
|
|
|
debugout(other->fNegativeSuffix);
|
|
|
|
debug(")");
|
|
|
|
} else {
|
|
|
|
debugout(*other->fNegSuffixPattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!((fRoundingIncrement == other->fRoundingIncrement) // both null
|
|
|
|
|| (fRoundingIncrement != NULL &&
|
|
|
|
other->fRoundingIncrement != NULL &&
|
|
|
|
*fRoundingIncrement == *other->fRoundingIncrement))) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
debug("Rounding Increment !=");
|
|
|
|
}
|
|
|
|
if (fMultiplier != other->fMultiplier) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; }
|
|
|
|
printf("Multiplier %ld != %ld", fMultiplier, other->fMultiplier);
|
|
|
|
}
|
|
|
|
if (fGroupingSize != other->fGroupingSize) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
printf("Grouping Size %ld != %ld", fGroupingSize, other->fGroupingSize);
|
|
|
|
}
|
2000-06-07 00:15:59 +00:00
|
|
|
if (fGroupingSize2 != other->fGroupingSize2) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
printf("Secondary Grouping Size %ld != %ld", fGroupingSize2, other->fGroupingSize2);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
if (fDecimalSeparatorAlwaysShown != other->fDecimalSeparatorAlwaysShown) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
printf("Dec Sep Always %d != %d", fDecimalSeparatorAlwaysShown, other->fDecimalSeparatorAlwaysShown);
|
|
|
|
}
|
|
|
|
if (fUseExponentialNotation != other->fUseExponentialNotation) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
debug("Use Exp !=");
|
|
|
|
}
|
|
|
|
if (!(!fUseExponentialNotation ||
|
|
|
|
fMinExponentDigits != other->fMinExponentDigits)) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
debug("Exp Digits !=");
|
|
|
|
}
|
|
|
|
if (*fSymbols != *(other->fSymbols)) {
|
|
|
|
if (first) { printf("[ "); first = FALSE; } else { printf(", "); }
|
|
|
|
debug("Symbols !=");
|
|
|
|
}
|
|
|
|
if (!first) { printf(" ]"); }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (NumberFormat::operator==(that) &&
|
|
|
|
((fPosPrefixPattern == other->fPosPrefixPattern && // both null
|
|
|
|
fPositivePrefix == other->fPositivePrefix)
|
|
|
|
|| (fPosPrefixPattern != 0 && other->fPosPrefixPattern != 0 &&
|
|
|
|
*fPosPrefixPattern == *other->fPosPrefixPattern)) &&
|
|
|
|
((fPosSuffixPattern == other->fPosSuffixPattern && // both null
|
|
|
|
fPositiveSuffix == other->fPositiveSuffix)
|
|
|
|
|| (fPosSuffixPattern != 0 && other->fPosSuffixPattern != 0 &&
|
|
|
|
*fPosSuffixPattern == *other->fPosSuffixPattern)) &&
|
|
|
|
((fNegPrefixPattern == other->fNegPrefixPattern && // both null
|
|
|
|
fNegativePrefix == other->fNegativePrefix)
|
|
|
|
|| (fNegPrefixPattern != 0 && other->fNegPrefixPattern != 0 &&
|
|
|
|
*fNegPrefixPattern == *other->fNegPrefixPattern)) &&
|
|
|
|
((fNegSuffixPattern == other->fNegSuffixPattern && // both null
|
|
|
|
fNegativeSuffix == other->fNegativeSuffix)
|
|
|
|
|| (fNegSuffixPattern != 0 && other->fNegSuffixPattern != 0 &&
|
|
|
|
*fNegSuffixPattern == *other->fNegSuffixPattern)) &&
|
|
|
|
((fRoundingIncrement == other->fRoundingIncrement) // both null
|
|
|
|
|| (fRoundingIncrement != NULL &&
|
|
|
|
other->fRoundingIncrement != NULL &&
|
|
|
|
*fRoundingIncrement == *other->fRoundingIncrement)) &&
|
|
|
|
fMultiplier == other->fMultiplier &&
|
|
|
|
fGroupingSize == other->fGroupingSize &&
|
2000-06-07 00:15:59 +00:00
|
|
|
fGroupingSize2 == other->fGroupingSize2 &&
|
1999-08-16 21:50:52 +00:00
|
|
|
fDecimalSeparatorAlwaysShown == other->fDecimalSeparatorAlwaysShown &&
|
|
|
|
fUseExponentialNotation == other->fUseExponentialNotation &&
|
|
|
|
(!fUseExponentialNotation ||
|
|
|
|
fMinExponentDigits == other->fMinExponentDigits) &&
|
|
|
|
*fSymbols == *(other->fSymbols));
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Format*
|
|
|
|
DecimalFormat::clone() const
|
|
|
|
{
|
|
|
|
return new DecimalFormat(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(int32_t number,
|
|
|
|
UnicodeString& result,
|
|
|
|
FieldPosition& fieldPosition) const
|
|
|
|
{
|
2000-11-02 02:42:40 +00:00
|
|
|
DigitList digits;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// Clears field positions.
|
|
|
|
fieldPosition.setBeginIndex(0);
|
|
|
|
fieldPosition.setEndIndex(0);
|
|
|
|
|
|
|
|
// If we are to do rounding, we need to move into the BigDecimal
|
|
|
|
// domain in order to do divide/multiply correctly.
|
2000-11-02 02:42:40 +00:00
|
|
|
// ||
|
1999-08-16 21:50:52 +00:00
|
|
|
// In general, long values always represent real finite numbers, so
|
|
|
|
// we don't have to check for +/- Infinity or NaN. However, there
|
|
|
|
// is one case we have to be careful of: The multiplier can push
|
|
|
|
// a number near MIN_VALUE or MAX_VALUE outside the legal range. We
|
|
|
|
// check for this before multiplying, and if it happens we use doubles
|
|
|
|
// instead, trading off accuracy for range.
|
2000-11-02 02:42:40 +00:00
|
|
|
if (fRoundingIncrement != NULL
|
2000-12-12 21:57:44 +00:00
|
|
|
|| (fMultiplier != 0 && (number > (INT32_MAX / fMultiplier)
|
|
|
|
|| number < (INT32_MIN / fMultiplier))))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-11-02 02:42:40 +00:00
|
|
|
digits.set(((double)number) * fMultiplier,
|
|
|
|
fUseExponentialNotation ?
|
2000-11-07 18:57:11 +00:00
|
|
|
getMinimumIntegerDigits() + getMaximumFractionDigits() : 0,
|
2000-11-02 02:42:40 +00:00
|
|
|
!fUseExponentialNotation);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
digits.set(number * fMultiplier,
|
|
|
|
fUseExponentialNotation ?
|
|
|
|
getMinimumIntegerDigits() + getMaximumFractionDigits() : 0);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-11-02 02:42:40 +00:00
|
|
|
return subformat(result, fieldPosition, digits, TRUE);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format( double number,
|
|
|
|
UnicodeString& result,
|
|
|
|
FieldPosition& fieldPosition) const
|
|
|
|
{
|
|
|
|
// Clears field positions.
|
|
|
|
fieldPosition.setBeginIndex(0);
|
|
|
|
fieldPosition.setEndIndex(0);
|
|
|
|
|
|
|
|
// Special case for NaN, sets the begin and end index to be the
|
|
|
|
// the string length of localized name of NaN.
|
1999-12-28 23:57:50 +00:00
|
|
|
if (uprv_isNaN(number))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kIntegerField)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setBeginIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-09-17 21:50:19 +00:00
|
|
|
result += fSymbols->getSymbol(DecimalFormatSymbols::kNaNSymbol);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kIntegerField)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setEndIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-03-08 19:40:09 +00:00
|
|
|
addPadding(result, fieldPosition, FALSE, FALSE /*ignored*/);
|
1999-08-16 21:50:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Detecting whether a double is negative is easy with the exception of
|
|
|
|
* the value -0.0. This is a double which has a zero mantissa (and
|
|
|
|
* exponent), but a negative sign bit. It is semantically distinct from
|
|
|
|
* a zero with a positive sign bit, and this distinction is important
|
|
|
|
* to certain kinds of computations. However, it's a little tricky to
|
|
|
|
* detect, since (-0.0 == 0.0) and !(-0.0 < 0.0). How then, you may
|
|
|
|
* ask, does it behave distinctly from +0.0? Well, 1/(-0.0) ==
|
|
|
|
* -Infinity. Proper detection of -0.0 is needed to deal with the
|
|
|
|
* issues raised by bugs 4106658, 4106667, and 4147706. Liu 7/6/98.
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool isNegative = uprv_isNegative(number);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Do this BEFORE checking to see if value is infinite! Sets the
|
|
|
|
// begin and end index to be length of the string composed of
|
|
|
|
// localized name of Infinite and the positive/negative localized
|
|
|
|
// signs.
|
|
|
|
|
2000-11-02 02:42:40 +00:00
|
|
|
number *= fMultiplier;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Apply rounding after multiplier
|
|
|
|
if (fRoundingIncrement != NULL) {
|
2000-11-02 02:42:40 +00:00
|
|
|
if (isNegative) // For rounding in the correct direction
|
|
|
|
number = -number;
|
1999-08-16 21:50:52 +00:00
|
|
|
number = fRoundingDouble
|
|
|
|
* round(number / fRoundingDouble, fRoundingMode, isNegative);
|
2000-11-02 02:42:40 +00:00
|
|
|
if (isNegative)
|
|
|
|
number = -number;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special case for INFINITE,
|
1999-12-28 23:57:50 +00:00
|
|
|
if (uprv_isInfinite(number))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
result += (isNegative ? fNegativePrefix : fPositivePrefix);
|
|
|
|
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kIntegerField)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setBeginIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-09-17 21:50:19 +00:00
|
|
|
result += fSymbols->getSymbol(DecimalFormatSymbols::kInfinitySymbol);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kIntegerField)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setEndIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
result += (isNegative ? fNegativeSuffix : fPositiveSuffix);
|
|
|
|
|
2001-03-08 19:40:09 +00:00
|
|
|
addPadding(result, fieldPosition, TRUE, isNegative);
|
1999-08-16 21:50:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-11-02 02:42:40 +00:00
|
|
|
DigitList digits;
|
|
|
|
|
|
|
|
// This detects negativity too.
|
|
|
|
digits.set(number, fUseExponentialNotation ?
|
1999-08-16 21:50:52 +00:00
|
|
|
getMinimumIntegerDigits() + getMaximumFractionDigits() :
|
|
|
|
getMaximumFractionDigits(),
|
|
|
|
!fUseExponentialNotation);
|
|
|
|
|
2000-11-02 02:42:40 +00:00
|
|
|
return subformat(result, fieldPosition, digits, FALSE);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Round a double value to the nearest integer according to the
|
|
|
|
* given mode.
|
|
|
|
* @param a the absolute value of the number to be rounded
|
|
|
|
* @param mode a BigDecimal rounding mode
|
|
|
|
* @param isNegative true if the number to be rounded is negative
|
|
|
|
* @return the absolute value of the rounded result
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
double DecimalFormat::round(double a, ERoundingMode mode, UBool isNegative) {
|
1999-08-16 21:50:52 +00:00
|
|
|
switch (mode) {
|
|
|
|
case kRoundCeiling:
|
1999-12-28 23:57:50 +00:00
|
|
|
return isNegative ? uprv_floor(a) : uprv_ceil(a);
|
1999-08-16 21:50:52 +00:00
|
|
|
case kRoundFloor:
|
1999-12-28 23:57:50 +00:00
|
|
|
return isNegative ? uprv_ceil(a) : uprv_floor(a);
|
1999-08-16 21:50:52 +00:00
|
|
|
case kRoundDown:
|
1999-12-28 23:57:50 +00:00
|
|
|
return uprv_floor(a);
|
1999-08-16 21:50:52 +00:00
|
|
|
case kRoundUp:
|
1999-12-28 23:57:50 +00:00
|
|
|
return uprv_ceil(a);
|
1999-08-16 21:50:52 +00:00
|
|
|
case kRoundHalfEven:
|
|
|
|
{
|
1999-12-28 23:57:50 +00:00
|
|
|
double f = uprv_floor(a);
|
1999-08-16 21:50:52 +00:00
|
|
|
if ((a - f) != 0.5) {
|
1999-12-28 23:57:50 +00:00
|
|
|
return uprv_floor(a + 0.5);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
double g = f / 2.0;
|
1999-12-28 23:57:50 +00:00
|
|
|
return (g == uprv_floor(g)) ? f : (f + 1.0);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
case kRoundHalfDown:
|
1999-12-28 23:57:50 +00:00
|
|
|
return ((a - uprv_floor(a)) <= 0.5) ? uprv_floor(a) : uprv_ceil(a);
|
1999-08-16 21:50:52 +00:00
|
|
|
case kRoundHalfUp:
|
1999-12-28 23:57:50 +00:00
|
|
|
return ((a - uprv_floor(a)) < 0.5) ? uprv_floor(a) : uprv_ceil(a);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format( const Formattable& obj,
|
|
|
|
UnicodeString& result,
|
|
|
|
FieldPosition& fieldPosition,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
|
|
|
return NumberFormat::format(obj, result, fieldPosition, status);
|
|
|
|
}
|
|
|
|
|
2000-06-07 00:15:59 +00:00
|
|
|
/**
|
|
|
|
* Return true if a grouping separator belongs at the given
|
|
|
|
* position, based on whether grouping is in use and the values of
|
|
|
|
* the primary and secondary grouping interval.
|
|
|
|
* @param pos the number of integer digits to the right of
|
|
|
|
* the current position. Zero indicates the position after the
|
|
|
|
* rightmost integer digit.
|
|
|
|
* @return true if a grouping character belongs at the current
|
|
|
|
* position.
|
|
|
|
*/
|
|
|
|
UBool DecimalFormat::isGroupingPosition(int32_t pos) const {
|
|
|
|
UBool result = FALSE;
|
|
|
|
if (isGroupingUsed() && (pos > 0) && (fGroupingSize > 0)) {
|
|
|
|
if ((fGroupingSize2 > 0) && (pos > fGroupingSize)) {
|
|
|
|
result = ((pos - fGroupingSize) % fGroupingSize2) == 0;
|
|
|
|
} else {
|
|
|
|
result = pos % fGroupingSize == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Complete the formatting of a finite number. On entry, the fDigitList must
|
|
|
|
* be filled in with the correct digits.
|
|
|
|
*/
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::subformat(UnicodeString& result,
|
|
|
|
FieldPosition& fieldPosition,
|
2000-11-02 02:42:40 +00:00
|
|
|
DigitList& digits,
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool isInteger) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// Gets the localized zero Unicode character.
|
2001-09-17 21:50:19 +00:00
|
|
|
UChar32 zero = fSymbols->getSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t zeroDelta = zero - '0'; // '0' is the DigitList representation of zero
|
2001-09-17 21:50:19 +00:00
|
|
|
UnicodeString grouping(fSymbols->getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
|
|
|
|
UnicodeString decimal(fIsCurrencyFormat ?
|
|
|
|
fSymbols->getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol) :
|
|
|
|
fSymbols->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t maxIntDig = getMaximumIntegerDigits();
|
|
|
|
int32_t minIntDig = getMinimumIntegerDigits();
|
|
|
|
|
|
|
|
/* Per bug 4147706, DecimalFormat must respect the sign of numbers which
|
|
|
|
* format as zero. This allows sensible computations and preserves
|
|
|
|
* relations such as signum(1/x) = signum(x), where x is +Infinity or
|
|
|
|
* -Infinity. Prior to this fix, we always formatted zero values as if
|
|
|
|
* they were positive. Liu 7/6/98.
|
|
|
|
*/
|
2000-11-02 02:42:40 +00:00
|
|
|
if (digits.isZero())
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-11-02 02:42:40 +00:00
|
|
|
digits.fDecimalAt = digits.fCount = 0; // Normalize
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Appends the prefix.
|
2000-11-02 02:42:40 +00:00
|
|
|
result += (digits.fIsPositive ? fPositivePrefix : fNegativePrefix);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
if (fUseExponentialNotation)
|
|
|
|
{
|
|
|
|
// Record field information for caller.
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kIntegerField)
|
|
|
|
{
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setBeginIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
fieldPosition.setEndIndex(-1);
|
|
|
|
}
|
|
|
|
else if (fieldPosition.getField() == NumberFormat::kFractionField)
|
|
|
|
{
|
|
|
|
fieldPosition.setBeginIndex(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minimum integer digits are handled in exponential format by
|
|
|
|
// adjusting the exponent. For example, 0.01234 with 3 minimum
|
|
|
|
// integer digits is "123.4E-4".
|
|
|
|
|
|
|
|
// Maximum integer digits are interpreted as indicating the
|
|
|
|
// repeating range. This is useful for engineering notation, in
|
|
|
|
// which the exponent is restricted to a multiple of 3. For
|
|
|
|
// example, 0.01234 with 3 maximum integer digits is "12.34e-3".
|
|
|
|
// If maximum integer digits are defined and are larger than
|
|
|
|
// minimum integer digits, then minimum integer digits are
|
|
|
|
// ignored.
|
2000-11-02 02:42:40 +00:00
|
|
|
int32_t exponent = digits.fDecimalAt;
|
1999-08-16 21:50:52 +00:00
|
|
|
if (maxIntDig > 1 && maxIntDig != minIntDig) {
|
|
|
|
// A exponent increment is defined; adjust to it.
|
|
|
|
exponent = (exponent > 0) ? (exponent - 1) / maxIntDig
|
|
|
|
: (exponent / maxIntDig) - 1;
|
|
|
|
exponent *= maxIntDig;
|
|
|
|
} else {
|
|
|
|
// No exponent increment is defined; use minimum integer digits.
|
|
|
|
// If none is specified, as in "#E0", generate 1 integer digit.
|
|
|
|
exponent -= (minIntDig > 0 || getMinimumFractionDigits() > 0)
|
|
|
|
? minIntDig : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We now output a minimum number of digits, and more if there
|
|
|
|
// are more digits, up to the maximum number of digits. We
|
|
|
|
// place the decimal point after the "integer" digits, which
|
|
|
|
// are the first (decimalAt - exponent) digits.
|
|
|
|
int32_t minimumDigits = minIntDig + getMinimumFractionDigits();
|
|
|
|
// The number of integer digits is handled specially if the number
|
|
|
|
// is zero, since then there may be no digits.
|
2000-11-02 02:42:40 +00:00
|
|
|
int32_t integerDigits = digits.isZero() ? minIntDig :
|
|
|
|
digits.fDecimalAt - exponent;
|
|
|
|
int32_t totalDigits = digits.fCount;
|
2000-10-12 19:04:06 +00:00
|
|
|
if (minimumDigits > totalDigits)
|
|
|
|
totalDigits = minimumDigits;
|
|
|
|
if (integerDigits > totalDigits)
|
|
|
|
totalDigits = integerDigits;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// totalDigits records total number of digits needs to be processed
|
|
|
|
int32_t i;
|
|
|
|
for (i=0; i<totalDigits; ++i)
|
|
|
|
{
|
|
|
|
if (i == integerDigits)
|
|
|
|
{
|
|
|
|
// Record field information for caller.
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kIntegerField)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setEndIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
result += (decimal);
|
|
|
|
|
|
|
|
// Record field information for caller.
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kFractionField)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setBeginIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
// Restores the digit character or pads the buffer with zeros.
|
2001-09-20 21:34:19 +00:00
|
|
|
UChar32 c = (UChar32)((i < digits.fCount) ?
|
2001-09-17 21:50:19 +00:00
|
|
|
(digits.fDigits[i] + zeroDelta) :
|
1999-08-16 21:50:52 +00:00
|
|
|
zero);
|
|
|
|
result += c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record field information
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kIntegerField)
|
|
|
|
{
|
|
|
|
if (fieldPosition.getEndIndex() < 0)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setEndIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
else if (fieldPosition.getField() == NumberFormat::kFractionField)
|
|
|
|
{
|
|
|
|
if (fieldPosition.getBeginIndex() < 0)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setBeginIndex(result.length());
|
|
|
|
fieldPosition.setEndIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The exponent is output using the pattern-specified minimum
|
|
|
|
// exponent digits. There is no maximum limit to the exponent
|
|
|
|
// digits, since truncating the exponent would result in an
|
|
|
|
// unacceptable inaccuracy.
|
2001-09-17 21:50:19 +00:00
|
|
|
result += fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// For zero values, we force the exponent to zero. We
|
|
|
|
// must do this here, and not earlier, because the value
|
|
|
|
// is used to determine integer digit count above.
|
2000-11-02 02:42:40 +00:00
|
|
|
if (digits.isZero())
|
2000-10-12 19:04:06 +00:00
|
|
|
exponent = 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-11-02 02:42:40 +00:00
|
|
|
if (exponent < 0) {
|
2001-09-17 21:50:19 +00:00
|
|
|
result += fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol);
|
1999-08-16 21:50:52 +00:00
|
|
|
} else if (fExponentSignAlwaysShown) {
|
2001-09-17 21:50:19 +00:00
|
|
|
result += fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-11-02 02:42:40 +00:00
|
|
|
|
|
|
|
DigitList expDigits;
|
|
|
|
expDigits.set(exponent);
|
|
|
|
for (i=expDigits.fDecimalAt; i<fMinExponentDigits; ++i)
|
1999-08-16 21:50:52 +00:00
|
|
|
result += (zero);
|
2000-11-02 02:42:40 +00:00
|
|
|
for (i=0; i<expDigits.fDecimalAt; ++i)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-09-20 21:34:19 +00:00
|
|
|
UChar32 c = (UChar32)((i < expDigits.fCount) ?
|
2001-09-17 21:50:19 +00:00
|
|
|
(expDigits.fDigits[i] + zeroDelta) : zero);
|
1999-08-16 21:50:52 +00:00
|
|
|
result += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Not using exponential notation
|
|
|
|
{
|
|
|
|
// Record field information for caller.
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kIntegerField)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setBeginIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Output the integer portion. Here 'count' is the total
|
|
|
|
// number of integer digits we will display, including both
|
|
|
|
// leading zeros required to satisfy getMinimumIntegerDigits,
|
|
|
|
// and actual digits present in the number.
|
|
|
|
int32_t count = minIntDig;
|
2000-11-02 02:42:40 +00:00
|
|
|
int32_t digitIndex = 0; // Index into digits.fDigits[]
|
|
|
|
if (digits.fDecimalAt > 0 && count < digits.fDecimalAt)
|
|
|
|
count = digits.fDecimalAt;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Handle the case where getMaximumIntegerDigits() is smaller
|
|
|
|
// than the real number of integer digits. If this is so, we
|
|
|
|
// output the least significant max integer digits. For example,
|
|
|
|
// the value 1997 printed with 2 max integer digits is just "97".
|
|
|
|
|
|
|
|
if (count > maxIntDig)
|
|
|
|
{
|
|
|
|
count = maxIntDig;
|
2000-11-02 02:42:40 +00:00
|
|
|
digitIndex = digits.fDecimalAt - count;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
1999-12-08 02:11:04 +00:00
|
|
|
int32_t sizeBeforeIntegerPart = result.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
int32_t i;
|
|
|
|
for (i=count-1; i>=0; --i)
|
|
|
|
{
|
2000-11-02 02:42:40 +00:00
|
|
|
if (i < digits.fDecimalAt && digitIndex < digits.fCount)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// Output a real digit
|
2001-09-20 21:34:19 +00:00
|
|
|
result += ((UChar32)(digits.fDigits[digitIndex++] + zeroDelta));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Output a leading zero
|
|
|
|
result += (zero);
|
|
|
|
}
|
|
|
|
|
2000-06-07 00:15:59 +00:00
|
|
|
// Output grouping separator if necessary.
|
|
|
|
if (isGroupingPosition(i)) {
|
|
|
|
result.append(grouping);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record field information for caller.
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kIntegerField)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setEndIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Determine whether or not there are any printable fractional
|
|
|
|
// digits. If we've used up the digits we know there aren't.
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool fractionPresent = (getMinimumFractionDigits() > 0) ||
|
2000-11-02 02:42:40 +00:00
|
|
|
(!isInteger && digitIndex < digits.fCount);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// If there is no fraction present, and we haven't printed any
|
|
|
|
// integer digits, then print a zero. Otherwise we won't print
|
|
|
|
// _any_ digits, and we won't be able to parse this string.
|
1999-12-08 02:11:04 +00:00
|
|
|
if (!fractionPresent && result.length() == sizeBeforeIntegerPart)
|
1999-08-16 21:50:52 +00:00
|
|
|
result += (zero);
|
|
|
|
|
|
|
|
// Output the decimal separator if we always do so.
|
|
|
|
if (fDecimalSeparatorAlwaysShown || fractionPresent)
|
|
|
|
result += (decimal);
|
|
|
|
|
|
|
|
// Record field information for caller.
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kFractionField)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setBeginIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-11-02 02:42:40 +00:00
|
|
|
int32_t maxFracDigits = getMaximumFractionDigits();
|
|
|
|
int32_t negDecimalAt = -digits.fDecimalAt;
|
|
|
|
for (i=0; i < maxFracDigits; ++i)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-11-02 02:42:40 +00:00
|
|
|
if (!isInteger && digitIndex < digits.fCount)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2000-11-02 02:42:40 +00:00
|
|
|
if (i >= negDecimalAt)
|
|
|
|
{
|
|
|
|
// Output a digit
|
2001-09-20 21:34:19 +00:00
|
|
|
result += ((UChar32)(digits.fDigits[digitIndex++] + zeroDelta));
|
2000-11-02 02:42:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Output leading fractional zeros. These are zeros that come after
|
|
|
|
// the decimal but before any significant digits. These are only
|
|
|
|
// output if abs(number being formatted) < 1.0.
|
|
|
|
result += zero;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-11-02 02:42:40 +00:00
|
|
|
// Here is where we escape from the loop. We escape if we've output
|
|
|
|
// the maximum fraction digits (specified in the for expression above).
|
|
|
|
// We also stop when we've output the minimum digits and either:
|
|
|
|
// we have an integer, so there is no fractional stuff to display,
|
|
|
|
// or we're out of significant digits.
|
|
|
|
if (i >= getMinimumFractionDigits())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// No precision is left.
|
|
|
|
result += zero;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record field information for caller.
|
|
|
|
if (fieldPosition.getField() == NumberFormat::kFractionField)
|
1999-12-08 02:11:04 +00:00
|
|
|
fieldPosition.setEndIndex(result.length());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-11-02 02:42:40 +00:00
|
|
|
result += (digits.fIsPositive ? fPositiveSuffix : fNegativeSuffix);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-03-08 19:40:09 +00:00
|
|
|
addPadding(result, fieldPosition, TRUE, !digits.fIsPositive);
|
1999-08-16 21:50:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inserts the character fPad as needed to expand result to fFormatWidth.
|
|
|
|
* @param result the string to be padded
|
|
|
|
* @param hasAffixes if true, padding is positioned appropriately before or
|
|
|
|
* after affixes. If false, then isNegative is ignored, and there are only
|
|
|
|
* two effective pad positions: kPadBeforePrefix/kPadAfterPrefix and
|
|
|
|
* kPadBeforeSuffix/kPadAfterSuffix.
|
|
|
|
* @param isNegative must be true if result contains a formatted negative
|
|
|
|
* number, and false otherwise. Ignored if hasAffixes is false.
|
|
|
|
*/
|
2001-03-08 19:40:09 +00:00
|
|
|
void DecimalFormat::addPadding(UnicodeString& result,
|
|
|
|
FieldPosition& fieldPosition,
|
|
|
|
UBool hasAffixes,
|
|
|
|
UBool isNegative) const
|
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
if (fFormatWidth > 0) {
|
1999-12-08 02:11:04 +00:00
|
|
|
int32_t len = fFormatWidth - result.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
if (len > 0) {
|
2001-09-17 21:50:19 +00:00
|
|
|
UnicodeString padding;
|
1999-08-16 21:50:52 +00:00
|
|
|
for (int32_t i=0; i<len; ++i) {
|
2001-09-17 21:50:19 +00:00
|
|
|
padding += fPad;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
switch (fPadPosition) {
|
|
|
|
case kPadAfterPrefix:
|
|
|
|
if (hasAffixes) {
|
1999-12-08 02:11:04 +00:00
|
|
|
result.insert(isNegative ? fNegativePrefix.length()
|
|
|
|
: fPositivePrefix.length(),
|
2001-09-17 21:50:19 +00:00
|
|
|
padding);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
} // else fall through to next case
|
|
|
|
case kPadBeforePrefix:
|
2001-09-17 21:50:19 +00:00
|
|
|
result.insert(0, padding);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
case kPadBeforeSuffix:
|
|
|
|
if (hasAffixes) {
|
1999-12-08 02:11:04 +00:00
|
|
|
result.insert(result.length() -
|
|
|
|
(isNegative ? fNegativeSuffix.length()
|
|
|
|
: fPositiveSuffix.length()),
|
2001-09-17 21:50:19 +00:00
|
|
|
padding);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
} // else fall through to next case
|
|
|
|
case kPadAfterSuffix:
|
|
|
|
result += padding;
|
|
|
|
break;
|
|
|
|
}
|
2001-03-08 19:40:09 +00:00
|
|
|
fieldPosition.setBeginIndex(len + fieldPosition.getBeginIndex());
|
|
|
|
fieldPosition.setEndIndex(len + fieldPosition.getEndIndex());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::parse(const UnicodeString& text,
|
|
|
|
Formattable& result,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
|
|
|
NumberFormat::parse(text, result, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::parse(const UnicodeString& text,
|
|
|
|
Formattable& result,
|
|
|
|
ParsePosition& parsePosition) const
|
|
|
|
{
|
|
|
|
int32_t backup = parsePosition.getIndex();
|
2000-11-02 02:42:40 +00:00
|
|
|
int32_t i;
|
2001-09-17 21:50:19 +00:00
|
|
|
int32_t padLen = fPad.length();
|
2000-11-02 02:42:40 +00:00
|
|
|
|
|
|
|
// Skip padding characters, if any
|
1999-08-16 21:50:52 +00:00
|
|
|
if (fFormatWidth > 0) {
|
2000-11-02 02:42:40 +00:00
|
|
|
i = parsePosition.getIndex();
|
2001-09-17 21:50:19 +00:00
|
|
|
while (i < text.length() && !text.compare(i, padLen, fPad, 0, padLen)) {
|
|
|
|
i += padLen;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
parsePosition.setIndex(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// special case NaN
|
2000-10-24 16:09:39 +00:00
|
|
|
// If the text is composed of the representation of NaN, returns NaN.length
|
2001-09-17 21:50:19 +00:00
|
|
|
UnicodeString nan(fSymbols->getSymbol(DecimalFormatSymbols::kNaNSymbol));
|
|
|
|
int32_t nanLen = (text.compare(parsePosition.getIndex(), nan.length(), nan)
|
|
|
|
? 0 : nan.length());
|
2000-08-07 23:06:28 +00:00
|
|
|
if (nanLen) {
|
|
|
|
parsePosition.setIndex(parsePosition.getIndex() + nanLen);
|
1999-12-28 23:57:50 +00:00
|
|
|
result.setDouble(uprv_getNaN());
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-11-02 02:42:40 +00:00
|
|
|
// status is used to record whether a number is infinite.
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool status[fgStatusLength];
|
2000-11-02 02:42:40 +00:00
|
|
|
DigitList digits;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-11-02 02:42:40 +00:00
|
|
|
if (!subparse(text, parsePosition, digits, status)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
parsePosition.setIndex(backup);
|
|
|
|
return;
|
2000-11-02 02:42:40 +00:00
|
|
|
}
|
|
|
|
if (fFormatWidth < 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
i = parsePosition.getIndex();
|
2001-09-17 21:50:19 +00:00
|
|
|
while (i < text.length() && !text.compare(i, padLen, fPad, 0, padLen)) {
|
|
|
|
i += padLen;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
parsePosition.setIndex(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle infinity
|
|
|
|
if (status[fgStatusInfinite]) {
|
1999-12-28 23:57:50 +00:00
|
|
|
double inf = uprv_getInfinity();
|
2000-11-02 02:42:40 +00:00
|
|
|
result.setDouble(digits.fIsPositive ? inf : -inf);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do as much of the multiplier conversion as possible without
|
|
|
|
// losing accuracy.
|
|
|
|
int32_t mult = fMultiplier; // Don't modify this.multiplier
|
|
|
|
while (mult % 10 == 0) {
|
|
|
|
mult /= 10;
|
2000-11-02 02:42:40 +00:00
|
|
|
--digits.fDecimalAt;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-10-11 15:46:21 +00:00
|
|
|
// Handle integral values. We want to return the most
|
|
|
|
// parsimonious type that will accommodate all of the result's
|
|
|
|
// precision. We therefore only return a long if the result fits
|
|
|
|
// entirely within a long (taking into account the multiplier) --
|
|
|
|
// otherwise we fall through and return a double. When more
|
|
|
|
// numeric types are supported by Formattable (e.g., 64-bit
|
|
|
|
// integers, bignums) we will extend this logic to include them.
|
2000-11-02 02:42:40 +00:00
|
|
|
if (digits.fitsIntoLong(isParseIntegerOnly())) {
|
|
|
|
int32_t n = digits.getLong();
|
1999-08-16 21:50:52 +00:00
|
|
|
if (n % mult == 0) {
|
2000-11-02 02:42:40 +00:00
|
|
|
result.setLong(n / mult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else { // else handle the remainder
|
|
|
|
result.setDouble(((double)n) / mult);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2000-11-02 02:42:40 +00:00
|
|
|
else {
|
|
|
|
// Handle non-integral or very large values
|
|
|
|
// Dividing by one is okay and not that costly.
|
|
|
|
result.setDouble(digits.getDouble() / mult);
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 16:09:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
This is an old implimentation that was preparing for 64-bit numbers in ICU.
|
|
|
|
It is very slow, and 64-bit numbers are not ANSI-C compatible. This code
|
|
|
|
is here if we change our minds.
|
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Parse the given text into a number. The text is parsed beginning at
|
|
|
|
* parsePosition, until an unparseable character is seen.
|
|
|
|
* @param text The string to parse.
|
|
|
|
* @param parsePosition The position at which to being parsing. Upon
|
|
|
|
* return, the first unparseable character.
|
|
|
|
* @param digits The DigitList to set to the parsed value.
|
|
|
|
* @param isExponent If true, parse an exponent. This means no
|
2000-10-12 19:04:06 +00:00
|
|
|
* infinite values and integer only. By default it's really false.
|
1999-08-16 21:50:52 +00:00
|
|
|
* @param status Upon return contains boolean status flags indicating
|
|
|
|
* whether the value was infinite and whether it was positive.
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool DecimalFormat::subparse(const UnicodeString& text, ParsePosition& parsePosition,
|
2000-10-12 19:04:06 +00:00
|
|
|
DigitList& digits, UBool* status) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-09-17 21:50:19 +00:00
|
|
|
UTextOffset position = parsePosition.getIndex();
|
|
|
|
UTextOffset oldStart = position;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// check for positivePrefix; take longest
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool gotPositive = text.compare(position,fPositivePrefix.length(),fPositivePrefix,0,
|
1999-12-08 02:11:04 +00:00
|
|
|
fPositivePrefix.length()) == 0;
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool gotNegative = text.compare(position,fNegativePrefix.length(),fNegativePrefix,0,
|
1999-12-08 02:11:04 +00:00
|
|
|
fNegativePrefix.length()) == 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
// If the number is positive and negative at the same time,
|
|
|
|
// 1. the number is positive if the positive prefix is longer
|
|
|
|
// 2. the number is negative if the negative prefix is longer
|
|
|
|
if (gotPositive && gotNegative) {
|
1999-12-08 02:11:04 +00:00
|
|
|
if (fPositivePrefix.length() > fNegativePrefix.length())
|
1999-08-16 21:50:52 +00:00
|
|
|
gotNegative = FALSE;
|
1999-12-08 02:11:04 +00:00
|
|
|
else if (fPositivePrefix.length() < fNegativePrefix.length())
|
1999-08-16 21:50:52 +00:00
|
|
|
gotPositive = FALSE;
|
|
|
|
}
|
|
|
|
if(gotPositive)
|
1999-12-08 02:11:04 +00:00
|
|
|
position += fPositivePrefix.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
else if(gotNegative)
|
1999-12-08 02:11:04 +00:00
|
|
|
position += fNegativePrefix.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
else {
|
|
|
|
parsePosition.setErrorIndex(position);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2000-10-12 19:04:06 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// process digits or Inf, find decimal position
|
2001-09-17 21:50:19 +00:00
|
|
|
UnicodeString inf(fSymbols->getSymbol(DecimalFormatSymbols::kInfinitySymbol));
|
|
|
|
int32_t infLen = (text.compare(position, inf.length(), inf)
|
|
|
|
? 0 : inf.length());
|
2000-10-12 19:04:06 +00:00
|
|
|
position += infLen; // infLen is non-zero when it does equal to infinity
|
|
|
|
status[fgStatusInfinite] = (UBool)infLen;
|
|
|
|
if (!infLen)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// We now have a string of digits, possibly with grouping symbols,
|
|
|
|
// and decimal points. We want to process these into a DigitList.
|
|
|
|
// We don't want to put a bunch of leading zeros into the DigitList
|
|
|
|
// though, so we keep track of the location of the decimal point,
|
|
|
|
// put only significant digits into the DigitList, and adjust the
|
|
|
|
// exponent as needed.
|
|
|
|
|
|
|
|
digits.fDecimalAt = digits.fCount = 0;
|
2001-09-17 21:50:19 +00:00
|
|
|
UChar32 zero = fSymbols->getSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
|
|
|
|
UnicodeString decimal(fIsCurrencyFormat
|
|
|
|
? fSymbols->getSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol)
|
|
|
|
: fSymbols->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
|
|
|
|
UnicodeString grouping(fSymbols->getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol));
|
|
|
|
UnicodeString exponentChar(fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol));
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool sawDecimal = FALSE;
|
2000-08-07 23:06:28 +00:00
|
|
|
UBool sawDigit = FALSE;
|
2000-10-12 19:04:06 +00:00
|
|
|
int32_t backup = -1;
|
2001-09-17 21:50:19 +00:00
|
|
|
UChar32 ch;
|
2000-08-07 23:06:28 +00:00
|
|
|
int32_t digit;
|
2000-10-24 16:09:39 +00:00
|
|
|
int32_t textLength = text.length(); // One less pointer to follow
|
2001-09-17 21:50:19 +00:00
|
|
|
int32_t groupingLen = grouping.length();
|
|
|
|
int32_t decimalLen = decimal.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// We have to track digitCount ourselves, because digits.fCount will
|
|
|
|
// pin when the maximum allowable digits is reached.
|
|
|
|
int32_t digitCount = 0;
|
|
|
|
|
2001-09-17 21:50:19 +00:00
|
|
|
for (; position < textLength; position += 1 + UTF_NEED_MULTIPLE_UCHAR(ch))
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-09-17 21:50:19 +00:00
|
|
|
ch = text.char32At(position);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* We recognize all digit ranges, not only the Latin digit range
|
|
|
|
* '0'..'9'. We do so by using the Character.digit() method,
|
|
|
|
* which converts a valid Unicode digit to the range 0..9.
|
|
|
|
*
|
|
|
|
* The character 'ch' may be a digit. If so, place its value
|
|
|
|
* from 0 to 9 in 'digit'. First try using the locale digit,
|
|
|
|
* which may or MAY NOT be a standard Unicode digit range. If
|
|
|
|
* this fails, try using the standard Unicode digit ranges by
|
|
|
|
* calling Character.digit(). If this also fails, digit will
|
|
|
|
* have a value outside the range 0..9.
|
|
|
|
*/
|
2000-08-07 23:06:28 +00:00
|
|
|
digit = ch - zero;
|
|
|
|
if (digit < 0 || digit > 9)
|
|
|
|
{
|
2001-07-09 22:40:08 +00:00
|
|
|
digit = u_charDigitValue(ch);
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-08-07 23:06:28 +00:00
|
|
|
if (digit > 0 && digit <= 9)
|
|
|
|
{
|
2000-10-24 16:09:39 +00:00
|
|
|
// Cancel out backup setting (see grouping handler below)
|
|
|
|
backup = -1;
|
|
|
|
|
2000-08-07 23:06:28 +00:00
|
|
|
sawDigit = TRUE;
|
|
|
|
// output a regular non-zero digit.
|
|
|
|
++digitCount;
|
|
|
|
digits.append((char)(digit + '0'));
|
|
|
|
}
|
|
|
|
else if (digit == 0)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// Cancel out backup setting (see grouping handler below)
|
2000-10-24 16:09:39 +00:00
|
|
|
backup = -1;
|
1999-08-16 21:50:52 +00:00
|
|
|
sawDigit = TRUE;
|
|
|
|
|
2000-10-12 19:04:06 +00:00
|
|
|
// Check for leading zeros
|
|
|
|
if (digits.fCount != 0)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// output a regular zero digit.
|
|
|
|
++digitCount;
|
|
|
|
digits.append((char)(digit + '0'));
|
|
|
|
}
|
2000-10-12 19:04:06 +00:00
|
|
|
else if (sawDecimal)
|
|
|
|
{
|
|
|
|
// If we have seen the decimal, but no significant digits yet,
|
|
|
|
// then we account for leading zeros by decrementing the
|
|
|
|
// digits.fDecimalAt into negative values.
|
|
|
|
--digits.fDecimalAt;
|
|
|
|
}
|
|
|
|
// else ignore leading zeros in integer part of number.
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
else if (!text.compare(position, groupingLen, grouping) && isGroupingUsed())
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// Ignore grouping characters, if we are using them, but require
|
|
|
|
// that they be followed by a digit. Otherwise we backup and
|
|
|
|
// reprocess them.
|
|
|
|
backup = position;
|
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
else if (!text.compare(position, decimalLen, decimal) && !isParseIntegerOnly() && !sawDecimal)
|
2000-10-12 19:04:06 +00:00
|
|
|
{
|
|
|
|
// If we're only parsing integers, or if we ALREADY saw the
|
|
|
|
// decimal, then don't parse this one.
|
2000-10-24 16:09:39 +00:00
|
|
|
|
2000-10-12 19:04:06 +00:00
|
|
|
digits.fDecimalAt = digitCount; // Not digits.fCount!
|
|
|
|
sawDecimal = TRUE;
|
|
|
|
}
|
2002-02-23 00:34:39 +00:00
|
|
|
else if (!text.caseCompare(position,
|
2001-09-17 21:50:19 +00:00
|
|
|
fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol).length(),
|
2002-02-23 00:34:39 +00:00
|
|
|
fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol),
|
|
|
|
U_FOLD_CASE_DEFAULT)) // error code is set below if !sawDigit
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
// Parse sign, if present
|
|
|
|
int32_t pos = position + 1; // position + exponentSep.length();
|
2000-10-12 19:04:06 +00:00
|
|
|
DigitList exponentDigits;
|
|
|
|
|
2000-10-24 16:09:39 +00:00
|
|
|
if (pos < textLength)
|
2000-08-07 23:06:28 +00:00
|
|
|
{
|
2001-09-17 21:50:19 +00:00
|
|
|
if (!text.compare(pos,
|
|
|
|
fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol).length(),
|
|
|
|
fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol)))
|
2000-08-07 23:06:28 +00:00
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
++pos;
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
else if (!text.compare(pos,
|
|
|
|
fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol).length(),
|
|
|
|
fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol)))
|
2000-08-07 23:06:28 +00:00
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
++pos;
|
2000-11-02 02:42:40 +00:00
|
|
|
exponentDigits.fIsPositive = FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-10-24 16:09:39 +00:00
|
|
|
while (pos < textLength) {
|
2000-08-07 23:06:28 +00:00
|
|
|
ch = text[(UTextOffset)pos];
|
|
|
|
digit = ch - zero;
|
|
|
|
|
|
|
|
if (digit < 0 || digit > 9) {
|
2001-07-09 22:40:08 +00:00
|
|
|
digit = u_charDigitValue(ch);
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
2000-11-02 02:42:40 +00:00
|
|
|
if (0 <= digit && digit <= 9) {
|
1999-08-16 21:50:52 +00:00
|
|
|
++pos;
|
2000-11-02 02:42:40 +00:00
|
|
|
exponentDigits.append((char)(digit + '0'));
|
1999-08-16 21:50:52 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exponentDigits.fCount > 0) {
|
|
|
|
exponentDigits.fDecimalAt = exponentDigits.fCount;
|
2000-11-02 02:42:40 +00:00
|
|
|
digits.fDecimalAt += exponentDigits.getLong();
|
1999-08-16 21:50:52 +00:00
|
|
|
position = pos; // Advance past the exponent
|
|
|
|
}
|
|
|
|
|
|
|
|
break; // Whether we fail or succeed, we exit this loop
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
else
|
|
|
|
break;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-08-07 23:06:28 +00:00
|
|
|
if (backup != -1)
|
|
|
|
{
|
|
|
|
position = backup;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// If there was no decimal point we have an integer
|
2000-08-07 23:06:28 +00:00
|
|
|
if (!sawDecimal)
|
|
|
|
{
|
2000-11-02 02:42:40 +00:00
|
|
|
digits.fDecimalAt += digitCount; // Not digits.fCount!
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// If none of the text string was recognized. For example, parse
|
|
|
|
// "x" with pattern "#0.00" (return index and error index both 0)
|
|
|
|
// parse "$" with pattern "$#0.00". (return index 0 and error index
|
|
|
|
// 1).
|
|
|
|
if (!sawDigit && digitCount == 0) {
|
|
|
|
parsePosition.setIndex(oldStart);
|
|
|
|
parsePosition.setErrorIndex(oldStart);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for positiveSuffix
|
2000-10-24 16:09:39 +00:00
|
|
|
if (gotPositive && fPositiveSuffix.length() > 0)
|
2000-08-07 23:06:28 +00:00
|
|
|
{
|
1999-12-08 02:11:04 +00:00
|
|
|
gotPositive = text.compare(position,fPositiveSuffix.length(),fPositiveSuffix,0,
|
|
|
|
fPositiveSuffix.length()) == 0;
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
2000-10-24 16:09:39 +00:00
|
|
|
if (gotNegative && fNegativeSuffix.length() > 0)
|
2000-08-07 23:06:28 +00:00
|
|
|
{
|
1999-12-08 02:11:04 +00:00
|
|
|
gotNegative = text.compare(position,fNegativeSuffix.length(),fNegativeSuffix,0,
|
|
|
|
fNegativeSuffix.length()) == 0;
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// if both match, take longest
|
2000-08-07 23:06:28 +00:00
|
|
|
if (gotPositive && gotNegative)
|
|
|
|
{
|
1999-12-08 02:11:04 +00:00
|
|
|
if (fPositiveSuffix.length() > fNegativeSuffix.length())
|
2000-08-07 23:06:28 +00:00
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
gotNegative = FALSE;
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
1999-12-08 02:11:04 +00:00
|
|
|
else if (fPositiveSuffix.length() < fNegativeSuffix.length())
|
2000-08-07 23:06:28 +00:00
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
gotPositive = FALSE;
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
2000-10-24 16:09:39 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
gotPositive = TRUE; // Make them equal to each other.
|
|
|
|
gotNegative = TRUE;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fail if neither or both
|
2000-08-07 23:06:28 +00:00
|
|
|
if (gotPositive == gotNegative)
|
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
parsePosition.setErrorIndex(position);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
parsePosition.setIndex(position +
|
1999-12-08 02:11:04 +00:00
|
|
|
(gotPositive ? fPositiveSuffix.length() :
|
|
|
|
fNegativeSuffix.length())); // mark success!
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-11-02 02:42:40 +00:00
|
|
|
digits.fIsPositive = gotPositive;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-08-07 23:06:28 +00:00
|
|
|
if(parsePosition.getIndex() == oldStart)
|
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
parsePosition.setErrorIndex(position);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2000-10-24 16:09:39 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the pointer to the localized decimal format symbols
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
const DecimalFormatSymbols*
|
|
|
|
DecimalFormat::getDecimalFormatSymbols() const
|
|
|
|
{
|
|
|
|
return fSymbols;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// De-owning the current localized symbols and adopt the new symbols.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::adoptDecimalFormatSymbols(DecimalFormatSymbols* symbolsToAdopt)
|
|
|
|
{
|
|
|
|
if (fSymbols != NULL)
|
|
|
|
delete fSymbols;
|
|
|
|
|
|
|
|
fSymbols = symbolsToAdopt;
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Setting the symbols is equlivalent to adopting a newly created localized
|
|
|
|
// symbols.
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::setDecimalFormatSymbols(const DecimalFormatSymbols& symbols)
|
|
|
|
{
|
|
|
|
adoptDecimalFormatSymbols(new DecimalFormatSymbols(symbols));
|
|
|
|
expandAffixes();
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the positive prefix of the number pattern.
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::getPositivePrefix(UnicodeString& result) const
|
|
|
|
{
|
|
|
|
result = fPositivePrefix;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Sets the positive prefix of the number pattern.
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::setPositivePrefix(const UnicodeString& newValue)
|
|
|
|
{
|
|
|
|
fPositivePrefix = newValue;
|
|
|
|
delete fPosPrefixPattern;
|
|
|
|
fPosPrefixPattern = 0;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the negative prefix of the number pattern.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::getNegativePrefix(UnicodeString& result) const
|
|
|
|
{
|
|
|
|
result = fNegativePrefix;
|
|
|
|
return result;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the negative prefix of the number pattern.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::setNegativePrefix(const UnicodeString& newValue)
|
|
|
|
{
|
|
|
|
fNegativePrefix = newValue;
|
|
|
|
delete fNegPrefixPattern;
|
|
|
|
fNegPrefixPattern = 0;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the positive suffix of the number pattern.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::getPositiveSuffix(UnicodeString& result) const
|
|
|
|
{
|
|
|
|
result = fPositiveSuffix;
|
|
|
|
return result;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Sets the positive suffix of the number pattern.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::setPositiveSuffix(const UnicodeString& newValue)
|
|
|
|
{
|
|
|
|
fPositiveSuffix = newValue;
|
|
|
|
delete fPosSuffixPattern;
|
|
|
|
fPosSuffixPattern = 0;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the negative suffix of the number pattern.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::getNegativeSuffix(UnicodeString& result) const
|
|
|
|
{
|
|
|
|
result = fNegativeSuffix;
|
|
|
|
return result;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Sets the negative suffix of the number pattern.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::setNegativeSuffix(const UnicodeString& newValue)
|
|
|
|
{
|
|
|
|
fNegativeSuffix = newValue;
|
|
|
|
delete fNegSuffixPattern;
|
|
|
|
fNegSuffixPattern = 0;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the multiplier of the number pattern.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t DecimalFormat::getMultiplier() const
|
|
|
|
{
|
|
|
|
return fMultiplier;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Sets the multiplier of the number pattern.
|
|
|
|
void
|
|
|
|
DecimalFormat::setMultiplier(int32_t newValue)
|
|
|
|
{
|
2000-10-24 16:09:39 +00:00
|
|
|
// This shouldn't be set to 0.
|
|
|
|
// Due to compatibility with ICU4J we cannot set an error code and refuse 0.
|
|
|
|
// So the rest of the code should ignore fMultiplier when it's 0. [grhoten]
|
1999-08-16 21:50:52 +00:00
|
|
|
fMultiplier = newValue;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Get the rounding increment.
|
|
|
|
* @return A positive rounding increment, or 0.0 if rounding
|
|
|
|
* is not in effect.
|
|
|
|
* @see #setRoundingIncrement
|
|
|
|
* @see #getRoundingMode
|
|
|
|
* @see #setRoundingMode
|
|
|
|
*/
|
|
|
|
double DecimalFormat::getRoundingIncrement() {
|
|
|
|
return fRoundingDouble;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the rounding increment. This method also controls whether
|
|
|
|
* rounding is enabled.
|
|
|
|
* @param newValue A positive rounding increment, or 0.0 to disable rounding.
|
|
|
|
* Negative increments are equivalent to 0.0.
|
|
|
|
* @see #getRoundingIncrement
|
|
|
|
* @see #getRoundingMode
|
|
|
|
* @see #setRoundingMode
|
|
|
|
*/
|
|
|
|
void DecimalFormat::setRoundingIncrement(double newValue) {
|
|
|
|
if (newValue > 0.0) {
|
|
|
|
if (fRoundingIncrement == NULL) {
|
|
|
|
fRoundingIncrement = new DigitList();
|
|
|
|
}
|
2001-02-10 02:16:19 +00:00
|
|
|
fRoundingIncrement->set((int32_t)newValue);
|
1999-08-16 21:50:52 +00:00
|
|
|
fRoundingDouble = newValue;
|
|
|
|
} else {
|
|
|
|
delete fRoundingIncrement;
|
|
|
|
fRoundingIncrement = NULL;
|
|
|
|
fRoundingDouble = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the rounding mode.
|
|
|
|
* @return A rounding mode
|
|
|
|
* @see #setRoundingIncrement
|
|
|
|
* @see #getRoundingIncrement
|
|
|
|
* @see #setRoundingMode
|
|
|
|
*/
|
|
|
|
DecimalFormat::ERoundingMode DecimalFormat::getRoundingMode() {
|
|
|
|
return fRoundingMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the rounding mode. This has no effect unless the rounding
|
|
|
|
* increment is greater than zero.
|
|
|
|
* @param roundingMode A rounding mode
|
|
|
|
* @see #setRoundingIncrement
|
|
|
|
* @see #getRoundingIncrement
|
|
|
|
* @see #getRoundingMode
|
|
|
|
*/
|
|
|
|
void DecimalFormat::setRoundingMode(ERoundingMode roundingMode) {
|
|
|
|
fRoundingMode = roundingMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the width to which the output of <code>format()</code> is padded.
|
|
|
|
* @return the format width, or zero if no padding is in effect
|
|
|
|
* @see #setFormatWidth
|
|
|
|
* @see #getPadCharacter
|
|
|
|
* @see #setPadCharacter
|
|
|
|
* @see #getPadPosition
|
|
|
|
* @see #setPadPosition
|
|
|
|
*/
|
|
|
|
int32_t DecimalFormat::getFormatWidth() {
|
|
|
|
return fFormatWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the width to which the output of <code>format()</code> is padded.
|
|
|
|
* This method also controls whether padding is enabled.
|
|
|
|
* @param width the width to which to pad the result of
|
|
|
|
* <code>format()</code>, or zero to disable padding. A negative
|
|
|
|
* width is equivalent to 0.
|
|
|
|
* @see #getFormatWidth
|
|
|
|
* @see #getPadCharacter
|
|
|
|
* @see #setPadCharacter
|
|
|
|
* @see #getPadPosition
|
|
|
|
* @see #setPadPosition
|
|
|
|
*/
|
|
|
|
void DecimalFormat::setFormatWidth(int32_t width) {
|
|
|
|
fFormatWidth = (width > 0) ? width : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the character used to pad to the format width. The default is ' '.
|
|
|
|
* @return the pad character
|
|
|
|
* @see #setFormatWidth
|
|
|
|
* @see #getFormatWidth
|
|
|
|
* @see #setPadCharacter
|
|
|
|
* @see #getPadPosition
|
|
|
|
* @see #setPadPosition
|
|
|
|
*/
|
2000-05-24 22:44:14 +00:00
|
|
|
UnicodeString DecimalFormat::getPadCharacterString() {
|
1999-08-16 21:50:52 +00:00
|
|
|
return fPad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the character used to pad to the format width. This has no effect
|
|
|
|
* unless padding is enabled.
|
|
|
|
* @param padChar the pad character
|
|
|
|
* @see #setFormatWidth
|
|
|
|
* @see #getFormatWidth
|
|
|
|
* @see #getPadCharacter
|
|
|
|
* @see #getPadPosition
|
|
|
|
* @see #setPadPosition
|
|
|
|
*/
|
2000-05-24 22:44:14 +00:00
|
|
|
void DecimalFormat::setPadCharacter(UnicodeString padChar) {
|
2001-09-17 21:50:19 +00:00
|
|
|
if (padChar.length() > 0) {
|
|
|
|
fPad = padChar;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fPad = kPatternPadEscape;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the position at which padding will take place. This is the location
|
|
|
|
* at which padding will be inserted if the result of <code>format()</code>
|
|
|
|
* is shorter than the format width.
|
|
|
|
* @return the pad position, one of <code>kPadBeforePrefix</code>,
|
|
|
|
* <code>kPadAfterPrefix</code>, <code>kPadBeforeSuffix</code>, or
|
|
|
|
* <code>kPadAfterSuffix</code>.
|
|
|
|
* @see #setFormatWidth
|
|
|
|
* @see #getFormatWidth
|
|
|
|
* @see #setPadCharacter
|
|
|
|
* @see #getPadCharacter
|
|
|
|
* @see #setPadPosition
|
|
|
|
* @see #kPadBeforePrefix
|
|
|
|
* @see #kPadAfterPrefix
|
|
|
|
* @see #kPadBeforeSuffix
|
|
|
|
* @see #kPadAfterSuffix
|
|
|
|
*/
|
|
|
|
DecimalFormat::EPadPosition DecimalFormat::getPadPosition() {
|
|
|
|
return fPadPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <strong><font face=helvetica color=red>NEW</font></strong>
|
|
|
|
* Set the position at which padding will take place. This is the location
|
|
|
|
* at which padding will be inserted if the result of <code>format()</code>
|
|
|
|
* is shorter than the format width. This has no effect unless padding is
|
|
|
|
* enabled.
|
|
|
|
* @param padPos the pad position, one of <code>kPadBeforePrefix</code>,
|
|
|
|
* <code>kPadAfterPrefix</code>, <code>kPadBeforeSuffix</code>, or
|
|
|
|
* <code>kPadAfterSuffix</code>.
|
|
|
|
* @see #setFormatWidth
|
|
|
|
* @see #getFormatWidth
|
|
|
|
* @see #setPadCharacter
|
|
|
|
* @see #getPadCharacter
|
|
|
|
* @see #getPadPosition
|
|
|
|
* @see #kPadBeforePrefix
|
|
|
|
* @see #kPadAfterPrefix
|
|
|
|
* @see #kPadBeforeSuffix
|
|
|
|
* @see #kPadAfterSuffix
|
|
|
|
*/
|
|
|
|
void DecimalFormat::setPadPosition(EPadPosition padPos) {
|
|
|
|
fPadPosition = padPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether or not scientific notation is used.
|
|
|
|
* @return TRUE if this object formats and parses scientific notation
|
|
|
|
* @see #setScientificNotation
|
|
|
|
* @see #getMinimumExponentDigits
|
|
|
|
* @see #setMinimumExponentDigits
|
|
|
|
* @see #isExponentSignAlwaysShown
|
|
|
|
* @see #setExponentSignAlwaysShown
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool DecimalFormat::isScientificNotation() {
|
1999-08-16 21:50:52 +00:00
|
|
|
return fUseExponentialNotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether or not scientific notation is used.
|
|
|
|
* @param useScientific TRUE if this object formats and parses scientific
|
|
|
|
* notation
|
|
|
|
* @see #isScientificNotation
|
|
|
|
* @see #getMinimumExponentDigits
|
|
|
|
* @see #setMinimumExponentDigits
|
|
|
|
* @see #isExponentSignAlwaysShown
|
|
|
|
* @see #setExponentSignAlwaysShown
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
void DecimalFormat::setScientificNotation(UBool useScientific) {
|
1999-08-16 21:50:52 +00:00
|
|
|
fUseExponentialNotation = useScientific;
|
|
|
|
if (fUseExponentialNotation && fMinExponentDigits < 1) {
|
|
|
|
fMinExponentDigits = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the minimum exponent digits that will be shown.
|
|
|
|
* @return the minimum exponent digits that will be shown
|
|
|
|
* @see #setScientificNotation
|
|
|
|
* @see #isScientificNotation
|
|
|
|
* @see #setMinimumExponentDigits
|
|
|
|
* @see #isExponentSignAlwaysShown
|
|
|
|
* @see #setExponentSignAlwaysShown
|
|
|
|
*/
|
|
|
|
int8_t DecimalFormat::getMinimumExponentDigits() {
|
|
|
|
return fMinExponentDigits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the minimum exponent digits that will be shown. This has no
|
|
|
|
* effect unless scientific notation is in use.
|
|
|
|
* @param minExpDig a value >= 1 indicating the fewest exponent digits
|
|
|
|
* that will be shown. Values less than 1 will be treated as 1.
|
|
|
|
* @see #setScientificNotation
|
|
|
|
* @see #isScientificNotation
|
|
|
|
* @see #getMinimumExponentDigits
|
|
|
|
* @see #isExponentSignAlwaysShown
|
|
|
|
* @see #setExponentSignAlwaysShown
|
|
|
|
*/
|
|
|
|
void DecimalFormat::setMinimumExponentDigits(int8_t minExpDig) {
|
2000-08-09 20:46:49 +00:00
|
|
|
fMinExponentDigits = (int8_t)((minExpDig > 0) ? minExpDig : 1);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the exponent sign is always shown.
|
|
|
|
* @return TRUE if the exponent is always prefixed with either the
|
|
|
|
* localized minus sign or the localized plus sign, false if only negative
|
|
|
|
* exponents are prefixed with the localized minus sign.
|
|
|
|
* @see #setScientificNotation
|
|
|
|
* @see #isScientificNotation
|
|
|
|
* @see #setMinimumExponentDigits
|
|
|
|
* @see #getMinimumExponentDigits
|
|
|
|
* @see #setExponentSignAlwaysShown
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool DecimalFormat::isExponentSignAlwaysShown() {
|
1999-08-16 21:50:52 +00:00
|
|
|
return fExponentSignAlwaysShown;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether the exponent sign is always shown. This has no effect
|
|
|
|
* unless scientific notation is in use.
|
|
|
|
* @param expSignAlways TRUE if the exponent is always prefixed with either
|
|
|
|
* the localized minus sign or the localized plus sign, false if only
|
|
|
|
* negative exponents are prefixed with the localized minus sign.
|
|
|
|
* @see #setScientificNotation
|
|
|
|
* @see #isScientificNotation
|
|
|
|
* @see #setMinimumExponentDigits
|
|
|
|
* @see #getMinimumExponentDigits
|
|
|
|
* @see #isExponentSignAlwaysShown
|
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
void DecimalFormat::setExponentSignAlwaysShown(UBool expSignAlways) {
|
1999-08-16 21:50:52 +00:00
|
|
|
fExponentSignAlwaysShown = expSignAlways;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the grouping size of the number pattern. For example, thousand or 10
|
|
|
|
// thousand groupings.
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
DecimalFormat::getGroupingSize() const
|
|
|
|
{
|
|
|
|
return fGroupingSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the grouping size of the number pattern.
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::setGroupingSize(int32_t newValue)
|
|
|
|
{
|
|
|
|
fGroupingSize = newValue;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
2000-06-07 00:15:59 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2000-08-07 23:06:28 +00:00
|
|
|
|
2000-06-07 00:15:59 +00:00
|
|
|
int32_t
|
|
|
|
DecimalFormat::getSecondaryGroupingSize() const
|
|
|
|
{
|
|
|
|
return fGroupingSize2;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
2000-06-07 00:15:59 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2000-08-07 23:06:28 +00:00
|
|
|
|
2000-06-07 00:15:59 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::setSecondaryGroupingSize(int32_t newValue)
|
|
|
|
{
|
|
|
|
fGroupingSize2 = newValue;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Checks if to show the decimal separator.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool
|
1999-08-16 21:50:52 +00:00
|
|
|
DecimalFormat::isDecimalSeparatorAlwaysShown() const
|
|
|
|
{
|
|
|
|
return fDecimalSeparatorAlwaysShown;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Sets to always show the decimal separator.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
2000-05-18 22:08:39 +00:00
|
|
|
DecimalFormat::setDecimalSeparatorAlwaysShown(UBool newValue)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
fDecimalSeparatorAlwaysShown = newValue;
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Emits the pattern of this DecimalFormat instance.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::toPattern(UnicodeString& result) const
|
|
|
|
{
|
|
|
|
return toPattern(result, FALSE);
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Emits the localized pattern this DecimalFormat instance.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::toLocalizedPattern(UnicodeString& result) const
|
|
|
|
{
|
|
|
|
return toPattern(result, TRUE);
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* Expand the affix pattern strings into the expanded affix strings. If any
|
|
|
|
* affix pattern string is null, do not expand it. This method should be
|
|
|
|
* called any time the symbols or the affix patterns change in order to keep
|
|
|
|
* the expanded affix strings up to date.
|
|
|
|
*/
|
1999-12-22 22:57:04 +00:00
|
|
|
void DecimalFormat::expandAffixes(void) {
|
1999-08-16 21:50:52 +00:00
|
|
|
if (fPosPrefixPattern != 0) {
|
|
|
|
expandAffix(*fPosPrefixPattern, fPositivePrefix);
|
|
|
|
}
|
|
|
|
if (fPosSuffixPattern != 0) {
|
|
|
|
expandAffix(*fPosSuffixPattern, fPositiveSuffix);
|
|
|
|
}
|
|
|
|
if (fNegPrefixPattern != 0) {
|
|
|
|
expandAffix(*fNegPrefixPattern, fNegativePrefix);
|
|
|
|
}
|
|
|
|
if (fNegSuffixPattern != 0) {
|
|
|
|
expandAffix(*fNegSuffixPattern, fNegativeSuffix);
|
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
#ifdef FMT_DEBUG
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString s;
|
|
|
|
s.append("[")
|
|
|
|
.append(*fPosPrefixPattern).append("|").append(*fPosSuffixPattern)
|
|
|
|
.append(";") .append(*fNegPrefixPattern).append("|").append(*fNegSuffixPattern)
|
|
|
|
.append("]->[")
|
|
|
|
.append(fPositivePrefix).append("|").append(fPositiveSuffix)
|
|
|
|
.append(";") .append(fNegativePrefix).append("|").append(fNegativeSuffix)
|
|
|
|
.append("]\n");
|
|
|
|
debugout(s);
|
|
|
|
#endif
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Expand an affix pattern into an affix string. All characters in the
|
|
|
|
* pattern are literal unless prefixed by kQuote. The following characters
|
|
|
|
* after kQuote are recognized: PATTERN_PERCENT, PATTERN_PER_MILLE,
|
|
|
|
* PATTERN_MINUS, and kCurrencySign. If kCurrencySign is doubled (kQuote +
|
|
|
|
* kCurrencySign + kCurrencySign), it is interpreted as an international
|
|
|
|
* currency sign. Any other character after a kQuote represents itself.
|
|
|
|
* kQuote must be followed by another character; kQuote may not occur by
|
|
|
|
* itself at the end of the pattern.
|
|
|
|
*
|
|
|
|
* @param pattern the non-null, fPossibly empty pattern
|
|
|
|
* @param affix string to receive the expanded equivalent of pattern
|
|
|
|
*/
|
|
|
|
void DecimalFormat::expandAffix(const UnicodeString& pattern,
|
|
|
|
UnicodeString& affix) const {
|
|
|
|
affix.remove();
|
1999-12-08 02:11:04 +00:00
|
|
|
for (int i=0; i<pattern.length(); ) {
|
2001-09-18 20:40:48 +00:00
|
|
|
UChar32 c = pattern.char32At(i++);
|
1999-08-16 21:50:52 +00:00
|
|
|
if (c == kQuote) {
|
2001-09-18 20:40:48 +00:00
|
|
|
c = pattern.char32At(i++);
|
1999-08-16 21:50:52 +00:00
|
|
|
switch (c) {
|
|
|
|
case kCurrencySign:
|
|
|
|
{
|
1999-12-08 02:11:04 +00:00
|
|
|
if (i<pattern.length() &&
|
2001-09-18 20:40:48 +00:00
|
|
|
pattern.char32At(i) == kCurrencySign) {
|
1999-08-16 21:50:52 +00:00
|
|
|
++i;
|
2001-09-17 21:50:19 +00:00
|
|
|
affix += fSymbols->getSymbol(DecimalFormatSymbols::kIntlCurrencySymbol);
|
1999-08-16 21:50:52 +00:00
|
|
|
} else {
|
2001-09-17 21:50:19 +00:00
|
|
|
affix += fSymbols->getSymbol(DecimalFormatSymbols::kCurrencySymbol);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case kPatternPercent:
|
2001-09-17 21:50:19 +00:00
|
|
|
affix.append(fSymbols->getSymbol(DecimalFormatSymbols::kPercentSymbol));
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
case kPatternPerMill:
|
2001-09-17 21:50:19 +00:00
|
|
|
affix.append(fSymbols->getSymbol(DecimalFormatSymbols::kPerMillSymbol));
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
case kPatternPlus:
|
2001-09-17 21:50:19 +00:00
|
|
|
affix.append(fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol));
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
case kPatternMinus:
|
2001-09-17 21:50:19 +00:00
|
|
|
affix.append(fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
affix.append(c);
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
else {
|
|
|
|
affix.append(c);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Appends an affix pattern to the given StringBuffer, quoting special
|
|
|
|
* characters as needed. Uses the internal affix pattern, if that exists,
|
|
|
|
* or the literal affix, if the internal affix pattern is null. The
|
|
|
|
* appended string will generate the same affix pattern (or literal affix)
|
|
|
|
* when passed to toPattern().
|
|
|
|
*
|
|
|
|
* @param buffer the affix string is appended to this
|
|
|
|
* @param affixPattern a pattern such as fPosPrefixPattern; may be null
|
|
|
|
* @param expAffix a corresponding expanded affix, such as fPositivePrefix.
|
|
|
|
* Ignored unless affixPattern is null. If affixPattern is null, then
|
|
|
|
* expAffix is appended as a literal affix.
|
|
|
|
* @param localized true if the appended pattern should contain localized
|
|
|
|
* pattern characters; otherwise, non-localized pattern chars are appended
|
|
|
|
*/
|
|
|
|
void DecimalFormat::appendAffix(UnicodeString& buffer,
|
|
|
|
const UnicodeString* affixPattern,
|
|
|
|
const UnicodeString& expAffix,
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool localized) const {
|
1999-08-16 21:50:52 +00:00
|
|
|
if (affixPattern == 0) {
|
|
|
|
appendAffix(buffer, expAffix, localized);
|
|
|
|
} else {
|
|
|
|
int i;
|
1999-12-08 02:11:04 +00:00
|
|
|
for (int pos=0; pos<affixPattern->length(); pos=i) {
|
1999-08-16 21:50:52 +00:00
|
|
|
i = affixPattern->indexOf(kQuote, pos);
|
|
|
|
if (i < 0) {
|
|
|
|
UnicodeString s;
|
1999-12-08 02:11:04 +00:00
|
|
|
affixPattern->extractBetween(pos, affixPattern->length(), s);
|
1999-08-16 21:50:52 +00:00
|
|
|
appendAffix(buffer, s, localized);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i > pos) {
|
|
|
|
UnicodeString s;
|
|
|
|
affixPattern->extractBetween(pos, i, s);
|
|
|
|
appendAffix(buffer, s, localized);
|
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
UChar32 c = affixPattern->char32At(++i);
|
1999-08-16 21:50:52 +00:00
|
|
|
++i;
|
|
|
|
if (c == kQuote) {
|
2001-09-17 21:50:19 +00:00
|
|
|
buffer.append(c).append(c);
|
1999-08-16 21:50:52 +00:00
|
|
|
// Fall through and append another kQuote below
|
|
|
|
} else if (c == kCurrencySign &&
|
1999-12-08 02:11:04 +00:00
|
|
|
i<affixPattern->length() &&
|
2001-09-17 21:50:19 +00:00
|
|
|
affixPattern->char32At(i) == kCurrencySign) {
|
1999-08-16 21:50:52 +00:00
|
|
|
++i;
|
2001-09-17 21:50:19 +00:00
|
|
|
buffer.append(c).append(c);
|
1999-08-16 21:50:52 +00:00
|
|
|
} else if (localized) {
|
|
|
|
switch (c) {
|
|
|
|
case kPatternPercent:
|
2001-09-17 21:50:19 +00:00
|
|
|
buffer.append(fSymbols->getSymbol(DecimalFormatSymbols::kPercentSymbol));
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
case kPatternPerMill:
|
2001-09-17 21:50:19 +00:00
|
|
|
buffer.append(fSymbols->getSymbol(DecimalFormatSymbols::kPerMillSymbol));
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
case kPatternPlus:
|
2001-09-17 21:50:19 +00:00
|
|
|
buffer.append(fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol));
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
|
|
|
case kPatternMinus:
|
2001-09-17 21:50:19 +00:00
|
|
|
buffer.append(fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol));
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2001-09-17 21:50:19 +00:00
|
|
|
default:
|
|
|
|
buffer.append(c);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
} else {
|
|
|
|
buffer.append(c);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append an affix to the given StringBuffer, using quotes if
|
|
|
|
* there are special characters. Single quotes themselves must be
|
|
|
|
* escaped in either case.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
DecimalFormat::appendAffix( UnicodeString& buffer,
|
|
|
|
const UnicodeString& affix,
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool localized) const {
|
|
|
|
UBool needQuote;
|
1999-08-16 21:50:52 +00:00
|
|
|
if(localized) {
|
2001-09-17 21:50:19 +00:00
|
|
|
needQuote = affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kZeroDigitSymbol)) >= 0
|
|
|
|
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol)) >= 0
|
|
|
|
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol)) >= 0
|
|
|
|
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kPercentSymbol)) >= 0
|
|
|
|
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kPerMillSymbol)) >= 0
|
|
|
|
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kDigitSymbol)) >= 0
|
|
|
|
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol)) >= 0
|
|
|
|
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol)) >= 0
|
|
|
|
|| affix.indexOf(fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol)) >= 0
|
1999-08-16 21:50:52 +00:00
|
|
|
|| affix.indexOf(kCurrencySign) >= 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
needQuote = affix.indexOf(kPatternZeroDigit) >= 0
|
|
|
|
|| affix.indexOf(kPatternGroupingSeparator) >= 0
|
|
|
|
|| affix.indexOf(kPatternDecimalSeparator) >= 0
|
|
|
|
|| affix.indexOf(kPatternPercent) >= 0
|
|
|
|
|| affix.indexOf(kPatternPerMill) >= 0
|
|
|
|
|| affix.indexOf(kPatternDigit) >= 0
|
|
|
|
|| affix.indexOf(kPatternSeparator) >= 0
|
|
|
|
|| affix.indexOf(kPatternExponent) >= 0
|
|
|
|
|| affix.indexOf(kPatternPlus) >= 0
|
|
|
|
|| affix.indexOf(kPatternMinus) >= 0
|
|
|
|
|| affix.indexOf(kCurrencySign) >= 0;
|
|
|
|
}
|
|
|
|
if (needQuote)
|
2000-04-03 23:53:59 +00:00
|
|
|
buffer += (UChar)0x0027 /*'\''*/;
|
|
|
|
if (affix.indexOf((UChar)0x0027 /*'\''*/) < 0)
|
1999-08-16 21:50:52 +00:00
|
|
|
buffer += affix;
|
|
|
|
else {
|
1999-12-08 02:11:04 +00:00
|
|
|
for (int32_t j = 0; j < affix.length(); ++j) {
|
2001-09-20 21:34:19 +00:00
|
|
|
UChar32 c = affix.char32At(j);
|
1999-08-16 21:50:52 +00:00
|
|
|
buffer += c;
|
2000-10-12 19:04:06 +00:00
|
|
|
if (c == 0x0027 /*'\''*/)
|
|
|
|
buffer += c;
|
2001-09-20 21:34:19 +00:00
|
|
|
j = j + UTF_NEED_MULTIPLE_UCHAR(c);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
2000-10-12 19:04:06 +00:00
|
|
|
if (needQuote)
|
|
|
|
buffer += (UChar)0x0027 /*'\''*/;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/* Tell the VC++ compiler not to spew out the warnings about integral size conversion */
|
2000-10-04 22:32:34 +00:00
|
|
|
/*
|
1999-08-16 21:50:52 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#pragma warning( disable : 4761 )
|
|
|
|
#endif
|
2000-10-04 22:32:34 +00:00
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
UnicodeString&
|
2000-05-18 22:08:39 +00:00
|
|
|
DecimalFormat::toPattern(UnicodeString& result, UBool localized) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
|
|
|
result.remove();
|
2001-09-17 21:50:19 +00:00
|
|
|
UChar32 zero;
|
|
|
|
UnicodeString digit;
|
|
|
|
UnicodeString group;
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t i;
|
|
|
|
int32_t roundingDecimalPos = 0; // Pos of decimal in roundingDigits
|
|
|
|
UnicodeString roundingDigits;
|
|
|
|
int32_t padPos = (fFormatWidth > 0) ? fPadPosition : -1;
|
|
|
|
UnicodeString padSpec;
|
2001-09-17 21:50:19 +00:00
|
|
|
|
|
|
|
if (localized) {
|
|
|
|
digit = fSymbols->getSymbol(DecimalFormatSymbols::kDigitSymbol);
|
|
|
|
group = fSymbols->getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol);
|
|
|
|
zero = fSymbols->getSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
digit.append((UChar)kPatternDigit);
|
|
|
|
group.append((UChar)kPatternGroupingSeparator);
|
|
|
|
zero = (UChar32)kPatternZeroDigit;
|
|
|
|
}
|
2000-10-12 19:04:06 +00:00
|
|
|
if (fFormatWidth > 0) {
|
2001-09-17 21:50:19 +00:00
|
|
|
if (localized) {
|
|
|
|
padSpec.append(fSymbols->getSymbol(DecimalFormatSymbols::kPadEscapeSymbol));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
padSpec.append((UChar)kPatternPadEscape);
|
|
|
|
}
|
|
|
|
padSpec.append(fPad);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-10-12 19:04:06 +00:00
|
|
|
if (fRoundingIncrement != NULL) {
|
2001-09-17 21:50:19 +00:00
|
|
|
for(i=0; i<fRoundingIncrement->fCount; ++i) {
|
|
|
|
roundingDigits.append((UChar)fRoundingIncrement->fDigits[i]);
|
|
|
|
}
|
|
|
|
roundingDecimalPos = fRoundingIncrement->fDecimalAt;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
for (int32_t part=0; part<2; ++part) {
|
2000-08-07 23:06:28 +00:00
|
|
|
if (padPos == kPadBeforePrefix) {
|
|
|
|
result.append(padSpec);
|
|
|
|
}
|
|
|
|
appendAffix(result,
|
|
|
|
(part==0 ? fPosPrefixPattern : fNegPrefixPattern),
|
|
|
|
(part==0 ? fPositivePrefix : fNegativePrefix),
|
|
|
|
localized);
|
2001-09-27 22:46:58 +00:00
|
|
|
if (padPos == kPadAfterPrefix && ! padSpec.isEmpty()) {
|
2000-08-07 23:06:28 +00:00
|
|
|
result.append(padSpec);
|
|
|
|
}
|
|
|
|
int32_t sub0Start = result.length();
|
|
|
|
int32_t g = isGroupingUsed() ? uprv_max(0, fGroupingSize) : 0;
|
|
|
|
if (g > 0 && fGroupingSize2 > 0 && fGroupingSize2 != fGroupingSize) {
|
|
|
|
g += fGroupingSize2;
|
|
|
|
}
|
|
|
|
int32_t maxIntDig = fUseExponentialNotation ? getMaximumIntegerDigits() :
|
|
|
|
(uprv_max(uprv_max(g, getMinimumIntegerDigits()),
|
|
|
|
roundingDecimalPos) + 1);
|
|
|
|
for (i = maxIntDig; i > 0; --i) {
|
|
|
|
if (!fUseExponentialNotation && i<maxIntDig &&
|
|
|
|
isGroupingPosition(i)) {
|
|
|
|
result.append(group);
|
|
|
|
}
|
2001-09-27 22:46:58 +00:00
|
|
|
if (! roundingDigits.isEmpty()) {
|
2000-08-07 23:06:28 +00:00
|
|
|
int32_t pos = roundingDecimalPos - i;
|
|
|
|
if (pos >= 0 && pos < roundingDigits.length()) {
|
2001-09-18 20:40:48 +00:00
|
|
|
result.append((UChar) (roundingDigits.char32At(pos) - kPatternZeroDigit + zero));
|
2000-08-07 23:06:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2001-09-18 20:40:48 +00:00
|
|
|
if (i<=getMinimumIntegerDigits()) {
|
|
|
|
result.append(zero);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.append(digit);
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
|
|
|
if (getMaximumFractionDigits() > 0 || fDecimalSeparatorAlwaysShown) {
|
2001-09-17 21:50:19 +00:00
|
|
|
if (localized) {
|
|
|
|
result.append(fSymbols->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.append((UChar)kPatternDecimalSeparator);
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
|
|
|
int32_t pos = roundingDecimalPos;
|
|
|
|
for (i = 0; i < getMaximumFractionDigits(); ++i) {
|
2001-09-27 22:46:58 +00:00
|
|
|
if (! roundingDigits.isEmpty() && pos < roundingDigits.length()) {
|
2001-09-18 20:40:48 +00:00
|
|
|
if (pos < 0) {
|
|
|
|
result.append(zero);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.append((UChar)(roundingDigits.char32At(pos) - kPatternZeroDigit + zero));
|
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
++pos;
|
|
|
|
continue;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-09-18 22:17:21 +00:00
|
|
|
if (i<getMinimumFractionDigits()) {
|
|
|
|
result.append(zero);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.append(digit);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
if (fUseExponentialNotation) {
|
2001-09-17 21:50:19 +00:00
|
|
|
if (localized) {
|
|
|
|
result.append(fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.append((UChar)kPatternExponent);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
if (fExponentSignAlwaysShown) {
|
2001-09-17 21:50:19 +00:00
|
|
|
if (localized) {
|
|
|
|
result.append(fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.append((UChar)kPatternPlus);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
for (i=0; i<fMinExponentDigits; ++i) {
|
|
|
|
result.append(zero);
|
|
|
|
}
|
|
|
|
}
|
2001-09-27 22:46:58 +00:00
|
|
|
if (! padSpec.isEmpty() && !fUseExponentialNotation) {
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t add = fFormatWidth - result.length() + sub0Start
|
|
|
|
- ((part == 0)
|
|
|
|
? fPositivePrefix.length() + fPositiveSuffix.length()
|
|
|
|
: fNegativePrefix.length() + fNegativeSuffix.length());
|
|
|
|
while (add > 0) {
|
|
|
|
result.insert(sub0Start, digit);
|
2000-06-07 00:15:59 +00:00
|
|
|
++maxIntDig;
|
1999-08-16 21:50:52 +00:00
|
|
|
--add;
|
2000-06-07 00:15:59 +00:00
|
|
|
// Only add a grouping separator if we have at least
|
|
|
|
// 2 additional characters to be added, so we don't
|
|
|
|
// end up with ",###".
|
|
|
|
if (add>1 && isGroupingPosition(maxIntDig)) {
|
1999-08-16 21:50:52 +00:00
|
|
|
result.insert(sub0Start, group);
|
2000-06-07 00:15:59 +00:00
|
|
|
--add;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-09-27 22:46:58 +00:00
|
|
|
if (fPadPosition == kPadBeforeSuffix && ! padSpec.isEmpty()) {
|
1999-08-16 21:50:52 +00:00
|
|
|
result.append(padSpec);
|
|
|
|
}
|
|
|
|
if (part == 0) {
|
|
|
|
appendAffix(result, fPosSuffixPattern, fPositiveSuffix, localized);
|
2001-09-27 22:46:58 +00:00
|
|
|
if (fPadPosition == kPadAfterSuffix && ! padSpec.isEmpty()) {
|
1999-08-16 21:50:52 +00:00
|
|
|
result.append(padSpec);
|
|
|
|
}
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool isDefault = FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
if ((fNegSuffixPattern == fPosSuffixPattern && // both null
|
|
|
|
fNegativeSuffix == fPositiveSuffix)
|
|
|
|
|| (fNegSuffixPattern != 0 && fPosSuffixPattern != 0 &&
|
2001-09-17 21:50:19 +00:00
|
|
|
*fNegSuffixPattern == *fPosSuffixPattern))
|
|
|
|
{
|
|
|
|
if (fNegPrefixPattern != NULL && fPosPrefixPattern != NULL)
|
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t length = fPosPrefixPattern->length();
|
|
|
|
isDefault = fNegPrefixPattern->length() == (length+2) &&
|
|
|
|
(*fNegPrefixPattern)[(UTextOffset)0] == kQuote &&
|
|
|
|
(*fNegPrefixPattern)[(UTextOffset)1] == kPatternMinus &&
|
|
|
|
fNegPrefixPattern->compare(2, length, *fPosPrefixPattern, 0, length) == 0;
|
|
|
|
}
|
|
|
|
if (!isDefault &&
|
2001-09-17 21:50:19 +00:00
|
|
|
fNegPrefixPattern == NULL && fPosPrefixPattern == NULL)
|
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t length = fPositivePrefix.length();
|
|
|
|
isDefault = fNegativePrefix.length() == (length+1) &&
|
2001-09-17 21:50:19 +00:00
|
|
|
fNegativePrefix.compare(fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol)) == 0 &&
|
1999-08-16 21:50:52 +00:00
|
|
|
fNegativePrefix.compare(1, length, fPositivePrefix, 0, length) == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isDefault) {
|
|
|
|
break; // Don't output default negative subpattern
|
|
|
|
} else {
|
2001-09-17 21:50:19 +00:00
|
|
|
if (localized) {
|
|
|
|
result.append(fSymbols->getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.append((UChar)kPatternSeparator);
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
appendAffix(result, fNegSuffixPattern, fNegativeSuffix, localized);
|
2001-09-27 22:46:58 +00:00
|
|
|
if (fPadPosition == kPadAfterSuffix && ! padSpec.isEmpty()) {
|
1999-08-16 21:50:52 +00:00
|
|
|
result.append(padSpec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::applyPattern(const UnicodeString& pattern, UErrorCode& status)
|
|
|
|
{
|
2001-08-16 00:55:16 +00:00
|
|
|
UParseError parseError;
|
|
|
|
applyPattern(pattern, FALSE, parseError, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2000-08-07 23:06:28 +00:00
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::applyPattern(const UnicodeString& pattern,
|
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
|
|
|
applyPattern(pattern, FALSE, parseError, status);
|
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status)
|
|
|
|
{
|
2001-08-16 00:55:16 +00:00
|
|
|
UParseError parseError;
|
|
|
|
applyPattern(pattern, TRUE,parseError,status);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern,
|
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
|
|
|
applyPattern(pattern, TRUE,parseError,status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::applyPattern(const UnicodeString& pattern,
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool localized,
|
2001-08-16 00:55:16 +00:00
|
|
|
UParseError& parseError,
|
1999-08-16 21:50:52 +00:00
|
|
|
UErrorCode& status)
|
|
|
|
{
|
2000-10-12 19:04:06 +00:00
|
|
|
if (U_FAILURE(status))
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
2000-10-12 19:04:06 +00:00
|
|
|
return;
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
// Clear error struct
|
2001-11-15 21:13:37 +00:00
|
|
|
parseError.offset = -1;
|
2001-08-16 00:55:16 +00:00
|
|
|
parseError.preContext[0] = parseError.postContext[0] = (UChar)0;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// Set the significant pattern symbols
|
2001-09-17 21:50:19 +00:00
|
|
|
UChar32 zeroDigit = kPatternZeroDigit;
|
|
|
|
UnicodeString groupingSeparator ((UChar)kPatternGroupingSeparator);
|
|
|
|
UnicodeString decimalSeparator ((UChar)kPatternDecimalSeparator);
|
|
|
|
UnicodeString percent ((UChar)kPatternPercent);
|
|
|
|
UnicodeString perMill ((UChar)kPatternPerMill);
|
|
|
|
UnicodeString digit ((UChar)kPatternDigit);
|
|
|
|
UnicodeString separator ((UChar)kPatternSeparator);
|
|
|
|
UnicodeString exponent ((UChar)kPatternExponent);
|
|
|
|
UnicodeString plus ((UChar)kPatternPlus);
|
|
|
|
UnicodeString minus ((UChar)kPatternMinus);
|
|
|
|
UnicodeString padEscape ((UChar)kPatternPadEscape);
|
1999-08-16 21:50:52 +00:00
|
|
|
// Substitute with the localized symbols if necessary
|
|
|
|
if (localized) {
|
2001-09-17 21:50:19 +00:00
|
|
|
zeroDigit = fSymbols->getSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
|
|
|
|
groupingSeparator = fSymbols->getSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol);
|
|
|
|
decimalSeparator = fSymbols->getSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
|
|
|
|
percent = fSymbols->getSymbol(DecimalFormatSymbols::kPercentSymbol);
|
|
|
|
perMill = fSymbols->getSymbol(DecimalFormatSymbols::kPerMillSymbol);
|
|
|
|
digit = fSymbols->getSymbol(DecimalFormatSymbols::kDigitSymbol);
|
|
|
|
separator = fSymbols->getSymbol(DecimalFormatSymbols::kPatternSeparatorSymbol);
|
|
|
|
exponent = fSymbols->getSymbol(DecimalFormatSymbols::kExponentialSymbol);
|
|
|
|
plus = fSymbols->getSymbol(DecimalFormatSymbols::kPlusSignSymbol);
|
|
|
|
minus = fSymbols->getSymbol(DecimalFormatSymbols::kMinusSignSymbol);
|
|
|
|
padEscape = fSymbols->getSymbol(DecimalFormatSymbols::kPadEscapeSymbol);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-08-09 20:46:49 +00:00
|
|
|
UChar nineDigit = (UChar)(zeroDigit + 9);
|
2001-09-17 21:50:19 +00:00
|
|
|
int32_t digitLen = digit.length();
|
|
|
|
int32_t groupSepLen = groupingSeparator.length();
|
|
|
|
int32_t decimalSepLen = decimalSeparator.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-09-17 21:50:19 +00:00
|
|
|
UTextOffset pos = 0;
|
|
|
|
int32_t patLen = pattern.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
// Part 0 is the positive pattern. Part 1, if present, is the negative
|
|
|
|
// pattern.
|
2001-09-17 21:50:19 +00:00
|
|
|
for (int32_t part=0; part<2 && pos<patLen; ++part) {
|
1999-08-16 21:50:52 +00:00
|
|
|
// 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;
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// 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;
|
|
|
|
int8_t groupingCount = -1;
|
2000-06-07 00:15:59 +00:00
|
|
|
int8_t groupingCount2 = -1;
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t padPos = -1;
|
2001-09-17 21:50:19 +00:00
|
|
|
UnicodeString padChar;
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t roundingPos = -1;
|
|
|
|
DigitList roundingInc;
|
|
|
|
int8_t expDigits = -1;
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool expSignAlways = FALSE;
|
|
|
|
UBool isCurrency = FALSE;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// The affix is either the prefix or the suffix.
|
|
|
|
UnicodeString* affix = &prefix;
|
|
|
|
|
|
|
|
int32_t start = pos;
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool isPartDone = FALSE;
|
2001-09-17 21:50:19 +00:00
|
|
|
UChar32 ch;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-09-17 21:50:19 +00:00
|
|
|
for (; !isPartDone && pos < patLen; pos += UTF_NEED_MULTIPLE_UCHAR(ch)) {
|
|
|
|
// Todo: account for surrogate pairs
|
|
|
|
ch = pattern.char32At(pos);
|
1999-08-16 21:50:52 +00:00
|
|
|
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.
|
2001-09-17 21:50:19 +00:00
|
|
|
if (pattern.compare(pos, digitLen, digit) == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
if (zeroDigitCount > 0) {
|
|
|
|
++digitRightCount;
|
|
|
|
} else {
|
|
|
|
++digitLeftCount;
|
|
|
|
}
|
|
|
|
if (groupingCount >= 0 && decimalPos < 0) {
|
|
|
|
++groupingCount;
|
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
pos += digitLen;
|
1999-08-16 21:50:52 +00:00
|
|
|
} else if (ch >= zeroDigit && ch <= nineDigit) {
|
|
|
|
if (digitRightCount > 0) {
|
|
|
|
// Unexpected '0'
|
|
|
|
debug("Unexpected '0'")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_UNEXPECTED_TOKEN;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
++zeroDigitCount;
|
|
|
|
if (groupingCount >= 0 && decimalPos < 0) {
|
|
|
|
++groupingCount;
|
|
|
|
}
|
|
|
|
if (ch != zeroDigit && roundingPos < 0) {
|
|
|
|
roundingPos = digitLeftCount + zeroDigitCount;
|
|
|
|
}
|
|
|
|
if (roundingPos >= 0) {
|
|
|
|
roundingInc.append((char)(ch - zeroDigit + '0'));
|
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
pos++;
|
|
|
|
} else if (pattern.compare(pos, groupSepLen, groupingSeparator) == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
if (decimalPos >= 0) {
|
|
|
|
// Grouping separator after decimal
|
|
|
|
debug("Grouping separator after decimal")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_UNEXPECTED_TOKEN;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-06-07 00:15:59 +00:00
|
|
|
groupingCount2 = groupingCount;
|
1999-08-16 21:50:52 +00:00
|
|
|
groupingCount = 0;
|
2001-09-17 21:50:19 +00:00
|
|
|
pos += groupSepLen;
|
|
|
|
} else if (pattern.compare(pos, decimalSepLen, decimalSeparator) == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
if (decimalPos >= 0) {
|
|
|
|
// Multiple decimal separators
|
|
|
|
debug("Multiple decimal separators")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_MULTIPLE_DECIMAL_SEPERATORS;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
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;
|
2001-09-17 21:50:19 +00:00
|
|
|
pos += decimalSepLen;
|
1999-08-16 21:50:52 +00:00
|
|
|
} else {
|
2001-09-17 21:50:19 +00:00
|
|
|
if (pattern.compare(pos, exponent.length(), exponent) == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
if (expDigits >= 0) {
|
|
|
|
// Multiple exponential symbols
|
|
|
|
debug("Multiple exponential symbols")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_MULTIPLE_EXPONENTIAL_SYMBOLS;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (groupingCount >= 0) {
|
|
|
|
// Grouping separator in exponential pattern
|
|
|
|
debug("Grouping separator in exponential pattern")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_MALFORMED_EXPONENTIAL_PATTERN;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check for positive prefix
|
2001-09-17 21:50:19 +00:00
|
|
|
if ((pos+1) < patLen
|
|
|
|
&& pattern.compare((UTextOffset) (pos+1), plus.length(), plus) == 0)
|
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
expSignAlways = TRUE;
|
2001-09-17 21:50:19 +00:00
|
|
|
pos += plus.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
// Use lookahead to parse out the exponential part of the
|
|
|
|
// pattern, then jump into suffix subpart.
|
|
|
|
expDigits = 0;
|
2001-09-17 21:50:19 +00:00
|
|
|
pos += exponent.length() - 1;
|
|
|
|
while (++pos < patLen &&
|
|
|
|
pattern[(UTextOffset) pos] == zeroDigit)
|
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
++expDigits;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((digitLeftCount + zeroDigitCount) < 1 ||
|
|
|
|
expDigits < 1) {
|
|
|
|
// Malformed exponential pattern
|
|
|
|
debug("Malformed exponential pattern")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_MALFORMED_EXPONENTIAL_PATTERN;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Transition to suffix subpart
|
|
|
|
subpart = 2; // suffix subpart
|
|
|
|
affix = &suffix;
|
2001-09-17 21:50:19 +00:00
|
|
|
sub0Limit = pos;
|
1999-08-16 21:50:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1: // Prefix subpart
|
|
|
|
case 2: // Suffix subpart
|
|
|
|
// Process the prefix / suffix characters
|
|
|
|
// Process unquoted characters seen in prefix or suffix
|
|
|
|
// subpart.
|
2001-09-17 21:50:19 +00:00
|
|
|
if (pattern.compare(pos, digitLen, digit) == 0) {
|
|
|
|
// Any of these characters implicitly begins the
|
|
|
|
// next subpart if we are in the prefix
|
|
|
|
if (subpart == 1) { // prefix subpart
|
|
|
|
subpart = 0; // pattern proper subpart
|
|
|
|
sub0Start = pos; // Reprocess this character
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pos += digitLen;
|
|
|
|
// Fall through to append(ch)
|
|
|
|
} else if (pattern.compare(pos, groupSepLen, groupingSeparator) == 0) {
|
|
|
|
// Any of these characters implicitly begins the
|
|
|
|
// next subpart if we are in the prefix
|
|
|
|
if (subpart == 1) { // prefix subpart
|
|
|
|
subpart = 0; // pattern proper subpart
|
|
|
|
sub0Start = pos; // Reprocess this character
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pos += groupSepLen;
|
|
|
|
// Fall through to append(ch)
|
|
|
|
} else if (pattern.compare(pos, decimalSepLen, decimalSeparator) == 0) {
|
|
|
|
// Any of these characters implicitly begins the
|
|
|
|
// next subpart if we are in the prefix
|
|
|
|
if (subpart == 1) { // prefix subpart
|
|
|
|
subpart = 0; // pattern proper subpart
|
|
|
|
sub0Start = pos; // Reprocess this character
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pos += decimalSepLen;
|
|
|
|
// Fall through to append(ch)
|
|
|
|
} else if (ch >= zeroDigit && ch <= nineDigit) {
|
1999-08-16 21:50:52 +00:00
|
|
|
// Any of these characters implicitly begins the
|
|
|
|
// next subpart if we are in the prefix
|
|
|
|
if (subpart == 1) { // prefix subpart
|
|
|
|
subpart = 0; // pattern proper subpart
|
2001-09-17 21:50:19 +00:00
|
|
|
sub0Start = pos; // Reprocess this character
|
1999-08-16 21:50:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
pos++;
|
1999-08-16 21:50:52 +00:00
|
|
|
// Fall through to append(ch)
|
|
|
|
} else if (ch == kCurrencySign) {
|
|
|
|
// Use lookahead to determine if the currency sign is
|
|
|
|
// doubled or not.
|
2001-09-17 21:50:19 +00:00
|
|
|
pos++;
|
1999-08-16 21:50:52 +00:00
|
|
|
affix->append(kQuote); // Encode currency
|
2001-09-17 21:50:19 +00:00
|
|
|
if (pos < pattern.length() && pattern[pos] == kCurrencySign)
|
|
|
|
{
|
1999-08-16 21:50:52 +00:00
|
|
|
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.
|
2001-09-17 21:50:19 +00:00
|
|
|
++pos;
|
|
|
|
if (pos < pattern.length() && pattern[pos] == kQuote) {
|
1999-08-16 21:50:52 +00:00
|
|
|
affix->append(kQuote); // Encode quote
|
2001-09-17 21:50:19 +00:00
|
|
|
++pos;
|
1999-08-16 21:50:52 +00:00
|
|
|
// Fall through to append(ch)
|
|
|
|
} else {
|
|
|
|
subpart += 2; // open quote
|
|
|
|
continue;
|
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
} else if (pattern.compare(pos, separator.length(), separator) == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
// 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")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_UNEXPECTED_TOKEN;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sub2Limit = pos;
|
|
|
|
isPartDone = TRUE; // Go to next part
|
2001-09-17 21:50:19 +00:00
|
|
|
pos += separator.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
break;
|
2001-09-17 21:50:19 +00:00
|
|
|
} else if (pattern.compare(pos, percent.length(), percent) == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
// Next handle characters which are appended directly.
|
|
|
|
if (multiplier != 1) {
|
|
|
|
// Too many percent/perMill characters
|
2001-09-17 21:50:19 +00:00
|
|
|
debug("Too many percent characters")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_MULTIPLE_PERCENT_SYMBOLS;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
affix->append(kQuote); // Encode percent/perMill
|
2001-09-17 21:50:19 +00:00
|
|
|
multiplier = 100;
|
|
|
|
ch = kPatternPercent; // Use unlocalized pattern char
|
|
|
|
pos += percent.length();
|
|
|
|
// Fall through to append(ch)
|
|
|
|
} 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;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
affix->append(kQuote); // Encode percent/perMill
|
|
|
|
multiplier = 1000;
|
|
|
|
ch = kPatternPerMill; // Use unlocalized pattern char
|
|
|
|
pos += perMill.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
// Fall through to append(ch)
|
2001-09-17 21:50:19 +00:00
|
|
|
} else if (pattern.compare(pos, padEscape.length(), padEscape) == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
if (padPos >= 0 || // Multiple pad specifiers
|
1999-12-08 02:11:04 +00:00
|
|
|
(pos+1) == pattern.length()) { // Nothing after padEscape
|
1999-08-16 21:50:52 +00:00
|
|
|
debug("Multiple pad specifiers")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_MULTIPLE_PAD_SPECIFIERS;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
padPos = pos;
|
2001-09-20 21:34:19 +00:00
|
|
|
padChar = pattern.char32At(++pos);
|
|
|
|
pos += 1 + UTF_NEED_MULTIPLE_UCHAR(pattern.char32At(pos));
|
|
|
|
// pos += padEscape.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
continue;
|
2001-09-17 21:50:19 +00:00
|
|
|
} else if (pattern.compare(pos, minus.length(), minus) == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
affix->append(kQuote); // Encode minus
|
|
|
|
ch = kPatternMinus;
|
2001-09-17 21:50:19 +00:00
|
|
|
pos += minus.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
// Fall through to append(ch)
|
2001-09-17 21:50:19 +00:00
|
|
|
} else if (pattern.compare(pos, plus.length(), plus) == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
affix->append(kQuote); // Encode plus
|
|
|
|
ch = kPatternPlus;
|
2001-09-17 21:50:19 +00:00
|
|
|
pos += plus.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
// Fall through to append(ch)
|
2001-09-17 21:50:19 +00:00
|
|
|
} else {
|
|
|
|
pos++;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
// Unquoted, non-special characters fall through to here, as
|
|
|
|
// well as other code which needs to append something to the
|
|
|
|
// affix.
|
|
|
|
affix->append(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'.
|
2001-09-17 21:50:19 +00:00
|
|
|
pos++;
|
1999-08-16 21:50:52 +00:00
|
|
|
if (ch == kQuote) {
|
2001-09-17 21:50:19 +00:00
|
|
|
if (pos < pattern.length() && pattern[pos] == kQuote) {
|
1999-08-16 21:50:52 +00:00
|
|
|
++pos;
|
|
|
|
affix->append(kQuote); // Encode quote
|
|
|
|
// Fall through to append(ch)
|
|
|
|
} else {
|
|
|
|
subpart -= 2; // close quote
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
affix->append(ch);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sub0Limit == 0) {
|
1999-12-08 02:11:04 +00:00
|
|
|
sub0Limit = pattern.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sub2Limit == 0) {
|
1999-12-08 02:11:04 +00:00
|
|
|
sub2Limit = pattern.length();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 && digitLeftCount > 0 && decimalPos >= 0) {
|
|
|
|
// Handle "###.###" and "###." and ".###"
|
|
|
|
int n = decimalPos;
|
2000-10-12 19:04:06 +00:00
|
|
|
if (n == 0)
|
|
|
|
++n; // Handle ".###"
|
1999-08-16 21:50:52 +00:00
|
|
|
digitRightCount = digitLeftCount - n;
|
|
|
|
digitLeftCount = n - 1;
|
|
|
|
zeroDigitCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do syntax checking on the digits, decimal points, and quotes.
|
|
|
|
if ((decimalPos < 0 && digitRightCount > 0) ||
|
|
|
|
(decimalPos >= 0 &&
|
|
|
|
(decimalPos < digitLeftCount ||
|
|
|
|
decimalPos > (digitLeftCount + zeroDigitCount))) ||
|
2000-06-07 00:15:59 +00:00
|
|
|
groupingCount == 0 || groupingCount2 == 0 ||
|
2001-09-17 21:50:19 +00:00
|
|
|
subpart > 2)
|
|
|
|
{ // subpart > 2 == unmatched quote
|
1999-08-16 21:50:52 +00:00
|
|
|
debug("Syntax error")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_PATTERN_SYNTAX_ERROR;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
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")
|
2001-08-17 02:20:35 +00:00
|
|
|
status = U_ILLEGAL_PAD_POSITION;
|
|
|
|
syntaxError(pattern,pos,parseError);
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (part == 0) {
|
|
|
|
delete fPosPrefixPattern;
|
|
|
|
delete fPosSuffixPattern;
|
|
|
|
delete fNegPrefixPattern;
|
|
|
|
delete fNegSuffixPattern;
|
|
|
|
fPosPrefixPattern = new UnicodeString(prefix);
|
|
|
|
fPosSuffixPattern = new UnicodeString(suffix);
|
|
|
|
fNegPrefixPattern = 0;
|
|
|
|
fNegSuffixPattern = 0;
|
|
|
|
|
|
|
|
fUseExponentialNotation = (expDigits >= 0);
|
|
|
|
if (fUseExponentialNotation) {
|
|
|
|
fMinExponentDigits = expDigits;
|
|
|
|
fExponentSignAlwaysShown = expSignAlways;
|
|
|
|
}
|
|
|
|
fIsCurrencyFormat = isCurrency;
|
|
|
|
int 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.
|
|
|
|
int effectiveDecimalPos = decimalPos >= 0 ? decimalPos : digitTotalCount;
|
|
|
|
setMinimumIntegerDigits(effectiveDecimalPos - digitLeftCount);
|
|
|
|
setMaximumIntegerDigits(fUseExponentialNotation
|
|
|
|
? digitLeftCount + getMinimumIntegerDigits()
|
|
|
|
: kDoubleIntegerDigits);
|
|
|
|
setMaximumFractionDigits(decimalPos >= 0
|
|
|
|
? (digitTotalCount - decimalPos) : 0);
|
|
|
|
setMinimumFractionDigits(decimalPos >= 0
|
|
|
|
? (digitLeftCount + zeroDigitCount - decimalPos) : 0);
|
|
|
|
setGroupingUsed(groupingCount > 0);
|
|
|
|
fGroupingSize = (groupingCount > 0) ? groupingCount : 0;
|
2000-06-07 00:15:59 +00:00
|
|
|
fGroupingSize2 = (groupingCount2 > 0 && groupingCount2 != groupingCount)
|
|
|
|
? groupingCount2 : 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
fRoundingDouble = fRoundingIncrement->getDouble();
|
|
|
|
fRoundingMode = kRoundHalfEven;
|
|
|
|
} else {
|
|
|
|
setRoundingIncrement(0.0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fNegPrefixPattern = new UnicodeString(prefix);
|
|
|
|
fNegSuffixPattern = new UnicodeString(suffix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-08 02:11:04 +00:00
|
|
|
if (pattern.length() == 0) {
|
1999-08-16 21:50:52 +00:00
|
|
|
delete fNegPrefixPattern;
|
|
|
|
delete fNegSuffixPattern;
|
|
|
|
fNegPrefixPattern = NULL;
|
|
|
|
fNegSuffixPattern = NULL;
|
|
|
|
if (fPosPrefixPattern != NULL) {
|
|
|
|
fPosPrefixPattern->remove();
|
|
|
|
} else {
|
|
|
|
fPosPrefixPattern = new UnicodeString();
|
|
|
|
}
|
|
|
|
if (fPosSuffixPattern != NULL) {
|
|
|
|
fPosSuffixPattern->remove();
|
|
|
|
} else {
|
|
|
|
fPosSuffixPattern = new UnicodeString();
|
|
|
|
}
|
|
|
|
|
|
|
|
setMinimumIntegerDigits(0);
|
|
|
|
setMaximumIntegerDigits(kDoubleIntegerDigits);
|
|
|
|
setMinimumFractionDigits(0);
|
|
|
|
setMaximumFractionDigits(kDoubleFractionDigits);
|
|
|
|
|
|
|
|
fUseExponentialNotation = FALSE;
|
|
|
|
fIsCurrencyFormat = FALSE;
|
|
|
|
setGroupingUsed(FALSE);
|
|
|
|
fGroupingSize = 0;
|
2000-06-07 00:15:59 +00:00
|
|
|
fGroupingSize2 = 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
fMultiplier = 1;
|
|
|
|
setDecimalSeparatorAlwaysShown(FALSE);
|
|
|
|
fFormatWidth = 0;
|
|
|
|
setRoundingIncrement(0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there was no negative pattern, or if the negative pattern is
|
|
|
|
// identical to the positive pattern, then prepend the minus sign to the
|
|
|
|
// positive pattern to form the negative pattern.
|
|
|
|
if (fNegPrefixPattern == NULL ||
|
|
|
|
(*fNegPrefixPattern == *fPosPrefixPattern
|
|
|
|
&& *fNegSuffixPattern == *fPosSuffixPattern)) {
|
|
|
|
_copy_us_ptr(&fNegSuffixPattern, fPosSuffixPattern);
|
|
|
|
if (fNegPrefixPattern == NULL) {
|
|
|
|
fNegPrefixPattern = new UnicodeString();
|
|
|
|
} else {
|
|
|
|
fNegPrefixPattern->remove();
|
|
|
|
}
|
|
|
|
fNegPrefixPattern->append(kQuote).append(kPatternMinus)
|
|
|
|
.append(*fPosPrefixPattern);
|
|
|
|
}
|
2001-09-17 21:50:19 +00:00
|
|
|
#ifdef FMT_DEBUG
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString s;
|
|
|
|
s.append("\"").append(pattern).append("\"->");
|
|
|
|
debugout(s);
|
|
|
|
#endif
|
|
|
|
expandAffixes();
|
|
|
|
if (fFormatWidth > 0) {
|
|
|
|
// Finish computing format width (see above)
|
|
|
|
fFormatWidth += fPositivePrefix.length() + fPositiveSuffix.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the maximum number of digits allowed in the integer portion of a
|
|
|
|
* number. This override limits the integer digit count to 309.
|
|
|
|
* @see NumberFormat#setMaximumIntegerDigits
|
|
|
|
*/
|
|
|
|
void DecimalFormat::setMaximumIntegerDigits(int32_t newValue) {
|
1999-12-28 23:57:50 +00:00
|
|
|
NumberFormat::setMaximumIntegerDigits(uprv_min(newValue, kDoubleIntegerDigits));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the minimum number of digits allowed in the integer portion of a
|
|
|
|
* number. This override limits the integer digit count to 309.
|
|
|
|
* @see NumberFormat#setMinimumIntegerDigits
|
|
|
|
*/
|
|
|
|
void DecimalFormat::setMinimumIntegerDigits(int32_t newValue) {
|
1999-12-28 23:57:50 +00:00
|
|
|
NumberFormat::setMinimumIntegerDigits(uprv_min(newValue, kDoubleIntegerDigits));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the maximum number of digits allowed in the fraction portion of a
|
|
|
|
* number. This override limits the fraction digit count to 340.
|
|
|
|
* @see NumberFormat#setMaximumFractionDigits
|
|
|
|
*/
|
|
|
|
void DecimalFormat::setMaximumFractionDigits(int32_t newValue) {
|
1999-12-28 23:57:50 +00:00
|
|
|
NumberFormat::setMaximumFractionDigits(uprv_min(newValue, kDoubleFractionDigits));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the minimum number of digits allowed in the fraction portion of a
|
|
|
|
* number. This override limits the fraction digit count to 340.
|
|
|
|
* @see NumberFormat#setMinimumFractionDigits
|
|
|
|
*/
|
|
|
|
void DecimalFormat::setMinimumFractionDigits(int32_t newValue) {
|
1999-12-28 23:57:50 +00:00
|
|
|
NumberFormat::setMinimumFractionDigits(uprv_min(newValue, kDoubleFractionDigits));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//eof
|