From 636ed335381b954ec1ce49bbf7362bcc223107e6 Mon Sep 17 00:00:00 2001 From: Hugo van der Merwe <17109322+hugovdm@users.noreply.github.com> Date: Wed, 25 Mar 2020 11:35:08 +0100 Subject: [PATCH] Fixes to make complexunitsconverter and unitconverter compile again. --- icu4c/source/i18n/complexunitsconverter.cpp | 17 ++++++++++++----- icu4c/source/i18n/unitconverter.cpp | 21 ++++++++++----------- icu4c/source/i18n/unitconverter.h | 2 +- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/icu4c/source/i18n/complexunitsconverter.cpp b/icu4c/source/i18n/complexunitsconverter.cpp index 5a0a0879e74..e9dae22388d 100644 --- a/icu4c/source/i18n/complexunitsconverter.cpp +++ b/icu4c/source/i18n/complexunitsconverter.cpp @@ -18,9 +18,10 @@ U_NAMESPACE_BEGIN ComplexUnitsConverter::ComplexUnitsConverter(const MeasureUnit inputUnit, const MeasureUnit outputUnits, const MaybeStackVector &ratesInfo, UErrorCode &status) { - auto singleUnits = outputUnits.splitToSingleUnits(status); + int32_t length; + auto singleUnits = outputUnits.splitToSingleUnits(length, status); MaybeStackVector 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]); } @@ -59,7 +60,7 @@ UBool ComplexUnitsConverter::greaterThanOrEqual(double quantity, double limit) c 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; } @@ -72,12 +73,18 @@ MaybeStackVector ComplexUnitsConverter::convert(double quantity, UError 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); } } diff --git a/icu4c/source/i18n/unitconverter.cpp b/icu4c/source/i18n/unitconverter.cpp index 32761f4af35..7366613ab65 100644 --- a/icu4c/source/i18n/unitconverter.cpp +++ b/icu4c/source/i18n/unitconverter.cpp @@ -427,27 +427,26 @@ StringPiece getTarget(StringPiece source, const MaybeStackVector &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; } } @@ -484,7 +483,7 @@ UnitConverter::UnitConverter(MeasureUnit source, MeasureUnit target, 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. diff --git a/icu4c/source/i18n/unitconverter.h b/icu4c/source/i18n/unitconverter.h index 9549f5ee383..0963b1ec9d6 100644 --- a/icu4c/source/i18n/unitconverter.h +++ b/icu4c/source/i18n/unitconverter.h @@ -55,7 +55,7 @@ class U_I18N_API UnitConverter { * @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_; -- 2.40.0