// In case the `outputUnits` are `UMEASURE_UNIT_MIXED` such as `foot+inch`. In this case we need more
// converters to convert from the `inputUnit` to the first unit in the `outputUnits`. Then, a
// converter from the first unit in the `outputUnits` to the second unit and so on.
- // For Example:
+ // For Example:
// - inputUnit is `meter`
// - outputUnits is `foot+inch`
// - Therefore, we need to have two converters:
// 2. a converter from `foot` to `inch`
// - Therefore, if the input is `2 meter`:
// 1. convert `meter` to `foot` --> 2 meter to 6.56168 feet
- // 2. convert the residual of 6.56168 feet (0.56168) to inches, which will be (6.74016 inches)
- // 3. then, the final result will be (6 feet and 6.74016 inches)
+ // 2. convert the residual of 6.56168 feet (0.56168) to inches, which will be (6.74016
+ // inches)
+ // 3. then, the final result will be (6 feet and 6.74016 inches)
int32_t length;
auto singleUnits = outputUnits.splitToSingleUnits(length, status);
MaybeStackVector<MeasureUnit> singleUnitsInOrder;
status);
}
- if (U_FAILURE(status)) return;
+ if (U_FAILURE(status)) { return; }
}
units_.appendAll(singleUnitsInOrder, status);
}
UBool ComplexUnitsConverter::greaterThanOrEqual(double quantity, double limit) const {
- // TODO(younies): this assert fails for the first constructor above:
U_ASSERT(unitConverters_.length() > 0);
- // first quantity is the biggest one.
+ // First converter converts to the biggest quantity.
double newQuantity = unitConverters_[0]->convert(quantity);
return newQuantity >= limit;
for (int i = 0, n = unitConverters_.length(); i < n; ++i) {
quantity = (*unitConverters_[i]).convert(quantity);
- if (i < n - 1) { // not last element
+ if (i < n - 1) {
int64_t newQuantity = quantity;
Formattable formattableNewQuantity(newQuantity);
- // 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);
+ // NOTE: Measure would own its MeasureUnit.
+ result.emplaceBack(formattableNewQuantity, std::move(MeasureUnit(*units_[i])), status);
+
+ // Keep the residual of the quantity.
+ // For example: `3.6 feet`, keep only `0.6 feet`
quantity -= newQuantity;
- } else { // Last element
+ } else { // LAST ELEMENT
Formattable formattableQuantity(quantity);
- // 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);
+
+ // NOTE: Measure would own its MeasureUnit.
+ result.emplaceBack(formattableQuantity, std::move(MeasureUnit(*units_[i])), status);
}
}
converterPreferences_.emplaceBack(inputUnit, complexTargetUnit, preference.geq, conversionRates,
status);
- if (U_FAILURE(status)) {
- fprintf(
- stderr,
- "FAILED: converterPreferences_.emplaceBack(<%s>, <%s>, %f, conversionRates, status)\n",
- inputUnit.getIdentifier(), complexTargetUnit.getIdentifier(), preference.geq);
- return;
- }
+ if (U_FAILURE(status)) { return; }
}
}
const auto &converterPreference = *converterPreferences_[i];
- if (i == n - 1) { // Last element
- return converterPreference.converter.convert(quantity, status);
- }
+ // In case of the last converter, the conversion will performed even the value is less than the
+ // limit.
+ if (i == n - 1) { return converterPreference.converter.convert(quantity, status); }
if (converterPreference.converter.greaterThanOrEqual(quantity, converterPreference.limit)) {
return converterPreference.converter.convert(quantity, status);