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