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