/src/icu/icu4c/source/i18n/number_utils.h
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 | | #ifndef __NUMBER_UTILS_H__ |
8 | | #define __NUMBER_UTILS_H__ |
9 | | |
10 | | #include "unicode/numberformatter.h" |
11 | | #include "number_types.h" |
12 | | #include "number_decimalquantity.h" |
13 | | #include "number_scientific.h" |
14 | | #include "number_patternstring.h" |
15 | | #include "number_modifiers.h" |
16 | | #include "number_multiplier.h" |
17 | | #include "number_roundingutils.h" |
18 | | #include "decNumber.h" |
19 | | #include "charstr.h" |
20 | | #include "formatted_string_builder.h" |
21 | | |
22 | | U_NAMESPACE_BEGIN |
23 | | |
24 | | namespace number::impl { |
25 | | |
26 | | enum CldrPatternStyle { |
27 | | CLDR_PATTERN_STYLE_DECIMAL, |
28 | | CLDR_PATTERN_STYLE_CURRENCY, |
29 | | CLDR_PATTERN_STYLE_ACCOUNTING, |
30 | | CLDR_PATTERN_STYLE_PERCENT, |
31 | | CLDR_PATTERN_STYLE_SCIENTIFIC, |
32 | | CLDR_PATTERN_STYLE_COUNT, |
33 | | }; |
34 | | |
35 | | // Namespace for naked functions |
36 | | namespace utils { |
37 | | |
38 | | inline int32_t insertDigitFromSymbols(FormattedStringBuilder& output, int32_t index, int8_t digit, |
39 | | const DecimalFormatSymbols& symbols, Field field, |
40 | 1.48G | UErrorCode& status) { |
41 | 1.48G | if (symbols.getCodePointZero() != -1) { |
42 | 1.48G | return output.insertCodePoint(index, symbols.getCodePointZero() + digit, field, status); |
43 | 1.48G | } |
44 | 0 | return output.insert(index, symbols.getConstDigitSymbol(digit), field, status); |
45 | 1.48G | } |
46 | | |
47 | 12.0k | inline bool unitIsCurrency(const MeasureUnit& unit) { |
48 | 12.0k | return uprv_strcmp("currency", unit.getType()) == 0; |
49 | 12.0k | } |
50 | | |
51 | 13.6k | inline bool unitIsBaseUnit(const MeasureUnit& unit) { |
52 | 13.6k | return unit == MeasureUnit(); |
53 | 13.6k | } |
54 | | |
55 | 12.0k | inline bool unitIsPercent(const MeasureUnit& unit) { |
56 | 12.0k | return uprv_strcmp("percent", unit.getSubtype()) == 0; |
57 | 12.0k | } |
58 | | |
59 | 12.0k | inline bool unitIsPermille(const MeasureUnit& unit) { |
60 | 12.0k | return uprv_strcmp("permille", unit.getSubtype()) == 0; |
61 | 12.0k | } |
62 | | |
63 | | // NOTE: In Java, this method is in NumberFormat.java |
64 | | const char16_t* |
65 | | getPatternForStyle(const Locale& locale, const char* nsName, CldrPatternStyle style, UErrorCode& status); |
66 | | |
67 | | /** |
68 | | * Computes the plural form for this number based on the specified set of rules. |
69 | | * |
70 | | * @param rules A {@link PluralRules} object representing the set of rules. |
71 | | * @return The {@link StandardPlural} according to the PluralRules. If the plural form is not in |
72 | | * the set of standard plurals, {@link StandardPlural#OTHER} is returned instead. |
73 | | */ |
74 | | inline StandardPlural::Form getStandardPlural(const PluralRules *rules, |
75 | 4.50k | const IFixedDecimal &fdec) { |
76 | 4.50k | if (rules == nullptr) { |
77 | | // Fail gracefully if the user didn't provide a PluralRules |
78 | 0 | return StandardPlural::Form::OTHER; |
79 | 4.50k | } else { |
80 | 4.50k | UnicodeString ruleString = rules->select(fdec); |
81 | 4.50k | return StandardPlural::orOtherFromString(ruleString); |
82 | 4.50k | } |
83 | 4.50k | } |
84 | | |
85 | | /** |
86 | | * Computes the plural form after copying the number and applying rounding rules. |
87 | | */ |
88 | | inline StandardPlural::Form getPluralSafe( |
89 | | const RoundingImpl& rounder, |
90 | | const PluralRules* rules, |
91 | | const DecimalQuantity& dq, |
92 | 1.63k | UErrorCode& status) { |
93 | | // TODO(ICU-20500): Avoid the copy? |
94 | 1.63k | DecimalQuantity copy(dq); |
95 | 1.63k | rounder.apply(copy, status); |
96 | 1.63k | if (U_FAILURE(status)) { |
97 | 0 | return StandardPlural::Form::OTHER; |
98 | 0 | } |
99 | 1.63k | return getStandardPlural(rules, copy); |
100 | 1.63k | } |
101 | | |
102 | | } // namespace utils |
103 | | |
104 | | } // namespace number::impl |
105 | | |
106 | | U_NAMESPACE_END |
107 | | |
108 | | #endif //__NUMBER_UTILS_H__ |
109 | | |
110 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |