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#include "util.h"
18
19using namespace icu;
20using namespace icu::number;
21using namespace icu::number::impl;
22
23namespace {
24
25constexpr int32_t DNAM_INDEX = StandardPlural::Form::COUNT;
26constexpr int32_t PER_INDEX = StandardPlural::Form::COUNT + 1;
27constexpr int32_t ARRAY_LENGTH = StandardPlural::Form::COUNT + 2;
28
29static int32_t getIndex(const char* pluralKeyword, UErrorCode& status) {
30 // pluralKeyword can also be "dnam" or "per"
31 if (uprv_strcmp(pluralKeyword, "dnam") == 0) {
32 return DNAM_INDEX;
33 } else if (uprv_strcmp(pluralKeyword, "per") == 0) {
34 return PER_INDEX;
35 } else {
36 StandardPlural::Form plural = StandardPlural::fromString(pluralKeyword, status);
37 return plural;
38 }
39}
40
41static UnicodeString getWithPlural(
42 const UnicodeString* strings,
43 StandardPlural::Form plural,
44 UErrorCode& status) {
45 UnicodeString result = strings[plural];
46 if (result.isBogus()) {
47 result = strings[StandardPlural::Form::OTHER];
48 }
49 if (result.isBogus()) {
50 // There should always be data in the "other" plural variant.
51 status = U_INTERNAL_PROGRAM_ERROR;
52 }
53 return result;
54}
55
56
57//////////////////////////
58/// BEGIN DATA LOADING ///
59//////////////////////////
60
61class PluralTableSink : public ResourceSink {
62 public:
63 explicit PluralTableSink(UnicodeString *outArray) : outArray(outArray) {
64 // Initialize the array to bogus strings.
65 for (int32_t i = 0; i < ARRAY_LENGTH; i++) {
66 outArray[i].setToBogus();
67 }
68 }
69
70 void put(const char *key, ResourceValue &value, UBool /*noFallback*/, UErrorCode &status) U_OVERRIDE {
71 ResourceTable pluralsTable = value.getTable(status);
72 if (U_FAILURE(status)) { return; }
73 for (int32_t i = 0; pluralsTable.getKeyAndValue(i, key, value); ++i) {
74 int32_t index = getIndex(key, status);
75 if (U_FAILURE(status)) { return; }
76 if (!outArray[index].isBogus()) {
77 continue;
78 }
79 outArray[index] = value.getUnicodeString(status);
80 if (U_FAILURE(status)) { return; }
81 }
82 }
83
84 private:
85 UnicodeString *outArray;
86};
87
88// NOTE: outArray MUST have room for all StandardPlural values. No bounds checking is performed.
89
90void getMeasureData(const Locale &locale, const MeasureUnit &unit, const UNumberUnitWidth &width,
91 UnicodeString *outArray, UErrorCode &status) {
92 PluralTableSink sink(outArray);
93 LocalUResourceBundlePointer unitsBundle(ures_open(U_ICUDATA_UNIT, locale.getName(), &status));
94 if (U_FAILURE(status)) { return; }
95
96 // Map duration-year-person, duration-week-person, etc. to duration-year, duration-week, ...
97 // TODO(ICU-20400): Get duration-*-person data properly with aliases.
98 StringPiece subtypeForResource;
99 int32_t subtypeLen = static_cast<int32_t>(uprv_strlen(unit.getSubtype()));
100 if (subtypeLen > 7 && uprv_strcmp(unit.getSubtype() + subtypeLen - 7, "-person") == 0) {
101 subtypeForResource = {unit.getSubtype(), subtypeLen - 7};
102 } else {
103 subtypeForResource = unit.getSubtype();
104 }
105
106 CharString key;
107 key.append("units", status);
108 if (width == UNUM_UNIT_WIDTH_NARROW) {
109 key.append("Narrow", status);
110 } else if (width == UNUM_UNIT_WIDTH_SHORT) {
111 key.append("Short", status);
112 }
113 key.append("/", status);
114 key.append(unit.getType(), status);
115 key.append("/", status);
116 key.append(subtypeForResource, status);
117
118 UErrorCode localStatus = U_ZERO_ERROR;
119 ures_getAllItemsWithFallback(unitsBundle.getAlias(), key.data(), sink, localStatus);
120 if (width == UNUM_UNIT_WIDTH_SHORT) {
121 if (U_FAILURE(localStatus)) {
122 status = localStatus;
123 }
124 return;
125 }
126
127 // TODO(ICU-13353): The fallback to short does not work in ICU4C.
128 // Manually fall back to short (this is done automatically in Java).
129 key.clear();
130 key.append("unitsShort/", status);
131 key.append(unit.getType(), status);
132 key.append("/", status);
133 key.append(subtypeForResource, status);
134 ures_getAllItemsWithFallback(unitsBundle.getAlias(), key.data(), sink, status);
135}
136
137void getCurrencyLongNameData(const Locale &locale, const CurrencyUnit &currency, UnicodeString *outArray,
138 UErrorCode &status) {
139 // In ICU4J, this method gets a CurrencyData from CurrencyData.provider.
140 // TODO(ICU4J): Implement this without going through CurrencyData, like in ICU4C?
141 PluralTableSink sink(outArray);
142 LocalUResourceBundlePointer unitsBundle(ures_open(U_ICUDATA_CURR, locale.getName(), &status));
143 if (U_FAILURE(status)) { return; }
144 ures_getAllItemsWithFallback(unitsBundle.getAlias(), "CurrencyUnitPatterns", sink, status);
145 if (U_FAILURE(status)) { return; }
146 for (int32_t i = 0; i < StandardPlural::Form::COUNT; i++) {
147 UnicodeString &pattern = outArray[i];
148 if (pattern.isBogus()) {
149 continue;
150 }
151 int32_t longNameLen = 0;
152 const char16_t *longName = ucurr_getPluralName(
153 currency.getISOCurrency(),
154 locale.getName(),
155 nullptr /* isChoiceFormat */,
156 StandardPlural::getKeyword(static_cast<StandardPlural::Form>(i)),
157 &longNameLen,
158 &status);
159 // Example pattern from data: "{0} {1}"
160 // Example output after find-and-replace: "{0} US dollars"
161 pattern.findAndReplace(UnicodeString(u"{1}"), UnicodeString(longName, longNameLen));
162 }
163}
164
165UnicodeString getPerUnitFormat(const Locale& locale, const UNumberUnitWidth &width, UErrorCode& status) {
166 LocalUResourceBundlePointer unitsBundle(ures_open(U_ICUDATA_UNIT, locale.getName(), &status));
167 if (U_FAILURE(status)) { return {}; }
168 CharString key;
169 key.append("units", status);
170 if (width == UNUM_UNIT_WIDTH_NARROW) {
171 key.append("Narrow", status);
172 } else if (width == UNUM_UNIT_WIDTH_SHORT) {
173 key.append("Short", status);
174 }
175 key.append("/compound/per", status);
176 int32_t len = 0;
177 const UChar* ptr = ures_getStringByKeyWithFallback(unitsBundle.getAlias(), key.data(), &len, &status);
178 return UnicodeString(ptr, len);
179}
180
181////////////////////////
182/// END DATA LOADING ///
183////////////////////////
184
185} // namespace
186
187LongNameHandler*
188LongNameHandler::forMeasureUnit(const Locale &loc, const MeasureUnit &unitRef, const MeasureUnit &perUnit,
189 const UNumberUnitWidth &width, const PluralRules *rules,
190 const MicroPropsGenerator *parent, UErrorCode &status) {
191 if (uprv_strlen(unitRef.getType()) == 0 || uprv_strlen(perUnit.getType()) == 0) {
192 // TODO(ICU-20941): Unsanctioned unit. Not yet fully supported. Set an error code.
193 status = U_UNSUPPORTED_ERROR;
194 return nullptr;
195 }
196
197 MeasureUnit unit = unitRef;
198 if (uprv_strcmp(perUnit.getType(), "none") != 0) {
199 // Compound unit: first try to simplify (e.g., meters per second is its own unit).
200 bool isResolved = false;
201 MeasureUnit resolved = MeasureUnit::resolveUnitPerUnit(unit, perUnit, &isResolved);
202 if (isResolved) {
203 unit = resolved;
204 } else {
205 // No simplified form is available.
206 return forCompoundUnit(loc, unit, perUnit, width, rules, parent, status);
207 }
208 }
209
210 auto* result = new LongNameHandler(rules, parent);
211 if (result == nullptr) {
212 status = U_MEMORY_ALLOCATION_ERROR;
213 return nullptr;
214 }
215 UnicodeString simpleFormats[ARRAY_LENGTH];
216 getMeasureData(loc, unit, width, simpleFormats, status);
217 if (U_FAILURE(status)) { return result; }
218 result->simpleFormatsToModifiers(simpleFormats, {UFIELD_CATEGORY_NUMBER, UNUM_MEASURE_UNIT_FIELD}, status);
219 return result;
220}
221
222LongNameHandler*
223LongNameHandler::forCompoundUnit(const Locale &loc, const MeasureUnit &unit, const MeasureUnit &perUnit,
224 const UNumberUnitWidth &width, const PluralRules *rules,
225 const MicroPropsGenerator *parent, UErrorCode &status) {
226 auto* result = new LongNameHandler(rules, parent);
227 if (result == nullptr) {
228 status = U_MEMORY_ALLOCATION_ERROR;
229 return nullptr;
230 }
231 UnicodeString primaryData[ARRAY_LENGTH];
232 getMeasureData(loc, unit, width, primaryData, status);
233 if (U_FAILURE(status)) { return result; }
234 UnicodeString secondaryData[ARRAY_LENGTH];
235 getMeasureData(loc, perUnit, width, secondaryData, status);
236 if (U_FAILURE(status)) { return result; }
237
238 UnicodeString perUnitFormat;
239 if (!secondaryData[PER_INDEX].isBogus()) {
240 perUnitFormat = secondaryData[PER_INDEX];
241 } else {
242 UnicodeString rawPerUnitFormat = getPerUnitFormat(loc, width, status);
243 if (U_FAILURE(status)) { return result; }
244 // rawPerUnitFormat is something like "{0}/{1}"; we need to substitute in the secondary unit.
245 SimpleFormatter compiled(rawPerUnitFormat, 2, 2, status);
246 if (U_FAILURE(status)) { return result; }
247 UnicodeString secondaryFormat = getWithPlural(secondaryData, StandardPlural::Form::ONE, status);
248 if (U_FAILURE(status)) { return result; }
249 // Some "one" pattern may not contain "{0}". For example in "ar" or "ne" locale.
250 SimpleFormatter secondaryCompiled(secondaryFormat, 0, 1, status);
251 if (U_FAILURE(status)) { return result; }
252 UnicodeString secondaryString = secondaryCompiled.getTextWithNoArguments().trim();
253 // TODO: Why does UnicodeString need to be explicit in the following line?
254 compiled.format(UnicodeString(u"{0}"), secondaryString, perUnitFormat, status);
255 if (U_FAILURE(status)) { return result; }
256 }
257 result->multiSimpleFormatsToModifiers(primaryData, perUnitFormat, {UFIELD_CATEGORY_NUMBER, UNUM_MEASURE_UNIT_FIELD}, status);
258 return result;
259}
260
261UnicodeString LongNameHandler::getUnitDisplayName(
262 const Locale& loc,
263 const MeasureUnit& unit,
264 UNumberUnitWidth width,
265 UErrorCode& status) {
266 if (U_FAILURE(status)) {
267 return ICU_Utility::makeBogusString();
268 }
269 UnicodeString simpleFormats[ARRAY_LENGTH];
270 getMeasureData(loc, unit, width, simpleFormats, status);
271 return simpleFormats[DNAM_INDEX];
272}
273
274UnicodeString LongNameHandler::getUnitPattern(
275 const Locale& loc,
276 const MeasureUnit& unit,
277 UNumberUnitWidth width,
278 StandardPlural::Form pluralForm,
279 UErrorCode& status) {
280 if (U_FAILURE(status)) {
281 return ICU_Utility::makeBogusString();
282 }
283 UnicodeString simpleFormats[ARRAY_LENGTH];
284 getMeasureData(loc, unit, width, simpleFormats, status);
285 // The above already handles fallback from other widths to short
286 if (U_FAILURE(status)) {
287 return ICU_Utility::makeBogusString();
288 }
289 // Now handle fallback from other plural forms to OTHER
290 return (!(simpleFormats[pluralForm]).isBogus())? simpleFormats[pluralForm]:
291 simpleFormats[StandardPlural::Form::OTHER];
292}
293
294LongNameHandler* LongNameHandler::forCurrencyLongNames(const Locale &loc, const CurrencyUnit &currency,
295 const PluralRules *rules,
296 const MicroPropsGenerator *parent,
297 UErrorCode &status) {
298 auto* result = new LongNameHandler(rules, parent);
299 if (result == nullptr) {
300 status = U_MEMORY_ALLOCATION_ERROR;
301 return nullptr;
302 }
303 UnicodeString simpleFormats[ARRAY_LENGTH];
304 getCurrencyLongNameData(loc, currency, simpleFormats, status);
305 if (U_FAILURE(status)) { return nullptr; }
306 result->simpleFormatsToModifiers(simpleFormats, {UFIELD_CATEGORY_NUMBER, UNUM_CURRENCY_FIELD}, status);
307 return result;
308}
309
310void LongNameHandler::simpleFormatsToModifiers(const UnicodeString *simpleFormats, Field field,
311 UErrorCode &status) {
312 for (int32_t i = 0; i < StandardPlural::Form::COUNT; i++) {
313 StandardPlural::Form plural = static_cast<StandardPlural::Form>(i);
314 UnicodeString simpleFormat = getWithPlural(simpleFormats, plural, status);
315 if (U_FAILURE(status)) { return; }
316 SimpleFormatter compiledFormatter(simpleFormat, 0, 1, status);
317 if (U_FAILURE(status)) { return; }
318 fModifiers[i] = SimpleModifier(compiledFormatter, field, false, {this, SIGNUM_POS_ZERO, plural});
319 }
320}
321
322void LongNameHandler::multiSimpleFormatsToModifiers(const UnicodeString *leadFormats, UnicodeString trailFormat,
323 Field field, UErrorCode &status) {
324 SimpleFormatter trailCompiled(trailFormat, 1, 1, status);
325 if (U_FAILURE(status)) { return; }
326 for (int32_t i = 0; i < StandardPlural::Form::COUNT; i++) {
327 StandardPlural::Form plural = static_cast<StandardPlural::Form>(i);
328 UnicodeString leadFormat = getWithPlural(leadFormats, plural, status);
329 if (U_FAILURE(status)) { return; }
330 UnicodeString compoundFormat;
331 trailCompiled.format(leadFormat, compoundFormat, status);
332 if (U_FAILURE(status)) { return; }
333 SimpleFormatter compoundCompiled(compoundFormat, 0, 1, status);
334 if (U_FAILURE(status)) { return; }
335 fModifiers[i] = SimpleModifier(compoundCompiled, field, false, {this, SIGNUM_POS_ZERO, plural});
336 }
337}
338
339void LongNameHandler::processQuantity(DecimalQuantity &quantity, MicroProps &micros,
340 UErrorCode &status) const {
341 parent->processQuantity(quantity, micros, status);
342 StandardPlural::Form pluralForm = utils::getPluralSafe(micros.rounder, rules, quantity, status);
343 micros.modOuter = &fModifiers[pluralForm];
344}
345
346const Modifier* LongNameHandler::getModifier(Signum /*signum*/, StandardPlural::Form plural) const {
347 return &fModifiers[plural];
348}
349
350#endif /* #if !UCONFIG_NO_FORMATTING */
351