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