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