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 "uassert.h" |
9 | #include "unicode/numberformatter.h" |
10 | #include "number_decimalquantity.h" |
11 | #include "number_formatimpl.h" |
12 | #include "umutex.h" |
13 | #include "number_asformat.h" |
14 | #include "number_utils.h" |
15 | #include "number_utypes.h" |
16 | #include "util.h" |
17 | #include "fphdlimp.h" |
18 | |
19 | using namespace icu; |
20 | using namespace icu::number; |
21 | using namespace icu::number::impl; |
22 | |
23 | #if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER) |
24 | // Ignore MSVC warning 4661. This is generated for NumberFormatterSettings<>::toSkeleton() as this method |
25 | // is defined elsewhere (in number_skeletons.cpp). The compiler is warning that the explicit template instantiation |
26 | // inside this single translation unit (CPP file) is incomplete, and thus it isn't sure if the template class is |
27 | // fully defined. However, since each translation unit explicitly instantiates all the necessary template classes, |
28 | // they will all be passed to the linker, and the linker will still find and export all the class members. |
29 | #pragma warning(push) |
30 | #pragma warning(disable: 4661) |
31 | #endif |
32 | |
33 | template<typename Derived> |
34 | Derived NumberFormatterSettings<Derived>::notation(const Notation& notation) const& { |
35 | Derived copy(*this); |
36 | // NOTE: Slicing is OK. |
37 | copy.fMacros.notation = notation; |
38 | return copy; |
39 | } |
40 | |
41 | template<typename Derived> |
42 | Derived NumberFormatterSettings<Derived>::notation(const Notation& notation)&& { |
43 | Derived move(std::move(*this)); |
44 | // NOTE: Slicing is OK. |
45 | move.fMacros.notation = notation; |
46 | return move; |
47 | } |
48 | |
49 | template<typename Derived> |
50 | Derived NumberFormatterSettings<Derived>::unit(const icu::MeasureUnit& unit) const& { |
51 | Derived copy(*this); |
52 | // NOTE: Slicing occurs here. However, CurrencyUnit can be restored from MeasureUnit. |
53 | // TimeUnit may be affected, but TimeUnit is not as relevant to number formatting. |
54 | copy.fMacros.unit = unit; |
55 | return copy; |
56 | } |
57 | |
58 | template<typename Derived> |
59 | Derived NumberFormatterSettings<Derived>::unit(const icu::MeasureUnit& unit)&& { |
60 | Derived move(std::move(*this)); |
61 | // See comments above about slicing. |
62 | move.fMacros.unit = unit; |
63 | return move; |
64 | } |
65 | |
66 | template<typename Derived> |
67 | Derived NumberFormatterSettings<Derived>::adoptUnit(icu::MeasureUnit* unit) const& { |
68 | Derived copy(*this); |
69 | // Just move the unit into the MacroProps by value, and delete it since we have ownership. |
70 | // NOTE: Slicing occurs here. However, CurrencyUnit can be restored from MeasureUnit. |
71 | // TimeUnit may be affected, but TimeUnit is not as relevant to number formatting. |
72 | if (unit != nullptr) { |
73 | // TODO: On nullptr, reset to default value? |
74 | copy.fMacros.unit = std::move(*unit); |
75 | delete unit; |
76 | } |
77 | return copy; |
78 | } |
79 | |
80 | template<typename Derived> |
81 | Derived NumberFormatterSettings<Derived>::adoptUnit(icu::MeasureUnit* unit)&& { |
82 | Derived move(std::move(*this)); |
83 | // See comments above about slicing and ownership. |
84 | if (unit != nullptr) { |
85 | // TODO: On nullptr, reset to default value? |
86 | move.fMacros.unit = std::move(*unit); |
87 | delete unit; |
88 | } |
89 | return move; |
90 | } |
91 | |
92 | template<typename Derived> |
93 | Derived NumberFormatterSettings<Derived>::perUnit(const icu::MeasureUnit& perUnit) const& { |
94 | Derived copy(*this); |
95 | // See comments above about slicing. |
96 | copy.fMacros.perUnit = perUnit; |
97 | return copy; |
98 | } |
99 | |
100 | template<typename Derived> |
101 | Derived NumberFormatterSettings<Derived>::perUnit(const icu::MeasureUnit& perUnit)&& { |
102 | Derived move(std::move(*this)); |
103 | // See comments above about slicing. |
104 | move.fMacros.perUnit = perUnit; |
105 | return move; |
106 | } |
107 | |
108 | template<typename Derived> |
109 | Derived NumberFormatterSettings<Derived>::adoptPerUnit(icu::MeasureUnit* perUnit) const& { |
110 | Derived copy(*this); |
111 | // See comments above about slicing and ownership. |
112 | if (perUnit != nullptr) { |
113 | // TODO: On nullptr, reset to default value? |
114 | copy.fMacros.perUnit = std::move(*perUnit); |
115 | delete perUnit; |
116 | } |
117 | return copy; |
118 | } |
119 | |
120 | template<typename Derived> |
121 | Derived NumberFormatterSettings<Derived>::adoptPerUnit(icu::MeasureUnit* perUnit)&& { |
122 | Derived move(std::move(*this)); |
123 | // See comments above about slicing and ownership. |
124 | if (perUnit != nullptr) { |
125 | // TODO: On nullptr, reset to default value? |
126 | move.fMacros.perUnit = std::move(*perUnit); |
127 | delete perUnit; |
128 | } |
129 | return move; |
130 | } |
131 | |
132 | template<typename Derived> |
133 | Derived NumberFormatterSettings<Derived>::precision(const Precision& precision) const& { |
134 | Derived copy(*this); |
135 | // NOTE: Slicing is OK. |
136 | copy.fMacros.precision = precision; |
137 | return copy; |
138 | } |
139 | |
140 | template<typename Derived> |
141 | Derived NumberFormatterSettings<Derived>::precision(const Precision& precision)&& { |
142 | Derived move(std::move(*this)); |
143 | // NOTE: Slicing is OK. |
144 | move.fMacros.precision = precision; |
145 | return move; |
146 | } |
147 | |
148 | template<typename Derived> |
149 | Derived NumberFormatterSettings<Derived>::roundingMode(UNumberFormatRoundingMode roundingMode) const& { |
150 | Derived copy(*this); |
151 | copy.fMacros.roundingMode = roundingMode; |
152 | return copy; |
153 | } |
154 | |
155 | template<typename Derived> |
156 | Derived NumberFormatterSettings<Derived>::roundingMode(UNumberFormatRoundingMode roundingMode)&& { |
157 | Derived move(std::move(*this)); |
158 | move.fMacros.roundingMode = roundingMode; |
159 | return move; |
160 | } |
161 | |
162 | template<typename Derived> |
163 | Derived NumberFormatterSettings<Derived>::grouping(UNumberGroupingStrategy strategy) const& { |
164 | Derived copy(*this); |
165 | // NOTE: This is slightly different than how the setting is stored in Java |
166 | // because we want to put it on the stack. |
167 | copy.fMacros.grouper = Grouper::forStrategy(strategy); |
168 | return copy; |
169 | } |
170 | |
171 | template<typename Derived> |
172 | Derived NumberFormatterSettings<Derived>::grouping(UNumberGroupingStrategy strategy)&& { |
173 | Derived move(std::move(*this)); |
174 | move.fMacros.grouper = Grouper::forStrategy(strategy); |
175 | return move; |
176 | } |
177 | |
178 | template<typename Derived> |
179 | Derived NumberFormatterSettings<Derived>::integerWidth(const IntegerWidth& style) const& { |
180 | Derived copy(*this); |
181 | copy.fMacros.integerWidth = style; |
182 | return copy; |
183 | } |
184 | |
185 | template<typename Derived> |
186 | Derived NumberFormatterSettings<Derived>::integerWidth(const IntegerWidth& style)&& { |
187 | Derived move(std::move(*this)); |
188 | move.fMacros.integerWidth = style; |
189 | return move; |
190 | } |
191 | |
192 | template<typename Derived> |
193 | Derived NumberFormatterSettings<Derived>::symbols(const DecimalFormatSymbols& symbols) const& { |
194 | Derived copy(*this); |
195 | copy.fMacros.symbols.setTo(symbols); |
196 | return copy; |
197 | } |
198 | |
199 | template<typename Derived> |
200 | Derived NumberFormatterSettings<Derived>::symbols(const DecimalFormatSymbols& symbols)&& { |
201 | Derived move(std::move(*this)); |
202 | move.fMacros.symbols.setTo(symbols); |
203 | return move; |
204 | } |
205 | |
206 | template<typename Derived> |
207 | Derived NumberFormatterSettings<Derived>::adoptSymbols(NumberingSystem* ns) const& { |
208 | Derived copy(*this); |
209 | copy.fMacros.symbols.setTo(ns); |
210 | return copy; |
211 | } |
212 | |
213 | template<typename Derived> |
214 | Derived NumberFormatterSettings<Derived>::adoptSymbols(NumberingSystem* ns)&& { |
215 | Derived move(std::move(*this)); |
216 | move.fMacros.symbols.setTo(ns); |
217 | return move; |
218 | } |
219 | |
220 | template<typename Derived> |
221 | Derived NumberFormatterSettings<Derived>::unitWidth(UNumberUnitWidth width) const& { |
222 | Derived copy(*this); |
223 | copy.fMacros.unitWidth = width; |
224 | return copy; |
225 | } |
226 | |
227 | template<typename Derived> |
228 | Derived NumberFormatterSettings<Derived>::unitWidth(UNumberUnitWidth width)&& { |
229 | Derived move(std::move(*this)); |
230 | move.fMacros.unitWidth = width; |
231 | return move; |
232 | } |
233 | |
234 | template<typename Derived> |
235 | Derived NumberFormatterSettings<Derived>::sign(UNumberSignDisplay style) const& { |
236 | Derived copy(*this); |
237 | copy.fMacros.sign = style; |
238 | return copy; |
239 | } |
240 | |
241 | template<typename Derived> |
242 | Derived NumberFormatterSettings<Derived>::sign(UNumberSignDisplay style)&& { |
243 | Derived move(std::move(*this)); |
244 | move.fMacros.sign = style; |
245 | return move; |
246 | } |
247 | |
248 | template<typename Derived> |
249 | Derived NumberFormatterSettings<Derived>::decimal(UNumberDecimalSeparatorDisplay style) const& { |
250 | Derived copy(*this); |
251 | copy.fMacros.decimal = style; |
252 | return copy; |
253 | } |
254 | |
255 | template<typename Derived> |
256 | Derived NumberFormatterSettings<Derived>::decimal(UNumberDecimalSeparatorDisplay style)&& { |
257 | Derived move(std::move(*this)); |
258 | move.fMacros.decimal = style; |
259 | return move; |
260 | } |
261 | |
262 | template<typename Derived> |
263 | Derived NumberFormatterSettings<Derived>::scale(const Scale& scale) const& { |
264 | Derived copy(*this); |
265 | copy.fMacros.scale = scale; |
266 | return copy; |
267 | } |
268 | |
269 | template<typename Derived> |
270 | Derived NumberFormatterSettings<Derived>::scale(const Scale& scale)&& { |
271 | Derived move(std::move(*this)); |
272 | move.fMacros.scale = scale; |
273 | return move; |
274 | } |
275 | |
276 | template<typename Derived> |
277 | Derived NumberFormatterSettings<Derived>::padding(const Padder& padder) const& { |
278 | Derived copy(*this); |
279 | copy.fMacros.padder = padder; |
280 | return copy; |
281 | } |
282 | |
283 | template<typename Derived> |
284 | Derived NumberFormatterSettings<Derived>::padding(const Padder& padder)&& { |
285 | Derived move(std::move(*this)); |
286 | move.fMacros.padder = padder; |
287 | return move; |
288 | } |
289 | |
290 | template<typename Derived> |
291 | Derived NumberFormatterSettings<Derived>::threshold(int32_t threshold) const& { |
292 | Derived copy(*this); |
293 | copy.fMacros.threshold = threshold; |
294 | return copy; |
295 | } |
296 | |
297 | template<typename Derived> |
298 | Derived NumberFormatterSettings<Derived>::threshold(int32_t threshold)&& { |
299 | Derived move(std::move(*this)); |
300 | move.fMacros.threshold = threshold; |
301 | return move; |
302 | } |
303 | |
304 | template<typename Derived> |
305 | Derived NumberFormatterSettings<Derived>::macros(const impl::MacroProps& macros) const& { |
306 | Derived copy(*this); |
307 | copy.fMacros = macros; |
308 | return copy; |
309 | } |
310 | |
311 | template<typename Derived> |
312 | Derived NumberFormatterSettings<Derived>::macros(const impl::MacroProps& macros)&& { |
313 | Derived move(std::move(*this)); |
314 | move.fMacros = macros; |
315 | return move; |
316 | } |
317 | |
318 | template<typename Derived> |
319 | Derived NumberFormatterSettings<Derived>::macros(impl::MacroProps&& macros) const& { |
320 | Derived copy(*this); |
321 | copy.fMacros = std::move(macros); |
322 | return copy; |
323 | } |
324 | |
325 | template<typename Derived> |
326 | Derived NumberFormatterSettings<Derived>::macros(impl::MacroProps&& macros)&& { |
327 | Derived move(std::move(*this)); |
328 | move.fMacros = std::move(macros); |
329 | return move; |
330 | } |
331 | |
332 | // Note: toSkeleton defined in number_skeletons.cpp |
333 | |
334 | template<typename Derived> |
335 | LocalPointer<Derived> NumberFormatterSettings<Derived>::clone() const & { |
336 | return LocalPointer<Derived>(new Derived(*this)); |
337 | } |
338 | |
339 | template<typename Derived> |
340 | LocalPointer<Derived> NumberFormatterSettings<Derived>::clone() && { |
341 | return LocalPointer<Derived>(new Derived(std::move(*this))); |
342 | } |
343 | |
344 | // Declare all classes that implement NumberFormatterSettings |
345 | // See https://stackoverflow.com/a/495056/1407170 |
346 | template |
347 | class icu::number::NumberFormatterSettings<icu::number::UnlocalizedNumberFormatter>; |
348 | template |
349 | class icu::number::NumberFormatterSettings<icu::number::LocalizedNumberFormatter>; |
350 | |
351 | |
352 | UnlocalizedNumberFormatter NumberFormatter::with() { |
353 | UnlocalizedNumberFormatter result; |
354 | return result; |
355 | } |
356 | |
357 | LocalizedNumberFormatter NumberFormatter::withLocale(const Locale& locale) { |
358 | return with().locale(locale); |
359 | } |
360 | |
361 | // Note: forSkeleton defined in number_skeletons.cpp |
362 | |
363 | |
364 | template<typename T> using NFS = NumberFormatterSettings<T>; |
365 | using LNF = LocalizedNumberFormatter; |
366 | using UNF = UnlocalizedNumberFormatter; |
367 | |
368 | UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(const UNF& other) |
369 | : UNF(static_cast<const NFS<UNF>&>(other)) {} |
370 | |
371 | UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(const NFS<UNF>& other) |
372 | : NFS<UNF>(other) { |
373 | // No additional fields to assign |
374 | } |
375 | |
376 | // Make default copy constructor call the NumberFormatterSettings copy constructor. |
377 | UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(UNF&& src) U_NOEXCEPT |
378 | : UNF(static_cast<NFS<UNF>&&>(src)) {} |
379 | |
380 | UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(NFS<UNF>&& src) U_NOEXCEPT |
381 | : NFS<UNF>(std::move(src)) { |
382 | // No additional fields to assign |
383 | } |
384 | |
385 | UnlocalizedNumberFormatter& UnlocalizedNumberFormatter::operator=(const UNF& other) { |
386 | NFS<UNF>::operator=(static_cast<const NFS<UNF>&>(other)); |
387 | // No additional fields to assign |
388 | return *this; |
389 | } |
390 | |
391 | UnlocalizedNumberFormatter& UnlocalizedNumberFormatter::operator=(UNF&& src) U_NOEXCEPT { |
392 | NFS<UNF>::operator=(static_cast<NFS<UNF>&&>(src)); |
393 | // No additional fields to assign |
394 | return *this; |
395 | } |
396 | |
397 | // Make default copy constructor call the NumberFormatterSettings copy constructor. |
398 | LocalizedNumberFormatter::LocalizedNumberFormatter(const LNF& other) |
399 | : LNF(static_cast<const NFS<LNF>&>(other)) {} |
400 | |
401 | LocalizedNumberFormatter::LocalizedNumberFormatter(const NFS<LNF>& other) |
402 | : NFS<LNF>(other) { |
403 | // No additional fields to assign (let call count and compiled formatter reset to defaults) |
404 | } |
405 | |
406 | LocalizedNumberFormatter::LocalizedNumberFormatter(LocalizedNumberFormatter&& src) U_NOEXCEPT |
407 | : LNF(static_cast<NFS<LNF>&&>(src)) {} |
408 | |
409 | LocalizedNumberFormatter::LocalizedNumberFormatter(NFS<LNF>&& src) U_NOEXCEPT |
410 | : NFS<LNF>(std::move(src)) { |
411 | // For the move operators, copy over the compiled formatter. |
412 | // Note: if the formatter is not compiled, call count information is lost. |
413 | if (static_cast<LNF&&>(src).fCompiled != nullptr) { |
414 | lnfMoveHelper(static_cast<LNF&&>(src)); |
415 | } |
416 | } |
417 | |
418 | LocalizedNumberFormatter& LocalizedNumberFormatter::operator=(const LNF& other) { |
419 | NFS<LNF>::operator=(static_cast<const NFS<LNF>&>(other)); |
420 | // Reset to default values. |
421 | clear(); |
422 | return *this; |
423 | } |
424 | |
425 | LocalizedNumberFormatter& LocalizedNumberFormatter::operator=(LNF&& src) U_NOEXCEPT { |
426 | NFS<LNF>::operator=(static_cast<NFS<LNF>&&>(src)); |
427 | // For the move operators, copy over the compiled formatter. |
428 | // Note: if the formatter is not compiled, call count information is lost. |
429 | if (static_cast<LNF&&>(src).fCompiled != nullptr) { |
430 | // Formatter is compiled |
431 | lnfMoveHelper(static_cast<LNF&&>(src)); |
432 | } else { |
433 | clear(); |
434 | } |
435 | return *this; |
436 | } |
437 | |
438 | void LocalizedNumberFormatter::clear() { |
439 | // Reset to default values. |
440 | auto* callCount = reinterpret_cast<u_atomic_int32_t*>(fUnsafeCallCount); |
441 | umtx_storeRelease(*callCount, 0); |
442 | delete fCompiled; |
443 | fCompiled = nullptr; |
444 | } |
445 | |
446 | void LocalizedNumberFormatter::lnfMoveHelper(LNF&& src) { |
447 | // Copy over the compiled formatter and set call count to INT32_MIN as in computeCompiled(). |
448 | // Don't copy the call count directly because doing so requires a loadAcquire/storeRelease. |
449 | // The bits themselves appear to be platform-dependent, so copying them might not be safe. |
450 | auto* callCount = reinterpret_cast<u_atomic_int32_t*>(fUnsafeCallCount); |
451 | umtx_storeRelease(*callCount, INT32_MIN); |
452 | delete fCompiled; |
453 | fCompiled = src.fCompiled; |
454 | // Reset the source object to leave it in a safe state. |
455 | auto* srcCallCount = reinterpret_cast<u_atomic_int32_t*>(src.fUnsafeCallCount); |
456 | umtx_storeRelease(*srcCallCount, 0); |
457 | src.fCompiled = nullptr; |
458 | } |
459 | |
460 | |
461 | LocalizedNumberFormatter::~LocalizedNumberFormatter() { |
462 | delete fCompiled; |
463 | } |
464 | |
465 | LocalizedNumberFormatter::LocalizedNumberFormatter(const MacroProps& macros, const Locale& locale) { |
466 | fMacros = macros; |
467 | fMacros.locale = locale; |
468 | } |
469 | |
470 | LocalizedNumberFormatter::LocalizedNumberFormatter(MacroProps&& macros, const Locale& locale) { |
471 | fMacros = std::move(macros); |
472 | fMacros.locale = locale; |
473 | } |
474 | |
475 | LocalizedNumberFormatter UnlocalizedNumberFormatter::locale(const Locale& locale) const& { |
476 | return LocalizedNumberFormatter(fMacros, locale); |
477 | } |
478 | |
479 | LocalizedNumberFormatter UnlocalizedNumberFormatter::locale(const Locale& locale)&& { |
480 | return LocalizedNumberFormatter(std::move(fMacros), locale); |
481 | } |
482 | |
483 | SymbolsWrapper::SymbolsWrapper(const SymbolsWrapper& other) { |
484 | doCopyFrom(other); |
485 | } |
486 | |
487 | SymbolsWrapper::SymbolsWrapper(SymbolsWrapper&& src) U_NOEXCEPT { |
488 | doMoveFrom(std::move(src)); |
489 | } |
490 | |
491 | SymbolsWrapper& SymbolsWrapper::operator=(const SymbolsWrapper& other) { |
492 | if (this == &other) { |
493 | return *this; |
494 | } |
495 | doCleanup(); |
496 | doCopyFrom(other); |
497 | return *this; |
498 | } |
499 | |
500 | SymbolsWrapper& SymbolsWrapper::operator=(SymbolsWrapper&& src) U_NOEXCEPT { |
501 | if (this == &src) { |
502 | return *this; |
503 | } |
504 | doCleanup(); |
505 | doMoveFrom(std::move(src)); |
506 | return *this; |
507 | } |
508 | |
509 | SymbolsWrapper::~SymbolsWrapper() { |
510 | doCleanup(); |
511 | } |
512 | |
513 | void SymbolsWrapper::setTo(const DecimalFormatSymbols& dfs) { |
514 | doCleanup(); |
515 | fType = SYMPTR_DFS; |
516 | fPtr.dfs = new DecimalFormatSymbols(dfs); |
517 | } |
518 | |
519 | void SymbolsWrapper::setTo(const NumberingSystem* ns) { |
520 | doCleanup(); |
521 | fType = SYMPTR_NS; |
522 | fPtr.ns = ns; |
523 | } |
524 | |
525 | void SymbolsWrapper::doCopyFrom(const SymbolsWrapper& other) { |
526 | fType = other.fType; |
527 | switch (fType) { |
528 | case SYMPTR_NONE: |
529 | // No action necessary |
530 | break; |
531 | case SYMPTR_DFS: |
532 | // Memory allocation failures are exposed in copyErrorTo() |
533 | if (other.fPtr.dfs != nullptr) { |
534 | fPtr.dfs = new DecimalFormatSymbols(*other.fPtr.dfs); |
535 | } else { |
536 | fPtr.dfs = nullptr; |
537 | } |
538 | break; |
539 | case SYMPTR_NS: |
540 | // Memory allocation failures are exposed in copyErrorTo() |
541 | if (other.fPtr.ns != nullptr) { |
542 | fPtr.ns = new NumberingSystem(*other.fPtr.ns); |
543 | } else { |
544 | fPtr.ns = nullptr; |
545 | } |
546 | break; |
547 | } |
548 | } |
549 | |
550 | void SymbolsWrapper::doMoveFrom(SymbolsWrapper&& src) { |
551 | fType = src.fType; |
552 | switch (fType) { |
553 | case SYMPTR_NONE: |
554 | // No action necessary |
555 | break; |
556 | case SYMPTR_DFS: |
557 | fPtr.dfs = src.fPtr.dfs; |
558 | src.fPtr.dfs = nullptr; |
559 | break; |
560 | case SYMPTR_NS: |
561 | fPtr.ns = src.fPtr.ns; |
562 | src.fPtr.ns = nullptr; |
563 | break; |
564 | } |
565 | } |
566 | |
567 | void SymbolsWrapper::doCleanup() { |
568 | switch (fType) { |
569 | case SYMPTR_NONE: |
570 | // No action necessary |
571 | break; |
572 | case SYMPTR_DFS: |
573 | delete fPtr.dfs; |
574 | break; |
575 | case SYMPTR_NS: |
576 | delete fPtr.ns; |
577 | break; |
578 | } |
579 | } |
580 | |
581 | bool SymbolsWrapper::isDecimalFormatSymbols() const { |
582 | return fType == SYMPTR_DFS; |
583 | } |
584 | |
585 | bool SymbolsWrapper::isNumberingSystem() const { |
586 | return fType == SYMPTR_NS; |
587 | } |
588 | |
589 | const DecimalFormatSymbols* SymbolsWrapper::getDecimalFormatSymbols() const { |
590 | U_ASSERT(fType == SYMPTR_DFS); |
591 | return fPtr.dfs; |
592 | } |
593 | |
594 | const NumberingSystem* SymbolsWrapper::getNumberingSystem() const { |
595 | U_ASSERT(fType == SYMPTR_NS); |
596 | return fPtr.ns; |
597 | } |
598 | |
599 | |
600 | FormattedNumber LocalizedNumberFormatter::formatInt(int64_t value, UErrorCode& status) const { |
601 | if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); } |
602 | auto results = new UFormattedNumberData(); |
603 | if (results == nullptr) { |
604 | status = U_MEMORY_ALLOCATION_ERROR; |
605 | return FormattedNumber(status); |
606 | } |
607 | results->quantity.setToLong(value); |
608 | formatImpl(results, status); |
609 | |
610 | // Do not save the results object if we encountered a failure. |
611 | if (U_SUCCESS(status)) { |
612 | return FormattedNumber(results); |
613 | } else { |
614 | delete results; |
615 | return FormattedNumber(status); |
616 | } |
617 | } |
618 | |
619 | FormattedNumber LocalizedNumberFormatter::formatDouble(double value, UErrorCode& status) const { |
620 | if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); } |
621 | auto results = new UFormattedNumberData(); |
622 | if (results == nullptr) { |
623 | status = U_MEMORY_ALLOCATION_ERROR; |
624 | return FormattedNumber(status); |
625 | } |
626 | results->quantity.setToDouble(value); |
627 | formatImpl(results, status); |
628 | |
629 | // Do not save the results object if we encountered a failure. |
630 | if (U_SUCCESS(status)) { |
631 | return FormattedNumber(results); |
632 | } else { |
633 | delete results; |
634 | return FormattedNumber(status); |
635 | } |
636 | } |
637 | |
638 | FormattedNumber LocalizedNumberFormatter::formatDecimal(StringPiece value, UErrorCode& status) const { |
639 | if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); } |
640 | auto results = new UFormattedNumberData(); |
641 | if (results == nullptr) { |
642 | status = U_MEMORY_ALLOCATION_ERROR; |
643 | return FormattedNumber(status); |
644 | } |
645 | results->quantity.setToDecNumber(value, status); |
646 | formatImpl(results, status); |
647 | |
648 | // Do not save the results object if we encountered a failure. |
649 | if (U_SUCCESS(status)) { |
650 | return FormattedNumber(results); |
651 | } else { |
652 | delete results; |
653 | return FormattedNumber(status); |
654 | } |
655 | } |
656 | |
657 | FormattedNumber |
658 | LocalizedNumberFormatter::formatDecimalQuantity(const DecimalQuantity& dq, UErrorCode& status) const { |
659 | if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); } |
660 | auto results = new UFormattedNumberData(); |
661 | if (results == nullptr) { |
662 | status = U_MEMORY_ALLOCATION_ERROR; |
663 | return FormattedNumber(status); |
664 | } |
665 | results->quantity = dq; |
666 | formatImpl(results, status); |
667 | |
668 | // Do not save the results object if we encountered a failure. |
669 | if (U_SUCCESS(status)) { |
670 | return FormattedNumber(results); |
671 | } else { |
672 | delete results; |
673 | return FormattedNumber(status); |
674 | } |
675 | } |
676 | |
677 | void LocalizedNumberFormatter::formatImpl(impl::UFormattedNumberData* results, UErrorCode& status) const { |
678 | if (computeCompiled(status)) { |
679 | fCompiled->format(results->quantity, results->getStringRef(), status); |
680 | } else { |
681 | NumberFormatterImpl::formatStatic(fMacros, results->quantity, results->getStringRef(), status); |
682 | } |
683 | if (U_FAILURE(status)) { |
684 | return; |
685 | } |
686 | results->getStringRef().writeTerminator(status); |
687 | } |
688 | |
689 | void LocalizedNumberFormatter::getAffixImpl(bool isPrefix, bool isNegative, UnicodeString& result, |
690 | UErrorCode& status) const { |
691 | FormattedStringBuilder string; |
692 | auto signum = static_cast<Signum>(isNegative ? SIGNUM_NEG : SIGNUM_POS); |
693 | // Always return affixes for plural form OTHER. |
694 | static const StandardPlural::Form plural = StandardPlural::OTHER; |
695 | int32_t prefixLength; |
696 | if (computeCompiled(status)) { |
697 | prefixLength = fCompiled->getPrefixSuffix(signum, plural, string, status); |
698 | } else { |
699 | prefixLength = NumberFormatterImpl::getPrefixSuffixStatic(fMacros, signum, plural, string, status); |
700 | } |
701 | result.remove(); |
702 | if (isPrefix) { |
703 | result.append(string.toTempUnicodeString().tempSubStringBetween(0, prefixLength)); |
704 | } else { |
705 | result.append(string.toTempUnicodeString().tempSubStringBetween(prefixLength, string.length())); |
706 | } |
707 | } |
708 | |
709 | bool LocalizedNumberFormatter::computeCompiled(UErrorCode& status) const { |
710 | // fUnsafeCallCount contains memory to be interpreted as an atomic int, most commonly |
711 | // std::atomic<int32_t>. Since the type of atomic int is platform-dependent, we cast the |
712 | // bytes in fUnsafeCallCount to u_atomic_int32_t, a typedef for the platform-dependent |
713 | // atomic int type defined in umutex.h. |
714 | static_assert( |
715 | sizeof(u_atomic_int32_t) <= sizeof(fUnsafeCallCount), |
716 | "Atomic integer size on this platform exceeds the size allocated by fUnsafeCallCount" ); |
717 | auto* callCount = reinterpret_cast<u_atomic_int32_t*>( |
718 | const_cast<LocalizedNumberFormatter*>(this)->fUnsafeCallCount); |
719 | |
720 | // A positive value in the atomic int indicates that the data structure is not yet ready; |
721 | // a negative value indicates that it is ready. If, after the increment, the atomic int |
722 | // is exactly threshold, then it is the current thread's job to build the data structure. |
723 | // Note: We set the callCount to INT32_MIN so that if another thread proceeds to increment |
724 | // the atomic int, the value remains below zero. |
725 | int32_t currentCount = umtx_loadAcquire(*callCount); |
726 | if (0 <= currentCount && currentCount <= fMacros.threshold && fMacros.threshold > 0) { |
727 | currentCount = umtx_atomic_inc(callCount); |
728 | } |
729 | |
730 | if (currentCount == fMacros.threshold && fMacros.threshold > 0) { |
731 | // Build the data structure and then use it (slow to fast path). |
732 | const NumberFormatterImpl* compiled = new NumberFormatterImpl(fMacros, status); |
733 | if (compiled == nullptr) { |
734 | status = U_MEMORY_ALLOCATION_ERROR; |
735 | return false; |
736 | } |
737 | U_ASSERT(fCompiled == nullptr); |
738 | const_cast<LocalizedNumberFormatter*>(this)->fCompiled = compiled; |
739 | umtx_storeRelease(*callCount, INT32_MIN); |
740 | return true; |
741 | } else if (currentCount < 0) { |
742 | // The data structure is already built; use it (fast path). |
743 | U_ASSERT(fCompiled != nullptr); |
744 | return true; |
745 | } else { |
746 | // Format the number without building the data structure (slow path). |
747 | return false; |
748 | } |
749 | } |
750 | |
751 | const impl::NumberFormatterImpl* LocalizedNumberFormatter::getCompiled() const { |
752 | return fCompiled; |
753 | } |
754 | |
755 | int32_t LocalizedNumberFormatter::getCallCount() const { |
756 | auto* callCount = reinterpret_cast<u_atomic_int32_t*>( |
757 | const_cast<LocalizedNumberFormatter*>(this)->fUnsafeCallCount); |
758 | return umtx_loadAcquire(*callCount); |
759 | } |
760 | |
761 | // Note: toFormat defined in number_asformat.cpp |
762 | |
763 | #if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER) |
764 | // Warning 4661. |
765 | #pragma warning(pop) |
766 | #endif |
767 | |
768 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
769 | |