X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/51004dcb01e06fef634b61be77ed73dd61cb6db9..249c4c5ea9376c24572daf9c2effa7484a282f14:/icuSources/i18n/currunit.cpp diff --git a/icuSources/i18n/currunit.cpp b/icuSources/i18n/currunit.cpp index 870d9d0c..3de6856a 100644 --- a/icuSources/i18n/currunit.cpp +++ b/icuSources/i18n/currunit.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /* ********************************************************************** -* Copyright (c) 2004-2012, International Business Machines +* Copyright (c) 2004-2014, International Business Machines * Corporation and others. All Rights Reserved. ********************************************************************** * Author: Alan Liu @@ -8,37 +10,71 @@ * Since: ICU 3.0 ********************************************************************** */ -#include "utypeinfo.h" // for 'typeid' to work - #include "unicode/utypes.h" #if !UCONFIG_NO_FORMATTING #include "unicode/currunit.h" #include "unicode/ustring.h" +#include "cstring.h" +#include "uinvchar.h" + +static constexpr char16_t kDefaultCurrency[] = u"XXX"; U_NAMESPACE_BEGIN -CurrencyUnit::CurrencyUnit(const UChar* _isoCode, UErrorCode& ec) { - *isoCode = 0; - if (U_SUCCESS(ec)) { - if (_isoCode && u_strlen(_isoCode)==3) { - u_strcpy(isoCode, _isoCode); - } else { - ec = U_ILLEGAL_ARGUMENT_ERROR; - } +CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) { + // The constructor always leaves the CurrencyUnit in a valid state (with a 3-character currency code). + // Note: in ICU4J Currency.getInstance(), we check string length for 3, but in ICU4C we allow a + // non-NUL-terminated string to be passed as an argument, so it is not possible to check length. + const char16_t* isoCodeToUse; + if (U_FAILURE(ec) || _isoCode == nullptr) { + isoCodeToUse = kDefaultCurrency; + } else if (!uprv_isInvariantUString(_isoCode, 3)) { + // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code? + isoCodeToUse = kDefaultCurrency; + ec = U_INVARIANT_CONVERSION_ERROR; + } else { + isoCodeToUse = _isoCode; } + // TODO: Perform uppercasing here like in ICU4J Currency.getInstance()? + uprv_memcpy(isoCode, isoCodeToUse, sizeof(UChar) * 3); + isoCode[3] = 0; + char simpleIsoCode[4]; + u_UCharsToChars(isoCode, simpleIsoCode, 4); + initCurrency(simpleIsoCode); } -CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : - MeasureUnit(other) { - *this = other; +CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) { + u_strcpy(isoCode, other.isoCode); +} + +CurrencyUnit::CurrencyUnit(const MeasureUnit& other, UErrorCode& ec) : MeasureUnit(other) { + // Make sure this is a currency. + // OK to hard-code the string because we are comparing against another hard-coded string. + if (uprv_strcmp("currency", getType()) != 0) { + ec = U_ILLEGAL_ARGUMENT_ERROR; + isoCode[0] = 0; + } else { + // Get the ISO Code from the subtype field. + u_charsToUChars(getSubtype(), isoCode, 4); + isoCode[3] = 0; // make 100% sure it is NUL-terminated + } +} + +CurrencyUnit::CurrencyUnit() : MeasureUnit() { + u_strcpy(isoCode, kDefaultCurrency); + char simpleIsoCode[4]; + u_UCharsToChars(isoCode, simpleIsoCode, 4); + initCurrency(simpleIsoCode); } CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) { - if (this != &other) { - u_strcpy(isoCode, other.isoCode); + if (this == &other) { + return *this; } + MeasureUnit::operator=(other); + u_strcpy(isoCode, other.isoCode); return *this; } @@ -49,12 +85,6 @@ UObject* CurrencyUnit::clone() const { CurrencyUnit::~CurrencyUnit() { } -UBool CurrencyUnit::operator==(const UObject& other) const { - const CurrencyUnit& c = (const CurrencyUnit&) other; - return typeid(*this) == typeid(other) && - u_strcmp(isoCode, c.isoCode) == 0; -} - UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit) U_NAMESPACE_END