1 /* 2 *************************************************************************** 3 * Copyright (C) 1999-2015, International Business Machines Corporation 4 * and others. All Rights Reserved. 5 *************************************************************************** 6 * Date Name Description 7 * 10/20/99 alan Creation. 8 *************************************************************************** 9 */ 10 11 #ifndef UNICODESET_H 12 #define UNICODESET_H 13 14 #include "unicode/unifilt.h" 15 #include "unicode/unistr.h" 16 #include "unicode/uset.h" 17 18 /** 19 * \file 20 * \brief C++ API: Unicode Set 21 */ 22 23 U_NAMESPACE_BEGIN 24 25 // Forward Declarations. 26 void U_CALLCONV UnicodeSet_initInclusion(int32_t src, UErrorCode &status); /**< @internal */ 27 28 class BMPSet; 29 class ParsePosition; 30 class RBBIRuleScanner; 31 class SymbolTable; 32 class UnicodeSetStringSpan; 33 class UVector; 34 class RuleCharacterIterator; 35 36 /** 37 * A mutable set of Unicode characters and multicharacter strings. Objects of this class 38 * represent <em>character classes</em> used in regular expressions. 39 * A character specifies a subset of Unicode code points. Legal 40 * code points are U+0000 to U+10FFFF, inclusive. 41 * 42 * <p>The UnicodeSet class is not designed to be subclassed. 43 * 44 * <p><code>UnicodeSet</code> supports two APIs. The first is the 45 * <em>operand</em> API that allows the caller to modify the value of 46 * a <code>UnicodeSet</code> object. It conforms to Java 2's 47 * <code>java.util.Set</code> interface, although 48 * <code>UnicodeSet</code> does not actually implement that 49 * interface. All methods of <code>Set</code> are supported, with the 50 * modification that they take a character range or single character 51 * instead of an <code>Object</code>, and they take a 52 * <code>UnicodeSet</code> instead of a <code>Collection</code>. The 53 * operand API may be thought of in terms of boolean logic: a boolean 54 * OR is implemented by <code>add</code>, a boolean AND is implemented 55 * by <code>retain</code>, a boolean XOR is implemented by 56 * <code>complement</code> taking an argument, and a boolean NOT is 57 * implemented by <code>complement</code> with no argument. In terms 58 * of traditional set theory function names, <code>add</code> is a 59 * union, <code>retain</code> is an intersection, <code>remove</code> 60 * is an asymmetric difference, and <code>complement</code> with no 61 * argument is a set complement with respect to the superset range 62 * <code>MIN_VALUE-MAX_VALUE</code> 63 * 64 * <p>The second API is the 65 * <code>applyPattern()</code>/<code>toPattern()</code> API from the 66 * <code>java.text.Format</code>-derived classes. Unlike the 67 * methods that add characters, add categories, and control the logic 68 * of the set, the method <code>applyPattern()</code> sets all 69 * attributes of a <code>UnicodeSet</code> at once, based on a 70 * string pattern. 71 * 72 * <p><b>Pattern syntax</b></p> 73 * 74 * Patterns are accepted by the constructors and the 75 * <code>applyPattern()</code> methods and returned by the 76 * <code>toPattern()</code> method. These patterns follow a syntax 77 * similar to that employed by version 8 regular expression character 78 * classes. Here are some simple examples: 79 * 80 * \htmlonly<blockquote>\endhtmlonly 81 * <table> 82 * <tr align="top"> 83 * <td nowrap valign="top" align="left"><code>[]</code></td> 84 * <td valign="top">No characters</td> 85 * </tr><tr align="top"> 86 * <td nowrap valign="top" align="left"><code>[a]</code></td> 87 * <td valign="top">The character 'a'</td> 88 * </tr><tr align="top"> 89 * <td nowrap valign="top" align="left"><code>[ae]</code></td> 90 * <td valign="top">The characters 'a' and 'e'</td> 91 * </tr> 92 * <tr> 93 * <td nowrap valign="top" align="left"><code>[a-e]</code></td> 94 * <td valign="top">The characters 'a' through 'e' inclusive, in Unicode code 95 * point order</td> 96 * </tr> 97 * <tr> 98 * <td nowrap valign="top" align="left"><code>[\\u4E01]</code></td> 99 * <td valign="top">The character U+4E01</td> 100 * </tr> 101 * <tr> 102 * <td nowrap valign="top" align="left"><code>[a{ab}{ac}]</code></td> 103 * <td valign="top">The character 'a' and the multicharacter strings "ab" and 104 * "ac"</td> 105 * </tr> 106 * <tr> 107 * <td nowrap valign="top" align="left"><code>[\\p{Lu}]</code></td> 108 * <td valign="top">All characters in the general category Uppercase Letter</td> 109 * </tr> 110 * </table> 111 * \htmlonly</blockquote>\endhtmlonly 112 * 113 * Any character may be preceded by a backslash in order to remove any special 114 * meaning. White space characters, as defined by UCharacter.isWhitespace(), are 115 * ignored, unless they are escaped. 116 * 117 * <p>Property patterns specify a set of characters having a certain 118 * property as defined by the Unicode standard. Both the POSIX-like 119 * "[:Lu:]" and the Perl-like syntax "\\p{Lu}" are recognized. For a 120 * complete list of supported property patterns, see the User's Guide 121 * for UnicodeSet at 122 * <a href="http://icu-project.org/userguide/unicodeSet.html"> 123 * http://icu-project.org/userguide/unicodeSet.html</a>. 124 * Actual determination of property data is defined by the underlying 125 * Unicode database as implemented by UCharacter. 126 * 127 * <p>Patterns specify individual characters, ranges of characters, and 128 * Unicode property sets. When elements are concatenated, they 129 * specify their union. To complement a set, place a '^' immediately 130 * after the opening '['. Property patterns are inverted by modifying 131 * their delimiters; "[:^foo]" and "\\P{foo}". In any other location, 132 * '^' has no special meaning. 133 * 134 * <p>Ranges are indicated by placing two a '-' between two 135 * characters, as in "a-z". This specifies the range of all 136 * characters from the left to the right, in Unicode order. If the 137 * left character is greater than or equal to the 138 * right character it is a syntax error. If a '-' occurs as the first 139 * character after the opening '[' or '[^', or if it occurs as the 140 * last character before the closing ']', then it is taken as a 141 * literal. Thus "[a\-b]", "[-ab]", and "[ab-]" all indicate the same 142 * set of three characters, 'a', 'b', and '-'. 143 * 144 * <p>Sets may be intersected using the '&' operator or the asymmetric 145 * set difference may be taken using the '-' operator, for example, 146 * "[[:L:]&[\\u0000-\\u0FFF]]" indicates the set of all Unicode letters 147 * with values less than 4096. Operators ('&' and '|') have equal 148 * precedence and bind left-to-right. Thus 149 * "[[:L:]-[a-z]-[\\u0100-\\u01FF]]" is equivalent to 150 * "[[[:L:]-[a-z]]-[\\u0100-\\u01FF]]". This only really matters for 151 * difference; intersection is commutative. 152 * 153 * <table> 154 * <tr valign=top><td nowrap><code>[a]</code><td>The set containing 'a' 155 * <tr valign=top><td nowrap><code>[a-z]</code><td>The set containing 'a' 156 * through 'z' and all letters in between, in Unicode order 157 * <tr valign=top><td nowrap><code>[^a-z]</code><td>The set containing 158 * all characters but 'a' through 'z', 159 * that is, U+0000 through 'a'-1 and 'z'+1 through U+10FFFF 160 * <tr valign=top><td nowrap><code>[[<em>pat1</em>][<em>pat2</em>]]</code> 161 * <td>The union of sets specified by <em>pat1</em> and <em>pat2</em> 162 * <tr valign=top><td nowrap><code>[[<em>pat1</em>]&[<em>pat2</em>]]</code> 163 * <td>The intersection of sets specified by <em>pat1</em> and <em>pat2</em> 164 * <tr valign=top><td nowrap><code>[[<em>pat1</em>]-[<em>pat2</em>]]</code> 165 * <td>The asymmetric difference of sets specified by <em>pat1</em> and 166 * <em>pat2</em> 167 * <tr valign=top><td nowrap><code>[:Lu:] or \\p{Lu}</code> 168 * <td>The set of characters having the specified 169 * Unicode property; in 170 * this case, Unicode uppercase letters 171 * <tr valign=top><td nowrap><code>[:^Lu:] or \\P{Lu}</code> 172 * <td>The set of characters <em>not</em> having the given 173 * Unicode property 174 * </table> 175 * 176 * <p><b>Warning</b>: you cannot add an empty string ("") to a UnicodeSet.</p> 177 * 178 * <p><b>Formal syntax</b></p> 179 * 180 * \htmlonly<blockquote>\endhtmlonly 181 * <table> 182 * <tr align="top"> 183 * <td nowrap valign="top" align="right"><code>pattern := </code></td> 184 * <td valign="top"><code>('[' '^'? item* ']') | 185 * property</code></td> 186 * </tr> 187 * <tr align="top"> 188 * <td nowrap valign="top" align="right"><code>item := </code></td> 189 * <td valign="top"><code>char | (char '-' char) | pattern-expr<br> 190 * </code></td> 191 * </tr> 192 * <tr align="top"> 193 * <td nowrap valign="top" align="right"><code>pattern-expr := </code></td> 194 * <td valign="top"><code>pattern | pattern-expr pattern | 195 * pattern-expr op pattern<br> 196 * </code></td> 197 * </tr> 198 * <tr align="top"> 199 * <td nowrap valign="top" align="right"><code>op := </code></td> 200 * <td valign="top"><code>'&' | '-'<br> 201 * </code></td> 202 * </tr> 203 * <tr align="top"> 204 * <td nowrap valign="top" align="right"><code>special := </code></td> 205 * <td valign="top"><code>'[' | ']' | '-'<br> 206 * </code></td> 207 * </tr> 208 * <tr align="top"> 209 * <td nowrap valign="top" align="right"><code>char := </code></td> 210 * <td valign="top"><em>any character that is not</em><code> special<br> 211 * | ('\' </code><em>any character</em><code>)<br> 212 * | ('\\u' hex hex hex hex)<br> 213 * </code></td> 214 * </tr> 215 * <tr align="top"> 216 * <td nowrap valign="top" align="right"><code>hex := </code></td> 217 * <td valign="top"><em>any character for which 218 * </em><code>Character.digit(c, 16)</code><em> 219 * returns a non-negative result</em></td> 220 * </tr> 221 * <tr> 222 * <td nowrap valign="top" align="right"><code>property := </code></td> 223 * <td valign="top"><em>a Unicode property set pattern</em></td> 224 * </tr> 225 * </table> 226 * <br> 227 * <table border="1"> 228 * <tr> 229 * <td>Legend: <table> 230 * <tr> 231 * <td nowrap valign="top"><code>a := b</code></td> 232 * <td width="20" valign="top"> </td> 233 * <td valign="top"><code>a</code> may be replaced by <code>b</code> </td> 234 * </tr> 235 * <tr> 236 * <td nowrap valign="top"><code>a?</code></td> 237 * <td valign="top"></td> 238 * <td valign="top">zero or one instance of <code>a</code><br> 239 * </td> 240 * </tr> 241 * <tr> 242 * <td nowrap valign="top"><code>a*</code></td> 243 * <td valign="top"></td> 244 * <td valign="top">one or more instances of <code>a</code><br> 245 * </td> 246 * </tr> 247 * <tr> 248 * <td nowrap valign="top"><code>a | b</code></td> 249 * <td valign="top"></td> 250 * <td valign="top">either <code>a</code> or <code>b</code><br> 251 * </td> 252 * </tr> 253 * <tr> 254 * <td nowrap valign="top"><code>'a'</code></td> 255 * <td valign="top"></td> 256 * <td valign="top">the literal string between the quotes </td> 257 * </tr> 258 * </table> 259 * </td> 260 * </tr> 261 * </table> 262 * \htmlonly</blockquote>\endhtmlonly 263 * 264 * <p>Note: 265 * - Most UnicodeSet methods do not take a UErrorCode parameter because 266 * there are usually very few opportunities for failure other than a shortage 267 * of memory, error codes in low-level C++ string methods would be inconvenient, 268 * and the error code as the last parameter (ICU convention) would prevent 269 * the use of default parameter values. 270 * Instead, such methods set the UnicodeSet into a "bogus" state 271 * (see isBogus()) if an error occurs. 272 * 273 * @author Alan Liu 274 * @stable ICU 2.0 275 */ 276 class U_COMMON_API UnicodeSet U_FINAL : public UnicodeFilter { 277 278 int32_t len; // length of list used; 0 <= len <= capacity 279 int32_t capacity; // capacity of list 280 UChar32* list; // MUST be terminated with HIGH 281 BMPSet *bmpSet; // The set is frozen iff either bmpSet or stringSpan is not NULL. 282 UChar32* buffer; // internal buffer, may be NULL 283 int32_t bufferCapacity; // capacity of buffer 284 int32_t patLen; 285 286 /** 287 * The pattern representation of this set. This may not be the 288 * most economical pattern. It is the pattern supplied to 289 * applyPattern(), with variables substituted and whitespace 290 * removed. For sets constructed without applyPattern(), or 291 * modified using the non-pattern API, this string will be empty, 292 * indicating that toPattern() must generate a pattern 293 * representation from the inversion list. 294 */ 295 UChar *pat; 296 UVector* strings; // maintained in sorted order 297 UnicodeSetStringSpan *stringSpan; 298 299 private: 300 enum { // constants 301 kIsBogus = 1 // This set is bogus (i.e. not valid) 302 }; 303 uint8_t fFlags; // Bit flag (see constants above) 304 public: 305 /** 306 * Determine if this object contains a valid set. 307 * A bogus set has no value. It is different from an empty set. 308 * It can be used to indicate that no set value is available. 309 * 310 * @return TRUE if the set is valid, FALSE otherwise 311 * @see setToBogus() 312 * @stable ICU 4.0 313 */ 314 inline UBool isBogus(void) const; 315 316 /** 317 * Make this UnicodeSet object invalid. 318 * The string will test TRUE with isBogus(). 319 * 320 * A bogus set has no value. It is different from an empty set. 321 * It can be used to indicate that no set value is available. 322 * 323 * This utility function is used throughout the UnicodeSet 324 * implementation to indicate that a UnicodeSet operation failed, 325 * and may be used in other functions, 326 * especially but not exclusively when such functions do not 327 * take a UErrorCode for simplicity. 328 * 329 * @see isBogus() 330 * @stable ICU 4.0 331 */ 332 void setToBogus(); 333 334 public: 335 336 enum { 337 /** 338 * Minimum value that can be stored in a UnicodeSet. 339 * @stable ICU 2.4 340 */ 341 MIN_VALUE = 0, 342 343 /** 344 * Maximum value that can be stored in a UnicodeSet. 345 * @stable ICU 2.4 346 */ 347 MAX_VALUE = 0x10ffff 348 }; 349 350 //---------------------------------------------------------------- 351 // Constructors &c 352 //---------------------------------------------------------------- 353 354 public: 355 356 /** 357 * Constructs an empty set. 358 * @stable ICU 2.0 359 */ 360 UnicodeSet(); 361 362 /** 363 * Constructs a set containing the given range. If <code>end > 364 * start</code> then an empty set is created. 365 * 366 * @param start first character, inclusive, of range 367 * @param end last character, inclusive, of range 368 * @stable ICU 2.4 369 */ 370 UnicodeSet(UChar32 start, UChar32 end); 371 372 #ifndef U_HIDE_INTERNAL_API 373 /** 374 * @internal 375 */ 376 enum ESerialization { 377 kSerialized /* result of serialize() */ 378 }; 379 380 /** 381 * Constructs a set from the output of serialize(). 382 * 383 * @param buffer the 16 bit array 384 * @param bufferLen the original length returned from serialize() 385 * @param serialization the value 'kSerialized' 386 * @param status error code 387 * 388 * @internal 389 */ 390 UnicodeSet(const uint16_t buffer[], int32_t bufferLen, 391 ESerialization serialization, UErrorCode &status); 392 #endif /* U_HIDE_INTERNAL_API */ 393 394 /** 395 * Constructs a set from the given pattern. See the class 396 * description for the syntax of the pattern language. 397 * @param pattern a string specifying what characters are in the set 398 * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern 399 * contains a syntax error. 400 * @stable ICU 2.0 401 */ 402 UnicodeSet(const UnicodeString& pattern, 403 UErrorCode& status); 404 405 #ifndef U_HIDE_INTERNAL_API 406 /** 407 * Constructs a set from the given pattern. See the class 408 * description for the syntax of the pattern language. 409 * @param pattern a string specifying what characters are in the set 410 * @param options bitmask for options to apply to the pattern. 411 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE. 412 * @param symbols a symbol table mapping variable names to values 413 * and stand-in characters to UnicodeSets; may be NULL 414 * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern 415 * contains a syntax error. 416 * @internal 417 */ 418 UnicodeSet(const UnicodeString& pattern, 419 uint32_t options, 420 const SymbolTable* symbols, 421 UErrorCode& status); 422 #endif /* U_HIDE_INTERNAL_API */ 423 424 /** 425 * Constructs a set from the given pattern. See the class description 426 * for the syntax of the pattern language. 427 * @param pattern a string specifying what characters are in the set 428 * @param pos on input, the position in pattern at which to start parsing. 429 * On output, the position after the last character parsed. 430 * @param options bitmask for options to apply to the pattern. 431 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE. 432 * @param symbols a symbol table mapping variable names to values 433 * and stand-in characters to UnicodeSets; may be NULL 434 * @param status input-output error code 435 * @stable ICU 2.8 436 */ 437 UnicodeSet(const UnicodeString& pattern, ParsePosition& pos, 438 uint32_t options, 439 const SymbolTable* symbols, 440 UErrorCode& status); 441 442 /** 443 * Constructs a set that is identical to the given UnicodeSet. 444 * @stable ICU 2.0 445 */ 446 UnicodeSet(const UnicodeSet& o); 447 448 /** 449 * Destructs the set. 450 * @stable ICU 2.0 451 */ 452 virtual ~UnicodeSet(); 453 454 /** 455 * Assigns this object to be a copy of another. 456 * A frozen set will not be modified. 457 * @stable ICU 2.0 458 */ 459 UnicodeSet& operator=(const UnicodeSet& o); 460 461 /** 462 * Compares the specified object with this set for equality. Returns 463 * <tt>true</tt> if the two sets 464 * have the same size, and every member of the specified set is 465 * contained in this set (or equivalently, every member of this set is 466 * contained in the specified set). 467 * 468 * @param o set to be compared for equality with this set. 469 * @return <tt>true</tt> if the specified set is equal to this set. 470 * @stable ICU 2.0 471 */ 472 virtual UBool operator==(const UnicodeSet& o) const; 473 474 /** 475 * Compares the specified object with this set for equality. Returns 476 * <tt>true</tt> if the specified set is not equal to this set. 477 * @stable ICU 2.0 478 */ 479 UBool operator!=(const UnicodeSet& o) const; 480 481 /** 482 * Returns a copy of this object. All UnicodeFunctor objects have 483 * to support cloning in order to allow classes using 484 * UnicodeFunctors, such as Transliterator, to implement cloning. 485 * If this set is frozen, then the clone will be frozen as well. 486 * Use cloneAsThawed() for a mutable clone of a frozen set. 487 * @see cloneAsThawed 488 * @stable ICU 2.0 489 */ 490 virtual UnicodeFunctor* clone() const; 491 492 /** 493 * Returns the hash code value for this set. 494 * 495 * @return the hash code value for this set. 496 * @see Object#hashCode() 497 * @stable ICU 2.0 498 */ 499 virtual int32_t hashCode(void) const; 500 501 /** 502 * Get a UnicodeSet pointer from a USet 503 * 504 * @param uset a USet (the ICU plain C type for UnicodeSet) 505 * @return the corresponding UnicodeSet pointer. 506 * 507 * @stable ICU 4.2 508 */ 509 inline static UnicodeSet *fromUSet(USet *uset); 510 511 /** 512 * Get a UnicodeSet pointer from a const USet 513 * 514 * @param uset a const USet (the ICU plain C type for UnicodeSet) 515 * @return the corresponding UnicodeSet pointer. 516 * 517 * @stable ICU 4.2 518 */ 519 inline static const UnicodeSet *fromUSet(const USet *uset); 520 521 /** 522 * Produce a USet * pointer for this UnicodeSet. 523 * USet is the plain C type for UnicodeSet 524 * 525 * @return a USet pointer for this UnicodeSet 526 * @stable ICU 4.2 527 */ 528 inline USet *toUSet(); 529 530 531 /** 532 * Produce a const USet * pointer for this UnicodeSet. 533 * USet is the plain C type for UnicodeSet 534 * 535 * @return a const USet pointer for this UnicodeSet 536 * @stable ICU 4.2 537 */ 538 inline const USet * toUSet() const; 539 540 541 //---------------------------------------------------------------- 542 // Freezable API 543 //---------------------------------------------------------------- 544 545 /** 546 * Determines whether the set has been frozen (made immutable) or not. 547 * See the ICU4J Freezable interface for details. 548 * @return TRUE/FALSE for whether the set has been frozen 549 * @see freeze 550 * @see cloneAsThawed 551 * @stable ICU 3.8 552 */ 553 inline UBool isFrozen() const; 554 555 /** 556 * Freeze the set (make it immutable). 557 * Once frozen, it cannot be unfrozen and is therefore thread-safe 558 * until it is deleted. 559 * See the ICU4J Freezable interface for details. 560 * Freezing the set may also make some operations faster, for example 561 * contains() and span(). 562 * A frozen set will not be modified. (It remains frozen.) 563 * @return this set. 564 * @see isFrozen 565 * @see cloneAsThawed 566 * @stable ICU 3.8 567 */ 568 UnicodeFunctor *freeze(); 569 570 /** 571 * Clone the set and make the clone mutable. 572 * See the ICU4J Freezable interface for details. 573 * @return the mutable clone 574 * @see freeze 575 * @see isFrozen 576 * @stable ICU 3.8 577 */ 578 UnicodeFunctor *cloneAsThawed() const; 579 580 //---------------------------------------------------------------- 581 // Public API 582 //---------------------------------------------------------------- 583 584 /** 585 * Make this object represent the range <code>start - end</code>. 586 * If <code>end > start</code> then this object is set to an 587 * an empty range. 588 * A frozen set will not be modified. 589 * 590 * @param start first character in the set, inclusive 591 * @param end last character in the set, inclusive 592 * @stable ICU 2.4 593 */ 594 UnicodeSet& set(UChar32 start, UChar32 end); 595 596 /** 597 * Return true if the given position, in the given pattern, appears 598 * to be the start of a UnicodeSet pattern. 599 * @stable ICU 2.4 600 */ 601 static UBool resemblesPattern(const UnicodeString& pattern, 602 int32_t pos); 603 604 /** 605 * Modifies this set to represent the set specified by the given 606 * pattern, ignoring Unicode Pattern_White_Space characters. 607 * See the class description for the syntax of the pattern language. 608 * A frozen set will not be modified. 609 * @param pattern a string specifying what characters are in the set 610 * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern 611 * contains a syntax error. 612 * <em> Empties the set passed before applying the pattern.</em> 613 * @return a reference to this 614 * @stable ICU 2.0 615 */ 616 UnicodeSet& applyPattern(const UnicodeString& pattern, 617 UErrorCode& status); 618 619 #ifndef U_HIDE_INTERNAL_API 620 /** 621 * Modifies this set to represent the set specified by the given 622 * pattern, optionally ignoring Unicode Pattern_White_Space characters. 623 * See the class description for the syntax of the pattern language. 624 * A frozen set will not be modified. 625 * @param pattern a string specifying what characters are in the set 626 * @param options bitmask for options to apply to the pattern. 627 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE. 628 * @param symbols a symbol table mapping variable names to 629 * values and stand-ins to UnicodeSets; may be NULL 630 * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern 631 * contains a syntax error. 632 *<em> Empties the set passed before applying the pattern.</em> 633 * @return a reference to this 634 * @internal 635 */ 636 UnicodeSet& applyPattern(const UnicodeString& pattern, 637 uint32_t options, 638 const SymbolTable* symbols, 639 UErrorCode& status); 640 #endif /* U_HIDE_INTERNAL_API */ 641 642 /** 643 * Parses the given pattern, starting at the given position. The 644 * character at pattern.charAt(pos.getIndex()) must be '[', or the 645 * parse fails. Parsing continues until the corresponding closing 646 * ']'. If a syntax error is encountered between the opening and 647 * closing brace, the parse fails. Upon return from a successful 648 * parse, the ParsePosition is updated to point to the character 649 * following the closing ']', and a StringBuffer containing a 650 * pairs list for the parsed pattern is returned. This method calls 651 * itself recursively to parse embedded subpatterns. 652 *<em> Empties the set passed before applying the pattern.</em> 653 * A frozen set will not be modified. 654 * 655 * @param pattern the string containing the pattern to be parsed. 656 * The portion of the string from pos.getIndex(), which must be a 657 * '[', to the corresponding closing ']', is parsed. 658 * @param pos upon entry, the position at which to being parsing. 659 * The character at pattern.charAt(pos.getIndex()) must be a '['. 660 * Upon return from a successful parse, pos.getIndex() is either 661 * the character after the closing ']' of the parsed pattern, or 662 * pattern.length() if the closing ']' is the last character of 663 * the pattern string. 664 * @param options bitmask for options to apply to the pattern. 665 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE. 666 * @param symbols a symbol table mapping variable names to 667 * values and stand-ins to UnicodeSets; may be NULL 668 * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern 669 * contains a syntax error. 670 * @return a reference to this 671 * @stable ICU 2.8 672 */ 673 UnicodeSet& applyPattern(const UnicodeString& pattern, 674 ParsePosition& pos, 675 uint32_t options, 676 const SymbolTable* symbols, 677 UErrorCode& status); 678 679 /** 680 * Returns a string representation of this set. If the result of 681 * calling this function is passed to a UnicodeSet constructor, it 682 * will produce another set that is equal to this one. 683 * A frozen set will not be modified. 684 * @param result the string to receive the rules. Previous 685 * contents will be deleted. 686 * @param escapeUnprintable if TRUE then convert unprintable 687 * character to their hex escape representations, \\uxxxx or 688 * \\Uxxxxxxxx. Unprintable characters are those other than 689 * U+000A, U+0020..U+007E. 690 * @stable ICU 2.0 691 */ 692 virtual UnicodeString& toPattern(UnicodeString& result, 693 UBool escapeUnprintable = FALSE) const; 694 695 /** 696 * Modifies this set to contain those code points which have the given value 697 * for the given binary or enumerated property, as returned by 698 * u_getIntPropertyValue. Prior contents of this set are lost. 699 * A frozen set will not be modified. 700 * 701 * @param prop a property in the range UCHAR_BIN_START..UCHAR_BIN_LIMIT-1 702 * or UCHAR_INT_START..UCHAR_INT_LIMIT-1 703 * or UCHAR_MASK_START..UCHAR_MASK_LIMIT-1. 704 * 705 * @param value a value in the range u_getIntPropertyMinValue(prop).. 706 * u_getIntPropertyMaxValue(prop), with one exception. If prop is 707 * UCHAR_GENERAL_CATEGORY_MASK, then value should not be a UCharCategory, but 708 * rather a mask value produced by U_GET_GC_MASK(). This allows grouped 709 * categories such as [:L:] to be represented. 710 * 711 * @param ec error code input/output parameter 712 * 713 * @return a reference to this set 714 * 715 * @stable ICU 2.4 716 */ 717 UnicodeSet& applyIntPropertyValue(UProperty prop, 718 int32_t value, 719 UErrorCode& ec); 720 721 /** 722 * Modifies this set to contain those code points which have the 723 * given value for the given property. Prior contents of this 724 * set are lost. 725 * A frozen set will not be modified. 726 * 727 * @param prop a property alias, either short or long. The name is matched 728 * loosely. See PropertyAliases.txt for names and a description of loose 729 * matching. If the value string is empty, then this string is interpreted 730 * as either a General_Category value alias, a Script value alias, a binary 731 * property alias, or a special ID. Special IDs are matched loosely and 732 * correspond to the following sets: 733 * 734 * "ANY" = [\\u0000-\\U0010FFFF], 735 * "ASCII" = [\\u0000-\\u007F], 736 * "Assigned" = [:^Cn:]. 737 * 738 * @param value a value alias, either short or long. The name is matched 739 * loosely. See PropertyValueAliases.txt for names and a description of 740 * loose matching. In addition to aliases listed, numeric values and 741 * canonical combining classes may be expressed numerically, e.g., ("nv", 742 * "0.5") or ("ccc", "220"). The value string may also be empty. 743 * 744 * @param ec error code input/output parameter 745 * 746 * @return a reference to this set 747 * 748 * @stable ICU 2.4 749 */ 750 UnicodeSet& applyPropertyAlias(const UnicodeString& prop, 751 const UnicodeString& value, 752 UErrorCode& ec); 753 754 /** 755 * Returns the number of elements in this set (its cardinality). 756 * Note than the elements of a set may include both individual 757 * codepoints and strings. 758 * 759 * @return the number of elements in this set (its cardinality). 760 * @stable ICU 2.0 761 */ 762 virtual int32_t size(void) const; 763 764 /** 765 * Returns <tt>true</tt> if this set contains no elements. 766 * 767 * @return <tt>true</tt> if this set contains no elements. 768 * @stable ICU 2.0 769 */ 770 virtual UBool isEmpty(void) const; 771 772 /** 773 * Returns true if this set contains the given character. 774 * This function works faster with a frozen set. 775 * @param c character to be checked for containment 776 * @return true if the test condition is met 777 * @stable ICU 2.0 778 */ 779 virtual UBool contains(UChar32 c) const; 780 781 /** 782 * Returns true if this set contains every character 783 * of the given range. 784 * @param start first character, inclusive, of the range 785 * @param end last character, inclusive, of the range 786 * @return true if the test condition is met 787 * @stable ICU 2.0 788 */ 789 virtual UBool contains(UChar32 start, UChar32 end) const; 790 791 /** 792 * Returns <tt>true</tt> if this set contains the given 793 * multicharacter string. 794 * @param s string to be checked for containment 795 * @return <tt>true</tt> if this set contains the specified string 796 * @stable ICU 2.4 797 */ 798 UBool contains(const UnicodeString& s) const; 799 800 /** 801 * Returns true if this set contains all the characters and strings 802 * of the given set. 803 * @param c set to be checked for containment 804 * @return true if the test condition is met 805 * @stable ICU 2.4 806 */ 807 virtual UBool containsAll(const UnicodeSet& c) const; 808 809 /** 810 * Returns true if this set contains all the characters 811 * of the given string. 812 * @param s string containing characters to be checked for containment 813 * @return true if the test condition is met 814 * @stable ICU 2.4 815 */ 816 UBool containsAll(const UnicodeString& s) const; 817 818 /** 819 * Returns true if this set contains none of the characters 820 * of the given range. 821 * @param start first character, inclusive, of the range 822 * @param end last character, inclusive, of the range 823 * @return true if the test condition is met 824 * @stable ICU 2.4 825 */ 826 UBool containsNone(UChar32 start, UChar32 end) const; 827 828 /** 829 * Returns true if this set contains none of the characters and strings 830 * of the given set. 831 * @param c set to be checked for containment 832 * @return true if the test condition is met 833 * @stable ICU 2.4 834 */ 835 UBool containsNone(const UnicodeSet& c) const; 836 837 /** 838 * Returns true if this set contains none of the characters 839 * of the given string. 840 * @param s string containing characters to be checked for containment 841 * @return true if the test condition is met 842 * @stable ICU 2.4 843 */ 844 UBool containsNone(const UnicodeString& s) const; 845 846 /** 847 * Returns true if this set contains one or more of the characters 848 * in the given range. 849 * @param start first character, inclusive, of the range 850 * @param end last character, inclusive, of the range 851 * @return true if the condition is met 852 * @stable ICU 2.4 853 */ 854 inline UBool containsSome(UChar32 start, UChar32 end) const; 855 856 /** 857 * Returns true if this set contains one or more of the characters 858 * and strings of the given set. 859 * @param s The set to be checked for containment 860 * @return true if the condition is met 861 * @stable ICU 2.4 862 */ 863 inline UBool containsSome(const UnicodeSet& s) const; 864 865 /** 866 * Returns true if this set contains one or more of the characters 867 * of the given string. 868 * @param s string containing characters to be checked for containment 869 * @return true if the condition is met 870 * @stable ICU 2.4 871 */ 872 inline UBool containsSome(const UnicodeString& s) const; 873 874 /** 875 * Returns the length of the initial substring of the input string which 876 * consists only of characters and strings that are contained in this set 877 * (USET_SPAN_CONTAINED, USET_SPAN_SIMPLE), 878 * or only of characters and strings that are not contained 879 * in this set (USET_SPAN_NOT_CONTAINED). 880 * See USetSpanCondition for details. 881 * Similar to the strspn() C library function. 882 * Unpaired surrogates are treated according to contains() of their surrogate code points. 883 * This function works faster with a frozen set and with a non-negative string length argument. 884 * @param s start of the string 885 * @param length of the string; can be -1 for NUL-terminated 886 * @param spanCondition specifies the containment condition 887 * @return the length of the initial substring according to the spanCondition; 888 * 0 if the start of the string does not fit the spanCondition 889 * @stable ICU 3.8 890 * @see USetSpanCondition 891 */ 892 int32_t span(const UChar *s, int32_t length, USetSpanCondition spanCondition) const; 893 894 /** 895 * Returns the end of the substring of the input string according to the USetSpanCondition. 896 * Same as <code>start+span(s.getBuffer()+start, s.length()-start, spanCondition)</code> 897 * after pinning start to 0<=start<=s.length(). 898 * @param s the string 899 * @param start the start index in the string for the span operation 900 * @param spanCondition specifies the containment condition 901 * @return the exclusive end of the substring according to the spanCondition; 902 * the substring s.tempSubStringBetween(start, end) fulfills the spanCondition 903 * @stable ICU 4.4 904 * @see USetSpanCondition 905 */ 906 inline int32_t span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const; 907 908 /** 909 * Returns the start of the trailing substring of the input string which 910 * consists only of characters and strings that are contained in this set 911 * (USET_SPAN_CONTAINED, USET_SPAN_SIMPLE), 912 * or only of characters and strings that are not contained 913 * in this set (USET_SPAN_NOT_CONTAINED). 914 * See USetSpanCondition for details. 915 * Unpaired surrogates are treated according to contains() of their surrogate code points. 916 * This function works faster with a frozen set and with a non-negative string length argument. 917 * @param s start of the string 918 * @param length of the string; can be -1 for NUL-terminated 919 * @param spanCondition specifies the containment condition 920 * @return the start of the trailing substring according to the spanCondition; 921 * the string length if the end of the string does not fit the spanCondition 922 * @stable ICU 3.8 923 * @see USetSpanCondition 924 */ 925 int32_t spanBack(const UChar *s, int32_t length, USetSpanCondition spanCondition) const; 926 927 /** 928 * Returns the start of the substring of the input string according to the USetSpanCondition. 929 * Same as <code>spanBack(s.getBuffer(), limit, spanCondition)</code> 930 * after pinning limit to 0<=end<=s.length(). 931 * @param s the string 932 * @param limit the exclusive-end index in the string for the span operation 933 * (use s.length() or INT32_MAX for spanning back from the end of the string) 934 * @param spanCondition specifies the containment condition 935 * @return the start of the substring according to the spanCondition; 936 * the substring s.tempSubStringBetween(start, limit) fulfills the spanCondition 937 * @stable ICU 4.4 938 * @see USetSpanCondition 939 */ 940 inline int32_t spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const; 941 942 /** 943 * Returns the length of the initial substring of the input string which 944 * consists only of characters and strings that are contained in this set 945 * (USET_SPAN_CONTAINED, USET_SPAN_SIMPLE), 946 * or only of characters and strings that are not contained 947 * in this set (USET_SPAN_NOT_CONTAINED). 948 * See USetSpanCondition for details. 949 * Similar to the strspn() C library function. 950 * Malformed byte sequences are treated according to contains(0xfffd). 951 * This function works faster with a frozen set and with a non-negative string length argument. 952 * @param s start of the string (UTF-8) 953 * @param length of the string; can be -1 for NUL-terminated 954 * @param spanCondition specifies the containment condition 955 * @return the length of the initial substring according to the spanCondition; 956 * 0 if the start of the string does not fit the spanCondition 957 * @stable ICU 3.8 958 * @see USetSpanCondition 959 */ 960 int32_t spanUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const; 961 962 /** 963 * Returns the start of the trailing substring of the input string which 964 * consists only of characters and strings that are contained in this set 965 * (USET_SPAN_CONTAINED, USET_SPAN_SIMPLE), 966 * or only of characters and strings that are not contained 967 * in this set (USET_SPAN_NOT_CONTAINED). 968 * See USetSpanCondition for details. 969 * Malformed byte sequences are treated according to contains(0xfffd). 970 * This function works faster with a frozen set and with a non-negative string length argument. 971 * @param s start of the string (UTF-8) 972 * @param length of the string; can be -1 for NUL-terminated 973 * @param spanCondition specifies the containment condition 974 * @return the start of the trailing substring according to the spanCondition; 975 * the string length if the end of the string does not fit the spanCondition 976 * @stable ICU 3.8 977 * @see USetSpanCondition 978 */ 979 int32_t spanBackUTF8(const char *s, int32_t length, USetSpanCondition spanCondition) const; 980 981 /** 982 * Implement UnicodeMatcher::matches() 983 * @stable ICU 2.4 984 */ 985 virtual UMatchDegree matches(const Replaceable& text, 986 int32_t& offset, 987 int32_t limit, 988 UBool incremental); 989 990 private: 991 /** 992 * Returns the longest match for s in text at the given position. 993 * If limit > start then match forward from start+1 to limit 994 * matching all characters except s.charAt(0). If limit < start, 995 * go backward starting from start-1 matching all characters 996 * except s.charAt(s.length()-1). This method assumes that the 997 * first character, text.charAt(start), matches s, so it does not 998 * check it. 999 * @param text the text to match 1000 * @param start the first character to match. In the forward 1001 * direction, text.charAt(start) is matched against s.charAt(0). 1002 * In the reverse direction, it is matched against 1003 * s.charAt(s.length()-1). 1004 * @param limit the limit offset for matching, either last+1 in 1005 * the forward direction, or last-1 in the reverse direction, 1006 * where last is the index of the last character to match. 1007 * @param s 1008 * @return If part of s matches up to the limit, return |limit - 1009 * start|. If all of s matches before reaching the limit, return 1010 * s.length(). If there is a mismatch between s and text, return 1011 * 0 1012 */ 1013 static int32_t matchRest(const Replaceable& text, 1014 int32_t start, int32_t limit, 1015 const UnicodeString& s); 1016 1017 /** 1018 * Returns the smallest value i such that c < list[i]. Caller 1019 * must ensure that c is a legal value or this method will enter 1020 * an infinite loop. This method performs a binary search. 1021 * @param c a character in the range MIN_VALUE..MAX_VALUE 1022 * inclusive 1023 * @return the smallest integer i in the range 0..len-1, 1024 * inclusive, such that c < list[i] 1025 */ 1026 int32_t findCodePoint(UChar32 c) const; 1027 1028 public: 1029 1030 /** 1031 * Implementation of UnicodeMatcher API. Union the set of all 1032 * characters that may be matched by this object into the given 1033 * set. 1034 * @param toUnionTo the set into which to union the source characters 1035 * @stable ICU 2.4 1036 */ 1037 virtual void addMatchSetTo(UnicodeSet& toUnionTo) const; 1038 1039 /** 1040 * Returns the index of the given character within this set, where 1041 * the set is ordered by ascending code point. If the character 1042 * is not in this set, return -1. The inverse of this method is 1043 * <code>charAt()</code>. 1044 * @return an index from 0..size()-1, or -1 1045 * @stable ICU 2.4 1046 */ 1047 int32_t indexOf(UChar32 c) const; 1048 1049 /** 1050 * Returns the character at the given index within this set, where 1051 * the set is ordered by ascending code point. If the index is 1052 * out of range, return (UChar32)-1. The inverse of this method is 1053 * <code>indexOf()</code>. 1054 * @param index an index from 0..size()-1 1055 * @return the character at the given index, or (UChar32)-1. 1056 * @stable ICU 2.4 1057 */ 1058 UChar32 charAt(int32_t index) const; 1059 1060 /** 1061 * Adds the specified range to this set if it is not already 1062 * present. If this set already contains the specified range, 1063 * the call leaves this set unchanged. If <code>end > start</code> 1064 * then an empty range is added, leaving the set unchanged. 1065 * This is equivalent to a boolean logic OR, or a set UNION. 1066 * A frozen set will not be modified. 1067 * 1068 * @param start first character, inclusive, of range to be added 1069 * to this set. 1070 * @param end last character, inclusive, of range to be added 1071 * to this set. 1072 * @stable ICU 2.0 1073 */ 1074 virtual UnicodeSet& add(UChar32 start, UChar32 end); 1075 1076 /** 1077 * Adds the specified character to this set if it is not already 1078 * present. If this set already contains the specified character, 1079 * the call leaves this set unchanged. 1080 * A frozen set will not be modified. 1081 * @stable ICU 2.0 1082 */ 1083 UnicodeSet& add(UChar32 c); 1084 1085 /** 1086 * Adds the specified multicharacter to this set if it is not already 1087 * present. If this set already contains the multicharacter, 1088 * the call leaves this set unchanged. 1089 * Thus "ch" => {"ch"} 1090 * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b> 1091 * A frozen set will not be modified. 1092 * @param s the source string 1093 * @return this object, for chaining 1094 * @stable ICU 2.4 1095 */ 1096 UnicodeSet& add(const UnicodeString& s); 1097 1098 private: 1099 /** 1100 * @return a code point IF the string consists of a single one. 1101 * otherwise returns -1. 1102 * @param s string to test 1103 */ 1104 static int32_t getSingleCP(const UnicodeString& s); 1105 1106 void _add(const UnicodeString& s); 1107 1108 public: 1109 /** 1110 * Adds each of the characters in this string to the set. Thus "ch" => {"c", "h"} 1111 * If this set already any particular character, it has no effect on that character. 1112 * A frozen set will not be modified. 1113 * @param s the source string 1114 * @return this object, for chaining 1115 * @stable ICU 2.4 1116 */ 1117 UnicodeSet& addAll(const UnicodeString& s); 1118 1119 /** 1120 * Retains EACH of the characters in this string. Note: "ch" == {"c", "h"} 1121 * If this set already any particular character, it has no effect on that character. 1122 * A frozen set will not be modified. 1123 * @param s the source string 1124 * @return this object, for chaining 1125 * @stable ICU 2.4 1126 */ 1127 UnicodeSet& retainAll(const UnicodeString& s); 1128 1129 /** 1130 * Complement EACH of the characters in this string. Note: "ch" == {"c", "h"} 1131 * If this set already any particular character, it has no effect on that character. 1132 * A frozen set will not be modified. 1133 * @param s the source string 1134 * @return this object, for chaining 1135 * @stable ICU 2.4 1136 */ 1137 UnicodeSet& complementAll(const UnicodeString& s); 1138 1139 /** 1140 * Remove EACH of the characters in this string. Note: "ch" == {"c", "h"} 1141 * If this set already any particular character, it has no effect on that character. 1142 * A frozen set will not be modified. 1143 * @param s the source string 1144 * @return this object, for chaining 1145 * @stable ICU 2.4 1146 */ 1147 UnicodeSet& removeAll(const UnicodeString& s); 1148 1149 /** 1150 * Makes a set from a multicharacter string. Thus "ch" => {"ch"} 1151 * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b> 1152 * @param s the source string 1153 * @return a newly created set containing the given string. 1154 * The caller owns the return object and is responsible for deleting it. 1155 * @stable ICU 2.4 1156 */ 1157 static UnicodeSet* U_EXPORT2 createFrom(const UnicodeString& s); 1158 1159 1160 /** 1161 * Makes a set from each of the characters in the string. Thus "ch" => {"c", "h"} 1162 * @param s the source string 1163 * @return a newly created set containing the given characters 1164 * The caller owns the return object and is responsible for deleting it. 1165 * @stable ICU 2.4 1166 */ 1167 static UnicodeSet* U_EXPORT2 createFromAll(const UnicodeString& s); 1168 1169 /** 1170 * Retain only the elements in this set that are contained in the 1171 * specified range. If <code>end > start</code> then an empty range is 1172 * retained, leaving the set empty. This is equivalent to 1173 * a boolean logic AND, or a set INTERSECTION. 1174 * A frozen set will not be modified. 1175 * 1176 * @param start first character, inclusive, of range to be retained 1177 * to this set. 1178 * @param end last character, inclusive, of range to be retained 1179 * to this set. 1180 * @stable ICU 2.0 1181 */ 1182 virtual UnicodeSet& retain(UChar32 start, UChar32 end); 1183 1184 1185 /** 1186 * Retain the specified character from this set if it is present. 1187 * A frozen set will not be modified. 1188 * @stable ICU 2.0 1189 */ 1190 UnicodeSet& retain(UChar32 c); 1191 1192 /** 1193 * Removes the specified range from this set if it is present. 1194 * The set will not contain the specified range once the call 1195 * returns. If <code>end > start</code> then an empty range is 1196 * removed, leaving the set unchanged. 1197 * A frozen set will not be modified. 1198 * 1199 * @param start first character, inclusive, of range to be removed 1200 * from this set. 1201 * @param end last character, inclusive, of range to be removed 1202 * from this set. 1203 * @stable ICU 2.0 1204 */ 1205 virtual UnicodeSet& remove(UChar32 start, UChar32 end); 1206 1207 /** 1208 * Removes the specified character from this set if it is present. 1209 * The set will not contain the specified range once the call 1210 * returns. 1211 * A frozen set will not be modified. 1212 * @stable ICU 2.0 1213 */ 1214 UnicodeSet& remove(UChar32 c); 1215 1216 /** 1217 * Removes the specified string from this set if it is present. 1218 * The set will not contain the specified character once the call 1219 * returns. 1220 * A frozen set will not be modified. 1221 * @param s the source string 1222 * @return this object, for chaining 1223 * @stable ICU 2.4 1224 */ 1225 UnicodeSet& remove(const UnicodeString& s); 1226 1227 /** 1228 * Inverts this set. This operation modifies this set so that 1229 * its value is its complement. This is equivalent to 1230 * <code>complement(MIN_VALUE, MAX_VALUE)</code>. 1231 * A frozen set will not be modified. 1232 * @stable ICU 2.0 1233 */ 1234 virtual UnicodeSet& complement(void); 1235 1236 /** 1237 * Complements the specified range in this set. Any character in 1238 * the range will be removed if it is in this set, or will be 1239 * added if it is not in this set. If <code>end > start</code> 1240 * then an empty range is complemented, leaving the set unchanged. 1241 * This is equivalent to a boolean logic XOR. 1242 * A frozen set will not be modified. 1243 * 1244 * @param start first character, inclusive, of range to be removed 1245 * from this set. 1246 * @param end last character, inclusive, of range to be removed 1247 * from this set. 1248 * @stable ICU 2.0 1249 */ 1250 virtual UnicodeSet& complement(UChar32 start, UChar32 end); 1251 1252 /** 1253 * Complements the specified character in this set. The character 1254 * will be removed if it is in this set, or will be added if it is 1255 * not in this set. 1256 * A frozen set will not be modified. 1257 * @stable ICU 2.0 1258 */ 1259 UnicodeSet& complement(UChar32 c); 1260 1261 /** 1262 * Complement the specified string in this set. 1263 * The set will not contain the specified string once the call 1264 * returns. 1265 * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b> 1266 * A frozen set will not be modified. 1267 * @param s the string to complement 1268 * @return this object, for chaining 1269 * @stable ICU 2.4 1270 */ 1271 UnicodeSet& complement(const UnicodeString& s); 1272 1273 /** 1274 * Adds all of the elements in the specified set to this set if 1275 * they're not already present. This operation effectively 1276 * modifies this set so that its value is the <i>union</i> of the two 1277 * sets. The behavior of this operation is unspecified if the specified 1278 * collection is modified while the operation is in progress. 1279 * A frozen set will not be modified. 1280 * 1281 * @param c set whose elements are to be added to this set. 1282 * @see #add(UChar32, UChar32) 1283 * @stable ICU 2.0 1284 */ 1285 virtual UnicodeSet& addAll(const UnicodeSet& c); 1286 1287 /** 1288 * Retains only the elements in this set that are contained in the 1289 * specified set. In other words, removes from this set all of 1290 * its elements that are not contained in the specified set. This 1291 * operation effectively modifies this set so that its value is 1292 * the <i>intersection</i> of the two sets. 1293 * A frozen set will not be modified. 1294 * 1295 * @param c set that defines which elements this set will retain. 1296 * @stable ICU 2.0 1297 */ 1298 virtual UnicodeSet& retainAll(const UnicodeSet& c); 1299 1300 /** 1301 * Removes from this set all of its elements that are contained in the 1302 * specified set. This operation effectively modifies this 1303 * set so that its value is the <i>asymmetric set difference</i> of 1304 * the two sets. 1305 * A frozen set will not be modified. 1306 * 1307 * @param c set that defines which elements will be removed from 1308 * this set. 1309 * @stable ICU 2.0 1310 */ 1311 virtual UnicodeSet& removeAll(const UnicodeSet& c); 1312 1313 /** 1314 * Complements in this set all elements contained in the specified 1315 * set. Any character in the other set will be removed if it is 1316 * in this set, or will be added if it is not in this set. 1317 * A frozen set will not be modified. 1318 * 1319 * @param c set that defines which elements will be xor'ed from 1320 * this set. 1321 * @stable ICU 2.4 1322 */ 1323 virtual UnicodeSet& complementAll(const UnicodeSet& c); 1324 1325 /** 1326 * Removes all of the elements from this set. This set will be 1327 * empty after this call returns. 1328 * A frozen set will not be modified. 1329 * @stable ICU 2.0 1330 */ 1331 virtual UnicodeSet& clear(void); 1332 1333 /** 1334 * Close this set over the given attribute. For the attribute 1335 * USET_CASE, the result is to modify this set so that: 1336 * 1337 * 1. For each character or string 'a' in this set, all strings or 1338 * characters 'b' such that foldCase(a) == foldCase(b) are added 1339 * to this set. 1340 * 1341 * 2. For each string 'e' in the resulting set, if e != 1342 * foldCase(e), 'e' will be removed. 1343 * 1344 * Example: [aq\\u00DF{Bc}{bC}{Fi}] => [aAqQ\\u00DF\\uFB01{ss}{bc}{fi}] 1345 * 1346 * (Here foldCase(x) refers to the operation u_strFoldCase, and a 1347 * == b denotes that the contents are the same, not pointer 1348 * comparison.) 1349 * 1350 * A frozen set will not be modified. 1351 * 1352 * @param attribute bitmask for attributes to close over. 1353 * Currently only the USET_CASE bit is supported. Any undefined bits 1354 * are ignored. 1355 * @return a reference to this set. 1356 * @stable ICU 4.2 1357 */ 1358 UnicodeSet& closeOver(int32_t attribute); 1359 1360 /** 1361 * Remove all strings from this set. 1362 * 1363 * @return a reference to this set. 1364 * @stable ICU 4.2 1365 */ 1366 virtual UnicodeSet &removeAllStrings(); 1367 1368 /** 1369 * Iteration method that returns the number of ranges contained in 1370 * this set. 1371 * @see #getRangeStart 1372 * @see #getRangeEnd 1373 * @stable ICU 2.4 1374 */ 1375 virtual int32_t getRangeCount(void) const; 1376 1377 /** 1378 * Iteration method that returns the first character in the 1379 * specified range of this set. 1380 * @see #getRangeCount 1381 * @see #getRangeEnd 1382 * @stable ICU 2.4 1383 */ 1384 virtual UChar32 getRangeStart(int32_t index) const; 1385 1386 /** 1387 * Iteration method that returns the last character in the 1388 * specified range of this set. 1389 * @see #getRangeStart 1390 * @see #getRangeEnd 1391 * @stable ICU 2.4 1392 */ 1393 virtual UChar32 getRangeEnd(int32_t index) const; 1394 1395 /** 1396 * Serializes this set into an array of 16-bit integers. Serialization 1397 * (currently) only records the characters in the set; multicharacter 1398 * strings are ignored. 1399 * 1400 * The array has following format (each line is one 16-bit 1401 * integer): 1402 * 1403 * length = (n+2*m) | (m!=0?0x8000:0) 1404 * bmpLength = n; present if m!=0 1405 * bmp[0] 1406 * bmp[1] 1407 * ... 1408 * bmp[n-1] 1409 * supp-high[0] 1410 * supp-low[0] 1411 * supp-high[1] 1412 * supp-low[1] 1413 * ... 1414 * supp-high[m-1] 1415 * supp-low[m-1] 1416 * 1417 * The array starts with a header. After the header are n bmp 1418 * code points, then m supplementary code points. Either n or m 1419 * or both may be zero. n+2*m is always <= 0x7FFF. 1420 * 1421 * If there are no supplementary characters (if m==0) then the 1422 * header is one 16-bit integer, 'length', with value n. 1423 * 1424 * If there are supplementary characters (if m!=0) then the header 1425 * is two 16-bit integers. The first, 'length', has value 1426 * (n+2*m)|0x8000. The second, 'bmpLength', has value n. 1427 * 1428 * After the header the code points are stored in ascending order. 1429 * Supplementary code points are stored as most significant 16 1430 * bits followed by least significant 16 bits. 1431 * 1432 * @param dest pointer to buffer of destCapacity 16-bit integers. 1433 * May be NULL only if destCapacity is zero. 1434 * @param destCapacity size of dest, or zero. Must not be negative. 1435 * @param ec error code. Will be set to U_INDEX_OUTOFBOUNDS_ERROR 1436 * if n+2*m > 0x7FFF. Will be set to U_BUFFER_OVERFLOW_ERROR if 1437 * n+2*m+(m!=0?2:1) > destCapacity. 1438 * @return the total length of the serialized format, including 1439 * the header, that is, n+2*m+(m!=0?2:1), or 0 on error other 1440 * than U_BUFFER_OVERFLOW_ERROR. 1441 * @stable ICU 2.4 1442 */ 1443 int32_t serialize(uint16_t *dest, int32_t destCapacity, UErrorCode& ec) const; 1444 1445 /** 1446 * Reallocate this objects internal structures to take up the least 1447 * possible space, without changing this object's value. 1448 * A frozen set will not be modified. 1449 * @stable ICU 2.4 1450 */ 1451 virtual UnicodeSet& compact(); 1452 1453 /** 1454 * Return the class ID for this class. This is useful only for 1455 * comparing to a return value from getDynamicClassID(). For example: 1456 * <pre> 1457 * . Base* polymorphic_pointer = createPolymorphicObject(); 1458 * . if (polymorphic_pointer->getDynamicClassID() == 1459 * . Derived::getStaticClassID()) ... 1460 * </pre> 1461 * @return The class ID for all objects of this class. 1462 * @stable ICU 2.0 1463 */ 1464 static UClassID U_EXPORT2 getStaticClassID(void); 1465 1466 /** 1467 * Implement UnicodeFunctor API. 1468 * 1469 * @return The class ID for this object. All objects of a given 1470 * class have the same class ID. Objects of other classes have 1471 * different class IDs. 1472 * @stable ICU 2.4 1473 */ 1474 virtual UClassID getDynamicClassID(void) const; 1475 1476 private: 1477 1478 // Private API for the USet API 1479 1480 friend class USetAccess; 1481 1482 int32_t getStringCount() const; 1483 1484 const UnicodeString* getString(int32_t index) const; 1485 1486 //---------------------------------------------------------------- 1487 // RuleBasedTransliterator support 1488 //---------------------------------------------------------------- 1489 1490 private: 1491 1492 /** 1493 * Returns <tt>true</tt> if this set contains any character whose low byte 1494 * is the given value. This is used by <tt>RuleBasedTransliterator</tt> for 1495 * indexing. 1496 */ 1497 virtual UBool matchesIndexValue(uint8_t v) const; 1498 1499 private: 1500 friend class RBBIRuleScanner; 1501 1502 //---------------------------------------------------------------- 1503 // Implementation: Clone as thawed (see ICU4J Freezable) 1504 //---------------------------------------------------------------- 1505 1506 UnicodeSet(const UnicodeSet& o, UBool /* asThawed */); 1507 1508 //---------------------------------------------------------------- 1509 // Implementation: Pattern parsing 1510 //---------------------------------------------------------------- 1511 1512 void applyPatternIgnoreSpace(const UnicodeString& pattern, 1513 ParsePosition& pos, 1514 const SymbolTable* symbols, 1515 UErrorCode& status); 1516 1517 void applyPattern(RuleCharacterIterator& chars, 1518 const SymbolTable* symbols, 1519 UnicodeString& rebuiltPat, 1520 uint32_t options, 1521 UnicodeSet& (UnicodeSet::*caseClosure)(int32_t attribute), 1522 UErrorCode& ec); 1523 1524 //---------------------------------------------------------------- 1525 // Implementation: Utility methods 1526 //---------------------------------------------------------------- 1527 1528 void ensureCapacity(int32_t newLen, UErrorCode& ec); 1529 1530 void ensureBufferCapacity(int32_t newLen, UErrorCode& ec); 1531 1532 void swapBuffers(void); 1533 1534 UBool allocateStrings(UErrorCode &status); 1535 1536 UnicodeString& _toPattern(UnicodeString& result, 1537 UBool escapeUnprintable) const; 1538 1539 UnicodeString& _generatePattern(UnicodeString& result, 1540 UBool escapeUnprintable) const; 1541 1542 static void _appendToPat(UnicodeString& buf, const UnicodeString& s, UBool escapeUnprintable); 1543 1544 static void _appendToPat(UnicodeString& buf, UChar32 c, UBool escapeUnprintable); 1545 1546 //---------------------------------------------------------------- 1547 // Implementation: Fundamental operators 1548 //---------------------------------------------------------------- 1549 1550 void exclusiveOr(const UChar32* other, int32_t otherLen, int8_t polarity); 1551 1552 void add(const UChar32* other, int32_t otherLen, int8_t polarity); 1553 1554 void retain(const UChar32* other, int32_t otherLen, int8_t polarity); 1555 1556 /** 1557 * Return true if the given position, in the given pattern, appears 1558 * to be the start of a property set pattern [:foo:], \\p{foo}, or 1559 * \\P{foo}, or \\N{name}. 1560 */ 1561 static UBool resemblesPropertyPattern(const UnicodeString& pattern, 1562 int32_t pos); 1563 1564 static UBool resemblesPropertyPattern(RuleCharacterIterator& chars, 1565 int32_t iterOpts); 1566 1567 /** 1568 * Parse the given property pattern at the given parse position 1569 * and set this UnicodeSet to the result. 1570 * 1571 * The original design document is out of date, but still useful. 1572 * Ignore the property and value names: 1573 * http://source.icu-project.org/repos/icu/icuhtml/trunk/design/unicodeset_properties.html 1574 * 1575 * Recognized syntax: 1576 * 1577 * [:foo:] [:^foo:] - white space not allowed within "[:" or ":]" 1578 * \\p{foo} \\P{foo} - white space not allowed within "\\p" or "\\P" 1579 * \\N{name} - white space not allowed within "\\N" 1580 * 1581 * Other than the above restrictions, Unicode Pattern_White_Space characters are ignored. 1582 * Case is ignored except in "\\p" and "\\P" and "\\N". In 'name' leading 1583 * and trailing space is deleted, and internal runs of whitespace 1584 * are collapsed to a single space. 1585 * 1586 * We support binary properties, enumerated properties, and the 1587 * following non-enumerated properties: 1588 * 1589 * Numeric_Value 1590 * Name 1591 * Unicode_1_Name 1592 * 1593 * @param pattern the pattern string 1594 * @param ppos on entry, the position at which to begin parsing. 1595 * This should be one of the locations marked '^': 1596 * 1597 * [:blah:] \\p{blah} \\P{blah} \\N{name} 1598 * ^ % ^ % ^ % ^ % 1599 * 1600 * On return, the position after the last character parsed, that is, 1601 * the locations marked '%'. If the parse fails, ppos is returned 1602 * unchanged. 1603 * @param ec status 1604 * @return a reference to this. 1605 */ 1606 UnicodeSet& applyPropertyPattern(const UnicodeString& pattern, 1607 ParsePosition& ppos, 1608 UErrorCode &ec); 1609 1610 void applyPropertyPattern(RuleCharacterIterator& chars, 1611 UnicodeString& rebuiltPat, 1612 UErrorCode& ec); 1613 1614 friend void U_CALLCONV UnicodeSet_initInclusion(int32_t src, UErrorCode &status); 1615 static const UnicodeSet* getInclusions(int32_t src, UErrorCode &status); 1616 1617 /** 1618 * A filter that returns TRUE if the given code point should be 1619 * included in the UnicodeSet being constructed. 1620 */ 1621 typedef UBool (*Filter)(UChar32 codePoint, void* context); 1622 1623 /** 1624 * Given a filter, set this UnicodeSet to the code points 1625 * contained by that filter. The filter MUST be 1626 * property-conformant. That is, if it returns value v for one 1627 * code point, then it must return v for all affiliated code 1628 * points, as defined by the inclusions list. See 1629 * getInclusions(). 1630 * src is a UPropertySource value. 1631 */ 1632 void applyFilter(Filter filter, 1633 void* context, 1634 int32_t src, 1635 UErrorCode &status); 1636 1637 /** 1638 * Set the new pattern to cache. 1639 */ 1640 void setPattern(const UnicodeString& newPat); 1641 /** 1642 * Release existing cached pattern. 1643 */ 1644 void releasePattern(); 1645 1646 friend class UnicodeSetIterator; 1647 }; 1648 1649 1650 1651 inline UBool UnicodeSet::operator!=(const UnicodeSet& o) const { 1652 return !operator==(o); 1653 } 1654 1655 inline UBool UnicodeSet::isFrozen() const { 1656 return (UBool)(bmpSet!=NULL || stringSpan!=NULL); 1657 } 1658 1659 inline UBool UnicodeSet::containsSome(UChar32 start, UChar32 end) const { 1660 return !containsNone(start, end); 1661 } 1662 1663 inline UBool UnicodeSet::containsSome(const UnicodeSet& s) const { 1664 return !containsNone(s); 1665 } 1666 1667 inline UBool UnicodeSet::containsSome(const UnicodeString& s) const { 1668 return !containsNone(s); 1669 } 1670 1671 inline UBool UnicodeSet::isBogus() const { 1672 return (UBool)(fFlags & kIsBogus); 1673 } 1674 1675 inline UnicodeSet *UnicodeSet::fromUSet(USet *uset) { 1676 return reinterpret_cast<UnicodeSet *>(uset); 1677 } 1678 1679 inline const UnicodeSet *UnicodeSet::fromUSet(const USet *uset) { 1680 return reinterpret_cast<const UnicodeSet *>(uset); 1681 } 1682 1683 inline USet *UnicodeSet::toUSet() { 1684 return reinterpret_cast<USet *>(this); 1685 } 1686 1687 inline const USet *UnicodeSet::toUSet() const { 1688 return reinterpret_cast<const USet *>(this); 1689 } 1690 1691 inline int32_t UnicodeSet::span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const { 1692 int32_t sLength=s.length(); 1693 if(start<0) { 1694 start=0; 1695 } else if(start>sLength) { 1696 start=sLength; 1697 } 1698 return start+span(s.getBuffer()+start, sLength-start, spanCondition); 1699 } 1700 1701 inline int32_t UnicodeSet::spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const { 1702 int32_t sLength=s.length(); 1703 if(limit<0) { 1704 limit=0; 1705 } else if(limit>sLength) { 1706 limit=sLength; 1707 } 1708 return spanBack(s.getBuffer(), limit, spanCondition); 1709 } 1710 1711 U_NAMESPACE_END 1712 1713 #endif 1714