ComplexUnitsConverter::ComplexUnitsConverter(const MeasureUnit inputUnit, const MeasureUnit outputUnits,
const MaybeStackVector<ConversionRateInfo> &ratesInfo,
UErrorCode &status) {
- auto singleUnits = outputUnits.splitToSingleUnits(status);
+ int32_t length;
+ auto singleUnits = outputUnits.splitToSingleUnits(length, status);
MaybeStackVector<MeasureUnit> singleUnitsInOrder;
- for (int i = 0, n = singleUnits.length(); i < n; ++i) {
+ for (int i = 0; i < length; ++i) {
// TODO(younies): ensure units being in order in phase 2. Now, the units in order by default.
singleUnitsInOrder.emplaceBack(singleUnits[i]);
}
U_ASSERT(unitConverters_.length() > 0);
// first quantity is the biggest one.
- double newQuantity = (*unitConverters_[0]).convert(quantity);
+ double newQuantity = unitConverters_[0]->convert(quantity);
return newQuantity >= limit;
}
if (i < n - 1) { // not last element
int64_t newQuantity = quantity;
Formattable formattableNewQuantity(newQuantity);
- result.emplaceBack(Measure(formattableNewQuantity, units_[i], status));
+ // Measure wants to own its MeasureUnit. For now, this copies it.
+ // TODO(younies): consider whether ownership transfer would be
+ // reasonable? (If not, just delete this comment?)
+ result.emplaceBack(formattableNewQuantity, new MeasureUnit(*units_[i]), status);
quantity -= newQuantity;
} else { // Last element
Formattable formattableQuantity(quantity);
- result.emplaceBack(Measure(formattableQuantity, units_[i], status));
+ // Measure wants to own its MeasureUnit. For now, this copies it.
+ // TODO(younies): consider whether ownership transfer would be
+ // reasonable? (If not, just delete this comment?)
+ result.emplaceBack(formattableQuantity, new MeasureUnit(*units_[i]), status);
}
}
MeasureUnit extractTarget(MeasureUnit source, const MaybeStackVector<ConversionRateInfo> &ratesInfo,
UErrorCode &status) {
MeasureUnit result; // Empty unit.
- auto singleUnits = source.splitToSingleUnits(status);
+ int32_t singleUnitsLength;
+ auto singleUnits = source.splitToSingleUnits(singleUnitsLength, status);
- for (int i = 0; i < singleUnits.length(); i++) {
+ for (int i = 0; i < singleUnitsLength; i++) {
const auto &singleUnit = singleUnits[i];
StringPiece target = getTarget(singleUnit.getIdentifier(), ratesInfo, status);
if (U_FAILURE(status)) return result;
MeasureUnit targetUnit = MeasureUnit::forIdentifier(target, status);
- auto targetSingleUnits = targetUnit.splitToSingleUnits(status);
+ int32_t targetSingleUnitsLength;
+ auto targetSingleUnits = targetUnit.splitToSingleUnits(targetSingleUnitsLength, status);
if (U_FAILURE(status)) return result;
- for (int i = 0, n = targetSingleUnits.length(); i < n; ++i) {
- auto tempTargetUnit = TempSingleUnit::forMeasureUnit(targetSingleUnits[i], status);
- tempTargetUnit.dimensionality = singleUnit.getDimensionality(status);
+ for (int i = 0; i < targetSingleUnitsLength; ++i) {
+ auto targetUnit =
+ targetSingleUnits[i].withDimensionality(singleUnit.getDimensionality(status), status);
if (U_FAILURE(status)) return result;
- auto targetUnits = tempTargetUnit.build(status);
- if (U_FAILURE(status)) return result;
-
- result = result.product(targetUnits, status);
+ result = result.product(targetUnit, status);
if (U_FAILURE(status)) return result;
}
}
ratesInfo, status);
}
-double UnitConverter::convert(double inputValue) {
+double UnitConverter::convert(double inputValue) const {
double result =
inputValue + conversionRate_.sourceOffset; // Reset the input to the target zero index.
// Convert the quantity to from the source scale to the target scale.
* @param output_value the value that holds the result of the conversion.
* @param status
*/
- double convert(double inputValue);
+ double convert(double inputValue) const;
private:
ConversionRate conversionRate_;