Coverage Report

Created: 2026-06-07 06:21

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