diff options
author | 2013-02-12 07:57:23 +0000 | |
---|---|---|
committer | 2013-02-12 07:57:24 +0000 | |
commit | 2c2942dd4bdea8581191866e2f06a0fa9dec18ed (patch) | |
tree | 155511ba4e1ce4175cb657bea9a52b727aca8853 /java/src/com/android/inputmethod/latin/StringUtils.java | |
parent | e26f0d4320b9f3ad49a5c6d3f5f2ee78bac74a48 (diff) | |
parent | b3fd70118119e736209173d34053974e61f936d8 (diff) | |
download | latinime-2c2942dd4bdea8581191866e2f06a0fa9dec18ed.tar.gz latinime-2c2942dd4bdea8581191866e2f06a0fa9dec18ed.tar.xz latinime-2c2942dd4bdea8581191866e2f06a0fa9dec18ed.zip |
Merge "Move parseCsvString to StringUtils"
Diffstat (limited to 'java/src/com/android/inputmethod/latin/StringUtils.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/StringUtils.java | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/StringUtils.java b/java/src/com/android/inputmethod/latin/StringUtils.java index d00edbe92..6fac0d2d3 100644 --- a/java/src/com/android/inputmethod/latin/StringUtils.java +++ b/java/src/com/android/inputmethod/latin/StringUtils.java @@ -372,4 +372,42 @@ public final class StringUtils { // Here we arrived at the start of the line. This should behave exactly like whitespace. return (START == state || LETTER == state) ? noCaps : caps; } + + public static String[] parseCsvString(final String text) { + final int size = text.length(); + if (size == 0) { + return null; + } + if (codePointCount(text) == 1) { + return text.codePointAt(0) == Constants.CSV_SEPARATOR ? null : new String[] { text }; + } + + ArrayList<String> list = null; + int start = 0; + for (int pos = 0; pos < size; pos++) { + final char c = text.charAt(pos); + if (c == Constants.CSV_SEPARATOR) { + // Skip empty entry. + if (pos - start > 0) { + if (list == null) { + list = CollectionUtils.newArrayList(); + } + list.add(text.substring(start, pos)); + } + // Skip comma + start = pos + 1; + } else if (c == Constants.CSV_ESCAPE) { + // Skip escape character and escaped character. + pos++; + } + } + final String remain = (size - start > 0) ? text.substring(start) : null; + if (list == null) { + return remain != null ? new String[] { remain } : null; + } + if (remain != null) { + list.add(remain); + } + return list.toArray(new String[list.size()]); + } } |