]> granicus.if.org Git - icu/commitdiff
Have getConversionRatesInfo accept a MaybeStackVector instead of only source and...
authorHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Mon, 30 Mar 2020 18:47:40 +0000 (20:47 +0200)
committerHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Mon, 30 Mar 2020 18:51:37 +0000 (20:51 +0200)
icu4c/source/i18n/unitsdata.cpp
icu4c/source/i18n/unitsdata.h
icu4c/source/test/intltest/unitsdatatest.cpp

index 1fe9076001571eb53e6fe38c63f1fc53fd7c6a97..2464ccbefa86b2a57f1bc8d8417401cd40c1244e 100644 (file)
@@ -168,26 +168,23 @@ void processSingleUnit(const MeasureUnit &unit, const UResourceBundle *convertUn
 
 } // namespace
 
-MaybeStackVector<ConversionRateInfo>
-getConversionRatesInfo(const MeasureUnit source, const MeasureUnit target, UErrorCode &status) {
+MaybeStackVector<ConversionRateInfo> U_I18N_API
+getConversionRatesInfo(const MaybeStackVector<MeasureUnit> &units, UErrorCode &status) {
     MaybeStackVector<ConversionRateInfo> result;
     if (U_FAILURE(status)) return result;
 
-    int32_t sourceUnitsLength, targetUnitsLength;
-    LocalArray<MeasureUnit> sourceUnits = source.splitToSingleUnits(sourceUnitsLength, status);
-    LocalArray<MeasureUnit> targetUnits = target.splitToSingleUnits(targetUnitsLength, status);
-
     LocalUResourceBundlePointer unitsBundle(ures_openDirect(NULL, "units", &status));
     StackUResourceBundle convertUnitsBundle;
     ures_getByKey(unitsBundle.getAlias(), "convertUnits", convertUnitsBundle.getAlias(), &status);
     ConversionRateDataSink convertSink(result);
 
-    for (int i = 0; i < sourceUnitsLength; i++) {
-        processSingleUnit(sourceUnits[i], convertUnitsBundle.getAlias(), convertSink, status);
-    }
+    for (int i = 0; i < units.length(); i++) {
+        int32_t numSingleUnits;
+        LocalArray<MeasureUnit> singleUnits = units[i]->splitToSingleUnits(numSingleUnits, status);
 
-    for (int i = 0; i < targetUnitsLength; i++) {
-        processSingleUnit(targetUnits[i], convertUnitsBundle.getAlias(), convertSink, status);
+        for (int i = 0; i < numSingleUnits; i++) {
+            processSingleUnit(singleUnits[i], convertUnitsBundle.getAlias(), convertSink, status);
+        }
     }
     return result;
 }
index a34d5557f50d7df7a832879827353205b1b185ff..5f97143d1e0d7a4ec56dfe6af00b0dc3364705fa 100644 (file)
@@ -38,16 +38,14 @@ class U_I18N_API ConversionRateInfo {
 };
 
 /**
- * Collects and returns ConversionRateInfo needed for conversions from source
- * and to target.
+ * Collects and returns ConversionRateInfo needed for conversions for a set of
+ * units.
  *
- * @param source The source unit (the unit type converted from).
- * @param target The target unit (the unit type converted to).
+ * @param units The units for which to load conversion data.
  * @param status Receives status.
  */
-MaybeStackVector<ConversionRateInfo> U_I18N_API getConversionRatesInfo(MeasureUnit source,
-                                                                       MeasureUnit target,
-                                                                       UErrorCode &status);
+MaybeStackVector<ConversionRateInfo>
+    U_I18N_API getConversionRatesInfo(const MaybeStackVector<MeasureUnit> &units, UErrorCode &status);
 
 U_NAMESPACE_END
 
index f61f85fe822f5f8385ddbe253e1fcf2a182cb50a..bab60f87956713b5bc5ccefeb7256b59d1bc7509 100644 (file)
@@ -59,12 +59,21 @@ void UnitsDataTest::testGetConversionRateInfo() {
         logln("---testing: source=\"%s\", target=\"%s\"", t.sourceUnit, t.targetUnit);
         IcuTestErrorCode status(*this, "testGetConversionRateInfo");
 
-        MeasureUnit sourceUnit = MeasureUnit::forIdentifier(t.sourceUnit, status);
-        MeasureUnit targetUnit = MeasureUnit::forIdentifier(t.targetUnit, status);
-        MaybeStackVector<ConversionRateInfo> conversionInfo =
-            getConversionRatesInfo(sourceUnit, targetUnit, status);
-        if (status.errIfFailureAndReset("getConversionRatesInfo(<%s>, <%s>, ...)",
-                                        sourceUnit.getIdentifier(), targetUnit.getIdentifier())) {
+        MaybeStackVector<MeasureUnit> units;
+        MeasureUnit *item = units.emplaceBack(MeasureUnit::forIdentifier(t.sourceUnit, status));
+        if (!item) {
+            status.set(U_MEMORY_ALLOCATION_ERROR);
+            return;
+        }
+        item = units.emplaceBack(MeasureUnit::forIdentifier(t.targetUnit, status));
+        if (!item) {
+            status.set(U_MEMORY_ALLOCATION_ERROR);
+            return;
+        }
+
+        MaybeStackVector<ConversionRateInfo> conversionInfo = getConversionRatesInfo(units, status);
+        if (status.errIfFailureAndReset("getConversionRatesInfo({<%s>, <%s>}, ...)", t.sourceUnit,
+                                        t.targetUnit)) {
             continue;
         }