Rename StringUtils methods that handle title case manipulation

Change-Id: Iee0dd077a0423f110f4f8dad0f04933045baef2a
This commit is contained in:
Tadashi G. Takaoka 2014-11-19 13:45:21 +09:00
parent 0cf0bfaa75
commit 6c47403e27
7 changed files with 72 additions and 70 deletions

View File

@ -201,20 +201,20 @@ public final class StringUtils {
public static String capitalizeFirstCodePoint(@Nonnull final String s,
@Nonnull final Locale locale) {
if (s.length() <= 1) {
return toUpperCaseOfStringForLocale(s, true /* needsToUpperCase */, locale);
return s.toUpperCase(getLocaleUsedForToTitleCase(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 toUpperCaseOfStringForLocale(
s.substring(0, cutoff), true /* needsToUpperCase */, locale) + s.substring(cutoff);
return s.substring(0, cutoff).toUpperCase(getLocaleUsedForToTitleCase(locale))
+ s.substring(cutoff);
}
@Nonnull
public static String capitalizeFirstAndDowncaseRest(@Nonnull final String s,
@Nonnull final Locale locale) {
if (s.length() <= 1) {
return toUpperCaseOfStringForLocale(s, true /* needsToUpperCase */, locale);
return s.toUpperCase(getLocaleUsedForToTitleCase(locale));
}
// TODO: fix the bugs below
// - It does not work for Serbian, because it fails to account for the "lj" character,
@ -224,9 +224,8 @@ public final class StringUtils {
// be capitalized as "IJ" as if they were a single letter in most words (not all). If the
// unicode char for the ligature is used however, it works.
final int cutoff = s.offsetByCodePoints(0, 1);
final String titleCaseFirstLetter = toUpperCaseOfStringForLocale(
s.substring(0, cutoff), true /* needsToUpperCase */, locale);
return titleCaseFirstLetter + s.substring(cutoff).toLowerCase(locale);
return s.substring(0, cutoff).toUpperCase(getLocaleUsedForToTitleCase(locale))
+ s.substring(cutoff).toLowerCase(locale);
}
@Nonnull
@ -599,24 +598,22 @@ public final class StringUtils {
}
@Nullable
public static String toUpperCaseOfStringForLocale(@Nullable final String text,
final boolean needsToUpperCase, @Nonnull final Locale locale) {
if (text == null || !needsToUpperCase) {
return text;
public static String toTitleCaseOfKeyLabel(@Nullable final String label,
@Nonnull final Locale locale) {
if (label == null) {
return label;
}
return text.toUpperCase(getLocaleUsedForToTitleCase(locale));
return label.toUpperCase(getLocaleUsedForToTitleCase(locale));
}
public static int toUpperCaseOfCodeForLocale(final int code, final boolean needsToUpperCase,
@Nonnull final Locale locale) {
if (!Constants.isLetterCode(code) || !needsToUpperCase) {
public static int toTitleCaseOfKeyCode(final int code, @Nonnull final Locale locale) {
if (!Constants.isLetterCode(code)) {
return code;
}
final String text = newSingleCodePointString(code);
final String casedText = toUpperCaseOfStringForLocale(
text, needsToUpperCase, locale);
return codePointCount(casedText) == 1
? casedText.codePointAt(0) : Constants.CODE_UNSPECIFIED;
final String label = newSingleCodePointString(code);
final String titleCaseLabel = toTitleCaseOfKeyLabel(label, locale);
return codePointCount(titleCaseLabel) == 1
? titleCaseLabel.codePointAt(0) : Constants.CODE_UNSPECIFIED;
}
public static int getTrailingSingleQuotesCount(@Nonnull final CharSequence charSequence) {

View File

@ -341,17 +341,24 @@ public class Key implements Comparable<Key> {
// code point nor as a surrogate pair.
mLabel = new StringBuilder().appendCodePoint(code).toString();
} else {
mLabel = StringUtils.toUpperCaseOfStringForLocale(
KeySpecParser.getLabel(keySpec), needsToUpcase, localeForUpcasing);
final String label = KeySpecParser.getLabel(keySpec);
mLabel = needsToUpcase
? StringUtils.toTitleCaseOfKeyLabel(label, localeForUpcasing)
: label;
}
if ((mLabelFlags & LABEL_FLAGS_DISABLE_HINT_LABEL) != 0) {
mHintLabel = null;
} else {
mHintLabel = StringUtils.toUpperCaseOfStringForLocale(style.getString(keyAttr,
R.styleable.Keyboard_Key_keyHintLabel), needsToUpcase, localeForUpcasing);
final String hintLabel = style.getString(
keyAttr, R.styleable.Keyboard_Key_keyHintLabel);
mHintLabel = needsToUpcase
? StringUtils.toTitleCaseOfKeyLabel(hintLabel, localeForUpcasing)
: hintLabel;
}
String outputText = KeySpecParser.getOutputText(keySpec);
if (needsToUpcase) {
outputText = StringUtils.toTitleCaseOfKeyLabel(outputText, localeForUpcasing);
}
String outputText = StringUtils.toUpperCaseOfStringForLocale(
KeySpecParser.getOutputText(keySpec), needsToUpcase, localeForUpcasing);
// Choose the first letter of the label as primary code if not specified.
if (code == CODE_UNSPECIFIED && TextUtils.isEmpty(outputText)
&& !TextUtils.isEmpty(mLabel)) {
@ -377,12 +384,14 @@ public class Key implements Comparable<Key> {
mCode = CODE_OUTPUT_TEXT;
}
} else {
mCode = StringUtils.toUpperCaseOfCodeForLocale(code, needsToUpcase, localeForUpcasing);
mCode = needsToUpcase ? StringUtils.toTitleCaseOfKeyCode(code, localeForUpcasing)
: code;
}
final int altCodeInAttr = KeySpecParser.parseCode(
style.getString(keyAttr, R.styleable.Keyboard_Key_altCode), CODE_UNSPECIFIED);
final int altCode = StringUtils.toUpperCaseOfCodeForLocale(
altCodeInAttr, needsToUpcase, localeForUpcasing);
final int altCode = needsToUpcase
? StringUtils.toTitleCaseOfKeyCode(altCodeInAttr, localeForUpcasing)
: altCodeInAttr;
mOptionalAttributes = OptionalAttributes.newInstance(outputText, altCode,
disabledIconId, visualInsetsLeft, visualInsetsRight);
mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr);

View File

@ -57,7 +57,6 @@ import com.android.inputmethod.latin.RichInputMethodSubtype;
import com.android.inputmethod.latin.SuggestedWords;
import com.android.inputmethod.latin.common.Constants;
import com.android.inputmethod.latin.common.CoordinateUtils;
import com.android.inputmethod.latin.common.StringUtils;
import com.android.inputmethod.latin.settings.DebugSettings;
import com.android.inputmethod.latin.utils.TypefaceUtils;
@ -874,8 +873,7 @@ public final class MainKeyboardView extends KeyboardView implements DrawingProxy
final Locale[] locales = subtype.getLocales();
final String[] languages = new String[locales.length];
for (int i = 0; i < locales.length; ++i) {
languages[i] = StringUtils.toUpperCaseOfStringForLocale(
locales[i].getLanguage(), true /* needsToUpperCase */, Locale.ROOT);
languages[i] = locales[i].getLanguage().toUpperCase(Locale.ROOT);
}
return TextUtils.join(" / ", languages);
}

View File

@ -55,10 +55,11 @@ public final class MoreKeySpec {
if (TextUtils.isEmpty(moreKeySpec)) {
throw new KeySpecParser.KeySpecParserError("Empty more key spec");
}
mLabel = StringUtils.toUpperCaseOfStringForLocale(
KeySpecParser.getLabel(moreKeySpec), needsToUpperCase, locale);
final int code = StringUtils.toUpperCaseOfCodeForLocale(
KeySpecParser.getCode(moreKeySpec), needsToUpperCase, locale);
final String label = KeySpecParser.getLabel(moreKeySpec);
mLabel = needsToUpperCase ? StringUtils.toTitleCaseOfKeyLabel(label, locale) : label;
final int codeInSpec = KeySpecParser.getCode(moreKeySpec);
final int code = needsToUpperCase ? StringUtils.toTitleCaseOfKeyCode(codeInSpec, locale)
: codeInSpec;
if (code == Constants.CODE_UNSPECIFIED) {
// Some letter, for example German Eszett (U+00DF: "ß"), has multiple characters
// upper case representation ("SS").
@ -66,8 +67,9 @@ public final class MoreKeySpec {
mOutputText = mLabel;
} else {
mCode = code;
mOutputText = StringUtils.toUpperCaseOfStringForLocale(
KeySpecParser.getOutputText(moreKeySpec), needsToUpperCase, locale);
final String outputText = KeySpecParser.getOutputText(moreKeySpec);
mOutputText = needsToUpperCase
? StringUtils.toTitleCaseOfKeyLabel(outputText, locale) : outputText;
}
mIconId = KeySpecParser.getIconId(moreKeySpec);
}

View File

@ -63,8 +63,7 @@ abstract class ExpectedKeyOutput {
final String codeString = StringUtils.newSingleCodePointString(mCode);
// A letter may have an upper case counterpart that consists of multiple code
// points, for instance the upper case of "ß" is "SS".
return newInstance(StringUtils.toUpperCaseOfStringForLocale(
codeString, true /* needsToUpperCase */, locale));
return newInstance(StringUtils.toTitleCaseOfKeyLabel(codeString, locale));
}
// A special negative value has no upper case.
return this;

View File

@ -135,8 +135,7 @@ public abstract class ExpectedKeyVisual {
@Override
ExpectedKeyVisual toUpperCase(final Locale locale) {
return new Label(StringUtils.toUpperCaseOfStringForLocale(
mLabel, true /* needsToUpperCase */, locale));
return new Label(StringUtils.toTitleCaseOfKeyLabel(mLabel, locale));
}
@Override

View File

@ -30,17 +30,16 @@ public class StringUtilsTests extends AndroidTestCase {
private static final Locale TURKEY = new Locale("tr", "TR");
private static final Locale GREECE = new Locale("el", "GR");
private static void assert_toUpperCaseOfStringForLocale(final Locale locale,
private static void assert_toTitleCaseOfKeyLabel(final Locale locale,
final String lowerCase, final String expected) {
assertEquals(lowerCase + " in " + locale, expected,
StringUtils.toUpperCaseOfStringForLocale(
lowerCase, true /* needsToUpperCase */, locale));
StringUtils.toTitleCaseOfKeyLabel(lowerCase, locale));
}
public void test_toUpperCaseOfStringForLocale() {
assert_toUpperCaseOfStringForLocale(US, null, null);
assert_toUpperCaseOfStringForLocale(US, "", "");
assert_toUpperCaseOfStringForLocale(US, "aeiou", "AEIOU");
public void test_toTitleCaseOfKeyLabel() {
assert_toTitleCaseOfKeyLabel(US, null, null);
assert_toTitleCaseOfKeyLabel(US, "", "");
assert_toTitleCaseOfKeyLabel(US, "aeiou", "AEIOU");
// U+00E0: "à" LATIN SMALL LETTER A WITH GRAVE
// U+00E8: "è" LATIN SMALL LETTER E WITH GRAVE
// U+00EE: "î" LATIN SMALL LETTER I WITH CIRCUMFLEX
@ -55,7 +54,7 @@ public class StringUtilsTests extends AndroidTestCase {
// U+016A: "Ū" LATIN CAPITAL LETTER U WITH MACRON
// U+00D1: "Ñ" LATIN CAPITAL LETTER N WITH TILDE
// U+00C7: "Ç" LATIN CAPITAL LETTER C WITH CEDILLA
assert_toUpperCaseOfStringForLocale(US,
assert_toTitleCaseOfKeyLabel(US,
"\u00E0\u00E8\u00EE\u00F6\u016B\u00F1\u00E7",
"\u00C0\u00C8\u00CE\u00D6\u016A\u00D1\u00C7");
// U+00DF: "ß" LATIN SMALL LETTER SHARP S
@ -63,7 +62,7 @@ public class StringUtilsTests extends AndroidTestCase {
// U+0161: "š" LATIN SMALL LETTER S WITH CARON
// U+015A: "Ś" LATIN CAPITAL LETTER S WITH ACUTE
// U+0160: "Š" LATIN CAPITAL LETTER S WITH CARONZ
assert_toUpperCaseOfStringForLocale(GERMAN,
assert_toTitleCaseOfKeyLabel(GERMAN,
"\u00DF\u015B\u0161",
"SS\u015A\u0160");
// U+0259: "ə" LATIN SMALL LETTER SCHWA
@ -72,13 +71,13 @@ public class StringUtilsTests extends AndroidTestCase {
// U+018F: "Ə" LATIN SMALL LETTER SCHWA
// U+0130: "İ" LATIN SMALL LETTER I WITH DOT ABOVE
// U+0049: "I" LATIN SMALL LETTER I
assert_toUpperCaseOfStringForLocale(TURKEY,
assert_toTitleCaseOfKeyLabel(TURKEY,
"\u0259\u0069\u0131",
"\u018F\u0130\u0049");
// U+03C3: "σ" GREEK SMALL LETTER SIGMA
// U+03C2: "ς" GREEK SMALL LETTER FINAL SIGMA
// U+03A3: "Σ" GREEK CAPITAL LETTER SIGMA
assert_toUpperCaseOfStringForLocale(GREECE,
assert_toTitleCaseOfKeyLabel(GREECE,
"\u03C3\u03C2",
"\u03A3\u03A3");
// U+03AC: "ά" GREEK SMALL LETTER ALPHA WITH TONOS
@ -95,7 +94,7 @@ public class StringUtilsTests extends AndroidTestCase {
// U+038C: "Ό" GREEK CAPITAL LETTER OMICRON WITH TONOS
// U+038E: "Ύ" GREEK CAPITAL LETTER UPSILON WITH TONOS
// U+038F: "Ώ" GREEK CAPITAL LETTER OMEGA WITH TONOS
assert_toUpperCaseOfStringForLocale(GREECE,
assert_toTitleCaseOfKeyLabel(GREECE,
"\u03AC\u03AD\u03AE\u03AF\u03CC\u03CD\u03CE",
"\u0386\u0388\u0389\u038A\u038C\u038E\u038F");
// U+03CA: "ϊ" GREEK SMALL LETTER IOTA WITH DIALYTIKA
@ -108,42 +107,41 @@ public class StringUtilsTests extends AndroidTestCase {
// U+03A5: "Υ" GREEK CAPITAL LETTER UPSILON
// U+0308: COMBINING DIAERESIS
// U+0301: COMBINING GRAVE ACCENT
assert_toUpperCaseOfStringForLocale(GREECE,
assert_toTitleCaseOfKeyLabel(GREECE,
"\u03CA\u03CB\u0390\u03B0",
"\u03AA\u03AB\u0399\u0308\u0301\u03A5\u0308\u0301");
}
private static void assert_toUpperCaseOfCodeForLocale(final Locale locale, final int lowerCase,
private static void assert_toTitleCaseOfKeyCode(final Locale locale, final int lowerCase,
final int expected) {
assertEquals(lowerCase + " in " + locale, expected,
StringUtils.toUpperCaseOfCodeForLocale(
lowerCase, true /* needsToUpperCase */, locale));
StringUtils.toTitleCaseOfKeyCode(lowerCase, locale));
}
public void test_toUpperCaseOfCodeForLocale() {
assert_toUpperCaseOfCodeForLocale(US, Constants.CODE_ENTER, Constants.CODE_ENTER);
assert_toUpperCaseOfCodeForLocale(US, Constants.CODE_SPACE, Constants.CODE_SPACE);
assert_toUpperCaseOfCodeForLocale(US, Constants.CODE_COMMA, Constants.CODE_COMMA);
public void test_toTitleCaseOfKeyCode() {
assert_toTitleCaseOfKeyCode(US, Constants.CODE_ENTER, Constants.CODE_ENTER);
assert_toTitleCaseOfKeyCode(US, Constants.CODE_SPACE, Constants.CODE_SPACE);
assert_toTitleCaseOfKeyCode(US, Constants.CODE_COMMA, Constants.CODE_COMMA);
// U+0069: "i" LATIN SMALL LETTER I
// U+0131: "ı" LATIN SMALL LETTER DOTLESS I
// U+0130: "İ" LATIN SMALL LETTER I WITH DOT ABOVE
// U+0049: "I" LATIN SMALL LETTER I
assert_toUpperCaseOfCodeForLocale(US, 0x0069, 0x0049); // i -> I
assert_toUpperCaseOfCodeForLocale(US, 0x0131, 0x0049); // ı -> I
assert_toUpperCaseOfCodeForLocale(TURKEY, 0x0069, 0x0130); // i -> İ
assert_toUpperCaseOfCodeForLocale(TURKEY, 0x0131, 0x0049); // ı -> I
assert_toTitleCaseOfKeyCode(US, 0x0069, 0x0049); // i -> I
assert_toTitleCaseOfKeyCode(US, 0x0131, 0x0049); // ı -> I
assert_toTitleCaseOfKeyCode(TURKEY, 0x0069, 0x0130); // i -> İ
assert_toTitleCaseOfKeyCode(TURKEY, 0x0131, 0x0049); // ı -> I
// U+00DF: "ß" LATIN SMALL LETTER SHARP S
// The title case of "ß" is "SS".
assert_toUpperCaseOfCodeForLocale(US, 0x00DF, Constants.CODE_UNSPECIFIED);
assert_toTitleCaseOfKeyCode(US, 0x00DF, Constants.CODE_UNSPECIFIED);
// U+03AC: "ά" GREEK SMALL LETTER ALPHA WITH TONOS
// U+0386: "Ά" GREEK CAPITAL LETTER ALPHA WITH TONOS
assert_toUpperCaseOfCodeForLocale(GREECE, 0x03AC, 0x0386);
assert_toTitleCaseOfKeyCode(GREECE, 0x03AC, 0x0386);
// U+03CA: "ϊ" GREEK SMALL LETTER IOTA WITH DIALYTIKA
// U+03AA: "Ϊ" GREEK CAPITAL LETTER IOTA WITH DIALYTIKA
assert_toUpperCaseOfCodeForLocale(GREECE, 0x03CA, 0x03AA);
assert_toTitleCaseOfKeyCode(GREECE, 0x03CA, 0x03AA);
// U+03B0: "ΰ" GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS
// The title case of "ΰ" is "\u03A5\u0308\u0301".
assert_toUpperCaseOfCodeForLocale(GREECE, 0x03B0, Constants.CODE_UNSPECIFIED);
assert_toTitleCaseOfKeyCode(GREECE, 0x03B0, Constants.CODE_UNSPECIFIED);
}
private static void assert_capitalizeFirstCodePoint(final Locale locale, final String text,