/src/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  |  |     // 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  |  |     // 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::getFormalCurrencySymbol(UErrorCode& status) const { | 
48  |  |     // Note: currently no override is available for formal currency symbol  | 
49  | 0  |     return loadSymbol(UCURR_FORMAL_SYMBOL_NAME, status);  | 
50  | 0  | }  | 
51  |  |  | 
52  | 0  | UnicodeString CurrencySymbols::getVariantCurrencySymbol(UErrorCode& status) const { | 
53  |  |     // Note: currently no override is available for variant currency symbol  | 
54  | 0  |     return loadSymbol(UCURR_VARIANT_SYMBOL_NAME, status);  | 
55  | 0  | }  | 
56  |  |  | 
57  | 0  | UnicodeString CurrencySymbols::getCurrencySymbol(UErrorCode& status) const { | 
58  | 0  |     if (!fCurrencySymbol.isBogus()) { | 
59  | 0  |         return fCurrencySymbol;  | 
60  | 0  |     }  | 
61  | 0  |     return loadSymbol(UCURR_SYMBOL_NAME, status);  | 
62  | 0  | }  | 
63  |  |  | 
64  | 0  | UnicodeString CurrencySymbols::loadSymbol(UCurrNameStyle selector, UErrorCode& status) const { | 
65  | 0  |     const char16_t* isoCode = fCurrency.getISOCurrency();  | 
66  | 0  |     int32_t symbolLen = 0;  | 
67  | 0  |     const char16_t* symbol = ucurr_getName(  | 
68  | 0  |             isoCode,  | 
69  | 0  |             fLocaleName.data(),  | 
70  | 0  |             selector,  | 
71  | 0  |             nullptr /* isChoiceFormat */,  | 
72  | 0  |             &symbolLen,  | 
73  | 0  |             &status);  | 
74  |  |     // If given an unknown currency, ucurr_getName returns the input string, which we can't alias safely!  | 
75  |  |     // Otherwise, symbol points to a resource bundle, and we can use readonly-aliasing constructor.  | 
76  | 0  |     if (symbol == isoCode) { | 
77  | 0  |         return UnicodeString(isoCode, 3);  | 
78  | 0  |     } else { | 
79  | 0  |         return UnicodeString(TRUE, symbol, symbolLen);  | 
80  | 0  |     }  | 
81  | 0  | }  | 
82  |  |  | 
83  | 0  | UnicodeString CurrencySymbols::getIntlCurrencySymbol(UErrorCode&) const { | 
84  | 0  |     if (!fIntlCurrencySymbol.isBogus()) { | 
85  | 0  |         return fIntlCurrencySymbol;  | 
86  | 0  |     }  | 
87  |  |     // Note: Not safe to use readonly-aliasing constructor here because the buffer belongs to this object,  | 
88  |  |     // which could be destructed or moved during the lifetime of the return value.  | 
89  | 0  |     return UnicodeString(fCurrency.getISOCurrency(), 3);  | 
90  | 0  | }  | 
91  |  |  | 
92  | 0  | UnicodeString CurrencySymbols::getPluralName(StandardPlural::Form plural, UErrorCode& status) const { | 
93  | 0  |     const char16_t* isoCode = fCurrency.getISOCurrency();  | 
94  | 0  |     int32_t symbolLen = 0;  | 
95  | 0  |     const char16_t* symbol = ucurr_getPluralName(  | 
96  | 0  |             isoCode,  | 
97  | 0  |             fLocaleName.data(),  | 
98  | 0  |             nullptr /* isChoiceFormat */,  | 
99  | 0  |             StandardPlural::getKeyword(plural),  | 
100  | 0  |             &symbolLen,  | 
101  | 0  |             &status);  | 
102  |  |     // If given an unknown currency, ucurr_getName returns the input string, which we can't alias safely!  | 
103  |  |     // Otherwise, symbol points to a resource bundle, and we can use readonly-aliasing constructor.  | 
104  | 0  |     if (symbol == isoCode) { | 
105  | 0  |         return UnicodeString(isoCode, 3);  | 
106  | 0  |     } else { | 
107  | 0  |         return UnicodeString(TRUE, symbol, symbolLen);  | 
108  | 0  |     }  | 
109  | 0  | }  | 
110  |  |  | 
111  |  |  | 
112  |  | CurrencyUnit  | 
113  |  | icu::number::impl::resolveCurrency(const DecimalFormatProperties& properties, const Locale& locale,  | 
114  | 0  |                                    UErrorCode& status) { | 
115  | 0  |     if (!properties.currency.isNull()) { | 
116  | 0  |         return properties.currency.getNoError();  | 
117  | 0  |     } else { | 
118  | 0  |         UErrorCode localStatus = U_ZERO_ERROR;  | 
119  | 0  |         char16_t buf[4] = {}; | 
120  | 0  |         ucurr_forLocale(locale.getName(), buf, 4, &localStatus);  | 
121  | 0  |         if (U_SUCCESS(localStatus)) { | 
122  | 0  |             return CurrencyUnit(buf, status);  | 
123  | 0  |         } else { | 
124  |  |             // Default currency (XXX)  | 
125  | 0  |             return CurrencyUnit();  | 
126  | 0  |         }  | 
127  | 0  |     }  | 
128  | 0  | }  | 
129  |  |  | 
130  |  |  | 
131  |  | #endif /* #if !UCONFIG_NO_FORMATTING */  |