From eac8670830a172dd2ba2fea0632ecc7f7ec95992 Mon Sep 17 00:00:00 2001 From: Yohei Yukawa Date: Sat, 19 Jul 2014 04:10:24 +0900 Subject: 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 --- .../inputmethod/latin/utils/StringUtils.java | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'java/src/com/android/inputmethod/latin/utils/StringUtils.java') 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}. + *

+ * This is equivalent to + * {@code charSequence.toString().split(regex, preserveTrailingEmptySegments ? -1 : 0)} + * except that the spans are preserved in the result array. + *

+ * @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 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 { public String stringize(final E element) { -- cgit v1.2.3-83-g751a