• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING
7 
8 // Allow implicit conversion from char16_t* to UnicodeString for this file:
9 // Helpful in toString methods and elsewhere.
10 #define UNISTR_FROM_STRING_EXPLICIT
11 
12 #include "numparse_types.h"
13 #include "number_currencysymbols.h"
14 
15 using namespace icu;
16 using namespace icu::number;
17 using namespace icu::number::impl;
18 
19 
CurrencySymbols(CurrencyUnit currency,const Locale & locale,UErrorCode & status)20 CurrencySymbols::CurrencySymbols(CurrencyUnit currency, const Locale& locale, UErrorCode& status)
21         : fCurrency(currency), fLocaleName(locale.getName(), status) {
22     fCurrencySymbol.setToBogus();
23     fIntlCurrencySymbol.setToBogus();
24 }
25 
CurrencySymbols(CurrencyUnit currency,const Locale & locale,const DecimalFormatSymbols & symbols,UErrorCode & status)26 CurrencySymbols::CurrencySymbols(CurrencyUnit currency, const Locale& locale,
27                                  const DecimalFormatSymbols& symbols, UErrorCode& status)
28         : CurrencySymbols(currency, locale, status) {
29     // If either of the overrides is present, save it in the local UnicodeString.
30     if (symbols.isCustomCurrencySymbol()) {
31         fCurrencySymbol = symbols.getConstSymbol(DecimalFormatSymbols::kCurrencySymbol);
32     }
33     if (symbols.isCustomIntlCurrencySymbol()) {
34         fIntlCurrencySymbol = symbols.getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol);
35     }
36 }
37 
getIsoCode() const38 const char16_t* CurrencySymbols::getIsoCode() const {
39     return fCurrency.getISOCurrency();
40 }
41 
getNarrowCurrencySymbol(UErrorCode & status) const42 UnicodeString CurrencySymbols::getNarrowCurrencySymbol(UErrorCode& status) const {
43     // Note: currently no override is available for narrow currency symbol
44     return loadSymbol(UCURR_NARROW_SYMBOL_NAME, status);
45 }
46 
getCurrencySymbol(UErrorCode & status) const47 UnicodeString CurrencySymbols::getCurrencySymbol(UErrorCode& status) const {
48     if (!fCurrencySymbol.isBogus()) {
49         return fCurrencySymbol;
50     }
51     return loadSymbol(UCURR_SYMBOL_NAME, status);
52 }
53 
loadSymbol(UCurrNameStyle selector,UErrorCode & status) const54 UnicodeString CurrencySymbols::loadSymbol(UCurrNameStyle selector, UErrorCode& status) const {
55     const char16_t* isoCode = fCurrency.getISOCurrency();
56     UBool ignoredIsChoiceFormatFillIn = FALSE;
57     int32_t symbolLen = 0;
58     const char16_t* symbol = ucurr_getName(
59             isoCode,
60             fLocaleName.data(),
61             selector,
62             &ignoredIsChoiceFormatFillIn,
63             &symbolLen,
64             &status);
65     // If given an unknown currency, ucurr_getName returns the input string, which we can't alias safely!
66     // Otherwise, symbol points to a resource bundle, and we can use readonly-aliasing constructor.
67     if (symbol == isoCode) {
68         return UnicodeString(isoCode, 3);
69     } else {
70         return UnicodeString(TRUE, symbol, symbolLen);
71     }
72 }
73 
getIntlCurrencySymbol(UErrorCode &) const74 UnicodeString CurrencySymbols::getIntlCurrencySymbol(UErrorCode&) const {
75     if (!fIntlCurrencySymbol.isBogus()) {
76         return fIntlCurrencySymbol;
77     }
78     // Note: Not safe to use readonly-aliasing constructor here because the buffer belongs to this object,
79     // which could be destructed or moved during the lifetime of the return value.
80     return UnicodeString(fCurrency.getISOCurrency(), 3);
81 }
82 
getPluralName(StandardPlural::Form plural,UErrorCode & status) const83 UnicodeString CurrencySymbols::getPluralName(StandardPlural::Form plural, UErrorCode& status) const {
84     const char16_t* isoCode = fCurrency.getISOCurrency();
85     UBool isChoiceFormat = FALSE;
86     int32_t symbolLen = 0;
87     const char16_t* symbol = ucurr_getPluralName(
88             isoCode,
89             fLocaleName.data(),
90             &isChoiceFormat,
91             StandardPlural::getKeyword(plural),
92             &symbolLen,
93             &status);
94     // If given an unknown currency, ucurr_getName returns the input string, which we can't alias safely!
95     // Otherwise, symbol points to a resource bundle, and we can use readonly-aliasing constructor.
96     if (symbol == isoCode) {
97         return UnicodeString(isoCode, 3);
98     } else {
99         return UnicodeString(TRUE, symbol, symbolLen);
100     }
101 }
102 
103 
104 CurrencyUnit
resolveCurrency(const DecimalFormatProperties & properties,const Locale & locale,UErrorCode & status)105 icu::number::impl::resolveCurrency(const DecimalFormatProperties& properties, const Locale& locale,
106                                    UErrorCode& status) {
107     if (!properties.currency.isNull()) {
108         return properties.currency.getNoError();
109     } else {
110         UErrorCode localStatus = U_ZERO_ERROR;
111         char16_t buf[4] = {};
112         ucurr_forLocale(locale.getName(), buf, 4, &localStatus);
113         if (U_SUCCESS(localStatus)) {
114             return CurrencyUnit(buf, status);
115         } else {
116             // Default currency (XXX)
117             return CurrencyUnit();
118         }
119     }
120 }
121 
122 
123 #endif /* #if !UCONFIG_NO_FORMATTING */
124