Merge "Fix: StringIndexOutOfBoundsException."

main
Keisuke Kuroyanagi 2014-05-16 07:47:52 +00:00 committed by Android (Google) Code Review
commit f1b3518d71
5 changed files with 26 additions and 14 deletions

View File

@ -94,8 +94,8 @@ public final class Suggest {
final boolean blockOffensiveWords, final boolean isCorrectionEnabled, final boolean blockOffensiveWords, final boolean isCorrectionEnabled,
final int[] additionalFeaturesOptions, final int sequenceNumber, final int[] additionalFeaturesOptions, final int sequenceNumber,
final OnGetSuggestedWordsCallback callback) { final OnGetSuggestedWordsCallback callback) {
final int trailingSingleQuotesCount = wordComposer.trailingSingleQuotesCount();
final String typedWord = wordComposer.getTypedWord(); final String typedWord = wordComposer.getTypedWord();
final int trailingSingleQuotesCount = StringUtils.getTrailingSingleQuotesCount(typedWord);
final String consideredWord = trailingSingleQuotesCount > 0 final String consideredWord = trailingSingleQuotesCount > 0
? typedWord.substring(0, typedWord.length() - trailingSingleQuotesCount) ? typedWord.substring(0, typedWord.length() - trailingSingleQuotesCount)
: typedWord; : typedWord;

View File

@ -149,7 +149,8 @@ public final class WordComposer {
public int copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount( public int copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount(
final int[] destination) { final int[] destination) {
// lastIndex is exclusive // lastIndex is exclusive
final int lastIndex = mTypedWordCache.length() - trailingSingleQuotesCount(); final int lastIndex = mTypedWordCache.length()
- StringUtils.getTrailingSingleQuotesCount(mTypedWordCache);
if (lastIndex <= 0) { if (lastIndex <= 0) {
// The string is empty or contains only single quotes. // The string is empty or contains only single quotes.
return 0; return 0;
@ -331,15 +332,6 @@ public final class WordComposer {
return mIsFirstCharCapitalized; return mIsFirstCharCapitalized;
} }
public int trailingSingleQuotesCount() {
final int lastIndex = mTypedWordCache.length() - 1;
int i = lastIndex;
while (i >= 0 && mTypedWordCache.charAt(i) == Constants.CODE_SINGLE_QUOTE) {
--i;
}
return lastIndex - i;
}
/** /**
* Whether or not all of the user typed chars are upper case * Whether or not all of the user typed chars are upper case
* @return true if all user typed chars are upper case, false otherwise * @return true if all user typed chars are upper case, false otherwise

View File

@ -86,9 +86,10 @@ public class DistracterFilter {
coordinates = mKeyboard.getCoordinates(codePoints); coordinates = mKeyboard.getCoordinates(codePoints);
composer.setComposingWord(codePoints, coordinates, prevWord); composer.setComposingWord(codePoints, coordinates, prevWord);
final int trailingSingleQuotesCount = composer.trailingSingleQuotesCount(); final int trailingSingleQuotesCount = StringUtils.getTrailingSingleQuotesCount(testedWord);
final String consideredWord = trailingSingleQuotesCount > 0 ? testedWord.substring(0, final String consideredWord = trailingSingleQuotesCount > 0 ?
testedWord.length() - trailingSingleQuotesCount) : testedWord; testedWord.substring(0, testedWord.length() - trailingSingleQuotesCount) :
testedWord;
final AsyncResultHolder<Boolean> holder = new AsyncResultHolder<Boolean>(); final AsyncResultHolder<Boolean> holder = new AsyncResultHolder<Boolean>();
final OnGetSuggestedWordsCallback callback = new OnGetSuggestedWordsCallback() { final OnGetSuggestedWordsCallback callback = new OnGetSuggestedWordsCallback() {
@Override @Override

View File

@ -538,6 +538,15 @@ public final class StringUtils {
? casedText.codePointAt(0) : CODE_UNSPECIFIED; ? casedText.codePointAt(0) : CODE_UNSPECIFIED;
} }
public static int getTrailingSingleQuotesCount(final CharSequence charSequence) {
final int lastIndex = charSequence.length() - 1;
int i = lastIndex;
while (i >= 0 && charSequence.charAt(i) == Constants.CODE_SINGLE_QUOTE) {
--i;
}
return lastIndex - i;
}
@UsedForTesting @UsedForTesting
public static class Stringizer<E> { public static class Stringizer<E> {
public String stringize(final E element) { public String stringize(final E element) {

View File

@ -372,4 +372,14 @@ public class StringAndJsonUtilsTests extends AndroidTestCase {
assertTrue("copyCodePointsAndReturnCodePointCount throws when array is too small", assertTrue("copyCodePointsAndReturnCodePointCount throws when array is too small",
exceptionHappened); exceptionHappened);
} }
public void testGetTrailingSingleQuotesCount() {
assertEquals(0, StringUtils.getTrailingSingleQuotesCount(""));
assertEquals(1, StringUtils.getTrailingSingleQuotesCount("'"));
assertEquals(5, StringUtils.getTrailingSingleQuotesCount("'''''"));
assertEquals(0, StringUtils.getTrailingSingleQuotesCount("a"));
assertEquals(0, StringUtils.getTrailingSingleQuotesCount("'this"));
assertEquals(1, StringUtils.getTrailingSingleQuotesCount("'word'"));
assertEquals(0, StringUtils.getTrailingSingleQuotesCount("I'm"));
}
} }