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 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, ignoring Unicode Pattern_White_Space characters.
579     * See the class 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 Unicode Pattern_White_Space characters.
594     * See the class 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     * @param s
978     * @return If part of s matches up to the limit, return |limit -
979     * start|.  If all of s matches before reaching the limit, return
980     * s.length().  If there is a mismatch between s and text, return
981     * 0
982     */
983    static int32_t matchRest(const Replaceable& text,
984                             int32_t start, int32_t limit,
985                             const UnicodeString& s);
986
987    /**
988     * Returns the smallest value i such that c < list[i].  Caller
989     * must ensure that c is a legal value or this method will enter
990     * an infinite loop.  This method performs a binary search.
991     * @param c a character in the range MIN_VALUE..MAX_VALUE
992     * inclusive
993     * @return the smallest integer i in the range 0..len-1,
994     * inclusive, such that c < list[i]
995     */
996    int32_t findCodePoint(UChar32 c) const;
997
998public:
999
1000    /**
1001     * Implementation of UnicodeMatcher API.  Union the set of all
1002     * characters that may be matched by this object into the given
1003     * set.
1004     * @param toUnionTo the set into which to union the source characters
1005     * @stable ICU 2.4
1006     */
1007    virtual void addMatchSetTo(UnicodeSet& toUnionTo) const;
1008
1009    /**
1010     * Returns the index of the given character within this set, where
1011     * the set is ordered by ascending code point.  If the character
1012     * is not in this set, return -1.  The inverse of this method is
1013     * <code>charAt()</code>.
1014     * @return an index from 0..size()-1, or -1
1015     * @stable ICU 2.4
1016     */
1017    int32_t indexOf(UChar32 c) const;
1018
1019    /**
1020     * Returns the character at the given index within this set, where
1021     * the set is ordered by ascending code point.  If the index is
1022     * out of range, return (UChar32)-1.  The inverse of this method is
1023     * <code>indexOf()</code>.
1024     * @param index an index from 0..size()-1
1025     * @return the character at the given index, or (UChar32)-1.
1026     * @stable ICU 2.4
1027     */
1028    UChar32 charAt(int32_t index) const;
1029
1030    /**
1031     * Adds the specified range to this set if it is not already
1032     * present.  If this set already contains the specified range,
1033     * the call leaves this set unchanged.  If <code>end > start</code>
1034     * then an empty range is added, leaving the set unchanged.
1035     * This is equivalent to a boolean logic OR, or a set UNION.
1036     * A frozen set will not be modified.
1037     *
1038     * @param start first character, inclusive, of range to be added
1039     * to this set.
1040     * @param end last character, inclusive, of range to be added
1041     * to this set.
1042     * @stable ICU 2.0
1043     */
1044    virtual UnicodeSet& add(UChar32 start, UChar32 end);
1045
1046    /**
1047     * Adds the specified character to this set if it is not already
1048     * present.  If this set already contains the specified character,
1049     * the call leaves this set unchanged.
1050     * A frozen set will not be modified.
1051     * @stable ICU 2.0
1052     */
1053    UnicodeSet& add(UChar32 c);
1054
1055    /**
1056     * Adds the specified multicharacter to this set if it is not already
1057     * present.  If this set already contains the multicharacter,
1058     * the call leaves this set unchanged.
1059     * Thus "ch" => {"ch"}
1060     * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
1061     * A frozen set will not be modified.
1062     * @param s the source string
1063     * @return this object, for chaining
1064     * @stable ICU 2.4
1065     */
1066    UnicodeSet& add(const UnicodeString& s);
1067
1068 private:
1069    /**
1070     * @return a code point IF the string consists of a single one.
1071     * otherwise returns -1.
1072     * @param s string to test
1073     */
1074    static int32_t getSingleCP(const UnicodeString& s);
1075
1076    void _add(const UnicodeString& s);
1077
1078 public:
1079    /**
1080     * Adds each of the characters in this string to the set. Thus "ch" => {"c", "h"}
1081     * If this set already any particular character, it has no effect on that character.
1082     * A frozen set will not be modified.
1083     * @param s the source string
1084     * @return this object, for chaining
1085     * @stable ICU 2.4
1086     */
1087    UnicodeSet& addAll(const UnicodeString& s);
1088
1089    /**
1090     * Retains EACH of the characters in this string. Note: "ch" == {"c", "h"}
1091     * If this set already any particular character, it has no effect on that character.
1092     * A frozen set will not be modified.
1093     * @param s the source string
1094     * @return this object, for chaining
1095     * @stable ICU 2.4
1096     */
1097    UnicodeSet& retainAll(const UnicodeString& s);
1098
1099    /**
1100     * Complement EACH of the characters in this string. Note: "ch" == {"c", "h"}
1101     * If this set already any particular character, it has no effect on that character.
1102     * A frozen set will not be modified.
1103     * @param s the source string
1104     * @return this object, for chaining
1105     * @stable ICU 2.4
1106     */
1107    UnicodeSet& complementAll(const UnicodeString& s);
1108
1109    /**
1110     * Remove EACH of the characters in this string. Note: "ch" == {"c", "h"}
1111     * If this set already any particular character, it has no effect on that character.
1112     * A frozen set will not be modified.
1113     * @param s the source string
1114     * @return this object, for chaining
1115     * @stable ICU 2.4
1116     */
1117    UnicodeSet& removeAll(const UnicodeString& s);
1118
1119    /**
1120     * Makes a set from a multicharacter string. Thus "ch" => {"ch"}
1121     * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
1122     * @param s the source string
1123     * @return a newly created set containing the given string.
1124     * The caller owns the return object and is responsible for deleting it.
1125     * @stable ICU 2.4
1126     */
1127    static UnicodeSet* U_EXPORT2 createFrom(const UnicodeString& s);
1128
1129
1130    /**
1131     * Makes a set from each of the characters in the string. Thus "ch" => {"c", "h"}
1132     * @param s the source string
1133     * @return a newly created set containing the given characters
1134     * The caller owns the return object and is responsible for deleting it.
1135     * @stable ICU 2.4
1136     */
1137    static UnicodeSet* U_EXPORT2 createFromAll(const UnicodeString& s);
1138
1139    /**
1140     * Retain only the elements in this set that are contained in the
1141     * specified range.  If <code>end > start</code> then an empty range is
1142     * retained, leaving the set empty.  This is equivalent to
1143     * a boolean logic AND, or a set INTERSECTION.
1144     * A frozen set will not be modified.
1145     *
1146     * @param start first character, inclusive, of range to be retained
1147     * to this set.
1148     * @param end last character, inclusive, of range to be retained
1149     * to this set.
1150     * @stable ICU 2.0
1151     */
1152    virtual UnicodeSet& retain(UChar32 start, UChar32 end);
1153
1154
1155    /**
1156     * Retain the specified character from this set if it is present.
1157     * A frozen set will not be modified.
1158     * @stable ICU 2.0
1159     */
1160    UnicodeSet& retain(UChar32 c);
1161
1162    /**
1163     * Removes the specified range from this set if it is present.
1164     * The set will not contain the specified range once the call
1165     * returns.  If <code>end > start</code> then an empty range is
1166     * removed, leaving the set unchanged.
1167     * A frozen set will not be modified.
1168     *
1169     * @param start first character, inclusive, of range to be removed
1170     * from this set.
1171     * @param end last character, inclusive, of range to be removed
1172     * from this set.
1173     * @stable ICU 2.0
1174     */
1175    virtual UnicodeSet& remove(UChar32 start, UChar32 end);
1176
1177    /**
1178     * Removes the specified character from this set if it is present.
1179     * The set will not contain the specified range once the call
1180     * returns.
1181     * A frozen set will not be modified.
1182     * @stable ICU 2.0
1183     */
1184    UnicodeSet& remove(UChar32 c);
1185
1186    /**
1187     * Removes the specified string from this set if it is present.
1188     * The set will not contain the specified character once the call
1189     * returns.
1190     * A frozen set will not be modified.
1191     * @param s the source string
1192     * @return this object, for chaining
1193     * @stable ICU 2.4
1194     */
1195    UnicodeSet& remove(const UnicodeString& s);
1196
1197    /**
1198     * Inverts this set.  This operation modifies this set so that
1199     * its value is its complement.  This is equivalent to
1200     * <code>complement(MIN_VALUE, MAX_VALUE)</code>.
1201     * A frozen set will not be modified.
1202     * @stable ICU 2.0
1203     */
1204    virtual UnicodeSet& complement(void);
1205
1206    /**
1207     * Complements the specified range in this set.  Any character in
1208     * the range will be removed if it is in this set, or will be
1209     * added if it is not in this set.  If <code>end > start</code>
1210     * then an empty range is complemented, leaving the set unchanged.
1211     * This is equivalent to a boolean logic XOR.
1212     * A frozen set will not be modified.
1213     *
1214     * @param start first character, inclusive, of range to be removed
1215     * from this set.
1216     * @param end last character, inclusive, of range to be removed
1217     * from this set.
1218     * @stable ICU 2.0
1219     */
1220    virtual UnicodeSet& complement(UChar32 start, UChar32 end);
1221
1222    /**
1223     * Complements the specified character in this set.  The character
1224     * will be removed if it is in this set, or will be added if it is
1225     * not in this set.
1226     * A frozen set will not be modified.
1227     * @stable ICU 2.0
1228     */
1229    UnicodeSet& complement(UChar32 c);
1230
1231    /**
1232     * Complement the specified string in this set.
1233     * The set will not contain the specified string once the call
1234     * returns.
1235     * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
1236     * A frozen set will not be modified.
1237     * @param s the string to complement
1238     * @return this object, for chaining
1239     * @stable ICU 2.4
1240     */
1241    UnicodeSet& complement(const UnicodeString& s);
1242
1243    /**
1244     * Adds all of the elements in the specified set to this set if
1245     * they're not already present.  This operation effectively
1246     * modifies this set so that its value is the <i>union</i> of the two
1247     * sets.  The behavior of this operation is unspecified if the specified
1248     * collection is modified while the operation is in progress.
1249     * A frozen set will not be modified.
1250     *
1251     * @param c set whose elements are to be added to this set.
1252     * @see #add(UChar32, UChar32)
1253     * @stable ICU 2.0
1254     */
1255    virtual UnicodeSet& addAll(const UnicodeSet& c);
1256
1257    /**
1258     * Retains only the elements in this set that are contained in the
1259     * specified set.  In other words, removes from this set all of
1260     * its elements that are not contained in the specified set.  This
1261     * operation effectively modifies this set so that its value is
1262     * the <i>intersection</i> of the two sets.
1263     * A frozen set will not be modified.
1264     *
1265     * @param c set that defines which elements this set will retain.
1266     * @stable ICU 2.0
1267     */
1268    virtual UnicodeSet& retainAll(const UnicodeSet& c);
1269
1270    /**
1271     * Removes from this set all of its elements that are contained in the
1272     * specified set.  This operation effectively modifies this
1273     * set so that its value is the <i>asymmetric set difference</i> of
1274     * the two sets.
1275     * A frozen set will not be modified.
1276     *
1277     * @param c set that defines which elements will be removed from
1278     *          this set.
1279     * @stable ICU 2.0
1280     */
1281    virtual UnicodeSet& removeAll(const UnicodeSet& c);
1282
1283    /**
1284     * Complements in this set all elements contained in the specified
1285     * set.  Any character in the other set will be removed if it is
1286     * in this set, or will be added if it is not in this set.
1287     * A frozen set will not be modified.
1288     *
1289     * @param c set that defines which elements will be xor'ed from
1290     *          this set.
1291     * @stable ICU 2.4
1292     */
1293    virtual UnicodeSet& complementAll(const UnicodeSet& c);
1294
1295    /**
1296     * Removes all of the elements from this set.  This set will be
1297     * empty after this call returns.
1298     * A frozen set will not be modified.
1299     * @stable ICU 2.0
1300     */
1301    virtual UnicodeSet& clear(void);
1302
1303    /**
1304     * Close this set over the given attribute.  For the attribute
1305     * USET_CASE, the result is to modify this set so that:
1306     *
1307     * 1. For each character or string 'a' in this set, all strings or
1308     * characters 'b' such that foldCase(a) == foldCase(b) are added
1309     * to this set.
1310     *
1311     * 2. For each string 'e' in the resulting set, if e !=
1312     * foldCase(e), 'e' will be removed.
1313     *
1314     * Example: [aq\\u00DF{Bc}{bC}{Fi}] => [aAqQ\\u00DF\\uFB01{ss}{bc}{fi}]
1315     *
1316     * (Here foldCase(x) refers to the operation u_strFoldCase, and a
1317     * == b denotes that the contents are the same, not pointer
1318     * comparison.)
1319     *
1320     * A frozen set will not be modified.
1321     *
1322     * @param attribute bitmask for attributes to close over.
1323     * Currently only the USET_CASE bit is supported.  Any undefined bits
1324     * are ignored.
1325     * @return a reference to this set.
1326     * @stable ICU 4.2
1327     */
1328    UnicodeSet& closeOver(int32_t attribute);
1329
1330    /**
1331     * Remove all strings from this set.
1332     *
1333     * @return a reference to this set.
1334     * @stable ICU 4.2
1335     */
1336    virtual UnicodeSet &removeAllStrings();
1337
1338    /**
1339     * Iteration method that returns the number of ranges contained in
1340     * this set.
1341     * @see #getRangeStart
1342     * @see #getRangeEnd
1343     * @stable ICU 2.4
1344     */
1345    virtual int32_t getRangeCount(void) const;
1346
1347    /**
1348     * Iteration method that returns the first character in the
1349     * specified range of this set.
1350     * @see #getRangeCount
1351     * @see #getRangeEnd
1352     * @stable ICU 2.4
1353     */
1354    virtual UChar32 getRangeStart(int32_t index) const;
1355
1356    /**
1357     * Iteration method that returns the last character in the
1358     * specified range of this set.
1359     * @see #getRangeStart
1360     * @see #getRangeEnd
1361     * @stable ICU 2.4
1362     */
1363    virtual UChar32 getRangeEnd(int32_t index) const;
1364
1365    /**
1366     * Serializes this set into an array of 16-bit integers.  Serialization
1367     * (currently) only records the characters in the set; multicharacter
1368     * strings are ignored.
1369     *
1370     * The array has following format (each line is one 16-bit
1371     * integer):
1372     *
1373     *  length     = (n+2*m) | (m!=0?0x8000:0)
1374     *  bmpLength  = n; present if m!=0
1375     *  bmp[0]
1376     *  bmp[1]
1377     *  ...
1378     *  bmp[n-1]
1379     *  supp-high[0]
1380     *  supp-low[0]
1381     *  supp-high[1]
1382     *  supp-low[1]
1383     *  ...
1384     *  supp-high[m-1]
1385     *  supp-low[m-1]
1386     *
1387     * The array starts with a header.  After the header are n bmp
1388     * code points, then m supplementary code points.  Either n or m
1389     * or both may be zero.  n+2*m is always <= 0x7FFF.
1390     *
1391     * If there are no supplementary characters (if m==0) then the
1392     * header is one 16-bit integer, 'length', with value n.
1393     *
1394     * If there are supplementary characters (if m!=0) then the header
1395     * is two 16-bit integers.  The first, 'length', has value
1396     * (n+2*m)|0x8000.  The second, 'bmpLength', has value n.
1397     *
1398     * After the header the code points are stored in ascending order.
1399     * Supplementary code points are stored as most significant 16
1400     * bits followed by least significant 16 bits.
1401     *
1402     * @param dest pointer to buffer of destCapacity 16-bit integers.
1403     * May be NULL only if destCapacity is zero.
1404     * @param destCapacity size of dest, or zero.  Must not be negative.
1405     * @param ec error code.  Will be set to U_INDEX_OUTOFBOUNDS_ERROR
1406     * if n+2*m > 0x7FFF.  Will be set to U_BUFFER_OVERFLOW_ERROR if
1407     * n+2*m+(m!=0?2:1) > destCapacity.
1408     * @return the total length of the serialized format, including
1409     * the header, that is, n+2*m+(m!=0?2:1), or 0 on error other
1410     * than U_BUFFER_OVERFLOW_ERROR.
1411     * @stable ICU 2.4
1412     */
1413    int32_t serialize(uint16_t *dest, int32_t destCapacity, UErrorCode& ec) const;
1414
1415    /**
1416     * Reallocate this objects internal structures to take up the least
1417     * possible space, without changing this object's value.
1418     * A frozen set will not be modified.
1419     * @stable ICU 2.4
1420     */
1421    virtual UnicodeSet& compact();
1422
1423    /**
1424     * Return the class ID for this class.  This is useful only for
1425     * comparing to a return value from getDynamicClassID().  For example:
1426     * <pre>
1427     * .      Base* polymorphic_pointer = createPolymorphicObject();
1428     * .      if (polymorphic_pointer->getDynamicClassID() ==
1429     * .          Derived::getStaticClassID()) ...
1430     * </pre>
1431     * @return          The class ID for all objects of this class.
1432     * @stable ICU 2.0
1433     */
1434    static UClassID U_EXPORT2 getStaticClassID(void);
1435
1436    /**
1437     * Implement UnicodeFunctor API.
1438     *
1439     * @return The class ID for this object. All objects of a given
1440     * class have the same class ID.  Objects of other classes have
1441     * different class IDs.
1442     * @stable ICU 2.4
1443     */
1444    virtual UClassID getDynamicClassID(void) const;
1445
1446private:
1447
1448    // Private API for the USet API
1449
1450    friend class USetAccess;
1451
1452    int32_t getStringCount() const;
1453
1454    const UnicodeString* getString(int32_t index) const;
1455
1456    //----------------------------------------------------------------
1457    // RuleBasedTransliterator support
1458    //----------------------------------------------------------------
1459
1460private:
1461
1462    /**
1463     * Returns <tt>true</tt> if this set contains any character whose low byte
1464     * is the given value.  This is used by <tt>RuleBasedTransliterator</tt> for
1465     * indexing.
1466     */
1467    virtual UBool matchesIndexValue(uint8_t v) const;
1468
1469private:
1470
1471    //----------------------------------------------------------------
1472    // Implementation: Clone as thawed (see ICU4J Freezable)
1473    //----------------------------------------------------------------
1474
1475    UnicodeSet(const UnicodeSet& o, UBool /* asThawed */);
1476
1477    //----------------------------------------------------------------
1478    // Implementation: Pattern parsing
1479    //----------------------------------------------------------------
1480
1481    void applyPattern(RuleCharacterIterator& chars,
1482                      const SymbolTable* symbols,
1483                      UnicodeString& rebuiltPat,
1484                      uint32_t options,
1485                      UErrorCode& ec);
1486
1487    //----------------------------------------------------------------
1488    // Implementation: Utility methods
1489    //----------------------------------------------------------------
1490
1491    void ensureCapacity(int32_t newLen, UErrorCode& ec);
1492
1493    void ensureBufferCapacity(int32_t newLen, UErrorCode& ec);
1494
1495    void swapBuffers(void);
1496
1497    UBool allocateStrings(UErrorCode &status);
1498
1499    UnicodeString& _toPattern(UnicodeString& result,
1500                              UBool escapeUnprintable) const;
1501
1502    UnicodeString& _generatePattern(UnicodeString& result,
1503                                    UBool escapeUnprintable) const;
1504
1505    static void _appendToPat(UnicodeString& buf, const UnicodeString& s, UBool escapeUnprintable);
1506
1507    static void _appendToPat(UnicodeString& buf, UChar32 c, UBool escapeUnprintable);
1508
1509    //----------------------------------------------------------------
1510    // Implementation: Fundamental operators
1511    //----------------------------------------------------------------
1512
1513    void exclusiveOr(const UChar32* other, int32_t otherLen, int8_t polarity);
1514
1515    void add(const UChar32* other, int32_t otherLen, int8_t polarity);
1516
1517    void retain(const UChar32* other, int32_t otherLen, int8_t polarity);
1518
1519    /**
1520     * Return true if the given position, in the given pattern, appears
1521     * to be the start of a property set pattern [:foo:], \\p{foo}, or
1522     * \\P{foo}, or \\N{name}.
1523     */
1524    static UBool resemblesPropertyPattern(const UnicodeString& pattern,
1525                                          int32_t pos);
1526
1527    static UBool resemblesPropertyPattern(RuleCharacterIterator& chars,
1528                                          int32_t iterOpts);
1529
1530    /**
1531     * Parse the given property pattern at the given parse position
1532     * and set this UnicodeSet to the result.
1533     *
1534     * The original design document is out of date, but still useful.
1535     * Ignore the property and value names:
1536     * http://source.icu-project.org/repos/icu/icuhtml/trunk/design/unicodeset_properties.html
1537     *
1538     * Recognized syntax:
1539     *
1540     * [:foo:] [:^foo:] - white space not allowed within "[:" or ":]"
1541     * \\p{foo} \\P{foo}  - white space not allowed within "\\p" or "\\P"
1542     * \\N{name}         - white space not allowed within "\\N"
1543     *
1544     * Other than the above restrictions, Unicode Pattern_White_Space characters are ignored.
1545     * Case is ignored except in "\\p" and "\\P" and "\\N".  In 'name' leading
1546     * and trailing space is deleted, and internal runs of whitespace
1547     * are collapsed to a single space.
1548     *
1549     * We support binary properties, enumerated properties, and the
1550     * following non-enumerated properties:
1551     *
1552     *  Numeric_Value
1553     *  Name
1554     *  Unicode_1_Name
1555     *
1556     * @param pattern the pattern string
1557     * @param ppos on entry, the position at which to begin parsing.
1558     * This should be one of the locations marked '^':
1559     *
1560     *   [:blah:]     \\p{blah}     \\P{blah}     \\N{name}
1561     *   ^       %    ^       %    ^       %    ^       %
1562     *
1563     * On return, the position after the last character parsed, that is,
1564     * the locations marked '%'.  If the parse fails, ppos is returned
1565     * unchanged.
1566     * @param ec status
1567     * @return a reference to this.
1568     */
1569    UnicodeSet& applyPropertyPattern(const UnicodeString& pattern,
1570                                     ParsePosition& ppos,
1571                                     UErrorCode &ec);
1572
1573    void applyPropertyPattern(RuleCharacterIterator& chars,
1574                              UnicodeString& rebuiltPat,
1575                              UErrorCode& ec);
1576
1577    static const UnicodeSet* getInclusions(int32_t src, UErrorCode &status);
1578
1579    /**
1580     * A filter that returns TRUE if the given code point should be
1581     * included in the UnicodeSet being constructed.
1582     */
1583    typedef UBool (*Filter)(UChar32 codePoint, void* context);
1584
1585    /**
1586     * Given a filter, set this UnicodeSet to the code points
1587     * contained by that filter.  The filter MUST be
1588     * property-conformant.  That is, if it returns value v for one
1589     * code point, then it must return v for all affiliated code
1590     * points, as defined by the inclusions list.  See
1591     * getInclusions().
1592     * src is a UPropertySource value.
1593     */
1594    void applyFilter(Filter filter,
1595                     void* context,
1596                     int32_t src,
1597                     UErrorCode &status);
1598
1599    /**
1600     * Set the new pattern to cache.
1601     */
1602    void setPattern(const UnicodeString& newPat);
1603    /**
1604     * Release existing cached pattern.
1605     */
1606    void releasePattern();
1607
1608    friend class UnicodeSetIterator;
1609};
1610
1611
1612
1613inline UBool UnicodeSet::operator!=(const UnicodeSet& o) const {
1614    return !operator==(o);
1615}
1616
1617inline UBool UnicodeSet::isFrozen() const {
1618    return (UBool)(bmpSet!=NULL || stringSpan!=NULL);
1619}
1620
1621inline UBool UnicodeSet::containsSome(UChar32 start, UChar32 end) const {
1622    return !containsNone(start, end);
1623}
1624
1625inline UBool UnicodeSet::containsSome(const UnicodeSet& s) const {
1626    return !containsNone(s);
1627}
1628
1629inline UBool UnicodeSet::containsSome(const UnicodeString& s) const {
1630    return !containsNone(s);
1631}
1632
1633inline UBool UnicodeSet::isBogus() const {
1634    return (UBool)(fFlags & kIsBogus);
1635}
1636
1637inline UnicodeSet *UnicodeSet::fromUSet(USet *uset) {
1638    return reinterpret_cast<UnicodeSet *>(uset);
1639}
1640
1641inline const UnicodeSet *UnicodeSet::fromUSet(const USet *uset) {
1642    return reinterpret_cast<const UnicodeSet *>(uset);
1643}
1644
1645inline USet *UnicodeSet::toUSet() {
1646    return reinterpret_cast<USet *>(this);
1647}
1648
1649inline const USet *UnicodeSet::toUSet() const {
1650    return reinterpret_cast<const USet *>(this);
1651}
1652
1653inline int32_t UnicodeSet::span(const UnicodeString &s, int32_t start, USetSpanCondition spanCondition) const {
1654    int32_t sLength=s.length();
1655    if(start<0) {
1656        start=0;
1657    } else if(start>sLength) {
1658        start=sLength;
1659    }
1660    return start+span(s.getBuffer()+start, sLength-start, spanCondition);
1661}
1662
1663inline int32_t UnicodeSet::spanBack(const UnicodeString &s, int32_t limit, USetSpanCondition spanCondition) const {
1664    int32_t sLength=s.length();
1665    if(limit<0) {
1666        limit=0;
1667    } else if(limit>sLength) {
1668        limit=sLength;
1669    }
1670    return spanBack(s.getBuffer(), limit, spanCondition);
1671}
1672
1673U_NAMESPACE_END
1674
1675#endif
1676