/src/icu/icu4c/source/i18n/number_modifiers.cpp
Line | Count | Source |
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 "umutex.h" |
9 | | #include "ucln_cmn.h" |
10 | | #include "ucln_in.h" |
11 | | #include "number_modifiers.h" |
12 | | |
13 | | using namespace icu; |
14 | | using namespace icu::number; |
15 | | using namespace icu::number::impl; |
16 | | |
17 | | namespace { |
18 | | |
19 | | // TODO: This is copied from simpleformatter.cpp |
20 | | const int32_t ARG_NUM_LIMIT = 0x100; |
21 | | |
22 | | // These are the default currency spacing UnicodeSets in CLDR. |
23 | | // Pre-compute them for performance. |
24 | | // The Java unit test testCurrencySpacingPatternStability() will start failing if these change in CLDR. |
25 | | icu::UInitOnce gDefaultCurrencySpacingInitOnce {}; |
26 | | |
27 | | UnicodeSet *UNISET_DIGIT = nullptr; |
28 | | UnicodeSet *UNISET_NOTSZ = nullptr; |
29 | | |
30 | 0 | UBool U_CALLCONV cleanupDefaultCurrencySpacing() { |
31 | 0 | delete UNISET_DIGIT; |
32 | 0 | UNISET_DIGIT = nullptr; |
33 | 0 | delete UNISET_NOTSZ; |
34 | 0 | UNISET_NOTSZ = nullptr; |
35 | 0 | gDefaultCurrencySpacingInitOnce.reset(); |
36 | 0 | return true; |
37 | 0 | } |
38 | | |
39 | 0 | void U_CALLCONV initDefaultCurrencySpacing(UErrorCode &status) { |
40 | 0 | ucln_i18n_registerCleanup(UCLN_I18N_CURRENCY_SPACING, cleanupDefaultCurrencySpacing); |
41 | 0 | UNISET_DIGIT = new UnicodeSet(UnicodeString(u"[:digit:]"), status); |
42 | 0 | UNISET_NOTSZ = new UnicodeSet(UnicodeString(u"[[:^S:]&[:^Z:]]"), status); |
43 | 0 | if (UNISET_DIGIT == nullptr || UNISET_NOTSZ == nullptr) { |
44 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
45 | 0 | return; |
46 | 0 | } |
47 | 0 | UNISET_DIGIT->freeze(); |
48 | 0 | UNISET_NOTSZ->freeze(); |
49 | 0 | } |
50 | | |
51 | | } // namespace |
52 | | |
53 | | |
54 | 9.00M | Modifier::~Modifier() = default; |
55 | | |
56 | | Modifier::Parameters::Parameters() |
57 | 3.81M | : obj(nullptr) {} |
58 | | |
59 | | Modifier::Parameters::Parameters( |
60 | | const ModifierStore* _obj, Signum _signum, StandardPlural::Form _plural) |
61 | 13.3k | : obj(_obj), signum(_signum), plural(_plural) {} |
62 | | |
63 | 0 | bool Modifier::semanticallyEquivalent(const Modifier& other) const { |
64 | 0 | Parameters paramsThis; |
65 | 0 | Parameters paramsOther; |
66 | 0 | getParameters(paramsThis); |
67 | 0 | other.getParameters(paramsOther); |
68 | 0 | if (paramsThis.obj == nullptr && paramsOther.obj == nullptr) { |
69 | 0 | return strictEquals(other); |
70 | 0 | } else if (paramsThis.obj == nullptr || paramsOther.obj == nullptr) { |
71 | 0 | return false; |
72 | 0 | } |
73 | 0 | for (size_t i=0; i<SIGNUM_COUNT; i++) { |
74 | 0 | auto signum = static_cast<Signum>(i); |
75 | 0 | for (size_t j=0; j<StandardPlural::COUNT; j++) { |
76 | 0 | auto plural = static_cast<StandardPlural::Form>(j); |
77 | 0 | const auto* mod1 = paramsThis.obj->getModifier(signum, plural); |
78 | 0 | const auto* mod2 = paramsOther.obj->getModifier(signum, plural); |
79 | 0 | if (mod1 == mod2) { |
80 | | // Equal pointers |
81 | 0 | continue; |
82 | 0 | } else if (mod1 == nullptr || mod2 == nullptr) { |
83 | | // One pointer is null but not the other |
84 | 0 | return false; |
85 | 0 | } else if (!mod1->strictEquals(*mod2)) { |
86 | | // The modifiers are NOT equivalent |
87 | 0 | return false; |
88 | 0 | } else { |
89 | | // The modifiers are equivalent |
90 | 0 | continue; |
91 | 0 | } |
92 | 0 | } |
93 | 0 | } |
94 | 0 | return true; |
95 | 0 | } |
96 | | |
97 | | |
98 | 1.99k | ModifierStore::~ModifierStore() = default; |
99 | | |
100 | 111k | AdoptingSignumModifierStore::~AdoptingSignumModifierStore() { |
101 | 447k | for (const Modifier *mod : mods) { |
102 | 447k | delete mod; |
103 | 447k | } |
104 | 111k | } |
105 | | |
106 | | AdoptingSignumModifierStore& |
107 | 539 | AdoptingSignumModifierStore::operator=(AdoptingSignumModifierStore&& other) noexcept { |
108 | 2.69k | for (size_t i=0; i<SIGNUM_COUNT; i++) { |
109 | 2.15k | this->mods[i] = other.mods[i]; |
110 | 2.15k | other.mods[i] = nullptr; |
111 | 2.15k | } |
112 | 539 | return *this; |
113 | 539 | } |
114 | | |
115 | | |
116 | | int32_t ConstantAffixModifier::apply(FormattedStringBuilder &output, int leftIndex, int rightIndex, |
117 | 0 | UErrorCode &status) const { |
118 | | // Insert the suffix first since inserting the prefix will change the rightIndex |
119 | 0 | int length = output.insert(rightIndex, fSuffix, fField, status); |
120 | 0 | length += output.insert(leftIndex, fPrefix, fField, status); |
121 | 0 | return length; |
122 | 0 | } |
123 | | |
124 | 0 | int32_t ConstantAffixModifier::getPrefixLength() const { |
125 | 0 | return fPrefix.length(); |
126 | 0 | } |
127 | | |
128 | 0 | int32_t ConstantAffixModifier::getCodePointCount() const { |
129 | 0 | return fPrefix.countChar32() + fSuffix.countChar32(); |
130 | 0 | } |
131 | | |
132 | 0 | bool ConstantAffixModifier::isStrong() const { |
133 | 0 | return fStrong; |
134 | 0 | } |
135 | | |
136 | 0 | bool ConstantAffixModifier::containsField(Field field) const { |
137 | 0 | (void)field; |
138 | | // This method is not currently used. |
139 | 0 | UPRV_UNREACHABLE_EXIT; |
140 | 0 | } |
141 | | |
142 | 0 | void ConstantAffixModifier::getParameters(Parameters& output) const { |
143 | 0 | (void)output; |
144 | | // This method is not currently used. |
145 | 0 | UPRV_UNREACHABLE_EXIT; |
146 | 0 | } |
147 | | |
148 | 0 | bool ConstantAffixModifier::strictEquals(const Modifier& other) const { |
149 | 0 | const auto* _other = dynamic_cast<const ConstantAffixModifier*>(&other); |
150 | 0 | if (_other == nullptr) { |
151 | 0 | return false; |
152 | 0 | } |
153 | 0 | return fPrefix == _other->fPrefix |
154 | 0 | && fSuffix == _other->fSuffix |
155 | 0 | && fField == _other->fField |
156 | 0 | && fStrong == _other->fStrong; |
157 | 0 | } |
158 | | |
159 | | |
160 | | SimpleModifier::SimpleModifier(const SimpleFormatter &simpleFormatter, Field field, bool strong) |
161 | 1.67M | : SimpleModifier(simpleFormatter, field, strong, {}) {} |
162 | | |
163 | | SimpleModifier::SimpleModifier(const SimpleFormatter &simpleFormatter, Field field, bool strong, |
164 | | const Modifier::Parameters parameters) |
165 | 1.68M | : fCompiledPattern(simpleFormatter.compiledPattern), fField(field), fStrong(strong), |
166 | 1.68M | fParameters(parameters) { |
167 | 1.68M | int32_t argLimit = SimpleFormatter::getArgumentLimit( |
168 | 1.68M | fCompiledPattern.getBuffer(), fCompiledPattern.length()); |
169 | 1.68M | if (argLimit == 0) { |
170 | | // No arguments in compiled pattern |
171 | 1.15k | fPrefixLength = fCompiledPattern.charAt(1) - ARG_NUM_LIMIT; |
172 | 1.15k | U_ASSERT(2 + fPrefixLength == fCompiledPattern.length()); |
173 | | // Set suffixOffset = -1 to indicate no arguments in compiled pattern. |
174 | 1.15k | fSuffixOffset = -1; |
175 | 1.15k | fSuffixLength = 0; |
176 | 1.68M | } else { |
177 | 1.68M | U_ASSERT(argLimit == 1); |
178 | 1.68M | if (fCompiledPattern.charAt(1) != 0) { |
179 | | // Found prefix |
180 | 1.49M | fPrefixLength = fCompiledPattern.charAt(1) - ARG_NUM_LIMIT; |
181 | 1.49M | fSuffixOffset = 3 + fPrefixLength; |
182 | 1.49M | } else { |
183 | | // No prefix |
184 | 193k | fPrefixLength = 0; |
185 | 193k | fSuffixOffset = 2; |
186 | 193k | } |
187 | 1.68M | if (3 + fPrefixLength < fCompiledPattern.length()) { |
188 | | // Found suffix |
189 | 1.68M | fSuffixLength = fCompiledPattern.charAt(fSuffixOffset) - ARG_NUM_LIMIT; |
190 | 1.68M | } else { |
191 | | // No suffix |
192 | 7.67k | fSuffixLength = 0; |
193 | 7.67k | } |
194 | 1.68M | } |
195 | 1.68M | } |
196 | | |
197 | | SimpleModifier::SimpleModifier() |
198 | 1.69M | : fField(kUndefinedField), fStrong(false), fPrefixLength(0), fSuffixLength(0) { |
199 | 1.69M | } |
200 | | |
201 | | int32_t SimpleModifier::apply(FormattedStringBuilder &output, int leftIndex, int rightIndex, |
202 | 1.67k | UErrorCode &status) const { |
203 | 1.67k | return formatAsPrefixSuffix(output, leftIndex, rightIndex, status); |
204 | 1.67k | } |
205 | | |
206 | 0 | int32_t SimpleModifier::getPrefixLength() const { |
207 | 0 | return fPrefixLength; |
208 | 0 | } |
209 | | |
210 | 0 | int32_t SimpleModifier::getCodePointCount() const { |
211 | 0 | int32_t count = 0; |
212 | 0 | if (fPrefixLength > 0) { |
213 | 0 | count += fCompiledPattern.countChar32(2, fPrefixLength); |
214 | 0 | } |
215 | 0 | if (fSuffixLength > 0) { |
216 | 0 | count += fCompiledPattern.countChar32(1 + fSuffixOffset, fSuffixLength); |
217 | 0 | } |
218 | 0 | return count; |
219 | 0 | } |
220 | | |
221 | 0 | bool SimpleModifier::isStrong() const { |
222 | 0 | return fStrong; |
223 | 0 | } |
224 | | |
225 | 0 | bool SimpleModifier::containsField(Field field) const { |
226 | 0 | (void)field; |
227 | | // This method is not currently used. |
228 | 0 | UPRV_UNREACHABLE_EXIT; |
229 | 0 | } |
230 | | |
231 | 0 | void SimpleModifier::getParameters(Parameters& output) const { |
232 | 0 | output = fParameters; |
233 | 0 | } |
234 | | |
235 | 0 | bool SimpleModifier::strictEquals(const Modifier& other) const { |
236 | 0 | const auto* _other = dynamic_cast<const SimpleModifier*>(&other); |
237 | 0 | if (_other == nullptr) { |
238 | 0 | return false; |
239 | 0 | } |
240 | 0 | return fCompiledPattern == _other->fCompiledPattern |
241 | 0 | && fField == _other->fField |
242 | 0 | && fStrong == _other->fStrong; |
243 | 0 | } |
244 | | |
245 | | |
246 | | int32_t |
247 | | SimpleModifier::formatAsPrefixSuffix(FormattedStringBuilder &result, int32_t startIndex, int32_t endIndex, |
248 | 1.67M | UErrorCode &status) const { |
249 | 1.67M | if (fSuffixOffset == -1 && fPrefixLength + fSuffixLength > 0) { |
250 | | // There is no argument for the inner number; overwrite the entire segment with our string. |
251 | 1.15k | return result.splice(startIndex, endIndex, fCompiledPattern, 2, 2 + fPrefixLength, fField, status); |
252 | 1.67M | } else { |
253 | 1.67M | if (fPrefixLength > 0) { |
254 | 1.49M | result.insert(startIndex, fCompiledPattern, 2, 2 + fPrefixLength, fField, status); |
255 | 1.49M | } |
256 | 1.67M | if (fSuffixLength > 0) { |
257 | 1.66M | result.insert( |
258 | 1.66M | endIndex + fPrefixLength, |
259 | 1.66M | fCompiledPattern, |
260 | 1.66M | 1 + fSuffixOffset, |
261 | 1.66M | 1 + fSuffixOffset + fSuffixLength, |
262 | 1.66M | fField, |
263 | 1.66M | status); |
264 | 1.66M | } |
265 | 1.67M | return fPrefixLength + fSuffixLength; |
266 | 1.67M | } |
267 | 1.67M | } |
268 | | |
269 | | |
270 | | int32_t |
271 | | SimpleModifier::formatTwoArgPattern(const SimpleFormatter& compiled, FormattedStringBuilder& result, |
272 | | int32_t index, int32_t* outPrefixLength, int32_t* outSuffixLength, |
273 | 0 | Field field, UErrorCode& status) { |
274 | 0 | const UnicodeString& compiledPattern = compiled.compiledPattern; |
275 | 0 | int32_t argLimit = SimpleFormatter::getArgumentLimit( |
276 | 0 | compiledPattern.getBuffer(), compiledPattern.length()); |
277 | 0 | if (argLimit != 2) { |
278 | 0 | status = U_INTERNAL_PROGRAM_ERROR; |
279 | 0 | return 0; |
280 | 0 | } |
281 | 0 | int32_t offset = 1; // offset into compiledPattern |
282 | 0 | int32_t length = 0; // chars added to result |
283 | |
|
284 | 0 | int32_t prefixLength = compiledPattern.charAt(offset); |
285 | 0 | offset++; |
286 | 0 | if (prefixLength < ARG_NUM_LIMIT) { |
287 | | // No prefix |
288 | 0 | prefixLength = 0; |
289 | 0 | } else { |
290 | 0 | prefixLength -= ARG_NUM_LIMIT; |
291 | 0 | result.insert(index + length, compiledPattern, offset, offset + prefixLength, field, status); |
292 | 0 | offset += prefixLength; |
293 | 0 | length += prefixLength; |
294 | 0 | offset++; |
295 | 0 | } |
296 | |
|
297 | 0 | int32_t infixLength = compiledPattern.charAt(offset); |
298 | 0 | offset++; |
299 | 0 | if (infixLength < ARG_NUM_LIMIT) { |
300 | | // No infix |
301 | 0 | infixLength = 0; |
302 | 0 | } else { |
303 | 0 | infixLength -= ARG_NUM_LIMIT; |
304 | 0 | result.insert(index + length, compiledPattern, offset, offset + infixLength, field, status); |
305 | 0 | offset += infixLength; |
306 | 0 | length += infixLength; |
307 | 0 | offset++; |
308 | 0 | } |
309 | |
|
310 | 0 | int32_t suffixLength; |
311 | 0 | if (offset == compiledPattern.length()) { |
312 | | // No suffix |
313 | 0 | suffixLength = 0; |
314 | 0 | } else { |
315 | 0 | suffixLength = compiledPattern.charAt(offset) - ARG_NUM_LIMIT; |
316 | 0 | offset++; |
317 | 0 | result.insert(index + length, compiledPattern, offset, offset + suffixLength, field, status); |
318 | 0 | length += suffixLength; |
319 | 0 | } |
320 | |
|
321 | 0 | *outPrefixLength = prefixLength; |
322 | 0 | *outSuffixLength = suffixLength; |
323 | |
|
324 | 0 | return length; |
325 | 0 | } |
326 | | |
327 | | |
328 | | int32_t ConstantMultiFieldModifier::apply(FormattedStringBuilder &output, int leftIndex, int rightIndex, |
329 | 1.77M | UErrorCode &status) const { |
330 | 1.77M | int32_t length = output.insert(leftIndex, fPrefix, status); |
331 | 1.77M | if (fOverwrite) { |
332 | 0 | length += output.splice( |
333 | 0 | leftIndex + length, |
334 | 0 | rightIndex + length, |
335 | 0 | UnicodeString(), 0, 0, |
336 | 0 | kUndefinedField, status); |
337 | 0 | } |
338 | 1.77M | length += output.insert(rightIndex + length, fSuffix, status); |
339 | 1.77M | return length; |
340 | 1.77M | } |
341 | | |
342 | 0 | int32_t ConstantMultiFieldModifier::getPrefixLength() const { |
343 | 0 | return fPrefix.length(); |
344 | 0 | } |
345 | | |
346 | 0 | int32_t ConstantMultiFieldModifier::getCodePointCount() const { |
347 | 0 | return fPrefix.codePointCount() + fSuffix.codePointCount(); |
348 | 0 | } |
349 | | |
350 | 0 | bool ConstantMultiFieldModifier::isStrong() const { |
351 | 0 | return fStrong; |
352 | 0 | } |
353 | | |
354 | 0 | bool ConstantMultiFieldModifier::containsField(Field field) const { |
355 | 0 | return fPrefix.containsField(field) || fSuffix.containsField(field); |
356 | 0 | } |
357 | | |
358 | 0 | void ConstantMultiFieldModifier::getParameters(Parameters& output) const { |
359 | 0 | output = fParameters; |
360 | 0 | } |
361 | | |
362 | 0 | bool ConstantMultiFieldModifier::strictEquals(const Modifier& other) const { |
363 | 0 | const auto* _other = dynamic_cast<const ConstantMultiFieldModifier*>(&other); |
364 | 0 | if (_other == nullptr) { |
365 | 0 | return false; |
366 | 0 | } |
367 | 0 | return fPrefix.contentEquals(_other->fPrefix) |
368 | 0 | && fSuffix.contentEquals(_other->fSuffix) |
369 | 0 | && fOverwrite == _other->fOverwrite |
370 | 0 | && fStrong == _other->fStrong; |
371 | 0 | } |
372 | | |
373 | | |
374 | | CurrencySpacingEnabledModifier::CurrencySpacingEnabledModifier(const FormattedStringBuilder &prefix, |
375 | | const FormattedStringBuilder &suffix, |
376 | | bool overwrite, |
377 | | bool strong, |
378 | | const DecimalFormatSymbols &symbols, |
379 | | UErrorCode &status) |
380 | 0 | : ConstantMultiFieldModifier(prefix, suffix, overwrite, strong) { |
381 | | // Check for currency spacing. Do not build the UnicodeSets unless there is |
382 | | // a currency code point at a boundary. |
383 | 0 | if (prefix.length() > 0 && prefix.fieldAt(prefix.length() - 1) == Field(UFIELD_CATEGORY_NUMBER, UNUM_CURRENCY_FIELD)) { |
384 | 0 | int prefixCp = prefix.getLastCodePoint(); |
385 | 0 | UnicodeSet prefixUnicodeSet = getUnicodeSet(symbols, IN_CURRENCY, PREFIX, status); |
386 | 0 | if (prefixUnicodeSet.contains(prefixCp)) { |
387 | 0 | fAfterPrefixUnicodeSet = getUnicodeSet(symbols, IN_NUMBER, PREFIX, status); |
388 | 0 | fAfterPrefixUnicodeSet.freeze(); |
389 | 0 | fAfterPrefixInsert = getInsertString(symbols, PREFIX, status); |
390 | 0 | } else { |
391 | 0 | fAfterPrefixUnicodeSet.setToBogus(); |
392 | 0 | fAfterPrefixInsert.setToBogus(); |
393 | 0 | } |
394 | 0 | } else { |
395 | 0 | fAfterPrefixUnicodeSet.setToBogus(); |
396 | 0 | fAfterPrefixInsert.setToBogus(); |
397 | 0 | } |
398 | 0 | if (suffix.length() > 0 && suffix.fieldAt(0) == Field(UFIELD_CATEGORY_NUMBER, UNUM_CURRENCY_FIELD)) { |
399 | 0 | int suffixCp = suffix.getFirstCodePoint(); |
400 | 0 | UnicodeSet suffixUnicodeSet = getUnicodeSet(symbols, IN_CURRENCY, SUFFIX, status); |
401 | 0 | if (suffixUnicodeSet.contains(suffixCp)) { |
402 | 0 | fBeforeSuffixUnicodeSet = getUnicodeSet(symbols, IN_NUMBER, SUFFIX, status); |
403 | 0 | fBeforeSuffixUnicodeSet.freeze(); |
404 | 0 | fBeforeSuffixInsert = getInsertString(symbols, SUFFIX, status); |
405 | 0 | } else { |
406 | 0 | fBeforeSuffixUnicodeSet.setToBogus(); |
407 | 0 | fBeforeSuffixInsert.setToBogus(); |
408 | 0 | } |
409 | 0 | } else { |
410 | 0 | fBeforeSuffixUnicodeSet.setToBogus(); |
411 | 0 | fBeforeSuffixInsert.setToBogus(); |
412 | 0 | } |
413 | 0 | } |
414 | | |
415 | | int32_t CurrencySpacingEnabledModifier::apply(FormattedStringBuilder &output, int leftIndex, int rightIndex, |
416 | 0 | UErrorCode &status) const { |
417 | | // Currency spacing logic |
418 | 0 | int length = 0; |
419 | 0 | if (rightIndex - leftIndex > 0 && !fAfterPrefixUnicodeSet.isBogus() && |
420 | 0 | fAfterPrefixUnicodeSet.contains(output.codePointAt(leftIndex))) { |
421 | | // TODO: Should we use the CURRENCY field here? |
422 | 0 | length += output.insert( |
423 | 0 | leftIndex, |
424 | 0 | fAfterPrefixInsert, |
425 | 0 | kUndefinedField, |
426 | 0 | status); |
427 | 0 | } |
428 | 0 | if (rightIndex - leftIndex > 0 && !fBeforeSuffixUnicodeSet.isBogus() && |
429 | 0 | fBeforeSuffixUnicodeSet.contains(output.codePointBefore(rightIndex))) { |
430 | | // TODO: Should we use the CURRENCY field here? |
431 | 0 | length += output.insert( |
432 | 0 | rightIndex + length, |
433 | 0 | fBeforeSuffixInsert, |
434 | 0 | kUndefinedField, |
435 | 0 | status); |
436 | 0 | } |
437 | | |
438 | | // Call super for the remaining logic |
439 | 0 | length += ConstantMultiFieldModifier::apply(output, leftIndex, rightIndex + length, status); |
440 | 0 | return length; |
441 | 0 | } |
442 | | |
443 | | int32_t |
444 | | CurrencySpacingEnabledModifier::applyCurrencySpacing(FormattedStringBuilder &output, int32_t prefixStart, |
445 | | int32_t prefixLen, int32_t suffixStart, |
446 | | int32_t suffixLen, |
447 | | const DecimalFormatSymbols &symbols, |
448 | 10.8k | UErrorCode &status) { |
449 | 10.8k | int length = 0; |
450 | 10.8k | bool hasPrefix = (prefixLen > 0); |
451 | 10.8k | bool hasSuffix = (suffixLen > 0); |
452 | 10.8k | bool hasNumber = (suffixStart - prefixStart - prefixLen > 0); // could be empty string |
453 | 10.8k | if (hasPrefix && hasNumber) { |
454 | 3.08k | length += applyCurrencySpacingAffix(output, prefixStart + prefixLen, PREFIX, symbols, status); |
455 | 3.08k | } |
456 | 10.8k | if (hasSuffix && hasNumber) { |
457 | 2.37k | length += applyCurrencySpacingAffix(output, suffixStart + length, SUFFIX, symbols, status); |
458 | 2.37k | } |
459 | 10.8k | return length; |
460 | 10.8k | } |
461 | | |
462 | | int32_t |
463 | | CurrencySpacingEnabledModifier::applyCurrencySpacingAffix(FormattedStringBuilder &output, int32_t index, |
464 | | EAffix affix, |
465 | | const DecimalFormatSymbols &symbols, |
466 | 5.46k | UErrorCode &status) { |
467 | | // NOTE: For prefix, output.fieldAt(index-1) gets the last field type in the prefix. |
468 | | // This works even if the last code point in the prefix is 2 code units because the |
469 | | // field value gets populated to both indices in the field array. |
470 | 5.46k | Field affixField = (affix == PREFIX) ? output.fieldAt(index - 1) : output.fieldAt(index); |
471 | 5.46k | if (affixField != Field(UFIELD_CATEGORY_NUMBER, UNUM_CURRENCY_FIELD)) { |
472 | 5.46k | return 0; |
473 | 5.46k | } |
474 | 0 | int affixCp = (affix == PREFIX) ? output.codePointBefore(index) : output.codePointAt(index); |
475 | 0 | UnicodeSet affixUniset = getUnicodeSet(symbols, IN_CURRENCY, affix, status); |
476 | 0 | if (!affixUniset.contains(affixCp)) { |
477 | 0 | return 0; |
478 | 0 | } |
479 | 0 | int numberCp = (affix == PREFIX) ? output.codePointAt(index) : output.codePointBefore(index); |
480 | 0 | UnicodeSet numberUniset = getUnicodeSet(symbols, IN_NUMBER, affix, status); |
481 | 0 | if (!numberUniset.contains(numberCp)) { |
482 | 0 | return 0; |
483 | 0 | } |
484 | 0 | UnicodeString spacingString = getInsertString(symbols, affix, status); |
485 | | |
486 | | // NOTE: This next line *inserts* the spacing string, triggering an arraycopy. |
487 | | // It would be more efficient if this could be done before affixes were attached, |
488 | | // so that it could be prepended/appended instead of inserted. |
489 | | // However, the build code path is more efficient, and this is the most natural |
490 | | // place to put currency spacing in the non-build code path. |
491 | | // TODO: Should we use the CURRENCY field here? |
492 | 0 | return output.insert(index, spacingString, kUndefinedField, status); |
493 | 0 | } |
494 | | |
495 | | UnicodeSet |
496 | | CurrencySpacingEnabledModifier::getUnicodeSet(const DecimalFormatSymbols &symbols, EPosition position, |
497 | 0 | EAffix affix, UErrorCode &status) { |
498 | | // Ensure the static defaults are initialized: |
499 | 0 | umtx_initOnce(gDefaultCurrencySpacingInitOnce, &initDefaultCurrencySpacing, status); |
500 | 0 | if (U_FAILURE(status)) { |
501 | 0 | return {}; |
502 | 0 | } |
503 | | |
504 | 0 | const UnicodeString& pattern = symbols.getPatternForCurrencySpacing( |
505 | 0 | position == IN_CURRENCY ? UNUM_CURRENCY_MATCH : UNUM_CURRENCY_SURROUNDING_MATCH, |
506 | 0 | affix == SUFFIX, |
507 | 0 | status); |
508 | 0 | if (pattern.compare(u"[:digit:]", -1) == 0) { |
509 | 0 | return *UNISET_DIGIT; |
510 | 0 | } else if (pattern.compare(u"[[:^S:]&[:^Z:]]", -1) == 0) { |
511 | 0 | return *UNISET_NOTSZ; |
512 | 0 | } else { |
513 | 0 | return UnicodeSet(pattern, status); |
514 | 0 | } |
515 | 0 | } |
516 | | |
517 | | UnicodeString |
518 | | CurrencySpacingEnabledModifier::getInsertString(const DecimalFormatSymbols &symbols, EAffix affix, |
519 | 0 | UErrorCode &status) { |
520 | 0 | return symbols.getPatternForCurrencySpacing(UNUM_CURRENCY_INSERT, affix == SUFFIX, status); |
521 | 0 | } |
522 | | |
523 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |