Fix string iterations in a couple places.

Seems I didn't get how to iterate on a String correctly >.>
Talk about a big bug. Anyway, I think it's working now.

Bug: 5955228
Change-Id: I988c900cf2a16c44b9505cfd4f77c7cda7e592f0
main
Jean Chalard 2012-02-03 10:51:34 +09:00
parent fbd83a8712
commit 9242a2bcf8
6 changed files with 17 additions and 12 deletions

View File

@ -75,7 +75,8 @@ class BinaryDictionaryGetter {
// This assumes '%' is fully available as a non-separator, normal // This assumes '%' is fully available as a non-separator, normal
// character in a file name. This is probably true for all file systems. // character in a file name. This is probably true for all file systems.
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
for (int i = 0; i < name.length(); ++i) { final int nameLength = name.length();
for (int i = 0; i < nameLength; i = name.offsetByCodePoints(i, 1)) {
final int codePoint = name.codePointAt(i); final int codePoint = name.codePointAt(i);
if (isFileNameCharacter(codePoint)) { if (isFileNameCharacter(codePoint)) {
sb.appendCodePoint(codePoint); sb.appendCodePoint(codePoint);
@ -92,7 +93,8 @@ class BinaryDictionaryGetter {
*/ */
private static String getWordListIdFromFileName(final String fname) { private static String getWordListIdFromFileName(final String fname) {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
for (int i = 0; i < fname.length(); ++i) { final int fnameLength = fname.length();
for (int i = 0; i < fnameLength; i = fname.offsetByCodePoints(i, 1)) {
final int codePoint = fname.codePointAt(i); final int codePoint = fname.codePointAt(i);
if ('%' != codePoint) { if ('%' != codePoint) {
sb.appendCodePoint(codePoint); sb.appendCodePoint(codePoint);

View File

@ -93,7 +93,8 @@ public class SettingsValues {
mMagicSpaceStrippers = res.getString(R.string.magic_space_stripping_symbols); mMagicSpaceStrippers = res.getString(R.string.magic_space_stripping_symbols);
mMagicSpaceSwappers = res.getString(R.string.magic_space_swapping_symbols); mMagicSpaceSwappers = res.getString(R.string.magic_space_swapping_symbols);
if (LatinImeLogger.sDBG) { if (LatinImeLogger.sDBG) {
for (int i = 0; i < mMagicSpaceStrippers.length(); ++i) { final int length = mMagicSpaceStrippers.length();
for (int i = 0; i < length; i = mMagicSpaceStrippers.offsetByCodePoints(i, 1)) {
if (isMagicSpaceSwapper(mMagicSpaceStrippers.codePointAt(i))) { if (isMagicSpaceSwapper(mMagicSpaceStrippers.codePointAt(i))) {
throw new RuntimeException("Char code " + mMagicSpaceStrippers.codePointAt(i) throw new RuntimeException("Char code " + mMagicSpaceStrippers.codePointAt(i)
+ " is both a magic space swapper and stripper."); + " is both a magic space swapper and stripper.");
@ -234,10 +235,12 @@ public class SettingsValues {
} }
public boolean isMagicSpaceStripper(int code) { public boolean isMagicSpaceStripper(int code) {
// TODO: this does not work if the code does not fit in a char
return mMagicSpaceStrippers.contains(String.valueOf((char)code)); return mMagicSpaceStrippers.contains(String.valueOf((char)code));
} }
public boolean isMagicSpaceSwapper(int code) { public boolean isMagicSpaceSwapper(int code) {
// TODO: this does not work if the code does not fit in a char
return mMagicSpaceSwappers.contains(String.valueOf((char)code)); return mMagicSpaceSwappers.contains(String.valueOf((char)code));
} }

View File

@ -221,7 +221,8 @@ public class WordComposer {
if (mTrailingSingleQuotesCount > 0) { if (mTrailingSingleQuotesCount > 0) {
--mTrailingSingleQuotesCount; --mTrailingSingleQuotesCount;
} else { } else {
for (int i = mTypedWord.length() - 1; i >= 0; --i) { for (int i = mTypedWord.offsetByCodePoints(mTypedWord.length(), -1);
i >= 0; i = mTypedWord.offsetByCodePoints(i, -1)) {
if (Keyboard.CODE_SINGLE_QUOTE != mTypedWord.codePointAt(i)) break; if (Keyboard.CODE_SINGLE_QUOTE != mTypedWord.codePointAt(i)) break;
++mTrailingSingleQuotesCount; ++mTrailingSingleQuotesCount;
} }

View File

@ -431,9 +431,9 @@ public class AndroidSpellCheckerService extends SpellCheckerService
// If the first char is not uppercase, then the word is either all lower case, // If the first char is not uppercase, then the word is either all lower case,
// and in either case we return CAPITALIZE_NONE. // and in either case we return CAPITALIZE_NONE.
if (!Character.isUpperCase(text.codePointAt(0))) return CAPITALIZE_NONE; if (!Character.isUpperCase(text.codePointAt(0))) return CAPITALIZE_NONE;
final int len = text.codePointCount(0, text.length()); final int len = text.length();
int capsCount = 1; int capsCount = 1;
for (int i = 1; i < len; ++i) { for (int i = 1; i < len; i = text.offsetByCodePoints(i, 1)) {
if (1 != capsCount && i != capsCount) break; if (1 != capsCount && i != capsCount) break;
if (Character.isUpperCase(text.codePointAt(i))) ++capsCount; if (Character.isUpperCase(text.codePointAt(i))) ++capsCount;
} }
@ -522,13 +522,12 @@ public class AndroidSpellCheckerService extends SpellCheckerService
// Filter contents // Filter contents
final int length = text.length(); final int length = text.length();
int letterCount = 0; int letterCount = 0;
for (int i = 0; i < length; ++i) { for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) {
final int codePoint = text.codePointAt(i); final int codePoint = text.codePointAt(i);
// Any word containing a '@' is probably an e-mail address // Any word containing a '@' is probably an e-mail address
// Any word containing a '/' is probably either an ad-hoc combination of two // Any word containing a '/' is probably either an ad-hoc combination of two
// words or a URI - in either case we don't want to spell check that // words or a URI - in either case we don't want to spell check that
if ('@' == codePoint if ('@' == codePoint || '/' == codePoint) return true;
|| '/' == codePoint) return true;
if (isLetterCheckableByLanguage(codePoint, script)) ++letterCount; if (isLetterCheckableByLanguage(codePoint, script)) ++letterCount;
} }
// Guestimate heuristic: perform spell checking if at least 3/4 of the characters // Guestimate heuristic: perform spell checking if at least 3/4 of the characters
@ -570,7 +569,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService
suggestionsLimit); suggestionsLimit);
final WordComposer composer = new WordComposer(); final WordComposer composer = new WordComposer();
final int length = text.length(); final int length = text.length();
for (int i = 0; i < length; ++i) { for (int i = 0; i < length; i = text.offsetByCodePoints(i, 1)) {
final int character = text.codePointAt(i); final int character = text.codePointAt(i);
final int proximityIndex = final int proximityIndex =
SpellCheckerProximityInfo.getIndexOfCodeForScript(character, mScript); SpellCheckerProximityInfo.getIndexOfCodeForScript(character, mScript);

View File

@ -133,7 +133,7 @@ public class InputLogicTests extends ServiceTestCase<LatinIME> {
} }
private void type(final String stringToType) { private void type(final String stringToType) {
for (int i = 0; i < stringToType.length(); ++i) { for (int i = 0; i < stringToType.length(); i = stringToType.offsetByCodePoints(i, 1)) {
type(stringToType.codePointAt(i)); type(stringToType.codePointAt(i));
} }
} }

View File

@ -164,7 +164,7 @@ public class FusionDictionary implements Iterable<Word> {
static private int[] getCodePoints(String word) { static private int[] getCodePoints(String word) {
final int wordLength = word.length(); final int wordLength = word.length();
int[] array = new int[word.codePointCount(0, wordLength)]; int[] array = new int[word.codePointCount(0, wordLength)];
for (int i = 0; i < wordLength; ++i) { for (int i = 0; i < wordLength; i = word.offsetByCodePoints(i, 1)) {
array[i] = word.codePointAt(i); array[i] = word.codePointAt(i);
} }
return array; return array;