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 ***************************************************************************
14 #include "unicode/unifilt.h"
15 #include "unicode/unistr.h"
16 #include "unicode/uset.h"
20 * \brief C++ API: Unicode Set
28 class UnicodeSetStringSpan
;
30 class RuleCharacterIterator
;
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.
38 * <p>The UnicodeSet class is not designed to be subclassed.
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>
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
68 * <p><b>Pattern syntax</b></p>
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:
76 * \htmlonly<blockquote>\endhtmlonly
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>
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
94 * <td nowrap valign="top" align="left"><code>[\\u4E01]</code></td>
95 * <td valign="top">The character U+4E01</td>
98 * <td nowrap valign="top" align="left"><code>[a{ab}{ac}]</code></td>
99 * <td valign="top">The character 'a' and the multicharacter strings "ab" and
100 * "ac"</td>
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>
107 * \htmlonly</blockquote>\endhtmlonly
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.
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
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.
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.
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 '-'.
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.
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
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
172 * <p><b>Warning</b>: you cannot add an empty string ("") to a UnicodeSet.</p>
174 * <p><b>Formal syntax</b></p>
176 * \htmlonly<blockquote>\endhtmlonly
179 * <td nowrap valign="top" align="right"><code>pattern := </code></td>
180 * <td valign="top"><code>('[' '^'? item* ']') |
181 * property</code></td>
184 * <td nowrap valign="top" align="right"><code>item := </code></td>
185 * <td valign="top"><code>char | (char '-' char) | pattern-expr<br>
189 * <td nowrap valign="top" align="right"><code>pattern-expr := </code></td>
190 * <td valign="top"><code>pattern | pattern-expr pattern |
191 * pattern-expr op pattern<br>
195 * <td nowrap valign="top" align="right"><code>op := </code></td>
196 * <td valign="top"><code>'&' | '-'<br>
200 * <td nowrap valign="top" align="right"><code>special := </code></td>
201 * <td valign="top"><code>'[' | ']' | '-'<br>
205 * <td nowrap valign="top" align="right"><code>char := </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>
212 * <td nowrap valign="top" align="right"><code>hex := </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>
218 * <td nowrap valign="top" align="right"><code>property := </code></td>
219 * <td valign="top"><em>a Unicode property set pattern</em></td>
225 * <td>Legend: <table>
227 * <td nowrap valign="top"><code>a := b</code></td>
228 * <td width="20" valign="top"> </td>
229 * <td valign="top"><code>a</code> may be replaced by <code>b</code> </td>
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>
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>
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>
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>
258 * \htmlonly</blockquote>\endhtmlonly
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.
272 class U_COMMON_API UnicodeSet
: public UnicodeFilter
{
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
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.
292 UVector
* strings
; // maintained in sorted order
293 UnicodeSetStringSpan
*stringSpan
;
297 kIsBogus
= 1 // This set is bogus (i.e. not valid)
299 uint8_t fFlags
; // Bit flag (see constants above)
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.
306 * @return TRUE if the set is valid, FALSE otherwise
310 inline UBool
isBogus(void) const;
313 * Make this UnicodeSet object invalid.
314 * The string will test TRUE with isBogus().
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.
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.
334 * Minimum value that can be stored in a UnicodeSet.
340 * Maximum value that can be stored in a UnicodeSet.
346 //----------------------------------------------------------------
348 //----------------------------------------------------------------
353 * Constructs an empty set.
359 * Constructs a set containing the given range. If <code>end >
360 * start</code> then an empty set is created.
362 * @param start first character, inclusive, of range
363 * @param end last character, inclusive, of range
366 UnicodeSet(UChar32 start
, UChar32 end
);
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.
376 UnicodeSet(const UnicodeString
& pattern
,
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.
391 UnicodeSet(const UnicodeString
& pattern
,
393 const SymbolTable
* symbols
,
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
409 UnicodeSet(const UnicodeString
& pattern
, ParsePosition
& pos
,
411 const SymbolTable
* symbols
,
415 * Constructs a set that is identical to the given UnicodeSet.
418 UnicodeSet(const UnicodeSet
& o
);
424 virtual ~UnicodeSet();
427 * Assigns this object to be a copy of another.
428 * A frozen set will not be modified.
431 UnicodeSet
& operator=(const UnicodeSet
& o
);
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).
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.
444 virtual UBool
operator==(const UnicodeSet
& o
) const;
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.
451 UBool
operator!=(const UnicodeSet
& o
) const;
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.
462 virtual UnicodeFunctor
* clone() const;
465 * Returns the hash code value for this set.
467 * @return the hash code value for this set.
468 * @see Object#hashCode()
471 virtual int32_t hashCode(void) const;
473 //----------------------------------------------------------------
475 //----------------------------------------------------------------
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
485 inline UBool
isFrozen() const;
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.)
500 UnicodeFunctor
*freeze();
503 * Clone the set and make the clone mutable.
504 * See the ICU4J Freezable interface for details.
505 * @return the mutable clone
510 UnicodeFunctor
*cloneAsThawed() const;
512 //----------------------------------------------------------------
514 //----------------------------------------------------------------
517 * Make this object represent the range <code>start - end</code>.
518 * If <code>end > start</code> then this object is set to an
520 * A frozen set will not be modified.
522 * @param start first character in the set, inclusive
523 * @param end last character in the set, inclusive
526 UnicodeSet
& set(UChar32 start
, UChar32 end
);
529 * Return true if the given position, in the given pattern, appears
530 * to be the start of a UnicodeSet pattern.
533 static UBool
resemblesPattern(const UnicodeString
& pattern
,
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
548 UnicodeSet
& applyPattern(const UnicodeString
& pattern
,
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
567 UnicodeSet
& applyPattern(const UnicodeString
& pattern
,
569 const SymbolTable
* symbols
,
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.
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
603 UnicodeSet
& applyPattern(const UnicodeString
& pattern
,
606 const SymbolTable
* symbols
,
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.
622 virtual UnicodeString
& toPattern(UnicodeString
& result
,
623 UBool escapeUnprintable
= FALSE
) const;
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.
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.
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.
641 * @param ec error code input/output parameter
643 * @return a reference to this set
647 UnicodeSet
& applyIntPropertyValue(UProperty prop
,
652 * Modifies this set to contain those code points which have the
653 * given value for the given property. Prior contents of this
655 * A frozen set will not be modified.
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:
664 * "ANY" = [\\u0000-\\U0010FFFF],
665 * "ASCII" = [\\u0000-\\u007F],
666 * "Assigned" = [:^Cn:].
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.
674 * @param ec error code input/output parameter
676 * @return a reference to this set
680 UnicodeSet
& applyPropertyAlias(const UnicodeString
& prop
,
681 const UnicodeString
& value
,
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.
689 * @return the number of elements in this set (its cardinality).
692 virtual int32_t size(void) const;
695 * Returns <tt>true</tt> if this set contains no elements.
697 * @return <tt>true</tt> if this set contains no elements.
700 virtual UBool
isEmpty(void) const;
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
709 virtual UBool
contains(UChar32 c
) const;
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
719 virtual UBool
contains(UChar32 start
, UChar32 end
) const;
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
728 UBool
contains(const UnicodeString
& s
) const;
731 * Returns true if this set contains all the characters and strings
733 * @param c set to be checked for containment
734 * @return true if the test condition is met
737 virtual UBool
containsAll(const UnicodeSet
& c
) const;
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
746 UBool
containsAll(const UnicodeString
& s
) const;
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
756 UBool
containsNone(UChar32 start
, UChar32 end
) const;
759 * Returns true if this set contains none of the characters and strings
761 * @param c set to be checked for containment
762 * @return true if the test condition is met
765 UBool
containsNone(const UnicodeSet
& c
) const;
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
774 UBool
containsNone(const UnicodeString
& s
) const;
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
784 inline UBool
containsSome(UChar32 start
, UChar32 end
) const;
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
793 inline UBool
containsSome(const UnicodeSet
& s
) const;
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
802 inline UBool
containsSome(const UnicodeString
& s
) const;
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
820 * @see USetSpanCondition
822 int32_t span(const UChar
*s
, int32_t length
, USetSpanCondition spanCondition
) const;
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
839 * @see USetSpanCondition
841 int32_t spanBack(const UChar
*s
, int32_t length
, USetSpanCondition spanCondition
) const;
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
859 * @see USetSpanCondition
861 int32_t spanUTF8(const char *s
, int32_t length
, USetSpanCondition spanCondition
) const;
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
878 * @see USetSpanCondition
880 int32_t spanBackUTF8(const char *s
, int32_t length
, USetSpanCondition spanCondition
) const;
883 * Implement UnicodeMatcher::matches()
886 virtual UMatchDegree
matches(const Replaceable
& text
,
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
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
913 static int32_t matchRest(const Replaceable
& text
,
914 int32_t start
, int32_t limit
,
915 const UnicodeString
& s
);
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
923 * @return the smallest integer i in the range 0..len-1,
924 * inclusive, such that c < list[i]
926 int32_t findCodePoint(UChar32 c
) const;
931 * Implementation of UnicodeMatcher API. Union the set of all
932 * characters that may be matched by this object into the given
934 * @param toUnionTo the set into which to union the source characters
937 virtual void addMatchSetTo(UnicodeSet
& toUnionTo
) const;
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
947 int32_t indexOf(UChar32 c
) const;
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.
958 UChar32
charAt(int32_t index
) const;
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.
968 * @param start first character, inclusive, of range to be added
970 * @param end last character, inclusive, of range to be added
974 virtual UnicodeSet
& add(UChar32 start
, UChar32 end
);
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.
983 UnicodeSet
& add(UChar32 c
);
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
996 UnicodeSet
& add(const UnicodeString
& s
);
1000 * @return a code point IF the string consists of a single one.
1001 * otherwise returns -1.
1002 * @param string to test
1004 static int32_t getSingleCP(const UnicodeString
& s
);
1006 void _add(const UnicodeString
& s
);
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
1017 UnicodeSet
& addAll(const UnicodeString
& s
);
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
1027 UnicodeSet
& retainAll(const UnicodeString
& s
);
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
1037 UnicodeSet
& complementAll(const UnicodeString
& s
);
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
1047 UnicodeSet
& removeAll(const UnicodeString
& s
);
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.
1057 static UnicodeSet
* U_EXPORT2
createFrom(const UnicodeString
& s
);
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.
1067 static UnicodeSet
* U_EXPORT2
createFromAll(const UnicodeString
& s
);
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.
1076 * @param start first character, inclusive, of range to be retained
1078 * @param end last character, inclusive, of range to be retained
1082 virtual UnicodeSet
& retain(UChar32 start
, UChar32 end
);
1086 * Retain the specified character from this set if it is present.
1087 * A frozen set will not be modified.
1090 UnicodeSet
& retain(UChar32 c
);
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.
1099 * @param start first character, inclusive, of range to be removed
1101 * @param end last character, inclusive, of range to be removed
1105 virtual UnicodeSet
& remove(UChar32 start
, UChar32 end
);
1108 * Removes the specified character from this set if it is present.
1109 * The set will not contain the specified range once the call
1111 * A frozen set will not be modified.
1114 UnicodeSet
& remove(UChar32 c
);
1117 * Removes the specified string from this set if it is present.
1118 * The set will not contain the specified character once the call
1120 * A frozen set will not be modified.
1121 * @param s the source string
1122 * @return this object, for chaining
1125 UnicodeSet
& remove(const UnicodeString
& s
);
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.
1134 virtual UnicodeSet
& complement(void);
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.
1144 * @param start first character, inclusive, of range to be removed
1146 * @param end last character, inclusive, of range to be removed
1150 virtual UnicodeSet
& complement(UChar32 start
, UChar32 end
);
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
1156 * A frozen set will not be modified.
1159 UnicodeSet
& complement(UChar32 c
);
1162 * Complement the specified string in this set.
1163 * The set will not contain the specified string once the call
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
1171 UnicodeSet
& complement(const UnicodeString
& s
);
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.
1181 * @param c set whose elements are to be added to this set.
1182 * @see #add(UChar32, UChar32)
1185 virtual UnicodeSet
& addAll(const UnicodeSet
& c
);
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.
1195 * @param c set that defines which elements this set will retain.
1198 virtual UnicodeSet
& retainAll(const UnicodeSet
& c
);
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
1205 * A frozen set will not be modified.
1207 * @param c set that defines which elements will be removed from
1211 virtual UnicodeSet
& removeAll(const UnicodeSet
& c
);
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.
1219 * @param c set that defines which elements will be xor'ed from
1223 virtual UnicodeSet
& complementAll(const UnicodeSet
& c
);
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.
1231 virtual UnicodeSet
& clear(void);
1234 * Close this set over the given attribute. For the attribute
1235 * USET_CASE, the result is to modify this set so that:
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
1241 * 2. For each string 'e' in the resulting set, if e !=
1242 * foldCase(e), 'e' will be removed.
1244 * Example: [aq\\u00DF{Bc}{bC}{Fi}] => [aAqQ\\u00DF\\uFB01{ss}{bc}{fi}]
1246 * (Here foldCase(x) refers to the operation u_strFoldCase, and a
1247 * == b denotes that the contents are the same, not pointer
1250 * A frozen set will not be modified.
1252 * @param attribute bitmask for attributes to close over.
1253 * Currently only the USET_CASE bit is supported. Any undefined bits
1255 * @return a reference to this set.
1258 UnicodeSet
& closeOver(int32_t attribute
);
1261 * Remove all strings from this set.
1263 * @return a reference to this set.
1266 virtual UnicodeSet
&removeAllStrings();
1269 * Iteration method that returns the number of ranges contained in
1271 * @see #getRangeStart
1275 virtual int32_t getRangeCount(void) const;
1278 * Iteration method that returns the first character in the
1279 * specified range of this set.
1280 * @see #getRangeCount
1284 virtual UChar32
getRangeStart(int32_t index
) const;
1287 * Iteration method that returns the last character in the
1288 * specified range of this set.
1289 * @see #getRangeStart
1293 virtual UChar32
getRangeEnd(int32_t index
) const;
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.
1300 * The array has following format (each line is one 16-bit
1303 * length = (n+2*m) | (m!=0?0x8000:0)
1304 * bmpLength = n; present if m!=0
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.
1321 * If there are no supplementary characters (if m==0) then the
1322 * header is one 16-bit integer, 'length', with value n.
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.
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.
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.
1343 int32_t serialize(uint16_t *dest
, int32_t destCapacity
, UErrorCode
& ec
) const;
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.
1351 virtual UnicodeSet
& compact();
1354 * Return the class ID for this class. This is useful only for
1355 * comparing to a return value from getDynamicClassID(). For example:
1357 * . Base* polymorphic_pointer = createPolymorphicObject();
1358 * . if (polymorphic_pointer->getDynamicClassID() ==
1359 * . Derived::getStaticClassID()) ...
1361 * @return The class ID for all objects of this class.
1364 static UClassID U_EXPORT2
getStaticClassID(void);
1367 * Implement UnicodeFunctor API.
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.
1374 virtual UClassID
getDynamicClassID(void) const;
1378 // Private API for the USet API
1380 friend class USetAccess
;
1382 int32_t getStringCount() const;
1384 const UnicodeString
* getString(int32_t index
) const;
1386 //----------------------------------------------------------------
1387 // RuleBasedTransliterator support
1388 //----------------------------------------------------------------
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
1397 virtual UBool
matchesIndexValue(uint8_t v
) const;
1401 //----------------------------------------------------------------
1402 // Implementation: Clone as thawed (see ICU4J Freezable)
1403 //----------------------------------------------------------------
1405 UnicodeSet(const UnicodeSet
& o
, UBool
/* asThawed */);
1407 //----------------------------------------------------------------
1408 // Implementation: Pattern parsing
1409 //----------------------------------------------------------------
1411 void applyPattern(RuleCharacterIterator
& chars
,
1412 const SymbolTable
* symbols
,
1413 UnicodeString
& rebuiltPat
,
1417 //----------------------------------------------------------------
1418 // Implementation: Utility methods
1419 //----------------------------------------------------------------
1421 void ensureCapacity(int32_t newLen
, UErrorCode
& ec
);
1423 void ensureBufferCapacity(int32_t newLen
, UErrorCode
& ec
);
1425 void swapBuffers(void);
1427 UBool
allocateStrings(UErrorCode
&status
);
1429 UnicodeString
& _toPattern(UnicodeString
& result
,
1430 UBool escapeUnprintable
) const;
1432 UnicodeString
& _generatePattern(UnicodeString
& result
,
1433 UBool escapeUnprintable
) const;
1435 static void _appendToPat(UnicodeString
& buf
, const UnicodeString
& s
, UBool escapeUnprintable
);
1437 static void _appendToPat(UnicodeString
& buf
, UChar32 c
, UBool escapeUnprintable
);
1439 //----------------------------------------------------------------
1440 // Implementation: Fundamental operators
1441 //----------------------------------------------------------------
1443 void exclusiveOr(const UChar32
* other
, int32_t otherLen
, int8_t polarity
);
1445 void add(const UChar32
* other
, int32_t otherLen
, int8_t polarity
);
1447 void retain(const UChar32
* other
, int32_t otherLen
, int8_t polarity
);
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}.
1454 static UBool
resemblesPropertyPattern(const UnicodeString
& pattern
,
1457 static UBool
resemblesPropertyPattern(RuleCharacterIterator
& chars
,
1461 * Parse the given property pattern at the given parse position
1462 * and set this UnicodeSet to the result.
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
1468 * Recognized syntax:
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"
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.
1479 * We support binary properties, enumerated properties, and the
1480 * following non-enumerated properties:
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 '^':
1490 * [:blah:] \\p{blah} \\P{blah} \\N{name}
1493 * On return, the position after the last character parsed, that is,
1494 * the locations marked '%'. If the parse fails, ppos is returned
1496 * @return a reference to this.
1498 UnicodeSet
& applyPropertyPattern(const UnicodeString
& pattern
,
1499 ParsePosition
& ppos
,
1502 void applyPropertyPattern(RuleCharacterIterator
& chars
,
1503 UnicodeString
& rebuiltPat
,
1506 static const UnicodeSet
* getInclusions(int32_t src
, UErrorCode
&status
);
1509 * A filter that returns TRUE if the given code point should be
1510 * included in the UnicodeSet being constructed.
1512 typedef UBool (*Filter
)(UChar32 codePoint
, void* context
);
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
1521 * src is a UPropertySource value.
1523 void applyFilter(Filter filter
,
1526 UErrorCode
&status
);
1529 * Set the new pattern to cache.
1531 void setPattern(const UnicodeString
& newPat
);
1533 * Release existing cached pattern.
1535 void releasePattern();
1537 friend class UnicodeSetIterator
;
1540 inline UBool
UnicodeSet::operator!=(const UnicodeSet
& o
) const {
1541 return !operator==(o
);
1544 inline UBool
UnicodeSet::isFrozen() const {
1545 return (UBool
)(bmpSet
!=NULL
|| stringSpan
!=NULL
);
1548 inline UBool
UnicodeSet::containsSome(UChar32 start
, UChar32 end
) const {
1549 return !containsNone(start
, end
);
1552 inline UBool
UnicodeSet::containsSome(const UnicodeSet
& s
) const {
1553 return !containsNone(s
);
1556 inline UBool
UnicodeSet::containsSome(const UnicodeString
& s
) const {
1557 return !containsNone(s
);
1560 inline UBool
UnicodeSet::isBogus() const {
1561 return (UBool
)(fFlags
& kIsBogus
);