Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BreakIterator.preceding fails in Android V2?

Tags:

android

The following code works just fine with Android 4, but causes an IllegalArgumentException in Android 2.

Any clues?

Locale currentLocale = new Locale("en_UK"); 
final BreakIterator boundary = BreakIterator.getSentenceInstance(currentLocale);
boundary.setText("a"); 
int thisThrowsExceptionInVersion2 = boundary.preceding(1);

Exception:

08-08 22:29:14.414: E/AndroidRuntime(329): Caused by: java.lang.IllegalArgumentException
08-08 22:29:14.414: E/AndroidRuntime(329):  at java.text.RuleBasedBreakIterator.validateOffset(RuleBasedBreakIterator.java:74)
08-08 22:29:14.414: E/AndroidRuntime(329):  at java.text.RuleBasedBreakIterator.preceding(RuleBasedBreakIterator.java:158)
08-08 22:29:14.414: E/AndroidRuntime(329):  at kalle.palle.namespace.KallePalleActivity.onCreate(KallePalleActivity.java:26)
like image 758
Anders Sewerin Johansen Avatar asked Jul 18 '26 15:07

Anders Sewerin Johansen


1 Answers

Below is the validateOffset in Gingerbread code

private void validateOffset(int offset) {
    CharacterIterator it = wrapped.getText();
    if (offset < it.getBeginIndex() || offset >= it.getEndIndex()) {
        throw new IllegalArgumentException();
    }
}

and in ICS code is as below

private void validateOffset(int offset) {
    CharacterIterator it = wrapped.getText();
    if (offset < it.getBeginIndex() || offset > it.getEndIndex()) {
        String message = "Valid range is [" + it.getBeginIndex() + " " + it.getEndIndex() + "]";
        throw new IllegalArgumentException(message);
    }
}

>= has been changed to > . The end offset checking seems to be wrong in 2.X devices. This is especially true in your case where the offset you are passing to preceding overlaps with the end index of the string. This seems to be bug in framework.
You can find the source in AOSP code at libcore/luni/src/main/java/java/text/RuleBasedBreakIterator.java.
Here's the Gingerbread code and here's the ICS code

like image 151
nandeesh Avatar answered Jul 20 '26 03:07

nandeesh