mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-18 11:14:22 +00:00
ICU-4017 Fix failure behavior of explicitly requested non-existent currency
X-SVN-Rev: 16133
This commit is contained in:
parent
4bbfad48e4
commit
ded716bf2c
4 changed files with 65 additions and 49 deletions
|
@ -866,10 +866,11 @@ NumberFormat::makeInstance(const Locale& desiredLocale,
|
|||
int32_t bufCap = 20;
|
||||
const char* locName = desiredLocale.getName();
|
||||
bufCap = uloc_getKeywordValue(locName, "currency", buf, bufCap, &status);
|
||||
if(bufCap > 0){
|
||||
if(U_SUCCESS(status) && bufCap > 0) {
|
||||
/* An explicit currency was requested */
|
||||
ResourceBundle currencies(resource.getWithFallback("Currencies", status));
|
||||
ResourceBundle currency(currencies.getWithFallback(buf,status));
|
||||
if(currency.getSize()>2){
|
||||
if(U_SUCCESS(status) && currency.getSize()>2){
|
||||
ResourceBundle elements = currency.get(2, status);
|
||||
UnicodeString currPattern = elements.getStringEx((int32_t)0, status);
|
||||
UnicodeString decimalSep = elements.getStringEx((int32_t)1, status);
|
||||
|
@ -880,6 +881,7 @@ NumberFormat::makeInstance(const Locale& desiredLocale,
|
|||
pattern = currPattern;
|
||||
}
|
||||
}
|
||||
/* else An explicit currency was requested and is unknown or locale data is malformed. */
|
||||
}
|
||||
}
|
||||
if (U_FAILURE(status)) {
|
||||
|
|
|
@ -39,72 +39,72 @@ unum_open( UNumberFormatStyle style,
|
|||
UParseError* parseErr,
|
||||
UErrorCode* status)
|
||||
{
|
||||
|
||||
|
||||
if(U_FAILURE(*status))
|
||||
{
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
UNumberFormat *retVal = 0;
|
||||
|
||||
|
||||
switch(style) {
|
||||
case UNUM_DECIMAL:
|
||||
if(locale == 0)
|
||||
retVal = (UNumberFormat*)NumberFormat::createInstance(*status);
|
||||
else
|
||||
retVal = (UNumberFormat*)NumberFormat::createInstance(Locale(locale),
|
||||
*status);
|
||||
break;
|
||||
|
||||
if(locale == 0)
|
||||
retVal = (UNumberFormat*)NumberFormat::createInstance(*status);
|
||||
else
|
||||
retVal = (UNumberFormat*)NumberFormat::createInstance(Locale(locale),
|
||||
*status);
|
||||
break;
|
||||
|
||||
case UNUM_CURRENCY:
|
||||
if(locale == 0)
|
||||
retVal = (UNumberFormat*)NumberFormat::createCurrencyInstance(*status);
|
||||
else
|
||||
retVal = (UNumberFormat*)NumberFormat::createCurrencyInstance(Locale(locale),
|
||||
*status);
|
||||
break;
|
||||
|
||||
if(locale == 0)
|
||||
retVal = (UNumberFormat*)NumberFormat::createCurrencyInstance(*status);
|
||||
else
|
||||
retVal = (UNumberFormat*)NumberFormat::createCurrencyInstance(Locale(locale),
|
||||
*status);
|
||||
break;
|
||||
|
||||
case UNUM_PERCENT:
|
||||
if(locale == 0)
|
||||
retVal = (UNumberFormat*)NumberFormat::createPercentInstance(*status);
|
||||
else
|
||||
retVal = (UNumberFormat*)NumberFormat::createPercentInstance(Locale(locale),
|
||||
*status);
|
||||
break;
|
||||
|
||||
if(locale == 0)
|
||||
retVal = (UNumberFormat*)NumberFormat::createPercentInstance(*status);
|
||||
else
|
||||
retVal = (UNumberFormat*)NumberFormat::createPercentInstance(Locale(locale),
|
||||
*status);
|
||||
break;
|
||||
|
||||
case UNUM_SCIENTIFIC:
|
||||
if(locale == 0)
|
||||
retVal = (UNumberFormat*)NumberFormat::createScientificInstance(*status);
|
||||
else
|
||||
retVal = (UNumberFormat*)NumberFormat::createScientificInstance(Locale(locale),
|
||||
*status);
|
||||
break;
|
||||
if(locale == 0)
|
||||
retVal = (UNumberFormat*)NumberFormat::createScientificInstance(*status);
|
||||
else
|
||||
retVal = (UNumberFormat*)NumberFormat::createScientificInstance(Locale(locale),
|
||||
*status);
|
||||
break;
|
||||
|
||||
case UNUM_PATTERN_DECIMAL: {
|
||||
UParseError tErr;
|
||||
/* UnicodeString can handle the case when patternLength = -1. */
|
||||
const UnicodeString pat(pattern, patternLength);
|
||||
DecimalFormatSymbols *syms = 0;
|
||||
|
||||
|
||||
if(parseErr==NULL){
|
||||
parseErr = &tErr;
|
||||
}
|
||||
|
||||
|
||||
if(locale == 0)
|
||||
syms = new DecimalFormatSymbols(*status);
|
||||
else
|
||||
syms = new DecimalFormatSymbols(Locale(locale), *status);
|
||||
|
||||
|
||||
if(syms == 0) {
|
||||
*status = U_MEMORY_ALLOCATION_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
retVal = (UNumberFormat*)new DecimalFormat(pat, syms, *parseErr, *status);
|
||||
if(retVal == 0) {
|
||||
delete syms;
|
||||
}
|
||||
} break;
|
||||
} break;
|
||||
|
||||
#if U_HAVE_RBNF
|
||||
case UNUM_PATTERN_RULEBASED: {
|
||||
|
@ -120,27 +120,27 @@ unum_open( UNumberFormatStyle style,
|
|||
} break;
|
||||
|
||||
case UNUM_SPELLOUT:
|
||||
retVal = (UNumberFormat*)new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale(locale), *status);
|
||||
break;
|
||||
retVal = (UNumberFormat*)new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale(locale), *status);
|
||||
break;
|
||||
|
||||
case UNUM_ORDINAL:
|
||||
retVal = (UNumberFormat*)new RuleBasedNumberFormat(URBNF_ORDINAL, Locale(locale), *status);
|
||||
break;
|
||||
retVal = (UNumberFormat*)new RuleBasedNumberFormat(URBNF_ORDINAL, Locale(locale), *status);
|
||||
break;
|
||||
|
||||
case UNUM_DURATION:
|
||||
retVal = (UNumberFormat*)new RuleBasedNumberFormat(URBNF_DURATION, Locale(locale), *status);
|
||||
break;
|
||||
retVal = (UNumberFormat*)new RuleBasedNumberFormat(URBNF_DURATION, Locale(locale), *status);
|
||||
break;
|
||||
#endif
|
||||
|
||||
default:
|
||||
*status = U_UNSUPPORTED_ERROR;
|
||||
return 0;
|
||||
*status = U_UNSUPPORTED_ERROR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(retVal == 0) {
|
||||
*status = U_MEMORY_ALLOCATION_ERROR;
|
||||
|
||||
if(retVal == 0 && U_SUCCESS(*status)) {
|
||||
*status = U_MEMORY_ALLOCATION_ERROR;
|
||||
}
|
||||
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,7 @@ void addNumForTest(TestNode** root)
|
|||
TESTCASE(TestSignificantDigits);
|
||||
TESTCASE(TestNumberFormatPadding);
|
||||
TESTCASE(TestInt64Format);
|
||||
TESTCASE(TestNonExistentCurrency);
|
||||
TESTCASE(TestRBNFFormat);
|
||||
}
|
||||
|
||||
|
@ -1163,6 +1164,17 @@ static void test_fmt(UNumberFormat* fmt, UBool isDecimal) {
|
|||
}
|
||||
}
|
||||
|
||||
static void TestNonExistentCurrency() {
|
||||
UNumberFormat *format;
|
||||
UErrorCode status = U_ZERO_ERROR;
|
||||
|
||||
/* Get a non-existent currency and make sure it fails correctly */
|
||||
format = unum_open(UNUM_CURRENCY, NULL, 0, "th_TH@currency=QQQ", NULL, &status);
|
||||
if (format != NULL || status != U_MISSING_RESOURCE_ERROR) {
|
||||
log_err("unum_open did not return expected result for non-existent requested currency: '%s'\n", u_errorName(status));
|
||||
}
|
||||
}
|
||||
|
||||
static void TestRBNFFormat() {
|
||||
UErrorCode status;
|
||||
UParseError perr;
|
||||
|
|
|
@ -43,6 +43,8 @@ static void TestNumberFormatPadding(void);
|
|||
**/
|
||||
static void TestInt64Format(void);
|
||||
|
||||
static void TestNonExistentCurrency(void);
|
||||
|
||||
/**
|
||||
* Test RBNF access through unumfmt APIs.
|
||||
**/
|
||||
|
|
Loading…
Add table
Reference in a new issue