]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/text/UnicodeSet.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / text / UnicodeSet.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 package com.ibm.icu.text;\r
8 \r
9 import java.io.IOException;\r
10 import java.text.ParsePosition;\r
11 import java.util.ArrayList;\r
12 import java.util.Collection;\r
13 import java.util.Collections;\r
14 import java.util.Iterator;\r
15 import java.util.MissingResourceException;\r
16 import java.util.TreeSet;\r
17 \r
18 import com.ibm.icu.impl.BMPSet;\r
19 import com.ibm.icu.impl.Norm2AllModes;\r
20 import com.ibm.icu.impl.RuleCharacterIterator;\r
21 import com.ibm.icu.impl.SortedSetRelation;\r
22 import com.ibm.icu.impl.UBiDiProps;\r
23 import com.ibm.icu.impl.UCaseProps;\r
24 import com.ibm.icu.impl.UCharacterProperty;\r
25 import com.ibm.icu.impl.UPropertyAliases;\r
26 import com.ibm.icu.impl.UnicodeSetStringSpan;\r
27 import com.ibm.icu.impl.Utility;\r
28 import com.ibm.icu.lang.UCharacter;\r
29 import com.ibm.icu.lang.UProperty;\r
30 import com.ibm.icu.util.Freezable;\r
31 import com.ibm.icu.util.ULocale;\r
32 import com.ibm.icu.util.VersionInfo;\r
33 \r
34 /**\r
35  * A mutable set of Unicode characters and multicharacter strings.\r
36  * Objects of this class represent <em>character classes</em> used\r
37  * in regular expressions. A character specifies a subset of Unicode\r
38  * code points.  Legal code points are U+0000 to U+10FFFF, inclusive.\r
39  *\r
40  * Note: method freeze() will not only makes the set immutable, but\r
41  * also makes important methods much higher performance:\r
42  * contains(c), containsNone(...), span(...), spanBack(...) etc.\r
43  * After the object is frozen, any subsequent call that wants to change\r
44  * the object will throw UnsupportedOperationException.\r
45  *\r
46  * <p>The UnicodeSet class is not designed to be subclassed.\r
47  *\r
48  * <p><code>UnicodeSet</code> supports two APIs. The first is the\r
49  * <em>operand</em> API that allows the caller to modify the value of\r
50  * a <code>UnicodeSet</code> object. It conforms to Java 2's\r
51  * <code>java.util.Set</code> interface, although\r
52  * <code>UnicodeSet</code> does not actually implement that\r
53  * interface. All methods of <code>Set</code> are supported, with the\r
54  * modification that they take a character range or single character\r
55  * instead of an <code>Object</code>, and they take a\r
56  * <code>UnicodeSet</code> instead of a <code>Collection</code>.  The\r
57  * operand API may be thought of in terms of boolean logic: a boolean\r
58  * OR is implemented by <code>add</code>, a boolean AND is implemented\r
59  * by <code>retain</code>, a boolean XOR is implemented by\r
60  * <code>complement</code> taking an argument, and a boolean NOT is\r
61  * implemented by <code>complement</code> with no argument.  In terms\r
62  * of traditional set theory function names, <code>add</code> is a\r
63  * union, <code>retain</code> is an intersection, <code>remove</code>\r
64  * is an asymmetric difference, and <code>complement</code> with no\r
65  * argument is a set complement with respect to the superset range\r
66  * <code>MIN_VALUE-MAX_VALUE</code>\r
67  *\r
68  * <p>The second API is the\r
69  * <code>applyPattern()</code>/<code>toPattern()</code> API from the\r
70  * <code>java.text.Format</code>-derived classes.  Unlike the\r
71  * methods that add characters, add categories, and control the logic\r
72  * of the set, the method <code>applyPattern()</code> sets all\r
73  * attributes of a <code>UnicodeSet</code> at once, based on a\r
74  * string pattern.\r
75  *\r
76  * <p><b>Pattern syntax</b></p>\r
77  *\r
78  * Patterns are accepted by the constructors and the\r
79  * <code>applyPattern()</code> methods and returned by the\r
80  * <code>toPattern()</code> method.  These patterns follow a syntax\r
81  * similar to that employed by version 8 regular expression character\r
82  * classes.  Here are some simple examples:\r
83  *\r
84  * <blockquote>\r
85  *   <table>\r
86  *     <tr align="top">\r
87  *       <td nowrap valign="top" align="left"><code>[]</code></td>\r
88  *       <td valign="top">No characters</td>\r
89  *     </tr><tr align="top">\r
90  *       <td nowrap valign="top" align="left"><code>[a]</code></td>\r
91  *       <td valign="top">The character 'a'</td>\r
92  *     </tr><tr align="top">\r
93  *       <td nowrap valign="top" align="left"><code>[ae]</code></td>\r
94  *       <td valign="top">The characters 'a' and 'e'</td>\r
95  *     </tr>\r
96  *     <tr>\r
97  *       <td nowrap valign="top" align="left"><code>[a-e]</code></td>\r
98  *       <td valign="top">The characters 'a' through 'e' inclusive, in Unicode code\r
99  *       point order</td>\r
100  *     </tr>\r
101  *     <tr>\r
102  *       <td nowrap valign="top" align="left"><code>[\\u4E01]</code></td>\r
103  *       <td valign="top">The character U+4E01</td>\r
104  *     </tr>\r
105  *     <tr>\r
106  *       <td nowrap valign="top" align="left"><code>[a{ab}{ac}]</code></td>\r
107  *       <td valign="top">The character 'a' and the multicharacter strings &quot;ab&quot; and\r
108  *       &quot;ac&quot;</td>\r
109  *     </tr>\r
110  *     <tr>\r
111  *       <td nowrap valign="top" align="left"><code>[\p{Lu}]</code></td>\r
112  *       <td valign="top">All characters in the general category Uppercase Letter</td>\r
113  *     </tr>\r
114  *   </table>\r
115  * </blockquote>\r
116  *\r
117  * Any character may be preceded by a backslash in order to remove any special\r
118  * meaning.  White space characters, as defined by UCharacterProperty.isRuleWhiteSpace(), are\r
119  * ignored, unless they are escaped.\r
120  *\r
121  * <p>Property patterns specify a set of characters having a certain\r
122  * property as defined by the Unicode standard.  Both the POSIX-like\r
123  * "[:Lu:]" and the Perl-like syntax "\p{Lu}" are recognized.  For a\r
124  * complete list of supported property patterns, see the User's Guide\r
125  * for UnicodeSet at\r
126  * <a href="http://www.icu-project.org/userguide/unicodeSet.html">\r
127  * http://www.icu-project.org/userguide/unicodeSet.html</a>.\r
128  * Actual determination of property data is defined by the underlying\r
129  * Unicode database as implemented by UCharacter.\r
130  *\r
131  * <p>Patterns specify individual characters, ranges of characters, and\r
132  * Unicode property sets.  When elements are concatenated, they\r
133  * specify their union.  To complement a set, place a '^' immediately\r
134  * after the opening '['.  Property patterns are inverted by modifying\r
135  * their delimiters; "[:^foo]" and "\P{foo}".  In any other location,\r
136  * '^' has no special meaning.\r
137  *\r
138  * <p>Ranges are indicated by placing two a '-' between two\r
139  * characters, as in "a-z".  This specifies the range of all\r
140  * characters from the left to the right, in Unicode order.  If the\r
141  * left character is greater than or equal to the\r
142  * right character it is a syntax error.  If a '-' occurs as the first\r
143  * character after the opening '[' or '[^', or if it occurs as the\r
144  * last character before the closing ']', then it is taken as a\r
145  * literal.  Thus "[a\\-b]", "[-ab]", and "[ab-]" all indicate the same\r
146  * set of three characters, 'a', 'b', and '-'.\r
147  *\r
148  * <p>Sets may be intersected using the '&' operator or the asymmetric\r
149  * set difference may be taken using the '-' operator, for example,\r
150  * "[[:L:]&[\\u0000-\\u0FFF]]" indicates the set of all Unicode letters\r
151  * with values less than 4096.  Operators ('&' and '|') have equal\r
152  * precedence and bind left-to-right.  Thus\r
153  * "[[:L:]-[a-z]-[\\u0100-\\u01FF]]" is equivalent to\r
154  * "[[[:L:]-[a-z]]-[\\u0100-\\u01FF]]".  This only really matters for\r
155  * difference; intersection is commutative.\r
156  *\r
157  * <table>\r
158  * <tr valign=top><td nowrap><code>[a]</code><td>The set containing 'a'\r
159  * <tr valign=top><td nowrap><code>[a-z]</code><td>The set containing 'a'\r
160  * through 'z' and all letters in between, in Unicode order\r
161  * <tr valign=top><td nowrap><code>[^a-z]</code><td>The set containing\r
162  * all characters but 'a' through 'z',\r
163  * that is, U+0000 through 'a'-1 and 'z'+1 through U+10FFFF\r
164  * <tr valign=top><td nowrap><code>[[<em>pat1</em>][<em>pat2</em>]]</code>\r
165  * <td>The union of sets specified by <em>pat1</em> and <em>pat2</em>\r
166  * <tr valign=top><td nowrap><code>[[<em>pat1</em>]&[<em>pat2</em>]]</code>\r
167  * <td>The intersection of sets specified by <em>pat1</em> and <em>pat2</em>\r
168  * <tr valign=top><td nowrap><code>[[<em>pat1</em>]-[<em>pat2</em>]]</code>\r
169  * <td>The asymmetric difference of sets specified by <em>pat1</em> and\r
170  * <em>pat2</em>\r
171  * <tr valign=top><td nowrap><code>[:Lu:] or \p{Lu}</code>\r
172  * <td>The set of characters having the specified\r
173  * Unicode property; in\r
174  * this case, Unicode uppercase letters\r
175  * <tr valign=top><td nowrap><code>[:^Lu:] or \P{Lu}</code>\r
176  * <td>The set of characters <em>not</em> having the given\r
177  * Unicode property\r
178  * </table>\r
179  *\r
180  * <p><b>Warning</b>: you cannot add an empty string ("") to a UnicodeSet.</p>\r
181  *\r
182  * <p><b>Formal syntax</b></p>\r
183  *\r
184  * <blockquote>\r
185  *   <table>\r
186  *     <tr align="top">\r
187  *       <td nowrap valign="top" align="right"><code>pattern :=&nbsp; </code></td>\r
188  *       <td valign="top"><code>('[' '^'? item* ']') |\r
189  *       property</code></td>\r
190  *     </tr>\r
191  *     <tr align="top">\r
192  *       <td nowrap valign="top" align="right"><code>item :=&nbsp; </code></td>\r
193  *       <td valign="top"><code>char | (char '-' char) | pattern-expr<br>\r
194  *       </code></td>\r
195  *     </tr>\r
196  *     <tr align="top">\r
197  *       <td nowrap valign="top" align="right"><code>pattern-expr :=&nbsp; </code></td>\r
198  *       <td valign="top"><code>pattern | pattern-expr pattern |\r
199  *       pattern-expr op pattern<br>\r
200  *       </code></td>\r
201  *     </tr>\r
202  *     <tr align="top">\r
203  *       <td nowrap valign="top" align="right"><code>op :=&nbsp; </code></td>\r
204  *       <td valign="top"><code>'&amp;' | '-'<br>\r
205  *       </code></td>\r
206  *     </tr>\r
207  *     <tr align="top">\r
208  *       <td nowrap valign="top" align="right"><code>special :=&nbsp; </code></td>\r
209  *       <td valign="top"><code>'[' | ']' | '-'<br>\r
210  *       </code></td>\r
211  *     </tr>\r
212  *     <tr align="top">\r
213  *       <td nowrap valign="top" align="right"><code>char :=&nbsp; </code></td>\r
214  *       <td valign="top"><em>any character that is not</em><code> special<br>\r
215  *       | ('\\' </code><em>any character</em><code>)<br>\r
216  *       | ('&#92;u' hex hex hex hex)<br>\r
217  *       </code></td>\r
218  *     </tr>\r
219  *     <tr align="top">\r
220  *       <td nowrap valign="top" align="right"><code>hex :=&nbsp; </code></td>\r
221  *       <td valign="top"><em>any character for which\r
222  *       </em><code>Character.digit(c, 16)</code><em>\r
223  *       returns a non-negative result</em></td>\r
224  *     </tr>\r
225  *     <tr>\r
226  *       <td nowrap valign="top" align="right"><code>property :=&nbsp; </code></td>\r
227  *       <td valign="top"><em>a Unicode property set pattern</td>\r
228  *     </tr>\r
229  *   </table>\r
230  *   <br>\r
231  *   <table border="1">\r
232  *     <tr>\r
233  *       <td>Legend: <table>\r
234  *         <tr>\r
235  *           <td nowrap valign="top"><code>a := b</code></td>\r
236  *           <td width="20" valign="top">&nbsp; </td>\r
237  *           <td valign="top"><code>a</code> may be replaced by <code>b</code> </td>\r
238  *         </tr>\r
239  *         <tr>\r
240  *           <td nowrap valign="top"><code>a?</code></td>\r
241  *           <td valign="top"></td>\r
242  *           <td valign="top">zero or one instance of <code>a</code><br>\r
243  *           </td>\r
244  *         </tr>\r
245  *         <tr>\r
246  *           <td nowrap valign="top"><code>a*</code></td>\r
247  *           <td valign="top"></td>\r
248  *           <td valign="top">one or more instances of <code>a</code><br>\r
249  *           </td>\r
250  *         </tr>\r
251  *         <tr>\r
252  *           <td nowrap valign="top"><code>a | b</code></td>\r
253  *           <td valign="top"></td>\r
254  *           <td valign="top">either <code>a</code> or <code>b</code><br>\r
255  *           </td>\r
256  *         </tr>\r
257  *         <tr>\r
258  *           <td nowrap valign="top"><code>'a'</code></td>\r
259  *           <td valign="top"></td>\r
260  *           <td valign="top">the literal string between the quotes </td>\r
261  *         </tr>\r
262  *       </table>\r
263  *       </td>\r
264  *     </tr>\r
265  *   </table>\r
266  * </blockquote>\r
267  * <p>To iterate over contents of UnicodeSet, use UnicodeSetIterator class.\r
268  *\r
269  * @author Alan Liu\r
270  * @stable ICU 2.0\r
271  * @see UnicodeSetIterator\r
272  */\r
273 public class UnicodeSet extends UnicodeFilter implements Iterable<String>, Comparable<UnicodeSet>, Freezable<UnicodeSet> {\r
274 \r
275     private static final int LOW = 0x000000; // LOW <= all valid values. ZERO for codepoints\r
276     private static final int HIGH = 0x110000; // HIGH > all valid values. 10000 for code units.\r
277     // 110000 for codepoints\r
278 \r
279     /**\r
280      * Minimum value that can be stored in a UnicodeSet.\r
281      * @stable ICU 2.0\r
282      */\r
283     public static final int MIN_VALUE = LOW;\r
284 \r
285     /**\r
286      * Maximum value that can be stored in a UnicodeSet.\r
287      * @stable ICU 2.0\r
288      */\r
289     public static final int MAX_VALUE = HIGH - 1;\r
290 \r
291     private int len;      // length used; list may be longer to minimize reallocs\r
292     private int[] list;   // MUST be terminated with HIGH\r
293     private int[] rangeList; // internal buffer\r
294     private int[] buffer; // internal buffer\r
295 \r
296     // NOTE: normally the field should be of type SortedSet; but that is missing a public clone!!\r
297     // is not private so that UnicodeSetIterator can get access\r
298     TreeSet<String> strings = new TreeSet<String>();\r
299 \r
300     /**\r
301      * The pattern representation of this set.  This may not be the\r
302      * most economical pattern.  It is the pattern supplied to\r
303      * applyPattern(), with variables substituted and whitespace\r
304      * removed.  For sets constructed without applyPattern(), or\r
305      * modified using the non-pattern API, this string will be null,\r
306      * indicating that toPattern() must generate a pattern\r
307      * representation from the inversion list.\r
308      */\r
309     private String pat = null;\r
310 \r
311     private static final int START_EXTRA = 16;         // initial storage. Must be >= 0\r
312     private static final int GROW_EXTRA = START_EXTRA; // extra amount for growth. Must be >= 0\r
313 \r
314     // Special property set IDs\r
315     private static final String ANY_ID   = "ANY";   // [\u0000-\U0010FFFF]\r
316     private static final String ASCII_ID = "ASCII"; // [\u0000-\u007F]\r
317     private static final String ASSIGNED = "Assigned"; // [:^Cn:]\r
318 \r
319     /**\r
320      * A set of all characters _except_ the second through last characters of\r
321      * certain ranges.  These ranges are ranges of characters whose\r
322      * properties are all exactly alike, e.g. CJK Ideographs from\r
323      * U+4E00 to U+9FA5.\r
324      */\r
325     private static UnicodeSet INCLUSIONS[] = null;\r
326 \r
327     private BMPSet bmpSet; // The set is frozen iff either bmpSet or stringSpan is not null.\r
328     private UnicodeSetStringSpan stringSpan;\r
329     //----------------------------------------------------------------\r
330     // Public API\r
331     //----------------------------------------------------------------\r
332 \r
333     /**\r
334      * Constructs an empty set.\r
335      * @stable ICU 2.0\r
336      */\r
337     public UnicodeSet() {\r
338         list = new int[1 + START_EXTRA];\r
339         list[len++] = HIGH;\r
340     }\r
341 \r
342     /**\r
343      * Constructs a copy of an existing set.\r
344      * @stable ICU 2.0\r
345      */\r
346     public UnicodeSet(UnicodeSet other) {\r
347         set(other);\r
348     }\r
349 \r
350     /**\r
351      * Constructs a set containing the given range. If <code>end >\r
352      * start</code> then an empty set is created.\r
353      *\r
354      * @param start first character, inclusive, of range\r
355      * @param end last character, inclusive, of range\r
356      * @stable ICU 2.0\r
357      */\r
358     public UnicodeSet(int start, int end) {\r
359         this();\r
360         complement(start, end);\r
361     }\r
362 \r
363     /**\r
364      * Quickly constructs a set from a set of ranges <s0, e0, s1, e1, s2, e2, ..., sn, en>.\r
365      * There must be an even number of integers, and they must be all greater than zero,\r
366      * all less than or equal to Character.MAX_CODE_POINT.\r
367      * In each pair (..., si, ei, ...) it must be true that si <= ei\r
368      * Between adjacent pairs (...ei, sj...), it must be true that ei+1 < sj\r
369      * @param pairs pairs of character representing ranges\r
370      * @draft ICU 4.4\r
371      * @provisional This API might change or be removed in a future release.\r
372      */\r
373     public UnicodeSet(int... pairs) {\r
374         if ((pairs.length & 1) != 0) {\r
375             throw new IllegalArgumentException("Must have even number of integers");\r
376         }\r
377         list = new int[pairs.length + 1]; // don't allocate extra space, because it is likely that this is a fixed set.\r
378         len = list.length;\r
379         int last = -1; // used to ensure that the results are monotonically increasing.\r
380         int i = 0;\r
381         while (i < pairs.length) {\r
382             // start of pair\r
383             int start = pairs[i];\r
384             if (last >= start) {\r
385                 throw new IllegalArgumentException("Must be monotonically increasing.");\r
386             }\r
387             list[i++] = last = start;\r
388             // end of pair\r
389             int end = pairs[i] + 1;\r
390             if (last >= end) {\r
391                 throw new IllegalArgumentException("Must be monotonically increasing.");\r
392             }\r
393             list[i++] = last = end;\r
394         }\r
395         list[i] = HIGH; // terminate\r
396     }\r
397 \r
398     /**\r
399      * Constructs a set from the given pattern.  See the class description\r
400      * for the syntax of the pattern language.  Whitespace is ignored.\r
401      * @param pattern a string specifying what characters are in the set\r
402      * @exception java.lang.IllegalArgumentException if the pattern contains\r
403      * a syntax error.\r
404      * @stable ICU 2.0\r
405      */\r
406     public UnicodeSet(String pattern) {\r
407         this();\r
408         applyPattern(pattern, null, null, IGNORE_SPACE);\r
409     }\r
410 \r
411     /**\r
412      * Constructs a set from the given pattern.  See the class description\r
413      * for the syntax of the pattern language.\r
414      * @param pattern a string specifying what characters are in the set\r
415      * @param ignoreWhitespace if true, ignore characters for which\r
416      * UCharacterProperty.isRuleWhiteSpace() returns true\r
417      * @exception java.lang.IllegalArgumentException if the pattern contains\r
418      * a syntax error.\r
419      * @stable ICU 2.0\r
420      */\r
421     public UnicodeSet(String pattern, boolean ignoreWhitespace) {\r
422         this();\r
423         applyPattern(pattern, null, null, ignoreWhitespace ? IGNORE_SPACE : 0);\r
424     }\r
425 \r
426     /**\r
427      * Constructs a set from the given pattern.  See the class description\r
428      * for the syntax of the pattern language.\r
429      * @param pattern a string specifying what characters are in the set\r
430      * @param options a bitmask indicating which options to apply.\r
431      * Valid options are IGNORE_SPACE and CASE.\r
432      * @exception java.lang.IllegalArgumentException if the pattern contains\r
433      * a syntax error.\r
434      * @stable ICU 3.8\r
435      */\r
436     public UnicodeSet(String pattern, int options) {\r
437         this();\r
438         applyPattern(pattern, null, null, options);\r
439     }\r
440 \r
441     /**\r
442      * Constructs a set from the given pattern.  See the class description\r
443      * for the syntax of the pattern language.\r
444      * @param pattern a string specifying what characters are in the set\r
445      * @param pos on input, the position in pattern at which to start parsing.\r
446      * On output, the position after the last character parsed.\r
447      * @param symbols a symbol table mapping variables to char[] arrays\r
448      * and chars to UnicodeSets\r
449      * @exception java.lang.IllegalArgumentException if the pattern\r
450      * contains a syntax error.\r
451      * @stable ICU 2.0\r
452      */\r
453     public UnicodeSet(String pattern, ParsePosition pos, SymbolTable symbols) {\r
454         this();\r
455         applyPattern(pattern, pos, symbols, IGNORE_SPACE);\r
456     }\r
457 \r
458     /**\r
459      * Constructs a set from the given pattern.  See the class description\r
460      * for the syntax of the pattern language.\r
461      * @param pattern a string specifying what characters are in the set\r
462      * @param pos on input, the position in pattern at which to start parsing.\r
463      * On output, the position after the last character parsed.\r
464      * @param symbols a symbol table mapping variables to char[] arrays\r
465      * and chars to UnicodeSets\r
466      * @param options a bitmask indicating which options to apply.\r
467      * Valid options are IGNORE_SPACE and CASE.\r
468      * @exception java.lang.IllegalArgumentException if the pattern\r
469      * contains a syntax error.\r
470      * @stable ICU 3.2\r
471      */\r
472     public UnicodeSet(String pattern, ParsePosition pos, SymbolTable symbols, int options) {\r
473         this();\r
474         applyPattern(pattern, pos, symbols, options);\r
475     }\r
476 \r
477 \r
478     /**\r
479      * Return a new set that is equivalent to this one.\r
480      * @stable ICU 2.0\r
481      */\r
482     public Object clone() {\r
483         UnicodeSet result = new UnicodeSet(this);\r
484         result.bmpSet = this.bmpSet;\r
485         result.stringSpan = this.stringSpan;\r
486         return result;\r
487     }\r
488 \r
489     /**\r
490      * Make this object represent the range <code>start - end</code>.\r
491      * If <code>end > start</code> then this object is set to an\r
492      * an empty range.\r
493      *\r
494      * @param start first character in the set, inclusive\r
495      * @param end last character in the set, inclusive\r
496      * @stable ICU 2.0\r
497      */\r
498     public UnicodeSet set(int start, int end) {\r
499         checkFrozen();\r
500         clear();\r
501         complement(start, end);\r
502         return this;\r
503     }\r
504 \r
505     /**\r
506      * Make this object represent the same set as <code>other</code>.\r
507      * @param other a <code>UnicodeSet</code> whose value will be\r
508      * copied to this object\r
509      * @stable ICU 2.0\r
510      */\r
511     public UnicodeSet set(UnicodeSet other) {\r
512         checkFrozen();\r
513         list = other.list.clone();\r
514         len = other.len;\r
515         pat = other.pat;\r
516         strings = new TreeSet<String>(other.strings);\r
517         return this;\r
518     }\r
519 \r
520     /**\r
521      * Modifies this set to represent the set specified by the given pattern.\r
522      * See the class description for the syntax of the pattern language.\r
523      * Whitespace is ignored.\r
524      * @param pattern a string specifying what characters are in the set\r
525      * @exception java.lang.IllegalArgumentException if the pattern\r
526      * contains a syntax error.\r
527      * @stable ICU 2.0\r
528      */\r
529     public final UnicodeSet applyPattern(String pattern) {\r
530         checkFrozen();\r
531         return applyPattern(pattern, null, null, IGNORE_SPACE);\r
532     }\r
533 \r
534     /**\r
535      * Modifies this set to represent the set specified by the given pattern,\r
536      * optionally ignoring whitespace.\r
537      * See the class description for the syntax of the pattern language.\r
538      * @param pattern a string specifying what characters are in the set\r
539      * @param ignoreWhitespace if true then characters for which\r
540      * UCharacterProperty.isRuleWhiteSpace() returns true are ignored\r
541      * @exception java.lang.IllegalArgumentException if the pattern\r
542      * contains a syntax error.\r
543      * @stable ICU 2.0\r
544      */\r
545     public UnicodeSet applyPattern(String pattern, boolean ignoreWhitespace) {\r
546         checkFrozen();\r
547         return applyPattern(pattern, null, null, ignoreWhitespace ? IGNORE_SPACE : 0);\r
548     }\r
549 \r
550     /**\r
551      * Modifies this set to represent the set specified by the given pattern,\r
552      * optionally ignoring whitespace.\r
553      * See the class description for the syntax of the pattern language.\r
554      * @param pattern a string specifying what characters are in the set\r
555      * @param options a bitmask indicating which options to apply.\r
556      * Valid options are IGNORE_SPACE and CASE.\r
557      * @exception java.lang.IllegalArgumentException if the pattern\r
558      * contains a syntax error.\r
559      * @stable ICU 3.8\r
560      */\r
561     public UnicodeSet applyPattern(String pattern, int options) {\r
562         checkFrozen();\r
563         return applyPattern(pattern, null, null, options);\r
564     }\r
565 \r
566     /**\r
567      * Return true if the given position, in the given pattern, appears\r
568      * to be the start of a UnicodeSet pattern.\r
569      * @stable ICU 2.0\r
570      */\r
571     public static boolean resemblesPattern(String pattern, int pos) {\r
572         return ((pos+1) < pattern.length() &&\r
573                 pattern.charAt(pos) == '[') ||\r
574                 resemblesPropertyPattern(pattern, pos);\r
575     }\r
576 \r
577     /**\r
578      * Append the <code>toPattern()</code> representation of a\r
579      * string to the given <code>StringBuffer</code>.\r
580      */\r
581     private static void _appendToPat(StringBuffer buf, String s, boolean escapeUnprintable) {\r
582         for (int i = 0; i < s.length(); i += UTF16.getCharCount(i)) {\r
583             _appendToPat(buf, UTF16.charAt(s, i), escapeUnprintable);\r
584         }\r
585     }\r
586 \r
587     /**\r
588      * Append the <code>toPattern()</code> representation of a\r
589      * character to the given <code>StringBuffer</code>.\r
590      */\r
591     private static void _appendToPat(StringBuffer buf, int c, boolean escapeUnprintable) {\r
592         // "Utility.isUnprintable(c)" seems redundant since the the call\r
593         //      "Utility.escapeUnprintable(buf, c)" does it again inside the if statement\r
594         if (escapeUnprintable && Utility.isUnprintable(c)) {\r
595             // Use hex escape notation (<backslash>uxxxx or <backslash>Uxxxxxxxx) for anything\r
596             // unprintable\r
597             if (Utility.escapeUnprintable(buf, c)) {\r
598                 return;\r
599             }\r
600         }\r
601         // Okay to let ':' pass through\r
602         switch (c) {\r
603         case '[': // SET_OPEN:\r
604         case ']': // SET_CLOSE:\r
605         case '-': // HYPHEN:\r
606         case '^': // COMPLEMENT:\r
607         case '&': // INTERSECTION:\r
608         case '\\': //BACKSLASH:\r
609         case '{':\r
610         case '}':\r
611         case '$':\r
612         case ':':\r
613             buf.append('\\');\r
614             break;\r
615         default:\r
616             // Escape whitespace\r
617             if (UCharacterProperty.isRuleWhiteSpace(c)) {\r
618                 buf.append('\\');\r
619             }\r
620             break;\r
621         }\r
622         UTF16.append(buf, c);\r
623     }\r
624 \r
625     /**\r
626      * Returns a string representation of this set.  If the result of\r
627      * calling this function is passed to a UnicodeSet constructor, it\r
628      * will produce another set that is equal to this one.\r
629      * @stable ICU 2.0\r
630      */\r
631     public String toPattern(boolean escapeUnprintable) {\r
632         StringBuffer result = new StringBuffer();\r
633         return _toPattern(result, escapeUnprintable).toString();\r
634     }\r
635 \r
636     /**\r
637      * Append a string representation of this set to result.  This will be\r
638      * a cleaned version of the string passed to applyPattern(), if there\r
639      * is one.  Otherwise it will be generated.\r
640      */\r
641     private StringBuffer _toPattern(StringBuffer result,\r
642             boolean escapeUnprintable) {\r
643         if (pat != null) {\r
644             int i;\r
645             int backslashCount = 0;\r
646             for (i=0; i<pat.length(); ) {\r
647                 int c = UTF16.charAt(pat, i);\r
648                 i += UTF16.getCharCount(c);\r
649                 if (escapeUnprintable && Utility.isUnprintable(c)) {\r
650                     // If the unprintable character is preceded by an odd\r
651                     // number of backslashes, then it has been escaped.\r
652                     // Before unescaping it, we delete the final\r
653                     // backslash.\r
654                     if ((backslashCount % 2) == 1) {\r
655                         result.setLength(result.length() - 1);\r
656                     }\r
657                     Utility.escapeUnprintable(result, c);\r
658                     backslashCount = 0;\r
659                 } else {\r
660                     UTF16.append(result, c);\r
661                     if (c == '\\') {\r
662                         ++backslashCount;\r
663                     } else {\r
664                         backslashCount = 0;\r
665                     }\r
666                 }\r
667             }\r
668             return result;\r
669         }\r
670 \r
671         return _generatePattern(result, escapeUnprintable, true);\r
672     }\r
673 \r
674     /**\r
675      * Generate and append a string representation of this set to result.\r
676      * This does not use this.pat, the cleaned up copy of the string\r
677      * passed to applyPattern().\r
678      * @param result the buffer into which to generate the pattern\r
679      * @param escapeUnprintable escape unprintable characters if true\r
680      * @stable ICU 2.0\r
681      */\r
682     public StringBuffer _generatePattern(StringBuffer result, boolean escapeUnprintable) {\r
683         return _generatePattern(result, escapeUnprintable, true);\r
684     }\r
685 \r
686     /**\r
687      * Generate and append a string representation of this set to result.\r
688      * This does not use this.pat, the cleaned up copy of the string\r
689      * passed to applyPattern().\r
690      * @param includeStrings if false, doesn't include the strings.\r
691      * @stable ICU 3.8\r
692      */\r
693     public StringBuffer _generatePattern(StringBuffer result,\r
694             boolean escapeUnprintable, boolean includeStrings) {\r
695         result.append('[');\r
696 \r
697         //      // Check against the predefined categories.  We implicitly build\r
698         //      // up ALL category sets the first time toPattern() is called.\r
699         //      for (int cat=0; cat<CATEGORY_COUNT; ++cat) {\r
700         //          if (this.equals(getCategorySet(cat))) {\r
701         //              result.append(':');\r
702         //              result.append(CATEGORY_NAMES.substring(cat*2, cat*2+2));\r
703         //              return result.append(":]");\r
704         //          }\r
705         //      }\r
706 \r
707         int count = getRangeCount();\r
708 \r
709         // If the set contains at least 2 intervals and includes both\r
710         // MIN_VALUE and MAX_VALUE, then the inverse representation will\r
711         // be more economical.\r
712         if (count > 1 &&\r
713                 getRangeStart(0) == MIN_VALUE &&\r
714                 getRangeEnd(count-1) == MAX_VALUE) {\r
715 \r
716             // Emit the inverse\r
717             result.append('^');\r
718 \r
719             for (int i = 1; i < count; ++i) {\r
720                 int start = getRangeEnd(i-1)+1;\r
721                 int end = getRangeStart(i)-1;\r
722                 _appendToPat(result, start, escapeUnprintable);\r
723                 if (start != end) {\r
724                     if ((start+1) != end) {\r
725                         result.append('-');\r
726                     }\r
727                     _appendToPat(result, end, escapeUnprintable);\r
728                 }\r
729             }\r
730         }\r
731 \r
732         // Default; emit the ranges as pairs\r
733         else {\r
734             for (int i = 0; i < count; ++i) {\r
735                 int start = getRangeStart(i);\r
736                 int end = getRangeEnd(i);\r
737                 _appendToPat(result, start, escapeUnprintable);\r
738                 if (start != end) {\r
739                     if ((start+1) != end) {\r
740                         result.append('-');\r
741                     }\r
742                     _appendToPat(result, end, escapeUnprintable);\r
743                 }\r
744             }\r
745         }\r
746 \r
747         if (includeStrings && strings.size() > 0) {\r
748             for (String s : strings) {\r
749                 result.append('{');\r
750                 _appendToPat(result, s, escapeUnprintable);\r
751                 result.append('}');\r
752             }\r
753         }\r
754         return result.append(']');\r
755     }\r
756 \r
757     /**\r
758      * Returns the number of elements in this set (its cardinality)\r
759      * Note than the elements of a set may include both individual\r
760      * codepoints and strings.\r
761      *\r
762      * @return the number of elements in this set (its cardinality).\r
763      * @stable ICU 2.0\r
764      */\r
765     public int size() {\r
766         int n = 0;\r
767         int count = getRangeCount();\r
768         for (int i = 0; i < count; ++i) {\r
769             n += getRangeEnd(i) - getRangeStart(i) + 1;\r
770         }\r
771         return n + strings.size();\r
772     }\r
773 \r
774     /**\r
775      * Returns <tt>true</tt> if this set contains no elements.\r
776      *\r
777      * @return <tt>true</tt> if this set contains no elements.\r
778      * @stable ICU 2.0\r
779      */\r
780     public boolean isEmpty() {\r
781         return len == 1 && strings.size() == 0;\r
782     }\r
783 \r
784     /**\r
785      * Implementation of UnicodeMatcher API.  Returns <tt>true</tt> if\r
786      * this set contains any character whose low byte is the given\r
787      * value.  This is used by <tt>RuleBasedTransliterator</tt> for\r
788      * indexing.\r
789      * @stable ICU 2.0\r
790      */\r
791     public boolean matchesIndexValue(int v) {\r
792         /* The index value v, in the range [0,255], is contained in this set if\r
793          * it is contained in any pair of this set.  Pairs either have the high\r
794          * bytes equal, or unequal.  If the high bytes are equal, then we have\r
795          * aaxx..aayy, where aa is the high byte.  Then v is contained if xx <=\r
796          * v <= yy.  If the high bytes are unequal we have aaxx..bbyy, bb>aa.\r
797          * Then v is contained if xx <= v || v <= yy.  (This is identical to the\r
798          * time zone month containment logic.)\r
799          */\r
800         for (int i=0; i<getRangeCount(); ++i) {\r
801             int low = getRangeStart(i);\r
802             int high = getRangeEnd(i);\r
803             if ((low & ~0xFF) == (high & ~0xFF)) {\r
804                 if ((low & 0xFF) <= v && v <= (high & 0xFF)) {\r
805                     return true;\r
806                 }\r
807             } else if ((low & 0xFF) <= v || v <= (high & 0xFF)) {\r
808                 return true;\r
809             }\r
810         }\r
811         if (strings.size() != 0) {\r
812             for (String s : strings) {\r
813                 //if (s.length() == 0) {\r
814                 //    // Empty strings match everything\r
815                 //    return true;\r
816                 //}\r
817                 // assert(s.length() != 0); // We enforce this elsewhere\r
818                 int c = UTF16.charAt(s, 0);\r
819                 if ((c & 0xFF) == v) {\r
820                     return true;\r
821                 }\r
822             }\r
823         }\r
824         return false;\r
825     }\r
826 \r
827     /**\r
828      * Implementation of UnicodeMatcher.matches().  Always matches the\r
829      * longest possible multichar string.\r
830      * @stable ICU 2.0\r
831      */\r
832     public int matches(Replaceable text,\r
833             int[] offset,\r
834             int limit,\r
835             boolean incremental) {\r
836 \r
837         if (offset[0] == limit) {\r
838             // Strings, if any, have length != 0, so we don't worry\r
839             // about them here.  If we ever allow zero-length strings\r
840             // we much check for them here.\r
841             if (contains(UnicodeMatcher.ETHER)) {\r
842                 return incremental ? U_PARTIAL_MATCH : U_MATCH; \r
843             } else {\r
844                 return U_MISMATCH;\r
845             }\r
846         } else {\r
847             if (strings.size() != 0) { // try strings first\r
848 \r
849                 // might separate forward and backward loops later\r
850                 // for now they are combined\r
851 \r
852                 // TODO Improve efficiency of this, at least in the forward\r
853                 // direction, if not in both.  In the forward direction we\r
854                 // can assume the strings are sorted.\r
855 \r
856                 boolean forward = offset[0] < limit;\r
857 \r
858                 // firstChar is the leftmost char to match in the\r
859                 // forward direction or the rightmost char to match in\r
860                 // the reverse direction.\r
861                 char firstChar = text.charAt(offset[0]);\r
862 \r
863                 // If there are multiple strings that can match we\r
864                 // return the longest match.\r
865                 int highWaterLength = 0;\r
866 \r
867                 for (String trial : strings) {\r
868                     //if (trial.length() == 0) {\r
869                     //    return U_MATCH; // null-string always matches\r
870                     //}\r
871                     // assert(trial.length() != 0); // We ensure this elsewhere\r
872 \r
873                     char c = trial.charAt(forward ? 0 : trial.length() - 1);\r
874 \r
875                     // Strings are sorted, so we can optimize in the\r
876                     // forward direction.\r
877                     if (forward && c > firstChar) break;\r
878                     if (c != firstChar) continue; \r
879 \r
880                     int length = matchRest(text, offset[0], limit, trial);\r
881 \r
882                     if (incremental) {\r
883                         int maxLen = forward ? limit-offset[0] : offset[0]-limit;\r
884                         if (length == maxLen) {\r
885                             // We have successfully matched but only up to limit.\r
886                             return U_PARTIAL_MATCH;\r
887                         }\r
888                     }\r
889 \r
890                     if (length == trial.length()) {\r
891                         // We have successfully matched the whole string.\r
892                         if (length > highWaterLength) {\r
893                             highWaterLength = length;\r
894                         }\r
895                         // In the forward direction we know strings\r
896                         // are sorted so we can bail early.\r
897                         if (forward && length < highWaterLength) {\r
898                             break;\r
899                         }\r
900                         continue;\r
901                     }\r
902                 }\r
903 \r
904                 // We've checked all strings without a partial match.\r
905                 // If we have full matches, return the longest one.\r
906                 if (highWaterLength != 0) {\r
907                     offset[0] += forward ? highWaterLength : -highWaterLength;\r
908                     return U_MATCH;\r
909                 }\r
910             }\r
911             return super.matches(text, offset, limit, incremental);\r
912         }\r
913     }\r
914 \r
915     /**\r
916      * Returns the longest match for s in text at the given position.\r
917      * If limit > start then match forward from start+1 to limit\r
918      * matching all characters except s.charAt(0).  If limit < start,\r
919      * go backward starting from start-1 matching all characters\r
920      * except s.charAt(s.length()-1).  This method assumes that the\r
921      * first character, text.charAt(start), matches s, so it does not\r
922      * check it.\r
923      * @param text the text to match\r
924      * @param start the first character to match.  In the forward\r
925      * direction, text.charAt(start) is matched against s.charAt(0).\r
926      * In the reverse direction, it is matched against\r
927      * s.charAt(s.length()-1).\r
928      * @param limit the limit offset for matching, either last+1 in\r
929      * the forward direction, or last-1 in the reverse direction,\r
930      * where last is the index of the last character to match.\r
931      * @return If part of s matches up to the limit, return |limit -\r
932      * start|.  If all of s matches before reaching the limit, return\r
933      * s.length().  If there is a mismatch between s and text, return\r
934      * 0\r
935      */\r
936     private static int matchRest (Replaceable text, int start, int limit, String s) {\r
937         int maxLen;\r
938         int slen = s.length();\r
939         if (start < limit) {\r
940             maxLen = limit - start;\r
941             if (maxLen > slen) maxLen = slen;\r
942             for (int i = 1; i < maxLen; ++i) {\r
943                 if (text.charAt(start + i) != s.charAt(i)) return 0;\r
944             }\r
945         } else {\r
946             maxLen = start - limit;\r
947             if (maxLen > slen) maxLen = slen;\r
948             --slen; // <=> slen = s.length() - 1;\r
949             for (int i = 1; i < maxLen; ++i) {\r
950                 if (text.charAt(start - i) != s.charAt(slen - i)) return 0;\r
951             }\r
952         }\r
953         return maxLen;\r
954     }\r
955 \r
956     /**\r
957      * Tests whether the text matches at the offset. If so, returns the end of the longest substring that it matches. If not, returns -1. \r
958      * @internal\r
959      * @deprecated This API is ICU internal only.\r
960      */\r
961     public int matchesAt(CharSequence text, int offset) {\r
962         int lastLen = -1;\r
963         strings:\r
964             if (strings.size() != 0) {\r
965                 char firstChar = text.charAt(offset);\r
966                 String trial = null;\r
967                 // find the first string starting with firstChar\r
968                 Iterator<String> it = strings.iterator();\r
969                 while (it.hasNext()) {\r
970                     trial = it.next();\r
971                     char firstStringChar = trial.charAt(0);\r
972                     if (firstStringChar < firstChar) continue;\r
973                     if (firstStringChar > firstChar) break strings;\r
974                 }\r
975 \r
976                 // now keep checking string until we get the longest one\r
977                 for (;;) {\r
978                     int tempLen = matchesAt(text, offset, trial);\r
979                     if (lastLen > tempLen) break strings;\r
980                     lastLen = tempLen;\r
981                     if (!it.hasNext()) break;\r
982                     trial = it.next();\r
983                 }\r
984             }\r
985 \r
986         if (lastLen < 2) {\r
987             int cp = UTF16.charAt(text, offset);\r
988             if (contains(cp)) lastLen = UTF16.getCharCount(cp);\r
989         }\r
990 \r
991         return offset+lastLen;\r
992     }\r
993 \r
994     /**\r
995      * Does one string contain another, starting at a specific offset?\r
996      * @param text\r
997      * @param offset\r
998      * @param other\r
999      * @return\r
1000      */\r
1001     // Note: This method was moved from CollectionUtilities\r
1002     private static int matchesAt(CharSequence text, int offset, CharSequence other) {\r
1003         int len = other.length();\r
1004         int i = 0;\r
1005         int j = offset;\r
1006         for (; i < len; ++i, ++j) {\r
1007             char pc = other.charAt(i);\r
1008             char tc = text.charAt(j);\r
1009             if (pc != tc) return -1;\r
1010         }\r
1011         return i;\r
1012     }\r
1013 \r
1014     /**\r
1015      * Implementation of UnicodeMatcher API.  Union the set of all\r
1016      * characters that may be matched by this object into the given\r
1017      * set.\r
1018      * @param toUnionTo the set into which to union the source characters\r
1019      * @stable ICU 2.2\r
1020      */\r
1021     public void addMatchSetTo(UnicodeSet toUnionTo) {\r
1022         toUnionTo.addAll(this);\r
1023     }\r
1024 \r
1025     /**\r
1026      * Returns the index of the given character within this set, where\r
1027      * the set is ordered by ascending code point.  If the character\r
1028      * is not in this set, return -1.  The inverse of this method is\r
1029      * <code>charAt()</code>.\r
1030      * @return an index from 0..size()-1, or -1\r
1031      * @stable ICU 2.0\r
1032      */\r
1033     public int indexOf(int c) {\r
1034         if (c < MIN_VALUE || c > MAX_VALUE) {\r
1035             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(c, 6));\r
1036         }\r
1037         int i = 0;\r
1038         int n = 0;\r
1039         for (;;) {\r
1040             int start = list[i++];\r
1041             if (c < start) {\r
1042                 return -1;\r
1043             }\r
1044             int limit = list[i++];\r
1045             if (c < limit) {\r
1046                 return n + c - start;\r
1047             }\r
1048             n += limit - start;\r
1049         }\r
1050     }\r
1051 \r
1052     /**\r
1053      * Returns the character at the given index within this set, where\r
1054      * the set is ordered by ascending code point.  If the index is\r
1055      * out of range, return -1.  The inverse of this method is\r
1056      * <code>indexOf()</code>.\r
1057      * @param index an index from 0..size()-1\r
1058      * @return the character at the given index, or -1.\r
1059      * @stable ICU 2.0\r
1060      */\r
1061     public int charAt(int index) {\r
1062         if (index >= 0) {\r
1063             // len2 is the largest even integer <= len, that is, it is len\r
1064             // for even values and len-1 for odd values.  With odd values\r
1065             // the last entry is UNICODESET_HIGH.\r
1066             int len2 = len & ~1;\r
1067             for (int i=0; i < len2;) {\r
1068                 int start = list[i++];\r
1069                 int count = list[i++] - start;\r
1070                 if (index < count) {\r
1071                     return start + index;\r
1072                 }\r
1073                 index -= count;\r
1074             }\r
1075         }\r
1076         return -1;\r
1077     }\r
1078 \r
1079     /**\r
1080      * Adds the specified range to this set if it is not already\r
1081      * present.  If this set already contains the specified range,\r
1082      * the call leaves this set unchanged.  If <code>end > start</code>\r
1083      * then an empty range is added, leaving the set unchanged.\r
1084      *\r
1085      * @param start first character, inclusive, of range to be added\r
1086      * to this set.\r
1087      * @param end last character, inclusive, of range to be added\r
1088      * to this set.\r
1089      * @stable ICU 2.0\r
1090      */\r
1091     public UnicodeSet add(int start, int end) {\r
1092         checkFrozen();\r
1093         return add_unchecked(start, end);\r
1094     }\r
1095 \r
1096     /**\r
1097      * Adds all characters in range (uses preferred naming convention).\r
1098      * @param start The index of where to start on adding all characters.\r
1099      * @param end The index of where to end on adding all characters.\r
1100      * @return a reference to this object\r
1101      * @draft ICU 4.4\r
1102      * @provisional This API might change or be removed in a future release.\r
1103      */\r
1104     public UnicodeSet addAll(int start, int end) {\r
1105         checkFrozen();\r
1106         return add_unchecked(start, end);\r
1107     }\r
1108 \r
1109     // for internal use, after checkFrozen has been called\r
1110     private UnicodeSet add_unchecked(int start, int end) {\r
1111         if (start < MIN_VALUE || start > MAX_VALUE) {\r
1112             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
1113         }\r
1114         if (end < MIN_VALUE || end > MAX_VALUE) {\r
1115             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
1116         }\r
1117         if (start < end) {\r
1118             add(range(start, end), 2, 0);\r
1119         } else if (start == end) {\r
1120             add(start);\r
1121         }\r
1122         return this;\r
1123     }\r
1124 \r
1125     //    /**\r
1126     //     * Format out the inversion list as a string, for debugging.  Uncomment when\r
1127     //     * needed.\r
1128     //     */\r
1129     //    public final String dump() {\r
1130     //        StringBuffer buf = new StringBuffer("[");\r
1131     //        for (int i=0; i<len; ++i) {\r
1132     //            if (i != 0) buf.append(", ");\r
1133     //            int c = list[i];\r
1134     //            //if (c <= 0x7F && c != '\n' && c != '\r' && c != '\t' && c != ' ') {\r
1135     //            //    buf.append((char) c);\r
1136     //            //} else {\r
1137     //                buf.append("U+").append(Utility.hex(c, (c<0x10000)?4:6));\r
1138     //            //}\r
1139     //        }\r
1140     //        buf.append("]");\r
1141     //        return buf.toString();\r
1142     //    }\r
1143 \r
1144     /**\r
1145      * Adds the specified character to this set if it is not already\r
1146      * present.  If this set already contains the specified character,\r
1147      * the call leaves this set unchanged.\r
1148      * @stable ICU 2.0\r
1149      */\r
1150     public final UnicodeSet add(int c) {\r
1151         checkFrozen();\r
1152         return add_unchecked(c);\r
1153     }\r
1154 \r
1155     // for internal use only, after checkFrozen has been called\r
1156     private final UnicodeSet add_unchecked(int c) {\r
1157         if (c < MIN_VALUE || c > MAX_VALUE) {\r
1158             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(c, 6));\r
1159         }\r
1160 \r
1161         // find smallest i such that c < list[i]\r
1162         // if odd, then it is IN the set\r
1163         // if even, then it is OUT of the set\r
1164         int i = findCodePoint(c);\r
1165 \r
1166         // already in set?\r
1167         if ((i & 1) != 0) return this;\r
1168 \r
1169         // HIGH is 0x110000\r
1170         // assert(list[len-1] == HIGH);\r
1171 \r
1172         // empty = [HIGH]\r
1173         // [start_0, limit_0, start_1, limit_1, HIGH]\r
1174 \r
1175         // [..., start_k-1, limit_k-1, start_k, limit_k, ..., HIGH]\r
1176         //                             ^\r
1177         //                             list[i]\r
1178 \r
1179         // i == 0 means c is before the first range\r
1180         // TODO: Is the "list[i]-1" a typo? Even if you pass MAX_VALUE into\r
1181         //      add_unchecked, the maximum value that "c" will be compared to\r
1182         //      is "MAX_VALUE-1" meaning that "if (c == MAX_VALUE)" will\r
1183         //      never be reached according to this logic.\r
1184         if (c == list[i]-1) {\r
1185             // c is before start of next range\r
1186             list[i] = c;\r
1187             // if we touched the HIGH mark, then add a new one\r
1188             if (c == MAX_VALUE) { \r
1189                 ensureCapacity(len+1);\r
1190                 list[len++] = HIGH;\r
1191             }\r
1192             if (i > 0 && c == list[i-1]) {\r
1193                 // collapse adjacent ranges\r
1194 \r
1195                 // [..., start_k-1, c, c, limit_k, ..., HIGH]\r
1196                 //                     ^\r
1197                 //                     list[i]\r
1198                 System.arraycopy(list, i+1, list, i-1, len-i-1);\r
1199                 len -= 2;\r
1200             }\r
1201         }\r
1202 \r
1203         else if (i > 0 && c == list[i-1]) {\r
1204             // c is after end of prior range\r
1205             list[i-1]++;\r
1206             // no need to chcek for collapse here\r
1207         }\r
1208 \r
1209         else {\r
1210             // At this point we know the new char is not adjacent to\r
1211             // any existing ranges, and it is not 10FFFF.\r
1212 \r
1213 \r
1214             // [..., start_k-1, limit_k-1, start_k, limit_k, ..., HIGH]\r
1215             //                             ^\r
1216             //                             list[i]\r
1217 \r
1218             // [..., start_k-1, limit_k-1, c, c+1, start_k, limit_k, ..., HIGH]\r
1219             //                             ^\r
1220             //                             list[i]\r
1221 \r
1222             // Don't use ensureCapacity() to save on copying.\r
1223             // NOTE: This has no measurable impact on performance,\r
1224             // but it might help in some usage patterns.\r
1225             if (len+2 > list.length) {\r
1226                 int[] temp = new int[len + 2 + GROW_EXTRA];\r
1227                 if (i != 0) System.arraycopy(list, 0, temp, 0, i);\r
1228                 System.arraycopy(list, i, temp, i+2, len-i);\r
1229                 list = temp;\r
1230             } else {\r
1231                 System.arraycopy(list, i, list, i+2, len-i);\r
1232             }\r
1233 \r
1234             list[i] = c;\r
1235             list[i+1] = c+1;\r
1236             len += 2;\r
1237         }\r
1238 \r
1239         pat = null;\r
1240         return this;\r
1241     }\r
1242 \r
1243     /**\r
1244      * Adds the specified multicharacter to this set if it is not already\r
1245      * present.  If this set already contains the multicharacter,\r
1246      * the call leaves this set unchanged.\r
1247      * Thus "ch" => {"ch"}\r
1248      * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>\r
1249      * @param s the source string\r
1250      * @return this object, for chaining\r
1251      * @stable ICU 2.0\r
1252      */\r
1253     public final UnicodeSet add(String s) {\r
1254         checkFrozen();\r
1255         int cp = getSingleCP(s);\r
1256         if (cp < 0) {\r
1257             strings.add(s);\r
1258             pat = null;\r
1259         } else {\r
1260             add_unchecked(cp, cp);\r
1261         }\r
1262         return this;\r
1263     }\r
1264 \r
1265     /**\r
1266      * @return a code point IF the string consists of a single one.\r
1267      * otherwise returns -1.\r
1268      * @param string to test\r
1269      */\r
1270     private static int getSingleCP(String s) {\r
1271         if (s.length() < 1) {\r
1272             throw new IllegalArgumentException("Can't use zero-length strings in UnicodeSet");\r
1273         }\r
1274         if (s.length() > 2) return -1;\r
1275         if (s.length() == 1) return s.charAt(0);\r
1276 \r
1277         // at this point, len = 2\r
1278         int cp = UTF16.charAt(s, 0); \r
1279         if (cp > 0xFFFF) { // is surrogate pair\r
1280             return cp;\r
1281         }\r
1282         return -1;\r
1283     }\r
1284 \r
1285     /**\r
1286      * Adds each of the characters in this string to the set. Thus "ch" => {"c", "h"}\r
1287      * If this set already any particular character, it has no effect on that character.\r
1288      * @param s the source string\r
1289      * @return this object, for chaining\r
1290      * @stable ICU 2.0\r
1291      */\r
1292     public final UnicodeSet addAll(String s) {\r
1293         checkFrozen();\r
1294         int cp;\r
1295         for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {\r
1296             cp = UTF16.charAt(s, i);\r
1297             add_unchecked(cp, cp);\r
1298         }\r
1299         return this;\r
1300     }\r
1301 \r
1302     /**\r
1303      * Retains EACH of the characters in this string. Note: "ch" == {"c", "h"}\r
1304      * If this set already any particular character, it has no effect on that character.\r
1305      * @param s the source string\r
1306      * @return this object, for chaining\r
1307      * @stable ICU 2.0\r
1308      */\r
1309     public final UnicodeSet retainAll(String s) {\r
1310         return retainAll(fromAll(s));\r
1311     }\r
1312 \r
1313     /**\r
1314      * Complement EACH of the characters in this string. Note: "ch" == {"c", "h"}\r
1315      * If this set already any particular character, it has no effect on that character.\r
1316      * @param s the source string\r
1317      * @return this object, for chaining\r
1318      * @stable ICU 2.0\r
1319      */\r
1320     public final UnicodeSet complementAll(String s) {\r
1321         return complementAll(fromAll(s));\r
1322     }\r
1323 \r
1324     /**\r
1325      * Remove EACH of the characters in this string. Note: "ch" == {"c", "h"}\r
1326      * If this set already any particular character, it has no effect on that character.\r
1327      * @param s the source string\r
1328      * @return this object, for chaining\r
1329      * @stable ICU 2.0\r
1330      */\r
1331     public final UnicodeSet removeAll(String s) {\r
1332         return removeAll(fromAll(s));\r
1333     }\r
1334 \r
1335     /**\r
1336      * Remove all strings from this UnicodeSet\r
1337      * @return this object, for chaining\r
1338      * @stable ICU 4.2\r
1339      */\r
1340     public final UnicodeSet removeAllStrings() {\r
1341         checkFrozen();\r
1342         if (strings.size() != 0) {\r
1343             strings.clear();\r
1344             pat = null;\r
1345         }\r
1346         return this;\r
1347     }\r
1348 \r
1349     /**\r
1350      * Makes a set from a multicharacter string. Thus "ch" => {"ch"}\r
1351      * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>\r
1352      * @param s the source string\r
1353      * @return a newly created set containing the given string\r
1354      * @stable ICU 2.0\r
1355      */\r
1356     public static UnicodeSet from(String s) {\r
1357         return new UnicodeSet().add(s);\r
1358     }\r
1359 \r
1360 \r
1361     /**\r
1362      * Makes a set from each of the characters in the string. Thus "ch" => {"c", "h"}\r
1363      * @param s the source string\r
1364      * @return a newly created set containing the given characters\r
1365      * @stable ICU 2.0\r
1366      */\r
1367     public static UnicodeSet fromAll(String s) {\r
1368         return new UnicodeSet().addAll(s);\r
1369     }\r
1370 \r
1371 \r
1372     /**\r
1373      * Retain only the elements in this set that are contained in the\r
1374      * specified range.  If <code>end > start</code> then an empty range is\r
1375      * retained, leaving the set empty.\r
1376      *\r
1377      * @param start first character, inclusive, of range to be retained\r
1378      * to this set.\r
1379      * @param end last character, inclusive, of range to be retained\r
1380      * to this set.\r
1381      * @stable ICU 2.0\r
1382      */\r
1383     public UnicodeSet retain(int start, int end) {\r
1384         checkFrozen();\r
1385         if (start < MIN_VALUE || start > MAX_VALUE) {\r
1386             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
1387         }\r
1388         if (end < MIN_VALUE || end > MAX_VALUE) {\r
1389             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
1390         }\r
1391         if (start <= end) {\r
1392             retain(range(start, end), 2, 0);\r
1393         } else {\r
1394             clear();\r
1395         }\r
1396         return this;\r
1397     }\r
1398 \r
1399     /**\r
1400      * Retain the specified character from this set if it is present.\r
1401      * Upon return this set will be empty if it did not contain c, or\r
1402      * will only contain c if it did contain c.\r
1403      * @param c the character to be retained\r
1404      * @return this object, for chaining\r
1405      * @stable ICU 2.0\r
1406      */\r
1407     public final UnicodeSet retain(int c) {\r
1408         return retain(c, c);\r
1409     }\r
1410 \r
1411     /**\r
1412      * Retain the specified string in this set if it is present.\r
1413      * Upon return this set will be empty if it did not contain s, or\r
1414      * will only contain s if it did contain s.\r
1415      * @param s the string to be retained\r
1416      * @return this object, for chaining\r
1417      * @stable ICU 2.0\r
1418      */\r
1419     public final UnicodeSet retain(String s) {\r
1420         int cp = getSingleCP(s); \r
1421         if (cp < 0) {\r
1422             boolean isIn = strings.contains(s);\r
1423             if (isIn && size() == 1) {\r
1424                 return this;\r
1425             }\r
1426             clear();\r
1427             strings.add(s);\r
1428             pat = null;\r
1429         } else {\r
1430             retain(cp, cp);\r
1431         }\r
1432         return this;\r
1433     }\r
1434 \r
1435     /**\r
1436      * Removes the specified range from this set if it is present.\r
1437      * The set will not contain the specified range once the call\r
1438      * returns.  If <code>end > start</code> then an empty range is\r
1439      * removed, leaving the set unchanged.\r
1440      *\r
1441      * @param start first character, inclusive, of range to be removed\r
1442      * from this set.\r
1443      * @param end last character, inclusive, of range to be removed\r
1444      * from this set.\r
1445      * @stable ICU 2.0\r
1446      */\r
1447     public UnicodeSet remove(int start, int end) {\r
1448         checkFrozen();\r
1449         if (start < MIN_VALUE || start > MAX_VALUE) {\r
1450             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
1451         }\r
1452         if (end < MIN_VALUE || end > MAX_VALUE) {\r
1453             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
1454         }\r
1455         if (start <= end) {\r
1456             retain(range(start, end), 2, 2);\r
1457         }\r
1458         return this;\r
1459     }\r
1460 \r
1461     /**\r
1462      * Removes the specified character from this set if it is present.\r
1463      * The set will not contain the specified character once the call\r
1464      * returns.\r
1465      * @param c the character to be removed\r
1466      * @return this object, for chaining\r
1467      * @stable ICU 2.0\r
1468      */\r
1469     public final UnicodeSet remove(int c) {\r
1470         return remove(c, c);\r
1471     }\r
1472 \r
1473     /**\r
1474      * Removes the specified string from this set if it is present.\r
1475      * The set will not contain the specified string once the call\r
1476      * returns.\r
1477      * @param s the string to be removed\r
1478      * @return this object, for chaining\r
1479      * @stable ICU 2.0\r
1480      */\r
1481     public final UnicodeSet remove(String s) {\r
1482         int cp = getSingleCP(s);\r
1483         if (cp < 0) {\r
1484             strings.remove(s);\r
1485             pat = null;\r
1486         } else {\r
1487             remove(cp, cp);\r
1488         }\r
1489         return this;\r
1490     }\r
1491 \r
1492     /**\r
1493      * Complements the specified range in this set.  Any character in\r
1494      * the range will be removed if it is in this set, or will be\r
1495      * added if it is not in this set.  If <code>end > start</code>\r
1496      * then an empty range is complemented, leaving the set unchanged.\r
1497      *\r
1498      * @param start first character, inclusive, of range to be removed\r
1499      * from this set.\r
1500      * @param end last character, inclusive, of range to be removed\r
1501      * from this set.\r
1502      * @stable ICU 2.0\r
1503      */\r
1504     public UnicodeSet complement(int start, int end) {\r
1505         checkFrozen();\r
1506         if (start < MIN_VALUE || start > MAX_VALUE) {\r
1507             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
1508         }\r
1509         if (end < MIN_VALUE || end > MAX_VALUE) {\r
1510             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
1511         }\r
1512         if (start <= end) {\r
1513             xor(range(start, end), 2, 0);\r
1514         }\r
1515         pat = null;\r
1516         return this;\r
1517     }\r
1518 \r
1519     /**\r
1520      * Complements the specified character in this set.  The character\r
1521      * will be removed if it is in this set, or will be added if it is\r
1522      * not in this set.\r
1523      * @stable ICU 2.0\r
1524      */\r
1525     public final UnicodeSet complement(int c) {\r
1526         return complement(c, c);\r
1527     }\r
1528 \r
1529     /**\r
1530      * This is equivalent to\r
1531      * <code>complement(MIN_VALUE, MAX_VALUE)</code>.\r
1532      * @stable ICU 2.0\r
1533      */\r
1534     public UnicodeSet complement() {\r
1535         checkFrozen();\r
1536         if (list[0] == LOW) {\r
1537             System.arraycopy(list, 1, list, 0, len-1);\r
1538             --len;\r
1539         } else {\r
1540             ensureCapacity(len+1);\r
1541             System.arraycopy(list, 0, list, 1, len);\r
1542             list[0] = LOW;\r
1543             ++len;\r
1544         }\r
1545         pat = null;\r
1546         return this;\r
1547     }\r
1548 \r
1549     /**\r
1550      * Complement the specified string in this set.\r
1551      * The set will not contain the specified string once the call\r
1552      * returns.\r
1553      * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>\r
1554      * @param s the string to complement\r
1555      * @return this object, for chaining\r
1556      * @stable ICU 2.0\r
1557      */\r
1558     public final UnicodeSet complement(String s) {\r
1559         checkFrozen();\r
1560         int cp = getSingleCP(s);\r
1561         if (cp < 0) {\r
1562             if (strings.contains(s)) {\r
1563                 strings.remove(s);\r
1564             } else {\r
1565                 strings.add(s);\r
1566             }\r
1567             pat = null;\r
1568         } else {\r
1569             complement(cp, cp);\r
1570         }\r
1571         return this;\r
1572     }\r
1573 \r
1574     /**\r
1575      * Returns true if this set contains the given character.\r
1576      * @param c character to be checked for containment\r
1577      * @return true if the test condition is met\r
1578      * @stable ICU 2.0\r
1579      */\r
1580     public boolean contains(int c) {\r
1581         if (c < MIN_VALUE || c > MAX_VALUE) {\r
1582             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(c, 6));\r
1583         }\r
1584 \r
1585         /*\r
1586         // Set i to the index of the start item greater than ch\r
1587         // We know we will terminate without length test!\r
1588         int i = -1;\r
1589         while (true) {\r
1590             if (c < list[++i]) break;\r
1591         }\r
1592          */\r
1593 \r
1594         int i = findCodePoint(c);\r
1595 \r
1596         return ((i & 1) != 0); // return true if odd\r
1597     }\r
1598 \r
1599     /**\r
1600      * Returns the smallest value i such that c < list[i].  Caller\r
1601      * must ensure that c is a legal value or this method will enter\r
1602      * an infinite loop.  This method performs a binary search.\r
1603      * @param c a character in the range MIN_VALUE..MAX_VALUE\r
1604      * inclusive\r
1605      * @return the smallest integer i in the range 0..len-1,\r
1606      * inclusive, such that c < list[i]\r
1607      */\r
1608     private final int findCodePoint(int c) {\r
1609         /* Examples:\r
1610                                            findCodePoint(c)\r
1611            set              list[]         c=0 1 3 4 7 8\r
1612            ===              ==============   ===========\r
1613            []               [110000]         0 0 0 0 0 0\r
1614            [\u0000-\u0003]  [0, 4, 110000]   1 1 1 2 2 2\r
1615            [\u0004-\u0007]  [4, 8, 110000]   0 0 0 1 1 2\r
1616            [:all:]          [0, 110000]      1 1 1 1 1 1\r
1617          */\r
1618 \r
1619         // Return the smallest i such that c < list[i].  Assume\r
1620         // list[len - 1] == HIGH and that c is legal (0..HIGH-1).\r
1621         if (c < list[0]) return 0;\r
1622         // High runner test.  c is often after the last range, so an\r
1623         // initial check for this condition pays off.\r
1624         if (len >= 2 && c >= list[len-2]) return len-1;\r
1625         int lo = 0;\r
1626         int hi = len - 1;\r
1627         // invariant: c >= list[lo]\r
1628         // invariant: c < list[hi]\r
1629         for (;;) {\r
1630             int i = (lo + hi) >>> 1;\r
1631         if (i == lo) return hi;\r
1632         if (c < list[i]) {\r
1633             hi = i;\r
1634         } else {\r
1635             lo = i;\r
1636         }\r
1637         }\r
1638     }\r
1639 \r
1640     //    //----------------------------------------------------------------\r
1641     //    // Unrolled binary search\r
1642     //    //----------------------------------------------------------------\r
1643     //\r
1644     //    private int validLen = -1; // validated value of len\r
1645     //    private int topOfLow;\r
1646     //    private int topOfHigh;\r
1647     //    private int power;\r
1648     //    private int deltaStart;\r
1649     //\r
1650     //    private void validate() {\r
1651     //        if (len <= 1) {\r
1652     //            throw new IllegalArgumentException("list.len==" + len + "; must be >1");\r
1653     //        }\r
1654     //\r
1655     //        // find greatest power of 2 less than or equal to len\r
1656     //        for (power = exp2.length-1; power > 0 && exp2[power] > len; power--) {}\r
1657     //\r
1658     //        // assert(exp2[power] <= len);\r
1659     //\r
1660     //        // determine the starting points\r
1661     //        topOfLow = exp2[power] - 1;\r
1662     //        topOfHigh = len - 1;\r
1663     //        deltaStart = exp2[power-1];\r
1664     //        validLen = len;\r
1665     //    }\r
1666     //\r
1667     //    private static final int exp2[] = {\r
1668     //        0x1, 0x2, 0x4, 0x8,\r
1669     //        0x10, 0x20, 0x40, 0x80,\r
1670     //        0x100, 0x200, 0x400, 0x800,\r
1671     //        0x1000, 0x2000, 0x4000, 0x8000,\r
1672     //        0x10000, 0x20000, 0x40000, 0x80000,\r
1673     //        0x100000, 0x200000, 0x400000, 0x800000,\r
1674     //        0x1000000, 0x2000000, 0x4000000, 0x8000000,\r
1675     //        0x10000000, 0x20000000 // , 0x40000000 // no unsigned int in Java\r
1676     //    };\r
1677     //\r
1678     //    /**\r
1679     //     * Unrolled lowest index GT.\r
1680     //     */\r
1681     //    private final int leastIndexGT(int searchValue) {\r
1682     //\r
1683     //        if (len != validLen) {\r
1684     //            if (len == 1) return 0;\r
1685     //            validate();\r
1686     //        }\r
1687     //        int temp;\r
1688     //\r
1689     //        // set up initial range to search. Each subrange is a power of two in length\r
1690     //        int high = searchValue < list[topOfLow] ? topOfLow : topOfHigh;\r
1691     //\r
1692     //        // Completely unrolled binary search, folhighing "Programming Pearls"\r
1693     //        // Each case deliberately falls through to the next\r
1694     //        // Logically, list[-1] < all_search_values && list[count] > all_search_values\r
1695     //        // although the values -1 and count are never actually touched.\r
1696     //\r
1697     //        // The bounds at each point are low & high,\r
1698     //        // where low == high - delta*2\r
1699     //        // so high - delta is the midpoint\r
1700     //\r
1701     //        // The invariant AFTER each line is that list[low] < searchValue <= list[high]\r
1702     //\r
1703     //        switch (power) {\r
1704     //        //case 31: if (searchValue < list[temp = high-0x40000000]) high = temp; // no unsigned int in Java\r
1705     //        case 30: if (searchValue < list[temp = high-0x20000000]) high = temp;\r
1706     //        case 29: if (searchValue < list[temp = high-0x10000000]) high = temp;\r
1707     //\r
1708     //        case 28: if (searchValue < list[temp = high- 0x8000000]) high = temp;\r
1709     //        case 27: if (searchValue < list[temp = high- 0x4000000]) high = temp;\r
1710     //        case 26: if (searchValue < list[temp = high- 0x2000000]) high = temp;\r
1711     //        case 25: if (searchValue < list[temp = high- 0x1000000]) high = temp;\r
1712     //\r
1713     //        case 24: if (searchValue < list[temp = high-  0x800000]) high = temp;\r
1714     //        case 23: if (searchValue < list[temp = high-  0x400000]) high = temp;\r
1715     //        case 22: if (searchValue < list[temp = high-  0x200000]) high = temp;\r
1716     //        case 21: if (searchValue < list[temp = high-  0x100000]) high = temp;\r
1717     //\r
1718     //        case 20: if (searchValue < list[temp = high-   0x80000]) high = temp;\r
1719     //        case 19: if (searchValue < list[temp = high-   0x40000]) high = temp;\r
1720     //        case 18: if (searchValue < list[temp = high-   0x20000]) high = temp;\r
1721     //        case 17: if (searchValue < list[temp = high-   0x10000]) high = temp;\r
1722     //\r
1723     //        case 16: if (searchValue < list[temp = high-    0x8000]) high = temp;\r
1724     //        case 15: if (searchValue < list[temp = high-    0x4000]) high = temp;\r
1725     //        case 14: if (searchValue < list[temp = high-    0x2000]) high = temp;\r
1726     //        case 13: if (searchValue < list[temp = high-    0x1000]) high = temp;\r
1727     //\r
1728     //        case 12: if (searchValue < list[temp = high-     0x800]) high = temp;\r
1729     //        case 11: if (searchValue < list[temp = high-     0x400]) high = temp;\r
1730     //        case 10: if (searchValue < list[temp = high-     0x200]) high = temp;\r
1731     //        case  9: if (searchValue < list[temp = high-     0x100]) high = temp;\r
1732     //\r
1733     //        case  8: if (searchValue < list[temp = high-      0x80]) high = temp;\r
1734     //        case  7: if (searchValue < list[temp = high-      0x40]) high = temp;\r
1735     //        case  6: if (searchValue < list[temp = high-      0x20]) high = temp;\r
1736     //        case  5: if (searchValue < list[temp = high-      0x10]) high = temp;\r
1737     //\r
1738     //        case  4: if (searchValue < list[temp = high-       0x8]) high = temp;\r
1739     //        case  3: if (searchValue < list[temp = high-       0x4]) high = temp;\r
1740     //        case  2: if (searchValue < list[temp = high-       0x2]) high = temp;\r
1741     //        case  1: if (searchValue < list[temp = high-       0x1]) high = temp;\r
1742     //        }\r
1743     //\r
1744     //        return high;\r
1745     //    }\r
1746     //\r
1747     //    // For debugging only\r
1748     //    public int len() {\r
1749     //        return len;\r
1750     //    }\r
1751     //\r
1752     //    //----------------------------------------------------------------\r
1753     //    //----------------------------------------------------------------\r
1754 \r
1755     /**\r
1756      * Returns true if this set contains every character\r
1757      * of the given range.\r
1758      * @param start first character, inclusive, of the range\r
1759      * @param end last character, inclusive, of the range\r
1760      * @return true if the test condition is met\r
1761      * @stable ICU 2.0\r
1762      */\r
1763     public boolean contains(int start, int end) {\r
1764         if (start < MIN_VALUE || start > MAX_VALUE) {\r
1765             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
1766         }\r
1767         if (end < MIN_VALUE || end > MAX_VALUE) {\r
1768             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
1769         }\r
1770         //int i = -1;\r
1771         //while (true) {\r
1772         //    if (start < list[++i]) break;\r
1773         //}\r
1774         int i = findCodePoint(start);\r
1775         return ((i & 1) != 0 && end < list[i]);\r
1776     }\r
1777 \r
1778     /**\r
1779      * Returns <tt>true</tt> if this set contains the given\r
1780      * multicharacter string.\r
1781      * @param s string to be checked for containment\r
1782      * @return <tt>true</tt> if this set contains the specified string\r
1783      * @stable ICU 2.0\r
1784      */\r
1785     public final boolean contains(String s) {\r
1786 \r
1787         int cp = getSingleCP(s);\r
1788         if (cp < 0) {\r
1789             return strings.contains(s);\r
1790         } else {\r
1791             return contains(cp);\r
1792         }\r
1793     }\r
1794 \r
1795     /**\r
1796      * Returns true if this set contains all the characters and strings\r
1797      * of the given set.\r
1798      * @param b set to be checked for containment\r
1799      * @return true if the test condition is met\r
1800      * @stable ICU 2.0\r
1801      */\r
1802     public boolean containsAll(UnicodeSet b) {\r
1803         // The specified set is a subset if all of its pairs are contained in\r
1804         // this set. This implementation accesses the lists directly for speed.\r
1805         // TODO: this could be faster if size() were cached. But that would affect building speed\r
1806         // so it needs investigation.\r
1807         int[] listB = b.list;\r
1808         boolean needA = true;\r
1809         boolean needB = true;\r
1810         int aPtr = 0;\r
1811         int bPtr = 0;\r
1812         int aLen = len - 1;\r
1813         int bLen = b.len - 1;\r
1814         int startA = 0, startB = 0, limitA = 0, limitB = 0;\r
1815         while (true) {\r
1816             // double iterations are such a pain...\r
1817             if (needA) {\r
1818                 if (aPtr >= aLen) {\r
1819                     // ran out of A. If B is also exhausted, then break;\r
1820                     if (needB && bPtr >= bLen) {\r
1821                         break;\r
1822                     }\r
1823                     return false;\r
1824                 }\r
1825                 startA = list[aPtr++];\r
1826                 limitA = list[aPtr++];\r
1827             }\r
1828             if (needB) {\r
1829                 if (bPtr >= bLen) {\r
1830                     // ran out of B. Since we got this far, we have an A and we are ok so far\r
1831                     break;\r
1832                 }\r
1833                 startB = listB[bPtr++];\r
1834                 limitB = listB[bPtr++];\r
1835             }\r
1836             // if B doesn't overlap and is greater than A, get new A\r
1837             if (startB >= limitA) {\r
1838                 needA = true;\r
1839                 needB = false;\r
1840                 continue;\r
1841             }\r
1842             // if B is wholy contained in A, then get a new B\r
1843             if (startB >= startA && limitB <= limitA) {\r
1844                 needA = false;\r
1845                 needB = true;\r
1846                 continue;\r
1847             }\r
1848             // all other combinations mean we fail\r
1849             return false;\r
1850         }\r
1851 \r
1852         if (!strings.containsAll(b.strings)) return false;\r
1853         return true;\r
1854     }\r
1855 \r
1856     //    /**\r
1857     //     * Returns true if this set contains all the characters and strings\r
1858     //     * of the given set.\r
1859     //     * @param c set to be checked for containment\r
1860     //     * @return true if the test condition is met\r
1861     //     * @stable ICU 2.0\r
1862     //     */\r
1863     //    public boolean containsAllOld(UnicodeSet c) {\r
1864     //        // The specified set is a subset if all of its pairs are contained in\r
1865     //        // this set.  It's possible to code this more efficiently in terms of\r
1866     //        // direct manipulation of the inversion lists if the need arises.\r
1867     //        int n = c.getRangeCount();\r
1868     //        for (int i=0; i<n; ++i) {\r
1869     //            if (!contains(c.getRangeStart(i), c.getRangeEnd(i))) {\r
1870     //                return false;\r
1871     //            }\r
1872     //        }\r
1873     //        if (!strings.containsAll(c.strings)) return false;\r
1874     //        return true;\r
1875     //    }\r
1876 \r
1877     /**\r
1878      * Returns true if there is a partition of the string such that this set contains each of the partitioned strings.\r
1879      * For example, for the Unicode set [a{bc}{cd}]<br>\r
1880      * containsAll is true for each of: "a", "bc", ""cdbca"<br>\r
1881      * containsAll is false for each of: "acb", "bcda", "bcx"<br>\r
1882      * @param s string containing characters to be checked for containment\r
1883      * @return true if the test condition is met\r
1884      * @stable ICU 2.0\r
1885      */\r
1886     public boolean containsAll(String s) {\r
1887         int cp;\r
1888         for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {\r
1889             cp = UTF16.charAt(s, i);\r
1890             if (!contains(cp))  {\r
1891                 if (strings.size() == 0) {\r
1892                     return false;\r
1893                 }\r
1894                 return containsAll(s, 0);\r
1895             }\r
1896         }\r
1897         return true;\r
1898     }\r
1899 \r
1900     /**\r
1901      * Recursive routine called if we fail to find a match in containsAll, and there are strings\r
1902      * @param s source string\r
1903      * @param i point to match to the end on\r
1904      * @return true if ok\r
1905      */\r
1906     private boolean containsAll(String s, int i) {\r
1907         if (i >= s.length()) {\r
1908             return true;\r
1909         }\r
1910         int  cp= UTF16.charAt(s, i);\r
1911         if (contains(cp) && containsAll(s, i+UTF16.getCharCount(cp))) {\r
1912             return true;\r
1913         }\r
1914         for (String setStr : strings) {\r
1915             if (s.startsWith(setStr, i) &&  containsAll(s, i+setStr.length())) {\r
1916                 return true;\r
1917             }\r
1918         }\r
1919         return false;\r
1920 \r
1921     }\r
1922 \r
1923     /**\r
1924      * Get the Regex equivalent for this UnicodeSet\r
1925      * @return regex pattern equivalent to this UnicodeSet\r
1926      * @internal\r
1927      * @deprecated This API is ICU internal only.\r
1928      */\r
1929     public String getRegexEquivalent() {\r
1930         if (strings.size() == 0) {\r
1931             return toString();\r
1932         }\r
1933         StringBuffer result = new StringBuffer("(?:");\r
1934         _generatePattern(result, true, false);\r
1935         for (String s : strings) {\r
1936             result.append('|');\r
1937             _appendToPat(result, s, true);\r
1938         }\r
1939         return result.append(")").toString();\r
1940     }\r
1941 \r
1942     /**\r
1943      * Returns true if this set contains none of the characters\r
1944      * of the given range.\r
1945      * @param start first character, inclusive, of the range\r
1946      * @param end last character, inclusive, of the range\r
1947      * @return true if the test condition is met\r
1948      * @stable ICU 2.0\r
1949      */\r
1950     public boolean containsNone(int start, int end) {\r
1951         if (start < MIN_VALUE || start > MAX_VALUE) {\r
1952             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
1953         }\r
1954         if (end < MIN_VALUE || end > MAX_VALUE) {\r
1955             throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
1956         }\r
1957         int i = -1;\r
1958         while (true) {\r
1959             if (start < list[++i]) break;\r
1960         }\r
1961         return ((i & 1) == 0 && end < list[i]);\r
1962     }\r
1963 \r
1964     /**\r
1965      * Returns true if none of the characters or strings in this UnicodeSet appears in the string.\r
1966      * For example, for the Unicode set [a{bc}{cd}]<br>\r
1967      * containsNone is true for: "xy", "cb"<br>\r
1968      * containsNone is false for: "a", "bc", "bcd"<br>\r
1969      * @param b set to be checked for containment\r
1970      * @return true if the test condition is met\r
1971      * @stable ICU 2.0\r
1972      */\r
1973     public boolean containsNone(UnicodeSet b) {\r
1974         // The specified set is a subset if some of its pairs overlap with some of this set's pairs.\r
1975         // This implementation accesses the lists directly for speed.\r
1976         int[] listB = b.list;\r
1977         boolean needA = true;\r
1978         boolean needB = true;\r
1979         int aPtr = 0;\r
1980         int bPtr = 0;\r
1981         int aLen = len - 1;\r
1982         int bLen = b.len - 1;\r
1983         int startA = 0, startB = 0, limitA = 0, limitB = 0;\r
1984         while (true) {\r
1985             // double iterations are such a pain...\r
1986             if (needA) {\r
1987                 if (aPtr >= aLen) {\r
1988                     // ran out of A: break so we test strings\r
1989                     break;\r
1990                 }\r
1991                 startA = list[aPtr++];\r
1992                 limitA = list[aPtr++];\r
1993             }\r
1994             if (needB) {\r
1995                 if (bPtr >= bLen) {\r
1996                     // ran out of B: break so we test strings\r
1997                     break;\r
1998                 }\r
1999                 startB = listB[bPtr++];\r
2000                 limitB = listB[bPtr++];\r
2001             }\r
2002             // if B is higher than any part of A, get new A\r
2003             if (startB >= limitA) {\r
2004                 needA = true;\r
2005                 needB = false;\r
2006                 continue;\r
2007             }\r
2008             // if A is higher than any part of B, get new B\r
2009             if (startA >= limitB) {\r
2010                 needA = false;\r
2011                 needB = true;\r
2012                 continue;\r
2013             }\r
2014             // all other combinations mean we fail\r
2015             return false;\r
2016         }\r
2017 \r
2018         if (!SortedSetRelation.hasRelation(strings, SortedSetRelation.DISJOINT, b.strings)) return false;\r
2019         return true;\r
2020     }\r
2021 \r
2022     //    /**\r
2023     //     * Returns true if none of the characters or strings in this UnicodeSet appears in the string.\r
2024     //     * For example, for the Unicode set [a{bc}{cd}]<br>\r
2025     //     * containsNone is true for: "xy", "cb"<br>\r
2026     //     * containsNone is false for: "a", "bc", "bcd"<br>\r
2027     //     * @param c set to be checked for containment\r
2028     //     * @return true if the test condition is met\r
2029     //     * @stable ICU 2.0\r
2030     //     */\r
2031     //    public boolean containsNoneOld(UnicodeSet c) {\r
2032     //        // The specified set is a subset if all of its pairs are contained in\r
2033     //        // this set.  It's possible to code this more efficiently in terms of\r
2034     //        // direct manipulation of the inversion lists if the need arises.\r
2035     //        int n = c.getRangeCount();\r
2036     //        for (int i=0; i<n; ++i) {\r
2037     //            if (!containsNone(c.getRangeStart(i), c.getRangeEnd(i))) {\r
2038     //                return false;\r
2039     //            }\r
2040     //        }\r
2041     //        if (!SortedSetRelation.hasRelation(strings, SortedSetRelation.DISJOINT, c.strings)) return false;\r
2042     //        return true;\r
2043     //    }\r
2044 \r
2045     /**\r
2046      * Returns true if this set contains none of the characters\r
2047      * of the given string.\r
2048      * @param s string containing characters to be checked for containment\r
2049      * @return true if the test condition is met\r
2050      * @stable ICU 2.0\r
2051      */\r
2052     public boolean containsNone(String s) {\r
2053         return span(s, SpanCondition.NOT_CONTAINED) == s.length();\r
2054     }\r
2055 \r
2056     /**\r
2057      * Returns true if this set contains one or more of the characters\r
2058      * in the given range.\r
2059      * @param start first character, inclusive, of the range\r
2060      * @param end last character, inclusive, of the range\r
2061      * @return true if the condition is met\r
2062      * @stable ICU 2.0\r
2063      */\r
2064     public final boolean containsSome(int start, int end) {\r
2065         return !containsNone(start, end);\r
2066     }\r
2067 \r
2068     /**\r
2069      * Returns true if this set contains one or more of the characters\r
2070      * and strings of the given set.\r
2071      * @param s set to be checked for containment\r
2072      * @return true if the condition is met\r
2073      * @stable ICU 2.0\r
2074      */\r
2075     public final boolean containsSome(UnicodeSet s) {\r
2076         return !containsNone(s);\r
2077     }\r
2078 \r
2079     /**\r
2080      * Returns true if this set contains one or more of the characters\r
2081      * of the given string.\r
2082      * @param s string containing characters to be checked for containment\r
2083      * @return true if the condition is met\r
2084      * @stable ICU 2.0\r
2085      */\r
2086     public final boolean containsSome(String s) {\r
2087         return !containsNone(s);\r
2088     }\r
2089 \r
2090 \r
2091     /**\r
2092      * Adds all of the elements in the specified set to this set if\r
2093      * they're not already present.  This operation effectively\r
2094      * modifies this set so that its value is the <i>union</i> of the two\r
2095      * sets.  The behavior of this operation is unspecified if the specified\r
2096      * collection is modified while the operation is in progress.\r
2097      *\r
2098      * @param c set whose elements are to be added to this set.\r
2099      * @stable ICU 2.0\r
2100      */\r
2101     public UnicodeSet addAll(UnicodeSet c) {\r
2102         checkFrozen();\r
2103         add(c.list, c.len, 0);\r
2104         strings.addAll(c.strings);\r
2105         return this;\r
2106     }\r
2107 \r
2108     /**\r
2109      * Retains only the elements in this set that are contained in the\r
2110      * specified set.  In other words, removes from this set all of\r
2111      * its elements that are not contained in the specified set.  This\r
2112      * operation effectively modifies this set so that its value is\r
2113      * the <i>intersection</i> of the two sets.\r
2114      *\r
2115      * @param c set that defines which elements this set will retain.\r
2116      * @stable ICU 2.0\r
2117      */\r
2118     public UnicodeSet retainAll(UnicodeSet c) {\r
2119         checkFrozen();\r
2120         retain(c.list, c.len, 0);\r
2121         strings.retainAll(c.strings);\r
2122         return this;\r
2123     }\r
2124 \r
2125     /**\r
2126      * Removes from this set all of its elements that are contained in the\r
2127      * specified set.  This operation effectively modifies this\r
2128      * set so that its value is the <i>asymmetric set difference</i> of\r
2129      * the two sets.\r
2130      *\r
2131      * @param c set that defines which elements will be removed from\r
2132      *          this set.\r
2133      * @stable ICU 2.0\r
2134      */\r
2135     public UnicodeSet removeAll(UnicodeSet c) {\r
2136         checkFrozen();\r
2137         retain(c.list, c.len, 2);\r
2138         strings.removeAll(c.strings);\r
2139         return this;\r
2140     }\r
2141 \r
2142     /**\r
2143      * Complements in this set all elements contained in the specified\r
2144      * set.  Any character in the other set will be removed if it is\r
2145      * in this set, or will be added if it is not in this set.\r
2146      *\r
2147      * @param c set that defines which elements will be complemented from\r
2148      *          this set.\r
2149      * @stable ICU 2.0\r
2150      */\r
2151     public UnicodeSet complementAll(UnicodeSet c) {\r
2152         checkFrozen();\r
2153         xor(c.list, c.len, 0);\r
2154         SortedSetRelation.doOperation(strings, SortedSetRelation.COMPLEMENTALL, c.strings);\r
2155         return this;\r
2156     }\r
2157 \r
2158     /**\r
2159      * Removes all of the elements from this set.  This set will be\r
2160      * empty after this call returns.\r
2161      * @stable ICU 2.0\r
2162      */\r
2163     public UnicodeSet clear() {\r
2164         checkFrozen();\r
2165         list[0] = HIGH;\r
2166         len = 1;\r
2167         pat = null;\r
2168         strings.clear();\r
2169         return this;\r
2170     }\r
2171 \r
2172     /**\r
2173      * Iteration method that returns the number of ranges contained in\r
2174      * this set.\r
2175      * @see #getRangeStart\r
2176      * @see #getRangeEnd\r
2177      * @stable ICU 2.0\r
2178      */\r
2179     public int getRangeCount() {\r
2180         return len/2;\r
2181     }\r
2182 \r
2183     /**\r
2184      * Iteration method that returns the first character in the\r
2185      * specified range of this set.\r
2186      * @exception ArrayIndexOutOfBoundsException if index is outside\r
2187      * the range <code>0..getRangeCount()-1</code>\r
2188      * @see #getRangeCount\r
2189      * @see #getRangeEnd\r
2190      * @stable ICU 2.0\r
2191      */\r
2192     public int getRangeStart(int index) {\r
2193         return list[index*2];\r
2194     }\r
2195 \r
2196     /**\r
2197      * Iteration method that returns the last character in the\r
2198      * specified range of this set.\r
2199      * @exception ArrayIndexOutOfBoundsException if index is outside\r
2200      * the range <code>0..getRangeCount()-1</code>\r
2201      * @see #getRangeStart\r
2202      * @see #getRangeEnd\r
2203      * @stable ICU 2.0\r
2204      */\r
2205     public int getRangeEnd(int index) {\r
2206         return (list[index*2 + 1] - 1);\r
2207     }\r
2208 \r
2209     /**\r
2210      * Reallocate this objects internal structures to take up the least\r
2211      * possible space, without changing this object's value.\r
2212      * @stable ICU 2.0\r
2213      */\r
2214     public UnicodeSet compact() {\r
2215         checkFrozen();\r
2216         if (len != list.length) {\r
2217             int[] temp = new int[len];\r
2218             System.arraycopy(list, 0, temp, 0, len);\r
2219             list = temp;\r
2220         }\r
2221         rangeList = null;\r
2222         buffer = null;\r
2223         return this;\r
2224     }\r
2225 \r
2226     /**\r
2227      * Compares the specified object with this set for equality.  Returns\r
2228      * <tt>true</tt> if the specified object is also a set, the two sets\r
2229      * have the same size, and every member of the specified set is\r
2230      * contained in this set (or equivalently, every member of this set is\r
2231      * contained in the specified set).\r
2232      *\r
2233      * @param o Object to be compared for equality with this set.\r
2234      * @return <tt>true</tt> if the specified Object is equal to this set.\r
2235      * @stable ICU 2.0\r
2236      */\r
2237     public boolean equals(Object o) {\r
2238         try {\r
2239             UnicodeSet that = (UnicodeSet) o;\r
2240             if (len != that.len) return false;\r
2241             for (int i = 0; i < len; ++i) {\r
2242                 if (list[i] != that.list[i]) return false;\r
2243             }\r
2244             if (!strings.equals(that.strings)) return false;\r
2245         } catch (Exception e) {\r
2246             return false;\r
2247         }\r
2248         return true;\r
2249     }\r
2250 \r
2251     /**\r
2252      * Returns the hash code value for this set.\r
2253      *\r
2254      * @return the hash code value for this set.\r
2255      * @see java.lang.Object#hashCode()\r
2256      * @stable ICU 2.0\r
2257      */\r
2258     public int hashCode() {\r
2259         int result = len;\r
2260         for (int i = 0; i < len; ++i) {\r
2261             result *= 1000003;\r
2262             result += list[i];\r
2263         }\r
2264         return result;\r
2265     }\r
2266 \r
2267     /**\r
2268      * Return a programmer-readable string representation of this object.\r
2269      * @stable ICU 2.0\r
2270      */\r
2271     public String toString() {\r
2272         return toPattern(true);\r
2273     }\r
2274 \r
2275     //----------------------------------------------------------------\r
2276     // Implementation: Pattern parsing\r
2277     //----------------------------------------------------------------\r
2278 \r
2279     /**\r
2280      * Parses the given pattern, starting at the given position.  The character\r
2281      * at pattern.charAt(pos.getIndex()) must be '[', or the parse fails.\r
2282      * Parsing continues until the corresponding closing ']'.  If a syntax error\r
2283      * is encountered between the opening and closing brace, the parse fails.\r
2284      * Upon return from a successful parse, the ParsePosition is updated to\r
2285      * point to the character following the closing ']', and an inversion\r
2286      * list for the parsed pattern is returned.  This method\r
2287      * calls itself recursively to parse embedded subpatterns.\r
2288      *\r
2289      * @param pattern the string containing the pattern to be parsed.  The\r
2290      * portion of the string from pos.getIndex(), which must be a '[', to the\r
2291      * corresponding closing ']', is parsed.\r
2292      * @param pos upon entry, the position at which to being parsing.  The\r
2293      * character at pattern.charAt(pos.getIndex()) must be a '['.  Upon return\r
2294      * from a successful parse, pos.getIndex() is either the character after the\r
2295      * closing ']' of the parsed pattern, or pattern.length() if the closing ']'\r
2296      * is the last character of the pattern string.\r
2297      * @return an inversion list for the parsed substring\r
2298      * of <code>pattern</code>\r
2299      * @exception java.lang.IllegalArgumentException if the parse fails.\r
2300      * @internal\r
2301      * @deprecated This API is ICU internal only.\r
2302      */\r
2303     public UnicodeSet applyPattern(String pattern,\r
2304             ParsePosition pos,\r
2305             SymbolTable symbols,\r
2306             int options) {\r
2307 \r
2308         // Need to build the pattern in a temporary string because\r
2309         // _applyPattern calls add() etc., which set pat to empty.\r
2310         boolean parsePositionWasNull = pos == null;\r
2311         if (parsePositionWasNull) {\r
2312             pos = new ParsePosition(0);\r
2313         }\r
2314 \r
2315         StringBuffer rebuiltPat = new StringBuffer();\r
2316         RuleCharacterIterator chars =\r
2317             new RuleCharacterIterator(pattern, symbols, pos);\r
2318         applyPattern(chars, symbols, rebuiltPat, options);\r
2319         if (chars.inVariable()) {\r
2320             syntaxError(chars, "Extra chars in variable value");\r
2321         }\r
2322         pat = rebuiltPat.toString();\r
2323         if (parsePositionWasNull) {\r
2324             int i = pos.getIndex();\r
2325 \r
2326             // Skip over trailing whitespace\r
2327             if ((options & IGNORE_SPACE) != 0) {\r
2328                 i = Utility.skipWhitespace(pattern, i);\r
2329             }\r
2330 \r
2331             if (i != pattern.length()) {\r
2332                 throw new IllegalArgumentException("Parse of \"" + pattern +\r
2333                         "\" failed at " + i);\r
2334             }\r
2335         }\r
2336         return this;\r
2337     }\r
2338 \r
2339     /**\r
2340      * Parse the pattern from the given RuleCharacterIterator.  The\r
2341      * iterator is advanced over the parsed pattern.\r
2342      * @param chars iterator over the pattern characters.  Upon return\r
2343      * it will be advanced to the first character after the parsed\r
2344      * pattern, or the end of the iteration if all characters are\r
2345      * parsed.\r
2346      * @param symbols symbol table to use to parse and dereference\r
2347      * variables, or null if none.\r
2348      * @param rebuiltPat the pattern that was parsed, rebuilt or\r
2349      * copied from the input pattern, as appropriate.\r
2350      * @param options a bit mask of zero or more of the following:\r
2351      * IGNORE_SPACE, CASE.\r
2352      */\r
2353     void applyPattern(RuleCharacterIterator chars, SymbolTable symbols,\r
2354             StringBuffer rebuiltPat, int options) {\r
2355 \r
2356         // Syntax characters: [ ] ^ - & { }\r
2357 \r
2358         // Recognized special forms for chars, sets: c-c s-s s&s\r
2359 \r
2360         int opts = RuleCharacterIterator.PARSE_VARIABLES |\r
2361         RuleCharacterIterator.PARSE_ESCAPES;\r
2362         if ((options & IGNORE_SPACE) != 0) {\r
2363             opts |= RuleCharacterIterator.SKIP_WHITESPACE;\r
2364         }\r
2365 \r
2366         StringBuffer patBuf = new StringBuffer(), buf = null;\r
2367         boolean usePat = false;\r
2368         UnicodeSet scratch = null;\r
2369         Object backup = null;\r
2370 \r
2371         // mode: 0=before [, 1=between [...], 2=after ]\r
2372         // lastItem: 0=none, 1=char, 2=set\r
2373         int lastItem = 0, lastChar = 0, mode = 0;\r
2374         char op = 0;\r
2375 \r
2376         boolean invert = false;\r
2377 \r
2378         clear();\r
2379 \r
2380         while (mode != 2 && !chars.atEnd()) {\r
2381             //Eclipse stated the following is "dead code"\r
2382             /*\r
2383             if (false) {\r
2384                 // Debugging assertion\r
2385                 if (!((lastItem == 0 && op == 0) ||\r
2386                         (lastItem == 1 && (op == 0 || op == '-')) ||\r
2387                         (lastItem == 2 && (op == 0 || op == '-' || op == '&')))) {\r
2388                     throw new IllegalArgumentException();\r
2389                 }\r
2390             }*/\r
2391 \r
2392             int c = 0;\r
2393             boolean literal = false;\r
2394             UnicodeSet nested = null;\r
2395 \r
2396             // -------- Check for property pattern\r
2397 \r
2398             // setMode: 0=none, 1=unicodeset, 2=propertypat, 3=preparsed\r
2399             int setMode = 0;\r
2400             if (resemblesPropertyPattern(chars, opts)) {\r
2401                 setMode = 2;\r
2402             }\r
2403 \r
2404             // -------- Parse '[' of opening delimiter OR nested set.\r
2405             // If there is a nested set, use `setMode' to define how\r
2406             // the set should be parsed.  If the '[' is part of the\r
2407             // opening delimiter for this pattern, parse special\r
2408             // strings "[", "[^", "[-", and "[^-".  Check for stand-in\r
2409             // characters representing a nested set in the symbol\r
2410             // table.\r
2411 \r
2412             else {\r
2413                 // Prepare to backup if necessary\r
2414                 backup = chars.getPos(backup);\r
2415                 c = chars.next(opts);\r
2416                 literal = chars.isEscaped();\r
2417 \r
2418                 if (c == '[' && !literal) {\r
2419                     if (mode == 1) {\r
2420                         chars.setPos(backup); // backup\r
2421                         setMode = 1;\r
2422                     } else {\r
2423                         // Handle opening '[' delimiter\r
2424                         mode = 1;\r
2425                         patBuf.append('[');\r
2426                         backup = chars.getPos(backup); // prepare to backup\r
2427                         c = chars.next(opts);\r
2428                         literal = chars.isEscaped();\r
2429                         if (c == '^' && !literal) {\r
2430                             invert = true;\r
2431                             patBuf.append('^');\r
2432                             backup = chars.getPos(backup); // prepare to backup\r
2433                             c = chars.next(opts);\r
2434                             literal = chars.isEscaped();\r
2435                         }\r
2436                         // Fall through to handle special leading '-';\r
2437                         // otherwise restart loop for nested [], \p{}, etc.\r
2438                         if (c == '-') {\r
2439                             literal = true;\r
2440                             // Fall through to handle literal '-' below\r
2441                         } else {\r
2442                             chars.setPos(backup); // backup\r
2443                             continue;\r
2444                         }\r
2445                     }\r
2446                 } else if (symbols != null) {\r
2447                     UnicodeMatcher m = symbols.lookupMatcher(c); // may be null\r
2448                     if (m != null) {\r
2449                         try {\r
2450                             nested = (UnicodeSet) m;\r
2451                             setMode = 3;\r
2452                         } catch (ClassCastException e) {\r
2453                             syntaxError(chars, "Syntax error");\r
2454                         }\r
2455                     }\r
2456                 }\r
2457             }\r
2458 \r
2459             // -------- Handle a nested set.  This either is inline in\r
2460             // the pattern or represented by a stand-in that has\r
2461             // previously been parsed and was looked up in the symbol\r
2462             // table.\r
2463 \r
2464             if (setMode != 0) {\r
2465                 if (lastItem == 1) {\r
2466                     if (op != 0) {\r
2467                         syntaxError(chars, "Char expected after operator");\r
2468                     }\r
2469                     add_unchecked(lastChar, lastChar);\r
2470                     _appendToPat(patBuf, lastChar, false);\r
2471                     lastItem = op = 0;\r
2472                 }\r
2473 \r
2474                 if (op == '-' || op == '&') {\r
2475                     patBuf.append(op);\r
2476                 }\r
2477 \r
2478                 if (nested == null) {\r
2479                     if (scratch == null) scratch = new UnicodeSet();\r
2480                     nested = scratch;\r
2481                 }\r
2482                 switch (setMode) {\r
2483                 case 1:\r
2484                     nested.applyPattern(chars, symbols, patBuf, options);\r
2485                     break;\r
2486                 case 2:\r
2487                     chars.skipIgnored(opts);\r
2488                     nested.applyPropertyPattern(chars, patBuf, symbols);\r
2489                     break;\r
2490                 case 3: // `nested' already parsed\r
2491                     nested._toPattern(patBuf, false);\r
2492                     break;\r
2493                 }\r
2494 \r
2495                 usePat = true;\r
2496 \r
2497                 if (mode == 0) {\r
2498                     // Entire pattern is a category; leave parse loop\r
2499                     set(nested);\r
2500                     mode = 2;\r
2501                     break;\r
2502                 }\r
2503 \r
2504                 switch (op) {\r
2505                 case '-':\r
2506                     removeAll(nested);\r
2507                     break;\r
2508                 case '&':\r
2509                     retainAll(nested);\r
2510                     break;\r
2511                 case 0:\r
2512                     addAll(nested);\r
2513                     break;\r
2514                 }\r
2515 \r
2516                 op = 0;\r
2517                 lastItem = 2;\r
2518 \r
2519                 continue;\r
2520             }\r
2521 \r
2522             if (mode == 0) {\r
2523                 syntaxError(chars, "Missing '['");\r
2524             }\r
2525 \r
2526             // -------- Parse special (syntax) characters.  If the\r
2527             // current character is not special, or if it is escaped,\r
2528             // then fall through and handle it below.\r
2529 \r
2530             if (!literal) {\r
2531                 switch (c) {\r
2532                 case ']':\r
2533                     if (lastItem == 1) {\r
2534                         add_unchecked(lastChar, lastChar);\r
2535                         _appendToPat(patBuf, lastChar, false);\r
2536                     }\r
2537                     // Treat final trailing '-' as a literal\r
2538                     if (op == '-') {\r
2539                         add_unchecked(op, op);\r
2540                         patBuf.append(op);\r
2541                     } else if (op == '&') {\r
2542                         syntaxError(chars, "Trailing '&'");\r
2543                     }\r
2544                     patBuf.append(']');\r
2545                     mode = 2;\r
2546                     continue;\r
2547                 case '-':\r
2548                     if (op == 0) {\r
2549                         if (lastItem != 0) {\r
2550                             op = (char) c;\r
2551                             continue;\r
2552                         } else {\r
2553                             // Treat final trailing '-' as a literal\r
2554                             add_unchecked(c, c);\r
2555                             c = chars.next(opts);\r
2556                             literal = chars.isEscaped();\r
2557                             if (c == ']' && !literal) {\r
2558                                 patBuf.append("-]");\r
2559                                 mode = 2;\r
2560                                 continue;\r
2561                             }\r
2562                         }\r
2563                     }\r
2564                     syntaxError(chars, "'-' not after char or set");\r
2565                     break;\r
2566                 case '&':\r
2567                     if (lastItem == 2 && op == 0) {\r
2568                         op = (char) c;\r
2569                         continue;\r
2570                     }\r
2571                     syntaxError(chars, "'&' not after set");\r
2572                     break;\r
2573                 case '^':\r
2574                     syntaxError(chars, "'^' not after '['");\r
2575                     break;\r
2576                 case '{':\r
2577                     if (op != 0) {\r
2578                         syntaxError(chars, "Missing operand after operator");\r
2579                     }\r
2580                     if (lastItem == 1) {\r
2581                         add_unchecked(lastChar, lastChar);\r
2582                         _appendToPat(patBuf, lastChar, false);\r
2583                     }\r
2584                     lastItem = 0;\r
2585                     if (buf == null) {\r
2586                         buf = new StringBuffer();\r
2587                     } else {\r
2588                         buf.setLength(0);\r
2589                     }\r
2590                     boolean ok = false;\r
2591                     while (!chars.atEnd()) {\r
2592                         c = chars.next(opts);\r
2593                         literal = chars.isEscaped();\r
2594                         if (c == '}' && !literal) {\r
2595                             ok = true;\r
2596                             break;\r
2597                         }\r
2598                         UTF16.append(buf, c);\r
2599                     }\r
2600                     if (buf.length() < 1 || !ok) {\r
2601                         syntaxError(chars, "Invalid multicharacter string");\r
2602                     }\r
2603                     // We have new string. Add it to set and continue;\r
2604                     // we don't need to drop through to the further\r
2605                     // processing\r
2606                     add(buf.toString());\r
2607                     patBuf.append('{');\r
2608                     _appendToPat(patBuf, buf.toString(), false);\r
2609                     patBuf.append('}');\r
2610                     continue;\r
2611                 case SymbolTable.SYMBOL_REF:\r
2612                     //         symbols  nosymbols\r
2613                     // [a-$]   error    error (ambiguous)\r
2614                     // [a$]    anchor   anchor\r
2615                     // [a-$x]  var "x"* literal '$'\r
2616                     // [a-$.]  error    literal '$'\r
2617                     // *We won't get here in the case of var "x"\r
2618                     backup = chars.getPos(backup);\r
2619                     c = chars.next(opts);\r
2620                     literal = chars.isEscaped();\r
2621                     boolean anchor = (c == ']' && !literal);\r
2622                     if (symbols == null && !anchor) {\r
2623                         c = SymbolTable.SYMBOL_REF;\r
2624                         chars.setPos(backup);\r
2625                         break; // literal '$'\r
2626                     }\r
2627                     if (anchor && op == 0) {\r
2628                         if (lastItem == 1) {\r
2629                             add_unchecked(lastChar, lastChar);\r
2630                             _appendToPat(patBuf, lastChar, false);\r
2631                         }\r
2632                         add_unchecked(UnicodeMatcher.ETHER);\r
2633                         usePat = true;\r
2634                         patBuf.append(SymbolTable.SYMBOL_REF).append(']');\r
2635                         mode = 2;\r
2636                         continue;\r
2637                     }\r
2638                     syntaxError(chars, "Unquoted '$'");\r
2639                     break;\r
2640                 default:\r
2641                     break;\r
2642                 }\r
2643             }\r
2644 \r
2645             // -------- Parse literal characters.  This includes both\r
2646             // escaped chars ("\u4E01") and non-syntax characters\r
2647             // ("a").\r
2648 \r
2649             switch (lastItem) {\r
2650             case 0:\r
2651                 lastItem = 1;\r
2652                 lastChar = c;\r
2653                 break;\r
2654             case 1:\r
2655                 if (op == '-') {\r
2656                     if (lastChar >= c) {\r
2657                         // Don't allow redundant (a-a) or empty (b-a) ranges;\r
2658                         // these are most likely typos.\r
2659                         syntaxError(chars, "Invalid range");\r
2660                     }\r
2661                     add_unchecked(lastChar, c);\r
2662                     _appendToPat(patBuf, lastChar, false);\r
2663                     patBuf.append(op);\r
2664                     _appendToPat(patBuf, c, false);\r
2665                     lastItem = op = 0;\r
2666                 } else {\r
2667                     add_unchecked(lastChar, lastChar);\r
2668                     _appendToPat(patBuf, lastChar, false);\r
2669                     lastChar = c;\r
2670                 }\r
2671                 break;\r
2672             case 2:\r
2673                 if (op != 0) {\r
2674                     syntaxError(chars, "Set expected after operator");\r
2675                 }\r
2676                 lastChar = c;\r
2677                 lastItem = 1;\r
2678                 break;\r
2679             }\r
2680         }\r
2681 \r
2682         if (mode != 2) {\r
2683             syntaxError(chars, "Missing ']'");\r
2684         }\r
2685 \r
2686         chars.skipIgnored(opts);\r
2687 \r
2688         /**\r
2689          * Handle global flags (invert, case insensitivity).  If this\r
2690          * pattern should be compiled case-insensitive, then we need\r
2691          * to close over case BEFORE COMPLEMENTING.  This makes\r
2692          * patterns like /[^abc]/i work.\r
2693          */\r
2694         if ((options & CASE) != 0) {\r
2695             closeOver(CASE);\r
2696         }\r
2697         if (invert) {\r
2698             complement();\r
2699         }\r
2700 \r
2701         // Use the rebuilt pattern (pat) only if necessary.  Prefer the\r
2702         // generated pattern.\r
2703         if (usePat) {\r
2704             rebuiltPat.append(patBuf.toString());\r
2705         } else {\r
2706             _generatePattern(rebuiltPat, false, true);\r
2707         }\r
2708     }\r
2709 \r
2710     private static void syntaxError(RuleCharacterIterator chars, String msg) {\r
2711         throw new IllegalArgumentException("Error: " + msg + " at \"" +\r
2712                 Utility.escape(chars.toString()) +\r
2713         '"');\r
2714     }\r
2715 \r
2716     /**\r
2717      * Add the contents of the UnicodeSet (as strings) into a collection.\r
2718      * @param target collection to add into\r
2719      * @draft ICU 4.4\r
2720      * @provisional This API might change or be removed in a future release.\r
2721      */\r
2722     public <T extends Collection<String>> T addAllTo(T target) {\r
2723         return addAllTo(this, target);\r
2724     }\r
2725 \r
2726 \r
2727     /**\r
2728      * Add the contents of the UnicodeSet (as strings) into a collection.\r
2729      * @param target collection to add into\r
2730      * @draft ICU 4.4\r
2731      * @provisional This API might change or be removed in a future release.\r
2732      */\r
2733     public String[] addAllTo(String[] target) {\r
2734         return addAllTo(this, target);\r
2735     }\r
2736 \r
2737     /**\r
2738      * Add the contents of the UnicodeSet (as strings) into an array.\r
2739      * @draft ICU 4.4\r
2740      * @provisional This API might change or be removed in a future release.\r
2741      */\r
2742     public static String[] toArray(UnicodeSet set) {\r
2743         return addAllTo(set, new String[set.size()]);\r
2744     }\r
2745 \r
2746     /**\r
2747      * Add the contents of the collection (as strings) into this UnicodeSet.\r
2748      * @param source the collection to add\r
2749      * @return a reference to this object\r
2750      * @stable ICU 4.4\r
2751      */\r
2752     public UnicodeSet add(Collection<?> source) {\r
2753         return addAll(source);\r
2754     }\r
2755 \r
2756     /**\r
2757      * Add the contents of the UnicodeSet (as strings) into a collection.\r
2758      * Uses standard naming convention.\r
2759      * @param source collection to add into\r
2760      * @return a reference to this object\r
2761      * @stable ICU 4.4\r
2762      */\r
2763     public UnicodeSet addAll(Collection<?> source) {\r
2764         checkFrozen();\r
2765         for (Object o : source) {\r
2766             add(o.toString());\r
2767         }\r
2768         return this;\r
2769     }\r
2770 \r
2771     //----------------------------------------------------------------\r
2772     // Implementation: Utility methods\r
2773     //----------------------------------------------------------------\r
2774 \r
2775     private void ensureCapacity(int newLen) {\r
2776         if (newLen <= list.length) return;\r
2777         int[] temp = new int[newLen + GROW_EXTRA]; \r
2778         System.arraycopy(list, 0, temp, 0, len);\r
2779         list = temp;\r
2780     }\r
2781 \r
2782     private void ensureBufferCapacity(int newLen) {\r
2783         if (buffer != null && newLen <= buffer.length) return;\r
2784         buffer = new int[newLen + GROW_EXTRA];\r
2785     }\r
2786 \r
2787     /**\r
2788      * Assumes start <= end.\r
2789      */\r
2790     private int[] range(int start, int end) {\r
2791         if (rangeList == null) {\r
2792             rangeList = new int[] { start, end+1, HIGH };\r
2793         } else {\r
2794             rangeList[0] = start;\r
2795             rangeList[1] = end+1;\r
2796         }\r
2797         return rangeList;\r
2798     }\r
2799 \r
2800     //----------------------------------------------------------------\r
2801     // Implementation: Fundamental operations\r
2802     //----------------------------------------------------------------\r
2803 \r
2804     // polarity = 0, 3 is normal: x xor y\r
2805     // polarity = 1, 2: x xor ~y == x === y\r
2806 \r
2807     private UnicodeSet xor(int[] other, int otherLen, int polarity) {\r
2808         ensureBufferCapacity(len + otherLen);\r
2809         int i = 0, j = 0, k = 0;\r
2810         int a = list[i++];\r
2811         int b;\r
2812         // TODO: Based on the call hierarchy, polarity of 1 or 2 is never used\r
2813         //      so the following if statement will not be called.\r
2814         ///CLOVER:OFF\r
2815         if (polarity == 1 || polarity == 2) {\r
2816             b = LOW;\r
2817             if (other[j] == LOW) { // skip base if already LOW\r
2818                 ++j;\r
2819                 b = other[j];\r
2820             }\r
2821             ///CLOVER:ON\r
2822         } else {\r
2823             b = other[j++];\r
2824         }\r
2825         // simplest of all the routines\r
2826         // sort the values, discarding identicals!\r
2827         while (true) {\r
2828             if (a < b) {\r
2829                 buffer[k++] = a;\r
2830                 a = list[i++];\r
2831             } else if (b < a) {\r
2832                 buffer[k++] = b;\r
2833                 b = other[j++];\r
2834             } else if (a != HIGH) { // at this point, a == b\r
2835                 // discard both values!\r
2836                 a = list[i++];\r
2837                 b = other[j++];\r
2838             } else { // DONE!\r
2839                 buffer[k++] = HIGH;\r
2840                 len = k;\r
2841                 break;\r
2842             }\r
2843         }\r
2844         // swap list and buffer\r
2845         int[] temp = list;\r
2846         list = buffer;\r
2847         buffer = temp;\r
2848         pat = null;\r
2849         return this;\r
2850     }\r
2851 \r
2852     // polarity = 0 is normal: x union y\r
2853     // polarity = 2: x union ~y\r
2854     // polarity = 1: ~x union y\r
2855     // polarity = 3: ~x union ~y\r
2856 \r
2857     private UnicodeSet add(int[] other, int otherLen, int polarity) {\r
2858         ensureBufferCapacity(len + otherLen);\r
2859         int i = 0, j = 0, k = 0;\r
2860         int a = list[i++];\r
2861         int b = other[j++];\r
2862         // change from xor is that we have to check overlapping pairs\r
2863         // polarity bit 1 means a is second, bit 2 means b is.\r
2864         main:\r
2865             while (true) {\r
2866                 switch (polarity) {\r
2867                 case 0: // both first; take lower if unequal\r
2868                     if (a < b) { // take a\r
2869                         // Back up over overlapping ranges in buffer[]\r
2870                         if (k > 0 && a <= buffer[k-1]) {\r
2871                             // Pick latter end value in buffer[] vs. list[]\r
2872                             a = max(list[i], buffer[--k]);\r
2873                         } else {\r
2874                             // No overlap\r
2875                             buffer[k++] = a;\r
2876                             a = list[i];\r
2877                         }\r
2878                         i++; // Common if/else code factored out\r
2879                         polarity ^= 1;\r
2880                     } else if (b < a) { // take b\r
2881                         if (k > 0 && b <= buffer[k-1]) {\r
2882                             b = max(other[j], buffer[--k]);\r
2883                         } else {\r
2884                             buffer[k++] = b;\r
2885                             b = other[j];\r
2886                         }\r
2887                         j++;\r
2888                         polarity ^= 2;\r
2889                     } else { // a == b, take a, drop b\r
2890                         if (a == HIGH) break main;\r
2891                         // This is symmetrical; it doesn't matter if\r
2892                         // we backtrack with a or b. - liu\r
2893                         if (k > 0 && a <= buffer[k-1]) {\r
2894                             a = max(list[i], buffer[--k]);\r
2895                         } else {\r
2896                             // No overlap\r
2897                             buffer[k++] = a;\r
2898                             a = list[i];\r
2899                         }\r
2900                         i++;\r
2901                         polarity ^= 1;\r
2902                         b = other[j++]; polarity ^= 2;\r
2903                     }\r
2904                     break;\r
2905                 case 3: // both second; take higher if unequal, and drop other\r
2906                     if (b <= a) { // take a\r
2907                         if (a == HIGH) break main;\r
2908                         buffer[k++] = a;\r
2909                     } else { // take b\r
2910                         if (b == HIGH) break main;\r
2911                         buffer[k++] = b;\r
2912                     }\r
2913                     a = list[i++]; polarity ^= 1;   // factored common code\r
2914                     b = other[j++]; polarity ^= 2;\r
2915                     break;\r
2916                 case 1: // a second, b first; if b < a, overlap\r
2917                     if (a < b) { // no overlap, take a\r
2918                         buffer[k++] = a; a = list[i++]; polarity ^= 1;\r
2919                     } else if (b < a) { // OVERLAP, drop b\r
2920                         b = other[j++]; polarity ^= 2;\r
2921                     } else { // a == b, drop both!\r
2922                         if (a == HIGH) break main;\r
2923                         a = list[i++]; polarity ^= 1;\r
2924                         b = other[j++]; polarity ^= 2;\r
2925                     }\r
2926                     break;\r
2927                 case 2: // a first, b second; if a < b, overlap\r
2928                     if (b < a) { // no overlap, take b\r
2929                         buffer[k++] = b; b = other[j++]; polarity ^= 2;\r
2930                     } else  if (a < b) { // OVERLAP, drop a\r
2931                         a = list[i++]; polarity ^= 1;\r
2932                     } else { // a == b, drop both!\r
2933                         if (a == HIGH) break main;\r
2934                         a = list[i++]; polarity ^= 1;\r
2935                         b = other[j++]; polarity ^= 2;\r
2936                     }\r
2937                     break;\r
2938                 }\r
2939             }\r
2940         buffer[k++] = HIGH;    // terminate\r
2941         len = k;\r
2942         // swap list and buffer\r
2943         int[] temp = list;\r
2944         list = buffer;\r
2945         buffer = temp;\r
2946         pat = null;\r
2947         return this;\r
2948     }\r
2949 \r
2950     // polarity = 0 is normal: x intersect y\r
2951     // polarity = 2: x intersect ~y == set-minus\r
2952     // polarity = 1: ~x intersect y\r
2953     // polarity = 3: ~x intersect ~y\r
2954 \r
2955     private UnicodeSet retain(int[] other, int otherLen, int polarity) {\r
2956         ensureBufferCapacity(len + otherLen);\r
2957         int i = 0, j = 0, k = 0;\r
2958         int a = list[i++];\r
2959         int b = other[j++];\r
2960         // change from xor is that we have to check overlapping pairs\r
2961         // polarity bit 1 means a is second, bit 2 means b is.\r
2962         main:\r
2963             while (true) {\r
2964                 switch (polarity) {\r
2965                 case 0: // both first; drop the smaller\r
2966                     if (a < b) { // drop a\r
2967                         a = list[i++]; polarity ^= 1;\r
2968                     } else if (b < a) { // drop b\r
2969                         b = other[j++]; polarity ^= 2;\r
2970                     } else { // a == b, take one, drop other\r
2971                         if (a == HIGH) break main;\r
2972                         buffer[k++] = a; a = list[i++]; polarity ^= 1;\r
2973                         b = other[j++]; polarity ^= 2;\r
2974                     }\r
2975                     break;\r
2976                 case 3: // both second; take lower if unequal\r
2977                     if (a < b) { // take a\r
2978                         buffer[k++] = a; a = list[i++]; polarity ^= 1;\r
2979                     } else if (b < a) { // take b\r
2980                         buffer[k++] = b; b = other[j++]; polarity ^= 2;\r
2981                     } else { // a == b, take one, drop other\r
2982                         if (a == HIGH) break main;\r
2983                         buffer[k++] = a; a = list[i++]; polarity ^= 1;\r
2984                         b = other[j++]; polarity ^= 2;\r
2985                     }\r
2986                     break;\r
2987                 case 1: // a second, b first;\r
2988                     if (a < b) { // NO OVERLAP, drop a\r
2989                         a = list[i++]; polarity ^= 1;\r
2990                     } else if (b < a) { // OVERLAP, take b\r
2991                         buffer[k++] = b; b = other[j++]; polarity ^= 2;\r
2992                     } else { // a == b, drop both!\r
2993                         if (a == HIGH) break main;\r
2994                         a = list[i++]; polarity ^= 1;\r
2995                         b = other[j++]; polarity ^= 2;\r
2996                     }\r
2997                     break;\r
2998                 case 2: // a first, b second; if a < b, overlap\r
2999                     if (b < a) { // no overlap, drop b\r
3000                         b = other[j++]; polarity ^= 2;\r
3001                     } else  if (a < b) { // OVERLAP, take a\r
3002                         buffer[k++] = a; a = list[i++]; polarity ^= 1;\r
3003                     } else { // a == b, drop both!\r
3004                         if (a == HIGH) break main;\r
3005                         a = list[i++]; polarity ^= 1;\r
3006                         b = other[j++]; polarity ^= 2;\r
3007                     }\r
3008                     break;\r
3009                 }\r
3010             }\r
3011         buffer[k++] = HIGH;    // terminate\r
3012         len = k;\r
3013         // swap list and buffer\r
3014         int[] temp = list;\r
3015         list = buffer;\r
3016         buffer = temp;\r
3017         pat = null;\r
3018         return this;\r
3019     }\r
3020 \r
3021     private static final int max(int a, int b) {\r
3022         return (a > b) ? a : b;\r
3023     }\r
3024 \r
3025     //----------------------------------------------------------------\r
3026     // Generic filter-based scanning code\r
3027     //----------------------------------------------------------------\r
3028 \r
3029     private static interface Filter {\r
3030         boolean contains(int codePoint);\r
3031     }\r
3032 \r
3033     private static class NumericValueFilter implements Filter {\r
3034         double value;\r
3035         NumericValueFilter(double value) { this.value = value; }\r
3036         public boolean contains(int ch) {\r
3037             return UCharacter.getUnicodeNumericValue(ch) == value;\r
3038         }\r
3039     }\r
3040 \r
3041     private static class GeneralCategoryMaskFilter implements Filter {\r
3042         int mask;\r
3043         GeneralCategoryMaskFilter(int mask) { this.mask = mask; }\r
3044         public boolean contains(int ch) {\r
3045             return ((1 << UCharacter.getType(ch)) & mask) != 0;\r
3046         }\r
3047     }\r
3048 \r
3049     private static class IntPropertyFilter implements Filter {\r
3050         int prop;\r
3051         int value;\r
3052         IntPropertyFilter(int prop, int value) {\r
3053             this.prop = prop;\r
3054             this.value = value;\r
3055         }\r
3056         public boolean contains(int ch) {\r
3057             return UCharacter.getIntPropertyValue(ch, prop) == value;\r
3058         }\r
3059     }\r
3060 \r
3061     // VersionInfo for unassigned characters\r
3062     static final VersionInfo NO_VERSION = VersionInfo.getInstance(0, 0, 0, 0);\r
3063 \r
3064     private static class VersionFilter implements Filter {\r
3065         VersionInfo version;\r
3066         VersionFilter(VersionInfo version) { this.version = version; }\r
3067         public boolean contains(int ch) {\r
3068             VersionInfo v = UCharacter.getAge(ch);\r
3069             // Reference comparison ok; VersionInfo caches and reuses\r
3070             // unique objects.\r
3071             return v != NO_VERSION &&\r
3072             v.compareTo(version) <= 0;\r
3073         }\r
3074     }\r
3075 \r
3076     private static synchronized UnicodeSet getInclusions(int src) {\r
3077         if (INCLUSIONS == null) {\r
3078             INCLUSIONS = new UnicodeSet[UCharacterProperty.SRC_COUNT];\r
3079         }\r
3080         if(INCLUSIONS[src] == null) {\r
3081             UnicodeSet incl = new UnicodeSet();\r
3082             try {\r
3083                 switch(src) {\r
3084                 case UCharacterProperty.SRC_CHAR:\r
3085                     UCharacterProperty.INSTANCE.addPropertyStarts(incl);\r
3086                     break;\r
3087                 case UCharacterProperty.SRC_PROPSVEC:\r
3088                     UCharacterProperty.INSTANCE.upropsvec_addPropertyStarts(incl);\r
3089                     break;\r
3090                 case UCharacterProperty.SRC_CHAR_AND_PROPSVEC:\r
3091                     UCharacterProperty.INSTANCE.addPropertyStarts(incl);\r
3092                     UCharacterProperty.INSTANCE.upropsvec_addPropertyStarts(incl);\r
3093                     break;\r
3094                 case UCharacterProperty.SRC_CASE_AND_NORM:\r
3095                     Norm2AllModes.getNFCInstance().impl.addPropertyStarts(incl);\r
3096                     UCaseProps.getSingleton().addPropertyStarts(incl);\r
3097                     break;\r
3098                 case UCharacterProperty.SRC_NFC:\r
3099                     Norm2AllModes.getNFCInstance().impl.addPropertyStarts(incl);\r
3100                     break;\r
3101                 case UCharacterProperty.SRC_NFKC:\r
3102                     Norm2AllModes.getNFKCInstance().impl.addPropertyStarts(incl);\r
3103                     break;\r
3104                 case UCharacterProperty.SRC_NFKC_CF:\r
3105                     Norm2AllModes.getNFKC_CFInstance().impl.addPropertyStarts(incl);\r
3106                     break;\r
3107                 case UCharacterProperty.SRC_NFC_CANON_ITER:\r
3108                     Norm2AllModes.getNFCInstance().impl.addCanonIterPropertyStarts(incl);\r
3109                     break;\r
3110                 case UCharacterProperty.SRC_CASE:\r
3111                     UCaseProps.getSingleton().addPropertyStarts(incl);\r
3112                     break;\r
3113                 case UCharacterProperty.SRC_BIDI:\r
3114                     UBiDiProps.getSingleton().addPropertyStarts(incl);\r
3115                     break;\r
3116                 default:\r
3117                     throw new IllegalStateException("UnicodeSet.getInclusions(unknown src "+src+")");\r
3118                 }\r
3119             } catch(IOException e) {\r
3120                 throw new MissingResourceException(e.getMessage(),"","");\r
3121             }\r
3122             INCLUSIONS[src] = incl;\r
3123         }\r
3124         return INCLUSIONS[src];\r
3125     }\r
3126 \r
3127     /**\r
3128      * Generic filter-based scanning code for UCD property UnicodeSets.\r
3129      */\r
3130     private UnicodeSet applyFilter(Filter filter, int src) {\r
3131         // Walk through all Unicode characters, noting the start\r
3132         // and end of each range for which filter.contain(c) is\r
3133         // true.  Add each range to a set.\r
3134         //\r
3135         // To improve performance, use the INCLUSIONS set, which\r
3136         // encodes information about character ranges that are known\r
3137         // to have identical properties, such as the CJK Ideographs\r
3138         // from U+4E00 to U+9FA5.  INCLUSIONS contains all characters\r
3139         // except the first characters of such ranges.\r
3140         //\r
3141         // TODO Where possible, instead of scanning over code points,\r
3142         // use internal property data to initialize UnicodeSets for\r
3143         // those properties.  Scanning code points is slow.\r
3144 \r
3145         clear();\r
3146 \r
3147         int startHasProperty = -1;\r
3148         UnicodeSet inclusions = getInclusions(src);\r
3149         int limitRange = inclusions.getRangeCount();\r
3150 \r
3151         for (int j=0; j<limitRange; ++j) {\r
3152             // get current range\r
3153             int start = inclusions.getRangeStart(j);\r
3154             int end = inclusions.getRangeEnd(j);\r
3155 \r
3156             // for all the code points in the range, process\r
3157             for (int ch = start; ch <= end; ++ch) {\r
3158                 // only add to the unicodeset on inflection points --\r
3159                 // where the hasProperty value changes to false\r
3160                 if (filter.contains(ch)) {\r
3161                     if (startHasProperty < 0) {\r
3162                         startHasProperty = ch;\r
3163                     }\r
3164                 } else if (startHasProperty >= 0) {\r
3165                     add_unchecked(startHasProperty, ch-1);\r
3166                     startHasProperty = -1;\r
3167                 }\r
3168             }\r
3169         }\r
3170         if (startHasProperty >= 0) {\r
3171             add_unchecked(startHasProperty, 0x10FFFF);\r
3172         }\r
3173 \r
3174         return this;\r
3175     }\r
3176 \r
3177 \r
3178     /**\r
3179      * Remove leading and trailing rule white space and compress\r
3180      * internal rule white space to a single space character.\r
3181      *\r
3182      * @see UCharacterProperty#isRuleWhiteSpace\r
3183      */\r
3184     private static String mungeCharName(String source) {\r
3185         StringBuffer buf = new StringBuffer();\r
3186         for (int i=0; i<source.length(); ) {\r
3187             int ch = UTF16.charAt(source, i);\r
3188             i += UTF16.getCharCount(ch);\r
3189             if (UCharacterProperty.isRuleWhiteSpace(ch)) {\r
3190                 if (buf.length() == 0 ||\r
3191                         buf.charAt(buf.length() - 1) == ' ') {\r
3192                     continue;\r
3193                 }\r
3194                 ch = ' '; // convert to ' '\r
3195             }\r
3196             UTF16.append(buf, ch);\r
3197         }\r
3198         if (buf.length() != 0 &&\r
3199                 buf.charAt(buf.length() - 1) == ' ') {\r
3200             buf.setLength(buf.length() - 1);\r
3201         }\r
3202         return buf.toString();\r
3203     }\r
3204 \r
3205     //----------------------------------------------------------------\r
3206     // Property set API\r
3207     //----------------------------------------------------------------\r
3208 \r
3209     /**\r
3210      * Modifies this set to contain those code points which have the\r
3211      * given value for the given binary or enumerated property, as\r
3212      * returned by UCharacter.getIntPropertyValue.  Prior contents of\r
3213      * this set are lost.\r
3214      *\r
3215      * @param prop a property in the range\r
3216      * UProperty.BIN_START..UProperty.BIN_LIMIT-1 or\r
3217      * UProperty.INT_START..UProperty.INT_LIMIT-1 or.\r
3218      * UProperty.MASK_START..UProperty.MASK_LIMIT-1.\r
3219      *\r
3220      * @param value a value in the range\r
3221      * UCharacter.getIntPropertyMinValue(prop)..\r
3222      * UCharacter.getIntPropertyMaxValue(prop), with one exception.\r
3223      * If prop is UProperty.GENERAL_CATEGORY_MASK, then value should not be\r
3224      * a UCharacter.getType() result, but rather a mask value produced\r
3225      * by logically ORing (1 << UCharacter.getType()) values together.\r
3226      * This allows grouped categories such as [:L:] to be represented.\r
3227      *\r
3228      * @return a reference to this set\r
3229      *\r
3230      * @stable ICU 2.4\r
3231      */\r
3232     public UnicodeSet applyIntPropertyValue(int prop, int value) {\r
3233         checkFrozen();\r
3234         if (prop == UProperty.GENERAL_CATEGORY_MASK) {\r
3235             applyFilter(new GeneralCategoryMaskFilter(value), UCharacterProperty.SRC_CHAR);\r
3236         } else {\r
3237             applyFilter(new IntPropertyFilter(prop, value), UCharacterProperty.INSTANCE.getSource(prop));\r
3238         }\r
3239         return this;\r
3240     }\r
3241 \r
3242 \r
3243 \r
3244     /**\r
3245      * Modifies this set to contain those code points which have the\r
3246      * given value for the given property.  Prior contents of this\r
3247      * set are lost.\r
3248      *\r
3249      * @param propertyAlias a property alias, either short or long.\r
3250      * The name is matched loosely.  See PropertyAliases.txt for names\r
3251      * and a description of loose matching.  If the value string is\r
3252      * empty, then this string is interpreted as either a\r
3253      * General_Category value alias, a Script value alias, a binary\r
3254      * property alias, or a special ID.  Special IDs are matched\r
3255      * loosely and correspond to the following sets:\r
3256      *\r
3257      * "ANY" = [\u0000-\U0010FFFF],\r
3258      * "ASCII" = [\u0000-\u007F].\r
3259      *\r
3260      * @param valueAlias a value alias, either short or long.  The\r
3261      * name is matched loosely.  See PropertyValueAliases.txt for\r
3262      * names and a description of loose matching.  In addition to\r
3263      * aliases listed, numeric values and canonical combining classes\r
3264      * may be expressed numerically, e.g., ("nv", "0.5") or ("ccc",\r
3265      * "220").  The value string may also be empty.\r
3266      *\r
3267      * @return a reference to this set\r
3268      *\r
3269      * @stable ICU 2.4\r
3270      */\r
3271     public UnicodeSet applyPropertyAlias(String propertyAlias, String valueAlias) {\r
3272         return applyPropertyAlias(propertyAlias, valueAlias, null);\r
3273     }\r
3274 \r
3275     /**\r
3276      * Modifies this set to contain those code points which have the\r
3277      * given value for the given property.  Prior contents of this\r
3278      * set are lost.\r
3279      * @param propertyAlias A string of the property alias.\r
3280      * @param valueAlias A string of the value alias.\r
3281      * @param symbols if not null, then symbols are first called to see if a property\r
3282      * is available. If true, then everything else is skipped.\r
3283      * @return this set\r
3284      * @stable ICU 3.2\r
3285      */\r
3286     public UnicodeSet applyPropertyAlias(String propertyAlias,\r
3287             String valueAlias, SymbolTable symbols) {\r
3288         checkFrozen();\r
3289         int p;\r
3290         int v;\r
3291         boolean mustNotBeEmpty = false, invert = false;\r
3292 \r
3293         if (symbols != null\r
3294                 && (symbols instanceof XSymbolTable)\r
3295                 && ((XSymbolTable)symbols).applyPropertyAlias(propertyAlias, valueAlias, this)) {\r
3296             return this;\r
3297         }\r
3298 \r
3299         if (valueAlias.length() > 0) {\r
3300             p = UCharacter.getPropertyEnum(propertyAlias);\r
3301 \r
3302             // Treat gc as gcm\r
3303             if (p == UProperty.GENERAL_CATEGORY) {\r
3304                 p = UProperty.GENERAL_CATEGORY_MASK;\r
3305             }\r
3306 \r
3307             if ((p >= UProperty.BINARY_START && p < UProperty.BINARY_LIMIT) ||\r
3308                     (p >= UProperty.INT_START && p < UProperty.INT_LIMIT) ||\r
3309                     (p >= UProperty.MASK_START && p < UProperty.MASK_LIMIT)) {\r
3310                 try {\r
3311                     v = UCharacter.getPropertyValueEnum(p, valueAlias);\r
3312                 } catch (IllegalArgumentException e) {\r
3313                     // Handle numeric CCC\r
3314                     if (p == UProperty.CANONICAL_COMBINING_CLASS ||\r
3315                             p == UProperty.LEAD_CANONICAL_COMBINING_CLASS ||\r
3316                             p == UProperty.TRAIL_CANONICAL_COMBINING_CLASS) {\r
3317                         v = Integer.parseInt(Utility.deleteRuleWhiteSpace(valueAlias));\r
3318                         // If the resultant set is empty then the numeric value\r
3319                         // was invalid.\r
3320                         //mustNotBeEmpty = true;\r
3321                         // old code was wrong; anything between 0 and 255 is valid even if unused.\r
3322                         if (v < 0 || v > 255) throw e;\r
3323                     } else {\r
3324                         throw e;\r
3325                     }\r
3326                 }\r
3327             }\r
3328 \r
3329             else {\r
3330 \r
3331                 switch (p) {\r
3332                 case UProperty.NUMERIC_VALUE:\r
3333                 {\r
3334                     double value = Double.parseDouble(Utility.deleteRuleWhiteSpace(valueAlias));\r
3335                     applyFilter(new NumericValueFilter(value), UCharacterProperty.SRC_CHAR);\r
3336                     return this;\r
3337                 }\r
3338                 case UProperty.NAME:\r
3339                 case UProperty.UNICODE_1_NAME:\r
3340                 {\r
3341                     // Must munge name, since\r
3342                     // UCharacter.charFromName() does not do\r
3343                     // 'loose' matching.\r
3344                     String buf = mungeCharName(valueAlias);\r
3345                     int ch =\r
3346                         (p == UProperty.NAME) ?\r
3347                                 UCharacter.getCharFromExtendedName(buf) :\r
3348                                     UCharacter.getCharFromName1_0(buf);\r
3349                                 if (ch == -1) {\r
3350                                     throw new IllegalArgumentException("Invalid character name");\r
3351                                 }\r
3352                                 clear();\r
3353                                 add_unchecked(ch);\r
3354                                 return this;\r
3355                 }\r
3356                 case UProperty.AGE:\r
3357                 {\r
3358                     // Must munge name, since\r
3359                     // VersionInfo.getInstance() does not do\r
3360                     // 'loose' matching.\r
3361                     VersionInfo version = VersionInfo.getInstance(mungeCharName(valueAlias));\r
3362                     applyFilter(new VersionFilter(version), UCharacterProperty.SRC_PROPSVEC);\r
3363                     return this;\r
3364                 }\r
3365                 }\r
3366 \r
3367                 // p is a non-binary, non-enumerated property that we\r
3368                 // don't support (yet).\r
3369                 throw new IllegalArgumentException("Unsupported property");\r
3370             }\r
3371         }\r
3372 \r
3373         else {\r
3374             // valueAlias is empty.  Interpret as General Category, Script,\r
3375             // Binary property, or ANY or ASCII.  Upon success, p and v will\r
3376             // be set.\r
3377             UPropertyAliases pnames = UPropertyAliases.INSTANCE;\r
3378             p = UProperty.GENERAL_CATEGORY_MASK;\r
3379             v = pnames.getPropertyValueEnum(p, propertyAlias);\r
3380             if (v == UProperty.UNDEFINED) {\r
3381                 p = UProperty.SCRIPT;\r
3382                 v = pnames.getPropertyValueEnum(p, propertyAlias);\r
3383                 if (v == UProperty.UNDEFINED) {\r
3384                     p = pnames.getPropertyEnum(propertyAlias);\r
3385                     if (p == UProperty.UNDEFINED) {\r
3386                         p = -1;\r
3387                     }\r
3388                     if (p >= UProperty.BINARY_START && p < UProperty.BINARY_LIMIT) {\r
3389                         v = 1;\r
3390                     } else if (p == -1) {\r
3391                         if (0 == UPropertyAliases.compare(ANY_ID, propertyAlias)) {\r
3392                             set(MIN_VALUE, MAX_VALUE);\r
3393                             return this;\r
3394                         } else if (0 == UPropertyAliases.compare(ASCII_ID, propertyAlias)) {\r
3395                             set(0, 0x7F);\r
3396                             return this;\r
3397                         } else if (0 == UPropertyAliases.compare(ASSIGNED, propertyAlias)) {\r
3398                             // [:Assigned:]=[:^Cn:]\r
3399                             p = UProperty.GENERAL_CATEGORY_MASK;\r
3400                             v = (1<<UCharacter.UNASSIGNED);\r
3401                             invert = true;\r
3402                         } else {\r
3403                             // Property name was never matched.\r
3404                             throw new IllegalArgumentException("Invalid property alias: " + propertyAlias + "=" + valueAlias);\r
3405                         }\r
3406                     } else {\r
3407                         // Valid propery name, but it isn't binary, so the value\r
3408                         // must be supplied.\r
3409                         throw new IllegalArgumentException("Missing property value");\r
3410                     }\r
3411                 }\r
3412             }\r
3413         }\r
3414 \r
3415         applyIntPropertyValue(p, v);\r
3416         if(invert) {\r
3417             complement();\r
3418         }\r
3419 \r
3420         if (mustNotBeEmpty && isEmpty()) {\r
3421             // mustNotBeEmpty is set to true if an empty set indicates\r
3422             // invalid input.\r
3423             throw new IllegalArgumentException("Invalid property value");\r
3424         }\r
3425 \r
3426         return this;\r
3427     }\r
3428 \r
3429     //----------------------------------------------------------------\r
3430     // Property set patterns\r
3431     //----------------------------------------------------------------\r
3432 \r
3433     /**\r
3434      * Return true if the given position, in the given pattern, appears\r
3435      * to be the start of a property set pattern.\r
3436      */\r
3437     private static boolean resemblesPropertyPattern(String pattern, int pos) {\r
3438         // Patterns are at least 5 characters long\r
3439         if ((pos+5) > pattern.length()) {\r
3440             return false;\r
3441         }\r
3442 \r
3443         // Look for an opening [:, [:^, \p, or \P\r
3444         return pattern.regionMatches(pos, "[:", 0, 2) ||\r
3445         pattern.regionMatches(true, pos, "\\p", 0, 2) ||\r
3446         pattern.regionMatches(pos, "\\N", 0, 2);\r
3447     }\r
3448 \r
3449     /**\r
3450      * Return true if the given iterator appears to point at a\r
3451      * property pattern.  Regardless of the result, return with the\r
3452      * iterator unchanged.\r
3453      * @param chars iterator over the pattern characters.  Upon return\r
3454      * it will be unchanged.\r
3455      * @param iterOpts RuleCharacterIterator options\r
3456      */\r
3457     private static boolean resemblesPropertyPattern(RuleCharacterIterator chars,\r
3458             int iterOpts) {\r
3459         boolean result = false;\r
3460         iterOpts &= ~RuleCharacterIterator.PARSE_ESCAPES;\r
3461         Object pos = chars.getPos(null);\r
3462         int c = chars.next(iterOpts);\r
3463         if (c == '[' || c == '\\') {\r
3464             int d = chars.next(iterOpts & ~RuleCharacterIterator.SKIP_WHITESPACE);\r
3465             result = (c == '[') ? (d == ':') :\r
3466                 (d == 'N' || d == 'p' || d == 'P');\r
3467         }\r
3468         chars.setPos(pos);\r
3469         return result;\r
3470     }\r
3471 \r
3472     /**\r
3473      * Parse the given property pattern at the given parse position.\r
3474      * @param symbols TODO\r
3475      */\r
3476     private UnicodeSet applyPropertyPattern(String pattern, ParsePosition ppos, SymbolTable symbols) {\r
3477         int pos = ppos.getIndex();\r
3478 \r
3479         // On entry, ppos should point to one of the following locations:\r
3480 \r
3481         // Minimum length is 5 characters, e.g. \p{L}\r
3482         if ((pos+5) > pattern.length()) {\r
3483             return null;\r
3484         }\r
3485 \r
3486         boolean posix = false; // true for [:pat:], false for \p{pat} \P{pat} \N{pat}\r
3487         boolean isName = false; // true for \N{pat}, o/w false\r
3488         boolean invert = false;\r
3489 \r
3490         // Look for an opening [:, [:^, \p, or \P\r
3491         if (pattern.regionMatches(pos, "[:", 0, 2)) {\r
3492             posix = true;\r
3493             pos = Utility.skipWhitespace(pattern, pos+2);\r
3494             if (pos < pattern.length() && pattern.charAt(pos) == '^') {\r
3495                 ++pos;\r
3496                 invert = true;\r
3497             }\r
3498         } else if (pattern.regionMatches(true, pos, "\\p", 0, 2) ||\r
3499                 pattern.regionMatches(pos, "\\N", 0, 2)) {\r
3500             char c = pattern.charAt(pos+1);\r
3501             invert = (c == 'P');\r
3502             isName = (c == 'N');\r
3503             pos = Utility.skipWhitespace(pattern, pos+2);\r
3504             if (pos == pattern.length() || pattern.charAt(pos++) != '{') {\r
3505                 // Syntax error; "\p" or "\P" not followed by "{"\r
3506                 return null;\r
3507             }\r
3508         } else {\r
3509             // Open delimiter not seen\r
3510             return null;\r
3511         }\r
3512 \r
3513         // Look for the matching close delimiter, either :] or }\r
3514         int close = pattern.indexOf(posix ? ":]" : "}", pos);\r
3515         if (close < 0) {\r
3516             // Syntax error; close delimiter missing\r
3517             return null;\r
3518         }\r
3519 \r
3520         // Look for an '=' sign.  If this is present, we will parse a\r
3521         // medium \p{gc=Cf} or long \p{GeneralCategory=Format}\r
3522         // pattern.\r
3523         int equals = pattern.indexOf('=', pos);\r
3524         String propName, valueName;\r
3525         if (equals >= 0 && equals < close && !isName) {\r
3526             // Equals seen; parse medium/long pattern\r
3527             propName = pattern.substring(pos, equals);\r
3528             valueName = pattern.substring(equals+1, close);\r
3529         }\r
3530 \r
3531         else {\r
3532             // Handle case where no '=' is seen, and \N{}\r
3533             propName = pattern.substring(pos, close);\r
3534             valueName = "";\r
3535 \r
3536             // Handle \N{name}\r
3537             if (isName) {\r
3538                 // This is a little inefficient since it means we have to\r
3539                 // parse "na" back to UProperty.NAME even though we already\r
3540                 // know it's UProperty.NAME.  If we refactor the API to\r
3541                 // support args of (int, String) then we can remove\r
3542                 // "na" and make this a little more efficient.\r
3543                 valueName = propName;\r
3544                 propName = "na";\r
3545             }\r
3546         }\r
3547 \r
3548         applyPropertyAlias(propName, valueName, symbols);\r
3549 \r
3550         if (invert) {\r
3551             complement();\r
3552         }\r
3553 \r
3554         // Move to the limit position after the close delimiter\r
3555         ppos.setIndex(close + (posix ? 2 : 1));\r
3556 \r
3557         return this;\r
3558     }\r
3559 \r
3560     /**\r
3561      * Parse a property pattern.\r
3562      * @param chars iterator over the pattern characters.  Upon return\r
3563      * it will be advanced to the first character after the parsed\r
3564      * pattern, or the end of the iteration if all characters are\r
3565      * parsed.\r
3566      * @param rebuiltPat the pattern that was parsed, rebuilt or\r
3567      * copied from the input pattern, as appropriate.\r
3568      * @param symbols TODO\r
3569      */\r
3570     private void applyPropertyPattern(RuleCharacterIterator chars,\r
3571             StringBuffer rebuiltPat, SymbolTable symbols) {\r
3572         String patStr = chars.lookahead();\r
3573         ParsePosition pos = new ParsePosition(0);\r
3574         applyPropertyPattern(patStr, pos, symbols);\r
3575         if (pos.getIndex() == 0) {\r
3576             syntaxError(chars, "Invalid property pattern");\r
3577         }\r
3578         chars.jumpahead(pos.getIndex());\r
3579         rebuiltPat.append(patStr.substring(0, pos.getIndex()));\r
3580     }\r
3581 \r
3582     //----------------------------------------------------------------\r
3583     // Case folding API\r
3584     //----------------------------------------------------------------\r
3585 \r
3586     /**\r
3587      * Bitmask for constructor and applyPattern() indicating that\r
3588      * white space should be ignored.  If set, ignore characters for\r
3589      * which UCharacterProperty.isRuleWhiteSpace() returns true,\r
3590      * unless they are quoted or escaped.  This may be ORed together\r
3591      * with other selectors.\r
3592      * @stable ICU 3.8\r
3593      */\r
3594     public static final int IGNORE_SPACE = 1;\r
3595 \r
3596     /**\r
3597      * Bitmask for constructor, applyPattern(), and closeOver()\r
3598      * indicating letter case.  This may be ORed together with other\r
3599      * selectors.\r
3600      *\r
3601      * Enable case insensitive matching.  E.g., "[ab]" with this flag\r
3602      * will match 'a', 'A', 'b', and 'B'.  "[^ab]" with this flag will\r
3603      * match all except 'a', 'A', 'b', and 'B'. This performs a full\r
3604      * closure over case mappings, e.g. U+017F for s.\r
3605      *\r
3606      * The resulting set is a superset of the input for the code points but\r
3607      * not for the strings.\r
3608      * It performs a case mapping closure of the code points and adds\r
3609      * full case folding strings for the code points, and reduces strings of\r
3610      * the original set to their full case folding equivalents.\r
3611      *\r
3612      * This is designed for case-insensitive matches, for example\r
3613      * in regular expressions. The full code point case closure allows checking of\r
3614      * an input character directly against the closure set.\r
3615      * Strings are matched by comparing the case-folded form from the closure\r
3616      * set with an incremental case folding of the string in question.\r
3617      *\r
3618      * The closure set will also contain single code points if the original\r
3619      * set contained case-equivalent strings (like U+00DF for "ss" or "Ss" etc.).\r
3620      * This is not necessary (that is, redundant) for the above matching method\r
3621      * but results in the same closure sets regardless of whether the original\r
3622      * set contained the code point or a string.\r
3623      * @stable ICU 3.8\r
3624      */\r
3625     public static final int CASE = 2;\r
3626 \r
3627     /**\r
3628      * Alias for UnicodeSet.CASE, for ease of porting from C++ where ICU4C\r
3629      * also has both USET_CASE and USET_CASE_INSENSITIVE (see uset.h).\r
3630      * @see #CASE\r
3631      * @stable ICU 3.4\r
3632      */\r
3633     public static final int CASE_INSENSITIVE = 2;\r
3634 \r
3635     /**\r
3636      * Bitmask for constructor, applyPattern(), and closeOver()\r
3637      * indicating letter case.  This may be ORed together with other\r
3638      * selectors.\r
3639      *\r
3640      * Enable case insensitive matching.  E.g., "[ab]" with this flag\r
3641      * will match 'a', 'A', 'b', and 'B'.  "[^ab]" with this flag will\r
3642      * match all except 'a', 'A', 'b', and 'B'. This adds the lower-,\r
3643      * title-, and uppercase mappings as well as the case folding\r
3644      * of each existing element in the set.\r
3645      * @stable ICU 3.4\r
3646      */\r
3647     public static final int ADD_CASE_MAPPINGS = 4;\r
3648 \r
3649     //  add the result of a full case mapping to the set\r
3650     //  use str as a temporary string to avoid constructing one\r
3651     private static final void addCaseMapping(UnicodeSet set, int result, StringBuffer full) {\r
3652         if(result >= 0) {\r
3653             if(result > UCaseProps.MAX_STRING_LENGTH) {\r
3654                 // add a single-code point case mapping\r
3655                 set.add(result);\r
3656             } else {\r
3657                 // add a string case mapping from full with length result\r
3658                 set.add(full.toString());\r
3659                 full.setLength(0);\r
3660             }\r
3661         }\r
3662         // result < 0: the code point mapped to itself, no need to add it\r
3663         // see UCaseProps\r
3664     }\r
3665 \r
3666     /**\r
3667      * Close this set over the given attribute.  For the attribute\r
3668      * CASE, the result is to modify this set so that:\r
3669      *\r
3670      * 1. For each character or string 'a' in this set, all strings\r
3671      * 'b' such that foldCase(a) == foldCase(b) are added to this set.\r
3672      * (For most 'a' that are single characters, 'b' will have\r
3673      * b.length() == 1.)\r
3674      *\r
3675      * 2. For each string 'e' in the resulting set, if e !=\r
3676      * foldCase(e), 'e' will be removed.\r
3677      *\r
3678      * Example: [aq\u00DF{Bc}{bC}{Fi}] => [aAqQ\u00DF\uFB01{ss}{bc}{fi}]\r
3679      *\r
3680      * (Here foldCase(x) refers to the operation\r
3681      * UCharacter.foldCase(x, true), and a == b actually denotes\r
3682      * a.equals(b), not pointer comparison.)\r
3683      *\r
3684      * @param attribute bitmask for attributes to close over.\r
3685      * Currently only the CASE bit is supported.  Any undefined bits\r
3686      * are ignored.\r
3687      * @return a reference to this set.\r
3688      * @stable ICU 3.8\r
3689      */\r
3690     public UnicodeSet closeOver(int attribute) {\r
3691         checkFrozen();\r
3692         if ((attribute & (CASE | ADD_CASE_MAPPINGS)) != 0) {\r
3693             UCaseProps csp;\r
3694             try {\r
3695                 csp = UCaseProps.getSingleton();\r
3696             } catch(IOException e) {\r
3697                 return this;\r
3698             }\r
3699             UnicodeSet foldSet = new UnicodeSet(this);\r
3700             ULocale root = ULocale.ROOT;\r
3701 \r
3702             // start with input set to guarantee inclusion\r
3703             // CASE: remove strings because the strings will actually be reduced (folded);\r
3704             //       therefore, start with no strings and add only those needed\r
3705             if((attribute & CASE) != 0) {\r
3706                 foldSet.strings.clear();\r
3707             }\r
3708 \r
3709             int n = getRangeCount();\r
3710             int result;\r
3711             StringBuffer full = new StringBuffer();\r
3712             int locCache[] = new int[1];\r
3713 \r
3714             for (int i=0; i<n; ++i) {\r
3715                 int start = getRangeStart(i);\r
3716                 int end   = getRangeEnd(i);\r
3717 \r
3718                 if((attribute & CASE) != 0) {\r
3719                     // full case closure\r
3720                     for (int cp=start; cp<=end; ++cp) {\r
3721                         csp.addCaseClosure(cp, foldSet);\r
3722                     }\r
3723                 } else {\r
3724                     // add case mappings\r
3725                     // (does not add long s for regular s, or Kelvin for k, for example)\r
3726                     for (int cp=start; cp<=end; ++cp) {\r
3727                         result = csp.toFullLower(cp, null, full, root, locCache);\r
3728                         addCaseMapping(foldSet, result, full);\r
3729 \r
3730                         result = csp.toFullTitle(cp, null, full, root, locCache);\r
3731                         addCaseMapping(foldSet, result, full);\r
3732 \r
3733                         result = csp.toFullUpper(cp, null, full, root, locCache);\r
3734                         addCaseMapping(foldSet, result, full);\r
3735 \r
3736                         result = csp.toFullFolding(cp, full, 0);\r
3737                         addCaseMapping(foldSet, result, full);\r
3738                     }\r
3739                 }\r
3740             }\r
3741             if (!strings.isEmpty()) {\r
3742                 if ((attribute & CASE) != 0) {\r
3743                     for (String s : strings) {\r
3744                         String str = UCharacter.foldCase(s, 0);\r
3745                         if(!csp.addStringCaseClosure(str, foldSet)) {\r
3746                             foldSet.add(str); // does not map to code points: add the folded string itself\r
3747                         }\r
3748                     }\r
3749                 } else {\r
3750                     BreakIterator bi = BreakIterator.getWordInstance(root);\r
3751                     for (String str : strings) {\r
3752                         foldSet.add(UCharacter.toLowerCase(root, str));\r
3753                         foldSet.add(UCharacter.toTitleCase(root, str, bi));\r
3754                         foldSet.add(UCharacter.toUpperCase(root, str));\r
3755                         foldSet.add(UCharacter.foldCase(str, 0));\r
3756                     }\r
3757                 }\r
3758             }\r
3759             set(foldSet);\r
3760         }\r
3761         return this;\r
3762     }\r
3763 \r
3764     /**\r
3765      * Internal class for customizing UnicodeSet parsing of properties.\r
3766      * TODO: extend to allow customizing of codepoint ranges\r
3767      * @draft ICU3.8\r
3768      * @provisional This API might change or be removed in a future release.\r
3769      * @author medavis\r
3770      */\r
3771     abstract public static class XSymbolTable implements SymbolTable {\r
3772         /**\r
3773          * Default constructor\r
3774          * @draft ICU3.8\r
3775          * @provisional This API might change or be removed in a future release.\r
3776          */\r
3777         public XSymbolTable(){}\r
3778         /**\r
3779          * Supplies default implementation for SymbolTable (no action).\r
3780          * @draft ICU3.8\r
3781          * @provisional This API might change or be removed in a future release.\r
3782          */\r
3783         public UnicodeMatcher lookupMatcher(int i) {\r
3784             return null;\r
3785         }\r
3786         /**\r
3787          * Apply a new property alias. Is called when parsing [:xxx=yyy:]. Results are to put into result.\r
3788          * @param propertyName the xxx in [:xxx=yyy:]\r
3789          * @param propertyValue the yyy in [:xxx=yyy:]\r
3790          * @param result where the result is placed\r
3791          * @return true if handled\r
3792          * @draft ICU3.8\r
3793          * @provisional This API might change or be removed in a future release.\r
3794          */\r
3795         public boolean applyPropertyAlias(String propertyName, String propertyValue, UnicodeSet result) {\r
3796             return false;\r
3797         }\r
3798         /**\r
3799          * Supplies default implementation for SymbolTable (no action).\r
3800          * @draft ICU3.8\r
3801          * @provisional This API might change or be removed in a future release.\r
3802          */\r
3803         public char[] lookup(String s) {\r
3804             return null;\r
3805         }\r
3806         /**\r
3807          * Supplies default implementation for SymbolTable (no action).\r
3808          * @draft ICU3.8\r
3809          * @provisional This API might change or be removed in a future release.\r
3810          */\r
3811         public String parseReference(String text, ParsePosition pos, int limit) {\r
3812             return null;\r
3813         }\r
3814     }\r
3815 \r
3816     /**\r
3817      * Is this frozen, according to the Freezable interface?\r
3818      * \r
3819      * @return value\r
3820      * @stable ICU 3.8\r
3821      */\r
3822     public boolean isFrozen() {\r
3823         return (bmpSet != null || stringSpan != null);\r
3824     }\r
3825 \r
3826     /**\r
3827      * Freeze this class, according to the Freezable interface.\r
3828      * \r
3829      * @return this\r
3830      * @stable ICU 4.4\r
3831      */\r
3832     public UnicodeSet freeze() {\r
3833         if (!isFrozen()) {\r
3834             // Do most of what compact() does before freezing because\r
3835             // compact() will not work when the set is frozen.\r
3836             // Small modification: Don't shrink if the savings would be tiny (<=GROW_EXTRA).\r
3837 \r
3838             // Delete buffer first to defragment memory less.\r
3839             buffer = null;\r
3840             if (list.length > (len + GROW_EXTRA)) {\r
3841                 // Make the capacity equal to len or 1.\r
3842                 // We don't want to realloc of 0 size.\r
3843                 int capacity = (len == 0) ? 1 : len;\r
3844                 int[] oldList = list;\r
3845                 list = new int[capacity];\r
3846                 for (int i = capacity; i-- > 0;) {\r
3847                     list[i] = oldList[i];\r
3848                 }\r
3849             }\r
3850 \r
3851             // Optimize contains() and span() and similar functions.\r
3852             if (!strings.isEmpty()) {\r
3853                 stringSpan = new UnicodeSetStringSpan(this, new ArrayList<String>(strings), UnicodeSetStringSpan.ALL);\r
3854                 if (!stringSpan.needsStringSpanUTF16()) {\r
3855                     // All strings are irrelevant for span() etc. because\r
3856                     // all of each string's code points are contained in this set.\r
3857                     // Do not check needsStringSpanUTF8() because UTF-8 has at most as\r
3858                     // many relevant strings as UTF-16.\r
3859                     // (Thus needsStringSpanUTF8() implies needsStringSpanUTF16().)\r
3860                     stringSpan = null;\r
3861                 }\r
3862             }\r
3863             if (stringSpan == null) {\r
3864                 // No span-relevant strings: Optimize for code point spans.\r
3865                 bmpSet = new BMPSet(list, len);\r
3866             }\r
3867         }\r
3868         return this;\r
3869     }\r
3870 \r
3871     /**\r
3872      * Span a string using this UnicodeSet.\r
3873      * \r
3874      * @param s The string to be spanned\r
3875      * @param spanCondition The span condition\r
3876      * @return the length of the span\r
3877      * @draft ICU 4.4\r
3878      * @provisional This API might change or be removed in a future release.\r
3879      */\r
3880     public int span(CharSequence s, SpanCondition spanCondition) {\r
3881         return span(s, 0, spanCondition);\r
3882     }\r
3883 \r
3884     /**\r
3885      * Span a string using this UnicodeSet.\r
3886      *   If the start index is less than 0, span will start from 0.\r
3887      *   If the start index is greater than the string length, span returns the string length.\r
3888      * \r
3889      * @param s The string to be spanned\r
3890      * @param start The start index that the span begins\r
3891      * @param spanCondition The span condition\r
3892      * @return the string index which ends the span (i.e. exclusive)\r
3893      * @draft ICU 4.4\r
3894      * @provisional This API might change or be removed in a future release.\r
3895      */\r
3896     public int span(CharSequence s, int start, SpanCondition spanCondition) {\r
3897         int end = s.length();\r
3898         if (start < 0) {\r
3899             start = 0;\r
3900         } else if (start >= end) {\r
3901             return end;\r
3902         }\r
3903         if (bmpSet != null) {\r
3904             return start + bmpSet.span(s, start, end, spanCondition);\r
3905         }\r
3906         int len = end - start;\r
3907         if (stringSpan != null) {\r
3908             return start + stringSpan.span(s, start, len, spanCondition);\r
3909         } else if (!strings.isEmpty()) {\r
3910             int which = spanCondition == SpanCondition.NOT_CONTAINED ? UnicodeSetStringSpan.FWD_UTF16_NOT_CONTAINED\r
3911                     : UnicodeSetStringSpan.FWD_UTF16_CONTAINED;\r
3912             UnicodeSetStringSpan strSpan = new UnicodeSetStringSpan(this, new ArrayList<String>(strings), which);\r
3913             if (strSpan.needsStringSpanUTF16()) {\r
3914                 return start + strSpan.span(s, start, len, spanCondition);\r
3915             }\r
3916         }\r
3917 \r
3918         // Pin to 0/1 values.\r
3919         boolean spanContained = (spanCondition != SpanCondition.NOT_CONTAINED);\r
3920 \r
3921         int c;\r
3922         int next = start;\r
3923         do {\r
3924             c = Character.codePointAt(s, next);\r
3925             if (spanContained != contains(c)) {\r
3926                 break;\r
3927             }\r
3928             next = Character.offsetByCodePoints(s, next, 1);\r
3929         } while (next < end);\r
3930         return next;\r
3931     }\r
3932 \r
3933     /**\r
3934      * Span a string backwards (from the end) using this UnicodeSet.\r
3935      * \r
3936      * @param s The string to be spanned\r
3937      * @param spanCondition The span condition\r
3938      * @return The string index which starts the span (i.e. inclusive).\r
3939      * @draft ICU 4.4\r
3940      * @provisional This API might change or be removed in a future release.\r
3941      */\r
3942     public int spanBack(CharSequence s, SpanCondition spanCondition) {\r
3943       return spanBack(s, s.length(), spanCondition);\r
3944     }\r
3945 \r
3946     /**\r
3947      * Span a string backwards (from the fromIndex) using this UnicodeSet.\r
3948      * If the fromIndex is less than 0, spanBack will return 0.\r
3949      * If fromIndex is greater than the string length, spanBack will start from the string length.\r
3950      * \r
3951      * @param s The string to be spanned\r
3952      * @param fromIndex The index of the char (exclusive) that the string should be spanned backwards\r
3953      * @param spanCondition The span condition\r
3954      * @return The string index which starts the span (i.e. inclusive).\r
3955      * @draft ICU 4.4\r
3956      * @provisional This API might change or be removed in a future release.\r
3957      */\r
3958     public int spanBack(CharSequence s, int fromIndex, SpanCondition spanCondition) {\r
3959         if (fromIndex <= 0) {\r
3960             return 0;\r
3961         }\r
3962         if (fromIndex > s.length()) {\r
3963             fromIndex = s.length();\r
3964         }\r
3965         if (bmpSet != null) {\r
3966             return bmpSet.spanBack(s, fromIndex, spanCondition);\r
3967         }\r
3968         if (stringSpan != null) {\r
3969             return stringSpan.spanBack(s, fromIndex, spanCondition);\r
3970         } else if (!strings.isEmpty()) {\r
3971             int which = (spanCondition == SpanCondition.NOT_CONTAINED)\r
3972                     ? UnicodeSetStringSpan.BACK_UTF16_NOT_CONTAINED\r
3973                     : UnicodeSetStringSpan.BACK_UTF16_CONTAINED;\r
3974             UnicodeSetStringSpan strSpan = new UnicodeSetStringSpan(this, new ArrayList<String>(strings), which);\r
3975             if (strSpan.needsStringSpanUTF16()) {\r
3976                 return strSpan.spanBack(s, fromIndex, spanCondition);\r
3977             }\r
3978         }\r
3979 \r
3980         // Pin to 0/1 values.\r
3981         boolean spanContained = (spanCondition != SpanCondition.NOT_CONTAINED);\r
3982 \r
3983         int c;\r
3984         int prev = fromIndex;\r
3985         do {\r
3986             c = Character.codePointBefore(s, prev);\r
3987             if (spanContained != contains(c)) {\r
3988                 break;\r
3989             }\r
3990             prev = Character.offsetByCodePoints(s, prev, -1);\r
3991         } while (prev > 0);\r
3992         return prev;\r
3993     }\r
3994 \r
3995     /**\r
3996      * Clone a thawed version of this class, according to the Freezable interface.\r
3997      * @return this\r
3998      * @stable ICU 4.4\r
3999      */\r
4000     public UnicodeSet cloneAsThawed() {\r
4001         UnicodeSet result = (UnicodeSet) clone();\r
4002         result.bmpSet = null;\r
4003         result.stringSpan = null;\r
4004         return result;\r
4005     }\r
4006 \r
4007     // internal function\r
4008     private void checkFrozen() {\r
4009         if (isFrozen()) {\r
4010             throw new UnsupportedOperationException("Attempt to modify frozen object");\r
4011         }\r
4012     }\r
4013 \r
4014     // ************************\r
4015     // Additional methods for integration with Generics and Collections\r
4016     // ************************\r
4017 \r
4018     /**\r
4019      * Returns a string iterator. Uses the same order of iteration as {@link UnicodeSetIterator}.\r
4020      * @see java.util.Set#iterator()\r
4021      * @draft ICU 4.4\r
4022      * @provisional This API might change or be removed in a future release.\r
4023      */\r
4024     public Iterator<String> iterator() {\r
4025         return new UnicodeSetIterator2(this);\r
4026     }\r
4027 \r
4028     // Cover for string iteration. \r
4029     private static class UnicodeSetIterator2 implements Iterator<String> {\r
4030         // Invariants:\r
4031         // sourceList != null then sourceList[item] is a valid character\r
4032         // sourceList == null then delegates to stringIterator\r
4033         private int[] sourceList;\r
4034         private int len;\r
4035         private int item;\r
4036         private int current;\r
4037         private int limit;\r
4038         private TreeSet<String> sourceStrings;\r
4039         private Iterator<String> stringIterator;\r
4040         private char[] buffer;\r
4041 \r
4042         UnicodeSetIterator2(UnicodeSet source) {\r
4043             // set according to invariants\r
4044             len = source.len - 1;\r
4045             if (item >= len) {\r
4046                 stringIterator = source.strings.iterator();\r
4047                 sourceList = null;\r
4048             } else {\r
4049                 sourceStrings = source.strings;\r
4050                 sourceList = source.list;\r
4051                 current = sourceList[item++];\r
4052                 limit = sourceList[item++];\r
4053             }\r
4054         }\r
4055 \r
4056         /* (non-Javadoc)\r
4057          * @see java.util.Iterator#hasNext()\r
4058          */\r
4059         public boolean hasNext() {\r
4060             return sourceList != null || stringIterator.hasNext();\r
4061         }\r
4062 \r
4063         /* (non-Javadoc)\r
4064          * @see java.util.Iterator#next()\r
4065          */\r
4066         public String next() {\r
4067             if (sourceList == null) {\r
4068                 return stringIterator.next();\r
4069             }\r
4070             int codepoint = current++;\r
4071             // we have the codepoint we need, but we may need to adjust the state\r
4072             if (current >= limit) {\r
4073                 if (item >= len) {\r
4074                     stringIterator = sourceStrings.iterator();\r
4075                     sourceList = null;\r
4076                 } else {\r
4077                     current = sourceList[item++];\r
4078                     limit = sourceList[item++];\r
4079                 }\r
4080             }\r
4081             // Now return. Single code point is easy\r
4082             if (codepoint <= 0xFFFF) {\r
4083                 return String.valueOf((char)codepoint);\r
4084             }\r
4085             // But Java lacks a valueOfCodePoint, so we handle ourselves for speed\r
4086             // allocate a buffer the first time, to make conversion faster.\r
4087             if (buffer == null) {\r
4088                 buffer = new char[2];\r
4089             }\r
4090             // compute ourselves, to save tests and calls\r
4091             int offset = codepoint - Character.MIN_SUPPLEMENTARY_CODE_POINT;\r
4092             buffer[0] = (char)((offset >>> 10) + Character.MIN_HIGH_SURROGATE);\r
4093             buffer[1] = (char)((offset & 0x3ff) + Character.MIN_LOW_SURROGATE);\r
4094             return String.valueOf(buffer);\r
4095         }\r
4096 \r
4097         /* (non-Javadoc)\r
4098          * @see java.util.Iterator#remove()\r
4099          */\r
4100         public void remove() {\r
4101             throw new UnsupportedOperationException();\r
4102         }  \r
4103     }\r
4104 \r
4105     /**\r
4106      * @see #containsAll(com.ibm.icu.text.UnicodeSet)\r
4107      * @draft ICU 4.4\r
4108      * @provisional This API might change or be removed in a future release.\r
4109      */\r
4110     public boolean containsAll(Collection<String> collection) {\r
4111         for (String o : collection) {\r
4112             if (!contains(o)) {\r
4113                 return false;\r
4114             }\r
4115         }\r
4116         return true;\r
4117     }\r
4118 \r
4119     /**\r
4120      * @see #containsNone(com.ibm.icu.text.UnicodeSet)\r
4121      * @draft ICU 4.4\r
4122      * @provisional This API might change or be removed in a future release.\r
4123      */\r
4124     public boolean containsNone(Collection<String> collection) {\r
4125         for (String o : collection) {\r
4126             if (contains(o)) {\r
4127                 return false;\r
4128             }\r
4129         }\r
4130         return true;\r
4131     }\r
4132 \r
4133     /**\r
4134      * @see #containsAll(com.ibm.icu.text.UnicodeSet)\r
4135      * @draft ICU 4.4\r
4136      * @provisional This API might change or be removed in a future release.\r
4137      */\r
4138     public final boolean containsSome(Collection<String> collection) {\r
4139         return !containsNone(collection);\r
4140     }\r
4141 \r
4142     /**\r
4143      * @see #addAll(com.ibm.icu.text.UnicodeSet)\r
4144      * @draft ICU 4.4\r
4145      * @provisional This API might change or be removed in a future release.\r
4146      */\r
4147     public UnicodeSet addAll(String... collection) {\r
4148         checkFrozen();\r
4149         for (String str : collection) {\r
4150             add(str);\r
4151         }\r
4152         return this;\r
4153     }\r
4154 \r
4155 \r
4156     /**\r
4157      * @see #removeAll(com.ibm.icu.text.UnicodeSet)\r
4158      * @draft ICU 4.4\r
4159      * @provisional This API might change or be removed in a future release.\r
4160      */\r
4161     public UnicodeSet removeAll(Collection<String> collection) {\r
4162         checkFrozen();\r
4163         for (String o : collection) {\r
4164             remove(o);\r
4165         }\r
4166         return this;\r
4167     }\r
4168 \r
4169     /**\r
4170      * @see #retainAll(com.ibm.icu.text.UnicodeSet)\r
4171      * @draft ICU 4.4\r
4172      * @provisional This API might change or be removed in a future release.\r
4173      */\r
4174     public UnicodeSet retainAll(Collection<String> collection) {\r
4175         checkFrozen();\r
4176         // TODO optimize\r
4177         UnicodeSet toRetain = new UnicodeSet();\r
4178         toRetain.addAll(collection);\r
4179         retainAll(toRetain);\r
4180         return this;\r
4181     }\r
4182 \r
4183     /**\r
4184      * Comparison style enums used by {@link UnicodeSet#compareTo(UnicodeSet, ComparisonStyle)}.\r
4185      * @draft ICU 4.4\r
4186      * @provisional This API might change or be removed in a future release.\r
4187      */\r
4188     public enum ComparisonStyle {\r
4189         /**\r
4190          * @draft ICU 4.4\r
4191          * @provisional This API might change or be removed in a future release.\r
4192          */\r
4193         SHORTER_FIRST,\r
4194         /**\r
4195          * @draft ICU 4.4\r
4196          * @provisional This API might change or be removed in a future release.\r
4197          */\r
4198         LEXICOGRAPHIC,\r
4199         /**\r
4200          * @draft ICU 4.4\r
4201          * @provisional This API might change or be removed in a future release.\r
4202          */\r
4203         LONGER_FIRST}\r
4204 \r
4205     /**\r
4206      * Compares UnicodeSets, where shorter come first, and otherwise lexigraphically\r
4207      * (according to the comparison of the first characters that differ).\r
4208      * @see java.lang.Comparable#compareTo(java.lang.Object)\r
4209      * @draft ICU 4.4\r
4210      * @provisional This API might change or be removed in a future release.\r
4211      */\r
4212     public int compareTo(UnicodeSet o) {\r
4213         return compareTo(o, ComparisonStyle.SHORTER_FIRST);\r
4214     }\r
4215     /**\r
4216      * Compares UnicodeSets, in three different ways.\r
4217      * @see java.lang.Comparable#compareTo(java.lang.Object)\r
4218      * @draft ICU 4.4\r
4219      * @provisional This API might change or be removed in a future release.\r
4220      */\r
4221     public int compareTo(UnicodeSet o, ComparisonStyle style) {\r
4222         if (style != ComparisonStyle.LEXICOGRAPHIC) {\r
4223             int diff = size() - o.size();\r
4224             if (diff != 0) {\r
4225                 return (diff < 0) == (style == ComparisonStyle.SHORTER_FIRST) ? -1 : 1;\r
4226             }\r
4227         }\r
4228         int result;\r
4229         for (int i = 0; ; ++i) {\r
4230             if (0 != (result = list[i] - o.list[i])) {\r
4231                 // if either list ran out, compare to the last string\r
4232                 if (list[i] == HIGH) {\r
4233                     if (strings.isEmpty()) return 1;\r
4234                     String item = strings.first();\r
4235                     return compare(item, o.list[i]);\r
4236                 }\r
4237                 if (o.list[i] == HIGH) {\r
4238                     if (o.strings.isEmpty()) return -1;\r
4239                     String item = o.strings.first();\r
4240                     return -compare(item, list[i]);\r
4241                 }\r
4242                 // otherwise return the result if even index, or the reversal if not\r
4243                 return (i & 1) == 0 ? result : -result;\r
4244             }\r
4245             if (list[i] == HIGH) {\r
4246                 break;\r
4247             }\r
4248         }\r
4249         return compare(strings, o.strings);\r
4250     }\r
4251 \r
4252     /**\r
4253      * @draft ICU 4.4\r
4254      * @provisional This API might change or be removed in a future release.\r
4255      */\r
4256     public int compareTo(Iterable<String> other) {\r
4257         return compare(this, other);\r
4258     }\r
4259 \r
4260     /**\r
4261      * Utility to compare a string to a code point.\r
4262      * Same results as turning the code point into a string (with the [ugly] new StringBuilder().appendCodePoint(codepoint).toString())\r
4263      * and comparing, but much faster (no object creation). \r
4264      * Note that this (=String) order is UTF-16 order -- *not* code point order.\r
4265      * @draft ICU 4.4\r
4266      * @provisional This API might change or be removed in a future release.\r
4267      */\r
4268     public static int compare(String string, int codePoint) {\r
4269         if (codePoint < Character.MIN_CODE_POINT || codePoint > Character.MAX_CODE_POINT) {\r
4270             throw new IllegalArgumentException();\r
4271         }\r
4272         int stringLength = string.length();\r
4273         if (stringLength == 0) {\r
4274             return -1;\r
4275         }\r
4276         char firstChar = string.charAt(0);\r
4277         int offset = codePoint - Character.MIN_SUPPLEMENTARY_CODE_POINT;\r
4278 \r
4279         if (offset < 0) { // BMP codePoint\r
4280             int result = firstChar - codePoint;\r
4281             if (result != 0) {\r
4282                 return result;\r
4283             }\r
4284             return stringLength - 1;\r
4285         } \r
4286         // non BMP\r
4287         char lead = (char)((offset >>> 10) + Character.MIN_HIGH_SURROGATE);\r
4288         int result = firstChar - lead;\r
4289         if (result != 0) {\r
4290             return result;\r
4291         }\r
4292         if (stringLength > 1) {\r
4293             char trail = (char)((offset & 0x3ff) + Character.MIN_LOW_SURROGATE);\r
4294             result = string.charAt(1) - trail;\r
4295             if (result != 0) {\r
4296                 return result;\r
4297             }\r
4298         }\r
4299         return stringLength - 2;\r
4300     }\r
4301 \r
4302     /**\r
4303      * Utility to compare a string to a code point.\r
4304      * Same results as turning the code point into a string and comparing, but much faster (no object creation). \r
4305      * Actually, there is one difference; a null compares as less.\r
4306      * @draft ICU 4.4\r
4307      * @provisional This API might change or be removed in a future release.\r
4308      */\r
4309     public static int compare(int codepoint, String a) {\r
4310         return -compare(a, codepoint);\r
4311     }\r
4312 \r
4313     /**\r
4314      * Utility to compare two iterables. Warning: the ordering in iterables is important. For Collections that are ordered,\r
4315      * like Lists, that is expected. However, Sets in Java violate Leibniz's law when it comes to iteration.\r
4316      * That means that sets can't be compared directly with this method, unless they are TreeSets without\r
4317      * (or with the same) comparator. Unfortunately, it is impossible to reliably detect in Java whether subclass of\r
4318      * Collection satisfies the right criteria, so it is left to the user to avoid those circumstances.\r
4319      * @draft ICU 4.4\r
4320      * @provisional This API might change or be removed in a future release.\r
4321      */\r
4322     public static <T extends Comparable<T>> int compare(Iterable<T> collection1, Iterable<T> collection2) {\r
4323         Iterator<T> first = collection1.iterator();\r
4324         Iterator<T> other = collection2.iterator();\r
4325         while (true) {\r
4326             if (!first.hasNext()) {\r
4327                 return other.hasNext() ? -1 : 0;\r
4328             } else if (!other.hasNext()) {\r
4329                 return 1;\r
4330             }\r
4331             T item1 = first.next();\r
4332             T item2 = other.next();\r
4333             int result = item1.compareTo(item2);\r
4334             if (result != 0) {\r
4335                 return result;\r
4336             }\r
4337         }\r
4338     }\r
4339 \r
4340     /**\r
4341      * Utility to compare two collections, optionally by size, and then lexicographically.\r
4342      * @draft ICU 4.4\r
4343      * @provisional This API might change or be removed in a future release.\r
4344      */\r
4345     public static <T extends Comparable<T>> int compare(Collection<T> collection1, Collection<T> collection2, ComparisonStyle style) {\r
4346         if (style != ComparisonStyle.LEXICOGRAPHIC) {\r
4347             int diff = collection1.size() - collection2.size();\r
4348             if (diff != 0) {\r
4349                 return (diff < 0) == (style == ComparisonStyle.SHORTER_FIRST) ? -1 : 1;\r
4350             }\r
4351         }\r
4352         return compare(collection1, collection2);\r
4353     }\r
4354 \r
4355     /**\r
4356      * Utility for adding the contents of an iterable to a collection.\r
4357      * @draft ICU 4.4\r
4358      * @provisional This API might change or be removed in a future release.\r
4359      */\r
4360     public static <T, U extends Collection<T>> U addAllTo(Iterable<T> source, U target) {\r
4361         for (T item : source) {\r
4362             target.add(item);\r
4363         }\r
4364         return target;\r
4365     }\r
4366 \r
4367     /**\r
4368      * Utility for adding the contents of an iterable to a collection.\r
4369      * @draft ICU 4.4\r
4370      * @provisional This API might change or be removed in a future release.\r
4371      */\r
4372     public static <T> T[] addAllTo(Iterable<T> source, T[] target) {\r
4373         int i = 0;\r
4374         for (T item : source) {\r
4375             target[i++] = item;\r
4376         }\r
4377         return target;\r
4378     }\r
4379 \r
4380     /**\r
4381      * For iterating through the strings in the set. Example:\r
4382      * <pre>\r
4383      * for (String key : myUnicodeSet.strings()) {\r
4384      *   doSomethingWith(key);\r
4385      * }\r
4386      * </pre>\r
4387      * @draft ICU 4.4\r
4388      * @provisional This API might change or be removed in a future release.\r
4389      */\r
4390     public Iterable<String> strings() {\r
4391         return Collections.unmodifiableSortedSet(strings);\r
4392     }\r
4393 \r
4394     /**\r
4395      * Return the value of the first code point, if the string is exactly one code point. Otherwise return Integer.MAX_VALUE.\r
4396      * @internal\r
4397      * @deprecated This API is ICU internal only.\r
4398      */\r
4399     public static int getSingleCodePoint(String s) {\r
4400         int length = s.length();\r
4401         if (length < 1 || length > 2) {\r
4402             return Integer.MAX_VALUE;\r
4403         }\r
4404         int result = s.codePointAt(0);\r
4405         return (result < 0x10000) == (length == 1) ? result : Integer.MAX_VALUE;\r
4406     }\r
4407 \r
4408     /**\r
4409      * Simplify the ranges in a Unicode set by merging any ranges that are only separated by characters in the dontCare set. \r
4410      * For example, the ranges: \\u2E80-\\u2E99\\u2E9B-\\u2EF3\\u2F00-\\u2FD5\\u2FF0-\\u2FFB\\u3000-\\u303E change to \\u2E80-\\u303E \r
4411      * if the dontCare set includes unassigned characters (for a particular version of Unicode).\r
4412      * @param dontCare Set with the don't-care characters for spanning\r
4413      * @return the input set, modified\r
4414      * @internal\r
4415      * @deprecated This API is ICU internal only.\r
4416      */\r
4417     public UnicodeSet addBridges(UnicodeSet dontCare) {\r
4418         UnicodeSet notInInput = new UnicodeSet(this).complement();\r
4419         for (UnicodeSetIterator it = new UnicodeSetIterator(notInInput); it.nextRange();) {\r
4420             if (it.codepoint != 0 && it.codepoint != UnicodeSetIterator.IS_STRING && it.codepointEnd != 0x10FFFF && dontCare.contains(it.codepoint,it.codepointEnd)) {\r
4421                 add(it.codepoint,it.codepointEnd);\r
4422             }\r
4423         }\r
4424         return this;\r
4425     }\r
4426 \r
4427     /**\r
4428      * Find the first index at or after fromIndex where the UnicodeSet matches at that index.\r
4429      * If findNot is true, then reverse the sense of the match: find the first place where the UnicodeSet doesn't match.\r
4430      * If there is no match, length is returned.\r
4431      * @internal\r
4432      * @deprecated This API is ICU internal only.\r
4433      */\r
4434     public int findIn(CharSequence value, int fromIndex, boolean findNot) {\r
4435         //TODO add strings, optimize, using ICU4C algorithms\r
4436         int cp;\r
4437         for (; fromIndex < value.length(); fromIndex += UTF16.getCharCount(cp)) {\r
4438             cp = UTF16.charAt(value, fromIndex);\r
4439             if (contains(cp) != findNot) {\r
4440                 break;\r
4441             }\r
4442         }\r
4443         return fromIndex;\r
4444     }\r
4445 \r
4446     /**\r
4447      * Find the last index before fromIndex where the UnicodeSet matches at that index.\r
4448      * If findNot is true, then reverse the sense of the match: find the last place where the UnicodeSet doesn't match.\r
4449      * If there is no match, -1 is returned.\r
4450      * BEFORE index is not in the UnicodeSet.\r
4451      * @internal\r
4452      * @deprecated This API is ICU internal only.\r
4453      */\r
4454     public int findLastIn(CharSequence value, int fromIndex, boolean findNot) {\r
4455         //TODO add strings, optimize, using ICU4C algorithms\r
4456         int cp;\r
4457         fromIndex -= 1;\r
4458         for (; fromIndex >= 0; fromIndex -= UTF16.getCharCount(cp)) {\r
4459             cp = UTF16.charAt(value, fromIndex);\r
4460             if (contains(cp) != findNot) {\r
4461                 break;\r
4462             }\r
4463         }\r
4464         return fromIndex < 0 ? -1 : fromIndex;\r
4465     }\r
4466 \r
4467     /**\r
4468      * Strips code points from source. If matches is true, script all that match <i>this</i>. If matches is false, then strip all that <i>don't</i> match.\r
4469      * @param source The source of the CharSequence to strip from.\r
4470      * @param matches A boolean to either strip all that matches or don't match with the current UnicodeSet object.\r
4471      * @return The string after it has been stripped.\r
4472      * @internal\r
4473      * @deprecated This API is ICU internal only.\r
4474      */\r
4475     public String stripFrom(CharSequence source, boolean matches) {\r
4476         StringBuilder result = new StringBuilder();\r
4477         for (int pos = 0; pos < source.length();) {\r
4478             int inside = findIn(source, pos, !matches);\r
4479             result.append(source.subSequence(pos, inside));\r
4480             pos = findIn(source, inside, matches); // get next start\r
4481         }\r
4482         return result.toString();\r
4483     }\r
4484 \r
4485     /**\r
4486      * Argument values for whether span() and similar functions continue while the current character is contained vs.\r
4487      * not contained in the set.\r
4488      * <p>\r
4489      * The functionality is straightforward for sets with only single code points, without strings (which is the common\r
4490      * case):\r
4491      * <ul>\r
4492      * <li>CONTAINED and SIMPLE work the same.\r
4493      * <li>span() and spanBack() partition any string the\r
4494      * same way when alternating between span(NOT_CONTAINED) and span(either "contained" condition).\r
4495      * <li>Using a\r
4496      * complemented (inverted) set and the opposite span conditions yields the same results.\r
4497      * </ul>\r
4498      * When a set contains multi-code point strings, then these statements may not be true, depending on the strings in\r
4499      * the set (for example, whether they overlap with each other) and the string that is processed. For a set with\r
4500      * strings:\r
4501      * <ul>\r
4502      * <li>The complement of the set contains the opposite set of code points, but the same set of strings.\r
4503      * Therefore, complementing both the set and the span conditions may yield different results.\r
4504      * <li>When starting spans\r
4505      * at different positions in a string (span(s, ...) vs. span(s+1, ...)) the ends of the spans may be different\r
4506      * because a set string may start before the later position.\r
4507      * <li>span(SIMPLE) may be shorter than\r
4508      * span(CONTAINED) because it will not recursively try all possible paths. For example, with a set which\r
4509      * contains the three strings "xy", "xya" and "ax", span("xyax", CONTAINED) will return 4 but span("xyax",\r
4510      * SIMPLE) will return 3. span(SIMPLE) will never be longer than span(CONTAINED).\r
4511      * <li>With either "contained" condition, span() and spanBack() may partition a string in different ways. For example,\r
4512      * with a set which contains the two strings "ab" and "ba", and when processing the string "aba", span() will yield\r
4513      * contained/not-contained boundaries of { 0, 2, 3 } while spanBack() will yield boundaries of { 0, 1, 3 }.\r
4514      * </ul>\r
4515      * Note: If it is important to get the same boundaries whether iterating forward or backward through a string, then\r
4516      * either only span() should be used and the boundaries cached for backward operation, or an ICU BreakIterator could\r
4517      * be used.\r
4518      * <p>\r
4519      * Note: Unpaired surrogates are treated like surrogate code points. Similarly, set strings match only on code point\r
4520      * boundaries, never in the middle of a surrogate pair.\r
4521      * \r
4522      * @draft ICU 4.4\r
4523      * @provisional This API might change or be removed in a future release.\r
4524      */\r
4525     public enum SpanCondition {\r
4526         /**\r
4527          * Continue a span() while there is no set element at the current position. Stops before the first set element\r
4528          * (character or string). (For code points only, this is like while contains(current)==FALSE).\r
4529          * <p>\r
4530          * When span() returns, the substring between where it started and the position it returned consists only of\r
4531          * characters that are not in the set, and none of its strings overlap with the span.\r
4532          * \r
4533          * @draft ICU 4.4\r
4534          * @provisional This API might change or be removed in a future release.\r
4535          */\r
4536         NOT_CONTAINED,\r
4537         /**\r
4538          * Continue a span() while there is a set element at the current position. (For characters only, this is like\r
4539          * while contains(current)==TRUE).\r
4540          * <p>\r
4541          * When span() returns, the substring between where it started and the position it returned consists only of set\r
4542          * elements (characters or strings) that are in the set.\r
4543          * <p>\r
4544          * If a set contains strings, then the span will be the longest substring matching any of the possible\r
4545          * concatenations of set elements (characters or strings). (There must be a single, non-overlapping\r
4546          * concatenation of characters or strings.) This is equivalent to a POSIX regular expression for (OR of each set\r
4547          * element)*.\r
4548          * \r
4549          * @draft ICU 4.4\r
4550          * @provisional This API might change or be removed in a future release.\r
4551          */\r
4552         CONTAINED,\r
4553         /**\r
4554          * Continue a span() while there is a set element at the current position. (For characters only, this is like\r
4555          * while contains(current)==TRUE).\r
4556          * <p>\r
4557          * When span() returns, the substring between where it started and the position it returned consists only of set\r
4558          * elements (characters or strings) that are in the set.\r
4559          * <p>\r
4560          * If a set only contains single characters, then this is the same as CONTAINED.\r
4561          * <p>\r
4562          * If a set contains strings, then the span will be the longest substring with a match at each position with the\r
4563          * longest single set element (character or string).\r
4564          * <p>\r
4565          * Use this span condition together with other longest-match algorithms, such as ICU converters\r
4566          * (ucnv_getUnicodeSet()).\r
4567          * \r
4568          * @draft ICU 4.4\r
4569          * @provisional This API might change or be removed in a future release.\r
4570          */\r
4571         SIMPLE,\r
4572         /**\r
4573          * One more than the last span condition.\r
4574          * \r
4575          * @draft ICU 4.4\r
4576          * @provisional This API might change or be removed in a future release.\r
4577          */\r
4578         CONDITION_COUNT\r
4579     }\r
4580 }\r
4581 //eof\r