]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/number_patternmodifier.cpp
ICU-64252.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / number_patternmodifier.cpp
CommitLineData
0f5d89e8
A
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 "cstring.h"
9#include "number_patternmodifier.h"
10#include "unicode/dcfmtsym.h"
11#include "unicode/ucurr.h"
12#include "unicode/unistr.h"
13#include "number_microprops.h"
14
15using namespace icu;
16using namespace icu::number;
17using namespace icu::number::impl;
18
19
20AffixPatternProvider::~AffixPatternProvider() = default;
21
22
23MutablePatternModifier::MutablePatternModifier(bool isStrong)
24 : fStrong(isStrong) {}
25
3d1f044b
A
26void MutablePatternModifier::setPatternInfo(const AffixPatternProvider* patternInfo, Field field) {
27 fPatternInfo = patternInfo;
28 fField = field;
0f5d89e8
A
29}
30
31void MutablePatternModifier::setPatternAttributes(UNumberSignDisplay signDisplay, bool perMille) {
3d1f044b
A
32 fSignDisplay = signDisplay;
33 fPerMilleReplacesPercent = perMille;
0f5d89e8
A
34}
35
36void MutablePatternModifier::setSymbols(const DecimalFormatSymbols* symbols,
37 const CurrencySymbols* currencySymbols,
38 const UNumberUnitWidth unitWidth, const PluralRules* rules) {
39 U_ASSERT((rules != nullptr) == needsPlurals());
3d1f044b
A
40 fSymbols = symbols;
41 fCurrencySymbols = currencySymbols;
42 fUnitWidth = unitWidth;
43 fRules = rules;
0f5d89e8
A
44}
45
46void MutablePatternModifier::setNumberProperties(int8_t signum, StandardPlural::Form plural) {
3d1f044b
A
47 fSignum = signum;
48 fPlural = plural;
0f5d89e8
A
49}
50
51bool MutablePatternModifier::needsPlurals() const {
52 UErrorCode statusLocal = U_ZERO_ERROR;
3d1f044b 53 return fPatternInfo->containsSymbolType(AffixPatternType::TYPE_CURRENCY_TRIPLE, statusLocal);
0f5d89e8
A
54 // Silently ignore any error codes.
55}
56
57ImmutablePatternModifier* MutablePatternModifier::createImmutable(UErrorCode& status) {
58 return createImmutableAndChain(nullptr, status);
59}
60
61ImmutablePatternModifier*
62MutablePatternModifier::createImmutableAndChain(const MicroPropsGenerator* parent, UErrorCode& status) {
63
64 // TODO: Move StandardPlural VALUES to standardplural.h
65 static const StandardPlural::Form STANDARD_PLURAL_VALUES[] = {
66 StandardPlural::Form::ZERO,
67 StandardPlural::Form::ONE,
68 StandardPlural::Form::TWO,
69 StandardPlural::Form::FEW,
70 StandardPlural::Form::MANY,
71 StandardPlural::Form::OTHER};
72
3d1f044b 73 auto pm = new AdoptingModifierStore();
0f5d89e8
A
74 if (pm == nullptr) {
75 status = U_MEMORY_ALLOCATION_ERROR;
76 return nullptr;
77 }
78
79 if (needsPlurals()) {
80 // Slower path when we require the plural keyword.
81 for (StandardPlural::Form plural : STANDARD_PLURAL_VALUES) {
82 setNumberProperties(1, plural);
3d1f044b 83 pm->adoptModifier(1, plural, createConstantModifier(status));
0f5d89e8 84 setNumberProperties(0, plural);
3d1f044b 85 pm->adoptModifier(0, plural, createConstantModifier(status));
0f5d89e8 86 setNumberProperties(-1, plural);
3d1f044b 87 pm->adoptModifier(-1, plural, createConstantModifier(status));
0f5d89e8
A
88 }
89 if (U_FAILURE(status)) {
90 delete pm;
91 return nullptr;
92 }
3d1f044b 93 return new ImmutablePatternModifier(pm, fRules, parent); // adopts pm
0f5d89e8
A
94 } else {
95 // Faster path when plural keyword is not needed.
96 setNumberProperties(1, StandardPlural::Form::COUNT);
3d1f044b 97 pm->adoptModifierWithoutPlural(1, createConstantModifier(status));
0f5d89e8 98 setNumberProperties(0, StandardPlural::Form::COUNT);
3d1f044b 99 pm->adoptModifierWithoutPlural(0, createConstantModifier(status));
0f5d89e8 100 setNumberProperties(-1, StandardPlural::Form::COUNT);
3d1f044b 101 pm->adoptModifierWithoutPlural(-1, createConstantModifier(status));
0f5d89e8
A
102 if (U_FAILURE(status)) {
103 delete pm;
104 return nullptr;
105 }
106 return new ImmutablePatternModifier(pm, nullptr, parent); // adopts pm
107 }
108}
109
110ConstantMultiFieldModifier* MutablePatternModifier::createConstantModifier(UErrorCode& status) {
111 NumberStringBuilder a;
112 NumberStringBuilder b;
113 insertPrefix(a, 0, status);
114 insertSuffix(b, 0, status);
3d1f044b 115 if (fPatternInfo->hasCurrencySign()) {
0f5d89e8 116 return new CurrencySpacingEnabledModifier(
3d1f044b 117 a, b, !fPatternInfo->hasBody(), fStrong, *fSymbols, status);
0f5d89e8 118 } else {
3d1f044b 119 return new ConstantMultiFieldModifier(a, b, !fPatternInfo->hasBody(), fStrong);
0f5d89e8
A
120 }
121}
122
3d1f044b 123ImmutablePatternModifier::ImmutablePatternModifier(AdoptingModifierStore* pm, const PluralRules* rules,
0f5d89e8
A
124 const MicroPropsGenerator* parent)
125 : pm(pm), rules(rules), parent(parent) {}
126
127void ImmutablePatternModifier::processQuantity(DecimalQuantity& quantity, MicroProps& micros,
128 UErrorCode& status) const {
129 parent->processQuantity(quantity, micros, status);
3d1f044b 130 applyToMicros(micros, quantity, status);
0f5d89e8
A
131}
132
3d1f044b
A
133void ImmutablePatternModifier::applyToMicros(
134 MicroProps& micros, const DecimalQuantity& quantity, UErrorCode& status) const {
0f5d89e8 135 if (rules == nullptr) {
3d1f044b 136 micros.modMiddle = pm->getModifierWithoutPlural(quantity.signum());
0f5d89e8 137 } else {
3d1f044b
A
138 StandardPlural::Form pluralForm = utils::getPluralSafe(micros.rounder, rules, quantity, status);
139 micros.modMiddle = pm->getModifier(quantity.signum(), pluralForm);
0f5d89e8
A
140 }
141}
142
143const Modifier* ImmutablePatternModifier::getModifier(int8_t signum, StandardPlural::Form plural) const {
144 if (rules == nullptr) {
3d1f044b 145 return pm->getModifierWithoutPlural(signum);
0f5d89e8
A
146 } else {
147 return pm->getModifier(signum, plural);
148 }
149}
150
151
152/** Used by the unsafe code path. */
153MicroPropsGenerator& MutablePatternModifier::addToChain(const MicroPropsGenerator* parent) {
3d1f044b 154 fParent = parent;
0f5d89e8
A
155 return *this;
156}
157
158void MutablePatternModifier::processQuantity(DecimalQuantity& fq, MicroProps& micros,
159 UErrorCode& status) const {
3d1f044b 160 fParent->processQuantity(fq, micros, status);
0f5d89e8
A
161 // The unsafe code path performs self-mutation, so we need a const_cast.
162 // This method needs to be const because it overrides a const method in the parent class.
163 auto nonConstThis = const_cast<MutablePatternModifier*>(this);
164 if (needsPlurals()) {
3d1f044b
A
165 StandardPlural::Form pluralForm = utils::getPluralSafe(micros.rounder, fRules, fq, status);
166 nonConstThis->setNumberProperties(fq.signum(), pluralForm);
0f5d89e8
A
167 } else {
168 nonConstThis->setNumberProperties(fq.signum(), StandardPlural::Form::COUNT);
169 }
170 micros.modMiddle = this;
171}
172
173int32_t MutablePatternModifier::apply(NumberStringBuilder& output, int32_t leftIndex, int32_t rightIndex,
174 UErrorCode& status) const {
175 // The unsafe code path performs self-mutation, so we need a const_cast.
176 // This method needs to be const because it overrides a const method in the parent class.
177 auto nonConstThis = const_cast<MutablePatternModifier*>(this);
178 int32_t prefixLen = nonConstThis->insertPrefix(output, leftIndex, status);
179 int32_t suffixLen = nonConstThis->insertSuffix(output, rightIndex + prefixLen, status);
180 // If the pattern had no decimal stem body (like #,##0.00), overwrite the value.
181 int32_t overwriteLen = 0;
3d1f044b 182 if (!fPatternInfo->hasBody()) {
0f5d89e8
A
183 overwriteLen = output.splice(
184 leftIndex + prefixLen,
185 rightIndex + prefixLen,
186 UnicodeString(),
187 0,
188 0,
189 UNUM_FIELD_COUNT,
190 status);
191 }
192 CurrencySpacingEnabledModifier::applyCurrencySpacing(
193 output,
194 leftIndex,
195 prefixLen,
196 rightIndex + overwriteLen + prefixLen,
197 suffixLen,
3d1f044b 198 *fSymbols,
0f5d89e8
A
199 status);
200 return prefixLen + overwriteLen + suffixLen;
201}
202
3d1f044b 203int32_t MutablePatternModifier::getPrefixLength() const {
0f5d89e8
A
204 // The unsafe code path performs self-mutation, so we need a const_cast.
205 // This method needs to be const because it overrides a const method in the parent class.
206 auto nonConstThis = const_cast<MutablePatternModifier*>(this);
207
208 // Enter and exit CharSequence Mode to get the length.
3d1f044b 209 UErrorCode status = U_ZERO_ERROR; // status fails only with an iilegal argument exception
0f5d89e8
A
210 nonConstThis->prepareAffix(true);
211 int result = AffixUtils::unescapedCodePointCount(currentAffix, *this, status); // prefix length
212 return result;
213}
214
3d1f044b 215int32_t MutablePatternModifier::getCodePointCount() const {
0f5d89e8
A
216 // The unsafe code path performs self-mutation, so we need a const_cast.
217 // This method needs to be const because it overrides a const method in the parent class.
218 auto nonConstThis = const_cast<MutablePatternModifier*>(this);
219
220 // Render the affixes to get the length
3d1f044b 221 UErrorCode status = U_ZERO_ERROR; // status fails only with an iilegal argument exception
0f5d89e8
A
222 nonConstThis->prepareAffix(true);
223 int result = AffixUtils::unescapedCodePointCount(currentAffix, *this, status); // prefix length
224 nonConstThis->prepareAffix(false);
225 result += AffixUtils::unescapedCodePointCount(currentAffix, *this, status); // suffix length
226 return result;
227}
228
229bool MutablePatternModifier::isStrong() const {
230 return fStrong;
231}
232
3d1f044b
A
233bool MutablePatternModifier::containsField(UNumberFormatFields field) const {
234 (void)field;
235 // This method is not currently used.
236 UPRV_UNREACHABLE;
237}
238
239void MutablePatternModifier::getParameters(Parameters& output) const {
240 (void)output;
241 // This method is not currently used.
242 UPRV_UNREACHABLE;
243}
244
245bool MutablePatternModifier::semanticallyEquivalent(const Modifier& other) const {
246 (void)other;
247 // This method is not currently used.
248 UPRV_UNREACHABLE;
249}
250
0f5d89e8
A
251int32_t MutablePatternModifier::insertPrefix(NumberStringBuilder& sb, int position, UErrorCode& status) {
252 prepareAffix(true);
3d1f044b 253 int32_t length = AffixUtils::unescape(currentAffix, sb, position, *this, fField, status);
0f5d89e8
A
254 return length;
255}
256
257int32_t MutablePatternModifier::insertSuffix(NumberStringBuilder& sb, int position, UErrorCode& status) {
258 prepareAffix(false);
3d1f044b 259 int32_t length = AffixUtils::unescape(currentAffix, sb, position, *this, fField, status);
0f5d89e8
A
260 return length;
261}
262
263/** This method contains the heart of the logic for rendering LDML affix strings. */
264void MutablePatternModifier::prepareAffix(bool isPrefix) {
265 PatternStringUtils::patternInfoToStringBuilder(
3d1f044b 266 *fPatternInfo, isPrefix, fSignum, fSignDisplay, fPlural, fPerMilleReplacesPercent, currentAffix);
0f5d89e8
A
267}
268
269UnicodeString MutablePatternModifier::getSymbol(AffixPatternType type) const {
270 UErrorCode localStatus = U_ZERO_ERROR;
271 switch (type) {
272 case AffixPatternType::TYPE_MINUS_SIGN:
3d1f044b 273 return fSymbols->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kMinusSignSymbol);
0f5d89e8 274 case AffixPatternType::TYPE_PLUS_SIGN:
3d1f044b 275 return fSymbols->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kPlusSignSymbol);
0f5d89e8 276 case AffixPatternType::TYPE_PERCENT:
3d1f044b 277 return fSymbols->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kPercentSymbol);
0f5d89e8 278 case AffixPatternType::TYPE_PERMILLE:
3d1f044b 279 return fSymbols->getSymbol(DecimalFormatSymbols::ENumberFormatSymbol::kPerMillSymbol);
0f5d89e8
A
280 case AffixPatternType::TYPE_CURRENCY_SINGLE: {
281 // UnitWidth ISO and HIDDEN overrides the singular currency symbol.
3d1f044b
A
282 if (fUnitWidth == UNumberUnitWidth::UNUM_UNIT_WIDTH_ISO_CODE) {
283 return fCurrencySymbols->getIntlCurrencySymbol(localStatus);
284 } else if (fUnitWidth == UNumberUnitWidth::UNUM_UNIT_WIDTH_HIDDEN) {
0f5d89e8 285 return UnicodeString();
3d1f044b
A
286 } else if (fUnitWidth == UNumberUnitWidth::UNUM_UNIT_WIDTH_NARROW) {
287 return fCurrencySymbols->getNarrowCurrencySymbol(localStatus);
0f5d89e8 288 } else {
3d1f044b 289 return fCurrencySymbols->getCurrencySymbol(localStatus);
0f5d89e8
A
290 }
291 }
292 case AffixPatternType::TYPE_CURRENCY_DOUBLE:
3d1f044b 293 return fCurrencySymbols->getIntlCurrencySymbol(localStatus);
0f5d89e8
A
294 case AffixPatternType::TYPE_CURRENCY_TRIPLE:
295 // NOTE: This is the code path only for patterns containing "¤¤¤".
296 // Plural currencies set via the API are formatted in LongNameHandler.
297 // This code path is used by DecimalFormat via CurrencyPluralInfo.
3d1f044b
A
298 U_ASSERT(fPlural != StandardPlural::Form::COUNT);
299 return fCurrencySymbols->getPluralName(fPlural, localStatus);
0f5d89e8
A
300 case AffixPatternType::TYPE_CURRENCY_QUAD:
301 return UnicodeString(u"\uFFFD");
302 case AffixPatternType::TYPE_CURRENCY_QUINT:
303 return UnicodeString(u"\uFFFD");
304 default:
3d1f044b 305 UPRV_UNREACHABLE;
0f5d89e8
A
306 }
307}
308
309UnicodeString MutablePatternModifier::toUnicodeString() const {
310 // Never called by AffixUtils
3d1f044b 311 UPRV_UNREACHABLE;
0f5d89e8
A
312}
313
314#endif /* #if !UCONFIG_NO_FORMATTING */