ICU-20809 Remove FieldPositionIterator from listformatter.h

This commit is contained in:
Shane F. Carr 2020-03-09 21:33:51 -05:00
parent 9aed97703e
commit 2d83fc2278
4 changed files with 29 additions and 152 deletions

View File

@ -433,19 +433,6 @@ UnicodeString& ListFormatter::format(
return format(items, nItems, appendTo, -1, offset, errorCode); 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( UnicodeString& ListFormatter::format(
const UnicodeString items[], const UnicodeString items[],
int32_t nItems, int32_t nItems,

View File

@ -30,7 +30,6 @@
U_NAMESPACE_BEGIN U_NAMESPACE_BEGIN
class FieldPositionIterator;
class FieldPositionHandler; class FieldPositionHandler;
class FormattedListData; class FormattedListData;
class ListFormatter; class ListFormatter;
@ -238,27 +237,6 @@ class U_I18N_API ListFormatter : public UObject{
UnicodeString& format(const UnicodeString items[], int32_t n_items, UnicodeString& format(const UnicodeString items[], int32_t n_items,
UnicodeString& appendTo, UErrorCode& errorCode) const; 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 #if !UCONFIG_NO_FORMATTING
#ifndef U_HIDE_DRAFT_API #ifndef U_HIDE_DRAFT_API
/** /**

View File

@ -38,14 +38,10 @@ void ListFormatterTest::runIndexedTest(int32_t index, UBool exec,
TESTCASE_AUTO(TestEnglishGB); TESTCASE_AUTO(TestEnglishGB);
TESTCASE_AUTO(TestNynorsk); TESTCASE_AUTO(TestNynorsk);
TESTCASE_AUTO(TestChineseTradHK); TESTCASE_AUTO(TestChineseTradHK);
TESTCASE_AUTO(TestFieldPositionIteratorWontCrash);
TESTCASE_AUTO(TestFieldPositionIteratorWith1Item); TESTCASE_AUTO(TestFieldPositionIteratorWith1Item);
TESTCASE_AUTO(TestFieldPositionIteratorWith1ItemAndDataBefore);
TESTCASE_AUTO(TestFieldPositionIteratorWith2Items); TESTCASE_AUTO(TestFieldPositionIteratorWith2Items);
TESTCASE_AUTO(TestFieldPositionIteratorWith2ItemsAndDataBefore);
TESTCASE_AUTO(TestFieldPositionIteratorWith2ItemsPatternShift); TESTCASE_AUTO(TestFieldPositionIteratorWith2ItemsPatternShift);
TESTCASE_AUTO(TestFieldPositionIteratorWith3Items); TESTCASE_AUTO(TestFieldPositionIteratorWith3Items);
TESTCASE_AUTO(TestFieldPositionIteratorWith3ItemsAndDataBefore);
TESTCASE_AUTO(TestFieldPositionIteratorWith3ItemsPatternShift); TESTCASE_AUTO(TestFieldPositionIteratorWith3ItemsPatternShift);
TESTCASE_AUTO(TestFormattedValue); TESTCASE_AUTO(TestFormattedValue);
TESTCASE_AUTO(TestDifferentStyles); TESTCASE_AUTO(TestDifferentStyles);
@ -64,10 +60,14 @@ const char* attrString(int32_t attrId) {
} }
} // namespace } // namespace
void ListFormatterTest::ExpectPositions(FieldPositionIterator& iter, void ListFormatterTest::ExpectPositions(
int32_t *values, int32_t tupleCount) { const FormattedList& iter,
int32_t *values,
int32_t tupleCount,
UErrorCode& status) {
UBool found[10]; UBool found[10];
FieldPosition fp; ConstrainedFieldPosition cfp;
cfp.constrainCategory(UFIELD_CATEGORY_LIST);
if (tupleCount > 10) { if (tupleCount > 10) {
assertTrue("internal error, tupleCount too large", FALSE); assertTrue("internal error, tupleCount too large", FALSE);
} else { } else {
@ -75,11 +75,11 @@ void ListFormatterTest::ExpectPositions(FieldPositionIterator& iter,
found[i] = FALSE; found[i] = FALSE;
} }
} }
while (iter.next(fp)) { while (iter.nextPosition(cfp, status)) {
UBool ok = FALSE; UBool ok = FALSE;
int32_t id = fp.getField(); int32_t id = cfp.getField();
int32_t start = fp.getBeginIndex(); int32_t start = cfp.getStart();
int32_t limit = fp.getEndIndex(); int32_t limit = cfp.getLimit();
char buf[128]; char buf[128];
sprintf(buf, "%24s %3d %3d %3d", attrString(id), id, start, limit); sprintf(buf, "%24s %3d %3d %3d", attrString(id), id, start, limit);
logln(buf); logln(buf);
@ -248,53 +248,29 @@ void ListFormatterTest::TestEnglishGB() {
CheckFourCases("en_GB", one, two, three, four, results, "TestEnglishGB()"); CheckFourCases("en_GB", one, two, three, four, results, "TestEnglishGB()");
} }
void ListFormatterTest::TestFieldPositionIteratorWontCrash() {
IcuTestErrorCode errorCode(*this, "TestFieldPositionIteratorWontCrash()");
LocalPointer<ListFormatter> 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( void ListFormatterTest::RunTestFieldPositionIteratorWithFormatter(
ListFormatter* formatter, ListFormatter* formatter,
UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount, UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount,
UnicodeString& appendTo, const char16_t *expectedFormatted, const char16_t *expectedFormatted,
const char* testName) { const char* testName) {
IcuTestErrorCode errorCode(*this, testName); IcuTestErrorCode errorCode(*this, testName);
FieldPositionIterator iter; FormattedList fl = formatter->formatStringsToValue(data, n, errorCode);
formatter->format(data, n, appendTo, &iter, errorCode); UnicodeString actual = fl.toString(errorCode);
if (U_FAILURE(errorCode)) { if (U_FAILURE(errorCode)) {
dataerrln( dataerrln(
"ListFormatter::format(data, %d, &iter, errorCode) " "ListFormatter::format(data, %d, &iter, errorCode) "
"failed in %s: %s", n, testName, u_errorName(errorCode)); "failed in %s: %s", n, testName, u_errorName(errorCode));
return; return;
} }
if (appendTo != expectedFormatted) { if (actual != expectedFormatted) {
errln(UnicodeString("Expected: |") + expectedFormatted + "|, Actual: |" + appendTo + "|"); errln(UnicodeString("Expected: |") + expectedFormatted + "|, Actual: |" + actual + "|");
} }
ExpectPositions(iter, expected, tupleCount); ExpectPositions(fl, expected, tupleCount, errorCode);
} }
void ListFormatterTest::RunTestFieldPositionIteratorWithNItemsPatternShift( void ListFormatterTest::RunTestFieldPositionIteratorWithNItemsPatternShift(
UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount, UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount,
UnicodeString& appendTo, const char16_t *expectedFormatted, const char16_t *expectedFormatted,
const char* testName) { const char* testName) {
IcuTestErrorCode errorCode(*this, testName); IcuTestErrorCode errorCode(*this, testName);
LocalPointer<ListFormatter> formatter( LocalPointer<ListFormatter> formatter(
@ -307,12 +283,12 @@ void ListFormatterTest::RunTestFieldPositionIteratorWithNItemsPatternShift(
} }
RunTestFieldPositionIteratorWithFormatter( RunTestFieldPositionIteratorWithFormatter(
formatter.getAlias(), formatter.getAlias(),
data, n, expected, tupleCount, appendTo, expectedFormatted, testName); data, n, expected, tupleCount, expectedFormatted, testName);
} }
void ListFormatterTest::RunTestFieldPositionIteratorWithNItems( void ListFormatterTest::RunTestFieldPositionIteratorWithNItems(
UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount, UnicodeString data[], int32_t n, int32_t expected[], int32_t tupleCount,
UnicodeString& appendTo, const char16_t *expectedFormatted, const char16_t *expectedFormatted,
const char* testName) { const char* testName) {
IcuTestErrorCode errorCode(*this, testName); IcuTestErrorCode errorCode(*this, testName);
LocalPointer<ListFormatter> formatter( LocalPointer<ListFormatter> formatter(
@ -325,27 +301,7 @@ void ListFormatterTest::RunTestFieldPositionIteratorWithNItems(
} }
RunTestFieldPositionIteratorWithFormatter( RunTestFieldPositionIteratorWithFormatter(
formatter.getAlias(), formatter.getAlias(),
data, n, expected, tupleCount, appendTo, expectedFormatted, testName); data, n, expected, tupleCount, 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");
} }
void ListFormatterTest::TestFieldPositionIteratorWith3Items() { void ListFormatterTest::TestFieldPositionIteratorWith3Items() {
@ -361,9 +317,8 @@ void ListFormatterTest::TestFieldPositionIteratorWith3Items() {
ULISTFMT_ELEMENT_FIELD, 12, 14 ULISTFMT_ELEMENT_FIELD, 12, 14
}; };
int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
UnicodeString appendTo;
RunTestFieldPositionIteratorWithNItems( RunTestFieldPositionIteratorWithNItems(
data, 3, expected, tupleCount, appendTo, data, 3, expected, tupleCount,
u"a, bbb, and cc", u"a, bbb, and cc",
"TestFieldPositionIteratorWith3Items"); "TestFieldPositionIteratorWith3Items");
} }
@ -381,31 +336,12 @@ void ListFormatterTest::TestFieldPositionIteratorWith3ItemsPatternShift() {
ULISTFMT_ELEMENT_FIELD, 0, 2 ULISTFMT_ELEMENT_FIELD, 0, 2
}; };
int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
UnicodeString appendTo;
RunTestFieldPositionIteratorWithNItemsPatternShift( RunTestFieldPositionIteratorWithNItemsPatternShift(
data, 3, expected, tupleCount, appendTo, data, 3, expected, tupleCount,
u"cc bbb a", u"cc bbb a",
"TestFieldPositionIteratorWith3ItemsPatternShift"); "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() { void ListFormatterTest::TestFieldPositionIteratorWith2Items() {
// 0 1 // 0 1
// 01234567890 // 01234567890
@ -417,9 +353,8 @@ void ListFormatterTest::TestFieldPositionIteratorWith2Items() {
ULISTFMT_ELEMENT_FIELD, 8, 10 ULISTFMT_ELEMENT_FIELD, 8, 10
}; };
int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
UnicodeString appendTo;
RunTestFieldPositionIteratorWithNItems( RunTestFieldPositionIteratorWithNItems(
data, 2, expected, tupleCount, appendTo, data, 2, expected, tupleCount,
u"bbb and cc", u"bbb and cc",
"TestFieldPositionIteratorWith2Items"); "TestFieldPositionIteratorWith2Items");
} }
@ -435,28 +370,12 @@ void ListFormatterTest::TestFieldPositionIteratorWith2ItemsPatternShift() {
ULISTFMT_ELEMENT_FIELD, 0, 2 ULISTFMT_ELEMENT_FIELD, 0, 2
}; };
int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
UnicodeString appendTo;
RunTestFieldPositionIteratorWithNItemsPatternShift( RunTestFieldPositionIteratorWithNItemsPatternShift(
data, 2, expected, tupleCount, appendTo, data, 2, expected, tupleCount,
u"cc bbb", u"cc bbb",
"TestFieldPositionIteratorWith2ItemsPatternShift"); "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() { void ListFormatterTest::TestFieldPositionIteratorWith1Item() {
// 012 // 012
// "cc" // "cc"
@ -465,9 +384,8 @@ void ListFormatterTest::TestFieldPositionIteratorWith1Item() {
ULISTFMT_ELEMENT_FIELD, 0, 2 ULISTFMT_ELEMENT_FIELD, 0, 2
}; };
int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected)); int32_t tupleCount = sizeof(expected)/(3 * sizeof(*expected));
UnicodeString appendTo;
RunTestFieldPositionIteratorWithNItems( RunTestFieldPositionIteratorWithNItems(
data, 1, expected, tupleCount, appendTo, data, 1, expected, tupleCount,
u"cc", u"cc",
"TestFieldPositionIteratorWith1Item"); "TestFieldPositionIteratorWith1Item");
} }

View File

@ -45,13 +45,9 @@ class ListFormatterTest : public IntlTestWithFieldPosition {
void TestZulu(); void TestZulu();
void TestOutOfOrderPatterns(); void TestOutOfOrderPatterns();
void Test9946(); void Test9946();
void TestFieldPositionIteratorWontCrash();
void TestFieldPositionIteratorWith1Item(); void TestFieldPositionIteratorWith1Item();
void TestFieldPositionIteratorWith2Items(); void TestFieldPositionIteratorWith2Items();
void TestFieldPositionIteratorWith3Items(); void TestFieldPositionIteratorWith3Items();
void TestFieldPositionIteratorWith1ItemAndDataBefore();
void TestFieldPositionIteratorWith2ItemsAndDataBefore();
void TestFieldPositionIteratorWith3ItemsAndDataBefore();
void TestFieldPositionIteratorWith2ItemsPatternShift(); void TestFieldPositionIteratorWith2ItemsPatternShift();
void TestFieldPositionIteratorWith3ItemsPatternShift(); void TestFieldPositionIteratorWith3ItemsPatternShift();
void TestFormattedValue(); void TestFormattedValue();
@ -67,15 +63,15 @@ class ListFormatterTest : public IntlTestWithFieldPosition {
const UnicodeString& expected_result, const UnicodeString& expected_result,
const char* testName); const char* testName);
void ExpectPositions( void ExpectPositions(
FieldPositionIterator& iter, const FormattedList& iter,
int32_t *values, int32_t *values,
int32_t tupleCount); int32_t tupleCount,
UErrorCode& status);
void RunTestFieldPositionIteratorWithNItems( void RunTestFieldPositionIteratorWithNItems(
UnicodeString *data, UnicodeString *data,
int32_t n, int32_t n,
int32_t *values, int32_t *values,
int32_t tupleCount, int32_t tupleCount,
UnicodeString& appendTo,
const char16_t *expectedFormatted, const char16_t *expectedFormatted,
const char* testName); const char* testName);
void RunTestFieldPositionIteratorWithNItemsPatternShift( void RunTestFieldPositionIteratorWithNItemsPatternShift(
@ -83,7 +79,6 @@ class ListFormatterTest : public IntlTestWithFieldPosition {
int32_t n, int32_t n,
int32_t *values, int32_t *values,
int32_t tupleCount, int32_t tupleCount,
UnicodeString& appendTo,
const char16_t *expectedFormatted, const char16_t *expectedFormatted,
const char* testName); const char* testName);
void RunTestFieldPositionIteratorWithFormatter( void RunTestFieldPositionIteratorWithFormatter(
@ -92,7 +87,6 @@ class ListFormatterTest : public IntlTestWithFieldPosition {
int32_t n, int32_t n,
int32_t *values, int32_t *values,
int32_t tupleCount, int32_t tupleCount,
UnicodeString& appendTo,
const char16_t *expectedFormatted, const char16_t *expectedFormatted,
const char* testName); const char* testName);
void CheckFourCases( void CheckFourCases(