From 5e28c0c94157b4d02a854904855f13d31d74db97 Mon Sep 17 00:00:00 2001 From: younies Date: Tue, 16 Jun 2020 20:14:44 +0200 Subject: [PATCH] fix findAndMerge function --- icu4c/source/i18n/measunit_extra.cpp | 20 ++++++++++---------- icu4c/source/i18n/unitconverter.cpp | 6 ++++-- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/icu4c/source/i18n/measunit_extra.cpp b/icu4c/source/i18n/measunit_extra.cpp index 3f1891ac75a..f7ed5b9d295 100644 --- a/icu4c/source/i18n/measunit_extra.cpp +++ b/icu4c/source/i18n/measunit_extra.cpp @@ -769,15 +769,16 @@ MeasureUnit MeasureUnit::product(const MeasureUnit& other, UErrorCode& status) c return std::move(impl).build(status); } +namespace { /** - * Searches the `simplifiedUnits` for a unit with the same base identifier and the same SI prefix. - * For example, `square-meter` and `cubic-meter` but not `meter` and `centimeter`. - * After that, the matched units will be merged. + * Searches the `simplifiedUnits` for a unit with the same base identifier and the same SI prefix + * (For example, `square-meter` and `cubic-meter` but not `meter` and `centimeter`). After that, the + * matched units will be merged. Otherwise, the `newUnitImpl` will be appended. */ void findAndMerge(MeasureUnitImpl &simplifiedUnitsImpl, const SingleUnitImpl &newUnitImpl, UErrorCode &status) { for (int i = 0, n = simplifiedUnitsImpl.units.length(); i < n; i++) { - auto& singleSimplifiedUnitImpl = *(simplifiedUnitsImpl.units[i]); + auto &singleSimplifiedUnitImpl = *(simplifiedUnitsImpl.units[i]); if (singleSimplifiedUnitImpl.identifier == newUnitImpl.identifier && singleSimplifiedUnitImpl.siPrefix == newUnitImpl.siPrefix) { @@ -785,9 +786,9 @@ void findAndMerge(MeasureUnitImpl &simplifiedUnitsImpl, const SingleUnitImpl &ne return; } } - - simplifiedUnitsImpl.units.emplaceBackAndCheckErrorCode(status, newUnitImpl); + simplifiedUnitsImpl.append(newUnitImpl, status); } +} // namespace MeasureUnit MeasureUnit::simplify(UErrorCode &status) const { if (this->getComplexity(status) == UMeasureUnitComplexity::UMEASURE_UNIT_MIXED) { @@ -797,10 +798,9 @@ MeasureUnit MeasureUnit::simplify(UErrorCode &status) const { MeasureUnitImpl resultImpl; - int32_t outCount; - auto singleUnits = this->splitToSingleUnits(outCount, status); - for (int i = 0; i < outCount; ++i) { - findAndMerge(resultImpl, SingleUnitImpl::forMeasureUnit(singleUnits[i], status), status); + const auto &units = (*MeasureUnitImpl::get(*this)).units; + for (int i = 0, n = units.length(); i < n; ++i) { + findAndMerge(resultImpl, *units[i], status); } return std::move(resultImpl).build(status); diff --git a/icu4c/source/i18n/unitconverter.cpp b/icu4c/source/i18n/unitconverter.cpp index f73113ab9c5..dd4cc202106 100644 --- a/icu4c/source/i18n/unitconverter.cpp +++ b/icu4c/source/i18n/unitconverter.cpp @@ -495,8 +495,10 @@ double UnitConverter::convert(double inputValue) const { result = 1.0 / result; } - // Multiply the result by 1.000,000,001 to fix the deterioration from using `double` - // TODO: remove the multiplication by 1.000,000,001 after using `decNumber` + // TODO: remove the multiplication by 1.000,000,000,001 after using `decNumber` + + // Multiply the result by 1.000,000,000,001 to fix the deterioration from using `double` (the + // deterioration is around 15 to 17 decimal digit). return result * 1.000000000001; } -- 2.40.0