From 2d83fc2278c349275ca33ffb9a30789c9c6d4154 Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Mon, 9 Mar 2020 21:33:51 -0500 Subject: [PATCH] ICU-20809 Remove FieldPositionIterator from listformatter.h --- icu4c/source/i18n/listformatter.cpp | 13 -- icu4c/source/i18n/unicode/listformatter.h | 22 --- .../test/intltest/listformattertest.cpp | 134 ++++-------------- .../source/test/intltest/listformattertest.h | 12 +- 4 files changed, 29 insertions(+), 152 deletions(-) diff --git a/icu4c/source/i18n/listformatter.cpp b/icu4c/source/i18n/listformatter.cpp index 413f8ec4987..f8945d55155 100644 --- a/icu4c/source/i18n/listformatter.cpp +++ b/icu4c/source/i18n/listformatter.cpp @@ -433,19 +433,6 @@ UnicodeString& ListFormatter::format( return format(items, nItems, appendTo, -1, offset, errorCode); } -#if !UCONFIG_NO_FORMATTING -UnicodeString& ListFormatter::format( - const UnicodeString items[], - int32_t nItems, - UnicodeString & appendTo, - FieldPositionIterator* posIter, - UErrorCode& errorCode) const { - int32_t offset; - FieldPositionIteratorHandler handler(posIter, errorCode); - return format_(items, nItems, appendTo, -1, offset, &handler, errorCode); -} -#endif - UnicodeString& ListFormatter::format( const UnicodeString items[], int32_t nItems, diff --git a/icu4c/source/i18n/unicode/listformatter.h b/icu4c/source/i18n/unicode/listformatter.h index a0c1ae2efad..59be1cb073b 100644 --- a/icu4c/source/i18n/unicode/listformatter.h +++ b/icu4c/source/i18n/unicode/listformatter.h @@ -30,7 +30,6 @@ U_NAMESPACE_BEGIN -class FieldPositionIterator; class FieldPositionHandler; class FormattedListData; class ListFormatter; @@ -238,27 +237,6 @@ class U_I18N_API ListFormatter : public UObject{ UnicodeString& format(const UnicodeString items[], int32_t n_items, UnicodeString& appendTo, UErrorCode& errorCode) const; -#ifndef U_HIDE_DRAFT_API - /** - * Format a list of strings. - * - * @param items An array of strings to be combined and formatted. - * @param n_items Length of the array items. - * @param appendTo The string to which the formatted result will be - * appended. - * @param posIter On return, can be used to iterate over positions of - * fields generated by this format call. Field values are - * defined in UListFormatterField. Can be NULL. - * @param errorCode ICU error code returned here. - * @return Formatted string combining the elements of items, - * appended to appendTo. - * @draft ICU 63 - */ - UnicodeString& format(const UnicodeString items[], int32_t n_items, - UnicodeString & appendTo, FieldPositionIterator* posIter, - UErrorCode& errorCode) const; -#endif // U_HIDE_DRAFT_API - #if !UCONFIG_NO_FORMATTING #ifndef U_HIDE_DRAFT_API /** diff --git a/icu4c/source/test/intltest/listformattertest.cpp b/icu4c/source/test/intltest/listformattertest.cpp index 516bb9c14cb..f22d8a57c24 100644 --- a/icu4c/source/test/intltest/listformattertest.cpp +++ b/icu4c/source/test/intltest/listformattertest.cpp @@ -38,14 +38,10 @@ void ListFormatterTest::runIndexedTest(int32_t index, UBool exec, TESTCASE_AUTO(TestEnglishGB); TESTCASE_AUTO(TestNynorsk); TESTCASE_AUTO(TestChineseTradHK); - TESTCASE_AUTO(TestFieldPositionIteratorWontCrash); TESTCASE_AUTO(TestFieldPositionIteratorWith1Item); - TESTCASE_AUTO(TestFieldPositionIteratorWith1ItemAndDataBefore); TESTCASE_AUTO(TestFieldPositionIteratorWith2Items); - TESTCASE_AUTO(TestFieldPositionIteratorWith2ItemsAndDataBefore); TESTCASE_AUTO(TestFieldPositionIteratorWith2ItemsPatternShift); TESTCASE_AUTO(TestFieldPositionIteratorWith3Items); - TESTCASE_AUTO(TestFieldPositionIteratorWith3ItemsAndDataBefore); TESTCASE_AUTO(TestFieldPositionIteratorWith3ItemsPatternShift); TESTCASE_AUTO(TestFormattedValue); TESTCASE_AUTO(TestDifferentStyles); @@ -64,10 +60,14 @@ const char* attrString(int32_t attrId) { } } // namespace -void ListFormatterTest::ExpectPositions(FieldPositionIterator& iter, - int32_t *values, int32_t tupleCount) { +void ListFormatterTest::ExpectPositions( + const FormattedList& iter, + int32_t *values, + int32_t tupleCount, + UErrorCode& status) { UBool found[10]; - FieldPosition fp; + ConstrainedFieldPosition cfp; + cfp.constrainCategory(UFIELD_CATEGORY_LIST); if (tupleCount > 10) { assertTrue("internal error, tupleCount too large", FALSE); } else { @@ -75,11 +75,11 @@ void ListFormatterTest::ExpectPositions(FieldPositionIterator& iter, found[i] = FALSE; } } - while (iter.next(fp)) { + while (iter.nextPosition(cfp, status)) { UBool ok = FALSE; - int32_t id = fp.getField(); - int32_t start = fp.getBeginIndex(); - int32_t limit = fp.getEndIndex(); + int32_t id = cfp.getField(); + int32_t start = cfp.getStart(); + int32_t limit = cfp.getLimit(); char buf[128]; sprintf(buf, "%24s %3d %3d %3d", attrString(id), id, start, limit); logln(buf); @@ -248,53 +248,29 @@ void ListFormatterTest::TestEnglishGB() { CheckFourCases("en_GB", one, two, three, four, results, "TestEnglishGB()"); } -void ListFormatterTest::TestFieldPositionIteratorWontCrash() { - IcuTestErrorCode errorCode(*this, "TestFieldPositionIteratorWontCrash()"); - LocalPointer formatter( - ListFormatter::createInstance(Locale("en"), errorCode)); - if (U_FAILURE(errorCode)) { - dataerrln( - "ListFormatter::createInstance(Locale(\"en\"), errorCode) failed in " - "TestFieldPositionIteratorWontCrash: %s", - u_errorName(errorCode)); - return; - } - UnicodeString data[3] = {"a", "bbb", "cc"}; - UnicodeString actualResult; - // make sure NULL as FieldPositionIterator won't caused crash. - formatter->format(data, 3, actualResult, nullptr, errorCode); - if (U_FAILURE(errorCode)) { - dataerrln( - "ListFormatter::format(data, 3, nullptr, errorCode) " - "failed in TestFieldPositionIteratorWontCrash: %s", - u_errorName(errorCode)); - return; - } -} - void ListFormatterTest::RunTestFieldPositionIteratorWithFormatter( ListFormatter* formatter, UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount, - UnicodeString& appendTo, const char16_t *expectedFormatted, + const char16_t *expectedFormatted, const char* testName) { IcuTestErrorCode errorCode(*this, testName); - FieldPositionIterator iter; - formatter->format(data, n, appendTo, &iter, errorCode); + FormattedList fl = formatter->formatStringsToValue(data, n, errorCode); + UnicodeString actual = fl.toString(errorCode); if (U_FAILURE(errorCode)) { dataerrln( "ListFormatter::format(data, %d, &iter, errorCode) " "failed in %s: %s", n, testName, u_errorName(errorCode)); return; } - if (appendTo != expectedFormatted) { - errln(UnicodeString("Expected: |") + expectedFormatted + "|, Actual: |" + appendTo + "|"); + if (actual != expectedFormatted) { + errln(UnicodeString("Expected: |") + expectedFormatted + "|, Actual: |" + actual + "|"); } - ExpectPositions(iter, expected, tupleCount); + ExpectPositions(fl, expected, tupleCount, errorCode); } void ListFormatterTest::RunTestFieldPositionIteratorWithNItemsPatternShift( UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount, - UnicodeString& appendTo, const char16_t *expectedFormatted, + const char16_t *expectedFormatted, const char* testName) { IcuTestErrorCode errorCode(*this, testName); LocalPointer formatter( @@ -307,12 +283,12 @@ void ListFormatterTest::RunTestFieldPositionIteratorWithNItemsPatternShift( } RunTestFieldPositionIteratorWithFormatter( formatter.getAlias(), - data, n, expected, tupleCount, appendTo, expectedFormatted, testName); + data, n, expected, tupleCount, expectedFormatted, testName); } void ListFormatterTest::RunTestFieldPositionIteratorWithNItems( UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount, - UnicodeString& appendTo, const char16_t *expectedFormatted, + const char16_t *expectedFormatted, const char* testName) { IcuTestErrorCode errorCode(*this, testName); LocalPointer formatter( @@ -325,27 +301,7 @@ void ListFormatterTest::RunTestFieldPositionIteratorWithNItems( } RunTestFieldPositionIteratorWithFormatter( formatter.getAlias(), - data, n, expected, tupleCount, appendTo, expectedFormatted, testName); -} - -void ListFormatterTest::TestFieldPositionIteratorWith3ItemsAndDataBefore() { - // 0 1 2 - // 0123456789012345678901234567 - // "Hello World: a, bbb, and cc" - UnicodeString data[3] = {"a", "bbb", "cc"}; - int32_t expected[] = { - ULISTFMT_ELEMENT_FIELD, 13, 14, - ULISTFMT_LITERAL_FIELD, 14, 16, - ULISTFMT_ELEMENT_FIELD, 16, 19, - ULISTFMT_LITERAL_FIELD, 19, 25, - ULISTFMT_ELEMENT_FIELD, 25, 27 - }; - int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); - UnicodeString appendTo(u"Hello World: "); - RunTestFieldPositionIteratorWithNItems( - data, 3, expected, tupleCount, appendTo, - u"Hello World: a, bbb, and cc", - "TestFieldPositionIteratorWith3ItemsAndDataBefore"); + data, n, expected, tupleCount, expectedFormatted, testName); } void ListFormatterTest::TestFieldPositionIteratorWith3Items() { @@ -361,9 +317,8 @@ void ListFormatterTest::TestFieldPositionIteratorWith3Items() { ULISTFMT_ELEMENT_FIELD, 12, 14 }; int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); - UnicodeString appendTo; RunTestFieldPositionIteratorWithNItems( - data, 3, expected, tupleCount, appendTo, + data, 3, expected, tupleCount, u"a, bbb, and cc", "TestFieldPositionIteratorWith3Items"); } @@ -381,31 +336,12 @@ void ListFormatterTest::TestFieldPositionIteratorWith3ItemsPatternShift() { ULISTFMT_ELEMENT_FIELD, 0, 2 }; int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); - UnicodeString appendTo; RunTestFieldPositionIteratorWithNItemsPatternShift( - data, 3, expected, tupleCount, appendTo, + data, 3, expected, tupleCount, u"cc bbb a", "TestFieldPositionIteratorWith3ItemsPatternShift"); } -void ListFormatterTest::TestFieldPositionIteratorWith2ItemsAndDataBefore() { - // 0 1 - // 0123456789012345 - // "Foo: bbb and cc" - UnicodeString data[2] = {"bbb", "cc"}; - int32_t expected[] = { - ULISTFMT_ELEMENT_FIELD, 5, 8, - ULISTFMT_LITERAL_FIELD, 8, 13, - ULISTFMT_ELEMENT_FIELD, 13, 15 - }; - int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); - UnicodeString appendTo("Foo: "); - RunTestFieldPositionIteratorWithNItems( - data, 2, expected, tupleCount, appendTo, - u"Foo: bbb and cc", - "TestFieldPositionIteratorWith2ItemsAndDataBefore"); -} - void ListFormatterTest::TestFieldPositionIteratorWith2Items() { // 0 1 // 01234567890 @@ -417,9 +353,8 @@ void ListFormatterTest::TestFieldPositionIteratorWith2Items() { ULISTFMT_ELEMENT_FIELD, 8, 10 }; int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); - UnicodeString appendTo; RunTestFieldPositionIteratorWithNItems( - data, 2, expected, tupleCount, appendTo, + data, 2, expected, tupleCount, u"bbb and cc", "TestFieldPositionIteratorWith2Items"); } @@ -435,28 +370,12 @@ void ListFormatterTest::TestFieldPositionIteratorWith2ItemsPatternShift() { ULISTFMT_ELEMENT_FIELD, 0, 2 }; int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); - UnicodeString appendTo; RunTestFieldPositionIteratorWithNItemsPatternShift( - data, 2, expected, tupleCount, appendTo, + data, 2, expected, tupleCount, u"cc bbb", "TestFieldPositionIteratorWith2ItemsPatternShift"); } -void ListFormatterTest::TestFieldPositionIteratorWith1ItemAndDataBefore() { - // 012345678 - // "Hello cc" - UnicodeString data[1] = {"cc"}; - int32_t expected[] = { - ULISTFMT_ELEMENT_FIELD, 6, 8 - }; - int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); - UnicodeString appendTo("Hello "); - RunTestFieldPositionIteratorWithNItems( - data, 1, expected, tupleCount, appendTo, - u"Hello cc", - "TestFieldPositionIteratorWith1ItemAndDataBefore"); -} - void ListFormatterTest::TestFieldPositionIteratorWith1Item() { // 012 // "cc" @@ -465,9 +384,8 @@ void ListFormatterTest::TestFieldPositionIteratorWith1Item() { ULISTFMT_ELEMENT_FIELD, 0, 2 }; int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); - UnicodeString appendTo; RunTestFieldPositionIteratorWithNItems( - data, 1, expected, tupleCount, appendTo, + data, 1, expected, tupleCount, u"cc", "TestFieldPositionIteratorWith1Item"); } diff --git a/icu4c/source/test/intltest/listformattertest.h b/icu4c/source/test/intltest/listformattertest.h index f227166d375..f16dd23905d 100644 --- a/icu4c/source/test/intltest/listformattertest.h +++ b/icu4c/source/test/intltest/listformattertest.h @@ -45,13 +45,9 @@ class ListFormatterTest : public IntlTestWithFieldPosition { void TestZulu(); void TestOutOfOrderPatterns(); void Test9946(); - void TestFieldPositionIteratorWontCrash(); void TestFieldPositionIteratorWith1Item(); void TestFieldPositionIteratorWith2Items(); void TestFieldPositionIteratorWith3Items(); - void TestFieldPositionIteratorWith1ItemAndDataBefore(); - void TestFieldPositionIteratorWith2ItemsAndDataBefore(); - void TestFieldPositionIteratorWith3ItemsAndDataBefore(); void TestFieldPositionIteratorWith2ItemsPatternShift(); void TestFieldPositionIteratorWith3ItemsPatternShift(); void TestFormattedValue(); @@ -67,15 +63,15 @@ class ListFormatterTest : public IntlTestWithFieldPosition { const UnicodeString& expected_result, const char* testName); void ExpectPositions( - FieldPositionIterator& iter, + const FormattedList& iter, int32_t *values, - int32_t tupleCount); + int32_t tupleCount, + UErrorCode& status); void RunTestFieldPositionIteratorWithNItems( UnicodeString *data, int32_t n, int32_t *values, int32_t tupleCount, - UnicodeString& appendTo, const char16_t *expectedFormatted, const char* testName); void RunTestFieldPositionIteratorWithNItemsPatternShift( @@ -83,7 +79,6 @@ class ListFormatterTest : public IntlTestWithFieldPosition { int32_t n, int32_t *values, int32_t tupleCount, - UnicodeString& appendTo, const char16_t *expectedFormatted, const char* testName); void RunTestFieldPositionIteratorWithFormatter( @@ -92,7 +87,6 @@ class ListFormatterTest : public IntlTestWithFieldPosition { int32_t n, int32_t *values, int32_t tupleCount, - UnicodeString& appendTo, const char16_t *expectedFormatted, const char* testName); void CheckFourCases( -- 2.40.0