]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/currunit.cpp
ICU-64252.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"
3d1f044b 21#include "charstr.h"
0f5d89e8
A
22
23static constexpr char16_t kDefaultCurrency[] = u"XXX";
3d1f044b 24static constexpr char kDefaultCurrency8[] = "XXX";
374ca955
A
25
26U_NAMESPACE_BEGIN
27
f3c0d7a5 28CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) {
0f5d89e8
A
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.
3d1f044b
A
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.
0f5d89e8 34 const char16_t* isoCodeToUse;
3d1f044b 35 if (U_FAILURE(ec) || _isoCode == nullptr || _isoCode[0] == 0) {
0f5d89e8 36 isoCodeToUse = kDefaultCurrency;
3d1f044b
A
37 } else if (_isoCode[1] == 0 || _isoCode[2] == 0) {
38 isoCodeToUse = kDefaultCurrency;
39 ec = U_ILLEGAL_ARGUMENT_ERROR;
0f5d89e8
A
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;
374ca955 46 }
0f5d89e8
A
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);
374ca955
A
53}
54
3d1f044b
A
55CurrencyUnit::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
0f5d89e8 79CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) {
57a6839d 80 u_strcpy(isoCode, other.isoCode);
374ca955
A
81}
82
0f5d89e8
A
83CurrencyUnit::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
96CurrencyUnit::CurrencyUnit() : MeasureUnit() {
97 u_strcpy(isoCode, kDefaultCurrency);
98 char simpleIsoCode[4];
99 u_UCharsToChars(isoCode, simpleIsoCode, 4);
100 initCurrency(simpleIsoCode);
101}
102
374ca955 103CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) {
57a6839d
A
104 if (this == &other) {
105 return *this;
374ca955 106 }
57a6839d
A
107 MeasureUnit::operator=(other);
108 u_strcpy(isoCode, other.isoCode);
374ca955
A
109 return *this;
110}
111
112UObject* CurrencyUnit::clone() const {
113 return new CurrencyUnit(*this);
114}
115
116CurrencyUnit::~CurrencyUnit() {
117}
118
374ca955
A
119UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit)
120
121U_NAMESPACE_END
122
123#endif // !UCONFIG_NO_FORMATTING