Skip to content

Commit ccde962

Browse files
committed
ICU-23265 Moved the alias handling code into MeasureUnit::validateAndGet(). Fixed style-checker errors in Java.
1 parent 78be065 commit ccde962

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

icu4c/source/i18n/measunit.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,16 +2767,21 @@ StringEnumeration* MeasureUnit::getAvailableTypes(UErrorCode &errorCode) {
27672767
}
27682768

27692769
bool MeasureUnit::validateAndGet(StringPiece type, StringPiece subtype, MeasureUnit &result) {
2770-
// Find the type index using binary search
2770+
// Find the type and subtype indices using binary search
27712771
int32_t typeIdx = binarySearch(gTypes, 0, UPRV_LENGTHOF(gTypes), type);
2772-
if (typeIdx == -1) {
2773-
return false; // Type not found
2774-
}
2772+
int32_t subtypeIdx = typeIdx >= 0 ? binarySearch(typeIdx, subtype) : -1;
27752773

2776-
// Find the subtype within the type's range using binary search
2777-
int32_t subtypeIdx = binarySearch(typeIdx, subtype);
2778-
if (subtypeIdx == -1) {
2779-
return false; // Subtype not found
2774+
if (typeIdx >= 0 && subtypeIdx < 0) {
2775+
// if we did find the type, but didn't find the subtype, that might be because the sybtype name
2776+
// is an alias-- try using MeasureUnit::forIdentifier(), which will resolve aliases
2777+
UErrorCode localStatus = U_ZERO_ERROR;
2778+
MeasureUnit tempUnit = MeasureUnit::forIdentifier(subtype, localStatus);
2779+
if (U_SUCCESS(localStatus)) {
2780+
subtypeIdx = tempUnit.fSubTypeId + gOffsets[tempUnit.fTypeId];
2781+
}
2782+
}
2783+
if (typeIdx < 0 || subtypeIdx < 0) {
2784+
return false;
27802785
}
27812786

27822787
// Create the MeasureUnit and return it

icu4c/source/i18n/number_skeletons.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,13 +1078,6 @@ void blueprint_helpers::parseMeasureUnitOption(const StringSegment& segment, Mac
10781078
return;
10791079
}
10801080

1081-
UErrorCode localStatus = U_ZERO_ERROR;
1082-
unit = MeasureUnit::forIdentifier(subType.data(), localStatus);
1083-
if (U_SUCCESS(localStatus) && uprv_strcmp(type.data(), unit.getType()) == 0) {
1084-
macros.unit = unit;
1085-
return;
1086-
}
1087-
10881081
status = U_NUMBER_SKELETON_SYNTAX_ERROR;
10891082
}
10901083

icu4j/main/core/src/test/java/com/ibm/icu/dev/test/number/NumberSkeletonTest.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -560,22 +560,28 @@ class TestCase {
560560
this.expectedResult = expectedResult;
561561
}
562562
}
563-
TestCase[] testCases = new TestCase[] {
564-
new TestCase("measure-unit/concentr-part-per-1e6", "3.14 ppm"),
565-
new TestCase("measure-unit/concentr-part-per-million", "3.14 ppm"),
566-
new TestCase("measure-unit/concentr-permillion", "3.14 ppm"),
567-
new TestCase("measure-unit/concentr-milligram-ofglucose-per-deciliter", "3.14 mg/dL"),
568-
new TestCase("measure-unit/concentr-milligram-per-deciliter", "3.14 mg/dL"),
569-
new TestCase("measure-unit/mass-tonne", "3.14 t"),
570-
new TestCase("measure-unit/mass-metric-ton", "3.14 t"),
571-
};
563+
564+
TestCase[] testCases =
565+
new TestCase[] {
566+
new TestCase("measure-unit/concentr-part-per-1e6", "3.14 ppm"),
567+
new TestCase("measure-unit/concentr-part-per-million", "3.14 ppm"),
568+
new TestCase("measure-unit/concentr-permillion", "3.14 ppm"),
569+
new TestCase("measure-unit/concentr-milligram-ofglucose-per-deciliter", "3.14 mg/dL"),
570+
new TestCase("measure-unit/concentr-milligram-per-deciliter", "3.14 mg/dL"),
571+
new TestCase("measure-unit/mass-tonne", "3.14 t"),
572+
new TestCase("measure-unit/mass-metric-ton", "3.14 t"),
573+
};
572574

573575
for (TestCase testCase : testCases) {
574576
try {
575-
LocalizedNumberFormatter nf = NumberFormatter.forSkeleton(testCase.skeleton).locale(Locale.US);
577+
LocalizedNumberFormatter nf =
578+
NumberFormatter.forSkeleton(testCase.skeleton).locale(Locale.US);
576579
String actualResult = nf.format(3.14).toString();
577580

578-
assertEquals("Wrong result for " + testCase.skeleton + ":", testCase.expectedResult, actualResult);
581+
assertEquals(
582+
"Wrong result for " + testCase.skeleton + ":",
583+
testCase.expectedResult,
584+
actualResult);
579585
} catch (SkeletonSyntaxException e) {
580586
fail(testCase.skeleton);
581587
}

0 commit comments

Comments
 (0)