1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2009-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
10 #include "unicode/currpinf.h"
12 #if !UCONFIG_NO_FORMATTING
14 //#define CURRENCY_PLURAL_INFO_DEBUG 1
16 #ifdef CURRENCY_PLURAL_INFO_DEBUG
21 #include "unicode/locid.h"
22 #include "unicode/plurrule.h"
23 #include "unicode/ures.h"
24 #include "unicode/numsys.h"
33 static const UChar gNumberPatternSeparator = 0x3B; // ;
40 static UBool U_CALLCONV ValueComparator(UHashTok val1, UHashTok val2);
43 U_CALLCONV ValueComparator(UHashTok val1, UHashTok val2) {
44 const UnicodeString* affix_1 = (UnicodeString*)val1.pointer;
45 const UnicodeString* affix_2 = (UnicodeString*)val2.pointer;
46 return *affix_1 == *affix_2;
52 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyPluralInfo)
54 static const UChar gDefaultCurrencyPluralPattern[] = {'0', '.', '#', '#', ' ', 0xA4, 0xA4, 0xA4, 0};
55 static const UChar gTripleCurrencySign[] = {0xA4, 0xA4, 0xA4, 0};
56 static const UChar gPluralCountOther[] = {0x6F, 0x74, 0x68, 0x65, 0x72, 0};
57 static const UChar gPart0[] = {0x7B, 0x30, 0x7D, 0};
58 static const UChar gPart1[] = {0x7B, 0x31, 0x7D, 0};
60 static const char gNumberElementsTag[]="NumberElements";
61 static const char gLatnTag[]="latn";
62 static const char gPatternsTag[]="patterns";
63 static const char gDecimalFormatTag[]="decimalFormat";
64 static const char gCurrUnitPtnTag[]="CurrencyUnitPatterns";
66 CurrencyPluralInfo::CurrencyPluralInfo(UErrorCode& status)
67 : fPluralCountToCurrencyUnitPattern(NULL),
70 initialize(Locale::getDefault(), status);
73 CurrencyPluralInfo::CurrencyPluralInfo(const Locale& locale, UErrorCode& status)
74 : fPluralCountToCurrencyUnitPattern(NULL),
77 initialize(locale, status);
80 CurrencyPluralInfo::CurrencyPluralInfo(const CurrencyPluralInfo& info)
82 fPluralCountToCurrencyUnitPattern(NULL),
90 CurrencyPluralInfo::operator=(const CurrencyPluralInfo& info) {
95 deleteHash(fPluralCountToCurrencyUnitPattern);
96 UErrorCode status = U_ZERO_ERROR;
97 fPluralCountToCurrencyUnitPattern = initHash(status);
98 copyHash(info.fPluralCountToCurrencyUnitPattern,
99 fPluralCountToCurrencyUnitPattern, status);
100 if ( U_FAILURE(status) ) {
106 if (info.fPluralRules) {
107 fPluralRules = info.fPluralRules->clone();
112 fLocale = info.fLocale->clone();
120 CurrencyPluralInfo::~CurrencyPluralInfo() {
121 deleteHash(fPluralCountToCurrencyUnitPattern);
122 fPluralCountToCurrencyUnitPattern = NULL;
130 CurrencyPluralInfo::operator==(const CurrencyPluralInfo& info) const {
131 #ifdef CURRENCY_PLURAL_INFO_DEBUG
132 if (*fPluralRules == *info.fPluralRules) {
133 std::cout << "same plural rules\n";
135 if (*fLocale == *info.fLocale) {
136 std::cout << "same locale\n";
138 if (fPluralCountToCurrencyUnitPattern->equals(*info.fPluralCountToCurrencyUnitPattern)) {
139 std::cout << "same pattern\n";
142 return *fPluralRules == *info.fPluralRules &&
143 *fLocale == *info.fLocale &&
144 fPluralCountToCurrencyUnitPattern->equals(*info.fPluralCountToCurrencyUnitPattern);
149 CurrencyPluralInfo::clone() const {
150 return new CurrencyPluralInfo(*this);
154 CurrencyPluralInfo::getPluralRules() const {
159 CurrencyPluralInfo::getCurrencyPluralPattern(const UnicodeString& pluralCount,
160 UnicodeString& result) const {
161 const UnicodeString* currencyPluralPattern =
162 (UnicodeString*)fPluralCountToCurrencyUnitPattern->get(pluralCount);
163 if (currencyPluralPattern == NULL) {
164 // fall back to "other"
165 if (pluralCount.compare(gPluralCountOther, 5)) {
166 currencyPluralPattern =
167 (UnicodeString*)fPluralCountToCurrencyUnitPattern->get(UnicodeString(TRUE, gPluralCountOther, 5));
169 if (currencyPluralPattern == NULL) {
170 // no currencyUnitPatterns defined,
171 // fallback to predefined defult.
172 // This should never happen when ICU resource files are
173 // available, since currencyUnitPattern of "other" is always
175 result = UnicodeString(gDefaultCurrencyPluralPattern);
179 result = *currencyPluralPattern;
184 CurrencyPluralInfo::getLocale() const {
189 CurrencyPluralInfo::setPluralRules(const UnicodeString& ruleDescription,
190 UErrorCode& status) {
191 if (U_SUCCESS(status)) {
195 fPluralRules = PluralRules::createRules(ruleDescription, status);
201 CurrencyPluralInfo::setCurrencyPluralPattern(const UnicodeString& pluralCount,
202 const UnicodeString& pattern,
203 UErrorCode& status) {
204 if (U_SUCCESS(status)) {
205 fPluralCountToCurrencyUnitPattern->put(pluralCount, new UnicodeString(pattern), status);
211 CurrencyPluralInfo::setLocale(const Locale& loc, UErrorCode& status) {
212 initialize(loc, status);
217 CurrencyPluralInfo::initialize(const Locale& loc, UErrorCode& status) {
218 if (U_FAILURE(status)) {
222 fLocale = loc.clone();
226 fPluralRules = PluralRules::forLocale(loc, status);
227 setupCurrencyPluralPattern(loc, status);
232 CurrencyPluralInfo::setupCurrencyPluralPattern(const Locale& loc, UErrorCode& status) {
233 if (U_FAILURE(status)) {
237 if (fPluralCountToCurrencyUnitPattern) {
238 deleteHash(fPluralCountToCurrencyUnitPattern);
240 fPluralCountToCurrencyUnitPattern = initHash(status);
241 if (U_FAILURE(status)) {
245 NumberingSystem *ns = NumberingSystem::createInstance(loc,status);
246 UErrorCode ec = U_ZERO_ERROR;
247 UResourceBundle *rb = ures_open(NULL, loc.getName(), &ec);
248 UResourceBundle *numElements = ures_getByKeyWithFallback(rb, gNumberElementsTag, NULL, &ec);
249 rb = ures_getByKeyWithFallback(numElements, ns->getName(), rb, &ec);
250 rb = ures_getByKeyWithFallback(rb, gPatternsTag, rb, &ec);
252 const UChar* numberStylePattern = ures_getStringByKeyWithFallback(rb, gDecimalFormatTag, &ptnLen, &ec);
253 // Fall back to "latn" if num sys specific pattern isn't there.
254 if ( ec == U_MISSING_RESOURCE_ERROR && uprv_strcmp(ns->getName(),gLatnTag)) {
256 rb = ures_getByKeyWithFallback(numElements, gLatnTag, rb, &ec);
257 rb = ures_getByKeyWithFallback(rb, gPatternsTag, rb, &ec);
258 numberStylePattern = ures_getStringByKeyWithFallback(rb, gDecimalFormatTag, &ptnLen, &ec);
260 int32_t numberStylePatternLen = ptnLen;
261 const UChar* negNumberStylePattern = NULL;
262 int32_t negNumberStylePatternLen = 0;
264 // parse to check whether there is ";" separator in the numberStylePattern
265 UBool hasSeparator = false;
267 for (int32_t styleCharIndex = 0; styleCharIndex < ptnLen; ++styleCharIndex) {
268 if (numberStylePattern[styleCharIndex] == gNumberPatternSeparator) {
270 // split the number style pattern into positive and negative
271 negNumberStylePattern = numberStylePattern + styleCharIndex + 1;
272 negNumberStylePatternLen = ptnLen - styleCharIndex - 1;
273 numberStylePatternLen = styleCharIndex;
278 ures_close(numElements);
286 UResourceBundle *currRb = ures_open(U_ICUDATA_CURR, loc.getName(), &ec);
287 UResourceBundle *currencyRes = ures_getByKeyWithFallback(currRb, gCurrUnitPtnTag, NULL, &ec);
289 #ifdef CURRENCY_PLURAL_INFO_DEBUG
290 std::cout << "in set up\n";
292 StringEnumeration* keywords = fPluralRules->getKeywords(ec);
294 const char* pluralCount;
295 while ((pluralCount = keywords->next(NULL, ec)) != NULL) {
296 if ( U_SUCCESS(ec) ) {
298 UErrorCode err = U_ZERO_ERROR;
299 const UChar* patternChars = ures_getStringByKeyWithFallback(
300 currencyRes, pluralCount, &ptnLen, &err);
301 if (U_SUCCESS(err) && ptnLen > 0) {
302 UnicodeString* pattern = new UnicodeString(patternChars, ptnLen);
303 #ifdef CURRENCY_PLURAL_INFO_DEBUG
305 pattern->extract(0, pattern->length(), result_1, "UTF-8");
306 std::cout << "pluralCount: " << pluralCount << "; pattern: " << result_1 << "\n";
308 pattern->findAndReplace(UnicodeString(TRUE, gPart0, 3),
309 UnicodeString(numberStylePattern, numberStylePatternLen));
310 pattern->findAndReplace(UnicodeString(TRUE, gPart1, 3), UnicodeString(TRUE, gTripleCurrencySign, 3));
313 UnicodeString negPattern(patternChars, ptnLen);
314 negPattern.findAndReplace(UnicodeString(TRUE, gPart0, 3),
315 UnicodeString(negNumberStylePattern, negNumberStylePatternLen));
316 negPattern.findAndReplace(UnicodeString(TRUE, gPart1, 3), UnicodeString(TRUE, gTripleCurrencySign, 3));
317 pattern->append(gNumberPatternSeparator);
318 pattern->append(negPattern);
320 #ifdef CURRENCY_PLURAL_INFO_DEBUG
321 pattern->extract(0, pattern->length(), result_1, "UTF-8");
322 std::cout << "pluralCount: " << pluralCount << "; pattern: " << result_1 << "\n";
325 fPluralCountToCurrencyUnitPattern->put(UnicodeString(pluralCount, -1, US_INV), pattern, status);
331 ures_close(currencyRes);
338 CurrencyPluralInfo::deleteHash(Hashtable* hTable)
340 if ( hTable == NULL ) {
343 int32_t pos = UHASH_FIRST;
344 const UHashElement* element = NULL;
345 while ( (element = hTable->nextElement(pos)) != NULL ) {
346 const UHashTok valueTok = element->value;
347 const UnicodeString* value = (UnicodeString*)valueTok.pointer;
356 CurrencyPluralInfo::initHash(UErrorCode& status) {
357 if ( U_FAILURE(status) ) {
361 if ( (hTable = new Hashtable(TRUE, status)) == NULL ) {
362 status = U_MEMORY_ALLOCATION_ERROR;
365 if ( U_FAILURE(status) ) {
369 hTable->setValueComparator(ValueComparator);
375 CurrencyPluralInfo::copyHash(const Hashtable* source,
377 UErrorCode& status) {
378 if ( U_FAILURE(status) ) {
381 int32_t pos = UHASH_FIRST;
382 const UHashElement* element = NULL;
384 while ( (element = source->nextElement(pos)) != NULL ) {
385 const UHashTok keyTok = element->key;
386 const UnicodeString* key = (UnicodeString*)keyTok.pointer;
387 const UHashTok valueTok = element->value;
388 const UnicodeString* value = (UnicodeString*)valueTok.pointer;
389 UnicodeString* copy = new UnicodeString(*value);
390 target->put(UnicodeString(*key), copy, status);
391 if ( U_FAILURE(status) ) {