1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
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
12 #include "numparse_types.h"
13 #include "number_currencysymbols.h"
16 using namespace icu::number
;
17 using namespace icu::number::impl
;
20 CurrencySymbols::CurrencySymbols(CurrencyUnit currency
, const Locale
& locale
, UErrorCode
& status
)
21 : fCurrency(currency
), fLocaleName(locale
.getName(), status
) {
22 fCurrencySymbol
.setToBogus();
23 fIntlCurrencySymbol
.setToBogus();
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
);
33 if (symbols
.isCustomIntlCurrencySymbol()) {
34 fIntlCurrencySymbol
= symbols
.getConstSymbol(DecimalFormatSymbols::kIntlCurrencySymbol
);
38 const char16_t* CurrencySymbols::getIsoCode() const {
39 return fCurrency
.getISOCurrency();
42 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
);
47 UnicodeString
CurrencySymbols::getCurrencySymbol(UErrorCode
& status
) const {
48 if (!fCurrencySymbol
.isBogus()) {
49 return fCurrencySymbol
;
51 return loadSymbol(UCURR_SYMBOL_NAME
, status
);
54 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(
62 &ignoredIsChoiceFormatFillIn
,
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);
70 return UnicodeString(TRUE
, symbol
, symbolLen
);
74 UnicodeString
CurrencySymbols::getIntlCurrencySymbol(UErrorCode
&) const {
75 if (!fIntlCurrencySymbol
.isBogus()) {
76 return fIntlCurrencySymbol
;
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);
83 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(
91 StandardPlural::getKeyword(plural
),
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);
99 return UnicodeString(TRUE
, symbol
, symbolLen
);
105 icu::number::impl::resolveCurrency(const DecimalFormatProperties
& properties
, const Locale
& locale
,
106 UErrorCode
& status
) {
107 if (!properties
.currency
.isNull()) {
108 return properties
.currency
.getNoError();
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
);
116 // Default currency (XXX)
117 return CurrencyUnit();
123 #endif /* #if !UCONFIG_NO_FORMATTING */