Coverage Report

Created: 2025-06-24 06:43

/src/icu/source/i18n/currunit.cpp
Line
Count
Source (jump to first uncovered line)
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 "unicode/uchar.h"
20
#include "cstring.h"
21
#include "uinvchar.h"
22
#include "charstr.h"
23
#include "ustr_imp.h"
24
#include "measunit_impl.h"
25
26
U_NAMESPACE_BEGIN
27
28
0
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
0
    bool useDefault = false;
35
0
    if (U_FAILURE(ec) || _isoCode == nullptr || _isoCode[0] == 0) {
36
0
        useDefault = true;
37
0
    } else if (_isoCode[1] == 0 || _isoCode[2] == 0) {
38
0
        useDefault = true;
39
0
        ec = U_ILLEGAL_ARGUMENT_ERROR;
40
0
    } else if (!uprv_isInvariantUString(_isoCode, 3)) {
41
        // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
42
0
        useDefault = true;
43
0
        ec = U_INVARIANT_CONVERSION_ERROR;
44
0
    } else {
45
0
        for (int32_t i=0; i<3; i++) {
46
0
            isoCode[i] = u_asciiToUpper(_isoCode[i]);
47
0
        }
48
0
        isoCode[3] = 0;
49
0
    }
50
0
    if (useDefault) {
51
0
        uprv_memcpy(isoCode, kDefaultCurrency, sizeof(UChar) * 4);
52
0
    }
53
0
    char simpleIsoCode[4];
54
0
    u_UCharsToChars(isoCode, simpleIsoCode, 4);
55
0
    initCurrency(simpleIsoCode);
56
0
}
57
58
0
CurrencyUnit::CurrencyUnit(StringPiece _isoCode, UErrorCode& ec) {
59
    // Note: unlike the old constructor, reject empty arguments with an error.
60
0
    char isoCodeBuffer[4];
61
0
    const char* isoCodeToUse;
62
    // uprv_memchr checks that the string contains no internal NULs
63
0
    if (_isoCode.length() != 3 || uprv_memchr(_isoCode.data(), 0, 3) != nullptr) {
64
0
        isoCodeToUse = kDefaultCurrency8;
65
0
        ec = U_ILLEGAL_ARGUMENT_ERROR;
66
0
    } else if (!uprv_isInvariantString(_isoCode.data(), 3)) {
67
        // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
68
0
        isoCodeToUse = kDefaultCurrency8;
69
0
        ec = U_INVARIANT_CONVERSION_ERROR;
70
0
    } else {
71
        // Have to use isoCodeBuffer to ensure the string is NUL-terminated
72
0
        for (int32_t i=0; i<3; i++) {
73
0
            isoCodeBuffer[i] = uprv_toupper(_isoCode.data()[i]);
74
0
        }
75
0
        isoCodeBuffer[3] = 0;
76
0
        isoCodeToUse = isoCodeBuffer;
77
0
    }
78
0
    u_charsToUChars(isoCodeToUse, isoCode, 4);
79
0
    initCurrency(isoCodeToUse);
80
0
}
81
82
0
CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) {
83
0
    u_strcpy(isoCode, other.isoCode);
84
0
}
85
86
0
CurrencyUnit::CurrencyUnit(const MeasureUnit& other, UErrorCode& ec) : MeasureUnit(other) {
87
    // Make sure this is a currency.
88
    // OK to hard-code the string because we are comparing against another hard-coded string.
89
0
    if (uprv_strcmp("currency", getType()) != 0) {
90
0
        ec = U_ILLEGAL_ARGUMENT_ERROR;
91
0
        isoCode[0] = 0;
92
0
    } else {
93
        // Get the ISO Code from the subtype field.
94
0
        u_charsToUChars(getSubtype(), isoCode, 4);
95
0
        isoCode[3] = 0; // make 100% sure it is NUL-terminated
96
0
    }
97
0
}
98
99
0
CurrencyUnit::CurrencyUnit() : MeasureUnit() {
100
0
    u_strcpy(isoCode, kDefaultCurrency);
101
0
    char simpleIsoCode[4];
102
0
    u_UCharsToChars(isoCode, simpleIsoCode, 4);
103
0
    initCurrency(simpleIsoCode);
104
0
}
105
106
0
CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) {
107
0
    if (this == &other) {
108
0
        return *this;
109
0
    }
110
0
    MeasureUnit::operator=(other);
111
0
    u_strcpy(isoCode, other.isoCode);
112
0
    return *this;
113
0
}
114
115
0
CurrencyUnit* CurrencyUnit::clone() const {
116
0
    return new CurrencyUnit(*this);
117
0
}
118
119
0
CurrencyUnit::~CurrencyUnit() {
120
0
}
121
    
122
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit)
123
124
U_NAMESPACE_END
125
126
#endif // !UCONFIG_NO_FORMATTING