From d20c5a78e2819c21ecc5dff04bc82e9078c79dd4 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Wed, 10 Apr 2013 12:07:41 +0900 Subject: [PATCH] Separate StringUtils.capitalizeFirstCharacter Bug: 8582061 Change-Id: Iac8f65defdd92d7df533bdf0e2937ad897d96363 --- .../inputmethod/keyboard/MainKeyboardView.java | 2 +- .../com/android/inputmethod/latin/StringUtils.java | 12 ++++++++++-- .../com/android/inputmethod/latin/SubtypeLocale.java | 4 ++-- .../inputmethod/keyboard/SpacebarTextTests.java | 3 ++- .../android/inputmethod/latin/StringUtilsTests.java | 4 +--- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index 745e7dfed..dabc3075e 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.toTitleCase(locale.getLanguage(), locale); + return StringUtils.capitalizeFirstCharacter(locale.getLanguage(), locale); } // Get InputMethodSubtype's middle display name in its locale. diff --git a/java/src/com/android/inputmethod/latin/StringUtils.java b/java/src/com/android/inputmethod/latin/StringUtils.java index 59ad28fc9..4de104af5 100644 --- a/java/src/com/android/inputmethod/latin/StringUtils.java +++ b/java/src/com/android/inputmethod/latin/StringUtils.java @@ -106,10 +106,18 @@ public final class StringUtils { } } + public static String capitalizeFirstCharacter(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)}. + 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) { if (s.length() <= 1) { - // TODO: is this really correct? Shouldn't this be s.toUpperCase()? - return s; + return s.toUpperCase(locale); } // TODO: fix the bugs below // - This does not work for Greek, because it returns upper case instead of title case. diff --git a/java/src/com/android/inputmethod/latin/SubtypeLocale.java b/java/src/com/android/inputmethod/latin/SubtypeLocale.java index 5e28cc2d0..ae9cd8bde 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.toTitleCase(displayName, displayLocale); + return StringUtils.capitalizeFirstCharacter(displayName, displayLocale); } // InputMethodSubtype's display name in its locale. @@ -243,7 +243,7 @@ public final class SubtypeLocale { } } }; - return StringUtils.toTitleCase( + return StringUtils.capitalizeFirstCharacter( getSubtypeName.runInLocale(sResources, displayLocale), displayLocale); } diff --git a/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java b/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java index 1398db97c..864e78f43 100644 --- a/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java +++ b/tests/src/com/android/inputmethod/keyboard/SpacebarTextTests.java @@ -113,7 +113,8 @@ 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.toTitleCase(locale.getLanguage(), locale); + final String languageCode = StringUtils.capitalizeFirstCharacter( + locale.getLanguage(), locale); if (SubtypeLocale.isNoLanguage(subtype)) { assertEquals(subtypeName, "", spacebarText); } else { diff --git a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java index 966919ed3..9760f0bd1 100644 --- a/tests/src/com/android/inputmethod/latin/StringUtilsTests.java +++ b/tests/src/com/android/inputmethod/latin/StringUtilsTests.java @@ -106,9 +106,7 @@ public class StringUtilsTests extends AndroidTestCase { StringUtils.toTitleCase("iab", new Locale("tr"))); assertEquals("Aib", StringUtils.toTitleCase("AİB", new Locale("tr"))); - // For one character, toTitleCase returns the string as is. Not sure what the motivation - // is, but that's how it works now. - assertEquals("a", + assertEquals("A", StringUtils.toTitleCase("a", Locale.ENGLISH)); assertEquals("A", StringUtils.toTitleCase("A", Locale.ENGLISH));