2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2004-04-27 21:47:09 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (c) 2004-2014, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2004-04-27 21:47:09 +00:00
|
|
|
**********************************************************************
|
|
|
|
* Author: Alan Liu
|
|
|
|
* Created: April 26, 2004
|
|
|
|
* Since: ICU 3.0
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
#include "unicode/currunit.h"
|
|
|
|
#include "unicode/ustring.h"
|
2019-12-07 19:47:39 +00:00
|
|
|
#include "unicode/uchar.h"
|
2017-09-12 05:20:50 +00:00
|
|
|
#include "cstring.h"
|
2018-04-19 01:13:17 +00:00
|
|
|
#include "uinvchar.h"
|
2019-01-11 08:43:20 +00:00
|
|
|
#include "charstr.h"
|
2019-12-07 19:47:39 +00:00
|
|
|
#include "ustr_imp.h"
|
2020-03-24 18:28:50 +00:00
|
|
|
#include "measunit_impl.h"
|
2018-04-11 05:52:58 +00:00
|
|
|
|
2004-04-27 21:47:09 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2017-03-01 05:52:24 +00:00
|
|
|
CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) {
|
2018-04-11 05:52:58 +00:00
|
|
|
// The constructor always leaves the CurrencyUnit in a valid state (with a 3-character currency code).
|
2018-04-19 01:13:17 +00:00
|
|
|
// 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.
|
2018-06-23 02:09:42 +00:00
|
|
|
// However, we allow a NUL-terminated empty string, which should have the same behavior as nullptr.
|
2018-09-20 21:44:48 +00:00
|
|
|
// Consider NUL-terminated strings of length 1 or 2 as invalid.
|
2019-12-07 19:47:39 +00:00
|
|
|
bool useDefault = false;
|
2018-06-23 02:09:42 +00:00
|
|
|
if (U_FAILURE(ec) || _isoCode == nullptr || _isoCode[0] == 0) {
|
2019-12-07 19:47:39 +00:00
|
|
|
useDefault = true;
|
2018-09-20 21:44:48 +00:00
|
|
|
} else if (_isoCode[1] == 0 || _isoCode[2] == 0) {
|
2019-12-07 19:47:39 +00:00
|
|
|
useDefault = true;
|
2018-09-20 21:44:48 +00:00
|
|
|
ec = U_ILLEGAL_ARGUMENT_ERROR;
|
2018-04-19 01:13:17 +00:00
|
|
|
} else if (!uprv_isInvariantUString(_isoCode, 3)) {
|
|
|
|
// TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
|
2019-12-07 19:47:39 +00:00
|
|
|
useDefault = true;
|
2018-04-19 01:13:17 +00:00
|
|
|
ec = U_INVARIANT_CONVERSION_ERROR;
|
2018-04-11 05:52:58 +00:00
|
|
|
} else {
|
2019-12-07 19:47:39 +00:00
|
|
|
for (int32_t i=0; i<3; i++) {
|
|
|
|
isoCode[i] = u_asciiToUpper(_isoCode[i]);
|
|
|
|
}
|
|
|
|
isoCode[3] = 0;
|
|
|
|
}
|
|
|
|
if (useDefault) {
|
|
|
|
uprv_memcpy(isoCode, kDefaultCurrency, sizeof(UChar) * 4);
|
2004-04-27 21:47:09 +00:00
|
|
|
}
|
2018-04-11 05:52:58 +00:00
|
|
|
char simpleIsoCode[4];
|
|
|
|
u_UCharsToChars(isoCode, simpleIsoCode, 4);
|
|
|
|
initCurrency(simpleIsoCode);
|
2004-04-27 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 08:43:20 +00:00
|
|
|
CurrencyUnit::CurrencyUnit(StringPiece _isoCode, UErrorCode& ec) {
|
|
|
|
// Note: unlike the old constructor, reject empty arguments with an error.
|
|
|
|
char isoCodeBuffer[4];
|
|
|
|
const char* isoCodeToUse;
|
|
|
|
// uprv_memchr checks that the string contains no internal NULs
|
|
|
|
if (_isoCode.length() != 3 || uprv_memchr(_isoCode.data(), 0, 3) != nullptr) {
|
|
|
|
isoCodeToUse = kDefaultCurrency8;
|
|
|
|
ec = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
} else if (!uprv_isInvariantString(_isoCode.data(), 3)) {
|
|
|
|
// TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
|
|
|
|
isoCodeToUse = kDefaultCurrency8;
|
|
|
|
ec = U_INVARIANT_CONVERSION_ERROR;
|
|
|
|
} else {
|
|
|
|
// Have to use isoCodeBuffer to ensure the string is NUL-terminated
|
2019-12-07 19:47:39 +00:00
|
|
|
for (int32_t i=0; i<3; i++) {
|
|
|
|
isoCodeBuffer[i] = uprv_toupper(_isoCode.data()[i]);
|
|
|
|
}
|
2019-01-11 08:43:20 +00:00
|
|
|
isoCodeBuffer[3] = 0;
|
|
|
|
isoCodeToUse = isoCodeBuffer;
|
|
|
|
}
|
2019-12-07 19:47:39 +00:00
|
|
|
u_charsToUChars(isoCodeToUse, isoCode, 4);
|
2019-01-11 08:43:20 +00:00
|
|
|
initCurrency(isoCodeToUse);
|
|
|
|
}
|
|
|
|
|
2017-09-12 05:20:50 +00:00
|
|
|
CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) {
|
2014-02-04 00:29:17 +00:00
|
|
|
u_strcpy(isoCode, other.isoCode);
|
2004-04-27 21:47:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 05:20:50 +00:00
|
|
|
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() {
|
2018-04-11 05:52:58 +00:00
|
|
|
u_strcpy(isoCode, kDefaultCurrency);
|
2017-09-12 05:20:50 +00:00
|
|
|
char simpleIsoCode[4];
|
|
|
|
u_UCharsToChars(isoCode, simpleIsoCode, 4);
|
|
|
|
initCurrency(simpleIsoCode);
|
|
|
|
}
|
|
|
|
|
2004-04-27 21:47:09 +00:00
|
|
|
CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) {
|
2014-02-04 00:29:17 +00:00
|
|
|
if (this == &other) {
|
|
|
|
return *this;
|
2004-04-27 21:47:09 +00:00
|
|
|
}
|
2014-02-04 00:29:17 +00:00
|
|
|
MeasureUnit::operator=(other);
|
|
|
|
u_strcpy(isoCode, other.isoCode);
|
2004-04-27 21:47:09 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-22 00:13:18 +00:00
|
|
|
CurrencyUnit* CurrencyUnit::clone() const {
|
2004-04-27 21:47:09 +00:00
|
|
|
return new CurrencyUnit(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrencyUnit::~CurrencyUnit() {
|
|
|
|
}
|
|
|
|
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit)
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif // !UCONFIG_NO_FORMATTING
|