blob: 0be11467a08aef017b21c7f830117b9e6ce08d63 [file] [log] [blame]
Victor Chang978167a2021-01-18 17:56:33 +00001// © 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
24using namespace icu;
25using namespace icu::number;
26using namespace icu::number::impl;
27using icu::StringSegment;
28using icu::units::ConversionRates;
29
30// Copy constructor
31Usage::Usage(const Usage &other) : Usage() {
32 this->operator=(other);
33}
34
35// Copy assignment operator
36Usage &Usage::operator=(const Usage &other) {
Victor Chang92c98b52021-04-27 16:37:23 +010037 if (this == &other) { return *this; } // self-assignment: no-op
Victor Chang978167a2021-01-18 17:56:33 +000038 fLength = 0;
39 fError = other.fError;
40 if (fUsage != nullptr) {
41 uprv_free(fUsage);
42 fUsage = nullptr;
43 }
44 if (other.fUsage == nullptr) {
45 return *this;
46 }
47 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 Usage.
50 return *this;
51 }
52 fUsage = (char *)uprv_malloc(other.fLength + 1);
53 if (fUsage == nullptr) {
54 fError = U_MEMORY_ALLOCATION_ERROR;
55 return *this;
56 }
57 fLength = other.fLength;
58 uprv_strncpy(fUsage, other.fUsage, fLength + 1);
59 return *this;
60}
61
62// Move constructor
63Usage::Usage(Usage &&src) U_NOEXCEPT : fUsage(src.fUsage), fLength(src.fLength), fError(src.fError) {
64 // Take ownership away from src if necessary
65 src.fUsage = nullptr;
66}
67
68// Move assignment operator
69Usage &Usage::operator=(Usage &&src) U_NOEXCEPT {
70 if (this == &src) {
71 return *this;
72 }
73 if (fUsage != nullptr) {
74 uprv_free(fUsage);
75 }
76 fUsage = src.fUsage;
77 fLength = src.fLength;
78 fError = src.fError;
79 // Take ownership away from src if necessary
80 src.fUsage = nullptr;
81 return *this;
82}
83
84Usage::~Usage() {
85 if (fUsage != nullptr) {
86 uprv_free(fUsage);
87 fUsage = nullptr;
88 }
89}
90
91void Usage::set(StringPiece value) {
92 if (fUsage != nullptr) {
93 uprv_free(fUsage);
94 fUsage = nullptr;
95 }
96 fLength = value.length();
97 fUsage = (char *)uprv_malloc(fLength + 1);
98 if (fUsage == nullptr) {
99 fLength = 0;
100 fError = U_MEMORY_ALLOCATION_ERROR;
101 return;
102 }
103 uprv_strncpy(fUsage, value.data(), fLength);
104 fUsage[fLength] = 0;
105}
106
107// Populates micros.mixedMeasures and modifies quantity, based on the values in
108// measures.
109void mixedMeasuresToMicros(const MaybeStackVector<Measure> &measures, DecimalQuantity *quantity,
110 MicroProps *micros, UErrorCode status) {
111 micros->mixedMeasuresCount = measures.length() - 1;
112 if (micros->mixedMeasuresCount > 0) {
113#ifdef U_DEBUG
114 U_ASSERT(micros->outputUnit.getComplexity(status) == UMEASURE_UNIT_MIXED);
115 U_ASSERT(U_SUCCESS(status));
116 // Check that we received measurements with the expected MeasureUnits:
117 MeasureUnitImpl temp;
118 const MeasureUnitImpl& impl = MeasureUnitImpl::forMeasureUnit(micros->outputUnit, temp, status);
119 U_ASSERT(U_SUCCESS(status));
120 U_ASSERT(measures.length() == impl.units.length());
121 for (int32_t i = 0; i < measures.length(); i++) {
122 U_ASSERT(measures[i]->getUnit() == impl.units[i]->build(status));
123 }
124 (void)impl;
125#endif
126 // Mixed units: except for the last value, we pass all values to the
127 // LongNameHandler via micros->mixedMeasures.
128 if (micros->mixedMeasures.getCapacity() < micros->mixedMeasuresCount) {
129 if (micros->mixedMeasures.resize(micros->mixedMeasuresCount) == nullptr) {
130 status = U_MEMORY_ALLOCATION_ERROR;
131 return;
132 }
133 }
134 for (int32_t i = 0; i < micros->mixedMeasuresCount; i++) {
135 micros->mixedMeasures[i] = measures[i]->getNumber().getInt64();
136 }
137 } else {
138 micros->mixedMeasuresCount = 0;
139 }
140 // The last value (potentially the only value) gets passed on via quantity.
141 quantity->setToDouble(measures[measures.length() - 1]->getNumber().getDouble());
142}
143
144UsagePrefsHandler::UsagePrefsHandler(const Locale &locale,
145 const MeasureUnit &inputUnit,
146 const StringPiece usage,
147 const MicroPropsGenerator *parent,
148 UErrorCode &status)
149 : fUnitsRouter(inputUnit, StringPiece(locale.getCountry()), usage, status),
150 fParent(parent) {
151}
152
153void UsagePrefsHandler::processQuantity(DecimalQuantity &quantity, MicroProps &micros,
154 UErrorCode &status) const {
155 fParent->processQuantity(quantity, micros, status);
156 if (U_FAILURE(status)) {
157 return;
158 }
159
160 quantity.roundToInfinity(); // Enables toDouble
161 const units::RouteResult routed = fUnitsRouter.route(quantity.toDouble(), &micros.rounder, status);
162 if (U_FAILURE(status)) {
163 return;
164 }
165 const MaybeStackVector<Measure>& routedMeasures = routed.measures;
166 micros.outputUnit = routed.outputUnit.copy(status).build(status);
167 if (U_FAILURE(status)) {
168 return;
169 }
170
171 mixedMeasuresToMicros(routedMeasures, &quantity, &micros, status);
172}
173
174UnitConversionHandler::UnitConversionHandler(const MeasureUnit &inputUnit, const MeasureUnit &outputUnit,
175 const MicroPropsGenerator *parent, UErrorCode &status)
176 : fOutputUnit(outputUnit), fParent(parent) {
177 MeasureUnitImpl tempInput, tempOutput;
178 const MeasureUnitImpl &inputUnitImpl = MeasureUnitImpl::forMeasureUnit(inputUnit, tempInput, status);
179 const MeasureUnitImpl &outputUnitImpl =
180 MeasureUnitImpl::forMeasureUnit(outputUnit, tempOutput, status);
181
182 // TODO: this should become an initOnce thing? Review with other
183 // ConversionRates usages.
184 ConversionRates conversionRates(status);
185 if (U_FAILURE(status)) {
186 return;
187 }
188 fUnitConverter.adoptInsteadAndCheckErrorCode(
189 new ComplexUnitsConverter(inputUnitImpl, outputUnitImpl, conversionRates, status), status);
190}
191
192void UnitConversionHandler::processQuantity(DecimalQuantity &quantity, MicroProps &micros,
193 UErrorCode &status) const {
194 fParent->processQuantity(quantity, micros, status);
195 if (U_FAILURE(status)) {
196 return;
197 }
198 quantity.roundToInfinity(); // Enables toDouble
199 MaybeStackVector<Measure> measures =
200 fUnitConverter->convert(quantity.toDouble(), &micros.rounder, status);
201 micros.outputUnit = fOutputUnit;
202 if (U_FAILURE(status)) {
203 return;
204 }
205
206 mixedMeasuresToMicros(measures, &quantity, &micros, status);
207}
208
209#endif /* #if !UCONFIG_NO_FORMATTING */