Coverage Report

Created: 2025-06-13 06:38

/src/icu/icu4c/source/i18n/number_compact.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/ustring.h"
9
#include "unicode/ures.h"
10
#include "cstring.h"
11
#include "charstr.h"
12
#include "resource.h"
13
#include "number_compact.h"
14
#include "number_microprops.h"
15
#include "uresimp.h"
16
17
using namespace icu;
18
using namespace icu::number;
19
using namespace icu::number::impl;
20
21
namespace {
22
23
// A dummy object used when a "0" compact decimal entry is encountered. This is necessary
24
// in order to prevent falling back to root. Object equality ("==") is intended.
25
const char16_t *USE_FALLBACK = u"<USE FALLBACK>";
26
27
/** Produces a string like "NumberElements/latn/patternsShort/decimalFormat". */
28
void getResourceBundleKey(const char *nsName, CompactStyle compactStyle, CompactType compactType,
29
3.40k
                                 CharString &sb, UErrorCode &status) {
30
3.40k
    sb.clear();
31
3.40k
    sb.append("NumberElements/", status);
32
3.40k
    sb.append(nsName, status);
33
3.40k
    sb.append(compactStyle == CompactStyle::UNUM_SHORT ? "/patternsShort" : "/patternsLong", status);
34
3.40k
    sb.append(compactType == CompactType::TYPE_DECIMAL ? "/decimalFormat" : "/currencyFormat", status);
35
3.40k
}
36
37
167k
int32_t getIndex(int32_t magnitude, StandardPlural::Form plural) {
38
167k
    return magnitude * StandardPlural::COUNT + plural;
39
167k
}
40
41
40.7k
int32_t countZeros(const char16_t *patternString, int32_t patternLength) {
42
    // NOTE: This strategy for computing the number of zeros is a hack for efficiency.
43
    // It could break if there are any 0s that aren't part of the main pattern.
44
40.7k
    int32_t numZeros = 0;
45
125k
    for (int32_t i = 0; i < patternLength; i++) {
46
124k
        if (patternString[i] == u'0') {
47
80.9k
            numZeros++;
48
80.9k
        } else if (numZeros > 0) {
49
39.7k
            break; // zeros should always be contiguous
50
39.7k
        }
51
124k
    }
52
40.7k
    return numZeros;
53
40.7k
}
54
55
} // namespace
56
57
// NOTE: patterns and multipliers both get zero-initialized.
58
3.37k
CompactData::CompactData() : patterns(), multipliers(), largestMagnitude(0), isEmpty(true) {
59
3.37k
}
60
61
void CompactData::populate(const Locale &locale, const char *nsName, CompactStyle compactStyle,
62
3.37k
                           CompactType compactType, UErrorCode &status) {
63
3.37k
    CompactDataSink sink(*this);
64
3.37k
    LocalUResourceBundlePointer rb(ures_open(nullptr, locale.getName(), &status));
65
3.37k
    if (U_FAILURE(status)) { return; }
66
67
3.37k
    bool nsIsLatn = strcmp(nsName, "latn") == 0;
68
3.37k
    bool compactIsShort = compactStyle == CompactStyle::UNUM_SHORT;
69
70
    // Fall back to latn numbering system and/or short compact style.
71
3.37k
    CharString resourceKey;
72
3.37k
    getResourceBundleKey(nsName, compactStyle, compactType, resourceKey, status);
73
3.37k
    UErrorCode localStatus = U_ZERO_ERROR;
74
3.37k
    ures_getAllItemsWithFallback(rb.getAlias(), resourceKey.data(), sink, localStatus);
75
3.37k
    if (isEmpty && !nsIsLatn) {
76
22
        getResourceBundleKey("latn", compactStyle, compactType, resourceKey, status);
77
22
        localStatus = U_ZERO_ERROR;
78
22
        ures_getAllItemsWithFallback(rb.getAlias(), resourceKey.data(), sink, localStatus);
79
22
    }
80
3.37k
    if (isEmpty && !compactIsShort) {
81
10
        getResourceBundleKey(nsName, CompactStyle::UNUM_SHORT, compactType, resourceKey, status);
82
10
        localStatus = U_ZERO_ERROR;
83
10
        ures_getAllItemsWithFallback(rb.getAlias(), resourceKey.data(), sink, localStatus);
84
10
    }
85
3.37k
    if (isEmpty && !nsIsLatn && !compactIsShort) {
86
0
        getResourceBundleKey("latn", CompactStyle::UNUM_SHORT, compactType, resourceKey, status);
87
0
        localStatus = U_ZERO_ERROR;
88
0
        ures_getAllItemsWithFallback(rb.getAlias(), resourceKey.data(), sink, localStatus);
89
0
    }
90
91
    // The last fallback should be guaranteed to return data.
92
3.37k
    if (isEmpty) {
93
0
        status = U_INTERNAL_PROGRAM_ERROR;
94
0
    }
95
3.37k
}
96
97
3.41k
int32_t CompactData::getMultiplier(int32_t magnitude) const {
98
3.41k
    if (magnitude < 0) {
99
771
        return 0;
100
771
    }
101
2.64k
    if (magnitude > largestMagnitude) {
102
2.00k
        magnitude = largestMagnitude;
103
2.00k
    }
104
2.64k
    return multipliers[magnitude];
105
3.41k
}
106
107
const char16_t *CompactData::getPattern(
108
        int32_t magnitude,
109
        const PluralRules *rules,
110
3.37k
        const DecimalQuantity &dq) const {
111
3.37k
    if (magnitude < 0) {
112
694
        return nullptr;
113
694
    }
114
2.68k
    if (magnitude > largestMagnitude) {
115
1.98k
        magnitude = largestMagnitude;
116
1.98k
    }
117
2.68k
    const char16_t *patternString = nullptr;
118
2.68k
    if (dq.hasIntegerValue()) {
119
2.23k
        int64_t i = dq.toLong(true);
120
2.23k
        if (i == 0) {
121
808
            patternString = patterns[getIndex(magnitude, StandardPlural::Form::EQ_0)];
122
1.42k
        } else if (i == 1) {
123
46
            patternString = patterns[getIndex(magnitude, StandardPlural::Form::EQ_1)];
124
46
        }
125
2.23k
        if (patternString != nullptr) {
126
15
            return patternString;
127
15
        }
128
2.23k
    }
129
2.66k
    StandardPlural::Form plural = utils::getStandardPlural(rules, dq);
130
2.66k
    patternString = patterns[getIndex(magnitude, plural)];
131
2.66k
    if (patternString == nullptr && plural != StandardPlural::OTHER) {
132
        // Fall back to "other" plural variant
133
431
        patternString = patterns[getIndex(magnitude, StandardPlural::OTHER)];
134
431
    }
135
2.66k
    if (patternString == USE_FALLBACK) { // == is intended
136
        // Return null if USE_FALLBACK is present
137
4
        patternString = nullptr;
138
4
    }
139
2.66k
    return patternString;
140
2.68k
}
141
142
0
void CompactData::getUniquePatterns(UVector &output, UErrorCode &status) const {
143
0
    U_ASSERT(output.isEmpty());
144
    // NOTE: In C++, this is done more manually with a UVector.
145
    // In Java, we can take advantage of JDK HashSet.
146
0
    for (const auto* pattern : patterns) {
147
0
        if (pattern == nullptr || pattern == USE_FALLBACK) {
148
0
            continue;
149
0
        }
150
151
        // Insert pattern into the UVector if the UVector does not already contain the pattern.
152
        // Search the UVector from the end since identical patterns are likely to be adjacent.
153
0
        for (int32_t i = output.size() - 1; i >= 0; i--) {
154
0
            if (u_strcmp(pattern, static_cast<const char16_t *>(output[i])) == 0) {
155
0
                goto continue_outer;
156
0
            }
157
0
        }
158
159
        // The string was not found; add it to the UVector.
160
        // Note: must cast off const from pattern to store it in a UVector, which expects (void *)
161
0
        output.addElement(const_cast<char16_t *>(pattern), status);
162
163
0
        continue_outer:
164
0
        continue;
165
0
    }
166
0
}
167
168
void CompactData::CompactDataSink::put(const char *key, ResourceValue &value, UBool /*noFallback*/,
169
5.72k
                                       UErrorCode &status) {
170
    // traverse into the table of powers of ten
171
5.72k
    ResourceTable powersOfTenTable = value.getTable(status);
172
5.72k
    if (U_FAILURE(status)) { return; }
173
71.3k
    for (int i3 = 0; powersOfTenTable.getKeyAndValue(i3, key, value); ++i3) {
174
175
        // Assumes that the keys are always of the form "10000" where the magnitude is the
176
        // length of the key minus one.  We only support magnitudes less than COMPACT_MAX_DIGITS;
177
        // ignore entries that have greater magnitude.
178
65.6k
        auto magnitude = static_cast<int8_t> (strlen(key) - 1);
179
65.6k
        U_ASSERT(magnitude < COMPACT_MAX_DIGITS); // debug assert
180
65.6k
        if (magnitude >= COMPACT_MAX_DIGITS) { // skip in production
181
0
            continue;
182
0
        }
183
65.6k
        int8_t multiplier = data.multipliers[magnitude];
184
185
        // Iterate over the plural variants ("one", "other", etc)
186
65.6k
        ResourceTable pluralVariantsTable = value.getTable(status);
187
65.6k
        if (U_FAILURE(status)) { return; }
188
160k
        for (int i4 = 0; pluralVariantsTable.getKeyAndValue(i4, key, value); ++i4) {
189
            // Skip this magnitude/plural if we already have it from a child locale.
190
            // Note: This also skips USE_FALLBACK entries.
191
94.6k
            StandardPlural::Form plural = StandardPlural::fromString(key, status);
192
94.6k
            if (U_FAILURE(status)) { return; }
193
94.6k
            if (data.patterns[getIndex(magnitude, plural)] != nullptr) {
194
26.1k
                continue;
195
26.1k
            }
196
197
            // The value "0" means that we need to use the default pattern and not fall back
198
            // to parent locales. Example locale where this is relevant: 'it'.
199
68.5k
            int32_t patternLength;
200
68.5k
            const char16_t *patternString = value.getString(patternLength, status);
201
68.5k
            if (U_FAILURE(status)) { return; }
202
68.5k
            if (u_strcmp(patternString, u"0") == 0) {
203
362
                patternString = USE_FALLBACK;
204
362
                patternLength = 0;
205
362
            }
206
207
            // Save the pattern string. We will parse it lazily.
208
68.5k
            data.patterns[getIndex(magnitude, plural)] = patternString;
209
210
            // If necessary, compute the multiplier: the difference between the magnitude
211
            // and the number of zeros in the pattern.
212
68.5k
            if (multiplier == 0) {
213
40.7k
                int32_t numZeros = countZeros(patternString, patternLength);
214
40.7k
                if (numZeros > 0) { // numZeros==0 in certain cases, like Somali "Kun"
215
40.3k
                    multiplier = static_cast<int8_t> (numZeros - magnitude - 1);
216
40.3k
                }
217
40.7k
            }
218
68.5k
        }
219
220
        // Save the multiplier.
221
65.6k
        if (data.multipliers[magnitude] == 0) {
222
40.7k
            data.multipliers[magnitude] = multiplier;
223
40.7k
            if (magnitude > data.largestMagnitude) {
224
39.4k
                data.largestMagnitude = magnitude;
225
39.4k
            }
226
40.7k
            data.isEmpty = false;
227
40.7k
        } else {
228
24.9k
            U_ASSERT(data.multipliers[magnitude] == multiplier);
229
24.9k
        }
230
65.6k
    }
231
5.72k
}
232
233
///////////////////////////////////////////////////////////
234
/// END OF CompactData.java; BEGIN CompactNotation.java ///
235
///////////////////////////////////////////////////////////
236
237
CompactHandler::CompactHandler(
238
        CompactStyle compactStyle,
239
        const Locale &locale,
240
        const char *nsName,
241
        CompactType compactType,
242
        const PluralRules *rules,
243
        MutablePatternModifier *buildReference,
244
        bool safe,
245
        const MicroPropsGenerator *parent,
246
        UErrorCode &status)
