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