• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 1997-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
8 *
9 * File DCFMTSYM.CPP
10 *
11 * Modification History:
12 *
13 *   Date        Name        Description
14 *   02/19/97    aliu        Converted from java.
15 *   03/18/97    clhuang     Implemented with C++ APIs.
16 *   03/27/97    helena      Updated to pass the simple test after code review.
17 *   08/26/97    aliu        Added currency/intl currency symbol support.
18 *   07/20/98    stephen     Slightly modified initialization of monetarySeparator
19 ********************************************************************************
20 */
21 
22 #include "unicode/utypes.h"
23 
24 #if !UCONFIG_NO_FORMATTING
25 
26 #include "unicode/dcfmtsym.h"
27 #include "unicode/ures.h"
28 #include "unicode/decimfmt.h"
29 #include "unicode/ucurr.h"
30 #include "unicode/choicfmt.h"
31 #include "unicode/unistr.h"
32 #include "unicode/numsys.h"
33 #include "unicode/unum.h"
34 #include "unicode/utf16.h"
35 #include "ucurrimp.h"
36 #include "cstring.h"
37 #include "locbased.h"
38 #include "uresimp.h"
39 #include "ureslocs.h"
40 #include "charstr.h"
41 #include "uassert.h"
42 
43 // *****************************************************************************
44 // class DecimalFormatSymbols
45 // *****************************************************************************
46 
47 U_NAMESPACE_BEGIN
48 
49 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DecimalFormatSymbols)
50 
51 static const char gNumberElements[] = "NumberElements";
52 static const char gCurrencySpacingTag[] = "currencySpacing";
53 static const char gBeforeCurrencyTag[] = "beforeCurrency";
54 static const char gAfterCurrencyTag[] = "afterCurrency";
55 static const char gCurrencyMatchTag[] = "currencyMatch";
56 static const char gCurrencySudMatchTag[] = "surroundingMatch";
57 static const char gCurrencyInsertBtnTag[] = "insertBetween";
58 static const char gLatn[] =  "latn";
59 static const char gSymbols[] = "symbols";
60 static const char gNumberElementsLatnSymbols[] = "NumberElements/latn/symbols";
61 
62 static const UChar INTL_CURRENCY_SYMBOL_STR[] = {0xa4, 0xa4, 0};
63 
64 // List of field names to be loaded from the data files.
65 // These are parallel with the enum ENumberFormatSymbol in unicode/dcfmtsym.h.
66 static const char *gNumberElementKeys[DecimalFormatSymbols::kFormatSymbolCount] = {
67     "decimal",
68     "group",
69     NULL, /* #11897: the <list> symbol is NOT the pattern separator symbol */
70     "percentSign",
71     NULL, /* Native zero digit is deprecated from CLDR - get it from the numbering system */
72     NULL, /* Pattern digit character is deprecated from CLDR - use # by default always */
73     "minusSign",
74     "plusSign",
75     NULL, /* currency symbol - Wait until we know the currency before loading from CLDR */
76     NULL, /* intl currency symbol - Wait until we know the currency before loading from CLDR */
77     "currencyDecimal",
78     "exponential",
79     "perMille",
80     NULL, /* Escape padding character - not in CLDR */
81     "infinity",
82     "nan",
83     NULL, /* Significant digit symbol - not in CLDR */
84     "currencyGroup",
85     NULL, /* one digit - get it from the numbering system */
86     NULL, /* two digit - get it from the numbering system */
87     NULL, /* three digit - get it from the numbering system */
88     NULL, /* four digit - get it from the numbering system */
89     NULL, /* five digit - get it from the numbering system */
90     NULL, /* six digit - get it from the numbering system */
91     NULL, /* seven digit - get it from the numbering system */
92     NULL, /* eight digit - get it from the numbering system */
93     NULL, /* nine digit - get it from the numbering system */
94     "superscriptingExponent", /* Multiplication (x) symbol for exponents */
95     "approximatelySign" /* Approximately sign symbol */
96 };
97 
98 // -------------------------------------
99 // Initializes this with the decimal format symbols in the default locale.
100 
DecimalFormatSymbols(UErrorCode & status)101 DecimalFormatSymbols::DecimalFormatSymbols(UErrorCode& status)
102         : UObject(), locale(), currPattern(NULL) {
103     initialize(locale, status, true);
104 }
105 
106 // -------------------------------------
107 // Initializes this with the decimal format symbols in the desired locale.
108 
DecimalFormatSymbols(const Locale & loc,UErrorCode & status)109 DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, UErrorCode& status)
110         : UObject(), locale(loc), currPattern(NULL) {
111     initialize(locale, status);
112 }
113 
DecimalFormatSymbols(const Locale & loc,const NumberingSystem & ns,UErrorCode & status)114 DecimalFormatSymbols::DecimalFormatSymbols(const Locale& loc, const NumberingSystem& ns, UErrorCode& status)
115         : UObject(), locale(loc), currPattern(NULL) {
116     initialize(locale, status, false, &ns);
117 }
118 
DecimalFormatSymbols()119 DecimalFormatSymbols::DecimalFormatSymbols()
120         : UObject(), locale(Locale::getRoot()), currPattern(NULL) {
121     *validLocale = *actualLocale = 0;
122     initialize();
123 }
124 
125 DecimalFormatSymbols*
createWithLastResortData(UErrorCode & status)126 DecimalFormatSymbols::createWithLastResortData(UErrorCode& status) {
127     if (U_FAILURE(status)) { return NULL; }
128     DecimalFormatSymbols* sym = new DecimalFormatSymbols();
129     if (sym == NULL) {
130         status = U_MEMORY_ALLOCATION_ERROR;
131     }
132     return sym;
133 }
134 
135 // -------------------------------------
136 
~DecimalFormatSymbols()137 DecimalFormatSymbols::~DecimalFormatSymbols()
138 {
139 }
140 
141 // -------------------------------------
142 // copy constructor
143 
DecimalFormatSymbols(const DecimalFormatSymbols & source)144 DecimalFormatSymbols::DecimalFormatSymbols(const DecimalFormatSymbols &source)
145     : UObject(source)
146 {
147     *this = source;
148 }
149 
150 // -------------------------------------
151 // assignment operator
152 
153 DecimalFormatSymbols&
operator =(const DecimalFormatSymbols & rhs)154 DecimalFormatSymbols::operator=(const DecimalFormatSymbols& rhs)
155 {
156     if (this != &rhs) {
157         for(int32_t i = 0; i < (int32_t)kFormatSymbolCount; ++i) {
158             // fastCopyFrom is safe, see docs on fSymbols
159             fSymbols[(ENumberFormatSymbol)i].fastCopyFrom(rhs.fSymbols[(ENumberFormatSymbol)i]);
160         }
161         for(int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; ++i) {
162             currencySpcBeforeSym[i].fastCopyFrom(rhs.currencySpcBeforeSym[i]);
163             currencySpcAfterSym[i].fastCopyFrom(rhs.currencySpcAfterSym[i]);
164         }
165         locale = rhs.locale;
166         uprv_strcpy(validLocale, rhs.validLocale);
167         uprv_strcpy(actualLocale, rhs.actualLocale);
168         fIsCustomCurrencySymbol = rhs.fIsCustomCurrencySymbol;
169         fIsCustomIntlCurrencySymbol = rhs.fIsCustomIntlCurrencySymbol;
170         fCodePointZero = rhs.fCodePointZero;
171         currPattern = rhs.currPattern;
172     }
173     return *this;
174 }
175 
176 // -------------------------------------
177 
178 bool
operator ==(const DecimalFormatSymbols & that) const179 DecimalFormatSymbols::operator==(const DecimalFormatSymbols& that) const
180 {
181     if (this == &that) {
182         return true;
183     }
184     if (fIsCustomCurrencySymbol != that.fIsCustomCurrencySymbol) {
185         return false;
186     }
187     if (fIsCustomIntlCurrencySymbol != that.fIsCustomIntlCurrencySymbol) {
188         return false;
189     }
190     for(int32_t i = 0; i < (int32_t)kFormatSymbolCount; ++i) {
191         if(fSymbols[(ENumberFormatSymbol)i] != that.fSymbols[(ENumberFormatSymbol)i]) {
192             return false;
193         }
194     }
195     for(int32_t i = 0; i < (int32_t)UNUM_CURRENCY_SPACING_COUNT; ++i) {
196         if(currencySpcBeforeSym[i] != that.currencySpcBeforeSym[i]) {
197             return false;
198         }
199         if(currencySpcAfterSym[i] != that.currencySpcAfterSym[i]) {
200             return false;
201         }
202     }
203     // No need to check fCodePointZero since it is based on fSymbols
204     return locale == that.locale &&
205         uprv_strcmp(validLocale, that.validLocale) == 0 &&
206         uprv_strcmp(actualLocale, that.actualLocale) == 0;
207 }
208 
209 // -------------------------------------
210 
211 namespace {
212 
213 /**
214  * Sink for enumerating all of the decimal format symbols (more specifically, anything
215  * under the "NumberElements.symbols" tree).
216  *
217  * More specific bundles (en_GB) are enumerated before their parents (en_001, en, root):
218  * Only store a value if it is still missing, that is, it has not been overridden.
219  */
220 struct DecFmtSymDataSink : public ResourceSink {
221 
222     // Destination for data, modified via setters.
223     DecimalFormatSymbols& dfs;
224     // Boolean array of whether or not we have seen a particular symbol yet.
225     // Can't simply check fSymbols because it is pre-populated with defaults.
226     UBool seenSymbol[DecimalFormatSymbols::kFormatSymbolCount];
227 
228     // Constructor/Destructor
DecFmtSymDataSink__anon1c5b04e10111::DecFmtSymDataSink229     DecFmtSymDataSink(DecimalFormatSymbols& _dfs) : dfs(_dfs) {
230         uprv_memset(seenSymbol, false, sizeof(seenSymbol));
231     }
232     virtual ~DecFmtSymDataSink();
233 
put__anon1c5b04e10111::DecFmtSymDataSink234     virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
235             UErrorCode &errorCode) override {
236         ResourceTable symbolsTable = value.getTable(errorCode);
237         if (U_FAILURE(errorCode)) { return; }
238         for (int32_t j = 0; symbolsTable.getKeyAndValue(j, key, value); ++j) {
239             for (int32_t i=0; i<DecimalFormatSymbols::kFormatSymbolCount; i++) {
240                 if (gNumberElementKeys[i] != NULL && uprv_strcmp(key, gNumberElementKeys[i]) == 0) {
241                     if (!seenSymbol[i]) {
242                         seenSymbol[i] = true;
243                         dfs.setSymbol(
244                             (DecimalFormatSymbols::ENumberFormatSymbol) i,
245                             value.getUnicodeString(errorCode));
246                         if (U_FAILURE(errorCode)) { return; }
247                     }
248                     break;
249                 }
250             }
251         }
252     }
253 
254     // Returns true if all the symbols have been seen.
seenAll__anon1c5b04e10111::DecFmtSymDataSink255     UBool seenAll() {
256         for (int32_t i=0; i<DecimalFormatSymbols::kFormatSymbolCount; i++) {
257             if (!seenSymbol[i]) {
258                 return false;
259             }
260         }
261         return true;
262     }
263 
264     // If monetary decimal or grouping were not explicitly set, then set them to be the
265     // same as their non-monetary counterparts.
resolveMissingMonetarySeparators__anon1c5b04e10111::DecFmtSymDataSink266     void resolveMissingMonetarySeparators(const UnicodeString* fSymbols) {
267         if (!seenSymbol[DecimalFormatSymbols::kMonetarySeparatorSymbol]) {
268             dfs.setSymbol(
269                 DecimalFormatSymbols::kMonetarySeparatorSymbol,
270                 fSymbols[DecimalFormatSymbols::kDecimalSeparatorSymbol]);
271         }
272         if (!seenSymbol[DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol]) {
273             dfs.setSymbol(
274                 DecimalFormatSymbols::kMonetaryGroupingSeparatorSymbol,
275                 fSymbols[DecimalFormatSymbols::kGroupingSeparatorSymbol]);
276         }
277     }
278 };
279 
280 struct CurrencySpacingSink : public ResourceSink {
281     DecimalFormatSymbols& dfs;
282     UBool hasBeforeCurrency;
283     UBool hasAfterCurrency;
284 
CurrencySpacingSink__anon1c5b04e10111::CurrencySpacingSink285     CurrencySpacingSink(DecimalFormatSymbols& _dfs)
286         : dfs(_dfs), hasBeforeCurrency(false), hasAfterCurrency(false) {}
287     virtual ~CurrencySpacingSink();
288 
put__anon1c5b04e10111::CurrencySpacingSink289     virtual void put(const char *key, ResourceValue &value, UBool /*noFallback*/,
290             UErrorCode &errorCode) override {
291         ResourceTable spacingTypesTable = value.getTable(errorCode);
292         for (int32_t i = 0; spacingTypesTable.getKeyAndValue(i, key, value); ++i) {
293             UBool beforeCurrency;
294             if (uprv_strcmp(key, gBeforeCurrencyTag) == 0) {
295                 beforeCurrency = true;
296                 hasBeforeCurrency = true;
297             } else if (uprv_strcmp(key, gAfterCurrencyTag) == 0) {
298                 beforeCurrency = false;
299                 hasAfterCurrency = true;
300             } else {
301                 continue;
302             }
303 
304             ResourceTable patternsTable = value.getTable(errorCode);
305             for (int32_t j = 0; patternsTable.getKeyAndValue(j, key, value); ++j) {
306                 UCurrencySpacing pattern;
307                 if (uprv_strcmp(key, gCurrencyMatchTag) == 0) {
308                     pattern = UNUM_CURRENCY_MATCH;
309                 } else if (uprv_strcmp(key, gCurrencySudMatchTag) == 0) {
310                     pattern = UNUM_CURRENCY_SURROUNDING_MATCH;
311                 } else if (uprv_strcmp(key, gCurrencyInsertBtnTag) == 0) {
312                     pattern = UNUM_CURRENCY_INSERT;
313                 } else {
314                     continue;
315                 }
316 
317                 const UnicodeString& current = dfs.getPatternForCurrencySpacing(
318                     pattern, beforeCurrency, errorCode);
319                 if (current.isEmpty()) {
320                     dfs.setPatternForCurrencySpacing(
321                         pattern, beforeCurrency, value.getUnicodeString(errorCode));
322                 }
323             }
324         }
325     }
326 
resolveMissing__anon1c5b04e10111::CurrencySpacingSink327     void resolveMissing() {
328         // For consistency with Java, this method overwrites everything with the defaults unless
329         // both beforeCurrency and afterCurrency were found in CLDR.
330         static const char* defaults[] = { "[:letter:]", "[:digit:]", " " };
331         if (!hasBeforeCurrency || !hasAfterCurrency) {
332             for (UBool beforeCurrency = 0; beforeCurrency <= true; beforeCurrency++) {
333                 for (int32_t pattern = 0; pattern < UNUM_CURRENCY_SPACING_COUNT; pattern++) {
334                     dfs.setPatternForCurrencySpacing((UCurrencySpacing)pattern,
335                         beforeCurrency, UnicodeString(defaults[pattern], -1, US_INV));
336                 }
337             }
338         }
339     }
340 };
341 
342 // Virtual destructors must be defined out of line.
~DecFmtSymDataSink()343 DecFmtSymDataSink::~DecFmtSymDataSink() {}
~CurrencySpacingSink()344 CurrencySpacingSink::~CurrencySpacingSink() {}
345 
346 } // namespace
347 
348 void
initialize(const Locale & loc,UErrorCode & status,UBool useLastResortData,const NumberingSystem * ns)349 DecimalFormatSymbols::initialize(const Locale& loc, UErrorCode& status,
350     UBool useLastResortData, const NumberingSystem* ns)
351 {
352     if (U_FAILURE(status)) { return; }
353     *validLocale = *actualLocale = 0;
354 
355     // First initialize all the symbols to the fallbacks for anything we can't find
356     initialize();
357 
358     //
359     // Next get the numbering system for this locale and set zero digit
360     // and the digit string based on the numbering system for the locale
361     //
362     LocalPointer<NumberingSystem> nsLocal;
363     if (ns == nullptr) {
364         // Use the numbering system according to the locale.
365         // Save it into a LocalPointer so it gets cleaned up.
366         nsLocal.adoptInstead(NumberingSystem::createInstance(loc, status));
367         ns = nsLocal.getAlias();
368     }
369     const char *nsName;
370     if (U_SUCCESS(status) && ns->getRadix() == 10 && !ns->isAlgorithmic()) {
371         nsName = ns->getName();
372         UnicodeString digitString(ns->getDescription());
373         int32_t digitIndex = 0;
374         UChar32 digit = digitString.char32At(0);
375         fSymbols[kZeroDigitSymbol].setTo(digit);
376         for (int32_t i = kOneDigitSymbol; i <= kNineDigitSymbol; ++i) {
377             digitIndex += U16_LENGTH(digit);
378             digit = digitString.char32At(digitIndex);
379             fSymbols[i].setTo(digit);
380         }
381     } else {
382         nsName = gLatn;
383     }
384 
385     // Open resource bundles
386     const char* locStr = loc.getName();
387     LocalUResourceBundlePointer resource(ures_open(NULL, locStr, &status));
388     LocalUResourceBundlePointer numberElementsRes(
389         ures_getByKeyWithFallback(resource.getAlias(), gNumberElements, NULL, &status));
390 
391     if (U_FAILURE(status)) {
392         if ( useLastResortData ) {
393             status = U_USING_DEFAULT_WARNING;
394             initialize();
395         }
396         return;
397     }
398 
399     // Set locale IDs
400     // TODO: Is there a way to do this without depending on the resource bundle instance?
401     U_LOCALE_BASED(locBased, *this);
402     locBased.setLocaleIDs(
403         ures_getLocaleByType(
404             numberElementsRes.getAlias(),
405             ULOC_VALID_LOCALE, &status),
406         ures_getLocaleByType(
407             numberElementsRes.getAlias(),
408             ULOC_ACTUAL_LOCALE, &status));
409 
410     // Now load the rest of the data from the data sink.
411     // Start with loading this nsName if it is not Latin.
412     DecFmtSymDataSink sink(*this);
413     if (uprv_strcmp(nsName, gLatn) != 0) {
414         CharString path;
415         path.append(gNumberElements, status)
416             .append('/', status)
417             .append(nsName, status)
418             .append('/', status)
419             .append(gSymbols, status);
420         ures_getAllItemsWithFallback(resource.getAlias(), path.data(), sink, status);
421 
422         // If no symbols exist for the given nsName and resource bundle, silently ignore
423         // and fall back to Latin.
424         if (status == U_MISSING_RESOURCE_ERROR) {
425             status = U_ZERO_ERROR;
426         } else if (U_FAILURE(status)) {
427             return;
428         }
429     }
430 
431     // Continue with Latin if necessary.
432     if (!sink.seenAll()) {
433         ures_getAllItemsWithFallback(resource.getAlias(), gNumberElementsLatnSymbols, sink, status);
434         if (U_FAILURE(status)) { return; }
435     }
436 
437     // Let the monetary number separators equal the default number separators if necessary.
438     sink.resolveMissingMonetarySeparators(fSymbols);
439 
440     // Resolve codePointZero
441     UChar32 tempCodePointZero = -1;
442     for (int32_t i=0; i<=9; i++) {
443         const UnicodeString& stringDigit = getConstDigitSymbol(i);
444         if (stringDigit.countChar32() != 1) {
445             tempCodePointZero = -1;
446             break;
447         }
448         UChar32 cp = stringDigit.char32At(0);
449         if (i == 0) {
450             tempCodePointZero = cp;
451         } else if (cp != tempCodePointZero + i) {
452             tempCodePointZero = -1;
453             break;
454         }
455     }
456     fCodePointZero = tempCodePointZero;
457 
458     // Get the default currency from the currency API.
459     UErrorCode internalStatus = U_ZERO_ERROR; // don't propagate failures out
460     UChar curriso[4];
461     UnicodeString tempStr;
462     int32_t currisoLength = ucurr_forLocale(locStr, curriso, UPRV_LENGTHOF(curriso), &internalStatus);
463     if (U_SUCCESS(internalStatus) && currisoLength == 3) {
464         setCurrency(curriso, status);
465     } else {
466         setCurrency(nullptr, status);
467     }
468 
469     // Currency Spacing.
470     LocalUResourceBundlePointer currencyResource(ures_open(U_ICUDATA_CURR, locStr, &status));
471     CurrencySpacingSink currencySink(*this);
472     ures_getAllItemsWithFallback(currencyResource.getAlias(), gCurrencySpacingTag, currencySink, status);
473     currencySink.resolveMissing();
474     if (U_FAILURE(status)) { return; }
475 }
476 
477 void
initialize()478 DecimalFormatSymbols::initialize() {
479     /*
480      * These strings used to be in static arrays, but the HP/UX aCC compiler
481      * cannot initialize a static array with class constructors.
482      *  markus 2000may25
483      */
484     fSymbols[kDecimalSeparatorSymbol] = (UChar)0x2e;    // '.' decimal separator
485     fSymbols[kGroupingSeparatorSymbol].remove();        //     group (thousands) separator
486     fSymbols[kPatternSeparatorSymbol] = (UChar)0x3b;    // ';' pattern separator
487     fSymbols[kPercentSymbol] = (UChar)0x25;             // '%' percent sign
488     fSymbols[kZeroDigitSymbol] = (UChar)0x30;           // '0' native 0 digit
489     fSymbols[kOneDigitSymbol] = (UChar)0x31;            // '1' native 1 digit
490     fSymbols[kTwoDigitSymbol] = (UChar)0x32;            // '2' native 2 digit
491     fSymbols[kThreeDigitSymbol] = (UChar)0x33;          // '3' native 3 digit
492     fSymbols[kFourDigitSymbol] = (UChar)0x34;           // '4' native 4 digit
493     fSymbols[kFiveDigitSymbol] = (UChar)0x35;           // '5' native 5 digit
494     fSymbols[kSixDigitSymbol] = (UChar)0x36;            // '6' native 6 digit
495     fSymbols[kSevenDigitSymbol] = (UChar)0x37;          // '7' native 7 digit
496     fSymbols[kEightDigitSymbol] = (UChar)0x38;          // '8' native 8 digit
497     fSymbols[kNineDigitSymbol] = (UChar)0x39;           // '9' native 9 digit
498     fSymbols[kDigitSymbol] = (UChar)0x23;               // '#' pattern digit
499     fSymbols[kPlusSignSymbol] = (UChar)0x002b;          // '+' plus sign
500     fSymbols[kMinusSignSymbol] = (UChar)0x2d;           // '-' minus sign
501     fSymbols[kCurrencySymbol] = (UChar)0xa4;            // 'OX' currency symbol
502     fSymbols[kIntlCurrencySymbol].setTo(true, INTL_CURRENCY_SYMBOL_STR, 2);
503     fSymbols[kMonetarySeparatorSymbol] = (UChar)0x2e;   // '.' monetary decimal separator
504     fSymbols[kExponentialSymbol] = (UChar)0x45;         // 'E' exponential
505     fSymbols[kPerMillSymbol] = (UChar)0x2030;           // '%o' per mill
506     fSymbols[kPadEscapeSymbol] = (UChar)0x2a;           // '*' pad escape symbol
507     fSymbols[kInfinitySymbol] = (UChar)0x221e;          // 'oo' infinite
508     fSymbols[kNaNSymbol] = (UChar)0xfffd;               // SUB NaN
509     fSymbols[kSignificantDigitSymbol] = (UChar)0x0040;  // '@' significant digit
510     fSymbols[kMonetaryGroupingSeparatorSymbol].remove(); //
511     fSymbols[kExponentMultiplicationSymbol] = (UChar)0xd7; // 'x' multiplication symbol for exponents
512     fSymbols[kApproximatelySignSymbol] = u'~';          // '~' approximately sign
513     fIsCustomCurrencySymbol = false;
514     fIsCustomIntlCurrencySymbol = false;
515     fCodePointZero = 0x30;
516     U_ASSERT(fCodePointZero == fSymbols[kZeroDigitSymbol].char32At(0));
517     currPattern = nullptr;
518 
519 }
520 
setCurrency(const UChar * currency,UErrorCode & status)521 void DecimalFormatSymbols::setCurrency(const UChar* currency, UErrorCode& status) {
522     // TODO: If this method is made public:
523     // - Adopt ICU4J behavior of not allowing currency to be null.
524     // - Also verify that the length of currency is 3.
525     if (!currency) {
526         return;
527     }
528 
529     UnicodeString tempStr;
530     uprv_getStaticCurrencyName(currency, locale.getName(), tempStr, status);
531     if (U_SUCCESS(status)) {
532         fSymbols[kIntlCurrencySymbol].setTo(currency, 3);
533         fSymbols[kCurrencySymbol] = tempStr;
534     }
535 
536     char cc[4]={0};
537     u_UCharsToChars(currency, cc, 3);
538 
539     /* An explicit currency was requested */
540     // TODO(ICU-13297): Move this data loading logic into a centralized place
541     UErrorCode localStatus = U_ZERO_ERROR;
542     LocalUResourceBundlePointer rbTop(ures_open(U_ICUDATA_CURR, locale.getName(), &localStatus));
543     LocalUResourceBundlePointer rb(
544         ures_getByKeyWithFallback(rbTop.getAlias(), "Currencies", NULL, &localStatus));
545     ures_getByKeyWithFallback(rb.getAlias(), cc, rb.getAlias(), &localStatus);
546     if(U_SUCCESS(localStatus) && ures_getSize(rb.getAlias())>2) { // the length is 3 if more data is present
547         ures_getByIndex(rb.getAlias(), 2, rb.getAlias(), &localStatus);
548         int32_t currPatternLen = 0;
549         currPattern =
550             ures_getStringByIndex(rb.getAlias(), (int32_t)0, &currPatternLen, &localStatus);
551         UnicodeString decimalSep =
552             ures_getUnicodeStringByIndex(rb.getAlias(), (int32_t)1, &localStatus);
553         UnicodeString groupingSep =
554             ures_getUnicodeStringByIndex(rb.getAlias(), (int32_t)2, &localStatus);
555         if(U_SUCCESS(localStatus)){
556             fSymbols[kMonetaryGroupingSeparatorSymbol] = groupingSep;
557             fSymbols[kMonetarySeparatorSymbol] = decimalSep;
558             //pattern.setTo(true, currPattern, currPatternLen);
559         }
560     }
561     /* else An explicit currency was requested and is unknown or locale data is malformed. */
562     /* ucurr_* API will get the correct value later on. */
563 }
564 
565 Locale
getLocale(ULocDataLocaleType type,UErrorCode & status) const566 DecimalFormatSymbols::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
567     U_LOCALE_BASED(locBased, *this);
568     return locBased.getLocale(type, status);
569 }
570 
571 const UnicodeString&
getPatternForCurrencySpacing(UCurrencySpacing type,UBool beforeCurrency,UErrorCode & status) const572 DecimalFormatSymbols::getPatternForCurrencySpacing(UCurrencySpacing type,
573                                                  UBool beforeCurrency,
574                                                  UErrorCode& status) const {
575     if (U_FAILURE(status)) {
576       return fNoSymbol;  // always empty.
577     }
578     if (beforeCurrency) {
579       return currencySpcBeforeSym[(int32_t)type];
580     } else {
581       return currencySpcAfterSym[(int32_t)type];
582     }
583 }
584 
585 void
setPatternForCurrencySpacing(UCurrencySpacing type,UBool beforeCurrency,const UnicodeString & pattern)586 DecimalFormatSymbols::setPatternForCurrencySpacing(UCurrencySpacing type,
587                                                    UBool beforeCurrency,
588                                              const UnicodeString& pattern) {
589   if (beforeCurrency) {
590     currencySpcBeforeSym[(int32_t)type] = pattern;
591   } else {
592     currencySpcAfterSym[(int32_t)type] =  pattern;
593   }
594 }
595 U_NAMESPACE_END
596 
597 #endif /* #if !UCONFIG_NO_FORMATTING */
598 
599 //eof
600