/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 | 4 | void U_CALLCONV initDefaultProperties(UErrorCode&) { |
24 | | // can't fail, uses placement new into statically allocated space. |
25 | 4 | new(kRawDefaultProperties) DecimalFormatProperties(); // set to the default instance |
26 | 4 | } |
27 | | |
28 | | } |
29 | | |
30 | | |
31 | 575k | DecimalFormatProperties::DecimalFormatProperties() { |
32 | 575k | clear(); |
33 | 575k | } |
34 | | |
35 | 575k | void DecimalFormatProperties::clear() { |
36 | 575k | compactStyle.nullify(); |
37 | 575k | currency.nullify(); |
38 | 575k | currencyPluralInfo.fPtr.adoptInstead(nullptr); |
39 | 575k | currencyUsage.nullify(); |
40 | 575k | decimalPatternMatchRequired = false; |
41 | 575k | decimalSeparatorAlwaysShown = false; |
42 | 575k | exponentSignAlwaysShown = false; |
43 | 575k | currencyAsDecimal = false; |
44 | 575k | formatFailIfMoreThanMaxDigits = false; |
45 | 575k | formatWidth = -1; |
46 | 575k | groupingSize = -1; |
47 | 575k | groupingUsed = true; |
48 | 575k | magnitudeMultiplier = 0; |
49 | 575k | maximumFractionDigits = -1; |
50 | 575k | maximumIntegerDigits = -1; |
51 | 575k | maximumSignificantDigits = -1; |
52 | 575k | minimumExponentDigits = -1; |
53 | 575k | minimumFractionDigits = -1; |
54 | 575k | minimumGroupingDigits = -1; |
55 | 575k | minimumIntegerDigits = -1; |
56 | 575k | minimumSignificantDigits = -1; |
57 | 575k | multiplier = 1; |
58 | 575k | multiplierScale = 0; |
59 | 575k | negativePrefix.setToBogus(); |
60 | 575k | negativePrefixPattern.setToBogus(); |
61 | 575k | negativeSuffix.setToBogus(); |
62 | 575k | negativeSuffixPattern.setToBogus(); |
63 | 575k | padPosition.nullify(); |
64 | 575k | padString.setToBogus(); |
65 | 575k | parseCaseSensitive = false; |
66 | 575k | parseIntegerOnly = false; |
67 | 575k | parseMode.nullify(); |
68 | 575k | parseNoExponent = false; |
69 | 575k | parseToBigDecimal = false; |
70 | 575k | parseAllInput = UNUM_MAYBE; |
71 | 575k | positivePrefix.setToBogus(); |
72 | 575k | positivePrefixPattern.setToBogus(); |
73 | 575k | positiveSuffix.setToBogus(); |
74 | 575k | positiveSuffixPattern.setToBogus(); |
75 | 575k | roundingIncrement = 0.0; |
76 | 575k | roundingMode.nullify(); |
77 | 575k | secondaryGroupingSize = -1; |
78 | 575k | signAlwaysShown = false; |
79 | 575k | } |
80 | | |
81 | | bool |
82 | 495k | DecimalFormatProperties::_equals(const DecimalFormatProperties& other, bool ignoreForFastFormat) const { |
83 | 495k | bool eq = true; |
84 | | |
85 | | // Properties that must be equal both normally and for fast-path formatting |
86 | 495k | eq = eq && compactStyle == other.compactStyle; |
87 | 495k | eq = eq && currency == other.currency; |
88 | 495k | eq = eq && currencyPluralInfo.fPtr.getAlias() == other.currencyPluralInfo.fPtr.getAlias(); |
89 | 495k | eq = eq && currencyUsage == other.currencyUsage; |
90 | 495k | eq = eq && decimalSeparatorAlwaysShown == other.decimalSeparatorAlwaysShown; |
91 | 495k | eq = eq && exponentSignAlwaysShown == other.exponentSignAlwaysShown; |
92 | 495k | eq = eq && currencyAsDecimal == other.currencyAsDecimal; |
93 | 495k | eq = eq && formatFailIfMoreThanMaxDigits == other.formatFailIfMoreThanMaxDigits; |
94 | 495k | eq = eq && formatWidth == other.formatWidth; |
95 | 495k | eq = eq && magnitudeMultiplier == other.magnitudeMultiplier; |
96 | 495k | eq = eq && maximumSignificantDigits == other.maximumSignificantDigits; |
97 | 495k | eq = eq && minimumExponentDigits == other.minimumExponentDigits; |
98 | 495k | eq = eq && minimumGroupingDigits == other.minimumGroupingDigits; |
99 | 495k | eq = eq && minimumSignificantDigits == other.minimumSignificantDigits; |
100 | 495k | eq = eq && multiplier == other.multiplier; |
101 | 495k | eq = eq && multiplierScale == other.multiplierScale; |
102 | 495k | eq = eq && negativePrefix == other.negativePrefix; |
103 | 495k | eq = eq && negativeSuffix == other.negativeSuffix; |
104 | 495k | eq = eq && padPosition == other.padPosition; |
105 | 495k | eq = eq && padString == other.padString; |
106 | 495k | eq = eq && positivePrefix == other.positivePrefix; |
107 | 495k | eq = eq && positiveSuffix == other.positiveSuffix; |
108 | 495k | eq = eq && roundingIncrement == other.roundingIncrement; |
109 | 495k | eq = eq && roundingMode == other.roundingMode; |
110 | 495k | eq = eq && secondaryGroupingSize == other.secondaryGroupingSize; |
111 | 495k | eq = eq && signAlwaysShown == other.signAlwaysShown; |
112 | | |
113 | 495k | if (ignoreForFastFormat) { |
114 | 495k | return eq; |
115 | 495k | } |
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 | 495k | } |
141 | | |
142 | 495k | bool DecimalFormatProperties::equalsDefaultExceptFastFormat() const { |
143 | 495k | UErrorCode localStatus = U_ZERO_ERROR; |
144 | 495k | umtx_initOnce(gDefaultPropertiesInitOnce, &initDefaultProperties, localStatus); |
145 | 495k | return _equals(*reinterpret_cast<DecimalFormatProperties*>(kRawDefaultProperties), true); |
146 | 495k | } |
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 */ |