1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (c) 2004-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
9 * Created: April 26, 2004
11 **********************************************************************
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_FORMATTING
17 #include "unicode/currunit.h"
18 #include "unicode/ustring.h"
22 static constexpr char16_t kDefaultCurrency
[] = u
"XXX";
26 CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode
, UErrorCode
& ec
) {
27 // The constructor always leaves the CurrencyUnit in a valid state (with a 3-character currency code).
28 // Note: in ICU4J Currency.getInstance(), we check string length for 3, but in ICU4C we allow a
29 // non-NUL-terminated string to be passed as an argument, so it is not possible to check length.
30 const char16_t* isoCodeToUse
;
31 if (U_FAILURE(ec
) || _isoCode
== nullptr) {
32 isoCodeToUse
= kDefaultCurrency
;
33 } else if (!uprv_isInvariantUString(_isoCode
, 3)) {
34 // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
35 isoCodeToUse
= kDefaultCurrency
;
36 ec
= U_INVARIANT_CONVERSION_ERROR
;
38 isoCodeToUse
= _isoCode
;
40 // TODO: Perform uppercasing here like in ICU4J Currency.getInstance()?
41 uprv_memcpy(isoCode
, isoCodeToUse
, sizeof(UChar
) * 3);
43 char simpleIsoCode
[4];
44 u_UCharsToChars(isoCode
, simpleIsoCode
, 4);
45 initCurrency(simpleIsoCode
);
48 CurrencyUnit::CurrencyUnit(const CurrencyUnit
& other
) : MeasureUnit(other
) {
49 u_strcpy(isoCode
, other
.isoCode
);
52 CurrencyUnit::CurrencyUnit(const MeasureUnit
& other
, UErrorCode
& ec
) : MeasureUnit(other
) {
53 // Make sure this is a currency.
54 // OK to hard-code the string because we are comparing against another hard-coded string.
55 if (uprv_strcmp("currency", getType()) != 0) {
56 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
59 // Get the ISO Code from the subtype field.
60 u_charsToUChars(getSubtype(), isoCode
, 4);
61 isoCode
[3] = 0; // make 100% sure it is NUL-terminated
65 CurrencyUnit::CurrencyUnit() : MeasureUnit() {
66 u_strcpy(isoCode
, kDefaultCurrency
);
67 char simpleIsoCode
[4];
68 u_UCharsToChars(isoCode
, simpleIsoCode
, 4);
69 initCurrency(simpleIsoCode
);
72 CurrencyUnit
& CurrencyUnit::operator=(const CurrencyUnit
& other
) {
76 MeasureUnit::operator=(other
);
77 u_strcpy(isoCode
, other
.isoCode
);
81 UObject
* CurrencyUnit::clone() const {
82 return new CurrencyUnit(*this);
85 CurrencyUnit::~CurrencyUnit() {
88 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit
)
92 #endif // !UCONFIG_NO_FORMATTING