TextUtils.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.text;
18
19import com.android.internal.R;
20
21import android.content.res.ColorStateList;
22import android.content.res.Resources;
23import android.os.Parcel;
24import android.os.Parcelable;
25import android.text.method.TextKeyListener.Capitalize;
26import android.text.style.AbsoluteSizeSpan;
27import android.text.style.AlignmentSpan;
28import android.text.style.BackgroundColorSpan;
29import android.text.style.BulletSpan;
30import android.text.style.CharacterStyle;
31import android.text.style.ForegroundColorSpan;
32import android.text.style.LeadingMarginSpan;
33import android.text.style.MetricAffectingSpan;
34import android.text.style.QuoteSpan;
35import android.text.style.RelativeSizeSpan;
36import android.text.style.ReplacementSpan;
37import android.text.style.ScaleXSpan;
38import android.text.style.StrikethroughSpan;
39import android.text.style.StyleSpan;
40import android.text.style.SubscriptSpan;
41import android.text.style.SuperscriptSpan;
42import android.text.style.TextAppearanceSpan;
43import android.text.style.TypefaceSpan;
44import android.text.style.URLSpan;
45import android.text.style.UnderlineSpan;
46import android.util.Printer;
47
48import com.android.internal.util.ArrayUtils;
49
50import java.util.regex.Pattern;
51import java.util.Iterator;
52
53public class TextUtils {
54    private TextUtils() { /* cannot be instantiated */ }
55
56    private static String[] EMPTY_STRING_ARRAY = new String[]{};
57
58    public static void getChars(CharSequence s, int start, int end,
59                                char[] dest, int destoff) {
60        Class c = s.getClass();
61
62        if (c == String.class)
63            ((String) s).getChars(start, end, dest, destoff);
64        else if (c == StringBuffer.class)
65            ((StringBuffer) s).getChars(start, end, dest, destoff);
66        else if (c == StringBuilder.class)
67            ((StringBuilder) s).getChars(start, end, dest, destoff);
68        else if (s instanceof GetChars)
69            ((GetChars) s).getChars(start, end, dest, destoff);
70        else {
71            for (int i = start; i < end; i++)
72                dest[destoff++] = s.charAt(i);
73        }
74    }
75
76    public static int indexOf(CharSequence s, char ch) {
77        return indexOf(s, ch, 0);
78    }
79
80    public static int indexOf(CharSequence s, char ch, int start) {
81        Class c = s.getClass();
82
83        if (c == String.class)
84            return ((String) s).indexOf(ch, start);
85
86        return indexOf(s, ch, start, s.length());
87    }
88
89    public static int indexOf(CharSequence s, char ch, int start, int end) {
90        Class c = s.getClass();
91
92        if (s instanceof GetChars || c == StringBuffer.class ||
93            c == StringBuilder.class || c == String.class) {
94            final int INDEX_INCREMENT = 500;
95            char[] temp = obtain(INDEX_INCREMENT);
96
97            while (start < end) {
98                int segend = start + INDEX_INCREMENT;
99                if (segend > end)
100                    segend = end;
101
102                getChars(s, start, segend, temp, 0);
103
104                int count = segend - start;
105                for (int i = 0; i < count; i++) {
106                    if (temp[i] == ch) {
107                        recycle(temp);
108                        return i + start;
109                    }
110                }
111
112                start = segend;
113            }
114
115            recycle(temp);
116            return -1;
117        }
118
119        for (int i = start; i < end; i++)
120            if (s.charAt(i) == ch)
121                return i;
122
123        return -1;
124    }
125
126    public static int lastIndexOf(CharSequence s, char ch) {
127        return lastIndexOf(s, ch, s.length() - 1);
128    }
129
130    public static int lastIndexOf(CharSequence s, char ch, int last) {
131        Class c = s.getClass();
132
133        if (c == String.class)
134            return ((String) s).lastIndexOf(ch, last);
135
136        return lastIndexOf(s, ch, 0, last);
137    }
138
139    public static int lastIndexOf(CharSequence s, char ch,
140                                  int start, int last) {
141        if (last < 0)
142            return -1;
143        if (last >= s.length())
144            last = s.length() - 1;
145
146        int end = last + 1;
147
148        Class c = s.getClass();
149
150        if (s instanceof GetChars || c == StringBuffer.class ||
151            c == StringBuilder.class || c == String.class) {
152            final int INDEX_INCREMENT = 500;
153            char[] temp = obtain(INDEX_INCREMENT);
154
155            while (start < end) {
156                int segstart = end - INDEX_INCREMENT;
157                if (segstart < start)
158                    segstart = start;
159
160                getChars(s, segstart, end, temp, 0);
161
162                int count = end - segstart;
163                for (int i = count - 1; i >= 0; i--) {
164                    if (temp[i] == ch) {
165                        recycle(temp);
166                        return i + segstart;
167                    }
168                }
169
170                end = segstart;
171            }
172
173            recycle(temp);
174            return -1;
175        }
176
177        for (int i = end - 1; i >= start; i--)
178            if (s.charAt(i) == ch)
179                return i;
180
181        return -1;
182    }
183
184    public static int indexOf(CharSequence s, CharSequence needle) {
185        return indexOf(s, needle, 0, s.length());
186    }
187
188    public static int indexOf(CharSequence s, CharSequence needle, int start) {
189        return indexOf(s, needle, start, s.length());
190    }
191
192    public static int indexOf(CharSequence s, CharSequence needle,
193                              int start, int end) {
194        int nlen = needle.length();
195        if (nlen == 0)
196            return start;
197
198        char c = needle.charAt(0);
199
200        for (;;) {
201            start = indexOf(s, c, start);
202            if (start > end - nlen) {
203                break;
204            }
205
206            if (start < 0) {
207                return -1;
208            }
209
210            if (regionMatches(s, start, needle, 0, nlen)) {
211                return start;
212            }
213
214            start++;
215        }
216        return -1;
217    }
218
219    public static boolean regionMatches(CharSequence one, int toffset,
220                                        CharSequence two, int ooffset,
221                                        int len) {
222        char[] temp = obtain(2 * len);
223
224        getChars(one, toffset, toffset + len, temp, 0);
225        getChars(two, ooffset, ooffset + len, temp, len);
226
227        boolean match = true;
228        for (int i = 0; i < len; i++) {
229            if (temp[i] != temp[i + len]) {
230                match = false;
231                break;
232            }
233        }
234
235        recycle(temp);
236        return match;
237    }
238
239    public static String substring(CharSequence source, int start, int end) {
240        if (source instanceof String)
241            return ((String) source).substring(start, end);
242        if (source instanceof StringBuilder)
243            return ((StringBuilder) source).substring(start, end);
244        if (source instanceof StringBuffer)
245            return ((StringBuffer) source).substring(start, end);
246
247        char[] temp = obtain(end - start);
248        getChars(source, start, end, temp, 0);
249        String ret = new String(temp, 0, end - start);
250        recycle(temp);
251
252        return ret;
253    }
254
255    /**
256     * Returns a string containing the tokens joined by delimiters.
257     * @param tokens an array objects to be joined. Strings will be formed from
258     *     the objects by calling object.toString().
259     */
260    public static String join(CharSequence delimiter, Object[] tokens) {
261        StringBuilder sb = new StringBuilder();
262        boolean firstTime = true;
263        for (Object token: tokens) {
264            if (firstTime) {
265                firstTime = false;
266            } else {
267                sb.append(delimiter);
268            }
269            sb.append(token);
270        }
271        return sb.toString();
272    }
273
274    /**
275     * Returns a string containing the tokens joined by delimiters.
276     * @param tokens an array objects to be joined. Strings will be formed from
277     *     the objects by calling object.toString().
278     */
279    public static String join(CharSequence delimiter, Iterable tokens) {
280        StringBuilder sb = new StringBuilder();
281        boolean firstTime = true;
282        for (Object token: tokens) {
283            if (firstTime) {
284                firstTime = false;
285            } else {
286                sb.append(delimiter);
287            }
288            sb.append(token);
289        }
290        return sb.toString();
291    }
292
293    /**
294     * String.split() returns [''] when the string to be split is empty. This returns []. This does
295     * not remove any empty strings from the result. For example split("a,", ","  ) returns {"a", ""}.
296     *
297     * @param text the string to split
298     * @param expression the regular expression to match
299     * @return an array of strings. The array will be empty if text is empty
300     *
301     * @throws NullPointerException if expression or text is null
302     */
303    public static String[] split(String text, String expression) {
304        if (text.length() == 0) {
305            return EMPTY_STRING_ARRAY;
306        } else {
307            return text.split(expression, -1);
308        }
309    }
310
311    /**
312     * Splits a string on a pattern. String.split() returns [''] when the string to be
313     * split is empty. This returns []. This does not remove any empty strings from the result.
314     * @param text the string to split
315     * @param pattern the regular expression to match
316     * @return an array of strings. The array will be empty if text is empty
317     *
318     * @throws NullPointerException if expression or text is null
319     */
320    public static String[] split(String text, Pattern pattern) {
321        if (text.length() == 0) {
322            return EMPTY_STRING_ARRAY;
323        } else {
324            return pattern.split(text, -1);
325        }
326    }
327
328    /**
329     * An interface for splitting strings according to rules that are opaque to the user of this
330     * interface. This also has less overhead than split, which uses regular expressions and
331     * allocates an array to hold the results.
332     *
333     * <p>The most efficient way to use this class is:
334     *
335     * <pre>
336     * // Once
337     * TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter(delimiter);
338     *
339     * // Once per string to split
340     * splitter.setString(string);
341     * for (String s : splitter) {
342     *     ...
343     * }
344     * </pre>
345     */
346    public interface StringSplitter extends Iterable<String> {
347        public void setString(String string);
348    }
349
350    /**
351     * A simple string splitter.
352     *
353     * <p>If the final character in the string to split is the delimiter then no empty string will
354     * be returned for the empty string after that delimeter. That is, splitting <tt>"a,b,"</tt> on
355     * comma will return <tt>"a", "b"</tt>, not <tt>"a", "b", ""</tt>.
356     */
357    public static class SimpleStringSplitter implements StringSplitter, Iterator<String> {
358        private String mString;
359        private char mDelimiter;
360        private int mPosition;
361        private int mLength;
362
363        /**
364         * Initializes the splitter. setString may be called later.
365         * @param delimiter the delimeter on which to split
366         */
367        public SimpleStringSplitter(char delimiter) {
368            mDelimiter = delimiter;
369        }
370
371        /**
372         * Sets the string to split
373         * @param string the string to split
374         */
375        public void setString(String string) {
376            mString = string;
377            mPosition = 0;
378            mLength = mString.length();
379        }
380
381        public Iterator<String> iterator() {
382            return this;
383        }
384
385        public boolean hasNext() {
386            return mPosition < mLength;
387        }
388
389        public String next() {
390            int end = mString.indexOf(mDelimiter, mPosition);
391            if (end == -1) {
392                end = mLength;
393            }
394            String nextString = mString.substring(mPosition, end);
395            mPosition = end + 1; // Skip the delimiter.
396            return nextString;
397        }
398
399        public void remove() {
400            throw new UnsupportedOperationException();
401        }
402    }
403
404    public static CharSequence stringOrSpannedString(CharSequence source) {
405        if (source == null)
406            return null;
407        if (source instanceof SpannedString)
408            return source;
409        if (source instanceof Spanned)
410            return new SpannedString(source);
411
412        return source.toString();
413    }
414
415    /**
416     * Returns true if the string is null or 0-length.
417     * @param str the string to be examined
418     * @return true if str is null or zero length
419     */
420    public static boolean isEmpty(CharSequence str) {
421        if (str == null || str.length() == 0)
422            return true;
423        else
424            return false;
425    }
426
427    /**
428     * Returns the length that the specified CharSequence would have if
429     * spaces and control characters were trimmed from the start and end,
430     * as by {@link String#trim}.
431     */
432    public static int getTrimmedLength(CharSequence s) {
433        int len = s.length();
434
435        int start = 0;
436        while (start < len && s.charAt(start) <= ' ') {
437            start++;
438        }
439
440        int end = len;
441        while (end > start && s.charAt(end - 1) <= ' ') {
442            end--;
443        }
444
445        return end - start;
446    }
447
448    /**
449     * Returns true if a and b are equal, including if they are both null.
450     *
451     * @param a first CharSequence to check
452     * @param b second CharSequence to check
453     * @return true if a and b are equal
454     */
455    public static boolean equals(CharSequence a, CharSequence b) {
456        return a == b || (a != null && a.equals(b));
457    }
458
459    // XXX currently this only reverses chars, not spans
460    public static CharSequence getReverse(CharSequence source,
461                                          int start, int end) {
462        return new Reverser(source, start, end);
463    }
464
465    private static class Reverser
466    implements CharSequence, GetChars
467    {
468        public Reverser(CharSequence source, int start, int end) {
469            mSource = source;
470            mStart = start;
471            mEnd = end;
472        }
473
474        public int length() {
475            return mEnd - mStart;
476        }
477
478        public CharSequence subSequence(int start, int end) {
479            char[] buf = new char[end - start];
480
481            getChars(start, end, buf, 0);
482            return new String(buf);
483        }
484
485        public String toString() {
486            return subSequence(0, length()).toString();
487        }
488
489        public char charAt(int off) {
490            return AndroidCharacter.getMirror(mSource.charAt(mEnd - 1 - off));
491        }
492
493        public void getChars(int start, int end, char[] dest, int destoff) {
494            TextUtils.getChars(mSource, start + mStart, end + mStart,
495                               dest, destoff);
496            AndroidCharacter.mirror(dest, 0, end - start);
497
498            int len = end - start;
499            int n = (end - start) / 2;
500            for (int i = 0; i < n; i++) {
501                char tmp = dest[destoff + i];
502
503                dest[destoff + i] = dest[destoff + len - i - 1];
504                dest[destoff + len - i - 1] = tmp;
505            }
506        }
507
508        private CharSequence mSource;
509        private int mStart;
510        private int mEnd;
511    }
512
513    private static final int ALIGNMENT_SPAN = 1;
514    private static final int FOREGROUND_COLOR_SPAN = 2;
515    private static final int RELATIVE_SIZE_SPAN = 3;
516    private static final int SCALE_X_SPAN = 4;
517    private static final int STRIKETHROUGH_SPAN = 5;
518    private static final int UNDERLINE_SPAN = 6;
519    private static final int STYLE_SPAN = 7;
520    private static final int BULLET_SPAN = 8;
521    private static final int QUOTE_SPAN = 9;
522    private static final int LEADING_MARGIN_SPAN = 10;
523    private static final int URL_SPAN = 11;
524    private static final int BACKGROUND_COLOR_SPAN = 12;
525    private static final int TYPEFACE_SPAN = 13;
526    private static final int SUPERSCRIPT_SPAN = 14;
527    private static final int SUBSCRIPT_SPAN = 15;
528    private static final int ABSOLUTE_SIZE_SPAN = 16;
529    private static final int TEXT_APPEARANCE_SPAN = 17;
530    private static final int ANNOTATION = 18;
531
532    /**
533     * Flatten a CharSequence and whatever styles can be copied across processes
534     * into the parcel.
535     */
536    public static void writeToParcel(CharSequence cs, Parcel p,
537            int parcelableFlags) {
538        if (cs instanceof Spanned) {
539            p.writeInt(0);
540            p.writeString(cs.toString());
541
542            Spanned sp = (Spanned) cs;
543            Object[] os = sp.getSpans(0, cs.length(), Object.class);
544
545            // note to people adding to this: check more specific types
546            // before more generic types.  also notice that it uses
547            // "if" instead of "else if" where there are interfaces
548            // so one object can be several.
549
550            for (int i = 0; i < os.length; i++) {
551                Object o = os[i];
552                Object prop = os[i];
553
554                if (prop instanceof CharacterStyle) {
555                    prop = ((CharacterStyle) prop).getUnderlying();
556                }
557
558                if (prop instanceof AlignmentSpan) {
559                    p.writeInt(ALIGNMENT_SPAN);
560                    p.writeString(((AlignmentSpan) prop).getAlignment().name());
561                    writeWhere(p, sp, o);
562                }
563
564                if (prop instanceof ForegroundColorSpan) {
565                    p.writeInt(FOREGROUND_COLOR_SPAN);
566                    p.writeInt(((ForegroundColorSpan) prop).getForegroundColor());
567                    writeWhere(p, sp, o);
568                }
569
570                if (prop instanceof RelativeSizeSpan) {
571                    p.writeInt(RELATIVE_SIZE_SPAN);
572                    p.writeFloat(((RelativeSizeSpan) prop).getSizeChange());
573                    writeWhere(p, sp, o);
574                }
575
576                if (prop instanceof ScaleXSpan) {
577                    p.writeInt(SCALE_X_SPAN);
578                    p.writeFloat(((ScaleXSpan) prop).getScaleX());
579                    writeWhere(p, sp, o);
580                }
581
582                if (prop instanceof StrikethroughSpan) {
583                    p.writeInt(STRIKETHROUGH_SPAN);
584                    writeWhere(p, sp, o);
585                }
586
587                if (prop instanceof UnderlineSpan) {
588                    p.writeInt(UNDERLINE_SPAN);
589                    writeWhere(p, sp, o);
590                }
591
592                if (prop instanceof StyleSpan) {
593                    p.writeInt(STYLE_SPAN);
594                    p.writeInt(((StyleSpan) prop).getStyle());
595                    writeWhere(p, sp, o);
596                }
597
598                if (prop instanceof LeadingMarginSpan) {
599                    if (prop instanceof BulletSpan) {
600                        p.writeInt(BULLET_SPAN);
601                        writeWhere(p, sp, o);
602                    } else if (prop instanceof QuoteSpan) {
603                        p.writeInt(QUOTE_SPAN);
604                        p.writeInt(((QuoteSpan) prop).getColor());
605                        writeWhere(p, sp, o);
606                    } else {
607                        p.writeInt(LEADING_MARGIN_SPAN);
608                        p.writeInt(((LeadingMarginSpan) prop).
609                                           getLeadingMargin(true));
610                        p.writeInt(((LeadingMarginSpan) prop).
611                                           getLeadingMargin(false));
612                        writeWhere(p, sp, o);
613                    }
614                }
615
616                if (prop instanceof URLSpan) {
617                    p.writeInt(URL_SPAN);
618                    p.writeString(((URLSpan) prop).getURL());
619                    writeWhere(p, sp, o);
620                }
621
622                if (prop instanceof BackgroundColorSpan) {
623                    p.writeInt(BACKGROUND_COLOR_SPAN);
624                    p.writeInt(((BackgroundColorSpan) prop).getBackgroundColor());
625                    writeWhere(p, sp, o);
626                }
627
628                if (prop instanceof TypefaceSpan) {
629                    p.writeInt(TYPEFACE_SPAN);
630                    p.writeString(((TypefaceSpan) prop).getFamily());
631                    writeWhere(p, sp, o);
632                }
633
634                if (prop instanceof SuperscriptSpan) {
635                    p.writeInt(SUPERSCRIPT_SPAN);
636                    writeWhere(p, sp, o);
637                }
638
639                if (prop instanceof SubscriptSpan) {
640                    p.writeInt(SUBSCRIPT_SPAN);
641                    writeWhere(p, sp, o);
642                }
643
644                if (prop instanceof AbsoluteSizeSpan) {
645                    p.writeInt(ABSOLUTE_SIZE_SPAN);
646                    p.writeInt(((AbsoluteSizeSpan) prop).getSize());
647                    writeWhere(p, sp, o);
648                }
649
650                if (prop instanceof TextAppearanceSpan) {
651                    TextAppearanceSpan tas = (TextAppearanceSpan) prop;
652                    p.writeInt(TEXT_APPEARANCE_SPAN);
653
654                    String tf = tas.getFamily();
655                    if (tf != null) {
656                        p.writeInt(1);
657                        p.writeString(tf);
658                    } else {
659                        p.writeInt(0);
660                    }
661
662                    p.writeInt(tas.getTextSize());
663                    p.writeInt(tas.getTextStyle());
664
665                    ColorStateList csl = tas.getTextColor();
666                    if (csl == null) {
667                        p.writeInt(0);
668                    } else {
669                        p.writeInt(1);
670                        csl.writeToParcel(p, parcelableFlags);
671                    }
672
673                    csl = tas.getLinkTextColor();
674                    if (csl == null) {
675                        p.writeInt(0);
676                    } else {
677                        p.writeInt(1);
678                        csl.writeToParcel(p, parcelableFlags);
679                    }
680
681                    writeWhere(p, sp, o);
682                }
683
684                if (prop instanceof Annotation) {
685                    p.writeInt(ANNOTATION);
686                    p.writeString(((Annotation) prop).getKey());
687                    p.writeString(((Annotation) prop).getValue());
688                    writeWhere(p, sp, o);
689                }
690            }
691
692            p.writeInt(0);
693        } else {
694            p.writeInt(1);
695            if (cs != null) {
696                p.writeString(cs.toString());
697            } else {
698                p.writeString(null);
699            }
700        }
701    }
702
703    private static void writeWhere(Parcel p, Spanned sp, Object o) {
704        p.writeInt(sp.getSpanStart(o));
705        p.writeInt(sp.getSpanEnd(o));
706        p.writeInt(sp.getSpanFlags(o));
707    }
708
709    public static final Parcelable.Creator<CharSequence> CHAR_SEQUENCE_CREATOR
710            = new Parcelable.Creator<CharSequence>()
711    {
712        /**
713         * Read and return a new CharSequence, possibly with styles,
714         * from the parcel.
715         */
716        public  CharSequence createFromParcel(Parcel p) {
717            int kind = p.readInt();
718
719            if (kind == 1)
720                return p.readString();
721
722            SpannableString sp = new SpannableString(p.readString());
723
724            while (true) {
725                kind = p.readInt();
726
727                if (kind == 0)
728                    break;
729
730                switch (kind) {
731                case ALIGNMENT_SPAN:
732                    readSpan(p, sp, new AlignmentSpan.Standard(
733                            Layout.Alignment.valueOf(p.readString())));
734                    break;
735
736                case FOREGROUND_COLOR_SPAN:
737                    readSpan(p, sp, new ForegroundColorSpan(p.readInt()));
738                    break;
739
740                case RELATIVE_SIZE_SPAN:
741                    readSpan(p, sp, new RelativeSizeSpan(p.readFloat()));
742                    break;
743
744                case SCALE_X_SPAN:
745                    readSpan(p, sp, new ScaleXSpan(p.readFloat()));
746                    break;
747
748                case STRIKETHROUGH_SPAN:
749                    readSpan(p, sp, new StrikethroughSpan());
750                    break;
751
752                case UNDERLINE_SPAN:
753                    readSpan(p, sp, new UnderlineSpan());
754                    break;
755
756                case STYLE_SPAN:
757                    readSpan(p, sp, new StyleSpan(p.readInt()));
758                    break;
759
760                case BULLET_SPAN:
761                    readSpan(p, sp, new BulletSpan());
762                    break;
763
764                case QUOTE_SPAN:
765                    readSpan(p, sp, new QuoteSpan(p.readInt()));
766                    break;
767
768                case LEADING_MARGIN_SPAN:
769                    readSpan(p, sp, new LeadingMarginSpan.Standard(p.readInt(),
770                                                                   p.readInt()));
771                break;
772
773                case URL_SPAN:
774                    readSpan(p, sp, new URLSpan(p.readString()));
775                    break;
776
777                case BACKGROUND_COLOR_SPAN:
778                    readSpan(p, sp, new BackgroundColorSpan(p.readInt()));
779                    break;
780
781                case TYPEFACE_SPAN:
782                    readSpan(p, sp, new TypefaceSpan(p.readString()));
783                    break;
784
785                case SUPERSCRIPT_SPAN:
786                    readSpan(p, sp, new SuperscriptSpan());
787                    break;
788
789                case SUBSCRIPT_SPAN:
790                    readSpan(p, sp, new SubscriptSpan());
791                    break;
792
793                case ABSOLUTE_SIZE_SPAN:
794                    readSpan(p, sp, new AbsoluteSizeSpan(p.readInt()));
795                    break;
796
797                case TEXT_APPEARANCE_SPAN:
798                    readSpan(p, sp, new TextAppearanceSpan(
799                        p.readInt() != 0
800                            ? p.readString()
801                            : null,
802                        p.readInt(),
803                        p.readInt(),
804                        p.readInt() != 0
805                            ? ColorStateList.CREATOR.createFromParcel(p)
806                            : null,
807                        p.readInt() != 0
808                            ? ColorStateList.CREATOR.createFromParcel(p)
809                            : null));
810                    break;
811
812                case ANNOTATION:
813                    readSpan(p, sp,
814                             new Annotation(p.readString(), p.readString()));
815                    break;
816
817                default:
818                    throw new RuntimeException("bogus span encoding " + kind);
819                }
820            }
821
822            return sp;
823        }
824
825        public CharSequence[] newArray(int size)
826        {
827            return new CharSequence[size];
828        }
829    };
830
831    /**
832     * Debugging tool to print the spans in a CharSequence.  The output will
833     * be printed one span per line.  If the CharSequence is not a Spanned,
834     * then the entire string will be printed on a single line.
835     */
836    public static void dumpSpans(CharSequence cs, Printer printer, String prefix) {
837        if (cs instanceof Spanned) {
838            Spanned sp = (Spanned) cs;
839            Object[] os = sp.getSpans(0, cs.length(), Object.class);
840
841            for (int i = 0; i < os.length; i++) {
842                Object o = os[i];
843                printer.println(prefix + cs.subSequence(sp.getSpanStart(o),
844                        sp.getSpanEnd(o)) + ": "
845                        + Integer.toHexString(System.identityHashCode(o))
846                        + " " + o.getClass().getCanonicalName()
847                         + " (" + sp.getSpanStart(o) + "-" + sp.getSpanEnd(o)
848                         + ") fl=#" + sp.getSpanFlags(o));
849            }
850        } else {
851            printer.println(prefix + cs + ": (no spans)");
852        }
853    }
854
855    /**
856     * Return a new CharSequence in which each of the source strings is
857     * replaced by the corresponding element of the destinations.
858     */
859    public static CharSequence replace(CharSequence template,
860                                       String[] sources,
861                                       CharSequence[] destinations) {
862        SpannableStringBuilder tb = new SpannableStringBuilder(template);
863
864        for (int i = 0; i < sources.length; i++) {
865            int where = indexOf(tb, sources[i]);
866
867            if (where >= 0)
868                tb.setSpan(sources[i], where, where + sources[i].length(),
869                           Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
870        }
871
872        for (int i = 0; i < sources.length; i++) {
873            int start = tb.getSpanStart(sources[i]);
874            int end = tb.getSpanEnd(sources[i]);
875
876            if (start >= 0) {
877                tb.replace(start, end, destinations[i]);
878            }
879        }
880
881        return tb;
882    }
883
884    /**
885     * Replace instances of "^1", "^2", etc. in the
886     * <code>template</code> CharSequence with the corresponding
887     * <code>values</code>.  "^^" is used to produce a single caret in
888     * the output.  Only up to 9 replacement values are supported,
889     * "^10" will be produce the first replacement value followed by a
890     * '0'.
891     *
892     * @param template the input text containing "^1"-style
893     * placeholder values.  This object is not modified; a copy is
894     * returned.
895     *
896     * @param values CharSequences substituted into the template.  The
897     * first is substituted for "^1", the second for "^2", and so on.
898     *
899     * @return the new CharSequence produced by doing the replacement
900     *
901     * @throws IllegalArgumentException if the template requests a
902     * value that was not provided, or if more than 9 values are
903     * provided.
904     */
905    public static CharSequence expandTemplate(CharSequence template,
906                                              CharSequence... values) {
907        if (values.length > 9) {
908            throw new IllegalArgumentException("max of 9 values are supported");
909        }
910
911        SpannableStringBuilder ssb = new SpannableStringBuilder(template);
912
913        try {
914            int i = 0;
915            while (i < ssb.length()) {
916                if (ssb.charAt(i) == '^') {
917                    char next = ssb.charAt(i+1);
918                    if (next == '^') {
919                        ssb.delete(i+1, i+2);
920                        ++i;
921                        continue;
922                    } else if (Character.isDigit(next)) {
923                        int which = Character.getNumericValue(next) - 1;
924                        if (which < 0) {
925                            throw new IllegalArgumentException(
926                                "template requests value ^" + (which+1));
927                        }
928                        if (which >= values.length) {
929                            throw new IllegalArgumentException(
930                                "template requests value ^" + (which+1) +
931                                "; only " + values.length + " provided");
932                        }
933                        ssb.replace(i, i+2, values[which]);
934                        i += values[which].length();
935                        continue;
936                    }
937                }
938                ++i;
939            }
940        } catch (IndexOutOfBoundsException ignore) {
941            // happens when ^ is the last character in the string.
942        }
943        return ssb;
944    }
945
946    public static int getOffsetBefore(CharSequence text, int offset) {
947        if (offset == 0)
948            return 0;
949        if (offset == 1)
950            return 0;
951
952        char c = text.charAt(offset - 1);
953
954        if (c >= '\uDC00' && c <= '\uDFFF') {
955            char c1 = text.charAt(offset - 2);
956
957            if (c1 >= '\uD800' && c1 <= '\uDBFF')
958                offset -= 2;
959            else
960                offset -= 1;
961        } else {
962            offset -= 1;
963        }
964
965        if (text instanceof Spanned) {
966            ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
967                                                       ReplacementSpan.class);
968
969            for (int i = 0; i < spans.length; i++) {
970                int start = ((Spanned) text).getSpanStart(spans[i]);
971                int end = ((Spanned) text).getSpanEnd(spans[i]);
972
973                if (start < offset && end > offset)
974                    offset = start;
975            }
976        }
977
978        return offset;
979    }
980
981    public static int getOffsetAfter(CharSequence text, int offset) {
982        int len = text.length();
983
984        if (offset == len)
985            return len;
986        if (offset == len - 1)
987            return len;
988
989        char c = text.charAt(offset);
990
991        if (c >= '\uD800' && c <= '\uDBFF') {
992            char c1 = text.charAt(offset + 1);
993
994            if (c1 >= '\uDC00' && c1 <= '\uDFFF')
995                offset += 2;
996            else
997                offset += 1;
998        } else {
999            offset += 1;
1000        }
1001
1002        if (text instanceof Spanned) {
1003            ReplacementSpan[] spans = ((Spanned) text).getSpans(offset, offset,
1004                                                       ReplacementSpan.class);
1005
1006            for (int i = 0; i < spans.length; i++) {
1007                int start = ((Spanned) text).getSpanStart(spans[i]);
1008                int end = ((Spanned) text).getSpanEnd(spans[i]);
1009
1010                if (start < offset && end > offset)
1011                    offset = end;
1012            }
1013        }
1014
1015        return offset;
1016    }
1017
1018    private static void readSpan(Parcel p, Spannable sp, Object o) {
1019        sp.setSpan(o, p.readInt(), p.readInt(), p.readInt());
1020    }
1021
1022    public static void copySpansFrom(Spanned source, int start, int end,
1023                                     Class kind,
1024                                     Spannable dest, int destoff) {
1025        if (kind == null) {
1026            kind = Object.class;
1027        }
1028
1029        Object[] spans = source.getSpans(start, end, kind);
1030
1031        for (int i = 0; i < spans.length; i++) {
1032            int st = source.getSpanStart(spans[i]);
1033            int en = source.getSpanEnd(spans[i]);
1034            int fl = source.getSpanFlags(spans[i]);
1035
1036            if (st < start)
1037                st = start;
1038            if (en > end)
1039                en = end;
1040
1041            dest.setSpan(spans[i], st - start + destoff, en - start + destoff,
1042                         fl);
1043        }
1044    }
1045
1046    public enum TruncateAt {
1047        START,
1048        MIDDLE,
1049        END,
1050        MARQUEE,
1051    }
1052
1053    public interface EllipsizeCallback {
1054        /**
1055         * This method is called to report that the specified region of
1056         * text was ellipsized away by a call to {@link #ellipsize}.
1057         */
1058        public void ellipsized(int start, int end);
1059    }
1060
1061    private static String sEllipsis = null;
1062
1063    /**
1064     * Returns the original text if it fits in the specified width
1065     * given the properties of the specified Paint,
1066     * or, if it does not fit, a truncated
1067     * copy with ellipsis character added at the specified edge or center.
1068     */
1069    public static CharSequence ellipsize(CharSequence text,
1070                                         TextPaint p,
1071                                         float avail, TruncateAt where) {
1072        return ellipsize(text, p, avail, where, false, null);
1073    }
1074
1075    /**
1076     * Returns the original text if it fits in the specified width
1077     * given the properties of the specified Paint,
1078     * or, if it does not fit, a copy with ellipsis character added
1079     * at the specified edge or center.
1080     * If <code>preserveLength</code> is specified, the returned copy
1081     * will be padded with zero-width spaces to preserve the original
1082     * length and offsets instead of truncating.
1083     * If <code>callback</code> is non-null, it will be called to
1084     * report the start and end of the ellipsized range.
1085     */
1086    public static CharSequence ellipsize(CharSequence text,
1087                                         TextPaint p,
1088                                         float avail, TruncateAt where,
1089                                         boolean preserveLength,
1090                                         EllipsizeCallback callback) {
1091        if (sEllipsis == null) {
1092            Resources r = Resources.getSystem();
1093            sEllipsis = r.getString(R.string.ellipsis);
1094        }
1095
1096        int len = text.length();
1097
1098        // Use Paint.breakText() for the non-Spanned case to avoid having
1099        // to allocate memory and accumulate the character widths ourselves.
1100
1101        if (!(text instanceof Spanned)) {
1102            float wid = p.measureText(text, 0, len);
1103
1104            if (wid <= avail) {
1105                if (callback != null) {
1106                    callback.ellipsized(0, 0);
1107                }
1108
1109                return text;
1110            }
1111
1112            float ellipsiswid = p.measureText(sEllipsis);
1113
1114            if (ellipsiswid > avail) {
1115                if (callback != null) {
1116                    callback.ellipsized(0, len);
1117                }
1118
1119                if (preserveLength) {
1120                    char[] buf = obtain(len);
1121                    for (int i = 0; i < len; i++) {
1122                        buf[i] = '\uFEFF';
1123                    }
1124                    String ret = new String(buf, 0, len);
1125                    recycle(buf);
1126                    return ret;
1127                } else {
1128                    return "";
1129                }
1130            }
1131
1132            if (where == TruncateAt.START) {
1133                int fit = p.breakText(text, 0, len, false,
1134                                      avail - ellipsiswid, null);
1135
1136                if (callback != null) {
1137                    callback.ellipsized(0, len - fit);
1138                }
1139
1140                if (preserveLength) {
1141                    return blank(text, 0, len - fit);
1142                } else {
1143                    return sEllipsis + text.toString().substring(len - fit, len);
1144                }
1145            } else if (where == TruncateAt.END) {
1146                int fit = p.breakText(text, 0, len, true,
1147                                      avail - ellipsiswid, null);
1148
1149                if (callback != null) {
1150                    callback.ellipsized(fit, len);
1151                }
1152
1153                if (preserveLength) {
1154                    return blank(text, fit, len);
1155                } else {
1156                    return text.toString().substring(0, fit) + sEllipsis;
1157                }
1158            } else /* where == TruncateAt.MIDDLE */ {
1159                int right = p.breakText(text, 0, len, false,
1160                                        (avail - ellipsiswid) / 2, null);
1161                float used = p.measureText(text, len - right, len);
1162                int left = p.breakText(text, 0, len - right, true,
1163                                       avail - ellipsiswid - used, null);
1164
1165                if (callback != null) {
1166                    callback.ellipsized(left, len - right);
1167                }
1168
1169                if (preserveLength) {
1170                    return blank(text, left, len - right);
1171                } else {
1172                    String s = text.toString();
1173                    return s.substring(0, left) + sEllipsis +
1174                           s.substring(len - right, len);
1175                }
1176            }
1177        }
1178
1179        // But do the Spanned cases by hand, because it's such a pain
1180        // to iterate the span transitions backwards and getTextWidths()
1181        // will give us the information we need.
1182
1183        // getTextWidths() always writes into the start of the array,
1184        // so measure each span into the first half and then copy the
1185        // results into the second half to use later.
1186
1187        float[] wid = new float[len * 2];
1188        TextPaint temppaint = new TextPaint();
1189        Spanned sp = (Spanned) text;
1190
1191        int next;
1192        for (int i = 0; i < len; i = next) {
1193            next = sp.nextSpanTransition(i, len, MetricAffectingSpan.class);
1194
1195            Styled.getTextWidths(p, temppaint, sp, i, next, wid, null);
1196            System.arraycopy(wid, 0, wid, len + i, next - i);
1197        }
1198
1199        float sum = 0;
1200        for (int i = 0; i < len; i++) {
1201            sum += wid[len + i];
1202        }
1203
1204        if (sum <= avail) {
1205            if (callback != null) {
1206                callback.ellipsized(0, 0);
1207            }
1208
1209            return text;
1210        }
1211
1212        float ellipsiswid = p.measureText(sEllipsis);
1213
1214        if (ellipsiswid > avail) {
1215            if (callback != null) {
1216                callback.ellipsized(0, len);
1217            }
1218
1219            if (preserveLength) {
1220                char[] buf = obtain(len);
1221                for (int i = 0; i < len; i++) {
1222                    buf[i] = '\uFEFF';
1223                }
1224                SpannableString ss = new SpannableString(new String(buf, 0, len));
1225                recycle(buf);
1226                copySpansFrom(sp, 0, len, Object.class, ss, 0);
1227                return ss;
1228            } else {
1229                return "";
1230            }
1231        }
1232
1233        if (where == TruncateAt.START) {
1234            sum = 0;
1235            int i;
1236
1237            for (i = len; i >= 0; i--) {
1238                float w = wid[len + i - 1];
1239
1240                if (w + sum + ellipsiswid > avail) {
1241                    break;
1242                }
1243
1244                sum += w;
1245            }
1246
1247            if (callback != null) {
1248                callback.ellipsized(0, i);
1249            }
1250
1251            if (preserveLength) {
1252                SpannableString ss = new SpannableString(blank(text, 0, i));
1253                copySpansFrom(sp, 0, len, Object.class, ss, 0);
1254                return ss;
1255            } else {
1256                SpannableStringBuilder out = new SpannableStringBuilder(sEllipsis);
1257                out.insert(1, text, i, len);
1258
1259                return out;
1260            }
1261        } else if (where == TruncateAt.END) {
1262            sum = 0;
1263            int i;
1264
1265            for (i = 0; i < len; i++) {
1266                float w = wid[len + i];
1267
1268                if (w + sum + ellipsiswid > avail) {
1269                    break;
1270                }
1271
1272                sum += w;
1273            }
1274
1275            if (callback != null) {
1276                callback.ellipsized(i, len);
1277            }
1278
1279            if (preserveLength) {
1280                SpannableString ss = new SpannableString(blank(text, i, len));
1281                copySpansFrom(sp, 0, len, Object.class, ss, 0);
1282                return ss;
1283            } else {
1284                SpannableStringBuilder out = new SpannableStringBuilder(sEllipsis);
1285                out.insert(0, text, 0, i);
1286
1287                return out;
1288            }
1289        } else /* where = TruncateAt.MIDDLE */ {
1290            float lsum = 0, rsum = 0;
1291            int left = 0, right = len;
1292
1293            float ravail = (avail - ellipsiswid) / 2;
1294            for (right = len; right >= 0; right--) {
1295                float w = wid[len + right - 1];
1296
1297                if (w + rsum > ravail) {
1298                    break;
1299                }
1300
1301                rsum += w;
1302            }
1303
1304            float lavail = avail - ellipsiswid - rsum;
1305            for (left = 0; left < right; left++) {
1306                float w = wid[len + left];
1307
1308                if (w + lsum > lavail) {
1309                    break;
1310                }
1311
1312                lsum += w;
1313            }
1314
1315            if (callback != null) {
1316                callback.ellipsized(left, right);
1317            }
1318
1319            if (preserveLength) {
1320                SpannableString ss = new SpannableString(blank(text, left, right));
1321                copySpansFrom(sp, 0, len, Object.class, ss, 0);
1322                return ss;
1323            } else {
1324                SpannableStringBuilder out = new SpannableStringBuilder(sEllipsis);
1325                out.insert(0, text, 0, left);
1326                out.insert(out.length(), text, right, len);
1327
1328                return out;
1329            }
1330        }
1331    }
1332
1333    private static String blank(CharSequence source, int start, int end) {
1334        int len = source.length();
1335        char[] buf = obtain(len);
1336
1337        if (start != 0) {
1338            getChars(source, 0, start, buf, 0);
1339        }
1340        if (end != len) {
1341            getChars(source, end, len, buf, end);
1342        }
1343
1344        if (start != end) {
1345            buf[start] = '\u2026';
1346
1347            for (int i = start + 1; i < end; i++) {
1348                buf[i] = '\uFEFF';
1349            }
1350        }
1351
1352        String ret = new String(buf, 0, len);
1353        recycle(buf);
1354
1355        return ret;
1356    }
1357
1358    /**
1359     * Converts a CharSequence of the comma-separated form "Andy, Bob,
1360     * Charles, David" that is too wide to fit into the specified width
1361     * into one like "Andy, Bob, 2 more".
1362     *
1363     * @param text the text to truncate
1364     * @param p the Paint with which to measure the text
1365     * @param avail the horizontal width available for the text
1366     * @param oneMore the string for "1 more" in the current locale
1367     * @param more the string for "%d more" in the current locale
1368     */
1369    public static CharSequence commaEllipsize(CharSequence text,
1370                                              TextPaint p, float avail,
1371                                              String oneMore,
1372                                              String more) {
1373        int len = text.length();
1374        char[] buf = new char[len];
1375        TextUtils.getChars(text, 0, len, buf, 0);
1376
1377        int commaCount = 0;
1378        for (int i = 0; i < len; i++) {
1379            if (buf[i] == ',') {
1380                commaCount++;
1381            }
1382        }
1383
1384        float[] wid;
1385
1386        if (text instanceof Spanned) {
1387            Spanned sp = (Spanned) text;
1388            TextPaint temppaint = new TextPaint();
1389            wid = new float[len * 2];
1390
1391            int next;
1392            for (int i = 0; i < len; i = next) {
1393                next = sp.nextSpanTransition(i, len, MetricAffectingSpan.class);
1394
1395                Styled.getTextWidths(p, temppaint, sp, i, next, wid, null);
1396                System.arraycopy(wid, 0, wid, len + i, next - i);
1397            }
1398
1399            System.arraycopy(wid, len, wid, 0, len);
1400        } else {
1401            wid = new float[len];
1402            p.getTextWidths(text, 0, len, wid);
1403        }
1404
1405        int ok = 0;
1406        int okRemaining = commaCount + 1;
1407        String okFormat = "";
1408
1409        int w = 0;
1410        int count = 0;
1411
1412        for (int i = 0; i < len; i++) {
1413            w += wid[i];
1414
1415            if (buf[i] == ',') {
1416                count++;
1417
1418                int remaining = commaCount - count + 1;
1419                float moreWid;
1420                String format;
1421
1422                if (remaining == 1) {
1423                    format = " " + oneMore;
1424                } else {
1425                    format = " " + String.format(more, remaining);
1426                }
1427
1428                moreWid = p.measureText(format);
1429
1430                if (w + moreWid <= avail) {
1431                    ok = i + 1;
1432                    okRemaining = remaining;
1433                    okFormat = format;
1434                }
1435            }
1436        }
1437
1438        if (w <= avail) {
1439            return text;
1440        } else {
1441            SpannableStringBuilder out = new SpannableStringBuilder(okFormat);
1442            out.insert(0, text, 0, ok);
1443            return out;
1444        }
1445    }
1446
1447    /* package */ static char[] obtain(int len) {
1448        char[] buf;
1449
1450        synchronized (sLock) {
1451            buf = sTemp;
1452            sTemp = null;
1453        }
1454
1455        if (buf == null || buf.length < len)
1456            buf = new char[ArrayUtils.idealCharArraySize(len)];
1457
1458        return buf;
1459    }
1460
1461    /* package */ static void recycle(char[] temp) {
1462        if (temp.length > 1000)
1463            return;
1464
1465        synchronized (sLock) {
1466            sTemp = temp;
1467        }
1468    }
1469
1470    /**
1471     * Html-encode the string.
1472     * @param s the string to be encoded
1473     * @return the encoded string
1474     */
1475    public static String htmlEncode(String s) {
1476        StringBuilder sb = new StringBuilder();
1477        char c;
1478        for (int i = 0; i < s.length(); i++) {
1479            c = s.charAt(i);
1480            switch (c) {
1481            case '<':
1482                sb.append("&lt;"); //$NON-NLS-1$
1483                break;
1484            case '>':
1485                sb.append("&gt;"); //$NON-NLS-1$
1486                break;
1487            case '&':
1488                sb.append("&amp;"); //$NON-NLS-1$
1489                break;
1490            case '\'':
1491                sb.append("&apos;"); //$NON-NLS-1$
1492                break;
1493            case '"':
1494                sb.append("&quot;"); //$NON-NLS-1$
1495                break;
1496            default:
1497                sb.append(c);
1498            }
1499        }
1500        return sb.toString();
1501    }
1502
1503    /**
1504     * Returns a CharSequence concatenating the specified CharSequences,
1505     * retaining their spans if any.
1506     */
1507    public static CharSequence concat(CharSequence... text) {
1508        if (text.length == 0) {
1509            return "";
1510        }
1511
1512        if (text.length == 1) {
1513            return text[0];
1514        }
1515
1516        boolean spanned = false;
1517        for (int i = 0; i < text.length; i++) {
1518            if (text[i] instanceof Spanned) {
1519                spanned = true;
1520                break;
1521            }
1522        }
1523
1524        StringBuilder sb = new StringBuilder();
1525        for (int i = 0; i < text.length; i++) {
1526            sb.append(text[i]);
1527        }
1528
1529        if (!spanned) {
1530            return sb.toString();
1531        }
1532
1533        SpannableString ss = new SpannableString(sb);
1534        int off = 0;
1535        for (int i = 0; i < text.length; i++) {
1536            int len = text[i].length();
1537
1538            if (text[i] instanceof Spanned) {
1539                copySpansFrom((Spanned) text[i], 0, len, Object.class, ss, off);
1540            }
1541
1542            off += len;
1543        }
1544
1545        return new SpannedString(ss);
1546    }
1547
1548    /**
1549     * Returns whether the given CharSequence contains any printable characters.
1550     */
1551    public static boolean isGraphic(CharSequence str) {
1552        final int len = str.length();
1553        for (int i=0; i<len; i++) {
1554            int gc = Character.getType(str.charAt(i));
1555            if (gc != Character.CONTROL
1556                    && gc != Character.FORMAT
1557                    && gc != Character.SURROGATE
1558                    && gc != Character.UNASSIGNED
1559                    && gc != Character.LINE_SEPARATOR
1560                    && gc != Character.PARAGRAPH_SEPARATOR
1561                    && gc != Character.SPACE_SEPARATOR) {
1562                return true;
1563            }
1564        }
1565        return false;
1566    }
1567
1568    /**
1569     * Returns whether this character is a printable character.
1570     */
1571    public static boolean isGraphic(char c) {
1572        int gc = Character.getType(c);
1573        return     gc != Character.CONTROL
1574                && gc != Character.FORMAT
1575                && gc != Character.SURROGATE
1576                && gc != Character.UNASSIGNED
1577                && gc != Character.LINE_SEPARATOR
1578                && gc != Character.PARAGRAPH_SEPARATOR
1579                && gc != Character.SPACE_SEPARATOR;
1580    }
1581
1582    /**
1583     * Returns whether the given CharSequence contains only digits.
1584     */
1585    public static boolean isDigitsOnly(CharSequence str) {
1586        final int len = str.length();
1587        for (int i = 0; i < len; i++) {
1588            if (!Character.isDigit(str.charAt(i))) {
1589                return false;
1590            }
1591        }
1592        return true;
1593    }
1594
1595    /**
1596     * Capitalization mode for {@link #getCapsMode}: capitalize all
1597     * characters.  This value is explicitly defined to be the same as
1598     * {@link InputType#TYPE_TEXT_FLAG_CAP_CHARACTERS}.
1599     */
1600    public static final int CAP_MODE_CHARACTERS
1601            = InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
1602
1603    /**
1604     * Capitalization mode for {@link #getCapsMode}: capitalize the first
1605     * character of all words.  This value is explicitly defined to be the same as
1606     * {@link InputType#TYPE_TEXT_FLAG_CAP_WORDS}.
1607     */
1608    public static final int CAP_MODE_WORDS
1609            = InputType.TYPE_TEXT_FLAG_CAP_WORDS;
1610
1611    /**
1612     * Capitalization mode for {@link #getCapsMode}: capitalize the first
1613     * character of each sentence.  This value is explicitly defined to be the same as
1614     * {@link InputType#TYPE_TEXT_FLAG_CAP_SENTENCES}.
1615     */
1616    public static final int CAP_MODE_SENTENCES
1617            = InputType.TYPE_TEXT_FLAG_CAP_SENTENCES;
1618
1619    /**
1620     * Determine what caps mode should be in effect at the current offset in
1621     * the text.  Only the mode bits set in <var>reqModes</var> will be
1622     * checked.  Note that the caps mode flags here are explicitly defined
1623     * to match those in {@link InputType}.
1624     *
1625     * @param cs The text that should be checked for caps modes.
1626     * @param off Location in the text at which to check.
1627     * @param reqModes The modes to be checked: may be any combination of
1628     * {@link #CAP_MODE_CHARACTERS}, {@link #CAP_MODE_WORDS}, and
1629     * {@link #CAP_MODE_SENTENCES}.
1630     *
1631     * @return Returns the actual capitalization modes that can be in effect
1632     * at the current position, which is any combination of
1633     * {@link #CAP_MODE_CHARACTERS}, {@link #CAP_MODE_WORDS}, and
1634     * {@link #CAP_MODE_SENTENCES}.
1635     */
1636    public static int getCapsMode(CharSequence cs, int off, int reqModes) {
1637        int i;
1638        char c;
1639        int mode = 0;
1640
1641        if ((reqModes&CAP_MODE_CHARACTERS) != 0) {
1642            mode |= CAP_MODE_CHARACTERS;
1643        }
1644        if ((reqModes&(CAP_MODE_WORDS|CAP_MODE_SENTENCES)) == 0) {
1645            return mode;
1646        }
1647
1648        // Back over allowed opening punctuation.
1649
1650        for (i = off; i > 0; i--) {
1651            c = cs.charAt(i - 1);
1652
1653            if (c != '"' && c != '\'' &&
1654                Character.getType(c) != Character.START_PUNCTUATION) {
1655                break;
1656            }
1657        }
1658
1659        // Start of paragraph, with optional whitespace.
1660
1661        int j = i;
1662        while (j > 0 && ((c = cs.charAt(j - 1)) == ' ' || c == '\t')) {
1663            j--;
1664        }
1665        if (j == 0 || cs.charAt(j - 1) == '\n') {
1666            return mode | CAP_MODE_WORDS;
1667        }
1668
1669        // Or start of word if we are that style.
1670
1671        if ((reqModes&CAP_MODE_SENTENCES) == 0) {
1672            if (i != j) mode |= CAP_MODE_WORDS;
1673            return mode;
1674        }
1675
1676        // There must be a space if not the start of paragraph.
1677
1678        if (i == j) {
1679            return mode;
1680        }
1681
1682        // Back over allowed closing punctuation.
1683
1684        for (; j > 0; j--) {
1685            c = cs.charAt(j - 1);
1686
1687            if (c != '"' && c != '\'' &&
1688                Character.getType(c) != Character.END_PUNCTUATION) {
1689                break;
1690            }
1691        }
1692
1693        if (j > 0) {
1694            c = cs.charAt(j - 1);
1695
1696            if (c == '.' || c == '?' || c == '!') {
1697                // Do not capitalize if the word ends with a period but
1698                // also contains a period, in which case it is an abbreviation.
1699
1700                if (c == '.') {
1701                    for (int k = j - 2; k >= 0; k--) {
1702                        c = cs.charAt(k);
1703
1704                        if (c == '.') {
1705                            return mode;
1706                        }
1707
1708                        if (!Character.isLetter(c)) {
1709                            break;
1710                        }
1711                    }
1712                }
1713
1714                return mode | CAP_MODE_SENTENCES;
1715            }
1716        }
1717
1718        return mode;
1719    }
1720
1721    private static Object sLock = new Object();
1722    private static char[] sTemp = null;
1723}
1724