247
3.37k
        : rules(rules), parent(parent), safe(safe) {
248
3.37k
    data.populate(locale, nsName, compactStyle, compactType, status);
249
3.37k
    if (safe) {
250
        // Safe code path
251
0
        precomputeAllModifiers(*buildReference, status);
252
3.37k
    } else {
253
        // Unsafe code path
254
        // Store the MutablePatternModifier reference.
255
3.37k
        unsafePatternModifier = buildReference;
256
3.37k
    }
257
3.37k
}
258
259
3.37k
CompactHandler::~CompactHandler() {
260
3.37k
    for (int32_t i = 0; i < precomputedModsLength; i++) {
261
0
        delete precomputedMods[i].mod;
262
0
    }
263
3.37k
}
264
265
0
void CompactHandler::precomputeAllModifiers(MutablePatternModifier &buildReference, UErrorCode &status) {
266
0
    if (U_FAILURE(status)) { return; }
267
268
    // Initial capacity of 12 for 0K, 00K, 000K, ...M, ...B, and ...T
269
0
    UVector allPatterns(12, status);
270
0
    if (U_FAILURE(status)) { return; }
271
0
    data.getUniquePatterns(allPatterns, status);
272
0
    if (U_FAILURE(status)) { return; }
273
274
    // C++ only: ensure that precomputedMods has room.
275
0
    precomputedModsLength = allPatterns.size();
276
0
    if (precomputedMods.getCapacity() < precomputedModsLength) {
277
0
        precomputedMods.resize(allPatterns.size(), status);
278
0
        if (U_FAILURE(status)) { return; }
279
0
    }
280
281
0
    for (int32_t i = 0; i < precomputedModsLength; i++) {
282
0
        const auto* patternString = static_cast<const char16_t*>(allPatterns[i]);
283
0
        UnicodeString hello(patternString);
284
0
        CompactModInfo &info = precomputedMods[i];
285
0
        ParsedPatternInfo patternInfo;
286
0
        PatternParser::parseToPatternInfo(UnicodeString(patternString), patternInfo, status);
287
0
        if (U_FAILURE(status)) { return; }
288
0
        buildReference.setPatternInfo(&patternInfo, {UFIELD_CATEGORY_NUMBER, UNUM_COMPACT_FIELD});
289
0
        info.mod = buildReference.createImmutable(status);
290
0
        if (U_FAILURE(status)) { return; }
291
0
        info.patternString = patternString;
292
0
    }
293
0
}
294
295
void CompactHandler::processQuantity(DecimalQuantity &quantity, MicroProps &micros,
296
3.37k
                                     UErrorCode &status) const {
297
3.37k
    parent->processQuantity(quantity, micros, status);
298
3.37k
    if (U_FAILURE(status)) { return; }
299
300
    // Treat zero, NaN, and infinity as if they had magnitude 0
301
3.37k
    int32_t magnitude;
302
3.37k
    int32_t multiplier = 0;
303
3.37k
    if (quantity.isZeroish()) {
304
72
        magnitude = 0;
305
72
        micros.rounder.apply(quantity, status);
306
3.30k
    } else {
307
        // TODO: Revisit chooseMultiplierAndApply
308
3.30k
        multiplier = micros.rounder.chooseMultiplierAndApply(quantity, data, status);
309
3.30k
        magnitude = quantity.isZeroish() ? 0 : quantity.getMagnitude();
310
3.30k
        magnitude -= multiplier;
311
3.30k
    }
312
313
3.37k
    const char16_t *patternString = data.getPattern(magnitude, rules, quantity);
314
3.37k
    if (patternString == nullptr) {
315
        // Use the default (non-compact) modifier.
316
        // No need to take any action.
317
2.45k
    } else if (safe) {
318
        // Safe code path.
319
        // Java uses a hash set here for O(1) lookup.  C++ uses a linear search.
320
        // TODO: Benchmark this and maybe change to a binary search or hash table.
321
0
        int32_t i = 0;
322
0
        for (; i < precomputedModsLength; i++) {
323
0
            const CompactModInfo &info = precomputedMods[i];
324
0
            if (u_strcmp(patternString, info.patternString) == 0) {
325
0
                info.mod->applyToMicros(micros, quantity, status);
326
0
                break;
327
0
            }
328
0
        }
329
        // It should be guaranteed that we found the entry.
330
0
        U_ASSERT(i < precomputedModsLength);
331
2.45k
    } else {
332
        // Unsafe code path.
333
        // Overwrite the PatternInfo in the existing modMiddle.
334
        // C++ Note: Use unsafePatternInfo for proper lifecycle.
335
2.45k
        ParsedPatternInfo &patternInfo = const_cast<CompactHandler *>(this)->unsafePatternInfo;
336
2.45k
        PatternParser::parseToPatternInfo(UnicodeString(patternString), patternInfo, status);
337
2.45k
        unsafePatternModifier->setPatternInfo(
338
2.45k
            &unsafePatternInfo,
339
2.45k
            {UFIELD_CATEGORY_NUMBER, UNUM_COMPACT_FIELD});
340
2.45k
        unsafePatternModifier->setNumberProperties(quantity.signum(), StandardPlural::Form::COUNT);
341
2.45k
        micros.modMiddle = unsafePatternModifier;
342
2.45k
    }
343
344
    // Change the exponent only after we select appropriate plural form
345
    // for formatting purposes so that we preserve expected formatted
346
    // string behavior.
347
3.37k
    quantity.adjustExponent(-1 * multiplier);
348
349
    // We already performed rounding. Do not perform it again.
350
3.37k
    micros.rounder = RoundingImpl::passThrough();
351
3.37k
}
352
353
#endif /* #if !UCONFIG_NO_FORMATTING */