From 8d75df17e4f835c169ef61be5a36257da0c21f68 Mon Sep 17 00:00:00 2001 From: Younies Mahmoud Date: Mon, 4 May 2020 15:06:05 +0200 Subject: [PATCH] remove extra spaces from the factors --- icu4c/source/i18n/unitconverter.cpp | 20 ++++++++------------ icu4c/source/i18n/unitsdata.cpp | 13 +++++++++++++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/icu4c/source/i18n/unitconverter.cpp b/icu4c/source/i18n/unitconverter.cpp index 4f4283c5bce..f8255e2872f 100644 --- a/icu4c/source/i18n/unitconverter.cpp +++ b/icu4c/source/i18n/unitconverter.cpp @@ -274,18 +274,18 @@ Factor extractFactorConversions(StringPiece stringFactor, UErrorCode &status) { } // Load factor for a single source -Factor* loadSingleFactor(StringPiece source, const ConversionRates &ratesInfo, UErrorCode &status) { +Factor loadSingleFactor(StringPiece source, const ConversionRates &ratesInfo, UErrorCode &status) { const auto conversionUnit = ratesInfo.extractConversionInfo(source, status); - if (U_FAILURE(status)) return nullptr; + if (U_FAILURE(status)) return Factor(); if(conversionUnit == nullptr) { status = U_INTERNAL_PROGRAM_ERROR; - return nullptr; + return Factor(); } Factor result = extractFactorConversions(conversionUnit->factor.toStringPiece(), status); result.offset = strHasDivideSignToDouble(conversionUnit->offset.toStringPiece()); - return &result; + return result; } // Load Factor of a compound source unit. @@ -299,20 +299,16 @@ Factor loadCompoundFactor(const MeasureUnit &source, const ConversionRates &rate for (int32_t i = 0, n = compoundSourceUnit.units.length(); i < n; i++) { auto singleUnit = *compoundSourceUnit.units[i]; // a TempSingleUnit - Factor *singleFactor = loadSingleFactor(singleUnit.identifier, ratesInfo, status); + Factor singleFactor = loadSingleFactor(singleUnit.identifier, ratesInfo, status); if(U_FAILURE(status)) return result; - if (singleFactor == nullptr) { - status = U_INTERNAL_PROGRAM_ERROR; - return result; - } // Apply SiPrefix before the power, because the power may be will flip the factor. - singleFactor->applySiPrefix(singleUnit.siPrefix); + singleFactor.applySiPrefix(singleUnit.siPrefix); // Apply the power of the `dimensionality` - singleFactor->power(singleUnit.dimensionality); + singleFactor.power(singleUnit.dimensionality); - result.multiplyBy(*singleFactor); + result.multiplyBy(singleFactor); } return result; diff --git a/icu4c/source/i18n/unitsdata.cpp b/icu4c/source/i18n/unitsdata.cpp index c92e151604a..f8e8b4d358c 100644 --- a/icu4c/source/i18n/unitsdata.cpp +++ b/icu4c/source/i18n/unitsdata.cpp @@ -15,6 +15,18 @@ U_NAMESPACE_BEGIN namespace { +void trimSpaces(CharString& factor, UErrorCode& status){ + CharString trimmed; + for (int i = 0 ; i < factor.length(); i++) { + if (factor[i] == ' ') continue; + + trimmed.append(factor[i], status); + } + + factor.clear(); + factor.append(trimmed, status); +} + /** * A ResourceSink that collects conversion rate information. * @@ -84,6 +96,7 @@ class ConversionRateDataSink : public ResourceSink { cr->sourceUnit.append(srcUnit, status); cr->baseUnit.appendInvariantChars(baseUnit, status); cr->factor.appendInvariantChars(factor, status); + trimSpaces(cr->factor, status); if (!offset.isBogus()) cr->offset.appendInvariantChars(offset, status); } } -- 2.40.0