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