Coverage Report

Created: 2023-02-22 06:51

/src/icu/source/i18n/pluralranges.cpp
Line
Count
Source (jump to first uncovered line)
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 "unicode/numberrangeformatter.h"
13
#include "pluralranges.h"
14
#include "uresimp.h"
15
#include "charstr.h"
16
#include "uassert.h"
17
#include "util.h"
18
#include "numrange_impl.h"
19
20
U_NAMESPACE_BEGIN
21
22
23
namespace {
24
25
class PluralRangesDataSink : public ResourceSink {
26
  public:
27
0
    PluralRangesDataSink(StandardPluralRanges& output) : fOutput(output) {}
28
29
0
    void put(const char* /*key*/, ResourceValue& value, UBool /*noFallback*/, UErrorCode& status) U_OVERRIDE {
30
0
        ResourceArray entriesArray = value.getArray(status);
31
0
        if (U_FAILURE(status)) { return; }
32
0
        fOutput.setCapacity(entriesArray.getSize(), status);
33
0
        if (U_FAILURE(status)) { return; }
34
0
        for (int i = 0; entriesArray.getValue(i, value); i++) {
35
0
            ResourceArray pluralFormsArray = value.getArray(status);
36
0
            if (U_FAILURE(status)) { return; }
37
0
            if (pluralFormsArray.getSize() != 3) {
38
0
                status = U_RESOURCE_TYPE_MISMATCH;
39
0
                return;
40
0
            }
41
0
            pluralFormsArray.getValue(0, value);
42
0
            StandardPlural::Form first = StandardPlural::fromString(value.getUnicodeString(status), status);
43
0
            if (U_FAILURE(status)) { return; }
44
0
            pluralFormsArray.getValue(1, value);
45
0
            StandardPlural::Form second = StandardPlural::fromString(value.getUnicodeString(status), status);
46
0
            if (U_FAILURE(status)) { return; }
47
0
            pluralFormsArray.getValue(2, value);
48
0
            StandardPlural::Form result = StandardPlural::fromString(value.getUnicodeString(status), status);
49
0
            if (U_FAILURE(status)) { return; }
50
0
            fOutput.addPluralRange(first, second, result);
51
0
        }
52
0
    }
53
54
  private:
55
    StandardPluralRanges& fOutput;
56
};
57
58
0
void getPluralRangesData(const Locale& locale, StandardPluralRanges& output, UErrorCode& status) {
59
0
    LocalUResourceBundlePointer rb(ures_openDirect(nullptr, "pluralRanges", &status));
60
0
    if (U_FAILURE(status)) { return; }
61
62
0
    CharString dataPath;
63
0
    dataPath.append("locales/", -1, status);
64
0
    dataPath.append(locale.getLanguage(), -1, status);
65
0
    if (U_FAILURE(status)) { return; }
66
0
    int32_t setLen;
67
    // Not all languages are covered: fail gracefully
68
0
    UErrorCode internalStatus = U_ZERO_ERROR;
69
0
    const UChar* set = ures_getStringByKeyWithFallback(rb.getAlias(), dataPath.data(), &setLen, &internalStatus);
70
0
    if (U_FAILURE(internalStatus)) { return; }
71
72
0
    dataPath.clear();
73
0
    dataPath.append("rules/", -1, status);
74
0
    dataPath.appendInvariantChars(set, setLen, status);
75
0
    if (U_FAILURE(status)) { return; }
76
0
    PluralRangesDataSink sink(output);
77
0
    ures_getAllItemsWithFallback(rb.getAlias(), dataPath.data(), sink, status);
78
0
}
79
80
} // namespace
81
82
83
StandardPluralRanges
84
0
StandardPluralRanges::forLocale(const Locale& locale, UErrorCode& status) {
85
0
    StandardPluralRanges result;
86
0
    getPluralRangesData(locale, result, status);
87
0
    return result;
88
0
}
89
90
StandardPluralRanges
91
0
StandardPluralRanges::copy(UErrorCode& status) const {
92
0
    StandardPluralRanges result;
93
0
    if (fTriplesLen > result.fTriples.getCapacity()) {
94
0
        if (result.fTriples.resize(fTriplesLen) == nullptr) {
95
0
            status = U_MEMORY_ALLOCATION_ERROR;
96
0
            return result;
97
0
        }
98
0
    }
99
0
    uprv_memcpy(result.fTriples.getAlias(),
100
0
        fTriples.getAlias(),
101
0
        fTriplesLen * sizeof(fTriples[0]));
102
0
    result.fTriplesLen = fTriplesLen;
103
0
    return result;
104
0
}
105
106
LocalPointer<StandardPluralRanges>
107
0
StandardPluralRanges::toPointer(UErrorCode& status) && noexcept {
108
0
    return LocalPointer<StandardPluralRanges>(new StandardPluralRanges(std::move(*this)), status);
109
0
}
110
111
void StandardPluralRanges::addPluralRange(
112
        StandardPlural::Form first,
113
        StandardPlural::Form second,
114
0
        StandardPlural::Form result) {
115
0
    U_ASSERT(fTriplesLen < fTriples.getCapacity());
116
0
    fTriples[fTriplesLen] = {first, second, result};
117
0
    fTriplesLen++;
118
0
}
119
120
0
void StandardPluralRanges::setCapacity(int32_t length, UErrorCode& status) {
121
0
    if (U_FAILURE(status)) { return; }
122
0
    if (length > fTriples.getCapacity()) {
123
0
        if (fTriples.resize(length, 0) == nullptr) {
124
0
            status = U_MEMORY_ALLOCATION_ERROR;
125
0
        }
126
0
    }
127
0
}
128
129
StandardPlural::Form
130
0
StandardPluralRanges::resolve(StandardPlural::Form first, StandardPlural::Form second) const {
131
0
    for (int32_t i=0; i<fTriplesLen; i++) {
132
0
        const auto& triple = fTriples[i];
133
0
        if (triple.first == first && triple.second == second) {
134
0
            return triple.result;
135
0
        }
136
0
    }
137
    // Default fallback
138
0
    return StandardPlural::OTHER;
139
0
}
140
141
142
U_NAMESPACE_END
143
144
#endif /* #if !UCONFIG_NO_FORMATTING */