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