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