Home | History | Annotate | Download | only in lang
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  ********************************************************************************
      6  * Copyright (C) 2010-2014, Google, International Business Machines Corporation *
      7  * and others. All Rights Reserved.                                                 *
      8  ********************************************************************************
      9  */
     10 package android.icu.lang;
     11 
     12 
     13 /**
     14  * A number of utilities for dealing with CharSequences and related classes.
     15  * For accessing codepoints with a CharSequence, also see
     16  * <ul>
     17  * <li>{@link java.lang.Character#codePointAt(CharSequence, int)}</li>
     18  * <li>{@link java.lang.Character#codePointBefore(CharSequence, int)}</li>
     19  * <li>{@link java.lang.Character#codePointCount(CharSequence, int, int)}</li>
     20  * <li>{@link java.lang.Character#charCount(int)}</li>
     21  * <li>{@link java.lang.Character#offsetByCodePoints(CharSequence, int, int)}</li>
     22  * <li>{@link java.lang.Character#toChars(int, char[], int)}</li>
     23  * <li>{@link java.lang.Character#toCodePoint(char, char)}</li>
     24  * </ul>
     25  * @author markdavis
     26  * @deprecated This API is ICU internal only.
     27  * @hide Only a subset of ICU is exposed in Android
     28  * @hide draft / provisional / internal are hidden on Android
     29  */
     30 @Deprecated
     31 public class CharSequences {
     32     // TODO
     33     // compareTo(a, b);
     34     // compareToIgnoreCase(a, b)
     35     // contentEquals(a, b)
     36     // contentEqualsIgnoreCase(a, b)
     37 
     38     // contains(a, b) => indexOf >= 0
     39     // endsWith(a, b)
     40     // startsWith(a, b)
     41 
     42     // lastIndexOf(a, b, fromIndex)
     43     // indexOf(a, ch, fromIndex)
     44     // lastIndexOf(a, ch, fromIndex);
     45 
     46     // s.trim() => UnicodeSet.trim(CharSequence s); return a subsequence starting with the first character not in the set to the last character not in the set.
     47     // add UnicodeSet.split(CharSequence s);
     48 
     49     /**
     50      * Find the longest n such that a[aIndex,n] = b[bIndex,n], and n is on a character boundary.
     51      * @deprecated This API is ICU internal only.
     52      * @hide draft / provisional / internal are hidden on Android
     53      */
     54     @Deprecated
     55     public static int matchAfter(CharSequence a, CharSequence b, int aIndex, int bIndex) {
     56         int i = aIndex, j = bIndex;
     57         int alen = a.length();
     58         int blen = b.length();
     59         for (; i < alen && j < blen; ++i, ++j) {
     60             char ca = a.charAt(i);
     61             char cb = b.charAt(j);
     62             if (ca != cb) {
     63                 break;
     64             }
     65         }
     66         // if we failed a match make sure that we didn't match half a character
     67         int result = i - aIndex;
     68         if (result != 0 && !onCharacterBoundary(a, i) && !onCharacterBoundary(b, j)) {
     69             --result; // backup
     70         }
     71         return result;
     72     }
     73 
     74     /**
     75      * Count the code point length. Unpaired surrogates count as 1.
     76      * @deprecated This API is ICU internal only.
     77      * @hide draft / provisional / internal are hidden on Android
     78      */
     79     @Deprecated
     80     public int codePointLength(CharSequence s) {
     81         return Character.codePointCount(s, 0, s.length());
     82 //        int length = s.length();
     83 //        int result = length;
     84 //        for (int i = 1; i < length; ++i) {
     85 //            char ch = s.charAt(i);
     86 //            if (0xDC00 <= ch && ch <= 0xDFFF) {
     87 //                char ch0 = s.charAt(i-1);
     88 //                if (0xD800 <= ch && ch <= 0xDbFF) {
     89 //                    --result;
     90 //                }
     91 //            }
     92 //        }
     93     }
     94 
     95     /**
     96      * Utility function for comparing codepoint to string without generating new
     97      * string.
     98      *
     99      * @deprecated This API is ICU internal only.
    100      * @hide draft / provisional / internal are hidden on Android
    101      */
    102     @Deprecated
    103     public static final boolean equals(int codepoint, CharSequence other) {
    104         if (other == null) {
    105             return false;
    106         }
    107         switch (other.length()) {
    108         case 1: return codepoint == other.charAt(0);
    109         case 2: return codepoint > 0xFFFF && codepoint == Character.codePointAt(other, 0);
    110         default: return false;
    111         }
    112     }
    113 
    114     /**
    115      * @deprecated This API is ICU internal only.
    116      * @hide draft / provisional / internal are hidden on Android
    117      */
    118     @Deprecated
    119     public static final boolean equals(CharSequence other, int codepoint) {
    120         return equals(codepoint, other);
    121     }
    122 
    123     /**
    124      * Utility to compare a string to a code point.
    125      * Same results as turning the code point into a string (with the [ugly] new StringBuilder().appendCodePoint(codepoint).toString())
    126      * and comparing, but much faster (no object creation).
    127      * Actually, there is one difference; a null compares as less.
    128      * Note that this (=String) order is UTF-16 order -- *not* code point order.
    129      *
    130      * @deprecated This API is ICU internal only.
    131      * @hide draft / provisional / internal are hidden on Android
    132      */
    133     @Deprecated
    134     public static int compare(CharSequence string, int codePoint) {
    135         if (codePoint < Character.MIN_CODE_POINT || codePoint > Character.MAX_CODE_POINT) {
    136             throw new IllegalArgumentException();
    137         }
    138         int stringLength = string.length();
    139         if (stringLength == 0) {
    140             return -1;
    141         }
    142         char firstChar = string.charAt(0);
    143         int offset = codePoint - Character.MIN_SUPPLEMENTARY_CODE_POINT;
    144 
    145         if (offset < 0) { // BMP codePoint
    146             int result = firstChar - codePoint;
    147             if (result != 0) {
    148                 return result;
    149             }
    150             return stringLength - 1;
    151         }
    152         // non BMP
    153         char lead = (char)((offset >>> 10) + Character.MIN_HIGH_SURROGATE);
    154         int result = firstChar - lead;
    155         if (result != 0) {
    156             return result;
    157         }
    158         if (stringLength > 1) {
    159             char trail = (char)((offset & 0x3ff) + Character.MIN_LOW_SURROGATE);
    160             result = string.charAt(1) - trail;
    161             if (result != 0) {
    162                 return result;
    163             }
    164         }
    165         return stringLength - 2;
    166     }
    167 
    168     /**
    169      * Utility to compare a string to a code point.
    170      * Same results as turning the code point into a string and comparing, but much faster (no object creation).
    171      * Actually, there is one difference; a null compares as less.
    172      * Note that this (=String) order is UTF-16 order -- *not* code point order.
    173      *
    174      * @deprecated This API is ICU internal only.
    175      * @hide draft / provisional / internal are hidden on Android
    176      */
    177     @Deprecated
    178     public static int compare(int codepoint, CharSequence a) {
    179         int result = compare(a, codepoint);
    180         return result > 0 ? -1 : result < 0 ? 1 : 0; // Reverse the order.
    181     }
    182 
    183     /**
    184      * Return the value of the first code point, if the string is exactly one code point. Otherwise return Integer.MAX_VALUE.
    185      *
    186      * @deprecated This API is ICU internal only.
    187      * @hide draft / provisional / internal are hidden on Android
    188      */
    189     @Deprecated
    190     public static int getSingleCodePoint(CharSequence s) {
    191         int length = s.length();
    192         if (length < 1 || length > 2) {
    193             return Integer.MAX_VALUE;
    194         }
    195         int result = Character.codePointAt(s, 0);
    196         return (result < 0x10000) == (length == 1) ? result : Integer.MAX_VALUE;
    197     }
    198 
    199     /**
    200      * Utility function for comparing objects that may be null
    201      * string.
    202      *
    203      * @deprecated This API is ICU internal only.
    204      * @hide draft / provisional / internal are hidden on Android
    205      */
    206     @Deprecated
    207     public static final <T extends Object> boolean equals(T a, T b) {
    208         return a == null ? b == null
    209                 : b == null ? false
    210                         : a.equals(b);
    211     }
    212 
    213     /**
    214      * Utility for comparing the contents of CharSequences
    215      *
    216      * @deprecated This API is ICU internal only.
    217      * @hide draft / provisional / internal are hidden on Android
    218      */
    219     @Deprecated
    220     public static int compare(CharSequence a, CharSequence b) {
    221         int alength = a.length();
    222         int blength = b.length();
    223         int min = alength <= blength ? alength : blength;
    224         for (int i = 0; i < min; ++i) {
    225             int diff = a.charAt(i) - b.charAt(i);
    226             if (diff != 0) {
    227                 return diff;
    228             }
    229         }
    230         return alength - blength;
    231     }
    232 
    233     /**
    234      * Utility for comparing the contents of CharSequences
    235      *
    236      * @deprecated This API is ICU internal only.
    237      * @hide draft / provisional / internal are hidden on Android
    238      */
    239     @Deprecated
    240     public static boolean equalsChars(CharSequence a, CharSequence b) {
    241         // do length test first for fast path
    242         return a.length() == b.length() && compare(a,b) == 0;
    243     }
    244 
    245     /**
    246      * Are we on a character boundary?
    247      *
    248      * @deprecated This API is ICU internal only.
    249      * @hide draft / provisional / internal are hidden on Android
    250      */
    251     @Deprecated
    252     public static boolean onCharacterBoundary(CharSequence s, int i) {
    253         return i <= 0
    254         || i >= s.length()
    255         || !Character.isHighSurrogate(s.charAt(i-1))
    256         || !Character.isLowSurrogate(s.charAt(i));
    257     }
    258 
    259     /**
    260      * Find code point in string.
    261      *
    262      * @deprecated This API is ICU internal only.
    263      * @hide draft / provisional / internal are hidden on Android
    264      */
    265     @Deprecated
    266     public static int indexOf(CharSequence s, int codePoint) {
    267         int cp;
    268         for (int i = 0; i < s.length(); i += Character.charCount(cp)) {
    269             cp = Character.codePointAt(s, i);
    270             if (cp == codePoint) {
    271                 return i;
    272             }
    273         }
    274         return -1;
    275     }
    276 
    277     /**
    278      * Utility function for simplified, more robust loops, such as:
    279      * <pre>
    280      *   for (int codePoint : CharSequences.codePoints(string)) {
    281      *     doSomethingWith(codePoint);
    282      *   }
    283      * </pre>
    284      *
    285      * @deprecated This API is ICU internal only.
    286      * @hide draft / provisional / internal are hidden on Android
    287      */
    288     @Deprecated
    289     public static int[] codePoints(CharSequence s) {
    290         int[] result = new int[s.length()]; // in the vast majority of cases, the length is the same
    291         int j = 0;
    292         for (int i = 0; i < s.length(); ++i) {
    293             char cp = s.charAt(i);
    294             if (cp >= 0xDC00 && cp <= 0xDFFF && i != 0 ) { // hand-code for speed
    295                 char last = (char) result[j-1];
    296                 if (last >= 0xD800 && last <= 0xDBFF) {
    297                     // Note: j-1 is safe, because j can only be zero if i is zero. But i!=0 in this block.
    298                     result[j-1] = Character.toCodePoint(last, cp);
    299                     continue;
    300                 }
    301             }
    302             result[j++] = cp;
    303         }
    304         if (j == result.length) {
    305             return result;
    306         }
    307         int[] shortResult = new int[j];
    308         System.arraycopy(result, 0, shortResult, 0, j);
    309         return shortResult;
    310     }
    311 
    312     private CharSequences() {
    313     }
    314 }
    315