Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/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 "cstring.h"
20
#include "uinvchar.h"
21
22
static constexpr char16_t kDefaultCurrency[] = u"XXX";
23
24
U_NAMESPACE_BEGIN
25
26
0
CurrencyUnit::CurrencyUnit(ConstChar16Ptr _isoCode, UErrorCode& ec) {
27
0
    // The constructor always leaves the CurrencyUnit in a valid state (with a 3-character currency code).
28
0
    // Note: in ICU4J Currency.getInstance(), we check string length for 3, but in ICU4C we allow a
29
0
    // non-NUL-terminated string to be passed as an argument, so it is not possible to check length.
30
0
    const char16_t* isoCodeToUse;
31
0
    if (U_FAILURE(ec) || _isoCode == nullptr) {
32
0
        isoCodeToUse = kDefaultCurrency;
33
0
    } else if (!uprv_isInvariantUString(_isoCode, 3)) {
34
0
        // TODO: Perform a more strict ASCII check like in ICU4J isAlpha3Code?
35
0
        isoCodeToUse = kDefaultCurrency;
36
0
        ec = U_INVARIANT_CONVERSION_ERROR;
37
0
    } else {
38
0
        isoCodeToUse = _isoCode;
39
0
    }
40
0
    // TODO: Perform uppercasing here like in ICU4J Currency.getInstance()?
41
0
    uprv_memcpy(isoCode, isoCodeToUse, sizeof(UChar) * 3);
42
0
    isoCode[3] = 0;
43
0
    char simpleIsoCode[4];
44
0
    u_UCharsToChars(isoCode, simpleIsoCode, 4);
45
0
    initCurrency(simpleIsoCode);
46
0
}
47
48
0
CurrencyUnit::CurrencyUnit(const CurrencyUnit& other) : MeasureUnit(other) {
49
0
    u_strcpy(isoCode, other.isoCode);
50
0
}
51
52
0
CurrencyUnit::CurrencyUnit(const MeasureUnit& other, UErrorCode& ec) : MeasureUnit(other) {
53
0
    // Make sure this is a currency.
54
0
    // OK to hard-code the string because we are comparing against another hard-coded string.
55
0
    if (uprv_strcmp("currency", getType()) != 0) {
56
0
        ec = U_ILLEGAL_ARGUMENT_ERROR;
57
0
        isoCode[0] = 0;
58
0
    } else {
59
0
        // Get the ISO Code from the subtype field.
60
0
        u_charsToUChars(getSubtype(), isoCode, 4);
61
0
        isoCode[3] = 0; // make 100% sure it is NUL-terminated
62
0
    }
63
0
}
64
65
0
CurrencyUnit::CurrencyUnit() : MeasureUnit() {
66
0
    u_strcpy(isoCode, kDefaultCurrency);
67
0
    char simpleIsoCode[4];
68
0
    u_UCharsToChars(isoCode, simpleIsoCode, 4);
69
0
    initCurrency(simpleIsoCode);
70
0
}
71
72
0
CurrencyUnit& CurrencyUnit::operator=(const CurrencyUnit& other) {
73
0
    if (this == &other) {
74
0
        return *this;
75
0
    }
76
0
    MeasureUnit::operator=(other);
77
0
    u_strcpy(isoCode, other.isoCode);
78
0
    return *this;
79
0
}
80
81
0
UObject* CurrencyUnit::clone() const {
82
0
    return new CurrencyUnit(*this);
83
0
}
84
85
0
CurrencyUnit::~CurrencyUnit() {
86
0
}
87
    
88
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(CurrencyUnit)
89
90
U_NAMESPACE_END
91
92
#endif // !UCONFIG_NO_FORMATTING