Coverage Report

Created: 2023-03-29 06:15

/src/icu/icu4c/source/i18n/number_decimfmtprops.cpp
Line
Count
Source (jump to first uncovered line)
1
// © 2017 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_decimfmtprops.h"
9
#include "umutex.h"
10
11
using namespace icu;
12
using namespace icu::number;
13
using namespace icu::number::impl;
14
15
16
namespace {
17
18
alignas(DecimalFormatProperties)
19
char kRawDefaultProperties[sizeof(DecimalFormatProperties)];
20
21
icu::UInitOnce gDefaultPropertiesInitOnce {};
22
23
1
void U_CALLCONV initDefaultProperties(UErrorCode&) {
24
    // can't fail, uses placement new into statically allocated space.
25
1
    new(kRawDefaultProperties) DecimalFormatProperties(); // set to the default instance
26
1
}
27
28
}
29
30
31
4.13k
DecimalFormatProperties::DecimalFormatProperties() {
32
4.13k
    clear();
33
4.13k
}
34
35
4.13k
void DecimalFormatProperties::clear() {
36
4.13k
    compactStyle.nullify();
37
4.13k
    currency.nullify();
38
4.13k
    currencyPluralInfo.fPtr.adoptInstead(nullptr);
39
4.13k
    currencyUsage.nullify();
40
4.13k
    decimalPatternMatchRequired = false;
41
4.13k
    decimalSeparatorAlwaysShown = false;
42
4.13k
    exponentSignAlwaysShown = false;
43
4.13k
    currencyAsDecimal = false;
44
4.13k
    formatFailIfMoreThanMaxDigits = false;
45
4.13k
    formatWidth = -1;
46
4.13k
    groupingSize = -1;
47
4.13k
    groupingUsed = true;
48
4.13k
    magnitudeMultiplier = 0;
49
4.13k
    maximumFractionDigits = -1;
50
4.13k
    maximumIntegerDigits = -1;
51
4.13k
    maximumSignificantDigits = -1;
52
4.13k
    minimumExponentDigits = -1;
53
4.13k
    minimumFractionDigits = -1;
54
4.13k
    minimumGroupingDigits = -1;
55
4.13k
    minimumIntegerDigits = -1;
56
4.13k
    minimumSignificantDigits = -1;
57
4.13k
    multiplier = 1;
58
4.13k
    multiplierScale = 0;
59
4.13k
    negativePrefix.setToBogus();
60
4.13k
    negativePrefixPattern.setToBogus();
61
4.13k
    negativeSuffix.setToBogus();
62
4.13k
    negativeSuffixPattern.setToBogus();
63
4.13k
    padPosition.nullify();
64
4.13k
    padString.setToBogus();
65
4.13k
    parseCaseSensitive = false;
66
4.13k
    parseIntegerOnly = false;
67
4.13k
    parseMode.nullify();
68
4.13k
    parseNoExponent = false;
69
4.13k
    parseToBigDecimal = false;
70
4.13k
    parseAllInput = UNUM_MAYBE;
71
4.13k
    positivePrefix.setToBogus();
72
4.13k
    positivePrefixPattern.setToBogus();
73
4.13k
    positiveSuffix.setToBogus();
74
4.13k
    positiveSuffixPattern.setToBogus();
75
4.13k
    roundingIncrement = 0.0;
76
4.13k
    roundingMode.nullify();
77
4.13k
    secondaryGroupingSize = -1;
78
4.13k
    signAlwaysShown = false;
79
4.13k
}
80
81
bool
82
3.55k
DecimalFormatProperties::_equals(const DecimalFormatProperties& other, bool ignoreForFastFormat) const {
83
3.55k
    bool eq = true;
84
85
    // Properties that must be equal both normally and for fast-path formatting
86
3.55k
    eq = eq && compactStyle == other.compactStyle;
87
3.55k
    eq = eq && currency == other.currency;
88
3.55k
    eq = eq && currencyPluralInfo.fPtr.getAlias() == other.currencyPluralInfo.fPtr.getAlias();
89
3.55k
    eq = eq && currencyUsage == other.currencyUsage;
90
3.55k
    eq = eq && decimalSeparatorAlwaysShown == other.decimalSeparatorAlwaysShown;
91
3.55k
    eq = eq && exponentSignAlwaysShown == other.exponentSignAlwaysShown;
92
3.55k
    eq = eq && currencyAsDecimal == other.currencyAsDecimal;
93
3.55k
    eq = eq && formatFailIfMoreThanMaxDigits == other.formatFailIfMoreThanMaxDigits;
94
3.55k
    eq = eq && formatWidth == other.formatWidth;
95
3.55k
    eq = eq && magnitudeMultiplier == other.magnitudeMultiplier;
96
3.55k
    eq = eq && maximumSignificantDigits == other.maximumSignificantDigits;
97
3.55k
    eq = eq && minimumExponentDigits == other.minimumExponentDigits;
98
3.55k
    eq = eq && minimumGroupingDigits == other.minimumGroupingDigits;
99
3.55k
    eq = eq && minimumSignificantDigits == other.minimumSignificantDigits;
100
3.55k
    eq = eq && multiplier == other.multiplier;
101
3.55k
    eq = eq && multiplierScale == other.multiplierScale;
102
3.55k
    eq = eq && negativePrefix == other.negativePrefix;
103
3.55k
    eq = eq && negativeSuffix == other.negativeSuffix;
104
3.55k
    eq = eq && padPosition == other.padPosition;
105
3.55k
    eq = eq && padString == other.padString;
106
3.55k
    eq = eq && positivePrefix == other.positivePrefix;
107
3.55k
    eq = eq && positiveSuffix == other.positiveSuffix;
108
3.55k
    eq = eq && roundingIncrement == other.roundingIncrement;
109
3.55k
    eq = eq && roundingMode == other.roundingMode;
110
3.55k
    eq = eq && secondaryGroupingSize == other.secondaryGroupingSize;
111
3.55k
    eq = eq && signAlwaysShown == other.signAlwaysShown;
112
113
3.55k
    if (ignoreForFastFormat) {
114
3.55k
        return eq;
115
3.55k
    }
116
117
    // Properties ignored by fast-path formatting
118
    // Formatting (special handling required):
119
0
    eq = eq && groupingSize == other.groupingSize;
120
0
    eq = eq && groupingUsed == other.groupingUsed;
121
0
    eq = eq && minimumFractionDigits == other.minimumFractionDigits;
122
0
    eq = eq && maximumFractionDigits == other.maximumFractionDigits;
123
0
    eq = eq && maximumIntegerDigits == other.maximumIntegerDigits;
124
0
    eq = eq && minimumIntegerDigits == other.minimumIntegerDigits;
125
0
    eq = eq && negativePrefixPattern == other.negativePrefixPattern;
126
0
    eq = eq && negativeSuffixPattern == other.negativeSuffixPattern;
127
0
    eq = eq && positivePrefixPattern == other.positivePrefixPattern;
128
0
    eq = eq && positiveSuffixPattern == other.positiveSuffixPattern;
129
130
    // Parsing (always safe to ignore):
131
0
    eq = eq && decimalPatternMatchRequired == other.decimalPatternMatchRequired;
132
0
    eq = eq && parseCaseSensitive == other.parseCaseSensitive;
133
0
    eq = eq && parseIntegerOnly == other.parseIntegerOnly;
134
0
    eq = eq && parseMode == other.parseMode;
135
0
    eq = eq && parseNoExponent == other.parseNoExponent;
136
0
    eq = eq && parseToBigDecimal == other.parseToBigDecimal;
137
0
    eq = eq && parseAllInput == other.parseAllInput;
138
139
0
    return eq;
140
3.55k
}
141
142
3.55k
bool DecimalFormatProperties::equalsDefaultExceptFastFormat() const {
143
3.55k
    UErrorCode localStatus = U_ZERO_ERROR;
144
3.55k
    umtx_initOnce(gDefaultPropertiesInitOnce, &initDefaultProperties, localStatus);
145
3.55k
    return _equals(*reinterpret_cast<DecimalFormatProperties*>(kRawDefaultProperties), true);
146
3.55k
}
147
148
0
const DecimalFormatProperties& DecimalFormatProperties::getDefault() {
149
0
    UErrorCode localStatus = U_ZERO_ERROR;
150
0
    umtx_initOnce(gDefaultPropertiesInitOnce, &initDefaultProperties, localStatus);
151
0
    return *reinterpret_cast<const DecimalFormatProperties*>(kRawDefaultProperties);
152
0
}
153
154
#endif /* #if !UCONFIG_NO_FORMATTING */