/src/icu/source/i18n/number_asformat.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2018 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 | | // Allow implicit conversion from char16_t* to UnicodeString for this file: |
9 | | // Helpful in toString methods and elsewhere. |
10 | | #define UNISTR_FROM_STRING_EXPLICIT |
11 | | |
12 | | #include <stdlib.h> |
13 | | #include <cmath> |
14 | | #include "number_asformat.h" |
15 | | #include "number_types.h" |
16 | | #include "number_utils.h" |
17 | | #include "fphdlimp.h" |
18 | | #include "number_utypes.h" |
19 | | |
20 | | using namespace icu; |
21 | | using namespace icu::number; |
22 | | using namespace icu::number::impl; |
23 | | |
24 | | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LocalizedNumberFormatterAsFormat) |
25 | | |
26 | | LocalizedNumberFormatterAsFormat::LocalizedNumberFormatterAsFormat( |
27 | | const LocalizedNumberFormatter& formatter, const Locale& locale) |
28 | 0 | : fFormatter(formatter), fLocale(locale) { |
29 | 0 | const char* localeName = locale.getName(); |
30 | 0 | setLocaleIDs(localeName, localeName); |
31 | 0 | } |
32 | | |
33 | 0 | LocalizedNumberFormatterAsFormat::~LocalizedNumberFormatterAsFormat() = default; |
34 | | |
35 | 0 | bool LocalizedNumberFormatterAsFormat::operator==(const Format& other) const { |
36 | 0 | auto* _other = dynamic_cast<const LocalizedNumberFormatterAsFormat*>(&other); |
37 | 0 | if (_other == nullptr) { |
38 | 0 | return false; |
39 | 0 | } |
40 | | // TODO: Change this to use LocalizedNumberFormatter::operator== if it is ever proposed. |
41 | | // This implementation is fine, but not particularly efficient. |
42 | 0 | UErrorCode localStatus = U_ZERO_ERROR; |
43 | 0 | return fFormatter.toSkeleton(localStatus) == _other->fFormatter.toSkeleton(localStatus); |
44 | 0 | } |
45 | | |
46 | 0 | LocalizedNumberFormatterAsFormat* LocalizedNumberFormatterAsFormat::clone() const { |
47 | 0 | return new LocalizedNumberFormatterAsFormat(*this); |
48 | 0 | } |
49 | | |
50 | | UnicodeString& LocalizedNumberFormatterAsFormat::format(const Formattable& obj, UnicodeString& appendTo, |
51 | 0 | FieldPosition& pos, UErrorCode& status) const { |
52 | 0 | if (U_FAILURE(status)) { return appendTo; } |
53 | 0 | UFormattedNumberData data; |
54 | 0 | obj.populateDecimalQuantity(data.quantity, status); |
55 | 0 | if (U_FAILURE(status)) { |
56 | 0 | return appendTo; |
57 | 0 | } |
58 | 0 | fFormatter.formatImpl(&data, status); |
59 | 0 | if (U_FAILURE(status)) { |
60 | 0 | return appendTo; |
61 | 0 | } |
62 | | // always return first occurrence: |
63 | 0 | pos.setBeginIndex(0); |
64 | 0 | pos.setEndIndex(0); |
65 | 0 | bool found = data.nextFieldPosition(pos, status); |
66 | 0 | if (found && appendTo.length() != 0) { |
67 | 0 | pos.setBeginIndex(pos.getBeginIndex() + appendTo.length()); |
68 | 0 | pos.setEndIndex(pos.getEndIndex() + appendTo.length()); |
69 | 0 | } |
70 | 0 | appendTo.append(data.toTempString(status)); |
71 | 0 | return appendTo; |
72 | 0 | } |
73 | | |
74 | | UnicodeString& LocalizedNumberFormatterAsFormat::format(const Formattable& obj, UnicodeString& appendTo, |
75 | | FieldPositionIterator* posIter, |
76 | 0 | UErrorCode& status) const { |
77 | 0 | if (U_FAILURE(status)) { return appendTo; } |
78 | 0 | UFormattedNumberData data; |
79 | 0 | obj.populateDecimalQuantity(data.quantity, status); |
80 | 0 | if (U_FAILURE(status)) { |
81 | 0 | return appendTo; |
82 | 0 | } |
83 | 0 | fFormatter.formatImpl(&data, status); |
84 | 0 | if (U_FAILURE(status)) { |
85 | 0 | return appendTo; |
86 | 0 | } |
87 | 0 | appendTo.append(data.toTempString(status)); |
88 | 0 | if (posIter != nullptr) { |
89 | 0 | FieldPositionIteratorHandler fpih(posIter, status); |
90 | 0 | data.getAllFieldPositions(fpih, status); |
91 | 0 | } |
92 | 0 | return appendTo; |
93 | 0 | } |
94 | | |
95 | | void LocalizedNumberFormatterAsFormat::parseObject(const UnicodeString&, Formattable&, |
96 | 0 | ParsePosition& parse_pos) const { |
97 | | // Not supported. |
98 | 0 | parse_pos.setErrorIndex(0); |
99 | 0 | } |
100 | | |
101 | 0 | const LocalizedNumberFormatter& LocalizedNumberFormatterAsFormat::getNumberFormatter() const { |
102 | 0 | return fFormatter; |
103 | 0 | } |
104 | | |
105 | | |
106 | | // Definitions of public API methods (put here for dependency disentanglement) |
107 | | |
108 | 0 | Format* LocalizedNumberFormatter::toFormat(UErrorCode& status) const { |
109 | 0 | if (U_FAILURE(status)) { |
110 | 0 | return nullptr; |
111 | 0 | } |
112 | 0 | LocalPointer<LocalizedNumberFormatterAsFormat> retval( |
113 | 0 | new LocalizedNumberFormatterAsFormat(*this, fMacros.locale), status); |
114 | 0 | return retval.orphan(); |
115 | 0 | } |
116 | | |
117 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |