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