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"
23 static constexpr char16_t kDefaultCurrency
[] = u
"XXX";
24 static constexpr char kDefaultCurrency8
[] = "XXX";
28 CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode
, UErrorCode
& ec
) {
29 // The constructor always leaves the CurrencyUnit in a valid state (with a 3-character currency code).
30 // Note: in ICU4J Currency.getInstance(), we check string length for 3, but in ICU4C we allow a
31 // non-NUL-terminated string to be passed as an argument, so it is not possible to check length.
32 // However, we allow a NUL-terminated empty string, which should have the same behavior as nullptr.
33 // Consider NUL-terminated strings of length 1 or 2 as invalid.
34 const char16_t* isoCodeToUse
;
35 if (U_FAILURE(ec
) || _isoCode
== nullptr || _isoCode
[0] == 0) {
36 isoCodeToUse
= kDefaultCurrency
;
37 } else if (_isoCode
[1] == 0 || _isoCode
[2] == 0) {
38 isoCodeToUse
= kDefaultCurrency
;
39 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
40 } else if (!uprv_isInvariantUString(_isoCode
, 3)) {
41 // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
42 isoCodeToUse
= kDefaultCurrency
;
43 ec
= U_INVARIANT_CONVERSION_ERROR
;
45 isoCodeToUse
= _isoCode
;
47 // TODO: Perform uppercasing here like in ICU4J Currency.getInstance()?
48 uprv_memcpy(isoCode
, isoCodeToUse
, sizeof(UChar
) * 3);
50 char simpleIsoCode
[4];
51 u_UCharsToChars(isoCode
, simpleIsoCode
, 4);
52 initCurrency(simpleIsoCode
);
55 CurrencyUnit::CurrencyUnit(StringPiece _isoCode
, UErrorCode
& ec
) {
56 // Note: unlike the old constructor, reject empty arguments with an error.
57 char isoCodeBuffer
[4];
58 const char* isoCodeToUse
;
59 // uprv_memchr checks that the string contains no internal NULs
60 if (_isoCode
.length() != 3 || uprv_memchr(_isoCode
.data(), 0, 3) != nullptr) {
61 isoCodeToUse
= kDefaultCurrency8
;
62 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
63 } else if (!uprv_isInvariantString(_isoCode
.data(), 3)) {
64 // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
65 isoCodeToUse
= kDefaultCurrency8
;
66 ec
= U_INVARIANT_CONVERSION_ERROR
;
68 // Have to use isoCodeBuffer to ensure the string is NUL-terminated
69 uprv_strncpy(isoCodeBuffer
, _isoCode
.data(), 3);
71 isoCodeToUse
= isoCodeBuffer
;
73 // TODO: Perform uppercasing here like in ICU4J Currency.getInstance()?
74 u_charsToUChars(isoCodeToUse
, isoCode
, 3);
76 initCurrency(isoCodeToUse
);
79 CurrencyUnit::CurrencyUnit(const CurrencyUnit
& other
) : MeasureUnit(other
) {
80 u_strcpy(isoCode
, other
.isoCode
);
83 CurrencyUnit::CurrencyUnit(const MeasureUnit
& other
, UErrorCode
& ec
) : MeasureUnit(other
) {
84 // Make sure this is a currency.
85 // OK to hard-code the string because we are comparing against another hard-coded string.
86 if (uprv_strcmp("currency", getType()) != 0) {
87 ec
= U_ILLEGAL_ARGUMENT_ERROR
;
90 // Get the ISO Code from the subtype field.
91 u_charsToUChars(getSubtype(), isoCode
, 4);
92 isoCode
[3] = 0; // make 100% sure it is NUL-terminated
96 CurrencyUnit::CurrencyUnit() : MeasureUnit() {
97 u_strcpy(isoCode
, kDefaultCurrency
);
98 char simpleIsoCode
[4];
99 u_UCharsToChars(isoCode
, simpleIsoCode
, 4);
100 initCurrency(simpleIsoCode
);
103 CurrencyUnit
& CurrencyUnit::operator=(const CurrencyUnit
& other
) {
104 if (this == &other
) {
107 MeasureUnit::operator=(other
);
108 u_strcpy(isoCode
, other
.isoCode
);
112 CurrencyUnit
* CurrencyUnit::clone() const {
113 return new CurrencyUnit(*this);
116 CurrencyUnit::~CurrencyUnit() {
119 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit
)
123 #endif // !UCONFIG_NO_FORMATTING