]> granicus.if.org Git - icu/commitdiff
Add getUnitCategory.
authorHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Mon, 11 May 2020 22:43:03 +0000 (00:43 +0200)
committerHugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Mon, 11 May 2020 22:56:14 +0000 (00:56 +0200)
icu4c/source/i18n/unitsdata.cpp
icu4c/source/i18n/unitsdata.h

index 9c7b18c0a7d3a3f6b323842852a01b4287f35e00..3c956011d29474848d0831b239e1901f3315c8f5 100644 (file)
@@ -375,6 +375,30 @@ const ConversionRateInfo *ConversionRates::extractConversionInfo(StringPiece sou
     return nullptr;
 }
 
+// TODO/FIXME: baseUnitIdentifier seems onerous? If this function could access
+// extractCompoundBaseUnit directly, we could support any input unit identifier.
+// Shall we move extractCompoundBaseUnit to unitsdata.cpp?
+CharString U_I18N_API getUnitCategory(const char *baseUnitIdentifier, UErrorCode &status) {
+    CharString result;
+    LocalUResourceBundlePointer unitsBundle(ures_openDirect(NULL, "units", &status));
+    LocalUResourceBundlePointer unitQuantities(
+        ures_getByKey(unitsBundle.getAlias(), "unitQuantities", NULL, &status));
+    int32_t categoryLength;
+    if (U_FAILURE(status)) { return result; }
+    const UChar *uCategory =
+        ures_getStringByKey(unitQuantities.getAlias(), baseUnitIdentifier, &categoryLength, &status);
+    if (U_FAILURE(status)) {
+        // TODO: manually dealing with consumption-inverse
+        if (uprv_strcmp(baseUnitIdentifier, "meter-per-cubic-meter") == 0) {
+            status = U_ZERO_ERROR;
+            uCategory = ures_getStringByKey(unitQuantities.getAlias(), "cubic-meter-per-meter",
+                                            &categoryLength, &status);
+        }
+    }
+    result.appendInvariantChars(uCategory, categoryLength, status);
+    return result;
+}
+
 U_I18N_API UnitPreferences::UnitPreferences(UErrorCode &status) {
     LocalUResourceBundlePointer unitsBundle(ures_openDirect(NULL, "units", &status));
     UnitPreferencesSink sink(&unitPrefs_, &metadata_);
index 6961b301621ec4d936e00016ffc623bc9be1ff1c..c26914ffbe951a88dc6f8e6eab8add88d09cf673 100644 (file)
@@ -76,6 +76,8 @@ class U_I18N_API ConversionRates {
     MaybeStackVector<ConversionRateInfo> conversionInfo_;
 };
 
+CharString U_I18N_API getUnitCategory(const char *baseUnitIdentifier, UErrorCode &status);
+
 // Encapsulates unitPreferenceData information from units resources, specifying
 // a sequence of output unit preferences.
 struct U_I18N_API UnitPreference : public UMemory {