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