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