]> granicus.if.org Git - icu/commitdiff
remove extra spaces from the factors
authorYounies Mahmoud <younies@younies-macbookpro4.roam.corp.google.com>
Mon, 4 May 2020 13:06:05 +0000 (15:06 +0200)
committerYounies Mahmoud <younies@younies-macbookpro4.roam.corp.google.com>
Mon, 4 May 2020 13:06:05 +0000 (15:06 +0200)
icu4c/source/i18n/unitconverter.cpp
icu4c/source/i18n/unitsdata.cpp

index 4f4283c5bce7a0882c23d2e6ded4e4fc01093d5a..f8255e2872f964503f25fe5c26ea5f54acf0c230 100644 (file)
@@ -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;
index c92e151604a33b9b2a892dc70a2274d5fc4c81ad..f8e8b4d358c363ff4a5a496dad06448b64c010a9 100644 (file)
@@ -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);
             }
         }