Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/number_longnames.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/simpleformatter.h"
9
#include "unicode/ures.h"
10
#include "ureslocs.h"
11
#include "charstr.h"
12
#include "uresimp.h"
13
#include "number_longnames.h"
14
#include "number_microprops.h"
15
#include <algorithm>
16
#include "cstring.h"
17
18
using namespace icu;
19
using namespace icu::number;
20
using namespace icu::number::impl;
21
22
namespace {
23
24
constexpr int32_t DNAM_INDEX = StandardPlural::Form::COUNT;
25
constexpr int32_t PER_INDEX = StandardPlural::Form::COUNT + 1;
26
constexpr int32_t ARRAY_LENGTH = StandardPlural::Form::COUNT + 2;
27
28
0
static int32_t getIndex(const char* pluralKeyword, UErrorCode& status) {
29
0
    // pluralKeyword can also be "dnam" or "per"
30
0
    if (uprv_strcmp(pluralKeyword, "dnam") == 0) {
31
0
        return DNAM_INDEX;
32
0
    } else if (uprv_strcmp(pluralKeyword, "per") == 0) {
33
0
        return PER_INDEX;
34
0
    } else {
35
0
        StandardPlural::Form plural = StandardPlural::fromString(pluralKeyword, status);
36
0
        return plural;
37
0
    }
38
0
}
39
40
static UnicodeString getWithPlural(
41
        const UnicodeString* strings,
42
        int32_t plural,
43
0
        UErrorCode& status) {
44
0
    UnicodeString result = strings[plural];
45
0
    if (result.isBogus()) {
46
0
        result = strings[StandardPlural::Form::OTHER];
47
0
    }
48
0
    if (result.isBogus()) {
49
0
        // There should always be data in the "other" plural variant.
50
0
        status = U_INTERNAL_PROGRAM_ERROR;
51
0
    }
52
0
    return result;
53
0
}
54
55
56
//////////////////////////
57
/// BEGIN DATA LOADING ///
58
//////////////////////////
59
60
class PluralTableSink : public ResourceSink {
61
  public:
62
0
    explicit PluralTableSink(UnicodeString *outArray) : outArray(outArray) {
63
0
        // Initialize the array to bogus strings.
64
0
        for (int32_t i = 0; i < ARRAY_LENGTH; i++) {
65
0
            outArray[i].setToBogus();
66
0
        }
67
0
    }
68
69
0
    void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &status) U_OVERRIDE {
70
0
        ResourceTable pluralsTable = value.getTable(status);
71
0
        if (U_FAILURE(status)) { return; }
72
0
        for (int32_t i = 0; pluralsTable.getKeyAndValue(i, key, value); ++i) {
73
0
            int32_t index = getIndex(key, status);
74
0
            if (U_FAILURE(status)) { return; }
75
0
            if (!outArray[index].isBogus()) {
76
0
                continue;
77
0
            }
78
0
            outArray[index] = value.getUnicodeString(status);
79
0
            if (U_FAILURE(status)) { return; }
80
0
        }
81
0
    }
82
83
  private:
84
    UnicodeString *outArray;
85
};
86
87
// NOTE: outArray MUST have room for all StandardPlural values.  No bounds checking is performed.
88
89
void getMeasureData(const Locale &locale, const MeasureUnit &unit, const UNumberUnitWidth &width,
90
0
                    UnicodeString *outArray, UErrorCode &status) {
91
0
    PluralTableSink sink(outArray);
92
0
    LocalUResourceBundlePointer unitsBundle(ures_open(U_ICUDATA_UNIT, locale.getName(), &status));
93
0
    if (U_FAILURE(status)) { return; }
94
0
    CharString key;
95
0
    key.append("units", status);
96
0
    if (width == UNUM_UNIT_WIDTH_NARROW) {
97
0
        key.append("Narrow", status);
98
0
    } else if (width == UNUM_UNIT_WIDTH_SHORT) {
99
0
        key.append("Short", status);
100
0
    }
101
0
    key.append("/", status);
102
0
    key.append(unit.getType(), status);
103
0
    key.append("/", status);
104
0
    key.append(unit.getSubtype(), status);
105
0
    ures_getAllItemsWithFallback(unitsBundle.getAlias(), key.data(), sink, status);
106
0
}
107
108
void getCurrencyLongNameData(const Locale &locale, const CurrencyUnit &currency, UnicodeString *outArray,
109
0
                             UErrorCode &status) {
110
0
    // In ICU4J, this method gets a CurrencyData from CurrencyData.provider.
111
0
    // TODO(ICU4J): Implement this without going through CurrencyData, like in ICU4C?
112
0
    PluralTableSink sink(outArray);
113
0
    LocalUResourceBundlePointer unitsBundle(ures_open(U_ICUDATA_CURR, locale.getName(), &status));
114
0
    if (U_FAILURE(status)) { return; }
115
0
    ures_getAllItemsWithFallback(unitsBundle.getAlias(), "CurrencyUnitPatterns", sink, status);
116
0
    if (U_FAILURE(status)) { return; }
117
0
    for (int32_t i = 0; i < StandardPlural::Form::COUNT; i++) {
118
0
        UnicodeString &pattern = outArray[i];
119
0
        if (pattern.isBogus()) {
120
0
            continue;
121
0
        }
122
0
        UBool isChoiceFormat = FALSE;
123
0
        int32_t longNameLen = 0;
124
0
        const char16_t *longName = ucurr_getPluralName(
125
0
                currency.getISOCurrency(),
126
0
                locale.getName(),
127
0
                &isChoiceFormat,
128
0
                StandardPlural::getKeyword(static_cast<StandardPlural::Form>(i)),
129
0
                &longNameLen,
130
0
                &status);
131
0
        // Example pattern from data: "{0} {1}"
132
0
        // Example output after find-and-replace: "{0} US dollars"
133
0
        pattern.findAndReplace(UnicodeString(u"{1}"), UnicodeString(longName, longNameLen));
134
0
    }
135
0
}
136
137
0
UnicodeString getPerUnitFormat(const Locale& locale, const UNumberUnitWidth &width, UErrorCode& status) {
138
0
    LocalUResourceBundlePointer unitsBundle(ures_open(U_ICUDATA_UNIT, locale.getName(), &status));
139
0
    if (U_FAILURE(status)) { return {}; }
140
0
    CharString key;
141
0
    key.append("units", status);
142
0
    if (width == UNUM_UNIT_WIDTH_NARROW) {
143
0
        key.append("Narrow", status);
144
0
    } else if (width == UNUM_UNIT_WIDTH_SHORT) {
145
0
        key.append("Short", status);
146
0
    }
147
0
    key.append("/compound/per", status);
148
0
    int32_t len = 0;
149
0
    const UChar* ptr = ures_getStringByKeyWithFallback(unitsBundle.getAlias(), key.data(), &len, &status);
150
0
    return UnicodeString(ptr, len);
151
0
}
152
153
////////////////////////
154
/// END DATA LOADING ///
155
////////////////////////
156
157
} // namespace
158
159
LongNameHandler
160
LongNameHandler::forMeasureUnit(const Locale &loc, const MeasureUnit &unitRef, const MeasureUnit &perUnit,
161
                                const UNumberUnitWidth &width, const PluralRules *rules,
162
0
                                const MicroPropsGenerator *parent, UErrorCode &status) {
163
0
    MeasureUnit unit = unitRef;
164
0
    if (uprv_strcmp(perUnit.getType(), "none") != 0) {
165
0
        // Compound unit: first try to simplify (e.g., meters per second is its own unit).
166
0
        bool isResolved = false;
167
0
        MeasureUnit resolved = MeasureUnit::resolveUnitPerUnit(unit, perUnit, &isResolved);
168
0
        if (isResolved) {
169
0
            unit = resolved;
170
0
        } else {
171
0
            // No simplified form is available.
172
0
            return forCompoundUnit(loc, unit, perUnit, width, rules, parent, status);
173
0
        }
174
0
    }
175
0
176
0
    LongNameHandler result(rules, parent);
177
0
    UnicodeString simpleFormats[ARRAY_LENGTH];
178
0
    getMeasureData(loc, unit, width, simpleFormats, status);
179
0
    if (U_FAILURE(status)) { return result; }
180
0
    // TODO: What field to use for units?
181
0
    simpleFormatsToModifiers(simpleFormats, UNUM_FIELD_COUNT, result.fModifiers, status);
182
0
    return result;
183
0
}
184
185
LongNameHandler
186
LongNameHandler::forCompoundUnit(const Locale &loc, const MeasureUnit &unit, const MeasureUnit &perUnit,
187
                                 const UNumberUnitWidth &width, const PluralRules *rules,
188
0
                                 const MicroPropsGenerator *parent, UErrorCode &status) {
189
0
    LongNameHandler result(rules, parent);
190
0
    UnicodeString primaryData[ARRAY_LENGTH];
191
0
    getMeasureData(loc, unit, width, primaryData, status);
192
0
    if (U_FAILURE(status)) { return result; }
193
0
    UnicodeString secondaryData[ARRAY_LENGTH];
194
0
    getMeasureData(loc, perUnit, width, secondaryData, status);
195
0
    if (U_FAILURE(status)) { return result; }
196
0
197
0
    UnicodeString perUnitFormat;
198
0
    if (!secondaryData[PER_INDEX].isBogus()) {
199
0
        perUnitFormat = secondaryData[PER_INDEX];
200
0
    } else {
201
0
        UnicodeString rawPerUnitFormat = getPerUnitFormat(loc, width, status);
202
0
        if (U_FAILURE(status)) { return result; }
203
0
        // rawPerUnitFormat is something like "{0}/{1}"; we need to substitute in the secondary unit.
204
0
        SimpleFormatter compiled(rawPerUnitFormat, 2, 2, status);
205
0
        if (U_FAILURE(status)) { return result; }
206
0
        UnicodeString secondaryFormat = getWithPlural(secondaryData, StandardPlural::Form::ONE, status);
207
0
        if (U_FAILURE(status)) { return result; }
208
0
        SimpleFormatter secondaryCompiled(secondaryFormat, 1, 1, status);
209
0
        if (U_FAILURE(status)) { return result; }
210
0
        UnicodeString secondaryString = secondaryCompiled.getTextWithNoArguments().trim();
211
0
        // TODO: Why does UnicodeString need to be explicit in the following line?
212
0
        compiled.format(UnicodeString(u"{0}"), secondaryString, perUnitFormat, status);
213
0
        if (U_FAILURE(status)) { return result; }
214
0
    }
215
0
    // TODO: What field to use for units?
216
0
    multiSimpleFormatsToModifiers(primaryData, perUnitFormat, UNUM_FIELD_COUNT, result.fModifiers, status);
217
0
    return result;
218
0
}
219
220
LongNameHandler LongNameHandler::forCurrencyLongNames(const Locale &loc, const CurrencyUnit &currency,
221
                                                      const PluralRules *rules,
222
                                                      const MicroPropsGenerator *parent,
223
0
                                                      UErrorCode &status) {
224
0
    LongNameHandler result(rules, parent);
225
0
    UnicodeString simpleFormats[ARRAY_LENGTH];
226
0
    getCurrencyLongNameData(loc, currency, simpleFormats, status);
227
0
    if (U_FAILURE(status)) { return result; }
228
0
    simpleFormatsToModifiers(simpleFormats, UNUM_CURRENCY_FIELD, result.fModifiers, status);
229
0
    return result;
230
0
}
231
232
void LongNameHandler::simpleFormatsToModifiers(const UnicodeString *simpleFormats, Field field,
233
0
                                               SimpleModifier *output, UErrorCode &status) {
234
0
    for (int32_t i = 0; i < StandardPlural::Form::COUNT; i++) {
235
0
        UnicodeString simpleFormat = getWithPlural(simpleFormats, i, status);
236
0
        if (U_FAILURE(status)) { return; }
237
0
        SimpleFormatter compiledFormatter(simpleFormat, 0, 1, status);
238
0
        if (U_FAILURE(status)) { return; }
239
0
        output[i] = SimpleModifier(compiledFormatter, field, false);
240
0
    }
241
0
}
242
243
void LongNameHandler::multiSimpleFormatsToModifiers(const UnicodeString *leadFormats, UnicodeString trailFormat,
244
0
                                                    Field field, SimpleModifier *output, UErrorCode &status) {
245
0
    SimpleFormatter trailCompiled(trailFormat, 1, 1, status);
246
0
    if (U_FAILURE(status)) { return; }
247
0
    for (int32_t i = 0; i < StandardPlural::Form::COUNT; i++) {
248
0
        UnicodeString leadFormat = getWithPlural(leadFormats, i, status);
249
0
        if (U_FAILURE(status)) { return; }
250
0
        UnicodeString compoundFormat;
251
0
        trailCompiled.format(leadFormat, compoundFormat, status);
252
0
        if (U_FAILURE(status)) { return; }
253
0
        SimpleFormatter compoundCompiled(compoundFormat, 0, 1, status);
254
0
        if (U_FAILURE(status)) { return; }
255
0
        output[i] = SimpleModifier(compoundCompiled, field, false);
256
0
    }
257
0
}
258
259
void LongNameHandler::processQuantity(DecimalQuantity &quantity, MicroProps &micros,
260
0
                                      UErrorCode &status) const {
261
0
    parent->processQuantity(quantity, micros, status);
262
0
    // TODO: Avoid the copy here?
263
0
    DecimalQuantity copy(quantity);
264
0
    micros.rounder.apply(copy, status);
265
0
    micros.modOuter = &fModifiers[utils::getStandardPlural(rules, copy)];
266
0
}
267
268
#endif /* #if !UCONFIG_NO_FORMATTING */