Coverage Report

Created: 2025-06-24 06:43

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