From 99b93d17d53c2d587c45373831b327f7851ec0a8 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Wed, 10 Apr 2013 17:13:26 +0900 Subject: [PATCH] Fix a bug where autocorrection status would be wrong. Change-Id: Ic220129dc59f585164dbf63591cd1c96de17fe6f --- .../keyboard/MainKeyboardView.java | 2 +- .../inputmethod/latin/CapsModeUtils.java | 2 +- .../inputmethod/latin/StringUtils.java | 7 ++-- .../inputmethod/latin/SubtypeLocale.java | 4 +- .../android/inputmethod/latin/Suggest.java | 2 +- .../AndroidSpellCheckerService.java | 2 +- .../AndroidWordLevelSpellCheckerSession.java | 2 +- .../keyboard/SpacebarTextTests.java | 2 +- .../inputmethod/latin/StringUtilsTests.java | 40 ++++++++++++++----- 9 files changed, 42 insertions(+), 21 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index dabc3075e..7dfbea353 100644 --- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java @@ -1460,7 +1460,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack return ""; } final Locale locale = SubtypeLocale.getSubtypeLocale(subtype); - return StringUtils.capitalizeFirstCharacter(locale.getLanguage(), locale); + return StringUtils.capitalizeFirstCodePoint(locale.getLanguage(), locale); } // Get InputMethodSubtype's middle display name in its locale. diff --git a/java/src/com/android/inputmethod/latin/CapsModeUtils.java b/java/src/com/android/inputmethod/latin/CapsModeUtils.java index 1012cd519..4b8d1ac11 100644 --- a/java/src/com/android/inputmethod/latin/CapsModeUtils.java +++ b/java/src/com/android/inputmethod/latin/CapsModeUtils.java @@ -41,7 +41,7 @@ public final class CapsModeUtils { if (WordComposer.CAPS_MODE_AUTO_SHIFT_LOCKED == capitalizeMode) { return s.toUpperCase(locale); } else if (WordComposer.CAPS_MODE_AUTO_SHIFTED == capitalizeMode) { - return StringUtils.toTitleCase(s, locale); + return StringUtils.capitalizeFirstCodePoint(s, locale); } else { return s; } diff --git a/java/src/com/android/inputmethod/latin/StringUtils.java b/java/src/com/android/inputmethod/latin/StringUtils.java index 4de104af5..11ef60dc9 100644 --- a/java/src/com/android/inputmethod/latin/StringUtils.java +++ b/java/src/com/android/inputmethod/latin/StringUtils.java @@ -106,16 +106,17 @@ public final class StringUtils { } } - public static String capitalizeFirstCharacter(final String s, final Locale locale) { + public static String capitalizeFirstCodePoint(final String s, final Locale locale) { if (s.length() <= 1) { return s.toUpperCase(locale); } - // Please refer to the comment below in {@link #toTitleCase(String,Locale)}. + // Please refer to the comment below in + // {@link #capitalizeFirstAndDowncaseRest(String,Locale)} as this has the same shortcomings final int cutoff = s.offsetByCodePoints(0, 1); return s.substring(0, cutoff).toUpperCase(locale) + s.substring(cutoff); } - public static String toTitleCase(final String s, final Locale locale) { + public static String capitalizeFirstAndDowncaseRest(final String s, final Locale locale) { if (s.length() <= 1) { return s.toUpperCase(locale); } diff --git a/java/src/com/android/inputmethod/latin/SubtypeLocale.java b/java/src/com/android/inputmethod/latin/SubtypeLocale.java index ae9cd8bde..4d88ecc0c 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeLocale.java +++ b/java/src/com/android/inputmethod/latin/SubtypeLocale.java @@ -183,7 +183,7 @@ public final class SubtypeLocale { final Locale locale = LocaleUtils.constructLocaleFromString(localeString); displayName = locale.getDisplayName(displayLocale); } - return StringUtils.capitalizeFirstCharacter(displayName, displayLocale); + return StringUtils.capitalizeFirstCodePoint(displayName, displayLocale); } // InputMethodSubtype's display name in its locale. @@ -243,7 +243,7 @@ public final class SubtypeLocale { } } }; - return StringUtils.capitalizeFirstCharacter( + return StringUtils.capitalizeFirstCodePoint( getSubtypeName.runInLocale(sResources, displayLocale), displayLocale); } diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 975664dca..6464bd0d7 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -394,7 +394,7 @@ public final class Suggest { if (isAllUpperCase) { sb.append(wordInfo.mWord.toUpperCase(locale)); } else if (isFirstCharCapitalized) { - sb.append(StringUtils.toTitleCase(wordInfo.mWord, locale)); + sb.append(StringUtils.capitalizeFirstCodePoint(wordInfo.mWord, locale)); } else { sb.append(wordInfo.mWord); } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index fbed139f3..2d0a89bb3 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -330,7 +330,7 @@ public final class AndroidSpellCheckerService extends SpellCheckerService } else if (StringUtils.CAPITALIZE_FIRST == capitalizeType) { for (int i = 0; i < mSuggestions.size(); ++i) { // Likewise - mSuggestions.set(i, StringUtils.toTitleCase( + mSuggestions.set(i, StringUtils.capitalizeFirstCodePoint( mSuggestions.get(i).toString(), locale)); } } diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java index b15063235..96b2c818d 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidWordLevelSpellCheckerSession.java @@ -226,7 +226,7 @@ public abstract class AndroidWordLevelSpellCheckerSession extends Session { // If the lower case version is not in the dictionary, it's still possible // that we have an all-caps version of a word that needs to be capitalized // according to the dictionary. E.g. "GERMANS" only exists in the dictionary as "Germans". - return dict.isValidWord(StringUtils.toTitleCase(lowerCaseText, mLocale)); + return dict.isValidWord(StringUtils.capitalizeFirstAndDowncaseRest(lowerCaseText, mLocale)); } // Note : this must be reentrant diff --git a/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java b/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java index 864e78f43..850af94f7 100644 --- a/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java +++ b/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java @@ -113,7 +113,7 @@ public class SpacebarTextTests extends AndroidTestCase { final String subtypeName = SubtypeLocale.getSubtypeDisplayName(subtype); final Locale locale = SubtypeLocale.getSubtypeLocale(subtype); final String spacebarText = MainKeyboardView.getShortDisplayName(subtype); - final String languageCode = StringUtils.capitalizeFirstCharacter( + final String languageCode = StringUtils.capitalizeFirstCodePoint( locale.getLanguage(), locale); if (SubtypeLocale.isNoLanguage(subtype)) { assertEquals(subtypeName, "", spacebarText); diff --git a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java index 9760f0bd1..5810abb2d 100644 --- a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java @@ -93,23 +93,43 @@ public class StringUtilsTests extends AndroidTestCase { StringUtils.removeFromCsvIfExists("key", "key1,key,key3,key,key5")); } - public void testToTitleCase() { + + public void testCapitalizeFirstCodePoint() { assertEquals("SSaa", - StringUtils.toTitleCase("ßaa", Locale.GERMAN)); + StringUtils.capitalizeFirstCodePoint("ßaa", Locale.GERMAN)); assertEquals("Aßa", - StringUtils.toTitleCase("aßa", Locale.GERMAN)); + StringUtils.capitalizeFirstCodePoint("aßa", Locale.GERMAN)); assertEquals("Iab", - StringUtils.toTitleCase("iab", Locale.ENGLISH)); - assertEquals("Camelcase", - StringUtils.toTitleCase("cAmElCaSe", Locale.ENGLISH)); + StringUtils.capitalizeFirstCodePoint("iab", Locale.ENGLISH)); + assertEquals("cAmElCaSe", + StringUtils.capitalizeFirstCodePoint("cAmElCaSe", Locale.ENGLISH)); assertEquals("İab", - StringUtils.toTitleCase("iab", new Locale("tr"))); + StringUtils.capitalizeFirstCodePoint("iab", new Locale("tr"))); + assertEquals("AİB", + StringUtils.capitalizeFirstCodePoint("AİB", new Locale("tr"))); + assertEquals("A", + StringUtils.capitalizeFirstCodePoint("a", Locale.ENGLISH)); + assertEquals("A", + StringUtils.capitalizeFirstCodePoint("A", Locale.ENGLISH)); + } + + public void testCapitalizeFirstAndDowncaseRest() { + assertEquals("SSaa", + StringUtils.capitalizeFirstAndDowncaseRest("ßaa", Locale.GERMAN)); + assertEquals("Aßa", + StringUtils.capitalizeFirstAndDowncaseRest("aßa", Locale.GERMAN)); + assertEquals("Iab", + StringUtils.capitalizeFirstAndDowncaseRest("iab", Locale.ENGLISH)); + assertEquals("Camelcase", + StringUtils.capitalizeFirstAndDowncaseRest("cAmElCaSe", Locale.ENGLISH)); + assertEquals("İab", + StringUtils.capitalizeFirstAndDowncaseRest("iab", new Locale("tr"))); assertEquals("Aib", - StringUtils.toTitleCase("AİB", new Locale("tr"))); + StringUtils.capitalizeFirstAndDowncaseRest("AİB", new Locale("tr"))); assertEquals("A", - StringUtils.toTitleCase("a", Locale.ENGLISH)); + StringUtils.capitalizeFirstAndDowncaseRest("a", Locale.ENGLISH)); assertEquals("A", - StringUtils.toTitleCase("A", Locale.ENGLISH)); + StringUtils.capitalizeFirstAndDowncaseRest("A", Locale.ENGLISH)); } public void testGetCapitalizationType() {