Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6c47403e authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Rename StringUtils methods that handle title case manipulation

Change-Id: Iee0dd077a0423f110f4f8dad0f04933045baef2a
parent 0cf0bfaa
Loading
Loading
Loading
Loading
+17 −20
Original line number Diff line number Diff line
@@ -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) {
+18 −9
Original line number Diff line number Diff line
@@ -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);
+1 −3
Original line number Diff line number Diff line
@@ -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);
        }
+8 −6
Original line number Diff line number Diff line
@@ -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);
    }
+1 −2
Original line number Diff line number Diff line
@@ -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;
Loading