| 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 | #include "charstr.h" | 
|---|
| 22 |  | 
|---|
| 23 | static constexpr char16_t kDefaultCurrency[] = u "XXX"; | 
|---|
| 24 | static constexpr char kDefaultCurrency8[] = "XXX"; | 
|---|
| 25 |  | 
|---|
| 26 | U_NAMESPACE_BEGIN | 
|---|
| 27 |  | 
|---|
| 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; | 
|---|
| 44 | } else { | 
|---|
| 45 | isoCodeToUse = _isoCode; | 
|---|
| 46 | } | 
|---|
| 47 | // TODO: Perform uppercasing here like in ICU4J Currency.getInstance()? | 
|---|
| 48 | uprv_memcpy(isoCode, isoCodeToUse, sizeof(UChar) * 3); | 
|---|
| 49 | isoCode[3] = 0; | 
|---|
| 50 | char simpleIsoCode[4]; | 
|---|
| 51 | u_UCharsToChars(isoCode, simpleIsoCode, 4); | 
|---|
| 52 | initCurrency(simpleIsoCode); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 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; | 
|---|
| 67 | } else { | 
|---|
| 68 | // Have to use isoCodeBuffer to ensure the string is NUL-terminated | 
|---|
| 69 | uprv_strncpy(isoCodeBuffer, _isoCode.data(), 3); | 
|---|
| 70 | isoCodeBuffer[3] = 0; | 
|---|
| 71 | isoCodeToUse = isoCodeBuffer; | 
|---|
| 72 | } | 
|---|
| 73 | // TODO: Perform uppercasing here like in ICU4J Currency.getInstance()? | 
|---|
| 74 | u_charsToUChars(isoCodeToUse, isoCode, 3); | 
|---|
| 75 | isoCode[3] = 0; | 
|---|
| 76 | initCurrency(isoCodeToUse); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) { | 
|---|
| 80 | u_strcpy(isoCode, other.isoCode); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 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; | 
|---|
| 88 | isoCode[0] = 0; | 
|---|
| 89 | } else { | 
|---|
| 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 | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | CurrencyUnit::CurrencyUnit() : MeasureUnit() { | 
|---|
| 97 | u_strcpy(isoCode, kDefaultCurrency); | 
|---|
| 98 | char simpleIsoCode[4]; | 
|---|
| 99 | u_UCharsToChars(isoCode, simpleIsoCode, 4); | 
|---|
| 100 | initCurrency(simpleIsoCode); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) { | 
|---|
| 104 | if (this == &other) { | 
|---|
| 105 | return *this; | 
|---|
| 106 | } | 
|---|
| 107 | MeasureUnit::operator=(other); | 
|---|
| 108 | u_strcpy(isoCode, other.isoCode); | 
|---|
| 109 | return *this; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | CurrencyUnit* CurrencyUnit::clone() const { | 
|---|
| 113 | return new CurrencyUnit(*this); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | CurrencyUnit::~CurrencyUnit() { | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit) | 
|---|
| 120 |  | 
|---|
| 121 | U_NAMESPACE_END | 
|---|
| 122 |  | 
|---|
| 123 | #endif // !UCONFIG_NO_FORMATTING | 
|---|
| 124 |  | 
|---|