diff options
author | 2014-07-19 04:10:24 +0900 | |
---|---|---|
committer | 2014-07-19 22:47:56 +0900 | |
commit | eac8670830a172dd2ba2fea0632ecc7f7ec95992 (patch) | |
tree | dc3bca66cd82fea03d7bc5be30f82286b4d133fa /java/src/com/android/inputmethod/latin/utils/StringUtils.java | |
parent | e645715b2575d462dafd09e98fce84555549b65b (diff) | |
download | latinime-eac8670830a172dd2ba2fea0632ecc7f7ec95992.tar.gz latinime-eac8670830a172dd2ba2fea0632ecc7f7ec95992.tar.xz latinime-eac8670830a172dd2ba2fea0632ecc7f7ec95992.zip |
Enable StringUtils to split CharSequence like String#split
This is a groundwork for enabling LocaleSpan.
To enable LocaleSpan everywhere, we need to switch to
CharSequence from String so that Span infromation can be
preserved end-to-end. To achieve this, we need to have
CharSequence version of String#split.
BUG: 16029304
Change-Id: I0dd103185dcf62fb1e25054a374340790e6a4678
Diffstat (limited to 'java/src/com/android/inputmethod/latin/utils/StringUtils.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/utils/StringUtils.java | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/utils/StringUtils.java b/java/src/com/android/inputmethod/latin/utils/StringUtils.java index ceb038371..38f0b3fee 100644 --- a/java/src/com/android/inputmethod/latin/utils/StringUtils.java +++ b/java/src/com/android/inputmethod/latin/utils/StringUtils.java @@ -18,6 +18,7 @@ package com.android.inputmethod.latin.utils; import static com.android.inputmethod.latin.Constants.CODE_UNSPECIFIED; +import android.text.Spanned; import android.text.TextUtils; import com.android.inputmethod.annotations.UsedForTesting; @@ -26,6 +27,8 @@ import com.android.inputmethod.latin.Constants; import java.util.ArrayList; import java.util.Arrays; import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public final class StringUtils { public static final int CAPITALIZE_NONE = 0; // No caps, or mixed case @@ -503,6 +506,55 @@ public final class StringUtils { return lastIndex - i; } + /** + * Splits the given {@code charSequence} with at occurrences of the given {@code regex}. + * <p> + * This is equivalent to + * {@code charSequence.toString().split(regex, preserveTrailingEmptySegments ? -1 : 0)} + * except that the spans are preserved in the result array. + * </p> + * @param input the character sequence to be split. + * @param regex the regex pattern to be used as the separator. + * @param preserveTrailingEmptySegments {@code true} to preserve the trailing empty + * segments. Otherwise, trailing empty segments will be removed before being returned. + * @return the array which contains the result. All the spans in the {@param input} is + * preserved. + */ + @UsedForTesting + public static CharSequence[] split(final CharSequence charSequence, final String regex, + final boolean preserveTrailingEmptySegments) { + // A short-cut for non-spanned strings. + if (!(charSequence instanceof Spanned)) { + // -1 means that trailing empty segments will be preserved. + return charSequence.toString().split(regex, preserveTrailingEmptySegments ? -1 : 0); + } + + // Hereafter, emulate String.split for CharSequence. + final ArrayList<CharSequence> sequences = new ArrayList<>(); + final Matcher matcher = Pattern.compile(regex).matcher(charSequence); + int nextStart = 0; + boolean matched = false; + while (matcher.find()) { + sequences.add(charSequence.subSequence(nextStart, matcher.start())); + nextStart = matcher.end(); + matched = true; + } + if (!matched) { + // never matched. preserveTrailingEmptySegments is ignored in this case. + return new CharSequence[] { charSequence }; + } + sequences.add(charSequence.subSequence(nextStart, charSequence.length())); + if (!preserveTrailingEmptySegments) { + for (int i = sequences.size() - 1; i >= 0; --i) { + if (!TextUtils.isEmpty(sequences.get(i))) { + break; + } + sequences.remove(i); + } + } + return sequences.toArray(new CharSequence[sequences.size()]); + } + @UsedForTesting public static class Stringizer<E> { public String stringize(final E element) { |