1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 #include "unicode/utypes.h"
6 #if !UCONFIG_NO_FORMATTING
8 // Allow implicit conversion from char16_t* to UnicodeString for this file:
9 // Helpful in toString methods and elsewhere.
10 #define UNISTR_FROM_STRING_EXPLICIT
14 #include "number_asformat.h"
15 #include "number_types.h"
16 #include "number_utils.h"
18 #include "number_utypes.h"
21 using namespace icu::number
;
22 using namespace icu::number::impl
;
24 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocalizedNumberFormatterAsFormat
)
26 LocalizedNumberFormatterAsFormat::LocalizedNumberFormatterAsFormat(
27 const LocalizedNumberFormatter
& formatter
, const Locale
& locale
)
28 : fFormatter(formatter
), fLocale(locale
) {
29 const char* localeName
= locale
.getName();
30 setLocaleIDs(localeName
, localeName
);
33 LocalizedNumberFormatterAsFormat::~LocalizedNumberFormatterAsFormat() = default;
35 UBool
LocalizedNumberFormatterAsFormat::operator==(const Format
& other
) const {
36 auto* _other
= dynamic_cast<const LocalizedNumberFormatterAsFormat
*>(&other
);
37 if (_other
== nullptr) {
40 // TODO: Change this to use LocalizedNumberFormatter::operator== if it is ever proposed.
41 // This implementation is fine, but not particularly efficient.
42 UErrorCode localStatus
= U_ZERO_ERROR
;
43 return fFormatter
.toSkeleton(localStatus
) == _other
->fFormatter
.toSkeleton(localStatus
);
46 Format
* LocalizedNumberFormatterAsFormat::clone() const {
47 return new LocalizedNumberFormatterAsFormat(*this);
50 UnicodeString
& LocalizedNumberFormatterAsFormat::format(const Formattable
& obj
, UnicodeString
& appendTo
,
51 FieldPosition
& pos
, UErrorCode
& status
) const {
52 if (U_FAILURE(status
)) { return appendTo
; }
53 UFormattedNumberData data
;
54 obj
.populateDecimalQuantity(data
.quantity
, status
);
55 if (U_FAILURE(status
)) {
58 fFormatter
.formatImpl(&data
, status
);
59 if (U_FAILURE(status
)) {
62 // always return first occurrence:
65 bool found
= data
.getStringRef().nextFieldPosition(pos
, status
);
66 if (found
&& appendTo
.length() != 0) {
67 pos
.setBeginIndex(pos
.getBeginIndex() + appendTo
.length());
68 pos
.setEndIndex(pos
.getEndIndex() + appendTo
.length());
70 appendTo
.append(data
.getStringRef().toTempUnicodeString());
74 UnicodeString
& LocalizedNumberFormatterAsFormat::format(const Formattable
& obj
, UnicodeString
& appendTo
,
75 FieldPositionIterator
* posIter
,
76 UErrorCode
& status
) const {
77 if (U_FAILURE(status
)) { return appendTo
; }
78 UFormattedNumberData data
;
79 obj
.populateDecimalQuantity(data
.quantity
, status
);
80 if (U_FAILURE(status
)) {
83 fFormatter
.formatImpl(&data
, status
);
84 if (U_FAILURE(status
)) {
87 appendTo
.append(data
.getStringRef().toTempUnicodeString());
88 if (posIter
!= nullptr) {
89 FieldPositionIteratorHandler
fpih(posIter
, status
);
90 data
.getStringRef().getAllFieldPositions(fpih
, status
);
95 void LocalizedNumberFormatterAsFormat::parseObject(const UnicodeString
&, Formattable
&,
96 ParsePosition
& parse_pos
) const {
98 parse_pos
.setErrorIndex(0);
101 const LocalizedNumberFormatter
& LocalizedNumberFormatterAsFormat::getNumberFormatter() const {
105 #endif /* #if !UCONFIG_NO_FORMATTING */