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