Skip to content

Commit

Permalink
Improve performance of BoundedBreakIteratorScanner
Browse files Browse the repository at this point in the history
  • Loading branch information
thelink2012 committed May 26, 2021
1 parent a479a2a commit 85cb090
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,28 @@ public int preceding(int offset) {
innerStart = innerEnd;
innerEnd = windowEnd;
} else {
windowStart = innerStart = mainBreak.preceding(offset);
windowEnd = innerEnd = mainBreak.following(offset - 1);
// expand to next break until we reach maxLen
while (innerEnd - innerStart < maxLen) {
int newEnd = mainBreak.following(innerEnd);
if (newEnd == DONE || (newEnd - innerStart) > maxLen) {
break;
innerStart = Math.max(mainBreak.preceding(offset), 0);

final int targetEndOffset = offset + Math.max(0, maxLen - (offset - innerStart));
final int textEndIndex = getText().getEndIndex();

innerEnd = mainBreak.preceding(Math.min(targetEndOffset + 1, textEndIndex));
assert innerEnd != DONE && innerEnd >= innerStart;

// in case no break was found up to maxLen, find one afterwards.
if (innerStart == innerEnd) {
if (targetEndOffset >= textEndIndex) {
innerEnd = textEndIndex;
} else {
innerEnd = mainBreak.following(targetEndOffset);
assert innerEnd - innerStart > maxLen;
}
windowEnd = innerEnd = newEnd;
} else {
assert innerEnd - innerStart <= maxLen;
}

windowStart = innerStart;
windowEnd = innerEnd;
}

if (innerEnd - innerStart > maxLen) {
Expand Down

0 comments on commit 85cb090

Please sign in to comment.