/src/icu/source/i18n/number_usageprefs.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2020 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 "number_usageprefs.h" |
9 | | #include "cstring.h" |
10 | | #include "number_decimalquantity.h" |
11 | | #include "number_microprops.h" |
12 | | #include "number_roundingutils.h" |
13 | | #include "number_skeletons.h" |
14 | | #include "unicode/char16ptr.h" |
15 | | #include "unicode/currunit.h" |
16 | | #include "unicode/fmtable.h" |
17 | | #include "unicode/measure.h" |
18 | | #include "unicode/numberformatter.h" |
19 | | #include "unicode/platform.h" |
20 | | #include "unicode/unum.h" |
21 | | #include "unicode/urename.h" |
22 | | #include "units_data.h" |
23 | | |
24 | | using namespace icu; |
25 | | using namespace icu::number; |
26 | | using namespace icu::number::impl; |
27 | | using icu::StringSegment; |
28 | | using icu::units::ConversionRates; |
29 | | |
30 | | // Copy constructor |
31 | 0 | StringProp::StringProp(const StringProp &other) : StringProp() { |
32 | 0 | this->operator=(other); |
33 | 0 | } |
34 | | |
35 | | // Copy assignment operator |
36 | 0 | StringProp &StringProp::operator=(const StringProp &other) { |
37 | 0 | if (this == &other) { return *this; } // self-assignment: no-op |
38 | 0 | fLength = 0; |
39 | 0 | fError = other.fError; |
40 | 0 | if (fValue != nullptr) { |
41 | 0 | uprv_free(fValue); |
42 | 0 | fValue = nullptr; |
43 | 0 | } |
44 | 0 | if (other.fValue == nullptr) { |
45 | 0 | return *this; |
46 | 0 | } |
47 | 0 | if (U_FAILURE(other.fError)) { |
48 | | // We don't bother trying to allocating memory if we're in any case busy |
49 | | // copying an errored StringProp. |
50 | 0 | return *this; |
51 | 0 | } |
52 | 0 | fValue = (char *)uprv_malloc(other.fLength + 1); |
53 | 0 | if (fValue == nullptr) { |
54 | 0 | fError = U_MEMORY_ALLOCATION_ERROR; |
55 | 0 | return *this; |
56 | 0 | } |
57 | 0 | fLength = other.fLength; |
58 | 0 | uprv_strncpy(fValue, other.fValue, fLength + 1); |
59 | 0 | return *this; |
60 | 0 | } |
61 | | |
62 | | // Move constructor |
63 | 0 | StringProp::StringProp(StringProp &&src) U_NOEXCEPT : fValue(src.fValue), |
64 | 0 | fLength(src.fLength), |
65 | 0 | fError(src.fError) { |
66 | | // Take ownership away from src if necessary |
67 | 0 | src.fValue = nullptr; |
68 | 0 | } |
69 | | |
70 | | // Move assignment operator |
71 | 0 | StringProp &StringProp::operator=(StringProp &&src) U_NOEXCEPT { |
72 | 0 | if (this == &src) { |
73 | 0 | return *this; |
74 | 0 | } |
75 | 0 | if (fValue != nullptr) { |
76 | 0 | uprv_free(fValue); |
77 | 0 | } |
78 | 0 | fValue = src.fValue; |
79 | 0 | fLength = src.fLength; |
80 | 0 | fError = src.fError; |
81 | | // Take ownership away from src if necessary |
82 | 0 | src.fValue = nullptr; |
83 | 0 | return *this; |
84 | 0 | } |
85 | | |
86 | 0 | StringProp::~StringProp() { |
87 | 0 | if (fValue != nullptr) { |
88 | 0 | uprv_free(fValue); |
89 | 0 | fValue = nullptr; |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | 0 | void StringProp::set(StringPiece value) { |
94 | 0 | if (fValue != nullptr) { |
95 | 0 | uprv_free(fValue); |
96 | 0 | fValue = nullptr; |
97 | 0 | } |
98 | 0 | fLength = value.length(); |
99 | 0 | fValue = (char *)uprv_malloc(fLength + 1); |
100 | 0 | if (fValue == nullptr) { |
101 | 0 | fLength = 0; |
102 | 0 | fError = U_MEMORY_ALLOCATION_ERROR; |
103 | 0 | return; |
104 | 0 | } |
105 | 0 | uprv_strncpy(fValue, value.data(), fLength); |
106 | 0 | fValue[fLength] = 0; |
107 | 0 | } |
108 | | |
109 | | // Populates micros.mixedMeasures and modifies quantity, based on the values in |
110 | | // measures. |
111 | | void mixedMeasuresToMicros(const MaybeStackVector<Measure> &measures, DecimalQuantity *quantity, |
112 | 0 | MicroProps *micros, UErrorCode status) { |
113 | 0 | micros->mixedMeasuresCount = measures.length(); |
114 | |
|
115 | 0 | if (micros->mixedMeasures.getCapacity() < micros->mixedMeasuresCount) { |
116 | 0 | if (micros->mixedMeasures.resize(micros->mixedMeasuresCount) == nullptr) { |
117 | 0 | status = U_MEMORY_ALLOCATION_ERROR; |
118 | 0 | return; |
119 | 0 | } |
120 | 0 | } |
121 | | |
122 | 0 | for (int32_t i = 0; i < micros->mixedMeasuresCount; i++) { |
123 | 0 | switch (measures[i]->getNumber().getType()) { |
124 | 0 | case Formattable::kInt64: |
125 | 0 | micros->mixedMeasures[i] = measures[i]->getNumber().getInt64(); |
126 | 0 | break; |
127 | | |
128 | 0 | case Formattable::kDouble: |
129 | 0 | U_ASSERT(micros->indexOfQuantity < 0); |
130 | 0 | quantity->setToDouble(measures[i]->getNumber().getDouble()); |
131 | 0 | micros->indexOfQuantity = i; |
132 | 0 | break; |
133 | | |
134 | 0 | default: |
135 | 0 | U_ASSERT(0 == "Found a Measure Number which is neither a double nor an int"); |
136 | 0 | UPRV_UNREACHABLE; |
137 | 0 | break; |
138 | 0 | } |
139 | | |
140 | 0 | if (U_FAILURE(status)) { |
141 | 0 | return; |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | 0 | if (micros->indexOfQuantity < 0) { |
146 | | // There is no quantity. |
147 | 0 | status = U_INTERNAL_PROGRAM_ERROR; |
148 | 0 | } |
149 | 0 | } |
150 | | |
151 | | UsagePrefsHandler::UsagePrefsHandler(const Locale &locale, |
152 | | const MeasureUnit &inputUnit, |
153 | | const StringPiece usage, |
154 | | const MicroPropsGenerator *parent, |
155 | | UErrorCode &status) |
156 | 0 | : fUnitsRouter(inputUnit, StringPiece(locale.getCountry()), usage, status), |
157 | 0 | fParent(parent) { |
158 | 0 | } |
159 | | |
160 | | void UsagePrefsHandler::processQuantity(DecimalQuantity &quantity, MicroProps µs, |
161 | 0 | UErrorCode &status) const { |
162 | 0 | fParent->processQuantity(quantity, micros, status); |
163 | 0 | if (U_FAILURE(status)) { |
164 | 0 | return; |
165 | 0 | } |
166 | | |
167 | 0 | quantity.roundToInfinity(); // Enables toDouble |
168 | 0 | const units::RouteResult routed = fUnitsRouter.route(quantity.toDouble(), µs.rounder, status); |
169 | 0 | if (U_FAILURE(status)) { |
170 | 0 | return; |
171 | 0 | } |
172 | 0 | const MaybeStackVector<Measure>& routedMeasures = routed.measures; |
173 | 0 | micros.outputUnit = routed.outputUnit.copy(status).build(status); |
174 | 0 | if (U_FAILURE(status)) { |
175 | 0 | return; |
176 | 0 | } |
177 | | |
178 | 0 | mixedMeasuresToMicros(routedMeasures, &quantity, µs, status); |
179 | 0 | } |
180 | | |
181 | | UnitConversionHandler::UnitConversionHandler(const MeasureUnit &targetUnit, |
182 | | const MicroPropsGenerator *parent, UErrorCode &status) |
183 | 0 | : fOutputUnit(targetUnit), fParent(parent) { |
184 | 0 | MeasureUnitImpl tempInput, tempOutput; |
185 | |
|
186 | 0 | ConversionRates conversionRates(status); |
187 | 0 | if (U_FAILURE(status)) { |
188 | 0 | return; |
189 | 0 | } |
190 | | |
191 | 0 | const MeasureUnitImpl &targetUnitImpl = |
192 | 0 | MeasureUnitImpl::forMeasureUnit(targetUnit, tempOutput, status); |
193 | 0 | fUnitConverter.adoptInsteadAndCheckErrorCode( |
194 | 0 | new ComplexUnitsConverter(targetUnitImpl, conversionRates, status), status); |
195 | 0 | } |
196 | | |
197 | | void UnitConversionHandler::processQuantity(DecimalQuantity &quantity, MicroProps µs, |
198 | 0 | UErrorCode &status) const { |
199 | 0 | fParent->processQuantity(quantity, micros, status); |
200 | 0 | if (U_FAILURE(status)) { |
201 | 0 | return; |
202 | 0 | } |
203 | 0 | quantity.roundToInfinity(); // Enables toDouble |
204 | 0 | MaybeStackVector<Measure> measures = |
205 | 0 | fUnitConverter->convert(quantity.toDouble(), µs.rounder, status); |
206 | 0 | micros.outputUnit = fOutputUnit; |
207 | 0 | if (U_FAILURE(status)) { |
208 | 0 | return; |
209 | 0 | } |
210 | | |
211 | 0 | mixedMeasuresToMicros(measures, &quantity, µs, status); |
212 | 0 | } |
213 | | |
214 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |