Skip to content

Commit

Permalink
Fix uhighlight fragmentation algorithm breaking on large fragment_siz…
Browse files Browse the repository at this point in the history
…e values
  • Loading branch information
thelink2012 committed Jul 11, 2022
1 parent 7f7822b commit cafdece
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ public int preceding(int offset) {
} else {
innerStart = Math.max(mainBreak.preceding(offset), 0);

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

if (targetEndOffset + 1 > textEndIndex) {
innerEnd = textEndIndex;
} else {
innerEnd = mainBreak.preceding(targetEndOffset + 1);
innerEnd = mainBreak.preceding((int)targetEndOffset + 1);
}

assert innerEnd != DONE && innerEnd >= innerStart;

// in case no break was found up to maxLen, find one afterwards.
if (innerStart == innerEnd) {
innerEnd = mainBreak.following(targetEndOffset);
innerEnd = mainBreak.following((int)targetEndOffset);
assert innerEnd - innerStart > maxLen;
} else {
assert innerEnd - innerStart <= maxLen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,16 @@ public void testTextThatEndsBeforeMaxLen() {
assertEquals(33, bi.preceding(offset));
assertEquals(text.length(), bi.following(offset - 1));
}

public void testFragmentSizeThatIsTooBig() {
final int fragmentSize = Integer.MAX_VALUE;
BreakIterator bi = BoundedBreakIteratorScanner.getSentence(Locale.ROOT, fragmentSize);

final String text = "Any sentence";
final int offset = 0; // find at beggining of text

bi.setText(text);
assertEquals(0, bi.preceding(offset));
assertEquals(text.length(), bi.following(offset - 1));
}
}

0 comments on commit cafdece

Please sign in to comment.