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