1 // © 2018 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 // Allow implicit conversion from char16_t* to UnicodeString for this file:
9 // Helpful in toString methods and elsewhere.
10 #define UNISTR_FROM_STRING_EXPLICIT
11
12 #include "number_mapper.h"
13 #include "number_patternstring.h"
14 #include "unicode/errorcode.h"
15 #include "number_utils.h"
16
17 using namespace icu;
18 using namespace icu::number;
19 using namespace icu::number::impl;
20
21
create(const DecimalFormatProperties & properties,const DecimalFormatSymbols & symbols,DecimalFormatWarehouse & warehouse,UErrorCode & status)22 UnlocalizedNumberFormatter NumberPropertyMapper::create(const DecimalFormatProperties& properties,
23 const DecimalFormatSymbols& symbols,
24 DecimalFormatWarehouse& warehouse,
25 UErrorCode& status) {
26 return NumberFormatter::with().macros(oldToNew(properties, symbols, warehouse, nullptr, status));
27 }
28
create(const DecimalFormatProperties & properties,const DecimalFormatSymbols & symbols,DecimalFormatWarehouse & warehouse,DecimalFormatProperties & exportedProperties,UErrorCode & status)29 UnlocalizedNumberFormatter NumberPropertyMapper::create(const DecimalFormatProperties& properties,
30 const DecimalFormatSymbols& symbols,
31 DecimalFormatWarehouse& warehouse,
32 DecimalFormatProperties& exportedProperties,
33 UErrorCode& status) {
34 return NumberFormatter::with().macros(
35 oldToNew(
36 properties, symbols, warehouse, &exportedProperties, status));
37 }
38
oldToNew(const DecimalFormatProperties & properties,const DecimalFormatSymbols & symbols,DecimalFormatWarehouse & warehouse,DecimalFormatProperties * exportedProperties,UErrorCode & status)39 MacroProps NumberPropertyMapper::oldToNew(const DecimalFormatProperties& properties,
40 const DecimalFormatSymbols& symbols,
41 DecimalFormatWarehouse& warehouse,
42 DecimalFormatProperties* exportedProperties,
43 UErrorCode& status) {
44 MacroProps macros;
45 Locale locale = symbols.getLocale();
46
47 /////////////
48 // SYMBOLS //
49 /////////////
50
51 macros.symbols.setTo(symbols);
52
53 //////////////////
54 // PLURAL RULES //
55 //////////////////
56
57 if (!properties.currencyPluralInfo.fPtr.isNull()) {
58 macros.rules = properties.currencyPluralInfo.fPtr->getPluralRules();
59 }
60
61 /////////////
62 // AFFIXES //
63 /////////////
64
65 warehouse.affixProvider.setTo(properties, status);
66 macros.affixProvider = &warehouse.affixProvider.get();
67
68 ///////////
69 // UNITS //
70 ///////////
71
72 bool useCurrency = (
73 !properties.currency.isNull() ||
74 !properties.currencyPluralInfo.fPtr.isNull() ||
75 !properties.currencyUsage.isNull() ||
76 warehouse.affixProvider.get().hasCurrencySign());
77 CurrencyUnit currency = resolveCurrency(properties, locale, status);
78 UCurrencyUsage currencyUsage = properties.currencyUsage.getOrDefault(UCURR_USAGE_STANDARD);
79 if (useCurrency) {
80 // NOTE: Slicing is OK.
81 macros.unit = currency; // NOLINT
82 }
83
84 ///////////////////////
85 // ROUNDING STRATEGY //
86 ///////////////////////
87
88 int32_t maxInt = properties.maximumIntegerDigits;
89 int32_t minInt = properties.minimumIntegerDigits;
90 int32_t maxFrac = properties.maximumFractionDigits;
91 int32_t minFrac = properties.minimumFractionDigits;
92 int32_t minSig = properties.minimumSignificantDigits;
93 int32_t maxSig = properties.maximumSignificantDigits;
94 double roundingIncrement = properties.roundingIncrement;
95 // Not assigning directly to macros.roundingMode here: we change
96 // roundingMode if and when we also change macros.precision.
97 RoundingMode roundingMode = properties.roundingMode.getOrDefault(UNUM_ROUND_HALFEVEN);
98 bool explicitMinMaxFrac = minFrac != -1 || maxFrac != -1;
99 bool explicitMinMaxSig = minSig != -1 || maxSig != -1;
100 // Resolve min/max frac for currencies, required for the validation logic and for when minFrac or
101 // maxFrac was
102 // set (but not both) on a currency instance.
103 // NOTE: Increments are handled in "Precision.constructCurrency()".
104 if (useCurrency && (minFrac == -1 || maxFrac == -1)) {
105 int32_t digits = ucurr_getDefaultFractionDigitsForUsage(
106 currency.getISOCurrency(), currencyUsage, &status);
107 if (minFrac == -1 && maxFrac == -1) {
108 minFrac = digits;
109 maxFrac = digits;
110 } else if (minFrac == -1) {
111 minFrac = std::min(maxFrac, digits);
112 } else /* if (maxFrac == -1) */ {
113 maxFrac = std::max(minFrac, digits);
114 }
115 }
116 // Validate min/max int/frac.
117 // For backwards compatibility, minimum overrides maximum if the two conflict.
118 if (minInt == 0 && maxFrac != 0) {
119 minFrac = (minFrac < 0 || (minFrac == 0 && maxInt == 0)) ? 1 : minFrac;
120 maxFrac = maxFrac < 0 ? -1 : maxFrac < minFrac ? minFrac : maxFrac;
121 minInt = 0;
122 maxInt = maxInt < 0 ? -1 : maxInt > kMaxIntFracSig ? -1 : maxInt;
123 } else {
124 // Force a digit before the decimal point.
125 minFrac = minFrac < 0 ? 0 : minFrac;
126 maxFrac = maxFrac < 0 ? -1 : maxFrac < minFrac ? minFrac : maxFrac;
127 minInt = minInt <= 0 ? 1 : minInt > kMaxIntFracSig ? 1 : minInt;
128 maxInt = maxInt < 0 ? -1 : maxInt < minInt ? minInt : maxInt > kMaxIntFracSig ? -1 : maxInt;
129 }
130 Precision precision;
131 if (!properties.currencyUsage.isNull()) {
132 precision = Precision::constructCurrency(currencyUsage).withCurrency(currency);
133 } else if (roundingIncrement != 0.0) {
134 if (PatternStringUtils::ignoreRoundingIncrement(roundingIncrement, maxFrac)) {
135 precision = Precision::constructFraction(minFrac, maxFrac);
136 } else {
137 precision = Precision::constructIncrement(roundingIncrement, minFrac);
138 }
139 } else if (explicitMinMaxSig) {
140 minSig = minSig < 1 ? 1 : minSig > kMaxIntFracSig ? kMaxIntFracSig : minSig;
141 maxSig = maxSig < 0 ? kMaxIntFracSig : maxSig < minSig ? minSig : maxSig > kMaxIntFracSig
142 ? kMaxIntFracSig : maxSig;
143 precision = Precision::constructSignificant(minSig, maxSig);
144 } else if (explicitMinMaxFrac) {
145 precision = Precision::constructFraction(minFrac, maxFrac);
146 } else if (useCurrency) {
147 precision = Precision::constructCurrency(currencyUsage);
148 }
149 if (!precision.isBogus()) {
150 macros.roundingMode = roundingMode;
151 macros.precision = precision;
152 }
153
154 ///////////////////
155 // INTEGER WIDTH //
156 ///////////////////
157
158 macros.integerWidth = IntegerWidth(
159 static_cast<digits_t>(minInt),
160 static_cast<digits_t>(maxInt),
161 properties.formatFailIfMoreThanMaxDigits);
162
163 ///////////////////////
164 // GROUPING STRATEGY //
165 ///////////////////////
166
167 macros.grouper = Grouper::forProperties(properties);
168
169 /////////////
170 // PADDING //
171 /////////////
172
173 if (properties.formatWidth > 0) {
174 macros.padder = Padder::forProperties(properties);
175 }
176
177 ///////////////////////////////
178 // DECIMAL MARK ALWAYS SHOWN //
179 ///////////////////////////////
180
181 macros.decimal = properties.decimalSeparatorAlwaysShown ? UNUM_DECIMAL_SEPARATOR_ALWAYS
182 : UNUM_DECIMAL_SEPARATOR_AUTO;
183
184 ///////////////////////
185 // SIGN ALWAYS SHOWN //
186 ///////////////////////
187
188 macros.sign = properties.signAlwaysShown ? UNUM_SIGN_ALWAYS : UNUM_SIGN_AUTO;
189
190 /////////////////////////
191 // SCIENTIFIC NOTATION //
192 /////////////////////////
193
194 if (properties.minimumExponentDigits != -1) {
195 // Scientific notation is required.
196 // This whole section feels like a hack, but it is needed for regression tests.
197 // The mapping from property bag to scientific notation is nontrivial due to LDML rules.
198 if (maxInt > 8) {
199 // But #13110: The maximum of 8 digits has unknown origins and is not in the spec.
200 // If maxInt is greater than 8, it is set to minInt, even if minInt is greater than 8.
201 maxInt = minInt;
202 macros.integerWidth = IntegerWidth::zeroFillTo(minInt).truncateAt(maxInt);
203 } else if (maxInt > minInt && minInt > 1) {
204 // Bug #13289: if maxInt > minInt > 1, then minInt should be 1.
205 minInt = 1;
206 macros.integerWidth = IntegerWidth::zeroFillTo(minInt).truncateAt(maxInt);
207 }
208 int engineering = maxInt < 0 ? -1 : maxInt;
209 macros.notation = ScientificNotation(
210 // Engineering interval:
211 static_cast<int8_t>(engineering),
212 // Enforce minimum integer digits (for patterns like "000.00E0"):
213 (engineering == minInt),
214 // Minimum exponent digits:
215 static_cast<digits_t>(properties.minimumExponentDigits),
216 // Exponent sign always shown:
217 properties.exponentSignAlwaysShown ? UNUM_SIGN_ALWAYS : UNUM_SIGN_AUTO);
218 // Scientific notation also involves overriding the rounding mode.
219 // TODO: Overriding here is a bit of a hack. Should this logic go earlier?
220 if (macros.precision.fType == Precision::PrecisionType::RND_FRACTION) {
221 // For the purposes of rounding, get the original min/max int/frac, since the local
222 // variables have been manipulated for display purposes.
223 int maxInt_ = properties.maximumIntegerDigits;
224 int minInt_ = properties.minimumIntegerDigits;
225 int minFrac_ = properties.minimumFractionDigits;
226 int maxFrac_ = properties.maximumFractionDigits;
227 if (minInt_ == 0 && maxFrac_ == 0) {
228 // Patterns like "#E0" and "##E0", which mean no rounding!
229 macros.precision = Precision::unlimited();
230 } else if (minInt_ == 0 && minFrac_ == 0) {
231 // Patterns like "#.##E0" (no zeros in the mantissa), which mean round to maxFrac+1
232 macros.precision = Precision::constructSignificant(1, maxFrac_ + 1);
233 } else {
234 int maxSig_ = minInt_ + maxFrac_;
235 // Bug #20058: if maxInt_ > minInt_ > 1, then minInt_ should be 1.
236 if (maxInt_ > minInt_ && minInt_ > 1) {
237 minInt_ = 1;
238 }
239 int minSig_ = minInt_ + minFrac_;
240 // To avoid regression, maxSig is not reset when minInt_ set to 1.
241 // TODO: Reset maxSig_ = 1 + minFrac_ to follow the spec.
242 macros.precision = Precision::constructSignificant(minSig_, maxSig_);
243 }
244 macros.roundingMode = roundingMode;
245 }
246 }
247
248 //////////////////////
249 // COMPACT NOTATION //
250 //////////////////////
251
252 if (!properties.compactStyle.isNull()) {
253 if (properties.compactStyle.getNoError() == UNumberCompactStyle::UNUM_LONG) {
254 macros.notation = Notation::compactLong();
255 } else {
256 macros.notation = Notation::compactShort();
257 }
258 // Do not forward the affix provider.
259 macros.affixProvider = nullptr;
260 }
261
262 /////////////////
263 // MULTIPLIERS //
264 /////////////////
265
266 macros.scale = scaleFromProperties(properties);
267
268 //////////////////////
269 // PROPERTY EXPORTS //
270 //////////////////////
271
272 if (exportedProperties != nullptr) {
273
274 exportedProperties->currency = currency;
275 exportedProperties->roundingMode = roundingMode;
276 exportedProperties->minimumIntegerDigits = minInt;
277 exportedProperties->maximumIntegerDigits = maxInt == -1 ? INT32_MAX : maxInt;
278
279 Precision rounding_;
280 if (precision.fType == Precision::PrecisionType::RND_CURRENCY) {
281 rounding_ = precision.withCurrency(currency, status);
282 } else {
283 rounding_ = precision;
284 }
285 int minFrac_ = minFrac;
286 int maxFrac_ = maxFrac;
287 int minSig_ = minSig;
288 int maxSig_ = maxSig;
289 double increment_ = 0.0;
290 if (rounding_.fType == Precision::PrecisionType::RND_FRACTION) {
291 minFrac_ = rounding_.fUnion.fracSig.fMinFrac;
292 maxFrac_ = rounding_.fUnion.fracSig.fMaxFrac;
293 } else if (rounding_.fType == Precision::PrecisionType::RND_INCREMENT
294 || rounding_.fType == Precision::PrecisionType::RND_INCREMENT_ONE
295 || rounding_.fType == Precision::PrecisionType::RND_INCREMENT_FIVE) {
296 increment_ = rounding_.fUnion.increment.fIncrement;
297 minFrac_ = rounding_.fUnion.increment.fMinFrac;
298 maxFrac_ = rounding_.fUnion.increment.fMinFrac;
299 } else if (rounding_.fType == Precision::PrecisionType::RND_SIGNIFICANT) {
300 minSig_ = rounding_.fUnion.fracSig.fMinSig;
301 maxSig_ = rounding_.fUnion.fracSig.fMaxSig;
302 }
303
304 exportedProperties->minimumFractionDigits = minFrac_;
305 exportedProperties->maximumFractionDigits = maxFrac_;
306 exportedProperties->minimumSignificantDigits = minSig_;
307 exportedProperties->maximumSignificantDigits = maxSig_;
308 exportedProperties->roundingIncrement = increment_;
309 }
310
311 return macros;
312 }
313
314
setTo(const DecimalFormatProperties & properties,UErrorCode & status)315 void PropertiesAffixPatternProvider::setTo(const DecimalFormatProperties& properties, UErrorCode& status) {
316 fBogus = false;
317
318 // There are two ways to set affixes in DecimalFormat: via the pattern string (applyPattern), and via the
319 // explicit setters (setPositivePrefix and friends). The way to resolve the settings is as follows:
320 //
321 // 1) If the explicit setting is present for the field, use it.
322 // 2) Otherwise, follows UTS 35 rules based on the pattern string.
323 //
324 // Importantly, the explicit setters affect only the one field they override. If you set the positive
325 // prefix, that should not affect the negative prefix.
326
327 // Convenience: Extract the properties into local variables.
328 // Variables are named with three chars: [p/n][p/s][o/p]
329 // [p/n] => p for positive, n for negative
330 // [p/s] => p for prefix, s for suffix
331 // [o/p] => o for escaped custom override string, p for pattern string
332 UnicodeString ppo = AffixUtils::escape(properties.positivePrefix);
333 UnicodeString pso = AffixUtils::escape(properties.positiveSuffix);
334 UnicodeString npo = AffixUtils::escape(properties.negativePrefix);
335 UnicodeString nso = AffixUtils::escape(properties.negativeSuffix);
336 const UnicodeString& ppp = properties.positivePrefixPattern;
337 const UnicodeString& psp = properties.positiveSuffixPattern;
338 const UnicodeString& npp = properties.negativePrefixPattern;
339 const UnicodeString& nsp = properties.negativeSuffixPattern;
340
341 if (!properties.positivePrefix.isBogus()) {
342 posPrefix = ppo;
343 } else if (!ppp.isBogus()) {
344 posPrefix = ppp;
345 } else {
346 // UTS 35: Default positive prefix is empty string.
347 posPrefix = u"";
348 }
349
350 if (!properties.positiveSuffix.isBogus()) {
351 posSuffix = pso;
352 } else if (!psp.isBogus()) {
353 posSuffix = psp;
354 } else {
355 // UTS 35: Default positive suffix is empty string.
356 posSuffix = u"";
357 }
358
359 if (!properties.negativePrefix.isBogus()) {
360 negPrefix = npo;
361 } else if (!npp.isBogus()) {
362 negPrefix = npp;
363 } else {
364 // UTS 35: Default negative prefix is "-" with positive prefix.
365 // Important: We prepend the "-" to the pattern, not the override!
366 negPrefix = ppp.isBogus() ? u"-" : u"-" + ppp;
367 }
368
369 if (!properties.negativeSuffix.isBogus()) {
370 negSuffix = nso;
371 } else if (!nsp.isBogus()) {
372 negSuffix = nsp;
373 } else {
374 // UTS 35: Default negative prefix is the positive prefix.
375 negSuffix = psp.isBogus() ? u"" : psp;
376 }
377
378 // For declaring if this is a currency pattern, we need to look at the
379 // original pattern, not at any user-specified overrides.
380 isCurrencyPattern = (
381 AffixUtils::hasCurrencySymbols(ppp, status) ||
382 AffixUtils::hasCurrencySymbols(psp, status) ||
383 AffixUtils::hasCurrencySymbols(npp, status) ||
384 AffixUtils::hasCurrencySymbols(nsp, status));
385 }
386
charAt(int flags,int i) const387 char16_t PropertiesAffixPatternProvider::charAt(int flags, int i) const {
388 return getStringInternal(flags).charAt(i);
389 }
390
length(int flags) const391 int PropertiesAffixPatternProvider::length(int flags) const {
392 return getStringInternal(flags).length();
393 }
394
getString(int32_t flags) const395 UnicodeString PropertiesAffixPatternProvider::getString(int32_t flags) const {
396 return getStringInternal(flags);
397 }
398
getStringInternal(int32_t flags) const399 const UnicodeString& PropertiesAffixPatternProvider::getStringInternal(int32_t flags) const {
400 bool prefix = (flags & AFFIX_PREFIX) != 0;
401 bool negative = (flags & AFFIX_NEGATIVE_SUBPATTERN) != 0;
402 if (prefix && negative) {
403 return negPrefix;
404 } else if (prefix) {
405 return posPrefix;
406 } else if (negative) {
407 return negSuffix;
408 } else {
409 return posSuffix;
410 }
411 }
412
positiveHasPlusSign() const413 bool PropertiesAffixPatternProvider::positiveHasPlusSign() const {
414 // TODO: Change the internal APIs to propagate out the error?
415 ErrorCode localStatus;
416 return AffixUtils::containsType(posPrefix, TYPE_PLUS_SIGN, localStatus) ||
417 AffixUtils::containsType(posSuffix, TYPE_PLUS_SIGN, localStatus);
418 }
419
hasNegativeSubpattern() const420 bool PropertiesAffixPatternProvider::hasNegativeSubpattern() const {
421 return (
422 (negSuffix != posSuffix) ||
423 negPrefix.tempSubString(1) != posPrefix ||
424 negPrefix.charAt(0) != u'-'
425 );
426 }
427
negativeHasMinusSign() const428 bool PropertiesAffixPatternProvider::negativeHasMinusSign() const {
429 ErrorCode localStatus;
430 return AffixUtils::containsType(negPrefix, TYPE_MINUS_SIGN, localStatus) ||
431 AffixUtils::containsType(negSuffix, TYPE_MINUS_SIGN, localStatus);
432 }
433
hasCurrencySign() const434 bool PropertiesAffixPatternProvider::hasCurrencySign() const {
435 return isCurrencyPattern;
436 }
437
containsSymbolType(AffixPatternType type,UErrorCode & status) const438 bool PropertiesAffixPatternProvider::containsSymbolType(AffixPatternType type, UErrorCode& status) const {
439 return AffixUtils::containsType(posPrefix, type, status) ||
440 AffixUtils::containsType(posSuffix, type, status) ||
441 AffixUtils::containsType(negPrefix, type, status) ||
442 AffixUtils::containsType(negSuffix, type, status);
443 }
444
hasBody() const445 bool PropertiesAffixPatternProvider::hasBody() const {
446 return true;
447 }
448
449
setTo(const CurrencyPluralInfo & cpi,const DecimalFormatProperties & properties,UErrorCode & status)450 void CurrencyPluralInfoAffixProvider::setTo(const CurrencyPluralInfo& cpi,
451 const DecimalFormatProperties& properties,
452 UErrorCode& status) {
453 // We need to use a PropertiesAffixPatternProvider, not the simpler version ParsedPatternInfo,
454 // because user-specified affix overrides still need to work.
455 fBogus = false;
456 DecimalFormatProperties pluralProperties(properties);
457 for (int32_t plural = 0; plural < StandardPlural::COUNT; plural++) {
458 const char* keyword = StandardPlural::getKeyword(static_cast<StandardPlural::Form>(plural));
459 UnicodeString patternString;
460 patternString = cpi.getCurrencyPluralPattern(keyword, patternString);
461 PatternParser::parseToExistingProperties(
462 patternString,
463 pluralProperties,
464 IGNORE_ROUNDING_NEVER,
465 status);
466 affixesByPlural[plural].setTo(pluralProperties, status);
467 }
468 }
469
charAt(int32_t flags,int32_t i) const470 char16_t CurrencyPluralInfoAffixProvider::charAt(int32_t flags, int32_t i) const {
471 int32_t pluralOrdinal = (flags & AFFIX_PLURAL_MASK);
472 return affixesByPlural[pluralOrdinal].charAt(flags, i);
473 }
474
length(int32_t flags) const475 int32_t CurrencyPluralInfoAffixProvider::length(int32_t flags) const {
476 int32_t pluralOrdinal = (flags & AFFIX_PLURAL_MASK);
477 return affixesByPlural[pluralOrdinal].length(flags);
478 }
479
getString(int32_t flags) const480 UnicodeString CurrencyPluralInfoAffixProvider::getString(int32_t flags) const {
481 int32_t pluralOrdinal = (flags & AFFIX_PLURAL_MASK);
482 return affixesByPlural[pluralOrdinal].getString(flags);
483 }
484
positiveHasPlusSign() const485 bool CurrencyPluralInfoAffixProvider::positiveHasPlusSign() const {
486 return affixesByPlural[StandardPlural::OTHER].positiveHasPlusSign();
487 }
488
hasNegativeSubpattern() const489 bool CurrencyPluralInfoAffixProvider::hasNegativeSubpattern() const {
490 return affixesByPlural[StandardPlural::OTHER].hasNegativeSubpattern();
491 }
492
negativeHasMinusSign() const493 bool CurrencyPluralInfoAffixProvider::negativeHasMinusSign() const {
494 return affixesByPlural[StandardPlural::OTHER].negativeHasMinusSign();
495 }
496
hasCurrencySign() const497 bool CurrencyPluralInfoAffixProvider::hasCurrencySign() const {
498 return affixesByPlural[StandardPlural::OTHER].hasCurrencySign();
499 }
500
containsSymbolType(AffixPatternType type,UErrorCode & status) const501 bool CurrencyPluralInfoAffixProvider::containsSymbolType(AffixPatternType type, UErrorCode& status) const {
502 return affixesByPlural[StandardPlural::OTHER].containsSymbolType(type, status);
503 }
504
hasBody() const505 bool CurrencyPluralInfoAffixProvider::hasBody() const {
506 return affixesByPlural[StandardPlural::OTHER].hasBody();
507 }
508
509
510 #endif /* #if !UCONFIG_NO_FORMATTING */
511