2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-11-22 20:25:35 +00:00
|
|
|
*******************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1997-2015, International Business Machines Corporation and *
|
|
|
|
* others. All Rights Reserved. *
|
1999-11-22 20:25:35 +00:00
|
|
|
*******************************************************************************
|
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().
|
2010-01-28 19:39:24 +00:00
|
|
|
* 06/29/99 stephen Fixed operator= to copy fFormatWidth, fPad,
|
1999-08-16 21:50:52 +00:00
|
|
|
* fPadPosition
|
|
|
|
********************************************************************************
|
|
|
|
*/
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
#include "unicode/uniset.h"
|
2009-02-12 22:55:29 +00:00
|
|
|
#include "unicode/currpinf.h"
|
|
|
|
#include "unicode/plurrule.h"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/utf16.h"
|
2011-08-24 18:06:46 +00:00
|
|
|
#include "unicode/numsys.h"
|
2012-02-24 20:27:21 +00:00
|
|
|
#include "unicode/localpointer.h"
|
2017-01-12 18:35:18 +00:00
|
|
|
#include "unicode/ustring.h"
|
2011-03-12 14:57:18 +00:00
|
|
|
#include "uresimp.h"
|
2003-07-12 00:23:05 +00:00
|
|
|
#include "ucurrimp.h"
|
2010-05-20 21:16:44 +00:00
|
|
|
#include "charstr.h"
|
2011-04-26 06:39:29 +00:00
|
|
|
#include "patternprops.h"
|
2002-05-14 23:24:58 +00:00
|
|
|
#include "cstring.h"
|
2003-04-21 22:48:16 +00:00
|
|
|
#include "uassert.h"
|
2009-02-12 22:55:29 +00:00
|
|
|
#include "hash.h"
|
2011-05-04 12:23:42 +00:00
|
|
|
#include "decfmtst.h"
|
2013-07-23 20:02:13 +00:00
|
|
|
#include "plurrule_impl.h"
|
2013-10-17 18:44:44 +00:00
|
|
|
#include "decimalformatpattern.h"
|
2014-12-18 22:46:45 +00:00
|
|
|
#include "fmtableimp.h"
|
2015-08-25 23:31:01 +00:00
|
|
|
#include "decimfmtimpl.h"
|
|
|
|
#include "visibledigits.h"
|
2013-10-17 18:44:44 +00:00
|
|
|
|
2013-02-21 22:37:26 +00:00
|
|
|
/*
|
|
|
|
* On certain platforms, round is a macro defined in math.h
|
|
|
|
* This undefine is to avoid conflict between the macro and
|
|
|
|
* the function defined below.
|
|
|
|
*/
|
|
|
|
#ifdef round
|
|
|
|
#undef round
|
|
|
|
#endif
|
|
|
|
|
2013-06-20 22:09:42 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
2010-01-16 19:59:59 +00:00
|
|
|
|
2013-06-20 22:09:42 +00:00
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
static void _debugout(const char *f, int l, const UnicodeString& s) {
|
|
|
|
char buf[2000];
|
|
|
|
s.extract((int32_t) 0, s.length(), buf, "utf-8");
|
|
|
|
printf("%s:%d: %s\n", f,l, buf);
|
|
|
|
}
|
|
|
|
#define debugout(x) _debugout(__FILE__,__LINE__,x)
|
|
|
|
#define debug(x) printf("%s:%d: %s\n", __FILE__,__LINE__, x);
|
|
|
|
static const UnicodeString dbg_null("<NULL>","");
|
|
|
|
#define DEREFSTR(x) ((x!=NULL)?(*x):(dbg_null))
|
|
|
|
#else
|
|
|
|
#define debugout(x)
|
|
|
|
#define debug(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-01-16 19:59:59 +00:00
|
|
|
/* For currency parsing purose,
|
2010-01-28 19:39:24 +00:00
|
|
|
* Need to remember all prefix patterns and suffix patterns of
|
|
|
|
* every currency format pattern,
|
2010-01-16 19:59:59 +00:00
|
|
|
* including the pattern of default currecny style
|
|
|
|
* and plural currency style. And the patterns are set through applyPattern.
|
|
|
|
*/
|
|
|
|
struct AffixPatternsForCurrency : public UMemory {
|
|
|
|
// negative prefix pattern
|
|
|
|
UnicodeString negPrefixPatternForCurrency;
|
|
|
|
// negative suffix pattern
|
|
|
|
UnicodeString negSuffixPatternForCurrency;
|
|
|
|
// positive prefix pattern
|
|
|
|
UnicodeString posPrefixPatternForCurrency;
|
|
|
|
// positive suffix pattern
|
|
|
|
UnicodeString posSuffixPatternForCurrency;
|
|
|
|
int8_t patternType;
|
2010-01-28 19:39:24 +00:00
|
|
|
|
|
|
|
AffixPatternsForCurrency(const UnicodeString& negPrefix,
|
2010-01-16 19:59:59 +00:00
|
|
|
const UnicodeString& negSuffix,
|
|
|
|
const UnicodeString& posPrefix,
|
|
|
|
const UnicodeString& posSuffix,
|
|
|
|
int8_t type) {
|
|
|
|
negPrefixPatternForCurrency = negPrefix;
|
|
|
|
negSuffixPatternForCurrency = negSuffix;
|
|
|
|
posPrefixPatternForCurrency = posPrefix;
|
|
|
|
posSuffixPatternForCurrency = posSuffix;
|
|
|
|
patternType = type;
|
|
|
|
}
|
2013-06-20 22:09:42 +00:00
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
void dump() const {
|
|
|
|
debugout( UnicodeString("AffixPatternsForCurrency( -=\"") +
|
|
|
|
negPrefixPatternForCurrency + (UnicodeString)"\"/\"" +
|
|
|
|
negSuffixPatternForCurrency + (UnicodeString)"\" +=\"" +
|
|
|
|
posPrefixPatternForCurrency + (UnicodeString)"\"/\"" +
|
|
|
|
posSuffixPatternForCurrency + (UnicodeString)"\" )");
|
|
|
|
}
|
|
|
|
#endif
|
2010-01-16 19:59:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* affix for currency formatting when the currency sign in the pattern
|
2010-01-28 19:39:24 +00:00
|
|
|
* equals to 3, such as the pattern contains 3 currency sign or
|
2010-01-16 19:59:59 +00:00
|
|
|
* the formatter style is currency plural format style.
|
|
|
|
*/
|
|
|
|
struct AffixesForCurrency : public UMemory {
|
|
|
|
// negative prefix
|
|
|
|
UnicodeString negPrefixForCurrency;
|
|
|
|
// negative suffix
|
|
|
|
UnicodeString negSuffixForCurrency;
|
|
|
|
// positive prefix
|
|
|
|
UnicodeString posPrefixForCurrency;
|
|
|
|
// positive suffix
|
|
|
|
UnicodeString posSuffixForCurrency;
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2010-01-16 19:59:59 +00:00
|
|
|
int32_t formatWidth;
|
|
|
|
|
|
|
|
AffixesForCurrency(const UnicodeString& negPrefix,
|
|
|
|
const UnicodeString& negSuffix,
|
|
|
|
const UnicodeString& posPrefix,
|
|
|
|
const UnicodeString& posSuffix) {
|
|
|
|
negPrefixForCurrency = negPrefix;
|
|
|
|
negSuffixForCurrency = negSuffix;
|
|
|
|
posPrefixForCurrency = posPrefix;
|
|
|
|
posSuffixForCurrency = posSuffix;
|
|
|
|
}
|
2013-06-20 22:09:42 +00:00
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
void dump() const {
|
|
|
|
debugout( UnicodeString("AffixesForCurrency( -=\"") +
|
|
|
|
negPrefixForCurrency + (UnicodeString)"\"/\"" +
|
|
|
|
negSuffixForCurrency + (UnicodeString)"\" +=\"" +
|
|
|
|
posPrefixForCurrency + (UnicodeString)"\"/\"" +
|
|
|
|
posSuffixForCurrency + (UnicodeString)"\" )");
|
|
|
|
}
|
|
|
|
#endif
|
2010-01-16 19:59:59 +00:00
|
|
|
};
|
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
U_CDECL_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal ICU 4.2
|
|
|
|
*/
|
2010-01-16 19:59:59 +00:00
|
|
|
static UBool U_CALLCONV decimfmtAffixPatternValueComparator(UHashTok val1, UHashTok val2);
|
2009-02-12 22:55:29 +00:00
|
|
|
|
|
|
|
|
2010-01-16 19:59:59 +00:00
|
|
|
static UBool
|
2009-04-17 18:52:52 +00:00
|
|
|
U_CALLCONV decimfmtAffixPatternValueComparator(UHashTok val1, UHashTok val2) {
|
2010-01-28 19:39:24 +00:00
|
|
|
const AffixPatternsForCurrency* affix_1 =
|
2010-01-16 19:59:59 +00:00
|
|
|
(AffixPatternsForCurrency*)val1.pointer;
|
2010-01-28 19:39:24 +00:00
|
|
|
const AffixPatternsForCurrency* affix_2 =
|
2010-01-16 19:59:59 +00:00
|
|
|
(AffixPatternsForCurrency*)val2.pointer;
|
2010-01-28 19:39:24 +00:00
|
|
|
return affix_1->negPrefixPatternForCurrency ==
|
2009-02-12 22:55:29 +00:00
|
|
|
affix_2->negPrefixPatternForCurrency &&
|
2010-01-28 19:39:24 +00:00
|
|
|
affix_1->negSuffixPatternForCurrency ==
|
2009-02-12 22:55:29 +00:00
|
|
|
affix_2->negSuffixPatternForCurrency &&
|
2010-01-28 19:39:24 +00:00
|
|
|
affix_1->posPrefixPatternForCurrency ==
|
2009-02-12 22:55:29 +00:00
|
|
|
affix_2->posPrefixPatternForCurrency &&
|
2010-01-28 19:39:24 +00:00
|
|
|
affix_1->posSuffixPatternForCurrency ==
|
2009-03-10 20:53:27 +00:00
|
|
|
affix_2->posSuffixPatternForCurrency &&
|
|
|
|
affix_1->patternType == affix_2->patternType;
|
2009-02-12 22:55:29 +00:00
|
|
|
}
|
|
|
|
|
2009-12-14 05:57:34 +00:00
|
|
|
U_CDECL_END
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2009-12-17 22:15:20 +00:00
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// *****************************************************************************
|
|
|
|
// class DecimalFormat
|
|
|
|
// *****************************************************************************
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormat)
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Constants for characters used in programmatic (unlocalized) patterns.
|
2003-08-31 20:53:46 +00:00
|
|
|
#define kPatternZeroDigit ((UChar)0x0030) /*'0'*/
|
2004-03-25 17:42:28 +00:00
|
|
|
#define kPatternSignificantDigit ((UChar)0x0040) /*'@'*/
|
2003-08-31 20:53:46 +00:00
|
|
|
#define kPatternGroupingSeparator ((UChar)0x002C) /*','*/
|
|
|
|
#define kPatternDecimalSeparator ((UChar)0x002E) /*'.'*/
|
|
|
|
#define kPatternPerMill ((UChar)0x2030)
|
|
|
|
#define kPatternPercent ((UChar)0x0025) /*'%'*/
|
|
|
|
#define kPatternDigit ((UChar)0x0023) /*'#'*/
|
|
|
|
#define kPatternSeparator ((UChar)0x003B) /*';'*/
|
|
|
|
#define kPatternExponent ((UChar)0x0045) /*'E'*/
|
|
|
|
#define kPatternPlus ((UChar)0x002B) /*'+'*/
|
|
|
|
#define kPatternMinus ((UChar)0x002D) /*'-'*/
|
|
|
|
#define kPatternPadEscape ((UChar)0x002A) /*'*'*/
|
|
|
|
#define kQuote ((UChar)0x0027) /*'\''*/
|
|
|
|
/**
|
|
|
|
* The CURRENCY_SIGN is the standard Unicode symbol for currency. It
|
|
|
|
* is used in patterns and substitued with either the currency symbol,
|
|
|
|
* or if it is doubled, with the international currency symbol. If the
|
|
|
|
* CURRENCY_SIGN is seen in a pattern, then the decimal separator is
|
|
|
|
* replaced with the monetary decimal separator.
|
|
|
|
*/
|
2004-02-13 01:53:12 +00:00
|
|
|
#define kCurrencySign ((UChar)0x00A4)
|
|
|
|
#define kDefaultPad ((UChar)0x0020) /* */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
const int32_t DecimalFormat::kDoubleIntegerDigits = 309;
|
|
|
|
const int32_t DecimalFormat::kDoubleFractionDigits = 340;
|
|
|
|
|
2003-10-29 00:20:05 +00:00
|
|
|
const int32_t DecimalFormat::kMaxScientificIntegerDigits = 8;
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* These are the tags we expect to see in normal resource bundle files associated
|
|
|
|
* with a locale.
|
|
|
|
*/
|
2010-06-04 18:29:02 +00:00
|
|
|
const char DecimalFormat::fgNumberPatterns[]="NumberPatterns"; // Deprecated - not used
|
|
|
|
static const char fgNumberElements[]="NumberElements";
|
|
|
|
static const char fgLatn[]="latn";
|
|
|
|
static const char fgPatterns[]="patterns";
|
|
|
|
static const char fgDecimalFormat[]="decimalFormat";
|
|
|
|
static const char fgCurrencyFormat[]="currencyFormat";
|
2013-07-25 17:58:37 +00:00
|
|
|
|
2004-03-25 17:42:28 +00:00
|
|
|
inline int32_t _min(int32_t a, int32_t b) { return (a<b) ? a : b; }
|
|
|
|
inline int32_t _max(int32_t a, int32_t b) { return (a<b) ? b : a; }
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Constructs a DecimalFormat instance in the default locale.
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
DecimalFormat::DecimalFormat(UErrorCode& status) {
|
2013-05-31 23:50:15 +00:00
|
|
|
init();
|
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,
|
2010-02-26 02:29:00 +00:00
|
|
|
UErrorCode& status) {
|
2013-05-31 23:50:15 +00:00
|
|
|
init();
|
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,
|
2010-02-26 02:29:00 +00:00
|
|
|
UErrorCode& status) {
|
2013-05-31 23:50:15 +00:00
|
|
|
init();
|
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
|
|
|
}
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2001-08-16 00:55:16 +00:00
|
|
|
DecimalFormat::DecimalFormat( const UnicodeString& pattern,
|
|
|
|
DecimalFormatSymbols* symbolsToAdopt,
|
|
|
|
UParseError& parseErr,
|
2010-02-26 02:29:00 +00:00
|
|
|
UErrorCode& status) {
|
2013-05-31 23:50:15 +00:00
|
|
|
init();
|
2001-08-16 00:55:16 +00:00
|
|
|
if (symbolsToAdopt == NULL)
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
construct(status,parseErr, &pattern, symbolsToAdopt);
|
|
|
|
}
|
2010-02-26 02:29:00 +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 clone of the symbols.
|
2010-01-28 19:39:24 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
|
|
|
|
const DecimalFormatSymbols& symbols,
|
2010-02-26 02:29:00 +00:00
|
|
|
UErrorCode& status) {
|
2013-05-31 23:50:15 +00:00
|
|
|
init();
|
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
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Constructs a DecimalFormat instance with the specified number format
|
|
|
|
// pattern, the number format symbols, and the number format style.
|
|
|
|
// The created instance owns the clone of the symbols.
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
DecimalFormat::DecimalFormat(const UnicodeString& pattern,
|
|
|
|
DecimalFormatSymbols* symbolsToAdopt,
|
2011-04-29 00:00:47 +00:00
|
|
|
UNumberFormatStyle style,
|
2010-02-26 02:29:00 +00:00
|
|
|
UErrorCode& status) {
|
2013-05-31 23:50:15 +00:00
|
|
|
init();
|
2010-02-26 02:29:00 +00:00
|
|
|
fStyle = style;
|
2009-02-12 22:55:29 +00:00
|
|
|
UParseError parseError;
|
|
|
|
construct(status, parseError, &pattern, symbolsToAdopt);
|
|
|
|
}
|
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Common DecimalFormat initialization.
|
|
|
|
// Put all fields of an uninitialized object into a known state.
|
|
|
|
// Common code, shared by all constructors.
|
2013-05-31 23:50:15 +00:00
|
|
|
// Can not fail. Leave the object in good enough shape that the destructor
|
|
|
|
// or assignment operator can run successfully.
|
2010-02-26 02:29:00 +00:00
|
|
|
void
|
2013-05-31 23:50:15 +00:00
|
|
|
DecimalFormat::init() {
|
2012-10-12 19:52:43 +00:00
|
|
|
fBoolFlags.clear();
|
2011-04-29 00:00:47 +00:00
|
|
|
fStyle = UNUM_DECIMAL;
|
2010-02-26 02:29:00 +00:00
|
|
|
fAffixPatternsForCurrency = NULL;
|
|
|
|
fCurrencyPluralInfo = NULL;
|
2012-05-30 00:41:57 +00:00
|
|
|
#if UCONFIG_HAVE_PARSEALLINPUT
|
|
|
|
fParseAllInput = UNUM_MAYBE;
|
|
|
|
#endif
|
|
|
|
|
2013-05-31 23:50:15 +00:00
|
|
|
fStaticSets = NULL;
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl = NULL;
|
2010-02-26 02:29:00 +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
|
2013-05-31 23:50:15 +00:00
|
|
|
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,
|
2002-05-13 19:33:05 +00:00
|
|
|
DecimalFormatSymbols* symbolsToAdopt)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
LocalPointer<DecimalFormatSymbols> adoptedSymbols(symbolsToAdopt);
|
2000-10-12 19:04:06 +00:00
|
|
|
if (U_FAILURE(status))
|
|
|
|
return;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
if (adoptedSymbols.isNull())
|
2001-08-16 00:55:16 +00:00
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
adoptedSymbols.adoptInstead(
|
|
|
|
new DecimalFormatSymbols(Locale::getDefault(), status));
|
|
|
|
if (adoptedSymbols.isNull() && U_SUCCESS(status)) {
|
2002-06-29 09:31:05 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2015-08-25 23:31:01 +00:00
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
2002-06-29 09:31:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
2013-05-31 23:50:15 +00:00
|
|
|
fStaticSets = DecimalFormatStaticSets::getStaticSets(status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
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)
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
UErrorCode nsStatus = U_ZERO_ERROR;
|
|
|
|
LocalPointer<NumberingSystem> ns(
|
|
|
|
NumberingSystem::createInstance(nsStatus));
|
|
|
|
if (U_FAILURE(nsStatus)) {
|
|
|
|
status = nsStatus;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-09-02 19:08:28 +00:00
|
|
|
int32_t len = 0;
|
2011-08-24 18:06:46 +00:00
|
|
|
UResourceBundle *top = ures_open(NULL, Locale::getDefault().getName(), &status);
|
2000-05-15 18:39:17 +00:00
|
|
|
|
2011-08-24 18:06:46 +00:00
|
|
|
UResourceBundle *resource = ures_getByKeyWithFallback(top, fgNumberElements, NULL, &status);
|
|
|
|
resource = ures_getByKeyWithFallback(resource, ns->getName(), resource, &status);
|
2011-03-12 14:57:18 +00:00
|
|
|
resource = ures_getByKeyWithFallback(resource, fgPatterns, resource, &status);
|
|
|
|
const UChar *resStr = ures_getStringByKeyWithFallback(resource, fgDecimalFormat, &len, &status);
|
2011-08-24 18:06:46 +00:00
|
|
|
if ( status == U_MISSING_RESOURCE_ERROR && uprv_strcmp(fgLatn,ns->getName())) {
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
resource = ures_getByKeyWithFallback(top, fgNumberElements, resource, &status);
|
|
|
|
resource = ures_getByKeyWithFallback(resource, fgLatn, resource, &status);
|
|
|
|
resource = ures_getByKeyWithFallback(resource, fgPatterns, resource, &status);
|
|
|
|
resStr = ures_getStringByKeyWithFallback(resource, fgDecimalFormat, &len, &status);
|
|
|
|
}
|
2004-09-02 19:08:28 +00:00
|
|
|
str.setTo(TRUE, resStr, len);
|
1999-08-16 21:50:52 +00:00
|
|
|
pattern = &str;
|
2004-09-02 19:08:28 +00:00
|
|
|
ures_close(resource);
|
2011-08-24 18:06:46 +00:00
|
|
|
ures_close(top);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl = new DecimalFormatImpl(this, *pattern, adoptedSymbols.getAlias(), parseErr, status);
|
|
|
|
if (fImpl) {
|
|
|
|
adoptedSymbols.orphan();
|
|
|
|
} else if (U_SUCCESS(status)) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
}
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-08-30 03:22:38 +00:00
|
|
|
|
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
|
|
|
}
|
2002-05-13 19:33:05 +00:00
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
const UnicodeString* patternUsed;
|
|
|
|
UnicodeString currencyPluralPatternForOther;
|
|
|
|
// apply pattern
|
2011-04-29 00:00:47 +00:00
|
|
|
if (fStyle == UNUM_CURRENCY_PLURAL) {
|
2015-08-25 23:31:01 +00:00
|
|
|
fCurrencyPluralInfo = new CurrencyPluralInfo(fImpl->fSymbols->getLocale(), status);
|
2009-02-12 22:55:29 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the pattern used in format is not fixed until formatting,
|
2010-01-28 19:39:24 +00:00
|
|
|
// in which, the number is known and
|
2009-02-12 22:55:29 +00:00
|
|
|
// will be used to pick the right pattern based on plural count.
|
|
|
|
// Here, set the pattern as the pattern of plural count == "other".
|
|
|
|
// For most locale, the patterns are probably the same for all
|
|
|
|
// plural count. If not, the right pattern need to be re-applied
|
|
|
|
// during format.
|
2011-06-03 05:23:57 +00:00
|
|
|
fCurrencyPluralInfo->getCurrencyPluralPattern(UNICODE_STRING("other", 5), currencyPluralPatternForOther);
|
2015-08-25 23:31:01 +00:00
|
|
|
// TODO(refactor): Revisit, we are setting the pattern twice.
|
|
|
|
fImpl->applyPatternFavorCurrencyPrecision(
|
|
|
|
currencyPluralPatternForOther, status);
|
2009-02-12 22:55:29 +00:00
|
|
|
patternUsed = ¤cyPluralPatternForOther;
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
} else {
|
|
|
|
patternUsed = pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (patternUsed->indexOf(kCurrencySign) != -1) {
|
2010-01-28 19:39:24 +00:00
|
|
|
// initialize for currency, not only for plural format,
|
2009-02-12 22:55:29 +00:00
|
|
|
// but also for mix parsing
|
2016-09-15 03:55:22 +00:00
|
|
|
handleCurrencySignInPattern(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::handleCurrencySignInPattern(UErrorCode& status) {
|
|
|
|
// initialize for currency, not only for plural format,
|
|
|
|
// but also for mix parsing
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fCurrencyPluralInfo == NULL) {
|
|
|
|
fCurrencyPluralInfo = new CurrencyPluralInfo(fImpl->fSymbols->getLocale(), status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// need it for mix parsing
|
|
|
|
if (fAffixPatternsForCurrency == NULL) {
|
2009-02-12 22:55:29 +00:00
|
|
|
setupCurrencyAffixPatterns(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
static void
|
|
|
|
applyPatternWithNoSideEffects(
|
|
|
|
const UnicodeString& pattern,
|
|
|
|
UParseError& parseError,
|
|
|
|
UnicodeString &negPrefix,
|
|
|
|
UnicodeString &negSuffix,
|
|
|
|
UnicodeString &posPrefix,
|
|
|
|
UnicodeString &posSuffix,
|
|
|
|
UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DecimalFormatPatternParser patternParser;
|
|
|
|
DecimalFormatPattern out;
|
|
|
|
patternParser.applyPatternWithoutExpandAffix(
|
|
|
|
pattern,
|
|
|
|
out,
|
|
|
|
parseError,
|
|
|
|
status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
negPrefix = out.fNegPrefixPattern;
|
|
|
|
negSuffix = out.fNegSuffixPattern;
|
|
|
|
posPrefix = out.fPosPrefixPattern;
|
|
|
|
posSuffix = out.fPosSuffixPattern;
|
|
|
|
}
|
2009-02-12 22:55:29 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::setupCurrencyAffixPatterns(UErrorCode& status) {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
UParseError parseErr;
|
|
|
|
fAffixPatternsForCurrency = initHashForAffixPattern(status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
NumberingSystem *ns = NumberingSystem::createInstance(fImpl->fSymbols->getLocale(),status);
|
2011-08-30 03:22:38 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
2011-08-24 18:06:46 +00:00
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
// Save the default currency patterns of this locale.
|
|
|
|
// Here, chose onlyApplyPatternWithoutExpandAffix without
|
|
|
|
// expanding the affix patterns into affixes.
|
|
|
|
UnicodeString currencyPattern;
|
2010-06-04 18:29:02 +00:00
|
|
|
UErrorCode error = U_ZERO_ERROR;
|
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
UResourceBundle *resource = ures_open(NULL, fImpl->fSymbols->getLocale().getName(), &error);
|
2011-08-24 18:06:46 +00:00
|
|
|
UResourceBundle *numElements = ures_getByKeyWithFallback(resource, fgNumberElements, NULL, &error);
|
|
|
|
resource = ures_getByKeyWithFallback(numElements, ns->getName(), resource, &error);
|
2011-03-12 14:57:18 +00:00
|
|
|
resource = ures_getByKeyWithFallback(resource, fgPatterns, resource, &error);
|
2009-02-12 22:55:29 +00:00
|
|
|
int32_t patLen = 0;
|
2011-03-12 14:57:18 +00:00
|
|
|
const UChar *patResStr = ures_getStringByKeyWithFallback(resource, fgCurrencyFormat, &patLen, &error);
|
2011-08-24 18:06:46 +00:00
|
|
|
if ( error == U_MISSING_RESOURCE_ERROR && uprv_strcmp(ns->getName(),fgLatn)) {
|
|
|
|
error = U_ZERO_ERROR;
|
|
|
|
resource = ures_getByKeyWithFallback(numElements, fgLatn, resource, &error);
|
|
|
|
resource = ures_getByKeyWithFallback(resource, fgPatterns, resource, &error);
|
|
|
|
patResStr = ures_getStringByKeyWithFallback(resource, fgCurrencyFormat, &patLen, &error);
|
|
|
|
}
|
|
|
|
ures_close(numElements);
|
2009-02-12 22:55:29 +00:00
|
|
|
ures_close(resource);
|
2011-08-30 03:22:38 +00:00
|
|
|
delete ns;
|
2009-02-12 22:55:29 +00:00
|
|
|
|
|
|
|
if (U_SUCCESS(error)) {
|
2015-08-25 23:31:01 +00:00
|
|
|
UnicodeString negPrefix;
|
|
|
|
UnicodeString negSuffix;
|
|
|
|
UnicodeString posPrefix;
|
|
|
|
UnicodeString posSuffix;
|
|
|
|
applyPatternWithNoSideEffects(UnicodeString(patResStr, patLen),
|
|
|
|
parseErr,
|
|
|
|
negPrefix, negSuffix, posPrefix, posSuffix, status);
|
2009-02-12 22:55:29 +00:00
|
|
|
AffixPatternsForCurrency* affixPtn = new AffixPatternsForCurrency(
|
2015-08-25 23:31:01 +00:00
|
|
|
negPrefix,
|
|
|
|
negSuffix,
|
|
|
|
posPrefix,
|
|
|
|
posSuffix,
|
2009-03-10 20:53:27 +00:00
|
|
|
UCURR_SYMBOL_NAME);
|
2011-06-03 05:23:57 +00:00
|
|
|
fAffixPatternsForCurrency->put(UNICODE_STRING("default", 7), affixPtn, status);
|
2009-02-12 22:55:29 +00:00
|
|
|
}
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
// save the unique currency plural patterns of this locale.
|
|
|
|
Hashtable* pluralPtn = fCurrencyPluralInfo->fPluralCountToCurrencyUnitPattern;
|
|
|
|
const UHashElement* element = NULL;
|
2014-12-09 23:54:56 +00:00
|
|
|
int32_t pos = UHASH_FIRST;
|
2009-02-12 22:55:29 +00:00
|
|
|
Hashtable pluralPatternSet;
|
|
|
|
while ((element = pluralPtn->nextElement(pos)) != NULL) {
|
|
|
|
const UHashTok valueTok = element->value;
|
|
|
|
const UnicodeString* value = (UnicodeString*)valueTok.pointer;
|
|
|
|
const UHashTok keyTok = element->key;
|
|
|
|
const UnicodeString* key = (UnicodeString*)keyTok.pointer;
|
|
|
|
if (pluralPatternSet.geti(*value) != 1) {
|
2015-08-25 23:31:01 +00:00
|
|
|
UnicodeString negPrefix;
|
|
|
|
UnicodeString negSuffix;
|
|
|
|
UnicodeString posPrefix;
|
|
|
|
UnicodeString posSuffix;
|
2009-02-12 22:55:29 +00:00
|
|
|
pluralPatternSet.puti(*value, 1, status);
|
2015-08-25 23:31:01 +00:00
|
|
|
applyPatternWithNoSideEffects(
|
|
|
|
*value, parseErr,
|
|
|
|
negPrefix, negSuffix, posPrefix, posSuffix, status);
|
2009-02-12 22:55:29 +00:00
|
|
|
AffixPatternsForCurrency* affixPtn = new AffixPatternsForCurrency(
|
2015-08-25 23:31:01 +00:00
|
|
|
negPrefix,
|
|
|
|
negSuffix,
|
|
|
|
posPrefix,
|
|
|
|
posSuffix,
|
2009-03-10 20:53:27 +00:00
|
|
|
UCURR_LONG_NAME);
|
2009-02-12 22:55:29 +00:00
|
|
|
fAffixPatternsForCurrency->put(*key, affixPtn, status);
|
|
|
|
}
|
2006-05-03 00:00:34 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
DecimalFormat::~DecimalFormat()
|
|
|
|
{
|
2009-02-12 22:55:29 +00:00
|
|
|
deleteHashForAffixPattern();
|
|
|
|
delete fCurrencyPluralInfo;
|
2015-08-25 23:31:01 +00:00
|
|
|
delete fImpl;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// copy constructor
|
2000-08-07 23:06:28 +00:00
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
DecimalFormat::DecimalFormat(const DecimalFormat &source) :
|
|
|
|
NumberFormat(source) {
|
2013-05-31 23:50:15 +00:00
|
|
|
init();
|
1999-08-16 21:50:52 +00:00
|
|
|
*this = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// assignment operator
|
|
|
|
|
2012-09-27 18:50:24 +00:00
|
|
|
template <class T>
|
|
|
|
static void _clone_ptr(T** pdest, const T* source) {
|
2012-10-12 23:18:49 +00:00
|
|
|
delete *pdest;
|
2012-09-27 18:50:24 +00:00
|
|
|
if (source == NULL) {
|
2012-10-12 23:18:49 +00:00
|
|
|
*pdest = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
} else {
|
2012-10-12 23:18:49 +00:00
|
|
|
*pdest = static_cast<T*>(source->clone());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DecimalFormat&
|
|
|
|
DecimalFormat::operator=(const DecimalFormat& rhs)
|
|
|
|
{
|
2004-04-03 17:56:14 +00:00
|
|
|
if(this != &rhs) {
|
2013-05-31 23:50:15 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2004-04-03 17:56:14 +00:00
|
|
|
NumberFormat::operator=(rhs);
|
2015-08-25 23:31:01 +00:00
|
|
|
if (fImpl == NULL) {
|
|
|
|
fImpl = new DecimalFormatImpl(this, *rhs.fImpl, status);
|
|
|
|
} else {
|
|
|
|
fImpl->assign(*rhs.fImpl, status);
|
|
|
|
}
|
2013-05-31 23:50:15 +00:00
|
|
|
fStaticSets = DecimalFormatStaticSets::getStaticSets(status);
|
2009-02-12 22:55:29 +00:00
|
|
|
fStyle = rhs.fStyle;
|
2012-09-27 18:50:24 +00:00
|
|
|
_clone_ptr(&fCurrencyPluralInfo, rhs.fCurrencyPluralInfo);
|
|
|
|
deleteHashForAffixPattern();
|
2009-02-12 22:55:29 +00:00
|
|
|
if (rhs.fAffixPatternsForCurrency) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
fAffixPatternsForCurrency = initHashForAffixPattern(status);
|
2010-01-28 19:39:24 +00:00
|
|
|
copyHashForAffixPattern(rhs.fAffixPatternsForCurrency,
|
2009-02-12 22:55:29 +00:00
|
|
|
fAffixPatternsForCurrency, status);
|
|
|
|
}
|
2014-03-05 23:11:07 +00:00
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
|
2004-04-03 17:56:14 +00:00
|
|
|
return *this;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
|
2004-02-13 01:53:12 +00:00
|
|
|
// NumberFormat::operator== guarantees this cast is safe
|
1999-08-16 21:50:52 +00:00
|
|
|
const DecimalFormat* other = (DecimalFormat*)&that;
|
|
|
|
|
2014-03-12 02:44:16 +00:00
|
|
|
return (
|
|
|
|
NumberFormat::operator==(that) &&
|
2014-03-04 05:54:16 +00:00
|
|
|
fBoolFlags.getAll() == other->fBoolFlags.getAll() &&
|
2015-08-25 23:31:01 +00:00
|
|
|
*fImpl == *other->fImpl);
|
2014-03-12 02:44:16 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Format*
|
|
|
|
DecimalFormat::clone() const
|
|
|
|
{
|
|
|
|
return new DecimalFormat(*this);
|
|
|
|
}
|
|
|
|
|
2013-09-04 01:07:35 +00:00
|
|
|
|
|
|
|
FixedDecimal
|
2013-09-05 23:28:28 +00:00
|
|
|
DecimalFormat::getFixedDecimal(double number, UErrorCode &status) const {
|
2015-08-25 23:31:01 +00:00
|
|
|
VisibleDigitsWithExponent digits;
|
|
|
|
initVisibleDigitsWithExponent(number, digits, status);
|
2013-09-06 20:02:42 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2015-08-25 23:31:01 +00:00
|
|
|
return FixedDecimal();
|
2013-09-06 20:02:42 +00:00
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
return FixedDecimal(digits.getMantissa());
|
|
|
|
}
|
2013-09-06 20:02:42 +00:00
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
VisibleDigitsWithExponent &
|
|
|
|
DecimalFormat::initVisibleDigitsWithExponent(
|
|
|
|
double number,
|
|
|
|
VisibleDigitsWithExponent &digits,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
return fImpl->initVisibleDigitsWithExponent(number, digits, status);
|
2013-09-04 01:07:35 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 01:17:28 +00:00
|
|
|
FixedDecimal
|
|
|
|
DecimalFormat::getFixedDecimal(const Formattable &number, UErrorCode &status) const {
|
2015-08-25 23:31:01 +00:00
|
|
|
VisibleDigitsWithExponent digits;
|
|
|
|
initVisibleDigitsWithExponent(number, digits, status);
|
2013-09-11 01:17:28 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return FixedDecimal();
|
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
return FixedDecimal(digits.getMantissa());
|
|
|
|
}
|
|
|
|
|
|
|
|
VisibleDigitsWithExponent &
|
|
|
|
DecimalFormat::initVisibleDigitsWithExponent(
|
|
|
|
const Formattable &number,
|
|
|
|
VisibleDigitsWithExponent &digits,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return digits;
|
|
|
|
}
|
2013-09-11 01:17:28 +00:00
|
|
|
if (!number.isNumeric()) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2015-08-25 23:31:01 +00:00
|
|
|
return digits;
|
2013-09-11 01:17:28 +00:00
|
|
|
}
|
2013-09-12 01:00:04 +00:00
|
|
|
|
|
|
|
DigitList *dl = number.getDigitList();
|
|
|
|
if (dl != NULL) {
|
2015-08-25 23:31:01 +00:00
|
|
|
DigitList dlCopy(*dl);
|
|
|
|
return fImpl->initVisibleDigitsWithExponent(
|
|
|
|
dlCopy, digits, status);
|
2013-09-12 01:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Formattable::Type type = number.getType();
|
2013-09-13 17:08:58 +00:00
|
|
|
if (type == Formattable::kDouble || type == Formattable::kLong) {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->initVisibleDigitsWithExponent(
|
|
|
|
number.getDouble(status), digits, status);
|
2013-09-13 17:08:58 +00:00
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->initVisibleDigitsWithExponent(
|
|
|
|
number.getInt64(), digits, status);
|
2013-09-12 01:00:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create a fixed decimal from a DigitList.
|
|
|
|
// The digit list may be modified.
|
|
|
|
// Internal function only.
|
|
|
|
FixedDecimal
|
|
|
|
DecimalFormat::getFixedDecimal(DigitList &number, UErrorCode &status) const {
|
2015-08-25 23:31:01 +00:00
|
|
|
VisibleDigitsWithExponent digits;
|
|
|
|
initVisibleDigitsWithExponent(number, digits, status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return FixedDecimal();
|
2013-09-12 01:00:04 +00:00
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
return FixedDecimal(digits.getMantissa());
|
|
|
|
}
|
2013-09-12 01:00:04 +00:00
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
VisibleDigitsWithExponent &
|
|
|
|
DecimalFormat::initVisibleDigitsWithExponent(
|
|
|
|
DigitList &number,
|
|
|
|
VisibleDigitsWithExponent &digits,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
return fImpl->initVisibleDigitsWithExponent(
|
|
|
|
number, digits, status);
|
2013-09-11 01:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2010-01-28 19:39:24 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(int32_t number,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition& fieldPosition) const
|
2003-10-31 22:47:25 +00:00
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
return fImpl->format(number, appendTo, fieldPosition, status);
|
2003-10-31 22:47:25 +00:00
|
|
|
}
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2012-10-12 19:52:43 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(int32_t number,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& fieldPosition,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, appendTo, fieldPosition, status);
|
2012-10-12 19:52:43 +00:00
|
|
|
}
|
|
|
|
|
2009-12-17 22:15:20 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(int32_t number,
|
|
|
|
UnicodeString& appendTo,
|
2010-01-28 19:39:24 +00:00
|
|
|
FieldPositionIterator* posIter,
|
2009-12-17 22:15:20 +00:00
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, appendTo, posIter, status);
|
2009-12-17 22:15:20 +00:00
|
|
|
}
|
2003-10-31 22:47:25 +00:00
|
|
|
|
2012-05-30 00:41:57 +00:00
|
|
|
|
2003-10-31 22:47:25 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2003-10-31 22:47:25 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(int64_t number,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& fieldPosition) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2012-10-12 19:52:43 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR; /* ignored */
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, appendTo, fieldPosition, status);
|
2012-10-12 19:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(int64_t number,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& fieldPosition,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, appendTo, fieldPosition, status);
|
2009-12-17 22:15:20 +00:00
|
|
|
}
|
2000-11-02 02:42:40 +00:00
|
|
|
|
2009-12-17 22:15:20 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(int64_t number,
|
|
|
|
UnicodeString& appendTo,
|
2010-01-28 19:39:24 +00:00
|
|
|
FieldPositionIterator* posIter,
|
2009-12-17 22:15:20 +00:00
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, appendTo, posIter, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-01-28 19:39:24 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format( double number,
|
2002-11-23 01:11:50 +00:00
|
|
|
UnicodeString& appendTo,
|
1999-08-16 21:50:52 +00:00
|
|
|
FieldPosition& fieldPosition) const
|
|
|
|
{
|
2012-10-12 19:52:43 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR; /* ignored */
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, appendTo, fieldPosition, status);
|
2012-10-12 19:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format( double number,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& fieldPosition,
|
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, appendTo, fieldPosition, status);
|
2009-12-17 22:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format( double number,
|
|
|
|
UnicodeString& appendTo,
|
2010-01-28 19:39:24 +00:00
|
|
|
FieldPositionIterator* posIter,
|
2009-12-17 22:15:20 +00:00
|
|
|
UErrorCode& status) const
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, appendTo, posIter, status);
|
2010-02-26 02:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
UnicodeString&
|
2016-08-17 19:07:15 +00:00
|
|
|
DecimalFormat::format(StringPiece number,
|
2010-02-26 02:29:00 +00:00
|
|
|
UnicodeString &toAppendTo,
|
|
|
|
FieldPositionIterator *posIter,
|
|
|
|
UErrorCode &status) const
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, toAppendTo, posIter, status);
|
|
|
|
}
|
2010-02-26 02:29:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(const DigitList &number,
|
|
|
|
UnicodeString &appendTo,
|
|
|
|
FieldPositionIterator *posIter,
|
|
|
|
UErrorCode &status) const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, appendTo, posIter, status);
|
2010-02-26 02:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::format(const DigitList &number,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& pos,
|
|
|
|
UErrorCode &status) const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->format(number, appendTo, pos, status);
|
2012-11-14 18:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UnicodeString&
|
2015-08-25 23:31:01 +00:00
|
|
|
DecimalFormat::format(const VisibleDigitsWithExponent &number,
|
|
|
|
UnicodeString &appendTo,
|
|
|
|
FieldPositionIterator *posIter,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
return fImpl->format(number, appendTo, posIter, status);
|
2000-06-07 00:15:59 +00:00
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
UnicodeString&
|
2015-08-25 23:31:01 +00:00
|
|
|
DecimalFormat::format(const VisibleDigitsWithExponent &number,
|
|
|
|
UnicodeString& appendTo,
|
|
|
|
FieldPosition& pos,
|
|
|
|
UErrorCode &status) const {
|
|
|
|
return fImpl->format(number, appendTo, pos, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
DigitList&
|
|
|
|
DecimalFormat::_round(const DigitList& number, DigitList& adjustedNum, UBool& isNegative, UErrorCode& status) const {
|
|
|
|
adjustedNum = number;
|
|
|
|
fImpl->round(adjustedNum, status);
|
|
|
|
isNegative = !adjustedNum.isPositive();
|
|
|
|
return adjustedNum;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::parse(const UnicodeString& text,
|
|
|
|
Formattable& result,
|
2004-04-15 06:20:42 +00:00
|
|
|
ParsePosition& parsePosition) const {
|
2012-02-19 06:45:47 +00:00
|
|
|
parse(text, result, parsePosition, NULL);
|
2004-04-15 06:20:42 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 06:45:47 +00:00
|
|
|
CurrencyAmount* DecimalFormat::parseCurrency(const UnicodeString& text,
|
|
|
|
ParsePosition& pos) const {
|
|
|
|
Formattable parseResult;
|
|
|
|
int32_t start = pos.getIndex();
|
2013-02-25 23:56:37 +00:00
|
|
|
UChar curbuf[4] = {};
|
2012-02-19 06:45:47 +00:00
|
|
|
parse(text, parseResult, pos, curbuf);
|
2012-02-24 20:27:21 +00:00
|
|
|
if (pos.getIndex() != start) {
|
2012-02-19 06:45:47 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
2014-11-20 00:27:17 +00:00
|
|
|
LocalPointer<CurrencyAmount> currAmt(new CurrencyAmount(parseResult, curbuf, ec), ec);
|
2012-02-24 20:27:21 +00:00
|
|
|
if (U_FAILURE(ec)) {
|
2012-02-19 06:45:47 +00:00
|
|
|
pos.setIndex(start); // indicate failure
|
2012-02-24 20:27:21 +00:00
|
|
|
} else {
|
2012-02-24 22:54:57 +00:00
|
|
|
return currAmt.orphan();
|
2012-02-19 06:45:47 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-24 22:54:57 +00:00
|
|
|
return NULL;
|
2004-04-15 06:20:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-02-19 06:45:47 +00:00
|
|
|
* Parses the given text as a number, optionally providing a currency amount.
|
2004-04-15 06:20:42 +00:00
|
|
|
* @param text the string to parse
|
2012-02-19 06:45:47 +00:00
|
|
|
* @param result output parameter for the numeric result.
|
2004-04-15 06:20:42 +00:00
|
|
|
* @param parsePosition input-output position; on input, the
|
|
|
|
* position within text to match; must have 0 <= pos.getIndex() <
|
|
|
|
* text.length(); on output, the position after the last matched
|
|
|
|
* character. If the parse fails, the position in unchanged upon
|
|
|
|
* output.
|
2012-02-19 06:45:47 +00:00
|
|
|
* @param currency if non-NULL, it should point to a 4-UChar buffer.
|
|
|
|
* In this case the text is parsed as a currency format, and the
|
|
|
|
* ISO 4217 code for the parsed currency is put into the buffer.
|
|
|
|
* Otherwise the text is parsed as a non-currency format.
|
2004-04-15 06:20:42 +00:00
|
|
|
*/
|
|
|
|
void DecimalFormat::parse(const UnicodeString& text,
|
|
|
|
Formattable& result,
|
|
|
|
ParsePosition& parsePosition,
|
2012-02-19 06:45:47 +00:00
|
|
|
UChar* currency) const {
|
2012-02-14 07:05:58 +00:00
|
|
|
int32_t startIdx, backup;
|
|
|
|
int32_t i = startIdx = backup = parsePosition.getIndex();
|
2000-11-02 02:42:40 +00:00
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
// clear any old contents in the result. In particular, clears any DigitList
|
|
|
|
// that it may be holding.
|
|
|
|
result.setLong(0);
|
2013-08-30 22:16:21 +00:00
|
|
|
if (currency != NULL) {
|
|
|
|
for (int32_t ci=0; ci<4; ci++) {
|
|
|
|
currency[ci] = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-02-26 02:29:00 +00:00
|
|
|
|
2003-04-21 19:18:27 +00:00
|
|
|
// Handle NaN as a special case:
|
2015-08-25 23:31:01 +00:00
|
|
|
int32_t formatWidth = fImpl->getOldFormatWidth();
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2003-04-21 19:18:27 +00:00
|
|
|
// Skip padding characters, if around prefix
|
2015-08-25 23:31:01 +00:00
|
|
|
if (formatWidth > 0 && (
|
|
|
|
fImpl->fAffixes.fPadPosition == DigitAffixesAndPadding::kPadBeforePrefix ||
|
|
|
|
fImpl->fAffixes.fPadPosition == DigitAffixesAndPadding::kPadAfterPrefix)) {
|
2003-04-21 19:18:27 +00:00
|
|
|
i = skipPadding(text, i);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-05-04 12:23:42 +00:00
|
|
|
|
|
|
|
if (isLenient()) {
|
|
|
|
// skip any leading whitespace
|
|
|
|
i = backup = skipUWhiteSpace(text, i);
|
|
|
|
}
|
|
|
|
|
2000-10-24 16:09:39 +00:00
|
|
|
// If the text is composed of the representation of NaN, returns NaN.length
|
2015-08-25 23:31:01 +00:00
|
|
|
const UnicodeString *nan = &fImpl->getConstSymbol(DecimalFormatSymbols::kNaNSymbol);
|
2003-04-21 19:18:27 +00:00
|
|
|
int32_t nanLen = (text.compare(i, nan->length(), *nan)
|
|
|
|
? 0 : nan->length());
|
2000-08-07 23:06:28 +00:00
|
|
|
if (nanLen) {
|
2003-04-21 19:18:27 +00:00
|
|
|
i += nanLen;
|
2015-08-25 23:31:01 +00:00
|
|
|
if (formatWidth > 0 && (fImpl->fAffixes.fPadPosition == DigitAffixesAndPadding::kPadBeforeSuffix || fImpl->fAffixes.fPadPosition == DigitAffixesAndPadding::kPadAfterSuffix)) {
|
2003-04-21 19:18:27 +00:00
|
|
|
i = skipPadding(text, i);
|
|
|
|
}
|
|
|
|
parsePosition.setIndex(i);
|
1999-12-28 23:57:50 +00:00
|
|
|
result.setDouble(uprv_getNaN());
|
1999-08-16 21:50:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2003-04-21 19:18:27 +00:00
|
|
|
// NaN parse failed; start over
|
|
|
|
i = backup;
|
2011-05-04 12:23:42 +00:00
|
|
|
parsePosition.setIndex(i);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
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];
|
2012-05-30 00:41:57 +00:00
|
|
|
|
|
|
|
DigitList *digits = result.getInternalDigitList(); // get one from the stack buffer
|
2010-02-26 02:29:00 +00:00
|
|
|
if (digits == NULL) {
|
|
|
|
return; // no way to report error from here.
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
if (fImpl->fMonetary) {
|
2010-02-26 02:29:00 +00:00
|
|
|
if (!parseForCurrency(text, parsePosition, *digits,
|
2009-02-12 22:55:29 +00:00
|
|
|
status, currency)) {
|
2012-05-30 00:41:57 +00:00
|
|
|
return;
|
2009-02-12 22:55:29 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-01-28 19:39:24 +00:00
|
|
|
if (!subparse(text,
|
2015-08-25 23:31:01 +00:00
|
|
|
&fImpl->fAffixes.fNegativePrefix.getOtherVariant().toString(),
|
|
|
|
&fImpl->fAffixes.fNegativeSuffix.getOtherVariant().toString(),
|
|
|
|
&fImpl->fAffixes.fPositivePrefix.getOtherVariant().toString(),
|
|
|
|
&fImpl->fAffixes.fPositiveSuffix.getOtherVariant().toString(),
|
2009-03-10 20:53:27 +00:00
|
|
|
FALSE, UCURR_SYMBOL_NAME,
|
2010-02-26 02:29:00 +00:00
|
|
|
parsePosition, *digits, status, currency)) {
|
2012-05-30 00:41:57 +00:00
|
|
|
debug("!subparse(...) - rewind");
|
2012-02-14 07:05:58 +00:00
|
|
|
parsePosition.setIndex(startIdx);
|
2009-02-12 22:55:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-11-02 02:42:40 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
// Handle infinity
|
|
|
|
if (status[fgStatusInfinite]) {
|
1999-12-28 23:57:50 +00:00
|
|
|
double inf = uprv_getInfinity();
|
2010-02-26 02:29:00 +00:00
|
|
|
result.setDouble(digits->isPositive() ? inf : -inf);
|
2012-05-30 00:41:57 +00:00
|
|
|
// TODO: set the dl to infinity, and let it fall into the code below.
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2004-04-27 21:47:09 +00:00
|
|
|
else {
|
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
if (!fImpl->fMultiplier.isZero()) {
|
2010-02-26 02:29:00 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
2015-08-25 23:31:01 +00:00
|
|
|
digits->div(fImpl->fMultiplier, ec);
|
2003-10-31 22:47:25 +00:00
|
|
|
}
|
2010-02-26 02:29:00 +00:00
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
if (fImpl->fScale != 0) {
|
2013-02-07 19:48:29 +00:00
|
|
|
DigitList ten;
|
2013-09-12 06:29:39 +00:00
|
|
|
ten.set((int32_t)10);
|
2015-08-25 23:31:01 +00:00
|
|
|
if (fImpl->fScale > 0) {
|
|
|
|
for (int32_t i = fImpl->fScale; i > 0; i--) {
|
2013-02-07 19:48:29 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
digits->div(ten,ec);
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-25 23:31:01 +00:00
|
|
|
for (int32_t i = fImpl->fScale; i < 0; i++) {
|
2013-02-07 19:48:29 +00:00
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
digits->mult(ten,ec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
// Negative zero special case:
|
|
|
|
// if parsing integerOnly, change to +0, which goes into an int32 in a Formattable.
|
|
|
|
// if not parsing integerOnly, leave as -0, which a double can represent.
|
|
|
|
if (digits->isZero() && !digits->isPositive() && isParseIntegerOnly()) {
|
|
|
|
digits->setPositive(TRUE);
|
2003-10-31 22:47:25 +00:00
|
|
|
}
|
2010-02-26 02:29:00 +00:00
|
|
|
result.adoptDigitList(digits);
|
2003-10-31 22:47:25 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 16:09:39 +00:00
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
|
|
|
|
UBool
|
|
|
|
DecimalFormat::parseForCurrency(const UnicodeString& text,
|
|
|
|
ParsePosition& parsePosition,
|
|
|
|
DigitList& digits,
|
|
|
|
UBool* status,
|
|
|
|
UChar* currency) const {
|
2015-08-25 23:31:01 +00:00
|
|
|
UnicodeString positivePrefix;
|
|
|
|
UnicodeString positiveSuffix;
|
|
|
|
UnicodeString negativePrefix;
|
|
|
|
UnicodeString negativeSuffix;
|
|
|
|
fImpl->fPositivePrefixPattern.toString(positivePrefix);
|
|
|
|
fImpl->fPositiveSuffixPattern.toString(positiveSuffix);
|
|
|
|
fImpl->fNegativePrefixPattern.toString(negativePrefix);
|
|
|
|
fImpl->fNegativeSuffixPattern.toString(negativeSuffix);
|
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
int origPos = parsePosition.getIndex();
|
|
|
|
int maxPosIndex = origPos;
|
|
|
|
int maxErrorPos = -1;
|
|
|
|
// First, parse against current pattern.
|
|
|
|
// Since current pattern could be set by applyPattern(),
|
|
|
|
// it could be an arbitrary pattern, and it may not be the one
|
|
|
|
// defined in current locale.
|
|
|
|
UBool tmpStatus[fgStatusLength];
|
|
|
|
ParsePosition tmpPos(origPos);
|
|
|
|
DigitList tmpDigitList;
|
2009-03-10 20:53:27 +00:00
|
|
|
UBool found;
|
2011-04-29 00:00:47 +00:00
|
|
|
if (fStyle == UNUM_CURRENCY_PLURAL) {
|
2010-01-28 19:39:24 +00:00
|
|
|
found = subparse(text,
|
2015-08-25 23:31:01 +00:00
|
|
|
&negativePrefix, &negativeSuffix,
|
|
|
|
&positivePrefix, &positiveSuffix,
|
2009-03-10 20:53:27 +00:00
|
|
|
TRUE, UCURR_LONG_NAME,
|
|
|
|
tmpPos, tmpDigitList, tmpStatus, currency);
|
|
|
|
} else {
|
2010-01-28 19:39:24 +00:00
|
|
|
found = subparse(text,
|
2015-08-25 23:31:01 +00:00
|
|
|
&negativePrefix, &negativeSuffix,
|
|
|
|
&positivePrefix, &positiveSuffix,
|
2009-03-10 20:53:27 +00:00
|
|
|
TRUE, UCURR_SYMBOL_NAME,
|
|
|
|
tmpPos, tmpDigitList, tmpStatus, currency);
|
|
|
|
}
|
2009-02-12 22:55:29 +00:00
|
|
|
if (found) {
|
|
|
|
if (tmpPos.getIndex() > maxPosIndex) {
|
|
|
|
maxPosIndex = tmpPos.getIndex();
|
|
|
|
for (int32_t i = 0; i < fgStatusLength; ++i) {
|
|
|
|
status[i] = tmpStatus[i];
|
|
|
|
}
|
|
|
|
digits = tmpDigitList;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
maxErrorPos = tmpPos.getErrorIndex();
|
|
|
|
}
|
|
|
|
// Then, parse against affix patterns.
|
|
|
|
// Those are currency patterns and currency plural patterns.
|
2014-12-09 23:54:56 +00:00
|
|
|
int32_t pos = UHASH_FIRST;
|
2009-02-12 22:55:29 +00:00
|
|
|
const UHashElement* element = NULL;
|
|
|
|
while ( (element = fAffixPatternsForCurrency->nextElement(pos)) != NULL ) {
|
|
|
|
const UHashTok valueTok = element->value;
|
|
|
|
const AffixPatternsForCurrency* affixPtn = (AffixPatternsForCurrency*)valueTok.pointer;
|
|
|
|
UBool tmpStatus[fgStatusLength];
|
|
|
|
ParsePosition tmpPos(origPos);
|
|
|
|
DigitList tmpDigitList;
|
2013-06-20 22:09:42 +00:00
|
|
|
|
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
debug("trying affix for currency..");
|
|
|
|
affixPtn->dump();
|
|
|
|
#endif
|
|
|
|
|
2010-01-28 19:39:24 +00:00
|
|
|
UBool result = subparse(text,
|
2009-02-12 22:55:29 +00:00
|
|
|
&affixPtn->negPrefixPatternForCurrency,
|
2010-01-28 19:39:24 +00:00
|
|
|
&affixPtn->negSuffixPatternForCurrency,
|
|
|
|
&affixPtn->posPrefixPatternForCurrency,
|
2009-02-12 22:55:29 +00:00
|
|
|
&affixPtn->posSuffixPatternForCurrency,
|
2009-03-10 20:53:27 +00:00
|
|
|
TRUE, affixPtn->patternType,
|
2009-02-12 22:55:29 +00:00
|
|
|
tmpPos, tmpDigitList, tmpStatus, currency);
|
|
|
|
if (result) {
|
|
|
|
found = true;
|
|
|
|
if (tmpPos.getIndex() > maxPosIndex) {
|
|
|
|
maxPosIndex = tmpPos.getIndex();
|
|
|
|
for (int32_t i = 0; i < fgStatusLength; ++i) {
|
|
|
|
status[i] = tmpStatus[i];
|
|
|
|
}
|
|
|
|
digits = tmpDigitList;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
maxErrorPos = (tmpPos.getErrorIndex() > maxErrorPos) ?
|
|
|
|
tmpPos.getErrorIndex() : maxErrorPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Finally, parse against simple affix to find the match.
|
|
|
|
// For example, in TestMonster suite,
|
|
|
|
// if the to-be-parsed text is "-\u00A40,00".
|
|
|
|
// complexAffixCompare will not find match,
|
|
|
|
// since there is no ISO code matches "\u00A4",
|
|
|
|
// and the parse stops at "\u00A4".
|
|
|
|
// We will just use simple affix comparison (look for exact match)
|
|
|
|
// to pass it.
|
2013-08-20 20:51:31 +00:00
|
|
|
//
|
|
|
|
// TODO: We should parse against simple affix first when
|
|
|
|
// output currency is not requested. After the complex currency
|
|
|
|
// parsing implementation was introduced, the default currency
|
|
|
|
// instance parsing slowed down because of the new code flow.
|
|
|
|
// I filed #10312 - Yoshito
|
2009-02-12 22:55:29 +00:00
|
|
|
UBool tmpStatus_2[fgStatusLength];
|
|
|
|
ParsePosition tmpPos_2(origPos);
|
|
|
|
DigitList tmpDigitList_2;
|
2013-08-20 20:51:31 +00:00
|
|
|
|
|
|
|
// Disable complex currency parsing and try it again.
|
2010-01-28 19:39:24 +00:00
|
|
|
UBool result = subparse(text,
|
2015-08-25 23:31:01 +00:00
|
|
|
&fImpl->fAffixes.fNegativePrefix.getOtherVariant().toString(),
|
|
|
|
&fImpl->fAffixes.fNegativeSuffix.getOtherVariant().toString(),
|
|
|
|
&fImpl->fAffixes.fPositivePrefix.getOtherVariant().toString(),
|
|
|
|
&fImpl->fAffixes.fPositiveSuffix.getOtherVariant().toString(),
|
2013-08-20 20:51:31 +00:00
|
|
|
FALSE /* disable complex currency parsing */, UCURR_SYMBOL_NAME,
|
2010-01-28 19:39:24 +00:00
|
|
|
tmpPos_2, tmpDigitList_2, tmpStatus_2,
|
2009-02-12 22:55:29 +00:00
|
|
|
currency);
|
|
|
|
if (result) {
|
|
|
|
if (tmpPos_2.getIndex() > maxPosIndex) {
|
|
|
|
maxPosIndex = tmpPos_2.getIndex();
|
|
|
|
for (int32_t i = 0; i < fgStatusLength; ++i) {
|
|
|
|
status[i] = tmpStatus_2[i];
|
|
|
|
}
|
|
|
|
digits = tmpDigitList_2;
|
|
|
|
}
|
|
|
|
found = true;
|
|
|
|
} else {
|
|
|
|
maxErrorPos = (tmpPos_2.getErrorIndex() > maxErrorPos) ?
|
|
|
|
tmpPos_2.getErrorIndex() : maxErrorPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
//parsePosition.setIndex(origPos);
|
|
|
|
parsePosition.setErrorIndex(maxErrorPos);
|
|
|
|
} else {
|
|
|
|
parsePosition.setIndex(maxPosIndex);
|
|
|
|
parsePosition.setErrorIndex(-1);
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
2004-04-15 06:20:42 +00:00
|
|
|
* @param text the string to parse.
|
2009-02-12 22:55:29 +00:00
|
|
|
* @param negPrefix negative prefix.
|
|
|
|
* @param negSuffix negative suffix.
|
|
|
|
* @param posPrefix positive prefix.
|
|
|
|
* @param posSuffix positive suffix.
|
2013-08-20 20:51:31 +00:00
|
|
|
* @param complexCurrencyParsing whether it is complex currency parsing or not.
|
2009-03-10 20:53:27 +00:00
|
|
|
* @param type the currency type to parse against, LONG_NAME only or not.
|
1999-08-16 21:50:52 +00:00
|
|
|
* @param parsePosition The position at which to being parsing. Upon
|
2004-04-15 06:20:42 +00:00
|
|
|
* return, the first unparsed character.
|
|
|
|
* @param digits the DigitList to set to the parsed value.
|
|
|
|
* @param status output param containing boolean status flags indicating
|
1999-08-16 21:50:52 +00:00
|
|
|
* whether the value was infinite and whether it was positive.
|
2004-04-15 06:20:42 +00:00
|
|
|
* @param currency return value for parsed currency, for generic
|
|
|
|
* currency parsing mode, or NULL for normal parsing. In generic
|
|
|
|
* currency parsing mode, any currency is parsed, not just the
|
|
|
|
* currency that this formatter is set to.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2010-01-28 19:39:24 +00:00
|
|
|
UBool DecimalFormat::subparse(const UnicodeString& text,
|
2009-02-12 22:55:29 +00:00
|
|
|
const UnicodeString* negPrefix,
|
|
|
|
const UnicodeString* negSuffix,
|
|
|
|
const UnicodeString* posPrefix,
|
|
|
|
const UnicodeString* posSuffix,
|
2013-08-20 20:51:31 +00:00
|
|
|
UBool complexCurrencyParsing,
|
2009-03-10 20:53:27 +00:00
|
|
|
int8_t type,
|
2009-02-12 22:55:29 +00:00
|
|
|
ParsePosition& parsePosition,
|
2004-04-15 06:20:42 +00:00
|
|
|
DigitList& digits, UBool* status,
|
|
|
|
UChar* currency) const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2010-02-26 02:29:00 +00:00
|
|
|
// The parsing process builds up the number as char string, in the neutral format that
|
|
|
|
// will be acceptable to the decNumber library, then at the end passes that string
|
|
|
|
// off for conversion to a decNumber.
|
|
|
|
UErrorCode err = U_ZERO_ERROR;
|
2010-05-20 21:16:44 +00:00
|
|
|
CharString parsedNum;
|
2010-02-26 02:29:00 +00:00
|
|
|
digits.setToZero();
|
|
|
|
|
2002-03-12 01:32:42 +00:00
|
|
|
int32_t position = parsePosition.getIndex();
|
|
|
|
int32_t oldStart = position;
|
2012-05-30 00:41:57 +00:00
|
|
|
int32_t textLength = text.length(); // One less pointer to follow
|
2011-05-04 12:23:42 +00:00
|
|
|
UBool strictParse = !isLenient();
|
2015-08-25 23:31:01 +00:00
|
|
|
UChar32 zero = fImpl->getConstSymbol(DecimalFormatSymbols::kZeroDigitSymbol).char32At(0);
|
|
|
|
const UnicodeString *groupingString = &fImpl->getConstSymbol(
|
|
|
|
!fImpl->fMonetary ?
|
|
|
|
DecimalFormatSymbols::kGroupingSeparatorSymbol : DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol);
|
2012-12-04 20:33:19 +00:00
|
|
|
UChar32 groupingChar = groupingString->char32At(0);
|
|
|
|
int32_t groupingStringLength = groupingString->length();
|
|
|
|
int32_t groupingCharLength = U16_LENGTH(groupingChar);
|
|
|
|
UBool groupingUsed = isGroupingUsed();
|
2012-05-30 00:41:57 +00:00
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
UChar dbgbuf[300];
|
|
|
|
UnicodeString s(dbgbuf,0,300);;
|
|
|
|
s.append((UnicodeString)"PARSE \"").append(text.tempSubString(position)).append((UnicodeString)"\" " );
|
2012-12-04 21:33:15 +00:00
|
|
|
#define DBGAPPD(x) if(x) { s.append(UnicodeString(#x "=")); if(x->isEmpty()) { s.append(UnicodeString("<empty>")); } else { s.append(*x); } s.append(UnicodeString(" ")); } else { s.append(UnicodeString(#x "=NULL ")); }
|
2012-05-30 00:41:57 +00:00
|
|
|
DBGAPPD(negPrefix);
|
|
|
|
DBGAPPD(negSuffix);
|
|
|
|
DBGAPPD(posPrefix);
|
|
|
|
DBGAPPD(posSuffix);
|
|
|
|
debugout(s);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
UBool fastParseOk = false; /* TRUE iff fast parse is OK */
|
2012-11-30 22:19:58 +00:00
|
|
|
// UBool fastParseHadDecimal = FALSE; /* true if fast parse saw a decimal point. */
|
2015-08-25 23:31:01 +00:00
|
|
|
if((fImpl->isParseFastpath()) && !fImpl->fMonetary &&
|
2012-12-04 20:33:19 +00:00
|
|
|
text.length()>0 &&
|
|
|
|
text.length()<32 &&
|
|
|
|
(posPrefix==NULL||posPrefix->isEmpty()) &&
|
|
|
|
(posSuffix==NULL||posSuffix->isEmpty()) &&
|
|
|
|
// (negPrefix==NULL||negPrefix->isEmpty()) &&
|
|
|
|
// (negSuffix==NULL||(negSuffix->isEmpty()) ) &&
|
|
|
|
TRUE) { // optimized path
|
2012-05-30 00:41:57 +00:00
|
|
|
int j=position;
|
|
|
|
int l=text.length();
|
|
|
|
int digitCount=0;
|
|
|
|
UChar32 ch = text.char32At(j);
|
2015-08-25 23:31:01 +00:00
|
|
|
const UnicodeString *decimalString = &fImpl->getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
|
2012-05-30 00:41:57 +00:00
|
|
|
UChar32 decimalChar = 0;
|
2012-09-17 19:03:01 +00:00
|
|
|
UBool intOnly = FALSE;
|
2012-12-04 20:33:19 +00:00
|
|
|
UChar32 lookForGroup = (groupingUsed&&intOnly&&strictParse)?groupingChar:0;
|
|
|
|
|
2012-05-30 00:41:57 +00:00
|
|
|
int32_t decimalCount = decimalString->countChar32(0,3);
|
|
|
|
if(isParseIntegerOnly()) {
|
|
|
|
decimalChar = 0; // not allowed
|
2012-12-04 20:33:19 +00:00
|
|
|
intOnly = TRUE; // Don't look for decimals.
|
2012-05-30 00:41:57 +00:00
|
|
|
} else if(decimalCount==1) {
|
2012-12-04 20:33:19 +00:00
|
|
|
decimalChar = decimalString->char32At(0); // Look for this decimal
|
2012-05-30 00:41:57 +00:00
|
|
|
} else if(decimalCount==0) {
|
2012-12-04 20:33:19 +00:00
|
|
|
decimalChar=0; // NO decimal set
|
2012-05-30 00:41:57 +00:00
|
|
|
} else {
|
2012-12-04 20:33:19 +00:00
|
|
|
j=l+1;//Set counter to end of line, so that we break. Unknown decimal situation.
|
2012-05-30 00:41:57 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 20:33:19 +00:00
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
printf("Preparing to do fastpath parse: decimalChar=U+%04X, groupingChar=U+%04X, first ch=U+%04X intOnly=%c strictParse=%c\n",
|
|
|
|
decimalChar, groupingChar, ch,
|
|
|
|
(intOnly)?'y':'n',
|
|
|
|
(strictParse)?'y':'n');
|
|
|
|
#endif
|
2012-12-04 21:33:15 +00:00
|
|
|
if(ch==0x002D) { // '-'
|
|
|
|
j=l+1;//=break - negative number.
|
2012-05-30 00:41:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
parsedNum.append('-',err);
|
|
|
|
j+=U16_LENGTH(ch);
|
|
|
|
if(j<l) ch = text.char32At(j);
|
|
|
|
*/
|
|
|
|
} else {
|
|
|
|
parsedNum.append('+',err);
|
|
|
|
}
|
|
|
|
while(j<l) {
|
|
|
|
int32_t digit = ch - zero;
|
|
|
|
if(digit >=0 && digit <= 9) {
|
|
|
|
parsedNum.append((char)(digit + '0'), err);
|
|
|
|
if((digitCount>0) || digit!=0 || j==(l-1)) {
|
|
|
|
digitCount++;
|
|
|
|
}
|
2012-12-04 20:33:19 +00:00
|
|
|
} else if(ch == 0) { // break out
|
|
|
|
digitCount=-1;
|
|
|
|
break;
|
2012-05-30 00:41:57 +00:00
|
|
|
} else if(ch == decimalChar) {
|
|
|
|
parsedNum.append((char)('.'), err);
|
|
|
|
decimalChar=0; // no more decimals.
|
2012-11-30 22:19:58 +00:00
|
|
|
// fastParseHadDecimal=TRUE;
|
2012-12-04 20:33:19 +00:00
|
|
|
} else if(ch == lookForGroup) {
|
|
|
|
// ignore grouping char. No decimals, so it has to be an ignorable grouping sep
|
|
|
|
} else if(intOnly && (lookForGroup!=0) && !u_isdigit(ch)) {
|
|
|
|
// parsing integer only and can fall through
|
2012-05-30 00:41:57 +00:00
|
|
|
} else {
|
2012-12-04 20:33:19 +00:00
|
|
|
digitCount=-1; // fail - fall through to slow parse
|
2012-05-30 00:41:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
j+=U16_LENGTH(ch);
|
|
|
|
ch = text.char32At(j); // for next
|
|
|
|
}
|
2012-09-17 19:03:01 +00:00
|
|
|
if(
|
2012-12-04 20:33:19 +00:00
|
|
|
((j==l)||intOnly) // end OR only parsing integer
|
|
|
|
&& (digitCount>0)) { // and have at least one digit
|
2012-05-30 00:41:57 +00:00
|
|
|
fastParseOk=true; // Fast parse OK!
|
|
|
|
|
|
|
|
#ifdef SKIP_OPT
|
|
|
|
debug("SKIP_OPT");
|
|
|
|
/* for testing, try it the slow way. also */
|
|
|
|
fastParseOk=false;
|
|
|
|
parsedNum.clear();
|
|
|
|
#else
|
|
|
|
parsePosition.setIndex(position=j);
|
|
|
|
status[fgStatusInfinite]=false;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
// was not OK. reset, retry
|
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
printf("Fall through: j=%d, l=%d, digitCount=%d\n", j, l, digitCount);
|
|
|
|
#endif
|
|
|
|
parsedNum.clear();
|
|
|
|
}
|
2012-09-17 19:03:01 +00:00
|
|
|
} else {
|
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
printf("Could not fastpath parse. ");
|
|
|
|
printf("text.length()=%d ", text.length());
|
|
|
|
printf("posPrefix=%p posSuffix=%p ", posPrefix, posSuffix);
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
2012-05-30 00:41:57 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 23:31:01 +00:00
|
|
|
UnicodeString formatPattern;
|
|
|
|
toPattern(formatPattern);
|
|
|
|
|
2012-05-30 00:41:57 +00:00
|
|
|
if(!fastParseOk
|
|
|
|
#if UCONFIG_HAVE_PARSEALLINPUT
|
|
|
|
&& fParseAllInput!=UNUM_YES
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
int32_t formatWidth = fImpl->getOldFormatWidth();
|
2003-04-21 19:18:27 +00:00
|
|
|
// Match padding before prefix
|
2015-08-25 23:31:01 +00:00
|
|
|
if (formatWidth > 0 && fImpl->fAffixes.fPadPosition == DigitAffixesAndPadding::kPadBeforePrefix) {
|
2003-04-21 19:18:27 +00:00
|
|
|
position = skipPadding(text, position);
|
|
|
|
}
|
|
|
|
|
2003-04-04 20:55:10 +00:00
|
|
|
// Match positive and negative prefixes; prefer longest match.
|
2013-08-20 20:51:31 +00:00
|
|
|
int32_t posMatch = compareAffix(text, position, FALSE, TRUE, posPrefix, complexCurrencyParsing, type, currency);
|
|
|
|
int32_t negMatch = compareAffix(text, position, TRUE, TRUE, negPrefix, complexCurrencyParsing, type, currency);
|
2003-04-04 20:55:10 +00:00
|
|
|
if (posMatch >= 0 && negMatch >= 0) {
|
|
|
|
if (posMatch > negMatch) {
|
|
|
|
negMatch = -1;
|
|
|
|
} else if (negMatch > posMatch) {
|
|
|
|
posMatch = -1;
|
2010-01-28 19:39:24 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2003-04-04 20:55:10 +00:00
|
|
|
if (posMatch >= 0) {
|
|
|
|
position += posMatch;
|
2010-02-26 02:29:00 +00:00
|
|
|
parsedNum.append('+', err);
|
2003-04-04 20:55:10 +00:00
|
|
|
} else if (negMatch >= 0) {
|
|
|
|
position += negMatch;
|
2010-02-26 02:29:00 +00:00
|
|
|
parsedNum.append('-', err);
|
2011-05-04 12:23:42 +00:00
|
|
|
} else if (strictParse){
|
1999-08-16 21:50:52 +00:00
|
|
|
parsePosition.setErrorIndex(position);
|
|
|
|
return FALSE;
|
2012-02-14 07:05:58 +00:00
|
|
|
} else {
|
|
|
|
// Temporary set positive. This might be changed after checking suffix
|
|
|
|
parsedNum.append('+', err);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-10-12 19:04:06 +00:00
|
|
|
|
2003-04-21 19:18:27 +00:00
|
|
|
// Match padding before prefix
|
2015-08-25 23:31:01 +00:00
|
|
|
if (formatWidth > 0 && fImpl->fAffixes.fPadPosition == DigitAffixesAndPadding::kPadAfterPrefix) {
|
2003-04-21 19:18:27 +00:00
|
|
|
position = skipPadding(text, position);
|
|
|
|
}
|
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
if (! strictParse) {
|
|
|
|
position = skipUWhiteSpace(text, position);
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// process digits or Inf, find decimal position
|
2015-08-25 23:31:01 +00:00
|
|
|
const UnicodeString *inf = &fImpl->getConstSymbol(DecimalFormatSymbols::kInfinitySymbol);
|
2002-10-12 01:09:00 +00:00
|
|
|
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
|
2011-05-04 12:23:42 +00:00
|
|
|
status[fgStatusInfinite] = infLen != 0;
|
|
|
|
|
|
|
|
if (infLen != 0) {
|
2010-02-26 02:29:00 +00:00
|
|
|
parsedNum.append("Infinity", err);
|
|
|
|
} else {
|
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.
|
|
|
|
|
2002-10-12 01:09:00 +00:00
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
UBool strictFail = FALSE; // did we exit with a strict parse failure?
|
2017-10-05 21:41:46 +00:00
|
|
|
int32_t lastGroup = -1; // after which digit index did we last see a grouping separator?
|
|
|
|
int32_t currGroup = -1; // for temporary storage the digit index of the current grouping separator
|
2015-08-25 23:31:01 +00:00
|
|
|
int32_t gs2 = fImpl->fEffGrouping.fGrouping2 == 0 ? fImpl->fEffGrouping.fGrouping : fImpl->fEffGrouping.fGrouping2;
|
2011-05-04 12:23:42 +00:00
|
|
|
|
|
|
|
const UnicodeString *decimalString;
|
2015-08-25 23:31:01 +00:00
|
|
|
if (fImpl->fMonetary) {
|
|
|
|
decimalString = &fImpl->getConstSymbol(DecimalFormatSymbols::kMonetarySeparatorSymbol);
|
2002-10-12 01:09:00 +00:00
|
|
|
} else {
|
2015-08-25 23:31:01 +00:00
|
|
|
decimalString = &fImpl->getConstSymbol(DecimalFormatSymbols::kDecimalSeparatorSymbol);
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
2012-05-30 00:41:57 +00:00
|
|
|
UChar32 decimalChar = decimalString->char32At(0);
|
|
|
|
int32_t decimalStringLength = decimalString->length();
|
|
|
|
int32_t decimalCharLength = U16_LENGTH(decimalChar);
|
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool sawDecimal = FALSE;
|
2011-09-07 14:30:32 +00:00
|
|
|
UChar32 sawDecimalChar = 0xFFFF;
|
|
|
|
UBool sawGrouping = FALSE;
|
|
|
|
UChar32 sawGroupingChar = 0xFFFF;
|
2000-08-07 23:06:28 +00:00
|
|
|
UBool sawDigit = FALSE;
|
2000-10-12 19:04:06 +00:00
|
|
|
int32_t backup = -1;
|
2000-08-07 23:06:28 +00:00
|
|
|
int32_t digit;
|
2011-05-04 12:23:42 +00:00
|
|
|
|
|
|
|
// equivalent grouping and decimal support
|
2011-09-07 14:30:32 +00:00
|
|
|
const UnicodeSet *decimalSet = NULL;
|
|
|
|
const UnicodeSet *groupingSet = NULL;
|
2011-05-04 12:23:42 +00:00
|
|
|
|
|
|
|
if (decimalCharLength == decimalStringLength) {
|
2011-09-07 14:30:32 +00:00
|
|
|
decimalSet = DecimalFormatStaticSets::getSimilarDecimals(decimalChar, strictParse);
|
2011-05-04 12:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (groupingCharLength == groupingStringLength) {
|
|
|
|
if (strictParse) {
|
2013-05-31 23:50:15 +00:00
|
|
|
groupingSet = fStaticSets->fStrictDefaultGroupingSeparators;
|
2011-05-04 12:23:42 +00:00
|
|
|
} else {
|
2013-05-31 23:50:15 +00:00
|
|
|
groupingSet = fStaticSets->fDefaultGroupingSeparators;
|
2011-05-04 12:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-07 14:30:32 +00:00
|
|
|
// We need to test groupingChar and decimalChar separately from groupingSet and decimalSet, if the sets are even initialized.
|
|
|
|
// If sawDecimal is TRUE, only consider sawDecimalChar and NOT decimalSet
|
|
|
|
// If a character matches decimalSet, don't consider it to be a member of the groupingSet.
|
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;
|
2011-05-04 12:23:42 +00:00
|
|
|
int32_t integerDigitCount = 0;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-04-21 19:18:27 +00:00
|
|
|
for (; position < textLength; )
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-04-21 19:18:27 +00:00
|
|
|
UChar32 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
|
2010-01-28 19:39:24 +00:00
|
|
|
* calling Character.digit(). If this also fails, digit will
|
1999-08-16 21:50:52 +00:00
|
|
|
* 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
|
|
|
}
|
2010-08-13 21:23:00 +00:00
|
|
|
|
|
|
|
// As a last resort, look through the localized digits if the zero digit
|
|
|
|
// is not a "standard" Unicode digit.
|
|
|
|
if ( (digit < 0 || digit > 9) && u_charDigitValue(zero) != 0) {
|
|
|
|
digit = 0;
|
2015-08-25 23:31:01 +00:00
|
|
|
if ( fImpl->getConstSymbol((DecimalFormatSymbols::ENumberFormatSymbol)(DecimalFormatSymbols::kZeroDigitSymbol)).char32At(0) == ch ) {
|
2010-08-13 21:23:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (digit = 1 ; digit < 10 ; digit++ ) {
|
2015-08-25 23:31:01 +00:00
|
|
|
if ( fImpl->getConstSymbol((DecimalFormatSymbols::ENumberFormatSymbol)(DecimalFormatSymbols::kOneDigitSymbol+digit-1)).char32At(0) == ch ) {
|
2010-08-13 21:23:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
if (digit >= 0 && digit <= 9)
|
2000-08-07 23:06:28 +00:00
|
|
|
{
|
2011-05-04 12:23:42 +00:00
|
|
|
if (strictParse && backup != -1) {
|
|
|
|
// comma followed by digit, so group before comma is a
|
|
|
|
// secondary group. If there was a group separator
|
|
|
|
// before that, the group must == the secondary group
|
|
|
|
// length, else it can be <= the the secondary group
|
|
|
|
// length.
|
2017-10-05 21:41:46 +00:00
|
|
|
if ((lastGroup != -1 && currGroup - lastGroup != gs2) ||
|
|
|
|
(lastGroup == -1 && digitCount - 1 > gs2)) {
|
2011-05-04 12:23:42 +00:00
|
|
|
strictFail = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-10-05 21:41:46 +00:00
|
|
|
lastGroup = currGroup;
|
2011-05-04 12:23:42 +00:00
|
|
|
}
|
|
|
|
|
2000-10-24 16:09:39 +00:00
|
|
|
// Cancel out backup setting (see grouping handler below)
|
2017-10-05 21:41:46 +00:00
|
|
|
currGroup = -1;
|
2000-10-24 16:09:39 +00:00
|
|
|
backup = -1;
|
2000-08-07 23:06:28 +00:00
|
|
|
sawDigit = TRUE;
|
2011-05-04 12:23:42 +00:00
|
|
|
|
|
|
|
// Note: this will append leading zeros
|
2010-05-20 21:16:44 +00:00
|
|
|
parsedNum.append((char)(digit + '0'), err);
|
2011-05-04 12:23:42 +00:00
|
|
|
|
|
|
|
// count any digit that's not a leading zero
|
|
|
|
if (digit > 0 || digitCount > 0 || sawDecimal) {
|
|
|
|
digitCount += 1;
|
|
|
|
|
|
|
|
// count any integer digit that's not a leading zero
|
|
|
|
if (! sawDecimal) {
|
|
|
|
integerDigitCount += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-21 19:18:27 +00:00
|
|
|
position += U16_LENGTH(ch);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-09-07 14:30:32 +00:00
|
|
|
else if (groupingStringLength > 0 &&
|
|
|
|
matchGrouping(groupingChar, sawGrouping, sawGroupingChar, groupingSet,
|
|
|
|
decimalChar, decimalSet,
|
2012-12-04 20:33:19 +00:00
|
|
|
ch) && groupingUsed)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2011-05-04 12:23:42 +00:00
|
|
|
if (sawDecimal) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strictParse) {
|
|
|
|
if ((!sawDigit || backup != -1)) {
|
|
|
|
// leading group, or two group separators in a row
|
|
|
|
strictFail = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2017-10-05 21:41:46 +00:00
|
|
|
currGroup = digitCount;
|
1999-08-16 21:50:52 +00:00
|
|
|
backup = position;
|
2011-05-04 12:23:42 +00:00
|
|
|
position += groupingStringLength;
|
2011-09-07 14:30:32 +00:00
|
|
|
sawGrouping=TRUE;
|
|
|
|
// Once we see a grouping character, we only accept that grouping character from then on.
|
|
|
|
sawGroupingChar=ch;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2011-09-07 14:30:32 +00:00
|
|
|
else if (matchDecimal(decimalChar,sawDecimal,sawDecimalChar, decimalSet, ch))
|
2000-10-12 19:04:06 +00:00
|
|
|
{
|
2011-05-04 12:23:42 +00:00
|
|
|
if (strictParse) {
|
|
|
|
if (backup != -1 ||
|
2017-10-05 21:41:46 +00:00
|
|
|
(lastGroup != -1 && digitCount - lastGroup != fImpl->fEffGrouping.fGrouping)) {
|
2011-05-04 12:23:42 +00:00
|
|
|
strictFail = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2011-05-04 12:23:42 +00:00
|
|
|
if (isParseIntegerOnly() || sawDecimal) {
|
|
|
|
break;
|
|
|
|
}
|
2000-10-24 16:09:39 +00:00
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
parsedNum.append('.', err);
|
2011-05-04 12:23:42 +00:00
|
|
|
position += decimalStringLength;
|
2000-10-12 19:04:06 +00:00
|
|
|
sawDecimal = TRUE;
|
2011-09-07 14:30:32 +00:00
|
|
|
// Once we see a decimal character, we only accept that decimal character from then on.
|
|
|
|
sawDecimalChar=ch;
|
|
|
|
// decimalSet is considered to consist of (ch,ch)
|
2000-10-12 19:04:06 +00:00
|
|
|
}
|
2002-10-12 01:09:00 +00:00
|
|
|
else {
|
|
|
|
|
2012-10-12 19:52:43 +00:00
|
|
|
if(!fBoolFlags.contains(UNUM_PARSE_NO_EXPONENT) || // don't parse if this is set unless..
|
2013-09-04 00:01:26 +00:00
|
|
|
isScientificNotation()) { // .. it's an exponent format - ignore setting and parse anyways
|
2012-10-12 19:52:43 +00:00
|
|
|
const UnicodeString *tmp;
|
2015-08-25 23:31:01 +00:00
|
|
|
tmp = &fImpl->getConstSymbol(DecimalFormatSymbols::kExponentialSymbol);
|
2012-10-12 19:52:43 +00:00
|
|
|
// TODO: CASE
|
|
|
|
if (!text.caseCompare(position, tmp->length(), *tmp, U_FOLD_CASE_DEFAULT)) // error code is set below if !sawDigit
|
2000-08-07 23:06:28 +00:00
|
|
|
{
|
2012-10-12 19:52:43 +00:00
|
|
|
// Parse sign, if present
|
|
|
|
int32_t pos = position + tmp->length();
|
|
|
|
char exponentSign = '+';
|
|
|
|
|
|
|
|
if (pos < textLength)
|
2002-10-12 01:09:00 +00:00
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
tmp = &fImpl->getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
|
2002-10-12 01:09:00 +00:00
|
|
|
if (!text.compare(pos, tmp->length(), *tmp))
|
|
|
|
{
|
2003-04-21 19:18:27 +00:00
|
|
|
pos += tmp->length();
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
2012-10-12 19:52:43 +00:00
|
|
|
else {
|
2015-08-25 23:31:01 +00:00
|
|
|
tmp = &fImpl->getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
|
2012-10-12 19:52:43 +00:00
|
|
|
if (!text.compare(pos, tmp->length(), *tmp))
|
|
|
|
{
|
|
|
|
exponentSign = '-';
|
|
|
|
pos += tmp->length();
|
|
|
|
}
|
|
|
|
}
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
2012-10-12 19:52:43 +00:00
|
|
|
UBool sawExponentDigit = FALSE;
|
|
|
|
while (pos < textLength) {
|
2017-10-05 21:41:46 +00:00
|
|
|
ch = text.char32At(pos);
|
2012-10-12 19:52:43 +00:00
|
|
|
digit = ch - zero;
|
2002-10-12 01:09:00 +00:00
|
|
|
|
2012-10-12 19:52:43 +00:00
|
|
|
if (digit < 0 || digit > 9) {
|
|
|
|
digit = u_charDigitValue(ch);
|
|
|
|
}
|
|
|
|
if (0 <= digit && digit <= 9) {
|
|
|
|
if (!sawExponentDigit) {
|
|
|
|
parsedNum.append('E', err);
|
|
|
|
parsedNum.append(exponentSign, err);
|
|
|
|
sawExponentDigit = TRUE;
|
|
|
|
}
|
2017-10-05 21:41:46 +00:00
|
|
|
pos += U16_LENGTH(ch);
|
2012-10-12 19:52:43 +00:00
|
|
|
parsedNum.append((char)(digit + '0'), err);
|
|
|
|
} else {
|
|
|
|
break;
|
2010-02-26 02:29:00 +00:00
|
|
|
}
|
2002-10-12 01:09:00 +00:00
|
|
|
}
|
|
|
|
|
2012-10-12 19:52:43 +00:00
|
|
|
if (sawExponentDigit) {
|
|
|
|
position = pos; // Advance past the exponent
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2012-10-12 19:52:43 +00:00
|
|
|
break; // Whether we fail or succeed, we exit this loop
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else { // not parsing exponent
|
2002-10-12 01:09:00 +00:00
|
|
|
break;
|
2012-10-12 19:52:43 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-30 21:53:10 +00:00
|
|
|
// if we didn't see a decimal and it is required, check to see if the pattern had one
|
|
|
|
if(!sawDecimal && isDecimalPatternMatchRequired())
|
|
|
|
{
|
2016-09-20 04:30:06 +00:00
|
|
|
if(formatPattern.indexOf(kPatternDecimalSeparator) != -1)
|
2014-08-30 21:53:10 +00:00
|
|
|
{
|
|
|
|
parsePosition.setIndex(oldStart);
|
|
|
|
parsePosition.setErrorIndex(position);
|
|
|
|
debug("decimal point match required fail!");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-07 23:06:28 +00:00
|
|
|
if (backup != -1)
|
|
|
|
{
|
|
|
|
position = backup;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
if (strictParse && !sawDecimal) {
|
2017-10-05 21:41:46 +00:00
|
|
|
if (lastGroup != -1 && digitCount - lastGroup != fImpl->fEffGrouping.fGrouping) {
|
2011-05-04 12:23:42 +00:00
|
|
|
strictFail = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strictFail) {
|
|
|
|
// only set with strictParse and a grouping separator error
|
|
|
|
|
|
|
|
parsePosition.setIndex(oldStart);
|
|
|
|
parsePosition.setErrorIndex(position);
|
2012-05-30 00:41:57 +00:00
|
|
|
debug("strictFail!");
|
2011-05-04 12:23:42 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
// If there was no decimal point we have an integer
|
|
|
|
|
|
|
|
// If none of the text string was recognized. For example, parse
|
|
|
|
// "x" with pattern "#0.00" (return index and error index both 0)
|
|
|
|
// parse "$" with pattern "$#0.00". (return index 0 and error index
|
|
|
|
// 1).
|
|
|
|
if (!sawDigit && digitCount == 0) {
|
2012-05-30 00:41:57 +00:00
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
debug("none of text rec");
|
|
|
|
printf("position=%d\n",position);
|
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
parsePosition.setIndex(oldStart);
|
|
|
|
parsePosition.setErrorIndex(oldStart);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-21 19:18:27 +00:00
|
|
|
// Match padding before suffix
|
2015-08-25 23:31:01 +00:00
|
|
|
if (formatWidth > 0 && fImpl->fAffixes.fPadPosition == DigitAffixesAndPadding::kPadBeforeSuffix) {
|
2003-04-21 19:18:27 +00:00
|
|
|
position = skipPadding(text, position);
|
|
|
|
}
|
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
int32_t posSuffixMatch = -1, negSuffixMatch = -1;
|
|
|
|
|
2003-04-04 20:55:10 +00:00
|
|
|
// Match positive and negative suffixes; prefer longest match.
|
2011-05-04 12:23:42 +00:00
|
|
|
if (posMatch >= 0 || (!strictParse && negMatch < 0)) {
|
2013-08-20 20:51:31 +00:00
|
|
|
posSuffixMatch = compareAffix(text, position, FALSE, FALSE, posSuffix, complexCurrencyParsing, type, currency);
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
2003-04-04 20:55:10 +00:00
|
|
|
if (negMatch >= 0) {
|
2013-08-20 20:51:31 +00:00
|
|
|
negSuffixMatch = compareAffix(text, position, TRUE, FALSE, negSuffix, complexCurrencyParsing, type, currency);
|
2000-08-07 23:06:28 +00:00
|
|
|
}
|
2011-05-04 12:23:42 +00:00
|
|
|
if (posSuffixMatch >= 0 && negSuffixMatch >= 0) {
|
|
|
|
if (posSuffixMatch > negSuffixMatch) {
|
|
|
|
negSuffixMatch = -1;
|
|
|
|
} else if (negSuffixMatch > posSuffixMatch) {
|
|
|
|
posSuffixMatch = -1;
|
2010-01-28 19:39:24 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-04-04 20:55:10 +00:00
|
|
|
// Fail if neither or both
|
2011-05-04 12:23:42 +00:00
|
|
|
if (strictParse && ((posSuffixMatch >= 0) == (negSuffixMatch >= 0))) {
|
1999-08-16 21:50:52 +00:00
|
|
|
parsePosition.setErrorIndex(position);
|
2012-05-30 00:41:57 +00:00
|
|
|
debug("neither or both");
|
1999-08-16 21:50:52 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
position += (posSuffixMatch >= 0 ? posSuffixMatch : (negSuffixMatch >= 0 ? negSuffixMatch : 0));
|
2003-04-21 19:18:27 +00:00
|
|
|
|
|
|
|
// Match padding before suffix
|
2015-08-25 23:31:01 +00:00
|
|
|
if (formatWidth > 0 && fImpl->fAffixes.fPadPosition == DigitAffixesAndPadding::kPadAfterSuffix) {
|
2003-04-21 19:18:27 +00:00
|
|
|
position = skipPadding(text, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
parsePosition.setIndex(position);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
parsedNum.data()[0] = (posSuffixMatch >= 0 || (!strictParse && negMatch < 0 && negSuffixMatch < 0)) ? '+' : '-';
|
2012-05-30 00:41:57 +00:00
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
printf("PP -> %d, SLOW = [%s]! pp=%d, os=%d, err=%s\n", position, parsedNum.data(), parsePosition.getIndex(),oldStart,u_errorName(err));
|
|
|
|
#endif
|
|
|
|
} /* end SLOW parse */
|
|
|
|
if(parsePosition.getIndex() == oldStart)
|
2000-08-07 23:06:28 +00:00
|
|
|
{
|
2012-05-30 00:41:57 +00:00
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
printf(" PP didnt move, err\n");
|
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
parsePosition.setErrorIndex(position);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-05-30 00:41:57 +00:00
|
|
|
#if UCONFIG_HAVE_PARSEALLINPUT
|
|
|
|
else if (fParseAllInput==UNUM_YES&&parsePosition.getIndex()!=textLength)
|
|
|
|
{
|
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
printf(" PP didnt consume all (UNUM_YES), err\n");
|
|
|
|
#endif
|
|
|
|
parsePosition.setErrorIndex(position);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-17 19:03:01 +00:00
|
|
|
// uint32_t bits = (fastParseOk?kFastpathOk:0) |
|
|
|
|
// (fastParseHadDecimal?0:kNoDecimal);
|
|
|
|
//printf("FPOK=%d, FPHD=%d, bits=%08X\n", fastParseOk, fastParseHadDecimal, bits);
|
|
|
|
digits.set(parsedNum.toStringPiece(),
|
|
|
|
err,
|
|
|
|
0//bits
|
|
|
|
);
|
2010-02-26 02:29:00 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(err)) {
|
2012-05-30 00:41:57 +00:00
|
|
|
#ifdef FMT_DEBUG
|
|
|
|
printf(" err setting %s\n", u_errorName(err));
|
|
|
|
#endif
|
2010-02-26 02:29:00 +00:00
|
|
|
parsePosition.setErrorIndex(position);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2014-08-30 21:53:10 +00:00
|
|
|
|
|
|
|
// check if we missed a required decimal point
|
|
|
|
if(fastParseOk && isDecimalPatternMatchRequired())
|
|
|
|
{
|
2016-09-20 04:30:06 +00:00
|
|
|
if(formatPattern.indexOf(kPatternDecimalSeparator) != -1)
|
2014-08-30 21:53:10 +00:00
|
|
|
{
|
|
|
|
parsePosition.setIndex(oldStart);
|
|
|
|
parsePosition.setErrorIndex(position);
|
|
|
|
debug("decimal point match required fail!");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-04-21 19:18:27 +00:00
|
|
|
/**
|
|
|
|
* Starting at position, advance past a run of pad characters, if any.
|
|
|
|
* Return the index of the first character after position that is not a pad
|
|
|
|
* character. Result is >= position.
|
|
|
|
*/
|
|
|
|
int32_t DecimalFormat::skipPadding(const UnicodeString& text, int32_t position) const {
|
2015-08-25 23:31:01 +00:00
|
|
|
int32_t padLen = U16_LENGTH(fImpl->fAffixes.fPadChar);
|
2003-04-21 19:18:27 +00:00
|
|
|
while (position < text.length() &&
|
2015-08-25 23:31:01 +00:00
|
|
|
text.char32At(position) == fImpl->fAffixes.fPadChar) {
|
2003-04-21 19:18:27 +00:00
|
|
|
position += padLen;
|
|
|
|
}
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2003-04-21 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* Return the length matched by the given affix, or -1 if none.
|
|
|
|
* Runs of white space in the affix, match runs of white space in
|
|
|
|
* the input. Pattern white space and input white space are
|
|
|
|
* determined differently; see code.
|
|
|
|
* @param text input text
|
|
|
|
* @param pos offset into input at which to begin matching
|
|
|
|
* @param isNegative
|
|
|
|
* @param isPrefix
|
2009-02-12 22:55:29 +00:00
|
|
|
* @param affixPat affix pattern used for currency affix comparison.
|
2013-09-04 10:46:03 +00:00
|
|
|
* @param complexCurrencyParsing whether it is currency parsing or not
|
2009-03-10 20:53:27 +00:00
|
|
|
* @param type the currency type to parse against, LONG_NAME only or not.
|
2004-04-15 06:20:42 +00:00
|
|
|
* @param currency return value for parsed currency, for generic
|
|
|
|
* currency parsing mode, or null for normal parsing. In generic
|
|
|
|
* currency parsing mode, any currency is parsed, not just the
|
|
|
|
* currency that this formatter is set to.
|
2003-04-21 22:48:16 +00:00
|
|
|
* @return length of input that matches, or -1 if match failure
|
|
|
|
*/
|
|
|
|
int32_t DecimalFormat::compareAffix(const UnicodeString& text,
|
|
|
|
int32_t pos,
|
|
|
|
UBool isNegative,
|
2004-04-15 06:20:42 +00:00
|
|
|
UBool isPrefix,
|
2009-02-12 22:55:29 +00:00
|
|
|
const UnicodeString* affixPat,
|
2013-09-04 10:46:03 +00:00
|
|
|
UBool complexCurrencyParsing,
|
2009-03-10 20:53:27 +00:00
|
|
|
int8_t type,
|
2007-03-02 00:28:09 +00:00
|
|
|
UChar* currency) const
|
|
|
|
{
|
|
|
|
const UnicodeString *patternToCompare;
|
2015-08-25 23:31:01 +00:00
|
|
|
if (currency != NULL ||
|
|
|
|
(fImpl->fMonetary && complexCurrencyParsing)) {
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
if (affixPat != NULL) {
|
2009-03-10 20:53:27 +00:00
|
|
|
return compareComplexAffix(*affixPat, text, pos, type, currency);
|
2003-04-21 22:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-28 19:39:24 +00:00
|
|
|
|
2007-03-02 00:28:09 +00:00
|
|
|
if (isNegative) {
|
|
|
|
if (isPrefix) {
|
2015-08-25 23:31:01 +00:00
|
|
|
patternToCompare = &fImpl->fAffixes.fNegativePrefix.getOtherVariant().toString();
|
2007-03-02 00:28:09 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-08-25 23:31:01 +00:00
|
|
|
patternToCompare = &fImpl->fAffixes.fNegativeSuffix.getOtherVariant().toString();
|
2007-03-02 00:28:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (isPrefix) {
|
2015-08-25 23:31:01 +00:00
|
|
|
patternToCompare = &fImpl->fAffixes.fPositivePrefix.getOtherVariant().toString();
|
2007-03-02 00:28:09 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-08-25 23:31:01 +00:00
|
|
|
patternToCompare = &fImpl->fAffixes.fPositiveSuffix.getOtherVariant().toString();
|
2007-03-02 00:28:09 +00:00
|
|
|
}
|
2003-04-21 22:48:16 +00:00
|
|
|
}
|
2011-05-04 12:23:42 +00:00
|
|
|
return compareSimpleAffix(*patternToCompare, text, pos, isLenient());
|
2003-04-21 22:48:16 +00:00
|
|
|
}
|
|
|
|
|
2013-09-13 16:21:56 +00:00
|
|
|
UBool DecimalFormat::equalWithSignCompatibility(UChar32 lhs, UChar32 rhs) const {
|
2013-08-15 21:02:16 +00:00
|
|
|
if (lhs == rhs) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-09-13 16:21:56 +00:00
|
|
|
U_ASSERT(fStaticSets != NULL); // should already be loaded
|
|
|
|
const UnicodeSet *minusSigns = fStaticSets->fMinusSigns;
|
|
|
|
const UnicodeSet *plusSigns = fStaticSets->fPlusSigns;
|
2013-08-15 21:02:16 +00:00
|
|
|
return (minusSigns->contains(lhs) && minusSigns->contains(rhs)) ||
|
|
|
|
(plusSigns->contains(lhs) && plusSigns->contains(rhs));
|
2013-07-25 17:58:37 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 10:46:03 +00:00
|
|
|
// check for LRM 0x200E, RLM 0x200F, ALM 0x061C
|
|
|
|
#define IS_BIDI_MARK(c) (c==0x200E || c==0x200F || c==0x061C)
|
|
|
|
|
2013-09-05 22:13:07 +00:00
|
|
|
#define TRIM_BUFLEN 32
|
2013-09-04 10:46:03 +00:00
|
|
|
UnicodeString& DecimalFormat::trimMarksFromAffix(const UnicodeString& affix, UnicodeString& trimmedAffix) {
|
2013-09-05 22:13:07 +00:00
|
|
|
UChar trimBuf[TRIM_BUFLEN];
|
|
|
|
int32_t affixLen = affix.length();
|
|
|
|
int32_t affixPos, trimLen = 0;
|
2013-09-13 16:21:56 +00:00
|
|
|
|
2013-09-05 22:13:07 +00:00
|
|
|
for (affixPos = 0; affixPos < affixLen; affixPos++) {
|
|
|
|
UChar c = affix.charAt(affixPos);
|
|
|
|
if (!IS_BIDI_MARK(c)) {
|
2014-03-04 05:54:16 +00:00
|
|
|
if (trimLen < TRIM_BUFLEN) {
|
|
|
|
trimBuf[trimLen++] = c;
|
|
|
|
} else {
|
|
|
|
trimLen = 0;
|
|
|
|
break;
|
|
|
|
}
|
2013-09-05 22:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (trimLen > 0)? trimmedAffix.setTo(trimBuf, trimLen): trimmedAffix.setTo(affix);
|
2013-09-04 10:46:03 +00:00
|
|
|
}
|
|
|
|
|
2003-04-04 20:55:10 +00:00
|
|
|
/**
|
|
|
|
* Return the length matched by the given affix, or -1 if none.
|
|
|
|
* Runs of white space in the affix, match runs of white space in
|
|
|
|
* the input. Pattern white space and input white space are
|
|
|
|
* determined differently; see code.
|
|
|
|
* @param affix pattern string, taken as a literal
|
|
|
|
* @param input input text
|
|
|
|
* @param pos offset into input at which to begin matching
|
|
|
|
* @return length of input that matches, or -1 if match failure
|
|
|
|
*/
|
2003-04-21 22:48:16 +00:00
|
|
|
int32_t DecimalFormat::compareSimpleAffix(const UnicodeString& affix,
|
|
|
|
const UnicodeString& input,
|
2011-05-04 12:23:42 +00:00
|
|
|
int32_t pos,
|
2013-09-13 16:21:56 +00:00
|
|
|
UBool lenient) const {
|
2003-04-04 20:55:10 +00:00
|
|
|
int32_t start = pos;
|
2013-09-04 10:46:03 +00:00
|
|
|
UnicodeString trimmedAffix;
|
2013-09-05 22:13:07 +00:00
|
|
|
// For more efficiency we should keep lazily-created trimmed affixes around in
|
|
|
|
// instance variables instead of trimming each time they are used (the next step)
|
2013-09-04 10:46:03 +00:00
|
|
|
trimMarksFromAffix(affix, trimmedAffix);
|
|
|
|
UChar32 affixChar = trimmedAffix.char32At(0);
|
|
|
|
int32_t affixLength = trimmedAffix.length();
|
2011-05-04 12:23:42 +00:00
|
|
|
int32_t inputLength = input.length();
|
|
|
|
int32_t affixCharLength = U16_LENGTH(affixChar);
|
|
|
|
UnicodeSet *affixSet;
|
2013-05-31 23:50:15 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2013-09-13 16:21:56 +00:00
|
|
|
U_ASSERT(fStaticSets != NULL); // should already be loaded
|
|
|
|
|
2013-05-31 23:50:15 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-05-04 12:23:42 +00:00
|
|
|
if (!lenient) {
|
2013-09-13 16:21:56 +00:00
|
|
|
affixSet = fStaticSets->fStrictDashEquivalents;
|
|
|
|
|
2013-09-04 10:46:03 +00:00
|
|
|
// If the trimmedAffix is exactly one character long and that character
|
2011-05-04 12:23:42 +00:00
|
|
|
// is in the dash set and the very next input character is also
|
|
|
|
// in the dash set, return a match.
|
|
|
|
if (affixCharLength == affixLength && affixSet->contains(affixChar)) {
|
2013-09-04 10:46:03 +00:00
|
|
|
UChar32 ic = input.char32At(pos);
|
|
|
|
if (affixSet->contains(ic)) {
|
|
|
|
pos += U16_LENGTH(ic);
|
|
|
|
pos = skipBidiMarks(input, pos); // skip any trailing bidi marks
|
|
|
|
return pos - start;
|
2011-05-04 12:23:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < affixLength; ) {
|
2013-09-04 10:46:03 +00:00
|
|
|
UChar32 c = trimmedAffix.char32At(i);
|
2011-05-04 12:23:42 +00:00
|
|
|
int32_t len = U16_LENGTH(c);
|
|
|
|
if (PatternProps::isWhiteSpace(c)) {
|
|
|
|
// We may have a pattern like: \u200F \u0020
|
|
|
|
// and input text like: \u200F \u0020
|
|
|
|
// Note that U+200F and U+0020 are Pattern_White_Space but only
|
|
|
|
// U+0020 is UWhiteSpace. So we have to first do a direct
|
|
|
|
// match of the run of Pattern_White_Space in the pattern,
|
|
|
|
// then match any extra characters.
|
|
|
|
UBool literalMatch = FALSE;
|
2013-09-04 10:46:03 +00:00
|
|
|
while (pos < inputLength) {
|
|
|
|
UChar32 ic = input.char32At(pos);
|
|
|
|
if (ic == c) {
|
|
|
|
literalMatch = TRUE;
|
|
|
|
i += len;
|
|
|
|
pos += len;
|
|
|
|
if (i == affixLength) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = trimmedAffix.char32At(i);
|
|
|
|
len = U16_LENGTH(c);
|
|
|
|
if (!PatternProps::isWhiteSpace(c)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (IS_BIDI_MARK(ic)) {
|
|
|
|
pos ++; // just skip over this input text
|
|
|
|
} else {
|
2011-05-04 12:23:42 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-05-19 21:06:22 +00:00
|
|
|
}
|
2011-05-04 12:23:42 +00:00
|
|
|
|
|
|
|
// Advance over run in pattern
|
2013-09-04 10:46:03 +00:00
|
|
|
i = skipPatternWhiteSpace(trimmedAffix, i);
|
2011-05-04 12:23:42 +00:00
|
|
|
|
|
|
|
// Advance over run in input text
|
|
|
|
// Must see at least one white space char in input,
|
|
|
|
// unless we've already matched some characters literally.
|
|
|
|
int32_t s = pos;
|
|
|
|
pos = skipUWhiteSpace(input, pos);
|
|
|
|
if (pos == s && !literalMatch) {
|
|
|
|
return -1;
|
2003-05-19 20:04:57 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
// If we skip UWhiteSpace in the input text, we need to skip it in the pattern.
|
|
|
|
// Otherwise, the previous lines may have skipped over text (such as U+00A0) that
|
2013-09-04 10:46:03 +00:00
|
|
|
// is also in the trimmedAffix.
|
|
|
|
i = skipUWhiteSpace(trimmedAffix, i);
|
2011-05-04 12:23:42 +00:00
|
|
|
} else {
|
2013-09-04 10:46:03 +00:00
|
|
|
UBool match = FALSE;
|
|
|
|
while (pos < inputLength) {
|
|
|
|
UChar32 ic = input.char32At(pos);
|
|
|
|
if (!match && ic == c) {
|
|
|
|
i += len;
|
|
|
|
pos += len;
|
|
|
|
match = TRUE;
|
|
|
|
} else if (IS_BIDI_MARK(ic)) {
|
|
|
|
pos++; // just skip over this input text
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!match) {
|
2011-05-04 12:23:42 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
UBool match = FALSE;
|
2013-09-13 16:21:56 +00:00
|
|
|
|
|
|
|
affixSet = fStaticSets->fDashEquivalents;
|
2003-04-04 20:55:10 +00:00
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
if (affixCharLength == affixLength && affixSet->contains(affixChar)) {
|
2013-09-04 10:46:03 +00:00
|
|
|
pos = skipUWhiteSpaceAndMarks(input, pos);
|
|
|
|
UChar32 ic = input.char32At(pos);
|
2013-09-13 16:21:56 +00:00
|
|
|
|
2013-09-04 10:46:03 +00:00
|
|
|
if (affixSet->contains(ic)) {
|
|
|
|
pos += U16_LENGTH(ic);
|
|
|
|
pos = skipBidiMarks(input, pos);
|
|
|
|
return pos - start;
|
2003-04-04 20:55:10 +00:00
|
|
|
}
|
2011-05-04 12:23:42 +00:00
|
|
|
}
|
2008-06-20 00:43:45 +00:00
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
for (int32_t i = 0; i < affixLength; )
|
|
|
|
{
|
2013-09-04 10:46:03 +00:00
|
|
|
//i = skipRuleWhiteSpace(trimmedAffix, i);
|
|
|
|
i = skipUWhiteSpace(trimmedAffix, i);
|
|
|
|
pos = skipUWhiteSpaceAndMarks(input, pos);
|
2011-05-04 12:23:42 +00:00
|
|
|
|
|
|
|
if (i >= affixLength || pos >= inputLength) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-04 10:46:03 +00:00
|
|
|
UChar32 c = trimmedAffix.char32At(i);
|
|
|
|
UChar32 ic = input.char32At(pos);
|
2011-05-04 12:23:42 +00:00
|
|
|
|
2013-09-04 10:46:03 +00:00
|
|
|
if (!equalWithSignCompatibility(ic, c)) {
|
2003-04-04 20:55:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-05-04 12:23:42 +00:00
|
|
|
|
|
|
|
match = TRUE;
|
2013-09-04 10:46:03 +00:00
|
|
|
i += U16_LENGTH(c);
|
|
|
|
pos += U16_LENGTH(ic);
|
|
|
|
pos = skipBidiMarks(input, pos);
|
2011-05-04 12:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (affixLength > 0 && ! match) {
|
|
|
|
return -1;
|
2003-04-04 20:55:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return pos - start;
|
|
|
|
}
|
2000-10-24 16:09:39 +00:00
|
|
|
|
2003-04-21 22:48:16 +00:00
|
|
|
/**
|
2011-04-26 06:39:29 +00:00
|
|
|
* Skip over a run of zero or more Pattern_White_Space characters at
|
2003-04-21 22:48:16 +00:00
|
|
|
* pos in text.
|
|
|
|
*/
|
2011-04-26 06:39:29 +00:00
|
|
|
int32_t DecimalFormat::skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) {
|
|
|
|
const UChar* s = text.getBuffer();
|
|
|
|
return (int32_t)(PatternProps::skipWhiteSpace(s + pos, text.length() - pos) - s);
|
2003-04-21 22:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip over a run of zero or more isUWhiteSpace() characters at pos
|
|
|
|
* in text.
|
|
|
|
*/
|
|
|
|
int32_t DecimalFormat::skipUWhiteSpace(const UnicodeString& text, int32_t pos) {
|
|
|
|
while (pos < text.length()) {
|
|
|
|
UChar32 c = text.char32At(pos);
|
|
|
|
if (!u_isUWhiteSpace(c)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos += U16_LENGTH(c);
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2013-09-04 10:46:03 +00:00
|
|
|
/**
|
|
|
|
* Skip over a run of zero or more isUWhiteSpace() characters or bidi marks at pos
|
|
|
|
* in text.
|
|
|
|
*/
|
|
|
|
int32_t DecimalFormat::skipUWhiteSpaceAndMarks(const UnicodeString& text, int32_t pos) {
|
|
|
|
while (pos < text.length()) {
|
|
|
|
UChar32 c = text.char32At(pos);
|
|
|
|
if (!u_isUWhiteSpace(c) && !IS_BIDI_MARK(c)) { // u_isUWhiteSpace doesn't include LRM,RLM,ALM
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos += U16_LENGTH(c);
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip over a run of zero or more bidi marks at pos in text.
|
|
|
|
*/
|
|
|
|
int32_t DecimalFormat::skipBidiMarks(const UnicodeString& text, int32_t pos) {
|
|
|
|
while (pos < text.length()) {
|
|
|
|
UChar c = text.charAt(pos);
|
|
|
|
if (!IS_BIDI_MARK(c)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2003-04-21 22:48:16 +00:00
|
|
|
/**
|
|
|
|
* Return the length matched by the given affix, or -1 if none.
|
|
|
|
* @param affixPat pattern string
|
|
|
|
* @param input input text
|
|
|
|
* @param pos offset into input at which to begin matching
|
2009-03-10 20:53:27 +00:00
|
|
|
* @param type the currency type to parse against, LONG_NAME only or not.
|
2004-04-15 06:20:42 +00:00
|
|
|
* @param currency return value for parsed currency, for generic
|
|
|
|
* currency parsing mode, or null for normal parsing. In generic
|
|
|
|
* currency parsing mode, any currency is parsed, not just the
|
|
|
|
* currency that this formatter is set to.
|
2003-04-21 22:48:16 +00:00
|
|
|
* @return length of input that matches, or -1 if match failure
|
|
|
|
*/
|
|
|
|
int32_t DecimalFormat::compareComplexAffix(const UnicodeString& affixPat,
|
|
|
|
const UnicodeString& text,
|
2004-04-15 06:20:42 +00:00
|
|
|
int32_t pos,
|
2009-03-10 20:53:27 +00:00
|
|
|
int8_t type,
|
2006-07-10 23:40:17 +00:00
|
|
|
UChar* currency) const
|
|
|
|
{
|
|
|
|
int32_t start = pos;
|
2015-08-25 23:31:01 +00:00
|
|
|
U_ASSERT(currency != NULL || fImpl->fMonetary);
|
2003-04-21 22:48:16 +00:00
|
|
|
|
2010-01-28 19:39:24 +00:00
|
|
|
for (int32_t i=0;
|
2009-02-28 02:07:55 +00:00
|
|
|
i<affixPat.length() && pos >= 0; ) {
|
2003-04-21 22:48:16 +00:00
|
|
|
UChar32 c = affixPat.char32At(i);
|
|
|
|
i += U16_LENGTH(c);
|
|
|
|
|
|
|
|
if (c == kQuote) {
|
|
|
|
U_ASSERT(i <= affixPat.length());
|
|
|
|
c = affixPat.char32At(i);
|
|
|
|
i += U16_LENGTH(c);
|
|
|
|
|
|
|
|
const UnicodeString* affix = NULL;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case kCurrencySign: {
|
2009-02-12 22:55:29 +00:00
|
|
|
// since the currency names in choice format is saved
|
2010-01-28 19:39:24 +00:00
|
|
|
// the same way as other currency names,
|
2009-02-12 22:55:29 +00:00
|
|
|
// do not need to do currency choice parsing here.
|
|
|
|
// the general currency parsing parse against all names,
|
|
|
|
// including names in choice format.
|
2003-04-21 22:48:16 +00:00
|
|
|
UBool intl = i<affixPat.length() &&
|
|
|
|
affixPat.char32At(i) == kCurrencySign;
|
2009-02-12 22:55:29 +00:00
|
|
|
if (intl) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
UBool plural = i<affixPat.length() &&
|
|
|
|
affixPat.char32At(i) == kCurrencySign;
|
|
|
|
if (plural) {
|
|
|
|
++i;
|
|
|
|
intl = FALSE;
|
|
|
|
}
|
2004-04-15 06:20:42 +00:00
|
|
|
// Parse generic currency -- anything for which we
|
|
|
|
// have a display name, or any 3-letter ISO code.
|
2009-02-12 22:55:29 +00:00
|
|
|
// Try to parse display name for our locale; first
|
|
|
|
// determine our locale.
|
|
|
|
const char* loc = fCurrencyPluralInfo->getLocale().getName();
|
|
|
|
ParsePosition ppos(pos);
|
|
|
|
UChar curr[4];
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
// Delegate parse of display name => ISO code to Currency
|
2009-03-10 20:53:27 +00:00
|
|
|
uprv_parseCurrency(loc, text, ppos, type, curr, ec);
|
2009-02-12 22:55:29 +00:00
|
|
|
|
|
|
|
// If parse succeeds, populate currency[0]
|
|
|
|
if (U_SUCCESS(ec) && ppos.getIndex() != pos) {
|
|
|
|
if (currency) {
|
2004-04-15 06:20:42 +00:00
|
|
|
u_strcpy(currency, curr);
|
2011-07-12 03:22:59 +00:00
|
|
|
} else {
|
|
|
|
// The formatter is currency-style but the client has not requested
|
|
|
|
// the value of the parsed currency. In this case, if that value does
|
|
|
|
// not match the formatter's current value, then the parse fails.
|
|
|
|
UChar effectiveCurr[4];
|
|
|
|
getEffectiveCurrency(effectiveCurr, ec);
|
|
|
|
if ( U_FAILURE(ec) || u_strncmp(curr,effectiveCurr,4) != 0 ) {
|
2014-03-04 05:54:16 +00:00
|
|
|
pos = -1;
|
|
|
|
continue;
|
2011-07-12 03:22:59 +00:00
|
|
|
}
|
2004-04-15 06:20:42 +00:00
|
|
|
}
|
2009-02-12 22:55:29 +00:00
|
|
|
pos = ppos.getIndex();
|
2011-05-04 12:23:42 +00:00
|
|
|
} else if (!isLenient()){
|
2009-02-12 22:55:29 +00:00
|
|
|
pos = -1;
|
2003-04-21 22:48:16 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case kPatternPercent:
|
2015-08-25 23:31:01 +00:00
|
|
|
affix = &fImpl->getConstSymbol(DecimalFormatSymbols::kPercentSymbol);
|
2003-04-21 22:48:16 +00:00
|
|
|
break;
|
|
|
|
case kPatternPerMill:
|
2015-08-25 23:31:01 +00:00
|
|
|
affix = &fImpl->getConstSymbol(DecimalFormatSymbols::kPerMillSymbol);
|
2003-04-21 22:48:16 +00:00
|
|
|
break;
|
|
|
|
case kPatternPlus:
|
2015-08-25 23:31:01 +00:00
|
|
|
affix = &fImpl->getConstSymbol(DecimalFormatSymbols::kPlusSignSymbol);
|
2003-04-21 22:48:16 +00:00
|
|
|
break;
|
|
|
|
case kPatternMinus:
|
2015-08-25 23:31:01 +00:00
|
|
|
affix = &fImpl->getConstSymbol(DecimalFormatSymbols::kMinusSignSymbol);
|
2003-04-21 22:48:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// fall through to affix!=0 test, which will fail
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (affix != NULL) {
|
|
|
|
pos = match(text, pos, *affix);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = match(text, pos, c);
|
2011-04-26 06:39:29 +00:00
|
|
|
if (PatternProps::isWhiteSpace(c)) {
|
|
|
|
i = skipPatternWhiteSpace(affixPat, i);
|
2003-04-21 22:48:16 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-10 23:40:17 +00:00
|
|
|
return pos - start;
|
2003-04-21 22:48:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match a single character at text[pos] and return the index of the
|
|
|
|
* next character upon success. Return -1 on failure. If
|
2011-04-26 06:39:29 +00:00
|
|
|
* ch is a Pattern_White_Space then match a run of white space in text.
|
2003-04-21 22:48:16 +00:00
|
|
|
*/
|
|
|
|
int32_t DecimalFormat::match(const UnicodeString& text, int32_t pos, UChar32 ch) {
|
2011-04-26 06:39:29 +00:00
|
|
|
if (PatternProps::isWhiteSpace(ch)) {
|
2003-04-21 22:48:16 +00:00
|
|
|
// Advance over run of white space in input text
|
|
|
|
// Must see at least one white space char in input
|
|
|
|
int32_t s = pos;
|
2011-04-26 06:39:29 +00:00
|
|
|
pos = skipPatternWhiteSpace(text, pos);
|
2003-04-21 22:48:16 +00:00
|
|
|
if (pos == s) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
return (pos >= 0 && text.char32At(pos) == ch) ?
|
|
|
|
(pos + U16_LENGTH(ch)) : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Match a string at text[pos] and return the index of the next
|
|
|
|
* character upon success. Return -1 on failure. Match a run of
|
|
|
|
* white space in str with a run of white space in text.
|
|
|
|
*/
|
|
|
|
int32_t DecimalFormat::match(const UnicodeString& text, int32_t pos, const UnicodeString& str) {
|
|
|
|
for (int32_t i=0; i<str.length() && pos >= 0; ) {
|
|
|
|
UChar32 ch = str.char32At(i);
|
|
|
|
i += U16_LENGTH(ch);
|
2011-04-26 06:39:29 +00:00
|
|
|
if (PatternProps::isWhiteSpace(ch)) {
|
|
|
|
i = skipPatternWhiteSpace(str, i);
|
2003-04-21 22:48:16 +00:00
|
|
|
}
|
|
|
|
pos = match(text, pos, ch);
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2011-05-04 12:23:42 +00:00
|
|
|
UBool DecimalFormat::matchSymbol(const UnicodeString &text, int32_t position, int32_t length, const UnicodeString &symbol,
|
|
|
|
UnicodeSet *sset, UChar32 schar)
|
|
|
|
{
|
|
|
|
if (sset != NULL) {
|
|
|
|
return sset->contains(schar);
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.compare(position, length, symbol) == 0;
|
|
|
|
}
|
|
|
|
|
2011-09-07 14:30:32 +00:00
|
|
|
UBool DecimalFormat::matchDecimal(UChar32 symbolChar,
|
|
|
|
UBool sawDecimal, UChar32 sawDecimalChar,
|
|
|
|
const UnicodeSet *sset, UChar32 schar) {
|
|
|
|
if(sawDecimal) {
|
|
|
|
return schar==sawDecimalChar;
|
|
|
|
} else if(schar==symbolChar) {
|
|
|
|
return TRUE;
|
|
|
|
} else if(sset!=NULL) {
|
|
|
|
return sset->contains(schar);
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UBool DecimalFormat::matchGrouping(UChar32 groupingChar,
|
|
|
|
UBool sawGrouping, UChar32 sawGroupingChar,
|
|
|
|
const UnicodeSet *sset,
|
2011-12-12 23:29:51 +00:00
|
|
|
UChar32 /*decimalChar*/, const UnicodeSet *decimalSet,
|
2011-09-07 14:30:32 +00:00
|
|
|
UChar32 schar) {
|
|
|
|
if(sawGrouping) {
|
|
|
|
return schar==sawGroupingChar; // previously found
|
|
|
|
} else if(schar==groupingChar) {
|
|
|
|
return TRUE; // char from symbols
|
|
|
|
} else if(sset!=NULL) {
|
|
|
|
return sset->contains(schar) && // in groupingSet but...
|
|
|
|
((decimalSet==NULL) || !decimalSet->contains(schar)); // Exclude decimalSet from groupingSet
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-04 12:23:42 +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
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return &fImpl->getDecimalFormatSymbols();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2003-07-12 00:23:05 +00:00
|
|
|
if (symbolsToAdopt == NULL) {
|
|
|
|
return; // do not allow caller to set fSymbols to NULL
|
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->adoptDecimalFormatSymbols(symbolsToAdopt);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Setting the symbols is equlivalent to adopting a newly created localized
|
|
|
|
// symbols.
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::setDecimalFormatSymbols(const DecimalFormatSymbols& symbols)
|
|
|
|
{
|
|
|
|
adoptDecimalFormatSymbols(new DecimalFormatSymbols(symbols));
|
|
|
|
}
|
2009-02-12 22:55:29 +00:00
|
|
|
|
2010-01-28 19:39:24 +00:00
|
|
|
|
|
|
|
const CurrencyPluralInfo*
|
2009-02-12 22:55:29 +00:00
|
|
|
DecimalFormat::getCurrencyPluralInfo(void) const
|
|
|
|
{
|
|
|
|
return fCurrencyPluralInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-28 19:39:24 +00:00
|
|
|
void
|
2009-02-12 22:55:29 +00:00
|
|
|
DecimalFormat::adoptCurrencyPluralInfo(CurrencyPluralInfo* toAdopt)
|
|
|
|
{
|
|
|
|
if (toAdopt != NULL) {
|
2010-01-28 19:39:24 +00:00
|
|
|
delete fCurrencyPluralInfo;
|
2009-02-12 22:55:29 +00:00
|
|
|
fCurrencyPluralInfo = toAdopt;
|
|
|
|
// re-set currency affix patterns and currency affixes.
|
2015-08-25 23:31:01 +00:00
|
|
|
if (fImpl->fMonetary) {
|
2009-02-12 22:55:29 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
if (fAffixPatternsForCurrency) {
|
|
|
|
deleteHashForAffixPattern();
|
|
|
|
}
|
|
|
|
setupCurrencyAffixPatterns(status);
|
|
|
|
}
|
2010-01-28 19:39:24 +00:00
|
|
|
}
|
2009-02-12 22:55:29 +00:00
|
|
|
}
|
|
|
|
|
2010-01-28 19:39:24 +00:00
|
|
|
void
|
2009-02-12 22:55:29 +00:00
|
|
|
DecimalFormat::setCurrencyPluralInfo(const CurrencyPluralInfo& info)
|
|
|
|
{
|
|
|
|
adoptCurrencyPluralInfo(info.clone());
|
2002-05-13 19:33:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the positive prefix of the number pattern.
|
2010-01-28 19:39:24 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
UnicodeString&
|
|
|
|
DecimalFormat::getPositivePrefix(UnicodeString& result) const
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getPositivePrefix(result);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-01-28 19:39:24 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Sets the positive prefix of the number pattern.
|
2010-01-28 19:39:24 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::setPositivePrefix(const UnicodeString& newValue)
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setPositivePrefix(newValue);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getNegativePrefix(result);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setNegativePrefix(newValue);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getPositiveSuffix(result);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setPositiveSuffix(newValue);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getNegativeSuffix(result);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setNegativeSuffix(newValue);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the multiplier of the number pattern.
|
2010-02-26 02:29:00 +00:00
|
|
|
// Multipliers are stored as decimal numbers (DigitLists) because that
|
|
|
|
// is the most convenient for muliplying or dividing the numbers to be formatted.
|
|
|
|
// A NULL multiplier implies one, and the scaling operations are skipped.
|
2000-08-07 23:06:28 +00:00
|
|
|
|
2010-02-26 02:29:00 +00:00
|
|
|
int32_t
|
|
|
|
DecimalFormat::getMultiplier() const
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getMultiplier();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setMultiplier(newValue);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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
|
|
|
|
*/
|
2004-03-29 23:23:04 +00:00
|
|
|
double DecimalFormat::getRoundingIncrement() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getRoundingIncrement();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setRoundingIncrement(newValue);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the rounding mode.
|
|
|
|
* @return A rounding mode
|
|
|
|
* @see #setRoundingIncrement
|
|
|
|
* @see #getRoundingIncrement
|
|
|
|
* @see #setRoundingMode
|
|
|
|
*/
|
2004-03-31 22:09:40 +00:00
|
|
|
DecimalFormat::ERoundingMode DecimalFormat::getRoundingMode() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getRoundingMode();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setRoundingMode(roundingMode);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2004-03-29 23:23:04 +00:00
|
|
|
int32_t DecimalFormat::getFormatWidth() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getFormatWidth();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2015-08-25 23:31:01 +00:00
|
|
|
int32_t formatWidth = (width > 0) ? width : 0;
|
|
|
|
fImpl->setFormatWidth(formatWidth);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2004-03-29 23:23:04 +00:00
|
|
|
UnicodeString DecimalFormat::getPadCharacterString() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return UnicodeString(fImpl->getPadCharacter());
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-04-15 15:58:20 +00:00
|
|
|
void DecimalFormat::setPadCharacter(const UnicodeString &padChar) {
|
2017-09-22 18:00:51 +00:00
|
|
|
UChar32 pad;
|
2001-09-17 21:50:19 +00:00
|
|
|
if (padChar.length() > 0) {
|
2015-08-25 23:31:01 +00:00
|
|
|
pad = padChar.char32At(0);
|
2001-09-17 21:50:19 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-08-25 23:31:01 +00:00
|
|
|
pad = kDefaultPad;
|
2001-09-17 21:50:19 +00:00
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setPadCharacter(pad);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DecimalFormat::EPadPosition fromPadPosition(DigitAffixesAndPadding::EPadPosition padPos) {
|
|
|
|
switch (padPos) {
|
|
|
|
case DigitAffixesAndPadding::kPadBeforePrefix:
|
|
|
|
return DecimalFormat::kPadBeforePrefix;
|
|
|
|
case DigitAffixesAndPadding::kPadAfterPrefix:
|
|
|
|
return DecimalFormat::kPadAfterPrefix;
|
|
|
|
case DigitAffixesAndPadding::kPadBeforeSuffix:
|
|
|
|
return DecimalFormat::kPadBeforeSuffix;
|
|
|
|
case DigitAffixesAndPadding::kPadAfterSuffix:
|
|
|
|
return DecimalFormat::kPadAfterSuffix;
|
|
|
|
default:
|
|
|
|
U_ASSERT(FALSE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return DecimalFormat::kPadBeforePrefix;
|
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
|
|
|
|
*/
|
2004-03-29 23:23:04 +00:00
|
|
|
DecimalFormat::EPadPosition DecimalFormat::getPadPosition() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fromPadPosition(fImpl->getPadPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
static DigitAffixesAndPadding::EPadPosition toPadPosition(DecimalFormat::EPadPosition padPos) {
|
|
|
|
switch (padPos) {
|
|
|
|
case DecimalFormat::kPadBeforePrefix:
|
|
|
|
return DigitAffixesAndPadding::kPadBeforePrefix;
|
|
|
|
case DecimalFormat::kPadAfterPrefix:
|
|
|
|
return DigitAffixesAndPadding::kPadAfterPrefix;
|
|
|
|
case DecimalFormat::kPadBeforeSuffix:
|
|
|
|
return DigitAffixesAndPadding::kPadBeforeSuffix;
|
|
|
|
case DecimalFormat::kPadAfterSuffix:
|
|
|
|
return DigitAffixesAndPadding::kPadAfterSuffix;
|
|
|
|
default:
|
|
|
|
U_ASSERT(FALSE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return DigitAffixesAndPadding::kPadBeforePrefix;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* <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) {
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setPadPosition(toPadPosition(padPos));
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2013-09-04 00:01:26 +00:00
|
|
|
UBool DecimalFormat::isScientificNotation() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->isScientificNotation();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setScientificNotation(useScientific);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2004-03-29 23:23:04 +00:00
|
|
|
int8_t DecimalFormat::getMinimumExponentDigits() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getMinimumExponentDigits();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2015-08-25 23:31:01 +00:00
|
|
|
int32_t minExponentDigits = (int8_t)((minExpDig > 0) ? minExpDig : 1);
|
|
|
|
fImpl->setMinimumExponentDigits(minExponentDigits);
|
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
|
|
|
|
*/
|
2013-09-04 00:01:26 +00:00
|
|
|
UBool DecimalFormat::isExponentSignAlwaysShown() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->isExponentSignAlwaysShown();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setExponentSignAlwaysShown(expSignAlways);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the grouping size of the number pattern. For example, thousand or 10
|
|
|
|
// thousand groupings.
|
2010-01-28 19:39:24 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
int32_t
|
|
|
|
DecimalFormat::getGroupingSize() const
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getGroupingSize();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2010-01-28 19:39:24 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Gets the grouping size of the number pattern.
|
2010-01-28 19:39:24 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::setGroupingSize(int32_t newValue)
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setGroupingSize(newValue);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getSecondaryGroupingSize();
|
2000-06-07 00:15:59 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setSecondaryGroupingSize(newValue);
|
2000-06-07 00:15:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-11 19:18:00 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
int32_t
|
|
|
|
DecimalFormat::getMinimumGroupingDigits() const
|
|
|
|
{
|
|
|
|
return fImpl->getMinimumGroupingDigits();
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::setMinimumGroupingDigits(int32_t newValue)
|
|
|
|
{
|
|
|
|
fImpl->setMinimumGroupingDigits(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
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->isDecimalSeparatorAlwaysShown();
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setDecimalSeparatorAlwaysShown(newValue);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
2014-08-30 21:53:10 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Checks if decimal point pattern match is required
|
|
|
|
UBool
|
|
|
|
DecimalFormat::isDecimalPatternMatchRequired(void) const
|
|
|
|
{
|
2017-09-22 18:00:51 +00:00
|
|
|
return static_cast<UBool>(fBoolFlags.contains(UNUM_PARSE_DECIMAL_MARK_REQUIRED));
|
2014-08-30 21:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Checks if decimal point pattern match is required
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::setDecimalPatternMatchRequired(UBool newValue)
|
|
|
|
{
|
|
|
|
fBoolFlags.set(UNUM_PARSE_DECIMAL_MARK_REQUIRED, newValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->toPattern(result);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
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
|
|
|
|
{
|
2015-08-25 23:31:01 +00:00
|
|
|
// toLocalizedPattern is deprecated, so we just make it the same as
|
|
|
|
// toPattern.
|
|
|
|
return fImpl->toPattern(result);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2000-08-07 23:06:28 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2015-08-25 23:31:01 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::applyPattern(const UnicodeString& pattern, UErrorCode& status)
|
|
|
|
{
|
2016-09-15 03:55:22 +00:00
|
|
|
if (pattern.indexOf(kCurrencySign) != -1) {
|
|
|
|
handleCurrencySignInPattern(status);
|
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->applyPattern(pattern, 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,
|
2010-01-28 19:39:24 +00:00
|
|
|
UParseError& parseError,
|
2001-08-16 00:55:16 +00:00
|
|
|
UErrorCode& status)
|
|
|
|
{
|
2016-09-15 03:55:22 +00:00
|
|
|
if (pattern.indexOf(kCurrencySign) != -1) {
|
|
|
|
handleCurrencySignInPattern(status);
|
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->applyPattern(pattern, parseError, status);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status)
|
|
|
|
{
|
2016-09-15 03:55:22 +00:00
|
|
|
if (pattern.indexOf(kCurrencySign) != -1) {
|
|
|
|
handleCurrencySignInPattern(status);
|
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->applyLocalizedPattern(pattern, status);
|
2001-08-16 00:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::applyLocalizedPattern(const UnicodeString& pattern,
|
|
|
|
UParseError& parseError,
|
|
|
|
UErrorCode& status)
|
|
|
|
{
|
2016-09-15 03:55:22 +00:00
|
|
|
if (pattern.indexOf(kCurrencySign) != -1) {
|
|
|
|
handleCurrencySignInPattern(status);
|
|
|
|
}
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->applyLocalizedPattern(pattern, 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
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the maximum number of digits allowed in the integer portion of a
|
2013-09-17 21:56:23 +00:00
|
|
|
* number.
|
1999-08-16 21:50:52 +00:00
|
|
|
* @see NumberFormat#setMaximumIntegerDigits
|
|
|
|
*/
|
|
|
|
void DecimalFormat::setMaximumIntegerDigits(int32_t newValue) {
|
2015-08-25 23:31:01 +00:00
|
|
|
newValue = _min(newValue, gDefaultMaxIntegerDigits);
|
|
|
|
NumberFormat::setMaximumIntegerDigits(newValue);
|
|
|
|
fImpl->updatePrecision();
|
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) {
|
2015-08-25 23:31:01 +00:00
|
|
|
newValue = _min(newValue, kDoubleIntegerDigits);
|
|
|
|
NumberFormat::setMinimumIntegerDigits(newValue);
|
|
|
|
fImpl->updatePrecision();
|
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) {
|
2015-08-25 23:31:01 +00:00
|
|
|
newValue = _min(newValue, kDoubleFractionDigits);
|
|
|
|
NumberFormat::setMaximumFractionDigits(newValue);
|
|
|
|
fImpl->updatePrecision();
|
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) {
|
2015-08-25 23:31:01 +00:00
|
|
|
newValue = _min(newValue, kDoubleFractionDigits);
|
|
|
|
NumberFormat::setMinimumFractionDigits(newValue);
|
|
|
|
fImpl->updatePrecision();
|
2004-03-25 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t DecimalFormat::getMinimumSignificantDigits() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getMinimumSignificantDigits();
|
2004-03-25 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t DecimalFormat::getMaximumSignificantDigits() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getMaximumSignificantDigits();
|
2004-03-25 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecimalFormat::setMinimumSignificantDigits(int32_t min) {
|
|
|
|
if (min < 1) {
|
2010-01-28 19:39:24 +00:00
|
|
|
min = 1;
|
2004-03-25 17:42:28 +00:00
|
|
|
}
|
2004-04-03 17:56:14 +00:00
|
|
|
// pin max sig dig to >= min
|
2015-08-25 23:31:01 +00:00
|
|
|
int32_t max = _max(fImpl->fMaxSigDigits, min);
|
|
|
|
fImpl->setMinMaxSignificantDigits(min, max);
|
2004-03-25 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DecimalFormat::setMaximumSignificantDigits(int32_t max) {
|
|
|
|
if (max < 1) {
|
|
|
|
max = 1;
|
|
|
|
}
|
2004-04-03 17:56:14 +00:00
|
|
|
// pin min sig dig to 1..max
|
2015-08-25 23:31:01 +00:00
|
|
|
U_ASSERT(fImpl->fMinSigDigits >= 1);
|
|
|
|
int32_t min = _min(fImpl->fMinSigDigits, max);
|
|
|
|
fImpl->setMinMaxSignificantDigits(min, max);
|
2004-04-03 17:56:14 +00:00
|
|
|
}
|
|
|
|
|
2004-04-12 23:37:44 +00:00
|
|
|
UBool DecimalFormat::areSignificantDigitsUsed() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->areSignificantDigitsUsed();
|
2004-03-25 17:42:28 +00:00
|
|
|
}
|
|
|
|
|
2004-04-12 23:37:44 +00:00
|
|
|
void DecimalFormat::setSignificantDigitsUsed(UBool useSignificantDigits) {
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setSignificantDigitsUsed(useSignificantDigits);
|
2002-05-13 19:33:05 +00:00
|
|
|
}
|
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
void DecimalFormat::setCurrency(const UChar* theCurrency, UErrorCode& ec) {
|
|
|
|
// set the currency before compute affixes to get the right currency names
|
|
|
|
NumberFormat::setCurrency(theCurrency, ec);
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->updateCurrency(ec);
|
2009-02-12 22:55:29 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 21:40:34 +00:00
|
|
|
void DecimalFormat::setCurrencyUsage(UCurrencyUsage newContext, UErrorCode* ec){
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setCurrencyUsage(newContext, *ec);
|
2014-06-12 21:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UCurrencyUsage DecimalFormat::getCurrencyUsage() const {
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getCurrencyUsage();
|
2014-06-12 21:40:34 +00:00
|
|
|
}
|
|
|
|
|
2004-05-27 22:03:26 +00:00
|
|
|
// Deprecated variant with no UErrorCode parameter
|
|
|
|
void DecimalFormat::setCurrency(const UChar* theCurrency) {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
setCurrency(theCurrency, ec);
|
|
|
|
}
|
|
|
|
|
2008-01-11 18:44:41 +00:00
|
|
|
void DecimalFormat::getEffectiveCurrency(UChar* result, UErrorCode& ec) const {
|
2015-08-25 23:31:01 +00:00
|
|
|
if (fImpl->fSymbols == NULL) {
|
2008-02-23 19:15:18 +00:00
|
|
|
ec = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return;
|
2008-01-11 18:44:41 +00:00
|
|
|
}
|
|
|
|
ec = U_ZERO_ERROR;
|
2008-02-23 19:15:18 +00:00
|
|
|
const UChar* c = getCurrency();
|
2004-04-15 06:20:42 +00:00
|
|
|
if (*c == 0) {
|
|
|
|
const UnicodeString &intl =
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol);
|
2004-04-15 06:20:42 +00:00
|
|
|
c = intl.getBuffer(); // ok for intl to go out of scope
|
|
|
|
}
|
|
|
|
u_strncpy(result, c, 3);
|
|
|
|
result[3] = 0;
|
|
|
|
}
|
|
|
|
|
2009-02-12 22:55:29 +00:00
|
|
|
Hashtable*
|
|
|
|
DecimalFormat::initHashForAffixPattern(UErrorCode& status) {
|
|
|
|
if ( U_FAILURE(status) ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Hashtable* hTable;
|
|
|
|
if ( (hTable = new Hashtable(TRUE, status)) == NULL ) {
|
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-03-01 20:55:43 +00:00
|
|
|
if ( U_FAILURE(status) ) {
|
|
|
|
delete hTable;
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-02-16 23:43:22 +00:00
|
|
|
hTable->setValueComparator(decimfmtAffixPatternValueComparator);
|
2009-02-12 22:55:29 +00:00
|
|
|
return hTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-01-28 19:39:24 +00:00
|
|
|
DecimalFormat::deleteHashForAffixPattern()
|
2009-02-12 22:55:29 +00:00
|
|
|
{
|
|
|
|
if ( fAffixPatternsForCurrency == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-09 23:54:56 +00:00
|
|
|
int32_t pos = UHASH_FIRST;
|
2009-02-12 22:55:29 +00:00
|
|
|
const UHashElement* element = NULL;
|
|
|
|
while ( (element = fAffixPatternsForCurrency->nextElement(pos)) != NULL ) {
|
|
|
|
const UHashTok valueTok = element->value;
|
|
|
|
const AffixPatternsForCurrency* value = (AffixPatternsForCurrency*)valueTok.pointer;
|
|
|
|
delete value;
|
|
|
|
}
|
|
|
|
delete fAffixPatternsForCurrency;
|
|
|
|
fAffixPatternsForCurrency = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::copyHashForAffixPattern(const Hashtable* source,
|
|
|
|
Hashtable* target,
|
|
|
|
UErrorCode& status) {
|
|
|
|
if ( U_FAILURE(status) ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-12-09 23:54:56 +00:00
|
|
|
int32_t pos = UHASH_FIRST;
|
2009-02-12 22:55:29 +00:00
|
|
|
const UHashElement* element = NULL;
|
|
|
|
if ( source ) {
|
|
|
|
while ( (element = source->nextElement(pos)) != NULL ) {
|
|
|
|
const UHashTok keyTok = element->key;
|
|
|
|
const UnicodeString* key = (UnicodeString*)keyTok.pointer;
|
|
|
|
const UHashTok valueTok = element->value;
|
|
|
|
const AffixPatternsForCurrency* value = (AffixPatternsForCurrency*)valueTok.pointer;
|
|
|
|
AffixPatternsForCurrency* copy = new AffixPatternsForCurrency(
|
|
|
|
value->negPrefixPatternForCurrency,
|
|
|
|
value->negSuffixPatternForCurrency,
|
|
|
|
value->posPrefixPatternForCurrency,
|
2009-03-10 20:53:27 +00:00
|
|
|
value->posSuffixPatternForCurrency,
|
|
|
|
value->patternType);
|
2009-02-12 22:55:29 +00:00
|
|
|
target->put(UnicodeString(*key), copy, status);
|
|
|
|
if ( U_FAILURE(status) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-06 00:12:57 +00:00
|
|
|
void
|
|
|
|
DecimalFormat::setGroupingUsed(UBool newValue) {
|
|
|
|
NumberFormat::setGroupingUsed(newValue);
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->updateGrouping();
|
2014-03-06 00:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::setParseIntegerOnly(UBool newValue) {
|
|
|
|
NumberFormat::setParseIntegerOnly(newValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecimalFormat::setContext(UDisplayContext value, UErrorCode& status) {
|
|
|
|
NumberFormat::setContext(value, status);
|
|
|
|
}
|
|
|
|
|
2012-10-12 19:52:43 +00:00
|
|
|
DecimalFormat& DecimalFormat::setAttribute( UNumberFormatAttribute attr,
|
|
|
|
int32_t newValue,
|
|
|
|
UErrorCode &status) {
|
|
|
|
if(U_FAILURE(status)) return *this;
|
|
|
|
|
|
|
|
switch(attr) {
|
|
|
|
case UNUM_LENIENT_PARSE:
|
|
|
|
setLenient(newValue!=0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_PARSE_INT_ONLY:
|
|
|
|
setParseIntegerOnly(newValue!=0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_GROUPING_USED:
|
|
|
|
setGroupingUsed(newValue!=0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_DECIMAL_ALWAYS_SHOWN:
|
|
|
|
setDecimalSeparatorAlwaysShown(newValue!=0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MAX_INTEGER_DIGITS:
|
|
|
|
setMaximumIntegerDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MIN_INTEGER_DIGITS:
|
|
|
|
setMinimumIntegerDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_INTEGER_DIGITS:
|
|
|
|
setMinimumIntegerDigits(newValue);
|
|
|
|
setMaximumIntegerDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MAX_FRACTION_DIGITS:
|
|
|
|
setMaximumFractionDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MIN_FRACTION_DIGITS:
|
|
|
|
setMinimumFractionDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_FRACTION_DIGITS:
|
|
|
|
setMinimumFractionDigits(newValue);
|
|
|
|
setMaximumFractionDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_SIGNIFICANT_DIGITS_USED:
|
|
|
|
setSignificantDigitsUsed(newValue!=0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MAX_SIGNIFICANT_DIGITS:
|
|
|
|
setMaximumSignificantDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MIN_SIGNIFICANT_DIGITS:
|
|
|
|
setMinimumSignificantDigits(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MULTIPLIER:
|
|
|
|
setMultiplier(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_GROUPING_SIZE:
|
|
|
|
setGroupingSize(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_ROUNDING_MODE:
|
|
|
|
setRoundingMode((DecimalFormat::ERoundingMode)newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_FORMAT_WIDTH:
|
|
|
|
setFormatWidth(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_PADDING_POSITION:
|
|
|
|
/** The position at which padding will take place. */
|
|
|
|
setPadPosition((DecimalFormat::EPadPosition)newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_SECONDARY_GROUPING_SIZE:
|
|
|
|
setSecondaryGroupingSize(newValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if UCONFIG_HAVE_PARSEALLINPUT
|
|
|
|
case UNUM_PARSE_ALL_INPUT:
|
|
|
|
setParseAllInput((UNumberFormatAttributeValue)newValue);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* These are stored in fBoolFlags */
|
|
|
|
case UNUM_PARSE_NO_EXPONENT:
|
2012-10-12 21:53:41 +00:00
|
|
|
case UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS:
|
2014-08-30 21:53:10 +00:00
|
|
|
case UNUM_PARSE_DECIMAL_MARK_REQUIRED:
|
2012-10-12 19:52:43 +00:00
|
|
|
if(!fBoolFlags.isValidValue(newValue)) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
} else {
|
2015-08-25 23:31:01 +00:00
|
|
|
if (attr == UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS) {
|
|
|
|
fImpl->setFailIfMoreThanMaxDigits((UBool) newValue);
|
|
|
|
}
|
2012-10-12 19:52:43 +00:00
|
|
|
fBoolFlags.set(attr, newValue);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-02-07 19:48:29 +00:00
|
|
|
case UNUM_SCALE:
|
2015-08-25 23:31:01 +00:00
|
|
|
fImpl->setScale(newValue);
|
2013-02-07 19:48:29 +00:00
|
|
|
break;
|
|
|
|
|
2014-06-12 21:40:34 +00:00
|
|
|
case UNUM_CURRENCY_USAGE:
|
|
|
|
setCurrencyUsage((UCurrencyUsage)newValue, &status);
|
2015-09-11 19:18:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UNUM_MINIMUM_GROUPING_DIGITS:
|
|
|
|
setMinimumGroupingDigits(newValue);
|
|
|
|
break;
|
2014-06-12 21:40:34 +00:00
|
|
|
|
2012-10-12 19:52:43 +00:00
|
|
|
default:
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t DecimalFormat::getAttribute( UNumberFormatAttribute attr,
|
|
|
|
UErrorCode &status ) const {
|
|
|
|
if(U_FAILURE(status)) return -1;
|
|
|
|
switch(attr) {
|
|
|
|
case UNUM_LENIENT_PARSE:
|
|
|
|
return isLenient();
|
|
|
|
|
|
|
|
case UNUM_PARSE_INT_ONLY:
|
|
|
|
return isParseIntegerOnly();
|
|
|
|
|
|
|
|
case UNUM_GROUPING_USED:
|
|
|
|
return isGroupingUsed();
|
|
|
|
|
|
|
|
case UNUM_DECIMAL_ALWAYS_SHOWN:
|
|
|
|
return isDecimalSeparatorAlwaysShown();
|
|
|
|
|
|
|
|
case UNUM_MAX_INTEGER_DIGITS:
|
|
|
|
return getMaximumIntegerDigits();
|
|
|
|
|
|
|
|
case UNUM_MIN_INTEGER_DIGITS:
|
|
|
|
return getMinimumIntegerDigits();
|
|
|
|
|
|
|
|
case UNUM_INTEGER_DIGITS:
|
|
|
|
// TBD: what should this return?
|
|
|
|
return getMinimumIntegerDigits();
|
|
|
|
|
|
|
|
case UNUM_MAX_FRACTION_DIGITS:
|
|
|
|
return getMaximumFractionDigits();
|
|
|
|
|
|
|
|
case UNUM_MIN_FRACTION_DIGITS:
|
|
|
|
return getMinimumFractionDigits();
|
|
|
|
|
|
|
|
case UNUM_FRACTION_DIGITS:
|
|
|
|
// TBD: what should this return?
|
|
|
|
return getMinimumFractionDigits();
|
|
|
|
|
|
|
|
case UNUM_SIGNIFICANT_DIGITS_USED:
|
|
|
|
return areSignificantDigitsUsed();
|
|
|
|
|
|
|
|
case UNUM_MAX_SIGNIFICANT_DIGITS:
|
|
|
|
return getMaximumSignificantDigits();
|
|
|
|
|
|
|
|
case UNUM_MIN_SIGNIFICANT_DIGITS:
|
|
|
|
return getMinimumSignificantDigits();
|
|
|
|
|
|
|
|
case UNUM_MULTIPLIER:
|
|
|
|
return getMultiplier();
|
|
|
|
|
|
|
|
case UNUM_GROUPING_SIZE:
|
|
|
|
return getGroupingSize();
|
|
|
|
|
|
|
|
case UNUM_ROUNDING_MODE:
|
|
|
|
return getRoundingMode();
|
|
|
|
|
|
|
|
case UNUM_FORMAT_WIDTH:
|
|
|
|
return getFormatWidth();
|
|
|
|
|
|
|
|
case UNUM_PADDING_POSITION:
|
|
|
|
return getPadPosition();
|
|
|
|
|
|
|
|
case UNUM_SECONDARY_GROUPING_SIZE:
|
|
|
|
return getSecondaryGroupingSize();
|
|
|
|
|
|
|
|
/* These are stored in fBoolFlags */
|
|
|
|
case UNUM_PARSE_NO_EXPONENT:
|
2012-10-12 21:53:41 +00:00
|
|
|
case UNUM_FORMAT_FAIL_IF_MORE_THAN_MAX_DIGITS:
|
2014-08-30 21:53:10 +00:00
|
|
|
case UNUM_PARSE_DECIMAL_MARK_REQUIRED:
|
2012-10-12 19:52:43 +00:00
|
|
|
return fBoolFlags.get(attr);
|
|
|
|
|
2013-02-07 19:48:29 +00:00
|
|
|
case UNUM_SCALE:
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->fScale;
|
2013-02-07 19:48:29 +00:00
|
|
|
|
2014-06-12 21:40:34 +00:00
|
|
|
case UNUM_CURRENCY_USAGE:
|
2015-08-25 23:31:01 +00:00
|
|
|
return fImpl->getCurrencyUsage();
|
2014-06-12 21:40:34 +00:00
|
|
|
|
2015-09-11 19:18:00 +00:00
|
|
|
case UNUM_MINIMUM_GROUPING_DIGITS:
|
|
|
|
return getMinimumGroupingDigits();
|
|
|
|
|
2012-10-12 19:52:43 +00:00
|
|
|
default:
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1; /* undefined */
|
|
|
|
}
|
|
|
|
|
2012-05-30 00:41:57 +00:00
|
|
|
#if UCONFIG_HAVE_PARSEALLINPUT
|
|
|
|
void DecimalFormat::setParseAllInput(UNumberFormatAttributeValue value) {
|
|
|
|
fParseAllInput = value;
|
|
|
|
}
|
|
|
|
#endif
|
2009-02-12 22:55:29 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
//eof
|