2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.text;
\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
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
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
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
46 * <p>The UnicodeSet class is not designed to be subclassed.
\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
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
76 * <p><b>Pattern syntax</b></p>
\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
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
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
102 * <td nowrap valign="top" align="left"><code>[\\u4E01]</code></td>
\r
103 * <td valign="top">The character U+4E01</td>
\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 "ab" and
\r
108 * "ac"</td>
\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
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
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
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
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
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
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
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
180 * <p><b>Warning</b>: you cannot add an empty string ("") to a UnicodeSet.</p>
\r
182 * <p><b>Formal syntax</b></p>
\r
187 * <td nowrap valign="top" align="right"><code>pattern := </code></td>
\r
188 * <td valign="top"><code>('[' '^'? item* ']') |
\r
189 * property</code></td>
\r
192 * <td nowrap valign="top" align="right"><code>item := </code></td>
\r
193 * <td valign="top"><code>char | (char '-' char) | pattern-expr<br>
\r
197 * <td nowrap valign="top" align="right"><code>pattern-expr := </code></td>
\r
198 * <td valign="top"><code>pattern | pattern-expr pattern |
\r
199 * pattern-expr op pattern<br>
\r
203 * <td nowrap valign="top" align="right"><code>op := </code></td>
\r
204 * <td valign="top"><code>'&' | '-'<br>
\r
208 * <td nowrap valign="top" align="right"><code>special := </code></td>
\r
209 * <td valign="top"><code>'[' | ']' | '-'<br>
\r
213 * <td nowrap valign="top" align="right"><code>char := </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 * | ('\u' hex hex hex hex)<br>
\r
220 * <td nowrap valign="top" align="right"><code>hex := </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
226 * <td nowrap valign="top" align="right"><code>property := </code></td>
\r
227 * <td valign="top"><em>a Unicode property set pattern</td>
\r
231 * <table border="1">
\r
233 * <td>Legend: <table>
\r
235 * <td nowrap valign="top"><code>a := b</code></td>
\r
236 * <td width="20" valign="top"> </td>
\r
237 * <td valign="top"><code>a</code> may be replaced by <code>b</code> </td>
\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
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
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
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
267 * <p>To iterate over contents of UnicodeSet, use UnicodeSetIterator class.
\r
271 * @see UnicodeSetIterator
\r
273 public class UnicodeSet extends UnicodeFilter implements Iterable<String>, Comparable<UnicodeSet>, Freezable<UnicodeSet> {
\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
280 * Minimum value that can be stored in a UnicodeSet.
\r
283 public static final int MIN_VALUE = LOW;
\r
286 * Maximum value that can be stored in a UnicodeSet.
\r
289 public static final int MAX_VALUE = HIGH - 1;
\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
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
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
309 private String pat = null;
\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
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
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
325 private static UnicodeSet INCLUSIONS[] = null;
\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
331 //----------------------------------------------------------------
\r
334 * Constructs an empty set.
\r
337 public UnicodeSet() {
\r
338 list = new int[1 + START_EXTRA];
\r
339 list[len++] = HIGH;
\r
343 * Constructs a copy of an existing set.
\r
346 public UnicodeSet(UnicodeSet other) {
\r
351 * Constructs a set containing the given range. If <code>end >
\r
352 * start</code> then an empty set is created.
\r
354 * @param start first character, inclusive, of range
\r
355 * @param end last character, inclusive, of range
\r
358 public UnicodeSet(int start, int end) {
\r
360 complement(start, end);
\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
371 * @provisional This API might change or be removed in a future release.
\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
377 list = new int[pairs.length + 1]; // don't allocate extra space, because it is likely that this is a fixed set.
\r
379 int last = -1; // used to ensure that the results are monotonically increasing.
\r
381 while (i < pairs.length) {
\r
383 int start = pairs[i];
\r
384 if (last >= start) {
\r
385 throw new IllegalArgumentException("Must be monotonically increasing.");
\r
387 list[i++] = last = start;
\r
389 int end = pairs[i] + 1;
\r
391 throw new IllegalArgumentException("Must be monotonically increasing.");
\r
393 list[i++] = last = end;
\r
395 list[i] = HIGH; // terminate
\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
406 public UnicodeSet(String pattern) {
\r
408 applyPattern(pattern, null, null, IGNORE_SPACE);
\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
421 public UnicodeSet(String pattern, boolean ignoreWhitespace) {
\r
423 applyPattern(pattern, null, null, ignoreWhitespace ? IGNORE_SPACE : 0);
\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
436 public UnicodeSet(String pattern, int options) {
\r
438 applyPattern(pattern, null, null, options);
\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
453 public UnicodeSet(String pattern, ParsePosition pos, SymbolTable symbols) {
\r
455 applyPattern(pattern, pos, symbols, IGNORE_SPACE);
\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
472 public UnicodeSet(String pattern, ParsePosition pos, SymbolTable symbols, int options) {
\r
474 applyPattern(pattern, pos, symbols, options);
\r
479 * Return a new set that is equivalent to this one.
\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
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
494 * @param start first character in the set, inclusive
\r
495 * @param end last character in the set, inclusive
\r
498 public UnicodeSet set(int start, int end) {
\r
501 complement(start, end);
\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
511 public UnicodeSet set(UnicodeSet other) {
\r
513 list = other.list.clone();
\r
516 strings = new TreeSet<String>(other.strings);
\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
529 public final UnicodeSet applyPattern(String pattern) {
\r
531 return applyPattern(pattern, null, null, IGNORE_SPACE);
\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
545 public UnicodeSet applyPattern(String pattern, boolean ignoreWhitespace) {
\r
547 return applyPattern(pattern, null, null, ignoreWhitespace ? IGNORE_SPACE : 0);
\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
561 public UnicodeSet applyPattern(String pattern, int options) {
\r
563 return applyPattern(pattern, null, null, options);
\r
567 * Return true if the given position, in the given pattern, appears
\r
568 * to be the start of a UnicodeSet pattern.
\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
578 * Append the <code>toPattern()</code> representation of a
\r
579 * string to the given <code>StringBuffer</code>.
\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
588 * Append the <code>toPattern()</code> representation of a
\r
589 * character to the given <code>StringBuffer</code>.
\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
597 if (Utility.escapeUnprintable(buf, c)) {
\r
601 // Okay to let ':' pass through
\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
616 // Escape whitespace
\r
617 if (UCharacterProperty.isRuleWhiteSpace(c)) {
\r
622 UTF16.append(buf, c);
\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
631 public String toPattern(boolean escapeUnprintable) {
\r
632 StringBuffer result = new StringBuffer();
\r
633 return _toPattern(result, escapeUnprintable).toString();
\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
641 private StringBuffer _toPattern(StringBuffer result,
\r
642 boolean escapeUnprintable) {
\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
654 if ((backslashCount % 2) == 1) {
\r
655 result.setLength(result.length() - 1);
\r
657 Utility.escapeUnprintable(result, c);
\r
658 backslashCount = 0;
\r
660 UTF16.append(result, c);
\r
664 backslashCount = 0;
\r
671 return _generatePattern(result, escapeUnprintable, true);
\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
682 public StringBuffer _generatePattern(StringBuffer result, boolean escapeUnprintable) {
\r
683 return _generatePattern(result, escapeUnprintable, true);
\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
693 public StringBuffer _generatePattern(StringBuffer result,
\r
694 boolean escapeUnprintable, boolean includeStrings) {
\r
695 result.append('[');
\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
707 int count = getRangeCount();
\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
713 getRangeStart(0) == MIN_VALUE &&
\r
714 getRangeEnd(count-1) == MAX_VALUE) {
\r
716 // Emit the inverse
\r
717 result.append('^');
\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
727 _appendToPat(result, end, escapeUnprintable);
\r
732 // Default; emit the ranges as pairs
\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
742 _appendToPat(result, end, escapeUnprintable);
\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
754 return result.append(']');
\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
762 * @return the number of elements in this set (its cardinality).
\r
765 public int size() {
\r
767 int count = getRangeCount();
\r
768 for (int i = 0; i < count; ++i) {
\r
769 n += getRangeEnd(i) - getRangeStart(i) + 1;
\r
771 return n + strings.size();
\r
775 * Returns <tt>true</tt> if this set contains no elements.
\r
777 * @return <tt>true</tt> if this set contains no elements.
\r
780 public boolean isEmpty() {
\r
781 return len == 1 && strings.size() == 0;
\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
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
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
807 } else if ((low & 0xFF) <= v || v <= (high & 0xFF)) {
\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
817 // assert(s.length() != 0); // We enforce this elsewhere
\r
818 int c = UTF16.charAt(s, 0);
\r
819 if ((c & 0xFF) == v) {
\r
828 * Implementation of UnicodeMatcher.matches(). Always matches the
\r
829 * longest possible multichar string.
\r
832 public int matches(Replaceable text,
\r
835 boolean incremental) {
\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
847 if (strings.size() != 0) { // try strings first
\r
849 // might separate forward and backward loops later
\r
850 // for now they are combined
\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
856 boolean forward = offset[0] < limit;
\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
863 // If there are multiple strings that can match we
\r
864 // return the longest match.
\r
865 int highWaterLength = 0;
\r
867 for (String trial : strings) {
\r
868 //if (trial.length() == 0) {
\r
869 // return U_MATCH; // null-string always matches
\r
871 // assert(trial.length() != 0); // We ensure this elsewhere
\r
873 char c = trial.charAt(forward ? 0 : trial.length() - 1);
\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
880 int length = matchRest(text, offset[0], limit, trial);
\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
890 if (length == trial.length()) {
\r
891 // We have successfully matched the whole string.
\r
892 if (length > highWaterLength) {
\r
893 highWaterLength = length;
\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
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
911 return super.matches(text, offset, limit, incremental);
\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
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
936 private static int matchRest (Replaceable text, int start, int limit, String s) {
\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
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
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
959 * @deprecated This API is ICU internal only.
\r
961 public int matchesAt(CharSequence text, int offset) {
\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
971 char firstStringChar = trial.charAt(0);
\r
972 if (firstStringChar < firstChar) continue;
\r
973 if (firstStringChar > firstChar) break strings;
\r
976 // now keep checking string until we get the longest one
\r
978 int tempLen = matchesAt(text, offset, trial);
\r
979 if (lastLen > tempLen) break strings;
\r
981 if (!it.hasNext()) break;
\r
987 int cp = UTF16.charAt(text, offset);
\r
988 if (contains(cp)) lastLen = UTF16.getCharCount(cp);
\r
991 return offset+lastLen;
\r
995 * Does one string contain another, starting at a specific offset?
\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
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
1015 * Implementation of UnicodeMatcher API. Union the set of all
\r
1016 * characters that may be matched by this object into the given
\r
1018 * @param toUnionTo the set into which to union the source characters
\r
1021 public void addMatchSetTo(UnicodeSet toUnionTo) {
\r
1022 toUnionTo.addAll(this);
\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
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
1040 int start = list[i++];
\r
1044 int limit = list[i++];
\r
1046 return n + c - start;
\r
1048 n += limit - start;
\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
1061 public int charAt(int index) {
\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
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
1085 * @param start first character, inclusive, of range to be added
\r
1087 * @param end last character, inclusive, of range to be added
\r
1091 public UnicodeSet add(int start, int end) {
\r
1093 return add_unchecked(start, end);
\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
1102 * @provisional This API might change or be removed in a future release.
\r
1104 public UnicodeSet addAll(int start, int end) {
\r
1106 return add_unchecked(start, end);
\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
1114 if (end < MIN_VALUE || end > MAX_VALUE) {
\r
1115 throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
\r
1117 if (start < end) {
\r
1118 add(range(start, end), 2, 0);
\r
1119 } else if (start == end) {
\r
1126 // * Format out the inversion list as a string, for debugging. Uncomment when
\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
1137 // buf.append("U+").append(Utility.hex(c, (c<0x10000)?4:6));
\r
1140 // buf.append("]");
\r
1141 // return buf.toString();
\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
1150 public final UnicodeSet add(int c) {
\r
1152 return add_unchecked(c);
\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
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
1166 // already in set?
\r
1167 if ((i & 1) != 0) return this;
\r
1169 // HIGH is 0x110000
\r
1170 // assert(list[len-1] == HIGH);
\r
1173 // [start_0, limit_0, start_1, limit_1, HIGH]
\r
1175 // [..., start_k-1, limit_k-1, start_k, limit_k, ..., HIGH]
\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
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
1192 if (i > 0 && c == list[i-1]) {
\r
1193 // collapse adjacent ranges
\r
1195 // [..., start_k-1, c, c, limit_k, ..., HIGH]
\r
1198 System.arraycopy(list, i+1, list, i-1, len-i-1);
\r
1203 else if (i > 0 && c == list[i-1]) {
\r
1204 // c is after end of prior range
\r
1206 // no need to chcek for collapse here
\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
1214 // [..., start_k-1, limit_k-1, start_k, limit_k, ..., HIGH]
\r
1218 // [..., start_k-1, limit_k-1, c, c+1, start_k, limit_k, ..., HIGH]
\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
1231 System.arraycopy(list, i, list, i+2, len-i);
\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
1253 public final UnicodeSet add(String s) {
\r
1255 int cp = getSingleCP(s);
\r
1260 add_unchecked(cp, cp);
\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
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
1274 if (s.length() > 2) return -1;
\r
1275 if (s.length() == 1) return s.charAt(0);
\r
1277 // at this point, len = 2
\r
1278 int cp = UTF16.charAt(s, 0);
\r
1279 if (cp > 0xFFFF) { // is surrogate pair
\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
1292 public final UnicodeSet addAll(String s) {
\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
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
1309 public final UnicodeSet retainAll(String s) {
\r
1310 return retainAll(fromAll(s));
\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
1320 public final UnicodeSet complementAll(String s) {
\r
1321 return complementAll(fromAll(s));
\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
1331 public final UnicodeSet removeAll(String s) {
\r
1332 return removeAll(fromAll(s));
\r
1336 * Remove all strings from this UnicodeSet
\r
1337 * @return this object, for chaining
\r
1340 public final UnicodeSet removeAllStrings() {
\r
1342 if (strings.size() != 0) {
\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
1356 public static UnicodeSet from(String s) {
\r
1357 return new UnicodeSet().add(s);
\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
1367 public static UnicodeSet fromAll(String s) {
\r
1368 return new UnicodeSet().addAll(s);
\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
1377 * @param start first character, inclusive, of range to be retained
\r
1379 * @param end last character, inclusive, of range to be retained
\r
1383 public UnicodeSet retain(int start, int end) {
\r
1385 if (start < MIN_VALUE || start > MAX_VALUE) {
\r
1386 throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));
\r
1388 if (end < MIN_VALUE || end > MAX_VALUE) {
\r
1389 throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
\r
1391 if (start <= end) {
\r
1392 retain(range(start, end), 2, 0);
\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
1407 public final UnicodeSet retain(int c) {
\r
1408 return retain(c, c);
\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
1419 public final UnicodeSet retain(String s) {
\r
1420 int cp = getSingleCP(s);
\r
1422 boolean isIn = strings.contains(s);
\r
1423 if (isIn && size() == 1) {
\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
1441 * @param start first character, inclusive, of range to be removed
\r
1443 * @param end last character, inclusive, of range to be removed
\r
1447 public UnicodeSet remove(int start, int end) {
\r
1449 if (start < MIN_VALUE || start > MAX_VALUE) {
\r
1450 throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));
\r
1452 if (end < MIN_VALUE || end > MAX_VALUE) {
\r
1453 throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
\r
1455 if (start <= end) {
\r
1456 retain(range(start, end), 2, 2);
\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
1465 * @param c the character to be removed
\r
1466 * @return this object, for chaining
\r
1469 public final UnicodeSet remove(int c) {
\r
1470 return remove(c, c);
\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
1477 * @param s the string to be removed
\r
1478 * @return this object, for chaining
\r
1481 public final UnicodeSet remove(String s) {
\r
1482 int cp = getSingleCP(s);
\r
1484 strings.remove(s);
\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
1498 * @param start first character, inclusive, of range to be removed
\r
1500 * @param end last character, inclusive, of range to be removed
\r
1504 public UnicodeSet complement(int start, int end) {
\r
1506 if (start < MIN_VALUE || start > MAX_VALUE) {
\r
1507 throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));
\r
1509 if (end < MIN_VALUE || end > MAX_VALUE) {
\r
1510 throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
\r
1512 if (start <= end) {
\r
1513 xor(range(start, end), 2, 0);
\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
1525 public final UnicodeSet complement(int c) {
\r
1526 return complement(c, c);
\r
1530 * This is equivalent to
\r
1531 * <code>complement(MIN_VALUE, MAX_VALUE)</code>.
\r
1534 public UnicodeSet complement() {
\r
1536 if (list[0] == LOW) {
\r
1537 System.arraycopy(list, 1, list, 0, len-1);
\r
1540 ensureCapacity(len+1);
\r
1541 System.arraycopy(list, 0, list, 1, len);
\r
1550 * Complement the specified string in this set.
\r
1551 * The set will not contain the specified string once the call
\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
1558 public final UnicodeSet complement(String s) {
\r
1560 int cp = getSingleCP(s);
\r
1562 if (strings.contains(s)) {
\r
1563 strings.remove(s);
\r
1569 complement(cp, cp);
\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
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
1586 // Set i to the index of the start item greater than ch
\r
1587 // We know we will terminate without length test!
\r
1590 if (c < list[++i]) break;
\r
1594 int i = findCodePoint(c);
\r
1596 return ((i & 1) != 0); // return true if odd
\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
1605 * @return the smallest integer i in the range 0..len-1,
\r
1606 * inclusive, such that c < list[i]
\r
1608 private final int findCodePoint(int 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
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
1627 // invariant: c >= list[lo]
\r
1628 // invariant: c < list[hi]
\r
1630 int i = (lo + hi) >>> 1;
\r
1631 if (i == lo) return hi;
\r
1632 if (c < list[i]) {
\r
1640 // //----------------------------------------------------------------
\r
1641 // // Unrolled binary search
\r
1642 // //----------------------------------------------------------------
\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
1650 // private void validate() {
\r
1651 // if (len <= 1) {
\r
1652 // throw new IllegalArgumentException("list.len==" + len + "; must be >1");
\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
1658 // // assert(exp2[power] <= len);
\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
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
1679 // * Unrolled lowest index GT.
\r
1681 // private final int leastIndexGT(int searchValue) {
\r
1683 // if (len != validLen) {
\r
1684 // if (len == 1) return 0;
\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
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
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
1701 // // The invariant AFTER each line is that list[low] < searchValue <= list[high]
\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
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
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
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
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
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
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
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
1747 // // For debugging only
\r
1748 // public int len() {
\r
1752 // //----------------------------------------------------------------
\r
1753 // //----------------------------------------------------------------
\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
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
1767 if (end < MIN_VALUE || end > MAX_VALUE) {
\r
1768 throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
\r
1772 // if (start < list[++i]) break;
\r
1774 int i = findCodePoint(start);
\r
1775 return ((i & 1) != 0 && end < list[i]);
\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
1785 public final boolean contains(String s) {
\r
1787 int cp = getSingleCP(s);
\r
1789 return strings.contains(s);
\r
1791 return contains(cp);
\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
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
1812 int aLen = len - 1;
\r
1813 int bLen = b.len - 1;
\r
1814 int startA = 0, startB = 0, limitA = 0, limitB = 0;
\r
1816 // double iterations are such a pain...
\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
1825 startA = list[aPtr++];
\r
1826 limitA = list[aPtr++];
\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
1833 startB = listB[bPtr++];
\r
1834 limitB = listB[bPtr++];
\r
1836 // if B doesn't overlap and is greater than A, get new A
\r
1837 if (startB >= limitA) {
\r
1842 // if B is wholy contained in A, then get a new B
\r
1843 if (startB >= startA && limitB <= limitA) {
\r
1848 // all other combinations mean we fail
\r
1852 if (!strings.containsAll(b.strings)) return false;
\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
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
1873 // if (!strings.containsAll(c.strings)) return false;
\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
1886 public boolean containsAll(String s) {
\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
1894 return containsAll(s, 0);
\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
1906 private boolean containsAll(String s, int i) {
\r
1907 if (i >= s.length()) {
\r
1910 int cp= UTF16.charAt(s, i);
\r
1911 if (contains(cp) && containsAll(s, i+UTF16.getCharCount(cp))) {
\r
1914 for (String setStr : strings) {
\r
1915 if (s.startsWith(setStr, i) && containsAll(s, i+setStr.length())) {
\r
1924 * Get the Regex equivalent for this UnicodeSet
\r
1925 * @return regex pattern equivalent to this UnicodeSet
\r
1927 * @deprecated This API is ICU internal only.
\r
1929 public String getRegexEquivalent() {
\r
1930 if (strings.size() == 0) {
\r
1931 return toString();
\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
1939 return result.append(")").toString();
\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
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
1954 if (end < MIN_VALUE || end > MAX_VALUE) {
\r
1955 throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
\r
1959 if (start < list[++i]) break;
\r
1961 return ((i & 1) == 0 && end < list[i]);
\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
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
1981 int aLen = len - 1;
\r
1982 int bLen = b.len - 1;
\r
1983 int startA = 0, startB = 0, limitA = 0, limitB = 0;
\r
1985 // double iterations are such a pain...
\r
1987 if (aPtr >= aLen) {
\r
1988 // ran out of A: break so we test strings
\r
1991 startA = list[aPtr++];
\r
1992 limitA = list[aPtr++];
\r
1995 if (bPtr >= bLen) {
\r
1996 // ran out of B: break so we test strings
\r
1999 startB = listB[bPtr++];
\r
2000 limitB = listB[bPtr++];
\r
2002 // if B is higher than any part of A, get new A
\r
2003 if (startB >= limitA) {
\r
2008 // if A is higher than any part of B, get new B
\r
2009 if (startA >= limitB) {
\r
2014 // all other combinations mean we fail
\r
2018 if (!SortedSetRelation.hasRelation(strings, SortedSetRelation.DISJOINT, b.strings)) return false;
\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
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
2041 // if (!SortedSetRelation.hasRelation(strings, SortedSetRelation.DISJOINT, c.strings)) return false;
\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
2052 public boolean containsNone(String s) {
\r
2053 return span(s, SpanCondition.NOT_CONTAINED) == s.length();
\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
2064 public final boolean containsSome(int start, int end) {
\r
2065 return !containsNone(start, end);
\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
2075 public final boolean containsSome(UnicodeSet s) {
\r
2076 return !containsNone(s);
\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
2086 public final boolean containsSome(String s) {
\r
2087 return !containsNone(s);
\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
2098 * @param c set whose elements are to be added to this set.
\r
2101 public UnicodeSet addAll(UnicodeSet c) {
\r
2103 add(c.list, c.len, 0);
\r
2104 strings.addAll(c.strings);
\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
2115 * @param c set that defines which elements this set will retain.
\r
2118 public UnicodeSet retainAll(UnicodeSet c) {
\r
2120 retain(c.list, c.len, 0);
\r
2121 strings.retainAll(c.strings);
\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
2131 * @param c set that defines which elements will be removed from
\r
2135 public UnicodeSet removeAll(UnicodeSet c) {
\r
2137 retain(c.list, c.len, 2);
\r
2138 strings.removeAll(c.strings);
\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
2147 * @param c set that defines which elements will be complemented from
\r
2151 public UnicodeSet complementAll(UnicodeSet c) {
\r
2153 xor(c.list, c.len, 0);
\r
2154 SortedSetRelation.doOperation(strings, SortedSetRelation.COMPLEMENTALL, c.strings);
\r
2159 * Removes all of the elements from this set. This set will be
\r
2160 * empty after this call returns.
\r
2163 public UnicodeSet clear() {
\r
2173 * Iteration method that returns the number of ranges contained in
\r
2175 * @see #getRangeStart
\r
2176 * @see #getRangeEnd
\r
2179 public int getRangeCount() {
\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
2192 public int getRangeStart(int index) {
\r
2193 return list[index*2];
\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
2205 public int getRangeEnd(int index) {
\r
2206 return (list[index*2 + 1] - 1);
\r
2210 * Reallocate this objects internal structures to take up the least
\r
2211 * possible space, without changing this object's value.
\r
2214 public UnicodeSet compact() {
\r
2216 if (len != list.length) {
\r
2217 int[] temp = new int[len];
\r
2218 System.arraycopy(list, 0, temp, 0, len);
\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
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
2237 public boolean equals(Object o) {
\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
2244 if (!strings.equals(that.strings)) return false;
\r
2245 } catch (Exception e) {
\r
2252 * Returns the hash code value for this set.
\r
2254 * @return the hash code value for this set.
\r
2255 * @see java.lang.Object#hashCode()
\r
2258 public int hashCode() {
\r
2260 for (int i = 0; i < len; ++i) {
\r
2261 result *= 1000003;
\r
2262 result += list[i];
\r
2268 * Return a programmer-readable string representation of this object.
\r
2271 public String toString() {
\r
2272 return toPattern(true);
\r
2275 //----------------------------------------------------------------
\r
2276 // Implementation: Pattern parsing
\r
2277 //----------------------------------------------------------------
\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
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
2301 * @deprecated This API is ICU internal only.
\r
2303 public UnicodeSet applyPattern(String pattern,
\r
2304 ParsePosition pos,
\r
2305 SymbolTable symbols,
\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
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
2322 pat = rebuiltPat.toString();
\r
2323 if (parsePositionWasNull) {
\r
2324 int i = pos.getIndex();
\r
2326 // Skip over trailing whitespace
\r
2327 if ((options & IGNORE_SPACE) != 0) {
\r
2328 i = Utility.skipWhitespace(pattern, i);
\r
2331 if (i != pattern.length()) {
\r
2332 throw new IllegalArgumentException("Parse of \"" + pattern +
\r
2333 "\" failed at " + i);
\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
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
2353 void applyPattern(RuleCharacterIterator chars, SymbolTable symbols,
\r
2354 StringBuffer rebuiltPat, int options) {
\r
2356 // Syntax characters: [ ] ^ - & { }
\r
2358 // Recognized special forms for chars, sets: c-c s-s s&s
\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
2366 StringBuffer patBuf = new StringBuffer(), buf = null;
\r
2367 boolean usePat = false;
\r
2368 UnicodeSet scratch = null;
\r
2369 Object backup = null;
\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
2376 boolean invert = false;
\r
2380 while (mode != 2 && !chars.atEnd()) {
\r
2381 //Eclipse stated the following is "dead code"
\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
2393 boolean literal = false;
\r
2394 UnicodeSet nested = null;
\r
2396 // -------- Check for property pattern
\r
2398 // setMode: 0=none, 1=unicodeset, 2=propertypat, 3=preparsed
\r
2400 if (resemblesPropertyPattern(chars, opts)) {
\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
2413 // Prepare to backup if necessary
\r
2414 backup = chars.getPos(backup);
\r
2415 c = chars.next(opts);
\r
2416 literal = chars.isEscaped();
\r
2418 if (c == '[' && !literal) {
\r
2420 chars.setPos(backup); // backup
\r
2423 // Handle opening '[' delimiter
\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
2431 patBuf.append('^');
\r
2432 backup = chars.getPos(backup); // prepare to backup
\r
2433 c = chars.next(opts);
\r
2434 literal = chars.isEscaped();
\r
2436 // Fall through to handle special leading '-';
\r
2437 // otherwise restart loop for nested [], \p{}, etc.
\r
2440 // Fall through to handle literal '-' below
\r
2442 chars.setPos(backup); // backup
\r
2446 } else if (symbols != null) {
\r
2447 UnicodeMatcher m = symbols.lookupMatcher(c); // may be null
\r
2450 nested = (UnicodeSet) m;
\r
2452 } catch (ClassCastException e) {
\r
2453 syntaxError(chars, "Syntax error");
\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
2464 if (setMode != 0) {
\r
2465 if (lastItem == 1) {
\r
2467 syntaxError(chars, "Char expected after operator");
\r
2469 add_unchecked(lastChar, lastChar);
\r
2470 _appendToPat(patBuf, lastChar, false);
\r
2471 lastItem = op = 0;
\r
2474 if (op == '-' || op == '&') {
\r
2475 patBuf.append(op);
\r
2478 if (nested == null) {
\r
2479 if (scratch == null) scratch = new UnicodeSet();
\r
2482 switch (setMode) {
\r
2484 nested.applyPattern(chars, symbols, patBuf, options);
\r
2487 chars.skipIgnored(opts);
\r
2488 nested.applyPropertyPattern(chars, patBuf, symbols);
\r
2490 case 3: // `nested' already parsed
\r
2491 nested._toPattern(patBuf, false);
\r
2498 // Entire pattern is a category; leave parse loop
\r
2506 removeAll(nested);
\r
2509 retainAll(nested);
\r
2523 syntaxError(chars, "Missing '['");
\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
2533 if (lastItem == 1) {
\r
2534 add_unchecked(lastChar, lastChar);
\r
2535 _appendToPat(patBuf, lastChar, false);
\r
2537 // Treat final trailing '-' as a literal
\r
2539 add_unchecked(op, op);
\r
2540 patBuf.append(op);
\r
2541 } else if (op == '&') {
\r
2542 syntaxError(chars, "Trailing '&'");
\r
2544 patBuf.append(']');
\r
2549 if (lastItem != 0) {
\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
2564 syntaxError(chars, "'-' not after char or set");
\r
2567 if (lastItem == 2 && op == 0) {
\r
2571 syntaxError(chars, "'&' not after set");
\r
2574 syntaxError(chars, "'^' not after '['");
\r
2578 syntaxError(chars, "Missing operand after operator");
\r
2580 if (lastItem == 1) {
\r
2581 add_unchecked(lastChar, lastChar);
\r
2582 _appendToPat(patBuf, lastChar, false);
\r
2585 if (buf == null) {
\r
2586 buf = new StringBuffer();
\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
2598 UTF16.append(buf, c);
\r
2600 if (buf.length() < 1 || !ok) {
\r
2601 syntaxError(chars, "Invalid multicharacter string");
\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
2606 add(buf.toString());
\r
2607 patBuf.append('{');
\r
2608 _appendToPat(patBuf, buf.toString(), false);
\r
2609 patBuf.append('}');
\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
2627 if (anchor && op == 0) {
\r
2628 if (lastItem == 1) {
\r
2629 add_unchecked(lastChar, lastChar);
\r
2630 _appendToPat(patBuf, lastChar, false);
\r
2632 add_unchecked(UnicodeMatcher.ETHER);
\r
2634 patBuf.append(SymbolTable.SYMBOL_REF).append(']');
\r
2638 syntaxError(chars, "Unquoted '$'");
\r
2645 // -------- Parse literal characters. This includes both
\r
2646 // escaped chars ("\u4E01") and non-syntax characters
\r
2649 switch (lastItem) {
\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
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
2667 add_unchecked(lastChar, lastChar);
\r
2668 _appendToPat(patBuf, lastChar, false);
\r
2674 syntaxError(chars, "Set expected after operator");
\r
2683 syntaxError(chars, "Missing ']'");
\r
2686 chars.skipIgnored(opts);
\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
2694 if ((options & CASE) != 0) {
\r
2701 // Use the rebuilt pattern (pat) only if necessary. Prefer the
\r
2702 // generated pattern.
\r
2704 rebuiltPat.append(patBuf.toString());
\r
2706 _generatePattern(rebuiltPat, false, true);
\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
2717 * Add the contents of the UnicodeSet (as strings) into a collection.
\r
2718 * @param target collection to add into
\r
2720 * @provisional This API might change or be removed in a future release.
\r
2722 public <T extends Collection<String>> T addAllTo(T target) {
\r
2723 return addAllTo(this, target);
\r
2728 * Add the contents of the UnicodeSet (as strings) into a collection.
\r
2729 * @param target collection to add into
\r
2731 * @provisional This API might change or be removed in a future release.
\r
2733 public String[] addAllTo(String[] target) {
\r
2734 return addAllTo(this, target);
\r
2738 * Add the contents of the UnicodeSet (as strings) into an array.
\r
2740 * @provisional This API might change or be removed in a future release.
\r
2742 public static String[] toArray(UnicodeSet set) {
\r
2743 return addAllTo(set, new String[set.size()]);
\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
2752 public UnicodeSet add(Collection<?> source) {
\r
2753 return addAll(source);
\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
2763 public UnicodeSet addAll(Collection<?> source) {
\r
2765 for (Object o : source) {
\r
2766 add(o.toString());
\r
2771 //----------------------------------------------------------------
\r
2772 // Implementation: Utility methods
\r
2773 //----------------------------------------------------------------
\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
2782 private void ensureBufferCapacity(int newLen) {
\r
2783 if (buffer != null && newLen <= buffer.length) return;
\r
2784 buffer = new int[newLen + GROW_EXTRA];
\r
2788 * Assumes start <= end.
\r
2790 private int[] range(int start, int end) {
\r
2791 if (rangeList == null) {
\r
2792 rangeList = new int[] { start, end+1, HIGH };
\r
2794 rangeList[0] = start;
\r
2795 rangeList[1] = end+1;
\r
2800 //----------------------------------------------------------------
\r
2801 // Implementation: Fundamental operations
\r
2802 //----------------------------------------------------------------
\r
2804 // polarity = 0, 3 is normal: x xor y
\r
2805 // polarity = 1, 2: x xor ~y == x === y
\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
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
2815 if (polarity == 1 || polarity == 2) {
\r
2817 if (other[j] == LOW) { // skip base if already LOW
\r
2825 // simplest of all the routines
\r
2826 // sort the values, discarding identicals!
\r
2831 } else if (b < a) {
\r
2834 } else if (a != HIGH) { // at this point, a == b
\r
2835 // discard both values!
\r
2839 buffer[k++] = HIGH;
\r
2844 // swap list and buffer
\r
2845 int[] temp = list;
\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
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
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
2878 i++; // Common if/else code factored out
\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
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
2902 b = other[j++]; polarity ^= 2;
\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
2909 } else { // take b
\r
2910 if (b == HIGH) break main;
\r
2913 a = list[i++]; polarity ^= 1; // factored common code
\r
2914 b = other[j++]; polarity ^= 2;
\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
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
2940 buffer[k++] = HIGH; // terminate
\r
2942 // swap list and buffer
\r
2943 int[] temp = list;
\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
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
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
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
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
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
3011 buffer[k++] = HIGH; // terminate
\r
3013 // swap list and buffer
\r
3014 int[] temp = list;
\r
3021 private static final int max(int a, int b) {
\r
3022 return (a > b) ? a : b;
\r
3025 //----------------------------------------------------------------
\r
3026 // Generic filter-based scanning code
\r
3027 //----------------------------------------------------------------
\r
3029 private static interface Filter {
\r
3030 boolean contains(int codePoint);
\r
3033 private static class NumericValueFilter implements Filter {
\r
3035 NumericValueFilter(double value) { this.value = value; }
\r
3036 public boolean contains(int ch) {
\r
3037 return UCharacter.getUnicodeNumericValue(ch) == value;
\r
3041 private static class GeneralCategoryMaskFilter implements Filter {
\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
3049 private static class IntPropertyFilter implements Filter {
\r
3052 IntPropertyFilter(int prop, int value) {
\r
3054 this.value = value;
\r
3056 public boolean contains(int ch) {
\r
3057 return UCharacter.getIntPropertyValue(ch, prop) == value;
\r
3061 // VersionInfo for unassigned characters
\r
3062 static final VersionInfo NO_VERSION = VersionInfo.getInstance(0, 0, 0, 0);
\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
3076 private static synchronized UnicodeSet getInclusions(int src) {
\r
3077 if (INCLUSIONS == null) {
\r
3078 INCLUSIONS = new UnicodeSet[UCharacterProperty.SRC_COUNT];
\r
3080 if(INCLUSIONS[src] == null) {
\r
3081 UnicodeSet incl = new UnicodeSet();
\r
3084 case UCharacterProperty.SRC_CHAR:
\r
3085 UCharacterProperty.INSTANCE.addPropertyStarts(incl);
\r
3087 case UCharacterProperty.SRC_PROPSVEC:
\r
3088 UCharacterProperty.INSTANCE.upropsvec_addPropertyStarts(incl);
\r
3090 case UCharacterProperty.SRC_CHAR_AND_PROPSVEC:
\r
3091 UCharacterProperty.INSTANCE.addPropertyStarts(incl);
\r
3092 UCharacterProperty.INSTANCE.upropsvec_addPropertyStarts(incl);
\r
3094 case UCharacterProperty.SRC_CASE_AND_NORM:
\r
3095 Norm2AllModes.getNFCInstance().impl.addPropertyStarts(incl);
\r
3096 UCaseProps.getSingleton().addPropertyStarts(incl);
\r
3098 case UCharacterProperty.SRC_NFC:
\r
3099 Norm2AllModes.getNFCInstance().impl.addPropertyStarts(incl);
\r
3101 case UCharacterProperty.SRC_NFKC:
\r
3102 Norm2AllModes.getNFKCInstance().impl.addPropertyStarts(incl);
\r
3104 case UCharacterProperty.SRC_NFKC_CF:
\r
3105 Norm2AllModes.getNFKC_CFInstance().impl.addPropertyStarts(incl);
\r
3107 case UCharacterProperty.SRC_NFC_CANON_ITER:
\r
3108 Norm2AllModes.getNFCInstance().impl.addCanonIterPropertyStarts(incl);
\r
3110 case UCharacterProperty.SRC_CASE:
\r
3111 UCaseProps.getSingleton().addPropertyStarts(incl);
\r
3113 case UCharacterProperty.SRC_BIDI:
\r
3114 UBiDiProps.getSingleton().addPropertyStarts(incl);
\r
3117 throw new IllegalStateException("UnicodeSet.getInclusions(unknown src "+src+")");
\r
3119 } catch(IOException e) {
\r
3120 throw new MissingResourceException(e.getMessage(),"","");
\r
3122 INCLUSIONS[src] = incl;
\r
3124 return INCLUSIONS[src];
\r
3128 * Generic filter-based scanning code for UCD property UnicodeSets.
\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
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
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
3147 int startHasProperty = -1;
\r
3148 UnicodeSet inclusions = getInclusions(src);
\r
3149 int limitRange = inclusions.getRangeCount();
\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
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
3164 } else if (startHasProperty >= 0) {
\r
3165 add_unchecked(startHasProperty, ch-1);
\r
3166 startHasProperty = -1;
\r
3170 if (startHasProperty >= 0) {
\r
3171 add_unchecked(startHasProperty, 0x10FFFF);
\r
3179 * Remove leading and trailing rule white space and compress
\r
3180 * internal rule white space to a single space character.
\r
3182 * @see UCharacterProperty#isRuleWhiteSpace
\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
3194 ch = ' '; // convert to ' '
\r
3196 UTF16.append(buf, ch);
\r
3198 if (buf.length() != 0 &&
\r
3199 buf.charAt(buf.length() - 1) == ' ') {
\r
3200 buf.setLength(buf.length() - 1);
\r
3202 return buf.toString();
\r
3205 //----------------------------------------------------------------
\r
3206 // Property set API
\r
3207 //----------------------------------------------------------------
\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
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
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
3228 * @return a reference to this set
\r
3232 public UnicodeSet applyIntPropertyValue(int prop, int value) {
\r
3234 if (prop == UProperty.GENERAL_CATEGORY_MASK) {
\r
3235 applyFilter(new GeneralCategoryMaskFilter(value), UCharacterProperty.SRC_CHAR);
\r
3237 applyFilter(new IntPropertyFilter(prop, value), UCharacterProperty.INSTANCE.getSource(prop));
\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
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
3257 * "ANY" = [\u0000-\U0010FFFF],
\r
3258 * "ASCII" = [\u0000-\u007F].
\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
3267 * @return a reference to this set
\r
3271 public UnicodeSet applyPropertyAlias(String propertyAlias, String valueAlias) {
\r
3272 return applyPropertyAlias(propertyAlias, valueAlias, null);
\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
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
3286 public UnicodeSet applyPropertyAlias(String propertyAlias,
\r
3287 String valueAlias, SymbolTable symbols) {
\r
3291 boolean mustNotBeEmpty = false, invert = false;
\r
3293 if (symbols != null
\r
3294 && (symbols instanceof XSymbolTable)
\r
3295 && ((XSymbolTable)symbols).applyPropertyAlias(propertyAlias, valueAlias, this)) {
\r
3299 if (valueAlias.length() > 0) {
\r
3300 p = UCharacter.getPropertyEnum(propertyAlias);
\r
3302 // Treat gc as gcm
\r
3303 if (p == UProperty.GENERAL_CATEGORY) {
\r
3304 p = UProperty.GENERAL_CATEGORY_MASK;
\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
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
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
3332 case UProperty.NUMERIC_VALUE:
\r
3334 double value = Double.parseDouble(Utility.deleteRuleWhiteSpace(valueAlias));
\r
3335 applyFilter(new NumericValueFilter(value), UCharacterProperty.SRC_CHAR);
\r
3338 case UProperty.NAME:
\r
3339 case UProperty.UNICODE_1_NAME:
\r
3341 // Must munge name, since
\r
3342 // UCharacter.charFromName() does not do
\r
3343 // 'loose' matching.
\r
3344 String buf = mungeCharName(valueAlias);
\r
3346 (p == UProperty.NAME) ?
\r
3347 UCharacter.getCharFromExtendedName(buf) :
\r
3348 UCharacter.getCharFromName1_0(buf);
\r
3350 throw new IllegalArgumentException("Invalid character name");
\r
3353 add_unchecked(ch);
\r
3356 case UProperty.AGE:
\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
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
3374 // valueAlias is empty. Interpret as General Category, Script,
\r
3375 // Binary property, or ANY or ASCII. Upon success, p and v will
\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
3388 if (p >= UProperty.BINARY_START && p < UProperty.BINARY_LIMIT) {
\r
3390 } else if (p == -1) {
\r
3391 if (0 == UPropertyAliases.compare(ANY_ID, propertyAlias)) {
\r
3392 set(MIN_VALUE, MAX_VALUE);
\r
3394 } else if (0 == UPropertyAliases.compare(ASCII_ID, propertyAlias)) {
\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
3403 // Property name was never matched.
\r
3404 throw new IllegalArgumentException("Invalid property alias: " + propertyAlias + "=" + valueAlias);
\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
3415 applyIntPropertyValue(p, v);
\r
3420 if (mustNotBeEmpty && isEmpty()) {
\r
3421 // mustNotBeEmpty is set to true if an empty set indicates
\r
3423 throw new IllegalArgumentException("Invalid property value");
\r
3429 //----------------------------------------------------------------
\r
3430 // Property set patterns
\r
3431 //----------------------------------------------------------------
\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
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
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
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
3457 private static boolean resemblesPropertyPattern(RuleCharacterIterator chars,
\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
3468 chars.setPos(pos);
\r
3473 * Parse the given property pattern at the given parse position.
\r
3474 * @param symbols TODO
\r
3476 private UnicodeSet applyPropertyPattern(String pattern, ParsePosition ppos, SymbolTable symbols) {
\r
3477 int pos = ppos.getIndex();
\r
3479 // On entry, ppos should point to one of the following locations:
\r
3481 // Minimum length is 5 characters, e.g. \p{L}
\r
3482 if ((pos+5) > pattern.length()) {
\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
3490 // Look for an opening [:, [:^, \p, or \P
\r
3491 if (pattern.regionMatches(pos, "[:", 0, 2)) {
\r
3493 pos = Utility.skipWhitespace(pattern, pos+2);
\r
3494 if (pos < pattern.length() && pattern.charAt(pos) == '^') {
\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
3509 // Open delimiter not seen
\r
3513 // Look for the matching close delimiter, either :] or }
\r
3514 int close = pattern.indexOf(posix ? ":]" : "}", pos);
\r
3516 // Syntax error; close delimiter missing
\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
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
3532 // Handle case where no '=' is seen, and \N{}
\r
3533 propName = pattern.substring(pos, close);
\r
3536 // Handle \N{name}
\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
3548 applyPropertyAlias(propName, valueName, symbols);
\r
3554 // Move to the limit position after the close delimiter
\r
3555 ppos.setIndex(close + (posix ? 2 : 1));
\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
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
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
3578 chars.jumpahead(pos.getIndex());
\r
3579 rebuiltPat.append(patStr.substring(0, pos.getIndex()));
\r
3582 //----------------------------------------------------------------
\r
3583 // Case folding API
\r
3584 //----------------------------------------------------------------
\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
3594 public static final int IGNORE_SPACE = 1;
\r
3597 * Bitmask for constructor, applyPattern(), and closeOver()
\r
3598 * indicating letter case. This may be ORed together with other
\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
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
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
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
3625 public static final int CASE = 2;
\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
3633 public static final int CASE_INSENSITIVE = 2;
\r
3636 * Bitmask for constructor, applyPattern(), and closeOver()
\r
3637 * indicating letter case. This may be ORed together with other
\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
3647 public static final int ADD_CASE_MAPPINGS = 4;
\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
3653 if(result > UCaseProps.MAX_STRING_LENGTH) {
\r
3654 // add a single-code point case mapping
\r
3657 // add a string case mapping from full with length result
\r
3658 set.add(full.toString());
\r
3659 full.setLength(0);
\r
3662 // result < 0: the code point mapped to itself, no need to add it
\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
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
3675 * 2. For each string 'e' in the resulting set, if e !=
\r
3676 * foldCase(e), 'e' will be removed.
\r
3678 * Example: [aq\u00DF{Bc}{bC}{Fi}] => [aAqQ\u00DF\uFB01{ss}{bc}{fi}]
\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
3684 * @param attribute bitmask for attributes to close over.
\r
3685 * Currently only the CASE bit is supported. Any undefined bits
\r
3687 * @return a reference to this set.
\r
3690 public UnicodeSet closeOver(int attribute) {
\r
3692 if ((attribute & (CASE | ADD_CASE_MAPPINGS)) != 0) {
\r
3695 csp = UCaseProps.getSingleton();
\r
3696 } catch(IOException e) {
\r
3699 UnicodeSet foldSet = new UnicodeSet(this);
\r
3700 ULocale root = ULocale.ROOT;
\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
3709 int n = getRangeCount();
\r
3711 StringBuffer full = new StringBuffer();
\r
3712 int locCache[] = new int[1];
\r
3714 for (int i=0; i<n; ++i) {
\r
3715 int start = getRangeStart(i);
\r
3716 int end = getRangeEnd(i);
\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
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
3730 result = csp.toFullTitle(cp, null, full, root, locCache);
\r
3731 addCaseMapping(foldSet, result, full);
\r
3733 result = csp.toFullUpper(cp, null, full, root, locCache);
\r
3734 addCaseMapping(foldSet, result, full);
\r
3736 result = csp.toFullFolding(cp, full, 0);
\r
3737 addCaseMapping(foldSet, result, full);
\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
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
3765 * Internal class for customizing UnicodeSet parsing of properties.
\r
3766 * TODO: extend to allow customizing of codepoint ranges
\r
3768 * @provisional This API might change or be removed in a future release.
\r
3771 abstract public static class XSymbolTable implements SymbolTable {
\r
3773 * Default constructor
\r
3775 * @provisional This API might change or be removed in a future release.
\r
3777 public XSymbolTable(){}
\r
3779 * Supplies default implementation for SymbolTable (no action).
\r
3781 * @provisional This API might change or be removed in a future release.
\r
3783 public UnicodeMatcher lookupMatcher(int i) {
\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
3793 * @provisional This API might change or be removed in a future release.
\r
3795 public boolean applyPropertyAlias(String propertyName, String propertyValue, UnicodeSet result) {
\r
3799 * Supplies default implementation for SymbolTable (no action).
\r
3801 * @provisional This API might change or be removed in a future release.
\r
3803 public char[] lookup(String s) {
\r
3807 * Supplies default implementation for SymbolTable (no action).
\r
3809 * @provisional This API might change or be removed in a future release.
\r
3811 public String parseReference(String text, ParsePosition pos, int limit) {
\r
3817 * Is this frozen, according to the Freezable interface?
\r
3822 public boolean isFrozen() {
\r
3823 return (bmpSet != null || stringSpan != null);
\r
3827 * Freeze this class, according to the Freezable interface.
\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
3838 // Delete buffer first to defragment memory less.
\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
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
3863 if (stringSpan == null) {
\r
3864 // No span-relevant strings: Optimize for code point spans.
\r
3865 bmpSet = new BMPSet(list, len);
\r
3872 * Span a string using this UnicodeSet.
\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
3878 * @provisional This API might change or be removed in a future release.
\r
3880 public int span(CharSequence s, SpanCondition spanCondition) {
\r
3881 return span(s, 0, spanCondition);
\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
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
3894 * @provisional This API might change or be removed in a future release.
\r
3896 public int span(CharSequence s, int start, SpanCondition spanCondition) {
\r
3897 int end = s.length();
\r
3900 } else if (start >= end) {
\r
3903 if (bmpSet != null) {
\r
3904 return start + bmpSet.span(s, start, end, spanCondition);
\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
3918 // Pin to 0/1 values.
\r
3919 boolean spanContained = (spanCondition != SpanCondition.NOT_CONTAINED);
\r
3924 c = Character.codePointAt(s, next);
\r
3925 if (spanContained != contains(c)) {
\r
3928 next = Character.offsetByCodePoints(s, next, 1);
\r
3929 } while (next < end);
\r
3934 * Span a string backwards (from the end) using this UnicodeSet.
\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
3940 * @provisional This API might change or be removed in a future release.
\r
3942 public int spanBack(CharSequence s, SpanCondition spanCondition) {
\r
3943 return spanBack(s, s.length(), spanCondition);
\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
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
3956 * @provisional This API might change or be removed in a future release.
\r
3958 public int spanBack(CharSequence s, int fromIndex, SpanCondition spanCondition) {
\r
3959 if (fromIndex <= 0) {
\r
3962 if (fromIndex > s.length()) {
\r
3963 fromIndex = s.length();
\r
3965 if (bmpSet != null) {
\r
3966 return bmpSet.spanBack(s, fromIndex, spanCondition);
\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
3980 // Pin to 0/1 values.
\r
3981 boolean spanContained = (spanCondition != SpanCondition.NOT_CONTAINED);
\r
3984 int prev = fromIndex;
\r
3986 c = Character.codePointBefore(s, prev);
\r
3987 if (spanContained != contains(c)) {
\r
3990 prev = Character.offsetByCodePoints(s, prev, -1);
\r
3991 } while (prev > 0);
\r
3996 * Clone a thawed version of this class, according to the Freezable interface.
\r
4000 public UnicodeSet cloneAsThawed() {
\r
4001 UnicodeSet result = (UnicodeSet) clone();
\r
4002 result.bmpSet = null;
\r
4003 result.stringSpan = null;
\r
4007 // internal function
\r
4008 private void checkFrozen() {
\r
4010 throw new UnsupportedOperationException("Attempt to modify frozen object");
\r
4014 // ************************
\r
4015 // Additional methods for integration with Generics and Collections
\r
4016 // ************************
\r
4019 * Returns a string iterator. Uses the same order of iteration as {@link UnicodeSetIterator}.
\r
4020 * @see java.util.Set#iterator()
\r
4022 * @provisional This API might change or be removed in a future release.
\r
4024 public Iterator<String> iterator() {
\r
4025 return new UnicodeSetIterator2(this);
\r
4028 // Cover for string iteration.
\r
4029 private static class UnicodeSetIterator2 implements Iterator<String> {
\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
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
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
4049 sourceStrings = source.strings;
\r
4050 sourceList = source.list;
\r
4051 current = sourceList[item++];
\r
4052 limit = sourceList[item++];
\r
4057 * @see java.util.Iterator#hasNext()
\r
4059 public boolean hasNext() {
\r
4060 return sourceList != null || stringIterator.hasNext();
\r
4064 * @see java.util.Iterator#next()
\r
4066 public String next() {
\r
4067 if (sourceList == null) {
\r
4068 return stringIterator.next();
\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
4077 current = sourceList[item++];
\r
4078 limit = sourceList[item++];
\r
4081 // Now return. Single code point is easy
\r
4082 if (codepoint <= 0xFFFF) {
\r
4083 return String.valueOf((char)codepoint);
\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
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
4098 * @see java.util.Iterator#remove()
\r
4100 public void remove() {
\r
4101 throw new UnsupportedOperationException();
\r
4106 * @see #containsAll(com.ibm.icu.text.UnicodeSet)
\r
4108 * @provisional This API might change or be removed in a future release.
\r
4110 public boolean containsAll(Collection<String> collection) {
\r
4111 for (String o : collection) {
\r
4112 if (!contains(o)) {
\r
4120 * @see #containsNone(com.ibm.icu.text.UnicodeSet)
\r
4122 * @provisional This API might change or be removed in a future release.
\r
4124 public boolean containsNone(Collection<String> collection) {
\r
4125 for (String o : collection) {
\r
4126 if (contains(o)) {
\r
4134 * @see #containsAll(com.ibm.icu.text.UnicodeSet)
\r
4136 * @provisional This API might change or be removed in a future release.
\r
4138 public final boolean containsSome(Collection<String> collection) {
\r
4139 return !containsNone(collection);
\r
4143 * @see #addAll(com.ibm.icu.text.UnicodeSet)
\r
4145 * @provisional This API might change or be removed in a future release.
\r
4147 public UnicodeSet addAll(String... collection) {
\r
4149 for (String str : collection) {
\r
4157 * @see #removeAll(com.ibm.icu.text.UnicodeSet)
\r
4159 * @provisional This API might change or be removed in a future release.
\r
4161 public UnicodeSet removeAll(Collection<String> collection) {
\r
4163 for (String o : collection) {
\r
4170 * @see #retainAll(com.ibm.icu.text.UnicodeSet)
\r
4172 * @provisional This API might change or be removed in a future release.
\r
4174 public UnicodeSet retainAll(Collection<String> collection) {
\r
4177 UnicodeSet toRetain = new UnicodeSet();
\r
4178 toRetain.addAll(collection);
\r
4179 retainAll(toRetain);
\r
4184 * Comparison style enums used by {@link UnicodeSet#compareTo(UnicodeSet, ComparisonStyle)}.
\r
4186 * @provisional This API might change or be removed in a future release.
\r
4188 public enum ComparisonStyle {
\r
4191 * @provisional This API might change or be removed in a future release.
\r
4196 * @provisional This API might change or be removed in a future release.
\r
4201 * @provisional This API might change or be removed in a future release.
\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
4210 * @provisional This API might change or be removed in a future release.
\r
4212 public int compareTo(UnicodeSet o) {
\r
4213 return compareTo(o, ComparisonStyle.SHORTER_FIRST);
\r
4216 * Compares UnicodeSets, in three different ways.
\r
4217 * @see java.lang.Comparable#compareTo(java.lang.Object)
\r
4219 * @provisional This API might change or be removed in a future release.
\r
4221 public int compareTo(UnicodeSet o, ComparisonStyle style) {
\r
4222 if (style != ComparisonStyle.LEXICOGRAPHIC) {
\r
4223 int diff = size() - o.size();
\r
4225 return (diff < 0) == (style == ComparisonStyle.SHORTER_FIRST) ? -1 : 1;
\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
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
4242 // otherwise return the result if even index, or the reversal if not
\r
4243 return (i & 1) == 0 ? result : -result;
\r
4245 if (list[i] == HIGH) {
\r
4249 return compare(strings, o.strings);
\r
4254 * @provisional This API might change or be removed in a future release.
\r
4256 public int compareTo(Iterable<String> other) {
\r
4257 return compare(this, other);
\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
4266 * @provisional This API might change or be removed in a future release.
\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
4272 int stringLength = string.length();
\r
4273 if (stringLength == 0) {
\r
4276 char firstChar = string.charAt(0);
\r
4277 int offset = codePoint - Character.MIN_SUPPLEMENTARY_CODE_POINT;
\r
4279 if (offset < 0) { // BMP codePoint
\r
4280 int result = firstChar - codePoint;
\r
4281 if (result != 0) {
\r
4284 return stringLength - 1;
\r
4287 char lead = (char)((offset >>> 10) + Character.MIN_HIGH_SURROGATE);
\r
4288 int result = firstChar - lead;
\r
4289 if (result != 0) {
\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
4299 return stringLength - 2;
\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
4307 * @provisional This API might change or be removed in a future release.
\r
4309 public static int compare(int codepoint, String a) {
\r
4310 return -compare(a, codepoint);
\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
4320 * @provisional This API might change or be removed in a future release.
\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
4326 if (!first.hasNext()) {
\r
4327 return other.hasNext() ? -1 : 0;
\r
4328 } else if (!other.hasNext()) {
\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
4341 * Utility to compare two collections, optionally by size, and then lexicographically.
\r
4343 * @provisional This API might change or be removed in a future release.
\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
4349 return (diff < 0) == (style == ComparisonStyle.SHORTER_FIRST) ? -1 : 1;
\r
4352 return compare(collection1, collection2);
\r
4356 * Utility for adding the contents of an iterable to a collection.
\r
4358 * @provisional This API might change or be removed in a future release.
\r
4360 public static <T, U extends Collection<T>> U addAllTo(Iterable<T> source, U target) {
\r
4361 for (T item : source) {
\r
4368 * Utility for adding the contents of an iterable to a collection.
\r
4370 * @provisional This API might change or be removed in a future release.
\r
4372 public static <T> T[] addAllTo(Iterable<T> source, T[] target) {
\r
4374 for (T item : source) {
\r
4375 target[i++] = item;
\r
4381 * For iterating through the strings in the set. Example:
\r
4383 * for (String key : myUnicodeSet.strings()) {
\r
4384 * doSomethingWith(key);
\r
4388 * @provisional This API might change or be removed in a future release.
\r
4390 public Iterable<String> strings() {
\r
4391 return Collections.unmodifiableSortedSet(strings);
\r
4395 * Return the value of the first code point, if the string is exactly one code point. Otherwise return Integer.MAX_VALUE.
\r
4397 * @deprecated This API is ICU internal only.
\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
4404 int result = s.codePointAt(0);
\r
4405 return (result < 0x10000) == (length == 1) ? result : Integer.MAX_VALUE;
\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
4415 * @deprecated This API is ICU internal only.
\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
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
4432 * @deprecated This API is ICU internal only.
\r
4434 public int findIn(CharSequence value, int fromIndex, boolean findNot) {
\r
4435 //TODO add strings, optimize, using ICU4C algorithms
\r
4437 for (; fromIndex < value.length(); fromIndex += UTF16.getCharCount(cp)) {
\r
4438 cp = UTF16.charAt(value, fromIndex);
\r
4439 if (contains(cp) != findNot) {
\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
4452 * @deprecated This API is ICU internal only.
\r
4454 public int findLastIn(CharSequence value, int fromIndex, boolean findNot) {
\r
4455 //TODO add strings, optimize, using ICU4C algorithms
\r
4458 for (; fromIndex >= 0; fromIndex -= UTF16.getCharCount(cp)) {
\r
4459 cp = UTF16.charAt(value, fromIndex);
\r
4460 if (contains(cp) != findNot) {
\r
4464 return fromIndex < 0 ? -1 : fromIndex;
\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
4473 * @deprecated This API is ICU internal only.
\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
4482 return result.toString();
\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
4489 * The functionality is straightforward for sets with only single code points, without strings (which is the common
\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
4496 * complemented (inverted) set and the opposite span conditions yields the same results.
\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
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
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
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
4523 * @provisional This API might change or be removed in a future release.
\r
4525 public enum SpanCondition {
\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
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
4534 * @provisional This API might change or be removed in a future release.
\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
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
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
4550 * @provisional This API might change or be removed in a future release.
\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
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
4560 * If a set only contains single characters, then this is the same as CONTAINED.
\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
4565 * Use this span condition together with other longest-match algorithms, such as ICU converters
\r
4566 * (ucnv_getUnicodeSet()).
\r
4569 * @provisional This API might change or be removed in a future release.
\r
4573 * One more than the last span condition.
\r
4576 * @provisional This API might change or be removed in a future release.
\r