/src/mozilla-central/intl/icu/source/i18n/number_currencysymbols.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
20 | | CurrencySymbols::CurrencySymbols(CurrencyUnit currency, const Locale& locale, UErrorCode& status) |
21 | 0 | : fCurrency(currency), fLocaleName(locale.getName(), status) { |
22 | 0 | fCurrencySymbol.setToBogus(); |
23 | 0 | fIntlCurrencySymbol.setToBogus(); |
24 | 0 | } |
25 | | |
26 | | CurrencySymbols::CurrencySymbols(CurrencyUnit currency, const Locale& locale, |
27 | | const DecimalFormatSymbols& symbols, UErrorCode& status) |
28 | 0 | : CurrencySymbols(currency, locale, status) { |
29 | 0 | // If either of the overrides is present, save it in the local UnicodeString. |
30 | 0 | if (symbols.isCustomCurrencySymbol()) { |
31 | 0 | fCurrencySymbol = symbols.getConstSymbol(DecimalFormatSymbols::kCurrencySymbol); |
32 | 0 | } |
33 | 0 | if (symbols.isCustomIntlCurrencySymbol()) { |
34 | 0 | fIntlCurrencySymbol = symbols.getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol); |
35 | 0 | } |
36 | 0 | } |
37 | | |
38 | 0 | const char16_t* CurrencySymbols::getIsoCode() const { |
39 | 0 | return fCurrency.getISOCurrency(); |
40 | 0 | } |
41 | | |
42 | 0 | UnicodeString CurrencySymbols::getNarrowCurrencySymbol(UErrorCode& status) const { |
43 | 0 | // Note: currently no override is available for narrow currency symbol |
44 | 0 | return loadSymbol(UCURR_NARROW_SYMBOL_NAME, status); |
45 | 0 | } |
46 | | |
47 | 0 | UnicodeString CurrencySymbols::getCurrencySymbol(UErrorCode& status) const { |
48 | 0 | if (!fCurrencySymbol.isBogus()) { |
49 | 0 | return fCurrencySymbol; |
50 | 0 | } |
51 | 0 | return loadSymbol(UCURR_SYMBOL_NAME, status); |
52 | 0 | } |
53 | | |
54 | 0 | UnicodeString CurrencySymbols::loadSymbol(UCurrNameStyle selector, UErrorCode& status) const { |
55 | 0 | const char16_t* isoCode = fCurrency.getISOCurrency(); |
56 | 0 | UBool ignoredIsChoiceFormatFillIn = FALSE; |
57 | 0 | int32_t symbolLen = 0; |
58 | 0 | const char16_t* symbol = ucurr_getName( |
59 | 0 | isoCode, |
60 | 0 | fLocaleName.data(), |
61 | 0 | selector, |
62 | 0 | &ignoredIsChoiceFormatFillIn, |
63 | 0 | &symbolLen, |
64 | 0 | &status); |
65 | 0 | // If given an unknown currency, ucurr_getName returns the input string, which we can't alias safely! |
66 | 0 | // Otherwise, symbol points to a resource bundle, and we can use readonly-aliasing constructor. |
67 | 0 | if (symbol == isoCode) { |
68 | 0 | return UnicodeString(isoCode, 3); |
69 | 0 | } else { |
70 | 0 | return UnicodeString(TRUE, symbol, symbolLen); |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | 0 | UnicodeString CurrencySymbols::getIntlCurrencySymbol(UErrorCode&) const { |
75 | 0 | if (!fIntlCurrencySymbol.isBogus()) { |
76 | 0 | return fIntlCurrencySymbol; |
77 | 0 | } |
78 | 0 | // Note: Not safe to use readonly-aliasing constructor here because the buffer belongs to this object, |
79 | 0 | // which could be destructed or moved during the lifetime of the return value. |
80 | 0 | return UnicodeString(fCurrency.getISOCurrency(), 3); |
81 | 0 | } |
82 | | |
83 | 0 | UnicodeString CurrencySymbols::getPluralName(StandardPlural::Form plural, UErrorCode& status) const { |
84 | 0 | const char16_t* isoCode = fCurrency.getISOCurrency(); |
85 | 0 | UBool isChoiceFormat = FALSE; |
86 | 0 | int32_t symbolLen = 0; |
87 | 0 | const char16_t* symbol = ucurr_getPluralName( |
88 | 0 | isoCode, |
89 | 0 | fLocaleName.data(), |
90 | 0 | &isChoiceFormat, |
91 | 0 | StandardPlural::getKeyword(plural), |
92 | 0 | &symbolLen, |
93 | 0 | &status); |
94 | 0 | // If given an unknown currency, ucurr_getName returns the input string, which we can't alias safely! |
95 | 0 | // Otherwise, symbol points to a resource bundle, and we can use readonly-aliasing constructor. |
96 | 0 | if (symbol == isoCode) { |
97 | 0 | return UnicodeString(isoCode, 3); |
98 | 0 | } else { |
99 | 0 | return UnicodeString(TRUE, symbol, symbolLen); |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | | |
104 | | CurrencyUnit |
105 | | icu::number::impl::resolveCurrency(const DecimalFormatProperties& properties, const Locale& locale, |
106 | 0 | UErrorCode& status) { |
107 | 0 | if (!properties.currency.isNull()) { |
108 | 0 | return properties.currency.getNoError(); |
109 | 0 | } else { |
110 | 0 | UErrorCode localStatus = U_ZERO_ERROR; |
111 | 0 | char16_t buf[4] = {}; |
112 | 0 | ucurr_forLocale(locale.getName(), buf, 4, &localStatus); |
113 | 0 | if (U_SUCCESS(localStatus)) { |
114 | 0 | return CurrencyUnit(buf, status); |
115 | 0 | } else { |
116 | 0 | // Default currency (XXX) |
117 | 0 | return CurrencyUnit(); |
118 | 0 | } |
119 | 0 | } |
120 | 0 | } |
121 | | |
122 | | |
123 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |