Coverage Report

Created: 2023-02-22 06:51

/src/icu/source/i18n/number_symbolswrapper.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2020 and later: Unicode, Inc. and others.
2
// License & terms of use: http://www.unicode.org/copyright.html
3
4
#include "unicode/utypes.h"
5
6
#if !UCONFIG_NO_FORMATTING
7
8
#include "number_microprops.h"
9
#include "unicode/numberformatter.h"
10
11
using namespace icu;
12
using namespace icu::number;
13
using namespace icu::number::impl;
14
15
0
SymbolsWrapper::SymbolsWrapper(const SymbolsWrapper &other) {
16
0
    doCopyFrom(other);
17
0
}
18
19
0
SymbolsWrapper::SymbolsWrapper(SymbolsWrapper &&src) U_NOEXCEPT {
20
0
    doMoveFrom(std::move(src));
21
0
}
22
23
0
SymbolsWrapper &SymbolsWrapper::operator=(const SymbolsWrapper &other) {
24
0
    if (this == &other) {
25
0
        return *this;
26
0
    }
27
0
    doCleanup();
28
0
    doCopyFrom(other);
29
0
    return *this;
30
0
}
31
32
0
SymbolsWrapper &SymbolsWrapper::operator=(SymbolsWrapper &&src) U_NOEXCEPT {
33
0
    if (this == &src) {
34
0
        return *this;
35
0
    }
36
0
    doCleanup();
37
0
    doMoveFrom(std::move(src));
38
0
    return *this;
39
0
}
40
41
0
SymbolsWrapper::~SymbolsWrapper() {
42
0
    doCleanup();
43
0
}
44
45
0
void SymbolsWrapper::setTo(const DecimalFormatSymbols &dfs) {
46
0
    doCleanup();
47
0
    fType = SYMPTR_DFS;
48
0
    fPtr.dfs = new DecimalFormatSymbols(dfs);
49
0
}
50
51
0
void SymbolsWrapper::setTo(const NumberingSystem *ns) {
52
0
    doCleanup();
53
0
    fType = SYMPTR_NS;
54
0
    fPtr.ns = ns;
55
0
}
56
57
0
void SymbolsWrapper::doCopyFrom(const SymbolsWrapper &other) {
58
0
    fType = other.fType;
59
0
    switch (fType) {
60
0
    case SYMPTR_NONE:
61
        // No action necessary
62
0
        break;
63
0
    case SYMPTR_DFS:
64
        // Memory allocation failures are exposed in copyErrorTo()
65
0
        if (other.fPtr.dfs != nullptr) {
66
0
            fPtr.dfs = new DecimalFormatSymbols(*other.fPtr.dfs);
67
0
        } else {
68
0
            fPtr.dfs = nullptr;
69
0
        }
70
0
        break;
71
0
    case SYMPTR_NS:
72
        // Memory allocation failures are exposed in copyErrorTo()
73
0
        if (other.fPtr.ns != nullptr) {
74
0
            fPtr.ns = new NumberingSystem(*other.fPtr.ns);
75
0
        } else {
76
0
            fPtr.ns = nullptr;
77
0
        }
78
0
        break;
79
0
    }
80
0
}
81
82
0
void SymbolsWrapper::doMoveFrom(SymbolsWrapper &&src) {
83
0
    fType = src.fType;
84
0
    switch (fType) {
85
0
    case SYMPTR_NONE:
86
        // No action necessary
87
0
        break;
88
0
    case SYMPTR_DFS:
89
0
        fPtr.dfs = src.fPtr.dfs;
90
0
        src.fPtr.dfs = nullptr;
91
0
        break;
92
0
    case SYMPTR_NS:
93
0
        fPtr.ns = src.fPtr.ns;
94
0
        src.fPtr.ns = nullptr;
95
0
        break;
96
0
    }
97
0
}
98
99
0
void SymbolsWrapper::doCleanup() {
100
0
    switch (fType) {
101
0
    case SYMPTR_NONE:
102
        // No action necessary
103
0
        break;
104
0
    case SYMPTR_DFS:
105
0
        delete fPtr.dfs;
106
0
        break;
107
0
    case SYMPTR_NS:
108
0
        delete fPtr.ns;
109
0
        break;
110
0
    }
111
0
}
112
113
0
bool SymbolsWrapper::isDecimalFormatSymbols() const {
114
0
    return fType == SYMPTR_DFS;
115
0
}
116
117
0
bool SymbolsWrapper::isNumberingSystem() const {
118
0
    return fType == SYMPTR_NS;
119
0
}
120
121
0
const DecimalFormatSymbols *SymbolsWrapper::getDecimalFormatSymbols() const {
122
0
    U_ASSERT(fType == SYMPTR_DFS);
123
0
    return fPtr.dfs;
124
0
}
125
126
0
const NumberingSystem *SymbolsWrapper::getNumberingSystem() const {
127
0
    U_ASSERT(fType == SYMPTR_NS);
128
0
    return fPtr.ns;
129
0
}
130
131
#endif /* #if !UCONFIG_NO_FORMATTING */