Frank Tang | 6fff4cf | 2022-06-15 14:05:01 -0700 | [diff] [blame] | 1 | diff --git a/source/i18n/numrange_fluent.cpp b/source/i18n/numrange_fluent.cpp |
| 2 | index f1060b3c..c36defa3 100644 |
| 3 | --- a/source/i18n/numrange_fluent.cpp |
| 4 | +++ b/source/i18n/numrange_fluent.cpp |
| 5 | @@ -354,6 +354,7 @@ LocalizedNumberRangeFormatter::getFormatter(UErrorCode& status) const { |
| 6 | // Try computing the formatter on our own |
| 7 | auto* temp = new NumberRangeFormatterImpl(fMacros, status); |
| 8 | if (U_FAILURE(status)) { |
| 9 | + delete temp; |
| 10 | return nullptr; |
| 11 | } |
| 12 | if (temp == nullptr) { |
| 13 | diff --git a/source/i18n/numrange_impl.cpp b/source/i18n/numrange_impl.cpp |
| 14 | index 3c440c19..06efc7b2 100644 |
| 15 | --- a/source/i18n/numrange_impl.cpp |
| 16 | +++ b/source/i18n/numrange_impl.cpp |
| 17 | @@ -130,7 +130,7 @@ NumberRangeFormatterImpl::NumberRangeFormatterImpl(const RangeMacroProps& macros |
| 18 | fApproximatelyFormatter(status) { |
| 19 | |
| 20 | const char* nsName = formatterImpl1.getRawMicroProps().nsName; |
| 21 | - if (uprv_strcmp(nsName, formatterImpl2.getRawMicroProps().nsName) != 0) { |
| 22 | + if (!fSameFormatters && uprv_strcmp(nsName, formatterImpl2.getRawMicroProps().nsName) != 0) { |
| 23 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 24 | return; |
| 25 | } |
| 26 | diff --git a/source/test/intltest/numbertest.h b/source/test/intltest/numbertest.h |
| 27 | index c0f2e6fd..25af549e 100644 |
| 28 | --- a/source/test/intltest/numbertest.h |
| 29 | +++ b/source/test/intltest/numbertest.h |
| 30 | @@ -326,6 +326,7 @@ class NumberRangeFormatterTest : public IntlTestWithFieldPosition { |
| 31 | void test21684_Performance(); |
| 32 | void test21358_SignPosition(); |
| 33 | void test21683_StateLeak(); |
| 34 | + void testCreateLNRFFromNumberingSystemInSkeleton(); |
| 35 | |
| 36 | void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = 0) override; |
| 37 | |
| 38 | diff --git a/source/test/intltest/numbertest_range.cpp b/source/test/intltest/numbertest_range.cpp |
| 39 | index a47c675c..1364c860 100644 |
| 40 | --- a/source/test/intltest/numbertest_range.cpp |
| 41 | +++ b/source/test/intltest/numbertest_range.cpp |
| 42 | @@ -57,6 +57,7 @@ void NumberRangeFormatterTest::runIndexedTest(int32_t index, UBool exec, const c |
| 43 | TESTCASE_AUTO(test21684_Performance); |
| 44 | TESTCASE_AUTO(test21358_SignPosition); |
| 45 | TESTCASE_AUTO(test21683_StateLeak); |
| 46 | + TESTCASE_AUTO(testCreateLNRFFromNumberingSystemInSkeleton); |
| 47 | TESTCASE_AUTO_END; |
| 48 | } |
| 49 | |
| 50 | @@ -1031,6 +1032,51 @@ void NumberRangeFormatterTest::test21358_SignPosition() { |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | +void NumberRangeFormatterTest::testCreateLNRFFromNumberingSystemInSkeleton() { |
| 55 | + IcuTestErrorCode status(*this, "testCreateLNRFFromNumberingSystemInSkeleton"); |
| 56 | + { |
| 57 | + LocalizedNumberRangeFormatter lnrf = NumberRangeFormatter::withLocale("en") |
| 58 | + .numberFormatterBoth(NumberFormatter::forSkeleton( |
| 59 | + u".### rounding-mode-half-up", status)); |
| 60 | + UnicodeString actual = lnrf.formatFormattableRange(1, 234, status).toString(status); |
| 61 | + assertEquals("default numbering system", u"1–234", actual); |
| 62 | + status.errIfFailureAndReset("default numbering system"); |
| 63 | + } |
| 64 | + { |
| 65 | + LocalizedNumberRangeFormatter lnrf = NumberRangeFormatter::withLocale("th") |
| 66 | + .numberFormatterBoth(NumberFormatter::forSkeleton( |
| 67 | + u".### rounding-mode-half-up numbering-system/thai", status)); |
| 68 | + UnicodeString actual = lnrf.formatFormattableRange(1, 234, status).toString(status); |
| 69 | + assertEquals("Thai numbering system", u"๑-๒๓๔", actual); |
| 70 | + status.errIfFailureAndReset("thai numbering system"); |
| 71 | + } |
| 72 | + { |
| 73 | + LocalizedNumberRangeFormatter lnrf = NumberRangeFormatter::withLocale("en") |
| 74 | + .numberFormatterBoth(NumberFormatter::forSkeleton( |
| 75 | + u".### rounding-mode-half-up numbering-system/arab", status)); |
| 76 | + UnicodeString actual = lnrf.formatFormattableRange(1, 234, status).toString(status); |
| 77 | + assertEquals("Arabic numbering system", u"١–٢٣٤", actual); |
| 78 | + status.errIfFailureAndReset("arab numbering system"); |
| 79 | + } |
| 80 | + { |
| 81 | + LocalizedNumberRangeFormatter lnrf = NumberRangeFormatter::withLocale("en") |
| 82 | + .numberFormatterFirst(NumberFormatter::forSkeleton(u"numbering-system/arab", status)) |
| 83 | + .numberFormatterSecond(NumberFormatter::forSkeleton(u"numbering-system/arab", status)); |
| 84 | + UnicodeString actual = lnrf.formatFormattableRange(1, 234, status).toString(status); |
| 85 | + assertEquals("Double Arabic numbering system", u"١–٢٣٤", actual); |
| 86 | + status.errIfFailureAndReset("double arab numbering system"); |
| 87 | + } |
| 88 | + { |
| 89 | + LocalizedNumberRangeFormatter lnrf = NumberRangeFormatter::withLocale("en") |
| 90 | + .numberFormatterFirst(NumberFormatter::forSkeleton(u"numbering-system/arab", status)) |
| 91 | + .numberFormatterSecond(NumberFormatter::forSkeleton(u"numbering-system/latn", status)); |
| 92 | + // Note: The error is not set until `formatFormattableRange` because this is where the |
| 93 | + // formatter object gets built. |
| 94 | + lnrf.formatFormattableRange(1, 234, status); |
| 95 | + status.expectErrorAndReset(U_ILLEGAL_ARGUMENT_ERROR); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | void NumberRangeFormatterTest::test21683_StateLeak() { |
| 100 | IcuTestErrorCode status(*this, "test21683_StateLeak"); |
| 101 | UNumberRangeFormatter* nrf = nullptr; |