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