blob: fd486afb5122498b084ecd93a28de030f473731b [file] [log] [blame]
Jungshik Shinb3189662017-11-07 11:18:34 -08001// © 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
Jungshik Shina9a2bd32018-07-07 03:36:01 -07006#if !UCONFIG_NO_FORMATTING
Jungshik Shinb3189662017-11-07 11:18:34 -08007
8#include "uassert.h"
9#include "unicode/numberformatter.h"
10#include "number_decimalquantity.h"
11#include "number_formatimpl.h"
12#include "umutex.h"
Jungshik Shina9a2bd32018-07-07 03:36:01 -070013#include "number_asformat.h"
Jungshik Shina9a2bd32018-07-07 03:36:01 -070014#include "number_utils.h"
15#include "number_utypes.h"
Frank Tangf90543d2020-10-30 19:02:04 -070016#include "number_mapper.h"
Jungshik Shina9a2bd32018-07-07 03:36:01 -070017#include "util.h"
18#include "fphdlimp.h"
Jungshik Shinb3189662017-11-07 11:18:34 -080019
20using namespace icu;
21using namespace icu::number;
22using namespace icu::number::impl;
23
Frank Tangf2223962020-04-27 18:25:29 -070024#if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
25// Ignore MSVC warning 4661. This is generated for NumberFormatterSettings<>::toSkeleton() as this method
26// is defined elsewhere (in number_skeletons.cpp). The compiler is warning that the explicit template instantiation
27// inside this single translation unit (CPP file) is incomplete, and thus it isn't sure if the template class is
28// fully defined. However, since each translation unit explicitly instantiates all the necessary template classes,
29// they will all be passed to the linker, and the linker will still find and export all the class members.
30#pragma warning(push)
31#pragma warning(disable: 4661)
32#endif
33
Jungshik Shinb3189662017-11-07 11:18:34 -080034template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -070035Derived NumberFormatterSettings<Derived>::notation(const Notation& notation) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -080036 Derived copy(*this);
37 // NOTE: Slicing is OK.
38 copy.fMacros.notation = notation;
39 return copy;
40}
41
42template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -070043Derived NumberFormatterSettings<Derived>::notation(const Notation& notation)&& {
44 Derived move(std::move(*this));
45 // NOTE: Slicing is OK.
46 move.fMacros.notation = notation;
47 return move;
48}
49
50template<typename Derived>
51Derived NumberFormatterSettings<Derived>::unit(const icu::MeasureUnit& unit) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -080052 Derived copy(*this);
53 // NOTE: Slicing occurs here. However, CurrencyUnit can be restored from MeasureUnit.
54 // TimeUnit may be affected, but TimeUnit is not as relevant to number formatting.
55 copy.fMacros.unit = unit;
56 return copy;
57}
58
59template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -070060Derived NumberFormatterSettings<Derived>::unit(const icu::MeasureUnit& unit)&& {
61 Derived move(std::move(*this));
62 // See comments above about slicing.
63 move.fMacros.unit = unit;
64 return move;
65}
66
67template<typename Derived>
68Derived NumberFormatterSettings<Derived>::adoptUnit(icu::MeasureUnit* unit) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -080069 Derived copy(*this);
Jungshik Shina9a2bd32018-07-07 03:36:01 -070070 // Just move the unit into the MacroProps by value, and delete it since we have ownership.
Jungshik Shinb3189662017-11-07 11:18:34 -080071 // NOTE: Slicing occurs here. However, CurrencyUnit can be restored from MeasureUnit.
72 // TimeUnit may be affected, but TimeUnit is not as relevant to number formatting.
73 if (unit != nullptr) {
Jungshik Shina9a2bd32018-07-07 03:36:01 -070074 // TODO: On nullptr, reset to default value?
75 copy.fMacros.unit = std::move(*unit);
Jungshik Shinb3189662017-11-07 11:18:34 -080076 delete unit;
77 }
78 return copy;
79}
80
81template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -070082Derived NumberFormatterSettings<Derived>::adoptUnit(icu::MeasureUnit* unit)&& {
83 Derived move(std::move(*this));
84 // See comments above about slicing and ownership.
85 if (unit != nullptr) {
86 // TODO: On nullptr, reset to default value?
87 move.fMacros.unit = std::move(*unit);
88 delete unit;
89 }
90 return move;
91}
92
93template<typename Derived>
94Derived NumberFormatterSettings<Derived>::perUnit(const icu::MeasureUnit& perUnit) const& {
Jungshik Shinf61e46d2018-05-04 13:00:45 -070095 Derived copy(*this);
96 // See comments above about slicing.
97 copy.fMacros.perUnit = perUnit;
98 return copy;
99}
100
101template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700102Derived NumberFormatterSettings<Derived>::perUnit(const icu::MeasureUnit& perUnit)&& {
103 Derived move(std::move(*this));
104 // See comments above about slicing.
105 move.fMacros.perUnit = perUnit;
106 return move;
107}
108
109template<typename Derived>
110Derived NumberFormatterSettings<Derived>::adoptPerUnit(icu::MeasureUnit* perUnit) const& {
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700111 Derived copy(*this);
112 // See comments above about slicing and ownership.
113 if (perUnit != nullptr) {
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700114 // TODO: On nullptr, reset to default value?
115 copy.fMacros.perUnit = std::move(*perUnit);
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700116 delete perUnit;
117 }
118 return copy;
119}
120
121template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700122Derived NumberFormatterSettings<Derived>::adoptPerUnit(icu::MeasureUnit* perUnit)&& {
123 Derived move(std::move(*this));
124 // See comments above about slicing and ownership.
125 if (perUnit != nullptr) {
126 // TODO: On nullptr, reset to default value?
127 move.fMacros.perUnit = std::move(*perUnit);
128 delete perUnit;
129 }
130 return move;
131}
132
133template<typename Derived>
134Derived NumberFormatterSettings<Derived>::precision(const Precision& precision) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800135 Derived copy(*this);
136 // NOTE: Slicing is OK.
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700137 copy.fMacros.precision = precision;
Jungshik Shinb3189662017-11-07 11:18:34 -0800138 return copy;
139}
140
141template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700142Derived NumberFormatterSettings<Derived>::precision(const Precision& precision)&& {
143 Derived move(std::move(*this));
144 // NOTE: Slicing is OK.
145 move.fMacros.precision = precision;
146 return move;
147}
148
149template<typename Derived>
150Derived NumberFormatterSettings<Derived>::roundingMode(UNumberFormatRoundingMode roundingMode) const& {
151 Derived copy(*this);
152 copy.fMacros.roundingMode = roundingMode;
153 return copy;
154}
155
156template<typename Derived>
157Derived NumberFormatterSettings<Derived>::roundingMode(UNumberFormatRoundingMode roundingMode)&& {
158 Derived move(std::move(*this));
159 move.fMacros.roundingMode = roundingMode;
160 return move;
161}
162
163template<typename Derived>
Frank Tang69c72a62019-04-03 21:41:21 -0700164Derived NumberFormatterSettings<Derived>::grouping(UNumberGroupingStrategy strategy) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800165 Derived copy(*this);
Jungshik Shinf61e46d2018-05-04 13:00:45 -0700166 // NOTE: This is slightly different than how the setting is stored in Java
167 // because we want to put it on the stack.
168 copy.fMacros.grouper = Grouper::forStrategy(strategy);
Jungshik Shinb3189662017-11-07 11:18:34 -0800169 return copy;
170}
171
172template<typename Derived>
Frank Tang69c72a62019-04-03 21:41:21 -0700173Derived NumberFormatterSettings<Derived>::grouping(UNumberGroupingStrategy strategy)&& {
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700174 Derived move(std::move(*this));
175 move.fMacros.grouper = Grouper::forStrategy(strategy);
176 return move;
177}
178
179template<typename Derived>
180Derived NumberFormatterSettings<Derived>::integerWidth(const IntegerWidth& style) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800181 Derived copy(*this);
182 copy.fMacros.integerWidth = style;
183 return copy;
184}
185
186template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700187Derived NumberFormatterSettings<Derived>::integerWidth(const IntegerWidth& style)&& {
188 Derived move(std::move(*this));
189 move.fMacros.integerWidth = style;
190 return move;
191}
192
193template<typename Derived>
194Derived NumberFormatterSettings<Derived>::symbols(const DecimalFormatSymbols& symbols) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800195 Derived copy(*this);
196 copy.fMacros.symbols.setTo(symbols);
197 return copy;
198}
199
200template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700201Derived NumberFormatterSettings<Derived>::symbols(const DecimalFormatSymbols& symbols)&& {
202 Derived move(std::move(*this));
203 move.fMacros.symbols.setTo(symbols);
204 return move;
205}
206
207template<typename Derived>
208Derived NumberFormatterSettings<Derived>::adoptSymbols(NumberingSystem* ns) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800209 Derived copy(*this);
210 copy.fMacros.symbols.setTo(ns);
211 return copy;
212}
213
214template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700215Derived NumberFormatterSettings<Derived>::adoptSymbols(NumberingSystem* ns)&& {
216 Derived move(std::move(*this));
217 move.fMacros.symbols.setTo(ns);
218 return move;
219}
220
221template<typename Derived>
222Derived NumberFormatterSettings<Derived>::unitWidth(UNumberUnitWidth width) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800223 Derived copy(*this);
224 copy.fMacros.unitWidth = width;
225 return copy;
226}
227
228template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700229Derived NumberFormatterSettings<Derived>::unitWidth(UNumberUnitWidth width)&& {
230 Derived move(std::move(*this));
231 move.fMacros.unitWidth = width;
232 return move;
233}
234
235template<typename Derived>
236Derived NumberFormatterSettings<Derived>::sign(UNumberSignDisplay style) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800237 Derived copy(*this);
238 copy.fMacros.sign = style;
239 return copy;
240}
241
242template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700243Derived NumberFormatterSettings<Derived>::sign(UNumberSignDisplay style)&& {
244 Derived move(std::move(*this));
245 move.fMacros.sign = style;
246 return move;
247}
248
249template<typename Derived>
250Derived NumberFormatterSettings<Derived>::decimal(UNumberDecimalSeparatorDisplay style) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800251 Derived copy(*this);
252 copy.fMacros.decimal = style;
253 return copy;
254}
255
256template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700257Derived NumberFormatterSettings<Derived>::decimal(UNumberDecimalSeparatorDisplay style)&& {
258 Derived move(std::move(*this));
259 move.fMacros.decimal = style;
260 return move;
261}
262
263template<typename Derived>
264Derived NumberFormatterSettings<Derived>::scale(const Scale& scale) const& {
265 Derived copy(*this);
266 copy.fMacros.scale = scale;
267 return copy;
268}
269
270template<typename Derived>
271Derived NumberFormatterSettings<Derived>::scale(const Scale& scale)&& {
272 Derived move(std::move(*this));
273 move.fMacros.scale = scale;
274 return move;
275}
276
277template<typename Derived>
Frank Tangf90543d2020-10-30 19:02:04 -0700278Derived NumberFormatterSettings<Derived>::usage(const StringPiece usage) const& {
279 Derived copy(*this);
280 copy.fMacros.usage.set(usage);
281 return copy;
282}
283
284template<typename Derived>
285Derived NumberFormatterSettings<Derived>::usage(const StringPiece usage)&& {
286 Derived move(std::move(*this));
287 move.fMacros.usage.set(usage);
288 return move;
289}
290
291template<typename Derived>
Frank Tang7e7574b2021-04-13 21:19:13 -0700292Derived NumberFormatterSettings<Derived>::unitDisplayCase(const StringPiece unitDisplayCase) const& {
293 Derived copy(*this);
294 copy.fMacros.unitDisplayCase.set(unitDisplayCase);
295 return copy;
296}
297
298template<typename Derived>
299Derived NumberFormatterSettings<Derived>::unitDisplayCase(const StringPiece unitDisplayCase)&& {
300 Derived move(std::move(*this));
301 move.fMacros.unitDisplayCase.set(unitDisplayCase);
302 return move;
303}
304
305template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700306Derived NumberFormatterSettings<Derived>::padding(const Padder& padder) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800307 Derived copy(*this);
308 copy.fMacros.padder = padder;
309 return copy;
310}
311
312template<typename Derived>
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700313Derived NumberFormatterSettings<Derived>::padding(const Padder& padder)&& {
314 Derived move(std::move(*this));
315 move.fMacros.padder = padder;
316 return move;
317}
318
319template<typename Derived>
320Derived NumberFormatterSettings<Derived>::threshold(int32_t threshold) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800321 Derived copy(*this);
322 copy.fMacros.threshold = threshold;
323 return copy;
324}
325
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700326template<typename Derived>
327Derived NumberFormatterSettings<Derived>::threshold(int32_t threshold)&& {
328 Derived move(std::move(*this));
329 move.fMacros.threshold = threshold;
330 return move;
331}
332
333template<typename Derived>
334Derived NumberFormatterSettings<Derived>::macros(const impl::MacroProps& macros) const& {
335 Derived copy(*this);
336 copy.fMacros = macros;
337 return copy;
338}
339
340template<typename Derived>
341Derived NumberFormatterSettings<Derived>::macros(const impl::MacroProps& macros)&& {
342 Derived move(std::move(*this));
343 move.fMacros = macros;
344 return move;
345}
346
347template<typename Derived>
348Derived NumberFormatterSettings<Derived>::macros(impl::MacroProps&& macros) const& {
349 Derived copy(*this);
350 copy.fMacros = std::move(macros);
351 return copy;
352}
353
354template<typename Derived>
355Derived NumberFormatterSettings<Derived>::macros(impl::MacroProps&& macros)&& {
356 Derived move(std::move(*this));
357 move.fMacros = std::move(macros);
358 return move;
359}
360
Frank Tangf2223962020-04-27 18:25:29 -0700361// Note: toSkeleton defined in number_skeletons.cpp
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700362
Frank Tang69c72a62019-04-03 21:41:21 -0700363template<typename Derived>
364LocalPointer<Derived> NumberFormatterSettings<Derived>::clone() const & {
365 return LocalPointer<Derived>(new Derived(*this));
366}
367
368template<typename Derived>
369LocalPointer<Derived> NumberFormatterSettings<Derived>::clone() && {
370 return LocalPointer<Derived>(new Derived(std::move(*this)));
371}
372
Jungshik Shinb3189662017-11-07 11:18:34 -0800373// Declare all classes that implement NumberFormatterSettings
374// See https://stackoverflow.com/a/495056/1407170
375template
376class icu::number::NumberFormatterSettings<icu::number::UnlocalizedNumberFormatter>;
377template
378class icu::number::NumberFormatterSettings<icu::number::LocalizedNumberFormatter>;
379
380
381UnlocalizedNumberFormatter NumberFormatter::with() {
382 UnlocalizedNumberFormatter result;
383 return result;
384}
385
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700386LocalizedNumberFormatter NumberFormatter::withLocale(const Locale& locale) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800387 return with().locale(locale);
388}
389
Frank Tangf2223962020-04-27 18:25:29 -0700390// Note: forSkeleton defined in number_skeletons.cpp
Jungshik Shinb3189662017-11-07 11:18:34 -0800391
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700392
393template<typename T> using NFS = NumberFormatterSettings<T>;
394using LNF = LocalizedNumberFormatter;
395using UNF = UnlocalizedNumberFormatter;
396
397UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(const UNF& other)
398 : UNF(static_cast<const NFS<UNF>&>(other)) {}
399
400UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(const NFS<UNF>& other)
401 : NFS<UNF>(other) {
402 // No additional fields to assign
Jungshik Shinb3189662017-11-07 11:18:34 -0800403}
404
Jungshik Shin42d50272018-10-24 01:22:09 -0700405// Make default copy constructor call the NumberFormatterSettings copy constructor.
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700406UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(UNF&& src) U_NOEXCEPT
407 : UNF(static_cast<NFS<UNF>&&>(src)) {}
408
409UnlocalizedNumberFormatter::UnlocalizedNumberFormatter(NFS<UNF>&& src) U_NOEXCEPT
410 : NFS<UNF>(std::move(src)) {
411 // No additional fields to assign
412}
413
414UnlocalizedNumberFormatter& UnlocalizedNumberFormatter::operator=(const UNF& other) {
415 NFS<UNF>::operator=(static_cast<const NFS<UNF>&>(other));
416 // No additional fields to assign
417 return *this;
418}
419
420UnlocalizedNumberFormatter& UnlocalizedNumberFormatter::operator=(UNF&& src) U_NOEXCEPT {
421 NFS<UNF>::operator=(static_cast<NFS<UNF>&&>(src));
422 // No additional fields to assign
423 return *this;
424}
425
Jungshik Shin42d50272018-10-24 01:22:09 -0700426// Make default copy constructor call the NumberFormatterSettings copy constructor.
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700427LocalizedNumberFormatter::LocalizedNumberFormatter(const LNF& other)
428 : LNF(static_cast<const NFS<LNF>&>(other)) {}
429
430LocalizedNumberFormatter::LocalizedNumberFormatter(const NFS<LNF>& other)
431 : NFS<LNF>(other) {
Frank Tangf90543d2020-10-30 19:02:04 -0700432 UErrorCode localStatus = U_ZERO_ERROR; // Can't bubble up the error
433 lnfCopyHelper(static_cast<const LNF&>(other), localStatus);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700434}
435
436LocalizedNumberFormatter::LocalizedNumberFormatter(LocalizedNumberFormatter&& src) U_NOEXCEPT
437 : LNF(static_cast<NFS<LNF>&&>(src)) {}
438
439LocalizedNumberFormatter::LocalizedNumberFormatter(NFS<LNF>&& src) U_NOEXCEPT
440 : NFS<LNF>(std::move(src)) {
Frank Tangf90543d2020-10-30 19:02:04 -0700441 lnfMoveHelper(std::move(static_cast<LNF&&>(src)));
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700442}
443
444LocalizedNumberFormatter& LocalizedNumberFormatter::operator=(const LNF& other) {
Frank Tang7e7574b2021-04-13 21:19:13 -0700445 if (this == &other) { return *this; } // self-assignment: no-op
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700446 NFS<LNF>::operator=(static_cast<const NFS<LNF>&>(other));
Frank Tangf90543d2020-10-30 19:02:04 -0700447 UErrorCode localStatus = U_ZERO_ERROR; // Can't bubble up the error
448 lnfCopyHelper(other, localStatus);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700449 return *this;
450}
451
452LocalizedNumberFormatter& LocalizedNumberFormatter::operator=(LNF&& src) U_NOEXCEPT {
453 NFS<LNF>::operator=(static_cast<NFS<LNF>&&>(src));
Frank Tangf90543d2020-10-30 19:02:04 -0700454 lnfMoveHelper(std::move(src));
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700455 return *this;
456}
457
Frank Tangf90543d2020-10-30 19:02:04 -0700458void LocalizedNumberFormatter::resetCompiled() {
Jungshik Shin42d50272018-10-24 01:22:09 -0700459 auto* callCount = reinterpret_cast<u_atomic_int32_t*>(fUnsafeCallCount);
460 umtx_storeRelease(*callCount, 0);
Jungshik Shin42d50272018-10-24 01:22:09 -0700461 fCompiled = nullptr;
462}
463
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700464void LocalizedNumberFormatter::lnfMoveHelper(LNF&& src) {
465 // Copy over the compiled formatter and set call count to INT32_MIN as in computeCompiled().
466 // Don't copy the call count directly because doing so requires a loadAcquire/storeRelease.
467 // The bits themselves appear to be platform-dependent, so copying them might not be safe.
Jungshik Shin42d50272018-10-24 01:22:09 -0700468 delete fCompiled;
Frank Tangf90543d2020-10-30 19:02:04 -0700469 if (src.fCompiled != nullptr) {
470 auto* callCount = reinterpret_cast<u_atomic_int32_t*>(fUnsafeCallCount);
471 umtx_storeRelease(*callCount, INT32_MIN);
472 fCompiled = src.fCompiled;
473 // Reset the source object to leave it in a safe state.
474 src.resetCompiled();
475 } else {
476 resetCompiled();
477 }
478
479 // Unconditionally move the warehouse
480 delete fWarehouse;
481 fWarehouse = src.fWarehouse;
482 src.fWarehouse = nullptr;
483}
484
485void LocalizedNumberFormatter::lnfCopyHelper(const LNF&, UErrorCode& status) {
486 // When copying, always reset the compiled formatter.
487 delete fCompiled;
488 resetCompiled();
489
490 // If MacroProps has a reference to AffixPatternProvider, we need to copy it.
491 // If MacroProps has a reference to PluralRules, copy that one, too.
492 delete fWarehouse;
493 if (fMacros.affixProvider || fMacros.rules) {
494 LocalPointer<DecimalFormatWarehouse> warehouse(new DecimalFormatWarehouse(), status);
495 if (U_FAILURE(status)) {
496 fWarehouse = nullptr;
497 return;
498 }
499 if (fMacros.affixProvider) {
500 warehouse->affixProvider.setTo(fMacros.affixProvider, status);
501 fMacros.affixProvider = &warehouse->affixProvider.get();
502 }
503 if (fMacros.rules) {
504 warehouse->rules.adoptInsteadAndCheckErrorCode(
505 new PluralRules(*fMacros.rules), status);
506 fMacros.rules = warehouse->rules.getAlias();
507 }
508 fWarehouse = warehouse.orphan();
509 } else {
510 fWarehouse = nullptr;
511 }
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700512}
513
514
515LocalizedNumberFormatter::~LocalizedNumberFormatter() {
516 delete fCompiled;
Frank Tangf90543d2020-10-30 19:02:04 -0700517 delete fWarehouse;
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700518}
519
520LocalizedNumberFormatter::LocalizedNumberFormatter(const MacroProps& macros, const Locale& locale) {
Jungshik Shinb3189662017-11-07 11:18:34 -0800521 fMacros = macros;
522 fMacros.locale = locale;
523}
524
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700525LocalizedNumberFormatter::LocalizedNumberFormatter(MacroProps&& macros, const Locale& locale) {
526 fMacros = std::move(macros);
527 fMacros.locale = locale;
528}
529
530LocalizedNumberFormatter UnlocalizedNumberFormatter::locale(const Locale& locale) const& {
Jungshik Shinb3189662017-11-07 11:18:34 -0800531 return LocalizedNumberFormatter(fMacros, locale);
532}
533
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700534LocalizedNumberFormatter UnlocalizedNumberFormatter::locale(const Locale& locale)&& {
535 return LocalizedNumberFormatter(std::move(fMacros), locale);
536}
537
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700538FormattedNumber LocalizedNumberFormatter::formatInt(int64_t value, UErrorCode& status) const {
Jungshik Shinb3189662017-11-07 11:18:34 -0800539 if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); }
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700540 auto results = new UFormattedNumberData();
Jungshik Shinb3189662017-11-07 11:18:34 -0800541 if (results == nullptr) {
542 status = U_MEMORY_ALLOCATION_ERROR;
543 return FormattedNumber(status);
544 }
545 results->quantity.setToLong(value);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700546 formatImpl(results, status);
547
548 // Do not save the results object if we encountered a failure.
549 if (U_SUCCESS(status)) {
550 return FormattedNumber(results);
551 } else {
552 delete results;
553 return FormattedNumber(status);
554 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800555}
556
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700557FormattedNumber LocalizedNumberFormatter::formatDouble(double value, UErrorCode& status) const {
Jungshik Shinb3189662017-11-07 11:18:34 -0800558 if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); }
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700559 auto results = new UFormattedNumberData();
Jungshik Shinb3189662017-11-07 11:18:34 -0800560 if (results == nullptr) {
561 status = U_MEMORY_ALLOCATION_ERROR;
562 return FormattedNumber(status);
563 }
564 results->quantity.setToDouble(value);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700565 formatImpl(results, status);
566
567 // Do not save the results object if we encountered a failure.
568 if (U_SUCCESS(status)) {
569 return FormattedNumber(results);
570 } else {
571 delete results;
572 return FormattedNumber(status);
573 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800574}
575
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700576FormattedNumber LocalizedNumberFormatter::formatDecimal(StringPiece value, UErrorCode& status) const {
Jungshik Shinb3189662017-11-07 11:18:34 -0800577 if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); }
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700578 auto results = new UFormattedNumberData();
Jungshik Shinb3189662017-11-07 11:18:34 -0800579 if (results == nullptr) {
580 status = U_MEMORY_ALLOCATION_ERROR;
581 return FormattedNumber(status);
582 }
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700583 results->quantity.setToDecNumber(value, status);
584 formatImpl(results, status);
585
586 // Do not save the results object if we encountered a failure.
587 if (U_SUCCESS(status)) {
588 return FormattedNumber(results);
589 } else {
590 delete results;
591 return FormattedNumber(status);
592 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800593}
594
595FormattedNumber
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700596LocalizedNumberFormatter::formatDecimalQuantity(const DecimalQuantity& dq, UErrorCode& status) const {
597 if (U_FAILURE(status)) { return FormattedNumber(U_ILLEGAL_ARGUMENT_ERROR); }
598 auto results = new UFormattedNumberData();
599 if (results == nullptr) {
600 status = U_MEMORY_ALLOCATION_ERROR;
601 return FormattedNumber(status);
602 }
603 results->quantity = dq;
604 formatImpl(results, status);
605
606 // Do not save the results object if we encountered a failure.
607 if (U_SUCCESS(status)) {
608 return FormattedNumber(results);
609 } else {
610 delete results;
611 return FormattedNumber(status);
612 }
613}
614
615void LocalizedNumberFormatter::formatImpl(impl::UFormattedNumberData* results, UErrorCode& status) const {
616 if (computeCompiled(status)) {
Frank Tangf90543d2020-10-30 19:02:04 -0700617 fCompiled->format(results, status);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700618 } else {
Frank Tangf90543d2020-10-30 19:02:04 -0700619 NumberFormatterImpl::formatStatic(fMacros, results, status);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700620 }
Frank Tang69c72a62019-04-03 21:41:21 -0700621 if (U_FAILURE(status)) {
622 return;
623 }
624 results->getStringRef().writeTerminator(status);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700625}
626
627void LocalizedNumberFormatter::getAffixImpl(bool isPrefix, bool isNegative, UnicodeString& result,
628 UErrorCode& status) const {
Frank Tangb8696612019-10-25 14:58:21 -0700629 FormattedStringBuilder string;
630 auto signum = static_cast<Signum>(isNegative ? SIGNUM_NEG : SIGNUM_POS);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700631 // Always return affixes for plural form OTHER.
632 static const StandardPlural::Form plural = StandardPlural::OTHER;
633 int32_t prefixLength;
634 if (computeCompiled(status)) {
635 prefixLength = fCompiled->getPrefixSuffix(signum, plural, string, status);
636 } else {
637 prefixLength = NumberFormatterImpl::getPrefixSuffixStatic(fMacros, signum, plural, string, status);
638 }
639 result.remove();
640 if (isPrefix) {
641 result.append(string.toTempUnicodeString().tempSubStringBetween(0, prefixLength));
642 } else {
643 result.append(string.toTempUnicodeString().tempSubStringBetween(prefixLength, string.length()));
644 }
645}
646
647bool LocalizedNumberFormatter::computeCompiled(UErrorCode& status) const {
Jungshik Shinb3189662017-11-07 11:18:34 -0800648 // fUnsafeCallCount contains memory to be interpreted as an atomic int, most commonly
649 // std::atomic<int32_t>. Since the type of atomic int is platform-dependent, we cast the
650 // bytes in fUnsafeCallCount to u_atomic_int32_t, a typedef for the platform-dependent
651 // atomic int type defined in umutex.h.
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700652 static_assert(
653 sizeof(u_atomic_int32_t) <= sizeof(fUnsafeCallCount),
654 "Atomic integer size on this platform exceeds the size allocated by fUnsafeCallCount");
655 auto* callCount = reinterpret_cast<u_atomic_int32_t*>(
656 const_cast<LocalizedNumberFormatter*>(this)->fUnsafeCallCount);
Jungshik Shinb3189662017-11-07 11:18:34 -0800657
658 // A positive value in the atomic int indicates that the data structure is not yet ready;
659 // a negative value indicates that it is ready. If, after the increment, the atomic int
660 // is exactly threshold, then it is the current thread's job to build the data structure.
661 // Note: We set the callCount to INT32_MIN so that if another thread proceeds to increment
662 // the atomic int, the value remains below zero.
663 int32_t currentCount = umtx_loadAcquire(*callCount);
664 if (0 <= currentCount && currentCount <= fMacros.threshold && fMacros.threshold > 0) {
665 currentCount = umtx_atomic_inc(callCount);
666 }
667
668 if (currentCount == fMacros.threshold && fMacros.threshold > 0) {
669 // Build the data structure and then use it (slow to fast path).
Jungshik Shin42d50272018-10-24 01:22:09 -0700670 const NumberFormatterImpl* compiled = new NumberFormatterImpl(fMacros, status);
671 if (compiled == nullptr) {
672 status = U_MEMORY_ALLOCATION_ERROR;
673 return false;
674 }
Jungshik Shinb3189662017-11-07 11:18:34 -0800675 U_ASSERT(fCompiled == nullptr);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700676 const_cast<LocalizedNumberFormatter*>(this)->fCompiled = compiled;
Jungshik Shinb3189662017-11-07 11:18:34 -0800677 umtx_storeRelease(*callCount, INT32_MIN);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700678 return true;
Jungshik Shinb3189662017-11-07 11:18:34 -0800679 } else if (currentCount < 0) {
680 // The data structure is already built; use it (fast path).
681 U_ASSERT(fCompiled != nullptr);
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700682 return true;
Jungshik Shinb3189662017-11-07 11:18:34 -0800683 } else {
684 // Format the number without building the data structure (slow path).
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700685 return false;
Jungshik Shinb3189662017-11-07 11:18:34 -0800686 }
687}
688
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700689const impl::NumberFormatterImpl* LocalizedNumberFormatter::getCompiled() const {
690 return fCompiled;
691}
692
693int32_t LocalizedNumberFormatter::getCallCount() const {
694 auto* callCount = reinterpret_cast<u_atomic_int32_t*>(
695 const_cast<LocalizedNumberFormatter*>(this)->fUnsafeCallCount);
696 return umtx_loadAcquire(*callCount);
697}
698
Frank Tangf2223962020-04-27 18:25:29 -0700699// Note: toFormat defined in number_asformat.cpp
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700700
Frank Tang3e05d9d2021-11-08 14:04:04 -0800701const DecimalFormatSymbols* LocalizedNumberFormatter::getDecimalFormatSymbols() const {
702 return fMacros.symbols.getDecimalFormatSymbols();
703}
704
Frank Tangf2223962020-04-27 18:25:29 -0700705#if (U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(_MSC_VER)
706// Warning 4661.
707#pragma warning(pop)
708#endif
Jungshik Shina9a2bd32018-07-07 03:36:01 -0700709
Jungshik Shinb3189662017-11-07 11:18:34 -0800710#endif /* #if !UCONFIG_NO_FORMATTING */