Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions icu4c/source/common/rbbi_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,14 +544,17 @@ UBool RuleBasedBreakIterator::BreakCache::populatePreceding(UErrorCode &status)
}

int32_t backupPosition = fromPosition;
int32_t backupStep = 30;

// Find a boundary somewhere preceding the first already-cached boundary
do {
backupPosition = backupPosition - 30;
if (backupPosition <= 0) {
if (backupPosition <= backupStep) {
backupPosition = 0;
} else {
backupPosition = fBI->handleSafePrevious(backupPosition);
backupPosition = fBI->handleSafePrevious(backupPosition - backupStep);
}
if (backupStep <= INT32_MAX / 2) {
backupStep *= 2;
}
if (backupPosition == UBRK_DONE || backupPosition == 0) {
position = 0;
Expand Down
34 changes: 34 additions & 0 deletions icu4c/source/test/intltest/rbbitst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ void RBBITest::runIndexedTest( int32_t index, UBool exec, const char* &name, cha
TESTCASE_AUTO(TestLookaheadPolychromy);
TESTCASE_AUTO(TestBug23449);
TESTCASE_AUTO(TestICU23593);
TESTCASE_AUTO(TestICU23644);

#if U_ENABLE_TRACING
TESTCASE_AUTO(TestTraceCreateCharacter);
Expand Down Expand Up @@ -5295,4 +5296,37 @@ void RBBITest::TestICU23593() {
}
}

// Regression test for ICU-23644: BreakCache::populatePreceding() must not go
// quadratic on long unbroken word/sentence/line/CJK segments.
void RBBITest::TestICU23644() {
IcuTestErrorCode status(*this, "TestICU23644");
constexpr int32_t kLen = 100000;
UnicodeString longWord(kLen, static_cast<UChar32>(u'a'), kLen);
for (int32_t i = 0; i < 20; i += 2) {
longWord.setCharAt(i, u' ');
}

LocalPointer<BreakIterator> wb(BreakIterator::createWordInstance(Locale::getEnglish(), status));
if (assertSuccess(WHERE, status)) {
wb->setText(longWord);
assertFalse(WHERE, wb->isBoundary(kLen - 5));
assertEquals(WHERE, 19, wb->preceding(kLen - 5));
assertEquals(WHERE, kLen, wb->following(kLen - 5));
}

LocalPointer<BreakIterator> sb(BreakIterator::createSentenceInstance(Locale::getEnglish(), status));
if (assertSuccess(WHERE, status)) {
sb->setText(longWord);
assertFalse(WHERE, sb->isBoundary(kLen - 5));
assertEquals(WHERE, 0, sb->preceding(kLen - 5));
}

LocalPointer<BreakIterator> lb(BreakIterator::createLineInstance(Locale::getEnglish(), status));
if (assertSuccess(WHERE, status)) {
lb->setText(longWord);
assertFalse(WHERE, lb->isBoundary(kLen - 5));
assertEquals(WHERE, 19, lb->preceding(kLen - 5));
}
}

#endif // #if !UCONFIG_NO_BREAK_ITERATION
1 change: 1 addition & 0 deletions icu4c/source/test/intltest/rbbitst.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class RBBITest: public IntlTest {
void TestLookaheadPolychromy();
void TestBug23449();
void TestICU23593();
void TestICU23644();

#if U_ENABLE_TRACING
void TestTraceCreateCharacter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1666,14 +1666,17 @@ boolean populatePreceding() {
}

int backupPosition = fromPosition;
int backupStep = 30;

// Find a boundary somewhere preceding the first already-cached boundary
do {
backupPosition = backupPosition - 30;
if (backupPosition <= textBegin) {
if (backupPosition - textBegin <= backupStep) {
backupPosition = textBegin;
} else {
backupPosition = handleSafePrevious(backupPosition);
backupPosition = handleSafePrevious(backupPosition - backupStep);
}
if (backupStep <= Integer.MAX_VALUE / 2) {
backupStep *= 2;
}
if (backupPosition == BreakIterator.DONE || backupPosition == textBegin) {
position = textBegin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1729,4 +1729,30 @@ public void TestICU23593() {
assertEquals("next() 155", 155, bi.next());
assertEquals("next() DONE", BreakIterator.DONE, bi.next());
}

@Test
public void TestICU23644() {
final int len = 100000;
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; ++i) {
sb.append((i < 20 && (i % 2 == 0)) ? ' ' : 'a');
}
String longWord = sb.toString();

BreakIterator wb = BreakIterator.getWordInstance(ULocale.ENGLISH);
wb.setText(longWord);
assertFalse("word isBoundary", wb.isBoundary(len - 5));
assertEquals("word preceding", 19, wb.preceding(len - 5));
assertEquals("word following", len, wb.following(len - 5));

BreakIterator sent = BreakIterator.getSentenceInstance(ULocale.ENGLISH);
sent.setText(longWord);
assertFalse("sentence isBoundary", sent.isBoundary(len - 5));
assertEquals("sentence preceding", 0, sent.preceding(len - 5));

BreakIterator lb = BreakIterator.getLineInstance(ULocale.ENGLISH);
lb.setText(longWord);
assertFalse("line isBoundary", lb.isBoundary(len - 5));
assertEquals("line preceding", 19, lb.preceding(len - 5));
}
}
Loading