diff options
author | 2013-04-10 19:35:36 -0700 | |
---|---|---|
committer | 2013-04-10 19:35:36 -0700 | |
commit | 024d0e4abf9d3bfacfb5495fa27a512c266df114 (patch) | |
tree | 5d5266b40cafdafe0cc4048044dcaef5e631438f /java/src/com/android/inputmethod/latin/StringUtils.java | |
parent | 8ab65b8b4eb5815398db8abb0a0dfc02f613445e (diff) | |
parent | f10e7cbe96596971be5de6592c9cab0aba9ad764 (diff) | |
download | latinime-024d0e4abf9d3bfacfb5495fa27a512c266df114.tar.gz latinime-024d0e4abf9d3bfacfb5495fa27a512c266df114.tar.xz latinime-024d0e4abf9d3bfacfb5495fa27a512c266df114.zip |
am f10e7cbe: am e9f69e16: Merge "Move a generic string utility to StringUtils"
* commit 'f10e7cbe96596971be5de6592c9cab0aba9ad764':
Move a generic string utility to StringUtils
Diffstat (limited to 'java/src/com/android/inputmethod/latin/StringUtils.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/StringUtils.java | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/StringUtils.java b/java/src/com/android/inputmethod/latin/StringUtils.java index 90c3fcdd2..dcb514a5e 100644 --- a/java/src/com/android/inputmethod/latin/StringUtils.java +++ b/java/src/com/android/inputmethod/latin/StringUtils.java @@ -22,6 +22,10 @@ import java.util.ArrayList; import java.util.Locale; public final class StringUtils { + public static final int CAPITALIZE_NONE = 0; // No caps, or mixed case + public static final int CAPITALIZE_FIRST = 1; // First only + public static final int CAPITALIZE_ALL = 2; // All caps + private StringUtils() { // This utility class is not publicly instantiable. } @@ -171,4 +175,31 @@ public final class StringUtils { } return list.toArray(new String[list.size()]); } + + // This method assumes the text is not empty or null. + public static int getCapitalizationType(final String text) { + // If the first char is not uppercase, then the word is either all lower case or + // camel case, and in either case we return CAPITALIZE_NONE. + if (!Character.isUpperCase(text.codePointAt(0))) return CAPITALIZE_NONE; + final int len = text.length(); + int capsCount = 1; + int letterCount = 1; + for (int i = 1; i < len; i = text.offsetByCodePoints(i, 1)) { + if (1 != capsCount && letterCount != capsCount) break; + final int codePoint = text.codePointAt(i); + if (Character.isUpperCase(codePoint)) { + ++capsCount; + ++letterCount; + } else if (Character.isLetter(codePoint)) { + // We need to discount non-letters since they may not be upper-case, but may + // still be part of a word (e.g. single quote or dash, as in "IT'S" or "FULL-TIME") + ++letterCount; + } + } + // We know the first char is upper case. So we want to test if either every letter other + // than the first is lower case, or if they are all upper case. If the string is exactly + // one char long, then we will arrive here with letterCount 1, and this is correct, too. + if (1 == capsCount) return CAPITALIZE_FIRST; + return (letterCount == capsCount ? CAPITALIZE_ALL : CAPITALIZE_NONE); + } } |