]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/currunit.cpp
ICU-62141.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / currunit.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
374ca955
A
3/*
4**********************************************************************
57a6839d 5* Copyright (c) 2004-2014, International Business Machines
374ca955
A
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"
0f5d89e8
A
19#include "cstring.h"
20#include "uinvchar.h"
21
22static constexpr char16_t kDefaultCurrency[] = u"XXX";
374ca955
A
23
24U_NAMESPACE_BEGIN
25
f3c0d7a5 26CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) {
0f5d89e8
A
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 const char16_t* isoCodeToUse;
31 if (U_FAILURE(ec) || _isoCode == nullptr) {
32 isoCodeToUse = kDefaultCurrency;
33 } else if (!uprv_isInvariantUString(_isoCode, 3)) {
34 // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
35 isoCodeToUse = kDefaultCurrency;
36 ec = U_INVARIANT_CONVERSION_ERROR;
37 } else {
38 isoCodeToUse = _isoCode;
374ca955 39 }
0f5d89e8
A
40 // TODO: Perform uppercasing here like in ICU4J Currency.getInstance()?
41 uprv_memcpy(isoCode, isoCodeToUse, sizeof(UChar) * 3);
42 isoCode[3] = 0;
43 char simpleIsoCode[4];
44 u_UCharsToChars(isoCode, simpleIsoCode, 4);
45 initCurrency(simpleIsoCode);
374ca955
A
46}
47
0f5d89e8 48CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) {
57a6839d 49 u_strcpy(isoCode, other.isoCode);
374ca955
A
50}
51
0f5d89e8
A
52CurrencyUnit::CurrencyUnit(const MeasureUnit& other, UErrorCode& ec) : MeasureUnit(other) {
53 // Make sure this is a currency.
54 // OK to hard-code the string because we are comparing against another hard-coded string.
55 if (uprv_strcmp("currency", getType()) != 0) {
56 ec = U_ILLEGAL_ARGUMENT_ERROR;
57 isoCode[0] = 0;
58 } else {
59 // Get the ISO Code from the subtype field.
60 u_charsToUChars(getSubtype(), isoCode, 4);
61 isoCode[3] = 0; // make 100% sure it is NUL-terminated
62 }
63}
64
65CurrencyUnit::CurrencyUnit() : MeasureUnit() {
66 u_strcpy(isoCode, kDefaultCurrency);
67 char simpleIsoCode[4];
68 u_UCharsToChars(isoCode, simpleIsoCode, 4);
69 initCurrency(simpleIsoCode);
70}
71
374ca955 72CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) {
57a6839d
A
73 if (this == &other) {
74 return *this;
374ca955 75 }
57a6839d
A
76 MeasureUnit::operator=(other);
77 u_strcpy(isoCode, other.isoCode);
374ca955
A
78 return *this;
79}
80
81UObject* CurrencyUnit::clone() const {
82 return new CurrencyUnit(*this);
83}
84
85CurrencyUnit::~CurrencyUnit() {
86}
87
374ca955
A
88UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit)
89
90U_NAMESPACE_END
91
92#endif // !UCONFIG_NO_FORMATTING