]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/number_longnames.cpp
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / number_longnames.cpp
CommitLineData
0f5d89e8
A
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"
3d1f044b 17#include "util.h"
0f5d89e8
A
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,
3d1f044b 43 StandardPlural::Form plural,
0f5d89e8
A
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; }
3d1f044b
A
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
0f5d89e8
A
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);
3d1f044b
A
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);
0f5d89e8
A
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 UBool isChoiceFormat = FALSE;
152 int32_t longNameLen = 0;
153 const char16_t *longName = ucurr_getPluralName(
154 currency.getISOCurrency(),
155 locale.getName(),
156 &isChoiceFormat,
157 StandardPlural::getKeyword(static_cast<StandardPlural::Form>(i)),
158 &longNameLen,
159 &status);
160 // Example pattern from data: "{0} {1}"
161 // Example output after find-and-replace: "{0} US dollars"
162 pattern.findAndReplace(UnicodeString(u"{1}"), UnicodeString(longName, longNameLen));
163 }
164}
165
166UnicodeString getPerUnitFormat(const Locale& locale, const UNumberUnitWidth &width, UErrorCode& status) {
167 LocalUResourceBundlePointer unitsBundle(ures_open(U_ICUDATA_UNIT, locale.getName(), &status));
168 if (U_FAILURE(status)) { return {}; }
169 CharString key;
170 key.append("units", status);
171 if (width == UNUM_UNIT_WIDTH_NARROW) {
172 key.append("Narrow", status);
173 } else if (width == UNUM_UNIT_WIDTH_SHORT) {
174 key.append("Short", status);
175 }
176 key.append("/compound/per", status);
177 int32_t len = 0;
178 const UChar* ptr = ures_getStringByKeyWithFallback(unitsBundle.getAlias(), key.data(), &len, &status);
179 return UnicodeString(ptr, len);
180}
181
182////////////////////////
183/// END DATA LOADING ///
184////////////////////////
185
186} // namespace
187
3d1f044b 188LongNameHandler*
0f5d89e8
A
189LongNameHandler::forMeasureUnit(const Locale &loc, const MeasureUnit &unitRef, const MeasureUnit &perUnit,
190 const UNumberUnitWidth &width, const PluralRules *rules,
191 const MicroPropsGenerator *parent, UErrorCode &status) {
192 MeasureUnit unit = unitRef;
193 if (uprv_strcmp(perUnit.getType(), "none") != 0) {
194 // Compound unit: first try to simplify (e.g., meters per second is its own unit).
195 bool isResolved = false;
196 MeasureUnit resolved = MeasureUnit::resolveUnitPerUnit(unit, perUnit, &isResolved);
197 if (isResolved) {
198 unit = resolved;
199 } else {
200 // No simplified form is available.
201 return forCompoundUnit(loc, unit, perUnit, width, rules, parent, status);
202 }
203 }
204
3d1f044b
A
205 auto* result = new LongNameHandler(rules, parent);
206 if (result == nullptr) {
207 status = U_MEMORY_ALLOCATION_ERROR;
208 return nullptr;
209 }
0f5d89e8
A
210 UnicodeString simpleFormats[ARRAY_LENGTH];
211 getMeasureData(loc, unit, width, simpleFormats, status);
212 if (U_FAILURE(status)) { return result; }
3d1f044b 213 result->simpleFormatsToModifiers(simpleFormats, UNUM_MEASURE_UNIT_FIELD, status);
0f5d89e8
A
214 return result;
215}
216
3d1f044b 217LongNameHandler*
0f5d89e8
A
218LongNameHandler::forCompoundUnit(const Locale &loc, const MeasureUnit &unit, const MeasureUnit &perUnit,
219 const UNumberUnitWidth &width, const PluralRules *rules,
220 const MicroPropsGenerator *parent, UErrorCode &status) {
3d1f044b
A
221 auto* result = new LongNameHandler(rules, parent);
222 if (result == nullptr) {
223 status = U_MEMORY_ALLOCATION_ERROR;
224 return nullptr;
225 }
0f5d89e8
A
226 UnicodeString primaryData[ARRAY_LENGTH];
227 getMeasureData(loc, unit, width, primaryData, status);
228 if (U_FAILURE(status)) { return result; }
229 UnicodeString secondaryData[ARRAY_LENGTH];
230 getMeasureData(loc, perUnit, width, secondaryData, status);
231 if (U_FAILURE(status)) { return result; }
232
233 UnicodeString perUnitFormat;
234 if (!secondaryData[PER_INDEX].isBogus()) {
235 perUnitFormat = secondaryData[PER_INDEX];
236 } else {
237 UnicodeString rawPerUnitFormat = getPerUnitFormat(loc, width, status);
238 if (U_FAILURE(status)) { return result; }
239 // rawPerUnitFormat is something like "{0}/{1}"; we need to substitute in the secondary unit.
240 SimpleFormatter compiled(rawPerUnitFormat, 2, 2, status);
241 if (U_FAILURE(status)) { return result; }
242 UnicodeString secondaryFormat = getWithPlural(secondaryData, StandardPlural::Form::ONE, status);
243 if (U_FAILURE(status)) { return result; }
244 SimpleFormatter secondaryCompiled(secondaryFormat, 1, 1, status);
245 if (U_FAILURE(status)) { return result; }
246 UnicodeString secondaryString = secondaryCompiled.getTextWithNoArguments().trim();
247 // TODO: Why does UnicodeString need to be explicit in the following line?
248 compiled.format(UnicodeString(u"{0}"), secondaryString, perUnitFormat, status);
249 if (U_FAILURE(status)) { return result; }
250 }
3d1f044b 251 result->multiSimpleFormatsToModifiers(primaryData, perUnitFormat, UNUM_MEASURE_UNIT_FIELD, status);
0f5d89e8
A
252 return result;
253}
254
3d1f044b
A
255UnicodeString LongNameHandler::getUnitDisplayName(
256 const Locale& loc,
257 const MeasureUnit& unit,
258 UNumberUnitWidth width,
259 UErrorCode& status) {
260 if (U_FAILURE(status)) {
261 return ICU_Utility::makeBogusString();
262 }
263 UnicodeString simpleFormats[ARRAY_LENGTH];
264 getMeasureData(loc, unit, width, simpleFormats, status);
265 return simpleFormats[DNAM_INDEX];
266}
267
268UnicodeString LongNameHandler::getUnitPattern( // Apple-specific
269 const Locale& loc,
270 const MeasureUnit& unit,
271 UNumberUnitWidth width,
272 StandardPlural::Form pluralForm,
273 UErrorCode& status) {
274 if (U_FAILURE(status)) {
275 return ICU_Utility::makeBogusString();
276 }
277 UnicodeString simpleFormats[ARRAY_LENGTH];
278 getMeasureData(loc, unit, width, simpleFormats, status);
279 return simpleFormats[pluralForm];
280}
281
282LongNameHandler* LongNameHandler::forCurrencyLongNames(const Locale &loc, const CurrencyUnit &currency,
0f5d89e8
A
283 const PluralRules *rules,
284 const MicroPropsGenerator *parent,
285 UErrorCode &status) {
3d1f044b
A
286 auto* result = new LongNameHandler(rules, parent);
287 if (result == nullptr) {
288 status = U_MEMORY_ALLOCATION_ERROR;
289 return nullptr;
290 }
0f5d89e8
A
291 UnicodeString simpleFormats[ARRAY_LENGTH];
292 getCurrencyLongNameData(loc, currency, simpleFormats, status);
3d1f044b
A
293 if (U_FAILURE(status)) { return nullptr; }
294 result->simpleFormatsToModifiers(simpleFormats, UNUM_CURRENCY_FIELD, status);
0f5d89e8
A
295 return result;
296}
297
298void LongNameHandler::simpleFormatsToModifiers(const UnicodeString *simpleFormats, Field field,
3d1f044b 299 UErrorCode &status) {
0f5d89e8 300 for (int32_t i = 0; i < StandardPlural::Form::COUNT; i++) {
3d1f044b
A
301 StandardPlural::Form plural = static_cast<StandardPlural::Form>(i);
302 UnicodeString simpleFormat = getWithPlural(simpleFormats, plural, status);
0f5d89e8
A
303 if (U_FAILURE(status)) { return; }
304 SimpleFormatter compiledFormatter(simpleFormat, 0, 1, status);
305 if (U_FAILURE(status)) { return; }
3d1f044b 306 fModifiers[i] = SimpleModifier(compiledFormatter, field, false, {this, 0, plural});
0f5d89e8
A
307 }
308}
309
310void LongNameHandler::multiSimpleFormatsToModifiers(const UnicodeString *leadFormats, UnicodeString trailFormat,
3d1f044b 311 Field field, UErrorCode &status) {
0f5d89e8
A
312 SimpleFormatter trailCompiled(trailFormat, 1, 1, status);
313 if (U_FAILURE(status)) { return; }
314 for (int32_t i = 0; i < StandardPlural::Form::COUNT; i++) {
3d1f044b
A
315 StandardPlural::Form plural = static_cast<StandardPlural::Form>(i);
316 UnicodeString leadFormat = getWithPlural(leadFormats, plural, status);
0f5d89e8
A
317 if (U_FAILURE(status)) { return; }
318 UnicodeString compoundFormat;
319 trailCompiled.format(leadFormat, compoundFormat, status);
320 if (U_FAILURE(status)) { return; }
321 SimpleFormatter compoundCompiled(compoundFormat, 0, 1, status);
322 if (U_FAILURE(status)) { return; }
3d1f044b 323 fModifiers[i] = SimpleModifier(compoundCompiled, field, false, {this, 0, plural});
0f5d89e8
A
324 }
325}
326
327void LongNameHandler::processQuantity(DecimalQuantity &quantity, MicroProps &micros,
328 UErrorCode &status) const {
329 parent->processQuantity(quantity, micros, status);
3d1f044b
A
330 StandardPlural::Form pluralForm = utils::getPluralSafe(micros.rounder, rules, quantity, status);
331 micros.modOuter = &fModifiers[pluralForm];
332}
333
334const Modifier* LongNameHandler::getModifier(int8_t /*signum*/, StandardPlural::Form plural) const {
335 return &fModifiers[plural];
0f5d89e8
A
336}
337
338#endif /* #if !UCONFIG_NO_FORMATTING */