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/ustring.h"
9 #include "unicode/ures.h"
10 #include "cstring.h"
11 #include "charstr.h"
12 #include "resource.h"
13 #include "number_compact.h"
14 #include "number_microprops.h"
15 #include "uresimp.h"
16 
17 using namespace icu;
18 using namespace icu::number;
19 using namespace icu::number::impl;
20 
21 namespace {
22 
23 // A dummy object used when a "0" compact decimal entry is encountered. This is necessary
24 // in order to prevent falling back to root. Object equality ("==") is intended.
25 const UChar *USE_FALLBACK = u"<USE FALLBACK>";
26 
27 /** Produces a string like "NumberElements/latn/patternsShort/decimalFormat". */
getResourceBundleKey(const char * nsName,CompactStyle compactStyle,CompactType compactType,CharString & sb,UErrorCode & status)28 void getResourceBundleKey(const char *nsName, CompactStyle compactStyle, CompactType compactType,
29                                  CharString &sb, UErrorCode &status) {
30     sb.clear();
31     sb.append("NumberElements/", status);
32     sb.append(nsName, status);
33     sb.append(compactStyle == CompactStyle::UNUM_SHORT ? "/patternsShort" : "/patternsLong", status);
34     sb.append(compactType == CompactType::TYPE_DECIMAL ? "/decimalFormat" : "/currencyFormat", status);
35 }
36 
getIndex(int32_t magnitude,StandardPlural::Form plural)37 int32_t getIndex(int32_t magnitude, StandardPlural::Form plural) {
38     return magnitude * StandardPlural::COUNT + plural;
39 }
40 
countZeros(const UChar * patternString,int32_t patternLength)41 int32_t countZeros(const UChar *patternString, int32_t patternLength) {
42     // NOTE: This strategy for computing the number of zeros is a hack for efficiency.
43     // It could break if there are any 0s that aren't part of the main pattern.
44     int32_t numZeros = 0;
45     for (int32_t i = 0; i < patternLength; i++) {
46         if (patternString[i] == u'0') {
47             numZeros++;
48         } else if (numZeros > 0) {
49             break; // zeros should always be contiguous
50         }
51     }
52     return numZeros;
53 }
54 
55 } // namespace
56 
57 // NOTE: patterns and multipliers both get zero-initialized.
CompactData()58 CompactData::CompactData() : patterns(), multipliers(), largestMagnitude(0), isEmpty(TRUE) {
59 }
60 
populate(const Locale & locale,const char * nsName,CompactStyle compactStyle,CompactType compactType,UErrorCode & status)61 void CompactData::populate(const Locale &locale, const char *nsName, CompactStyle compactStyle,
62                            CompactType compactType, UErrorCode &status) {
63     CompactDataSink sink(*this);
64     LocalUResourceBundlePointer rb(ures_open(nullptr, locale.getName(), &status));
65     if (U_FAILURE(status)) { return; }
66 
67     bool nsIsLatn = strcmp(nsName, "latn") == 0;
68     bool compactIsShort = compactStyle == CompactStyle::UNUM_SHORT;
69 
70     // Fall back to latn numbering system and/or short compact style.
71     CharString resourceKey;
72     getResourceBundleKey(nsName, compactStyle, compactType, resourceKey, status);
73     UErrorCode localStatus = U_ZERO_ERROR;
74     ures_getAllItemsWithFallback(rb.getAlias(), resourceKey.data(), sink, localStatus);
75     if (isEmpty && !nsIsLatn) {
76         getResourceBundleKey("latn", compactStyle, compactType, resourceKey, status);
77         localStatus = U_ZERO_ERROR;
78         ures_getAllItemsWithFallback(rb.getAlias(), resourceKey.data(), sink, localStatus);
79     }
80     if (isEmpty && !compactIsShort) {
81         getResourceBundleKey(nsName, CompactStyle::UNUM_SHORT, compactType, resourceKey, status);
82         localStatus = U_ZERO_ERROR;
83         ures_getAllItemsWithFallback(rb.getAlias(), resourceKey.data(), sink, localStatus);
84     }
85     if (isEmpty && !nsIsLatn && !compactIsShort) {
86         getResourceBundleKey("latn", CompactStyle::UNUM_SHORT, compactType, resourceKey, status);
87         localStatus = U_ZERO_ERROR;
88         ures_getAllItemsWithFallback(rb.getAlias(), resourceKey.data(), sink, localStatus);
89     }
90 
91     // The last fallback should be guaranteed to return data.
92     if (isEmpty) {
93         status = U_INTERNAL_PROGRAM_ERROR;
94     }
95 }
96 
getMultiplier(int32_t magnitude) const97 int32_t CompactData::getMultiplier(int32_t magnitude) const {
98     if (magnitude < 0) {
99         return 0;
100     }
101     if (magnitude > largestMagnitude) {
102         magnitude = largestMagnitude;
103     }
104     return multipliers[magnitude];
105 }
106 
getPattern(int32_t magnitude,StandardPlural::Form plural) const107 const UChar *CompactData::getPattern(int32_t magnitude, StandardPlural::Form plural) const {
108     if (magnitude < 0) {
109         return nullptr;
110     }
111     if (magnitude > largestMagnitude) {
112         magnitude = largestMagnitude;
113     }
114     const UChar *patternString = patterns[getIndex(magnitude, plural)];
115     if (patternString == nullptr && plural != StandardPlural::OTHER) {
116         // Fall back to "other" plural variant
117         patternString = patterns[getIndex(magnitude, StandardPlural::OTHER)];
118     }
119     if (patternString == USE_FALLBACK) { // == is intended
120         // Return null if USE_FALLBACK is present
121         patternString = nullptr;
122     }
123     return patternString;
124 }
125 
getUniquePatterns(UVector & output,UErrorCode & status) const126 void CompactData::getUniquePatterns(UVector &output, UErrorCode &status) const {
127     U_ASSERT(output.isEmpty());
128     // NOTE: In C++, this is done more manually with a UVector.
129     // In Java, we can take advantage of JDK HashSet.
130     for (auto pattern : patterns) {
131         if (pattern == nullptr || pattern == USE_FALLBACK) {
132             continue;
133         }
134 
135         // Insert pattern into the UVector if the UVector does not already contain the pattern.
136         // Search the UVector from the end since identical patterns are likely to be adjacent.
137         for (int32_t i = output.size() - 1; i >= 0; i--) {
138             if (u_strcmp(pattern, static_cast<const UChar *>(output[i])) == 0) {
139                 goto continue_outer;
140             }
141         }
142 
143         // The string was not found; add it to the UVector.
144         // ANDY: This requires a const_cast.  Why?
145         output.addElement(const_cast<UChar *>(pattern), status);
146 
147         continue_outer:
148         continue;
149     }
150 }
151 
put(const char * key,ResourceValue & value,UBool,UErrorCode & status)152 void CompactData::CompactDataSink::put(const char *key, ResourceValue &value, UBool /*noFallback*/,
153                                        UErrorCode &status) {
154     // traverse into the table of powers of ten
155     ResourceTable powersOfTenTable = value.getTable(status);
156     if (U_FAILURE(status)) { return; }
157     for (int i3 = 0; powersOfTenTable.getKeyAndValue(i3, key, value); ++i3) {
158 
159         // Assumes that the keys are always of the form "10000" where the magnitude is the
160         // length of the key minus one.  We expect magnitudes to be less than MAX_DIGITS.
161         auto magnitude = static_cast<int8_t> (strlen(key) - 1);
162         int8_t multiplier = data.multipliers[magnitude];
163         U_ASSERT(magnitude < COMPACT_MAX_DIGITS);
164 
165         // Iterate over the plural variants ("one", "other", etc)
166         ResourceTable pluralVariantsTable = value.getTable(status);
167         if (U_FAILURE(status)) { return; }
168         for (int i4 = 0; pluralVariantsTable.getKeyAndValue(i4, key, value); ++i4) {
169 
170             if (uprv_strcmp(key, "0") == 0 || uprv_strcmp(key, "1") == 0) {
171                 // TODO(ICU-21258): Handle this case. For now, skip.
172                 continue;
173             }
174 
175             // Skip this magnitude/plural if we already have it from a child locale.
176             // Note: This also skips USE_FALLBACK entries.
177             StandardPlural::Form plural = StandardPlural::fromString(key, status);
178             if (U_FAILURE(status)) { return; }
179             if (data.patterns[getIndex(magnitude, plural)] != nullptr) {
180                 continue;
181             }
182 
183             // The value "0" means that we need to use the default pattern and not fall back
184             // to parent locales. Example locale where this is relevant: 'it'.
185             int32_t patternLength;
186             const UChar *patternString = value.getString(patternLength, status);
187             if (U_FAILURE(status)) { return; }
188             if (u_strcmp(patternString, u"0") == 0) {
189                 patternString = USE_FALLBACK;
190                 patternLength = 0;
191             }
192 
193             // Save the pattern string. We will parse it lazily.
194             data.patterns[getIndex(magnitude, plural)] = patternString;
195 
196             // If necessary, compute the multiplier: the difference between the magnitude
197             // and the number of zeros in the pattern.
198             if (multiplier == 0) {
199                 int32_t numZeros = countZeros(patternString, patternLength);
200                 if (numZeros > 0) { // numZeros==0 in certain cases, like Somali "Kun"
201                     multiplier = static_cast<int8_t> (numZeros - magnitude - 1);
202                 }
203             }
204         }
205 
206         // Save the multiplier.
207         if (data.multipliers[magnitude] == 0) {
208             data.multipliers[magnitude] = multiplier;
209             if (magnitude > data.largestMagnitude) {
210                 data.largestMagnitude = magnitude;
211             }
212             data.isEmpty = false;
213         } else {
214             U_ASSERT(data.multipliers[magnitude] == multiplier);
215         }
216     }
217 }
218 
219 ///////////////////////////////////////////////////////////
220 /// END OF CompactData.java; BEGIN CompactNotation.java ///
221 ///////////////////////////////////////////////////////////
222 
CompactHandler(CompactStyle compactStyle,const Locale & locale,const char * nsName,CompactType compactType,const PluralRules * rules,MutablePatternModifier * buildReference,bool safe,const MicroPropsGenerator * parent,UErrorCode & status)223 CompactHandler::CompactHandler(
224         CompactStyle compactStyle,
225         const Locale &locale,
226         const char *nsName,
227         CompactType compactType,
228         const PluralRules *rules,
229         MutablePatternModifier *buildReference,
230         bool safe,
231         const MicroPropsGenerator *parent,
232         UErrorCode &status)
233         : rules(rules), parent(parent), safe(safe) {
234     data.populate(locale, nsName, compactStyle, compactType, status);
235     if (safe) {
236         // Safe code path
237         precomputeAllModifiers(*buildReference, status);
238     } else {
239         // Unsafe code path
240         // Store the MutablePatternModifier reference.
241         unsafePatternModifier = buildReference;
242     }
243 }
244 
~CompactHandler()245 CompactHandler::~CompactHandler() {
246     for (int32_t i = 0; i < precomputedModsLength; i++) {
247         delete precomputedMods[i].mod;
248     }
249 }
250 
precomputeAllModifiers(MutablePatternModifier & buildReference,UErrorCode & status)251 void CompactHandler::precomputeAllModifiers(MutablePatternModifier &buildReference, UErrorCode &status) {
252     if (U_FAILURE(status)) { return; }
253 
254     // Initial capacity of 12 for 0K, 00K, 000K, ...M, ...B, and ...T
255     UVector allPatterns(12, status);
256     if (U_FAILURE(status)) { return; }
257     data.getUniquePatterns(allPatterns, status);
258     if (U_FAILURE(status)) { return; }
259 
260     // C++ only: ensure that precomputedMods has room.
261     precomputedModsLength = allPatterns.size();
262     if (precomputedMods.getCapacity() < precomputedModsLength) {
263         precomputedMods.resize(allPatterns.size(), status);
264         if (U_FAILURE(status)) { return; }
265     }
266 
267     for (int32_t i = 0; i < precomputedModsLength; i++) {
268         auto patternString = static_cast<const UChar *>(allPatterns[i]);
269         UnicodeString hello(patternString);
270         CompactModInfo &info = precomputedMods[i];
271         ParsedPatternInfo patternInfo;
272         PatternParser::parseToPatternInfo(UnicodeString(patternString), patternInfo, status);
273         if (U_FAILURE(status)) { return; }
274         buildReference.setPatternInfo(&patternInfo, {UFIELD_CATEGORY_NUMBER, UNUM_COMPACT_FIELD});
275         info.mod = buildReference.createImmutable(status);
276         if (U_FAILURE(status)) { return; }
277         info.patternString = patternString;
278     }
279 }
280 
processQuantity(DecimalQuantity & quantity,MicroProps & micros,UErrorCode & status) const281 void CompactHandler::processQuantity(DecimalQuantity &quantity, MicroProps &micros,
282                                      UErrorCode &status) const {
283     parent->processQuantity(quantity, micros, status);
284     if (U_FAILURE(status)) { return; }
285 
286     // Treat zero, NaN, and infinity as if they had magnitude 0
287     int32_t magnitude;
288     int32_t multiplier = 0;
289     if (quantity.isZeroish()) {
290         magnitude = 0;
291         micros.rounder.apply(quantity, status);
292     } else {
293         // TODO: Revisit chooseMultiplierAndApply
294         multiplier = micros.rounder.chooseMultiplierAndApply(quantity, data, status);
295         magnitude = quantity.isZeroish() ? 0 : quantity.getMagnitude();
296         magnitude -= multiplier;
297     }
298 
299     StandardPlural::Form plural = utils::getStandardPlural(rules, quantity);
300     const UChar *patternString = data.getPattern(magnitude, plural);
301     if (patternString == nullptr) {
302         // Use the default (non-compact) modifier.
303         // No need to take any action.
304     } else if (safe) {
305         // Safe code path.
306         // Java uses a hash set here for O(1) lookup.  C++ uses a linear search.
307         // TODO: Benchmark this and maybe change to a binary search or hash table.
308         int32_t i = 0;
309         for (; i < precomputedModsLength; i++) {
310             const CompactModInfo &info = precomputedMods[i];
311             if (u_strcmp(patternString, info.patternString) == 0) {
312                 info.mod->applyToMicros(micros, quantity, status);
313                 break;
314             }
315         }
316         // It should be guaranteed that we found the entry.
317         U_ASSERT(i < precomputedModsLength);
318     } else {
319         // Unsafe code path.
320         // Overwrite the PatternInfo in the existing modMiddle.
321         // C++ Note: Use unsafePatternInfo for proper lifecycle.
322         ParsedPatternInfo &patternInfo = const_cast<CompactHandler *>(this)->unsafePatternInfo;
323         PatternParser::parseToPatternInfo(UnicodeString(patternString), patternInfo, status);
324         unsafePatternModifier->setPatternInfo(
325             &unsafePatternInfo,
326             {UFIELD_CATEGORY_NUMBER, UNUM_COMPACT_FIELD});
327         unsafePatternModifier->setNumberProperties(quantity.signum(), StandardPlural::Form::COUNT);
328         micros.modMiddle = unsafePatternModifier;
329     }
330 
331     // Change the exponent only after we select appropriate plural form
332     // for formatting purposes so that we preserve expected formatted
333     // string behavior.
334     quantity.adjustExponent(-1 * multiplier);
335 
336     // We already performed rounding. Do not perform it again.
337     micros.rounder = RoundingImpl::passThrough();
338 }
339 
340 #endif /* #if !UCONFIG_NO_FORMATTING */
341