/*
******************************************************************************
-* Copyright (C) 1997-2011, International Business Machines
+* Copyright (C) 1997-2012, International Business Machines
* Corporation and others. All Rights Reserved.
******************************************************************************
* file name: nfrlist.h
fCapacity = 0;
return result;
}
+ void deleteAll() {
+ NFRule** tmp = NULL;
+ int32_t size = fCount;
+ if (size > 0) {
+ tmp = release();
+ for (int32_t i = 0; i < size; i++) {
+ delete tmp[i];
+ }
+ if (tmp) {
+ uprv_free(tmp);
+ }
+ }
+ }
private:
NFRuleList(const NFRuleList &other); // forbid copying of this class
return;
}
+ // ensure we are starting with an empty rule list
+ rules.deleteAll();
+
// dlf - the original code kept a separate description array for no reason,
// so I got rid of it. The loop was too complex so I simplified it.
// if it's the negative-number rule, copy it into its own
// data member and delete it from the list
case NFRule::kNegativeNumberRule:
+ if (negativeNumberRule) {
+ delete negativeNumberRule;
+ }
negativeNumberRule = rules.remove(i);
break;
// if it's the improper fraction rule, copy it into the
// correct element of fractionRules
case NFRule::kImproperFractionRule:
+ if (fractionRules[0]) {
+ delete fractionRules[0];
+ }
fractionRules[0] = rules.remove(i);
break;
// if it's the proper fraction rule, copy it into the
// correct element of fractionRules
case NFRule::kProperFractionRule:
+ if (fractionRules[1]) {
+ delete fractionRules[1];
+ }
fractionRules[1] = rules.remove(i);
break;
// if it's the master rule, copy it into the
// correct element of fractionRules
case NFRule::kMasterRule:
+ if (fractionRules[2]) {
+ delete fractionRules[2];
+ }
fractionRules[2] = rules.remove(i);
break;
const UnicodeString& locs,
const Locale& alocale, UParseError& perror, UErrorCode& status)
: ruleSets(NULL)
+ , ruleSetDescriptions(NULL)
+ , numRuleSets(0)
, defaultRuleSet(NULL)
, locale(alocale)
, collator(NULL)
const UnicodeString& locs,
UParseError& perror, UErrorCode& status)
: ruleSets(NULL)
+ , ruleSetDescriptions(NULL)
+ , numRuleSets(0)
, defaultRuleSet(NULL)
, locale(Locale::getDefault())
, collator(NULL)
LocalizationInfo* info,
const Locale& alocale, UParseError& perror, UErrorCode& status)
: ruleSets(NULL)
+ , ruleSetDescriptions(NULL)
+ , numRuleSets(0)
, defaultRuleSet(NULL)
, locale(alocale)
, collator(NULL)
UParseError& perror,
UErrorCode& status)
: ruleSets(NULL)
+ , ruleSetDescriptions(NULL)
+ , numRuleSets(0)
, defaultRuleSet(NULL)
, locale(Locale::getDefault())
, collator(NULL)
UParseError& perror,
UErrorCode& status)
: ruleSets(NULL)
+ , ruleSetDescriptions(NULL)
+ , numRuleSets(0)
, defaultRuleSet(NULL)
, locale(aLocale)
, collator(NULL)
RuleBasedNumberFormat::RuleBasedNumberFormat(URBNFRuleSetTag tag, const Locale& alocale, UErrorCode& status)
: ruleSets(NULL)
+ , ruleSetDescriptions(NULL)
+ , numRuleSets(0)
, defaultRuleSet(NULL)
, locale(alocale)
, collator(NULL)
RuleBasedNumberFormat::RuleBasedNumberFormat(const RuleBasedNumberFormat& rhs)
: NumberFormat(rhs)
, ruleSets(NULL)
+ , ruleSetDescriptions(NULL)
+ , numRuleSets(0)
, defaultRuleSet(NULL)
, locale(rhs.locale)
, collator(NULL)
// pre-flight parsing the description and count the number of
// rule sets (";%" marks the end of one rule set and the beginning
// of the next)
- int numRuleSets = 0;
+ numRuleSets = 0;
for (int32_t p = description.indexOf(gSemiPercent, 2, 0); p != -1; p = description.indexOf(gSemiPercent, 2, p)) {
++numRuleSets;
++p;
status = U_ILLEGAL_ARGUMENT_ERROR;
return;
}
- UnicodeString* ruleSetDescriptions = new UnicodeString[numRuleSets];
+
+ ruleSetDescriptions = new UnicodeString[numRuleSets];
if (ruleSetDescriptions == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
return;
ruleSets[curRuleSet] = new NFRuleSet(ruleSetDescriptions, curRuleSet, status);
if (ruleSets[curRuleSet] == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
- goto cleanup;
+ return;
}
++curRuleSet;
start = p + 1;
ruleSets[curRuleSet] = new NFRuleSet(ruleSetDescriptions, curRuleSet, status);
if (ruleSets[curRuleSet] == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
- goto cleanup;
+ return;
}
}
} else {
defaultRuleSet = getDefaultRuleSet();
}
-
-cleanup:
- delete[] ruleSetDescriptions;
}
void
ruleSets = NULL;
}
+ if (ruleSetDescriptions) {
+ delete [] ruleSetDescriptions;
+ }
+
#if !UCONFIG_NO_COLLATION
delete collator;
#endif
}
decimalFormatSymbols = symbolsToAdopt;
+
+ {
+ // Apply the new decimalFormatSymbols by reparsing the rulesets
+ UErrorCode status = U_ZERO_ERROR;
+
+ for (int32_t i = 0; i < numRuleSets; i++) {
+ ruleSets[i]->parseRules(ruleSetDescriptions[i], this, status);
+ }
+ }
}
// Setting the symbols is equlivalent to adopting a newly created localized symbols.
private:
NFRuleSet **ruleSets;
+ UnicodeString* ruleSetDescriptions;
+ int32_t numRuleSets;
NFRuleSet *defaultRuleSet;
Locale locale;
Collator* collator;
/*
*******************************************************************************
- * Copyright (C) 1996-2011, International Business Machines Corporation and *
+ * Copyright (C) 1996-2012, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
TESTCASE(16, TestHebrewFraction);
TESTCASE(17, TestPortugueseSpellout);
TESTCASE(18, TestMultiplierSubstitution);
+ TESTCASE(19, TestSetDecimalFormatSymbols);
#else
TESTCASE(0, TestRBNFDisabled);
#endif
}
}
+void
+IntlTestRBNF::TestSetDecimalFormatSymbols() {
+ UErrorCode status = U_ZERO_ERROR;
+
+ RuleBasedNumberFormat rbnf(URBNF_ORDINAL, Locale::getEnglish(), status);
+ if (U_FAILURE(status)) {
+ errln("Unable to create RuleBasedNumberFormat - " + UnicodeString(u_errorName(status)));
+ return;
+ }
+
+ DecimalFormatSymbols dfs(Locale::getEnglish(), status);
+ if (U_FAILURE(status)) {
+ errln("Unable to create DecimalFormatSymbols - " + UnicodeString(u_errorName(status)));
+ return;
+ }
+
+ UnicodeString expected[] = {
+ UnicodeString("1,001st"),
+ UnicodeString("1&001st")
+ };
+
+ double number = 1001;
+
+ UnicodeString result;
+
+ rbnf.format(number, result);
+ if (result != expected[0]) {
+ errln("Format Error - Got: " + result + " Expected: " + expected[0]);
+ }
+
+ result.remove();
+
+ /* Set new symbol for testing */
+ dfs.setSymbol(DecimalFormatSymbols::kGroupingSeparatorSymbol, UnicodeString("&"), TRUE);
+ rbnf.setDecimalFormatSymbols(dfs);
+
+ rbnf.format(number, result);
+ if (result != expected[1]) {
+ errln("Format Error - Got: " + result + " Expected: " + expected[1]);
+ }
+}
+
+
void
IntlTestRBNF::doTest(RuleBasedNumberFormat* formatter, const char* const testData[][2], UBool testParsing)
{
/*
*******************************************************************************
- * Copyright (C) 1996-2007, International Business Machines Corporation and *
+ * Copyright (C) 1996-2012, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
*/
virtual void TestMultiplierSubstitution();
+ /**
+ * Test the setDecimalFormatSymbols in RBNF
+ */
+ virtual void TestSetDecimalFormatSymbols();
+
protected:
virtual void doTest(RuleBasedNumberFormat* formatter, const char* const testData[][2], UBool testParsing);
virtual void doLenientParseTest(RuleBasedNumberFormat* formatter, const char* testData[][2]);