/src/mozilla-central/intl/icu/source/i18n/number_grouping.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 "unicode/numberformatter.h" |
9 | | #include "number_patternstring.h" |
10 | | #include "uresimp.h" |
11 | | |
12 | | using namespace icu; |
13 | | using namespace icu::number; |
14 | | using namespace icu::number::impl; |
15 | | |
16 | | namespace { |
17 | | |
18 | 0 | int16_t getMinGroupingForLocale(const Locale& locale) { |
19 | 0 | // TODO: Cache this? |
20 | 0 | UErrorCode localStatus = U_ZERO_ERROR; |
21 | 0 | LocalUResourceBundlePointer bundle(ures_open(NULL, locale.getName(), &localStatus)); |
22 | 0 | int32_t resultLen = 0; |
23 | 0 | const char16_t* result = ures_getStringByKeyWithFallback( |
24 | 0 | bundle.getAlias(), |
25 | 0 | "NumberElements/minimumGroupingDigits", |
26 | 0 | &resultLen, |
27 | 0 | &localStatus); |
28 | 0 | // TODO: Is it safe to assume resultLen == 1? Would locales set minGrouping >= 10? |
29 | 0 | if (U_FAILURE(localStatus) || resultLen != 1) { |
30 | 0 | return 1; |
31 | 0 | } |
32 | 0 | return result[0] - u'0'; |
33 | 0 | } |
34 | | |
35 | | } |
36 | | |
37 | | Grouper Grouper::forStrategy(UGroupingStrategy grouping) { |
38 | | switch (grouping) { |
39 | | case UNUM_GROUPING_OFF: |
40 | | return {-1, -1, -2, grouping}; |
41 | | case UNUM_GROUPING_AUTO: |
42 | | return {-2, -2, -2, grouping}; |
43 | | case UNUM_GROUPING_MIN2: |
44 | | return {-2, -2, -3, grouping}; |
45 | | case UNUM_GROUPING_ON_ALIGNED: |
46 | | return {-4, -4, 1, grouping}; |
47 | | case UNUM_GROUPING_THOUSANDS: |
48 | | return {3, 3, 1, grouping}; |
49 | | default: |
50 | | U_ASSERT(FALSE); |
51 | | return {}; // return a value: silence compiler warning |
52 | | } |
53 | | } |
54 | | |
55 | 0 | Grouper Grouper::forProperties(const DecimalFormatProperties& properties) { |
56 | 0 | if (!properties.groupingUsed) { |
57 | 0 | return forStrategy(UNUM_GROUPING_OFF); |
58 | 0 | } |
59 | 0 | auto grouping1 = static_cast<int16_t>(properties.groupingSize); |
60 | 0 | auto grouping2 = static_cast<int16_t>(properties.secondaryGroupingSize); |
61 | 0 | auto minGrouping = static_cast<int16_t>(properties.minimumGroupingDigits); |
62 | 0 | grouping1 = grouping1 > 0 ? grouping1 : grouping2 > 0 ? grouping2 : grouping1; |
63 | 0 | grouping2 = grouping2 > 0 ? grouping2 : grouping1; |
64 | 0 | return {grouping1, grouping2, minGrouping, UNUM_GROUPING_COUNT}; |
65 | 0 | } |
66 | | |
67 | 0 | void Grouper::setLocaleData(const impl::ParsedPatternInfo &patternInfo, const Locale& locale) { |
68 | 0 | if (fGrouping1 != -2 && fGrouping2 != -4) { |
69 | 0 | return; |
70 | 0 | } |
71 | 0 | auto grouping1 = static_cast<int16_t> (patternInfo.positive.groupingSizes & 0xffff); |
72 | 0 | auto grouping2 = static_cast<int16_t> ((patternInfo.positive.groupingSizes >> 16) & 0xffff); |
73 | 0 | auto grouping3 = static_cast<int16_t> ((patternInfo.positive.groupingSizes >> 32) & 0xffff); |
74 | 0 | if (grouping2 == -1) { |
75 | 0 | grouping1 = fGrouping1 == -4 ? (short) 3 : (short) -1; |
76 | 0 | } |
77 | 0 | if (grouping3 == -1) { |
78 | 0 | grouping2 = grouping1; |
79 | 0 | } |
80 | 0 | if (fMinGrouping == -2) { |
81 | 0 | fMinGrouping = getMinGroupingForLocale(locale); |
82 | 0 | } else if (fMinGrouping == -3) { |
83 | 0 | fMinGrouping = uprv_max(2, getMinGroupingForLocale(locale)); |
84 | 0 | } else { |
85 | 0 | // leave fMinGrouping alone |
86 | 0 | } |
87 | 0 | fGrouping1 = grouping1; |
88 | 0 | fGrouping2 = grouping2; |
89 | 0 | } |
90 | | |
91 | 0 | bool Grouper::groupAtPosition(int32_t position, const impl::DecimalQuantity &value) const { |
92 | 0 | U_ASSERT(fGrouping1 > -2); |
93 | 0 | if (fGrouping1 == -1 || fGrouping1 == 0) { |
94 | 0 | // Either -1 or 0 means "no grouping" |
95 | 0 | return false; |
96 | 0 | } |
97 | 0 | position -= fGrouping1; |
98 | 0 | return position >= 0 && (position % fGrouping2) == 0 |
99 | 0 | && value.getUpperDisplayMagnitude() - fGrouping1 + 1 >= fMinGrouping; |
100 | 0 | } |
101 | | |
102 | 0 | int16_t Grouper::getPrimary() const { |
103 | 0 | return fGrouping1; |
104 | 0 | } |
105 | | |
106 | 0 | int16_t Grouper::getSecondary() const { |
107 | 0 | return fGrouping2; |
108 | 0 | } |
109 | | |
110 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |