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