1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (c) 2004-2014, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Author: Alan Liu
9 * Created: April 26, 2004
10 * Since: ICU 3.0
11 **********************************************************************
12 */
13 #include "unicode/utypes.h"
14
15 #if !UCONFIG_NO_FORMATTING
16
17 #include "unicode/currunit.h"
18 #include "unicode/ustring.h"
19 #include "cstring.h"
20 #include "uinvchar.h"
21
22 static constexpr char16_t kDefaultCurrency[] = u"XXX";
23
24 U_NAMESPACE_BEGIN
25
CurrencyUnit(ConstChar16Ptr _isoCode,UErrorCode & ec)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 // However, we allow a NUL-terminated empty string, which should have the same behavior as nullptr.
31 // Consider NUL-terminated strings of length 1 or 2 as invalid.
32 const char16_t* isoCodeToUse;
33 if (U_FAILURE(ec) || _isoCode == nullptr || _isoCode[0] == 0) {
34 isoCodeToUse = kDefaultCurrency;
35 } else if (_isoCode[1] == 0 || _isoCode[2] == 0) {
36 isoCodeToUse = kDefaultCurrency;
37 ec = U_ILLEGAL_ARGUMENT_ERROR;
38 } else if (!uprv_isInvariantUString(_isoCode, 3)) {
39 // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
40 isoCodeToUse = kDefaultCurrency;
41 ec = U_INVARIANT_CONVERSION_ERROR;
42 } else {
43 isoCodeToUse = _isoCode;
44 }
45 // TODO: Perform uppercasing here like in ICU4J Currency.getInstance()?
46 uprv_memcpy(isoCode, isoCodeToUse, sizeof(UChar) * 3);
47 isoCode[3] = 0;
48 char simpleIsoCode[4];
49 u_UCharsToChars(isoCode, simpleIsoCode, 4);
50 initCurrency(simpleIsoCode);
51 }
52
CurrencyUnit(const CurrencyUnit & other)53 CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) {
54 u_strcpy(isoCode, other.isoCode);
55 }
56
CurrencyUnit(const MeasureUnit & other,UErrorCode & ec)57 CurrencyUnit::CurrencyUnit(const MeasureUnit& other, UErrorCode& ec) : MeasureUnit(other) {
58 // Make sure this is a currency.
59 // OK to hard-code the string because we are comparing against another hard-coded string.
60 if (uprv_strcmp("currency", getType()) != 0) {
61 ec = U_ILLEGAL_ARGUMENT_ERROR;
62 isoCode[0] = 0;
63 } else {
64 // Get the ISO Code from the subtype field.
65 u_charsToUChars(getSubtype(), isoCode, 4);
66 isoCode[3] = 0; // make 100% sure it is NUL-terminated
67 }
68 }
69
CurrencyUnit()70 CurrencyUnit::CurrencyUnit() : MeasureUnit() {
71 u_strcpy(isoCode, kDefaultCurrency);
72 char simpleIsoCode[4];
73 u_UCharsToChars(isoCode, simpleIsoCode, 4);
74 initCurrency(simpleIsoCode);
75 }
76
operator =(const CurrencyUnit & other)77 CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) {
78 if (this == &other) {
79 return *this;
80 }
81 MeasureUnit::operator=(other);
82 u_strcpy(isoCode, other.isoCode);
83 return *this;
84 }
85
clone() const86 UObject* CurrencyUnit::clone() const {
87 return new CurrencyUnit(*this);
88 }
89
~CurrencyUnit()90 CurrencyUnit::~CurrencyUnit() {
91 }
92
93 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit)
94
95 U_NAMESPACE_END
96
97 #endif // !UCONFIG_NO_FORMATTING
98