]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/uniset.h
2 **********************************************************************
3 * Copyright (C) 1999-2004, International Business Machines Corporation and others. All Rights Reserved.
4 **********************************************************************
5 * Date Name Description
6 * 10/20/99 alan Creation.
7 **********************************************************************
13 #include "unicode/unifilt.h"
14 #include "unicode/unistr.h"
15 #include "unicode/uset.h"
23 class RuleCharacterIterator
;
26 * A mutable set of Unicode characters and multicharacter strings. Objects of this class
27 * represent <em>character classes</em> used in regular expressions.
28 * A character specifies a subset of Unicode code points. Legal
29 * code points are U+0000 to U+10FFFF, inclusive.
31 * <p>The UnicodeSet class is not designed to be subclassed.
33 * <p><code>UnicodeSet</code> supports two APIs. The first is the
34 * <em>operand</em> API that allows the caller to modify the value of
35 * a <code>UnicodeSet</code> object. It conforms to Java 2's
36 * <code>java.util.Set</code> interface, although
37 * <code>UnicodeSet</code> does not actually implement that
38 * interface. All methods of <code>Set</code> are supported, with the
39 * modification that they take a character range or single character
40 * instead of an <code>Object</code>, and they take a
41 * <code>UnicodeSet</code> instead of a <code>Collection</code>. The
42 * operand API may be thought of in terms of boolean logic: a boolean
43 * OR is implemented by <code>add</code>, a boolean AND is implemented
44 * by <code>retain</code>, a boolean XOR is implemented by
45 * <code>complement</code> taking an argument, and a boolean NOT is
46 * implemented by <code>complement</code> with no argument. In terms
47 * of traditional set theory function names, <code>add</code> is a
48 * union, <code>retain</code> is an intersection, <code>remove</code>
49 * is an asymmetric difference, and <code>complement</code> with no
50 * argument is a set complement with respect to the superset range
51 * <code>MIN_VALUE-MAX_VALUE</code>
53 * <p>The second API is the
54 * <code>applyPattern()</code>/<code>toPattern()</code> API from the
55 * <code>java.text.Format</code>-derived classes. Unlike the
56 * methods that add characters, add categories, and control the logic
57 * of the set, the method <code>applyPattern()</code> sets all
58 * attributes of a <code>UnicodeSet</code> at once, based on a
61 * <p><b>Pattern syntax</b></p>
63 * Patterns are accepted by the constructors and the
64 * <code>applyPattern()</code> methods and returned by the
65 * <code>toPattern()</code> method. These patterns follow a syntax
66 * similar to that employed by version 8 regular expression character
67 * classes. Here are some simple examples:
69 * \htmlonly<blockquote>\endhtmlonly
72 * <td nowrap valign="top" align="left"><code>[]</code></td>
73 * <td valign="top">No characters</td>
74 * </tr><tr align="top">
75 * <td nowrap valign="top" align="left"><code>[a]</code></td>
76 * <td valign="top">The character 'a'</td>
77 * </tr><tr align="top">
78 * <td nowrap valign="top" align="left"><code>[ae]</code></td>
79 * <td valign="top">The characters 'a' and 'e'</td>
82 * <td nowrap valign="top" align="left"><code>[a-e]</code></td>
83 * <td valign="top">The characters 'a' through 'e' inclusive, in Unicode code
87 * <td nowrap valign="top" align="left"><code>[\\u4E01]</code></td>
88 * <td valign="top">The character U+4E01</td>
91 * <td nowrap valign="top" align="left"><code>[a{ab}{ac}]</code></td>
92 * <td valign="top">The character 'a' and the multicharacter strings "ab" and
96 * <td nowrap valign="top" align="left"><code>[\\p{Lu}]</code></td>
97 * <td valign="top">All characters in the general category Uppercase Letter</td>
100 * \htmlonly</blockquote>\endhtmlonly
102 * Any character may be preceded by a backslash in order to remove any special
103 * meaning. White space characters, as defined by UCharacter.isWhitespace(), are
104 * ignored, unless they are escaped.
106 * <p>Property patterns specify a set of characters having a certain
107 * property as defined by the Unicode standard. Both the POSIX-like
108 * "[:Lu:]" and the Perl-like syntax "\\p{Lu}" are recognized. For a
109 * complete list of supported property patterns, see the User's Guide
111 * <a href="http://oss.software.ibm.com/icu/userguide/unicodeSet.html">
112 * http://oss.software.ibm.com/icu/userguide/unicodeSet.html</a>.
113 * Actual determination of property data is defined by the underlying
114 * Unicode database as implemented by UCharacter.
116 * <p>Patterns specify individual characters, ranges of characters, and
117 * Unicode property sets. When elements are concatenated, they
118 * specify their union. To complement a set, place a '^' immediately
119 * after the opening '['. Property patterns are inverted by modifying
120 * their delimiters; "[:^foo]" and "\\P{foo}". In any other location,
121 * '^' has no special meaning.
123 * <p>Ranges are indicated by placing two a '-' between two
124 * characters, as in "a-z". This specifies the range of all
125 * characters from the left to the right, in Unicode order. If the
126 * left character is greater than or equal to the
127 * right character it is a syntax error. If a '-' occurs as the first
128 * character after the opening '[' or '[^', or if it occurs as the
129 * last character before the closing ']', then it is taken as a
130 * literal. Thus "[a\-b]", "[-ab]", and "[ab-]" all indicate the same
131 * set of three characters, 'a', 'b', and '-'.
133 * <p>Sets may be intersected using the '&' operator or the asymmetric
134 * set difference may be taken using the '-' operator, for example,
135 * "[[:L:]&[\\u0000-\\u0FFF]]" indicates the set of all Unicode letters
136 * with values less than 4096. Operators ('&' and '|') have equal
137 * precedence and bind left-to-right. Thus
138 * "[[:L:]-[a-z]-[\\u0100-\\u01FF]]" is equivalent to
139 * "[[[:L:]-[a-z]]-[\\u0100-\\u01FF]]". This only really matters for
140 * difference; intersection is commutative.
143 * <tr valign=top><td nowrap><code>[a]</code><td>The set containing 'a'
144 * <tr valign=top><td nowrap><code>[a-z]</code><td>The set containing 'a'
145 * through 'z' and all letters in between, in Unicode order
146 * <tr valign=top><td nowrap><code>[^a-z]</code><td>The set containing
147 * all characters but 'a' through 'z',
148 * that is, U+0000 through 'a'-1 and 'z'+1 through U+10FFFF
149 * <tr valign=top><td nowrap><code>[[<em>pat1</em>][<em>pat2</em>]]</code>
150 * <td>The union of sets specified by <em>pat1</em> and <em>pat2</em>
151 * <tr valign=top><td nowrap><code>[[<em>pat1</em>]&[<em>pat2</em>]]</code>
152 * <td>The intersection of sets specified by <em>pat1</em> and <em>pat2</em>
153 * <tr valign=top><td nowrap><code>[[<em>pat1</em>]-[<em>pat2</em>]]</code>
154 * <td>The asymmetric difference of sets specified by <em>pat1</em> and
156 * <tr valign=top><td nowrap><code>[:Lu:] or \\p{Lu}</code>
157 * <td>The set of characters having the specified
158 * Unicode property; in
159 * this case, Unicode uppercase letters
160 * <tr valign=top><td nowrap><code>[:^Lu:] or \\P{Lu}</code>
161 * <td>The set of characters <em>not</em> having the given
165 * <p><b>Warning</b>: you cannot add an empty string ("") to a UnicodeSet.</p>
167 * <p><b>Formal syntax</b></p>
169 * \htmlonly<blockquote>\endhtmlonly
172 * <td nowrap valign="top" align="right"><code>pattern := </code></td>
173 * <td valign="top"><code>('[' '^'? item* ']') |
174 * property</code></td>
177 * <td nowrap valign="top" align="right"><code>item := </code></td>
178 * <td valign="top"><code>char | (char '-' char) | pattern-expr<br>
182 * <td nowrap valign="top" align="right"><code>pattern-expr := </code></td>
183 * <td valign="top"><code>pattern | pattern-expr pattern |
184 * pattern-expr op pattern<br>
188 * <td nowrap valign="top" align="right"><code>op := </code></td>
189 * <td valign="top"><code>'&' | '-'<br>
193 * <td nowrap valign="top" align="right"><code>special := </code></td>
194 * <td valign="top"><code>'[' | ']' | '-'<br>
198 * <td nowrap valign="top" align="right"><code>char := </code></td>
199 * <td valign="top"><em>any character that is not</em><code> special<br>
200 * | ('\' </code><em>any character</em><code>)<br>
201 * | ('\\u' hex hex hex hex)<br>
205 * <td nowrap valign="top" align="right"><code>hex := </code></td>
206 * <td valign="top"><em>any character for which
207 * </em><code>Character.digit(c, 16)</code><em>
208 * returns a non-negative result</em></td>
211 * <td nowrap valign="top" align="right"><code>property := </code></td>
212 * <td valign="top"><em>a Unicode property set pattern</em></td>
218 * <td>Legend: <table>
220 * <td nowrap valign="top"><code>a := b</code></td>
221 * <td width="20" valign="top"> </td>
222 * <td valign="top"><code>a</code> may be replaced by <code>b</code> </td>
225 * <td nowrap valign="top"><code>a?</code></td>
226 * <td valign="top"></td>
227 * <td valign="top">zero or one instance of <code>a</code><br>
231 * <td nowrap valign="top"><code>a*</code></td>
232 * <td valign="top"></td>
233 * <td valign="top">one or more instances of <code>a</code><br>
237 * <td nowrap valign="top"><code>a | b</code></td>
238 * <td valign="top"></td>
239 * <td valign="top">either <code>a</code> or <code>b</code><br>
243 * <td nowrap valign="top"><code>'a'</code></td>
244 * <td valign="top"></td>
245 * <td valign="top">the literal string between the quotes </td>
251 * \htmlonly</blockquote>\endhtmlonly
256 class U_COMMON_API UnicodeSet
: public UnicodeFilter
{
258 int32_t len
; // length of list used; 0 <= len <= capacity
259 int32_t capacity
; // capacity of list
260 int32_t bufferCapacity
; // capacity of buffer
261 UChar32
* list
; // MUST be terminated with HIGH
262 UChar32
* buffer
; // internal buffer, may be NULL
264 UVector
* strings
; // maintained in sorted order
267 * The pattern representation of this set. This may not be the
268 * most economical pattern. It is the pattern supplied to
269 * applyPattern(), with variables substituted and whitespace
270 * removed. For sets constructed without applyPattern(), or
271 * modified using the non-pattern API, this string will be empty,
272 * indicating that toPattern() must generate a pattern
273 * representation from the inversion list.
280 * Minimum value that can be stored in a UnicodeSet.
284 static U_COMMON_API
const UChar32 MIN_VALUE
;
286 static const UChar32 MIN_VALUE
;
290 * Maximum value that can be stored in a UnicodeSet.
294 static U_COMMON_API
const UChar32 MAX_VALUE
;
296 static const UChar32 MAX_VALUE
;
299 //----------------------------------------------------------------
301 //----------------------------------------------------------------
306 * Constructs an empty set.
312 * Constructs a set containing the given range. If <code>end >
313 * start</code> then an empty set is created.
315 * @param start first character, inclusive, of range
316 * @param end last character, inclusive, of range
319 UnicodeSet(UChar32 start
, UChar32 end
);
322 * Constructs a set from the given pattern. See the class
323 * description for the syntax of the pattern language.
324 * @param pattern a string specifying what characters are in the set
325 * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern
326 * contains a syntax error.
329 UnicodeSet(const UnicodeString
& pattern
,
333 * Constructs a set from the given pattern. See the class
334 * description for the syntax of the pattern language.
335 * @param pattern a string specifying what characters are in the set
336 * @param options bitmask for options to apply to the pattern.
337 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
338 * @param symbols a symbol table mapping variable names to values
339 * and stand-in characters to UnicodeSets; may be NULL
340 * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern
341 * contains a syntax error.
344 UnicodeSet(const UnicodeString
& pattern
,
346 const SymbolTable
* symbols
,
350 * Constructs a set from the given pattern. See the class description
351 * for the syntax of the pattern language.
352 * @param pattern a string specifying what characters are in the set
353 * @param pos on input, the position in pattern at which to start parsing.
354 * On output, the position after the last character parsed.
355 * @param options bitmask for options to apply to the pattern.
356 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
357 * @param symbols a symbol table mapping variable names to values
358 * and stand-in characters to UnicodeSets; may be NULL
359 * @param status input-output error code
362 UnicodeSet(const UnicodeString
& pattern
, ParsePosition
& pos
,
364 const SymbolTable
* symbols
,
367 #ifdef U_USE_UNICODESET_DEPRECATES
369 * Obsolete: Constructs a set from the given Unicode character category.
370 * @param category an integer indicating the character category as
371 * defined in uchar.h.
372 * @obsolete ICU 2.6. Use a pattern with the category instead since this API will be removed in that release.
374 UnicodeSet(int8_t category
, UErrorCode
& status
);
378 * Constructs a set that is identical to the given UnicodeSet.
381 UnicodeSet(const UnicodeSet
& o
);
387 virtual ~UnicodeSet();
390 * Assigns this object to be a copy of another.
393 UnicodeSet
& operator=(const UnicodeSet
& o
);
396 * Compares the specified object with this set for equality. Returns
397 * <tt>true</tt> if the two sets
398 * have the same size, and every member of the specified set is
399 * contained in this set (or equivalently, every member of this set is
400 * contained in the specified set).
402 * @param o set to be compared for equality with this set.
403 * @return <tt>true</tt> if the specified set is equal to this set.
406 virtual UBool
operator==(const UnicodeSet
& o
) const;
409 * Compares the specified object with this set for equality. Returns
410 * <tt>true</tt> if the specified set is not equal to this set.
413 UBool
operator!=(const UnicodeSet
& o
) const;
416 * Returns a copy of this object. All UnicodeFunctor objects have
417 * to support cloning in order to allow classes using
418 * UnicodeFunctors, such as Transliterator, to implement cloning.
421 virtual UnicodeFunctor
* clone() const;
424 * Returns the hash code value for this set.
426 * @return the hash code value for this set.
427 * @see Object#hashCode()
430 virtual int32_t hashCode(void) const;
432 //----------------------------------------------------------------
434 //----------------------------------------------------------------
437 * Make this object represent the range <code>start - end</code>.
438 * If <code>end > start</code> then this object is set to an
441 * @param start first character in the set, inclusive
442 * @param end last character in the set, inclusive
445 UnicodeSet
& set(UChar32 start
, UChar32 end
);
448 * Return true if the given position, in the given pattern, appears
449 * to be the start of a UnicodeSet pattern.
452 static UBool
resemblesPattern(const UnicodeString
& pattern
,
456 * Modifies this set to represent the set specified by the given
457 * pattern, optionally ignoring white space. See the class
458 * description for the syntax of the pattern language.
459 * @param pattern a string specifying what characters are in the set
460 * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern
461 * contains a syntax error.
462 * <em> Empties the set passed before applying the pattern.</em>
463 * @return a reference to this
466 UnicodeSet
& applyPattern(const UnicodeString
& pattern
,
470 * Modifies this set to represent the set specified by the given
471 * pattern, optionally ignoring white space. See the class
472 * description for the syntax of the pattern language.
473 * @param pattern a string specifying what characters are in the set
474 * @param options bitmask for options to apply to the pattern.
475 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
476 * @param symbols a symbol table mapping variable names to
477 * values and stand-ins to UnicodeSets; may be NULL
478 * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern
479 * contains a syntax error.
480 *<em> Empties the set passed before applying the pattern.</em>
481 * @return a reference to this
484 UnicodeSet
& applyPattern(const UnicodeString
& pattern
,
486 const SymbolTable
* symbols
,
490 * Parses the given pattern, starting at the given position. The
491 * character at pattern.charAt(pos.getIndex()) must be '[', or the
492 * parse fails. Parsing continues until the corresponding closing
493 * ']'. If a syntax error is encountered between the opening and
494 * closing brace, the parse fails. Upon return from a successful
495 * parse, the ParsePosition is updated to point to the character
496 * following the closing ']', and a StringBuffer containing a
497 * pairs list for the parsed pattern is returned. This method calls
498 * itself recursively to parse embedded subpatterns.
499 *<em> Empties the set passed before applying the pattern.</em>
501 * @param pattern the string containing the pattern to be parsed.
502 * The portion of the string from pos.getIndex(), which must be a
503 * '[', to the corresponding closing ']', is parsed.
504 * @param pos upon entry, the position at which to being parsing.
505 * The character at pattern.charAt(pos.getIndex()) must be a '['.
506 * Upon return from a successful parse, pos.getIndex() is either
507 * the character after the closing ']' of the parsed pattern, or
508 * pattern.length() if the closing ']' is the last character of
509 * the pattern string.
510 * @param options bitmask for options to apply to the pattern.
511 * Valid options are USET_IGNORE_SPACE and USET_CASE_INSENSITIVE.
512 * @param symbols a symbol table mapping variable names to
513 * values and stand-ins to UnicodeSets; may be NULL
514 * @param status returns <code>U_ILLEGAL_ARGUMENT_ERROR</code> if the pattern
515 * contains a syntax error.
516 * @return a reference to this
519 UnicodeSet
& applyPattern(const UnicodeString
& pattern
,
522 const SymbolTable
* symbols
,
526 * Returns a string representation of this set. If the result of
527 * calling this function is passed to a UnicodeSet constructor, it
528 * will produce another set that is equal to this one.
529 * @param result the string to receive the rules. Previous
530 * contents will be deleted.
531 * @param escapeUnprintable if TRUE then convert unprintable
532 * character to their hex escape representations, \\uxxxx or
533 * \\Uxxxxxxxx. Unprintable characters are those other than
534 * U+000A, U+0020..U+007E.
537 virtual UnicodeString
& toPattern(UnicodeString
& result
,
538 UBool escapeUnprintable
= FALSE
) const;
541 * Modifies this set to contain those code points which have the given value
542 * for the given binary or enumerated property, as returned by
543 * u_getIntPropertyValue. Prior contents of this set are lost.
545 * @param prop a property in the range UCHAR_BIN_START..UCHAR_BIN_LIMIT-1
546 * or UCHAR_INT_START..UCHAR_INT_LIMIT-1
547 * or UCHAR_MASK_START..UCHAR_MASK_LIMIT-1.
549 * @param value a value in the range u_getIntPropertyMinValue(prop)..
550 * u_getIntPropertyMaxValue(prop), with one exception. If prop is
551 * UCHAR_GENERAL_CATEGORY_MASK, then value should not be a UCharCategory, but
552 * rather a mask value produced by U_GET_GC_MASK(). This allows grouped
553 * categories such as [:L:] to be represented.
555 * @param ec error code input/output parameter
557 * @return a reference to this set
561 UnicodeSet
& applyIntPropertyValue(UProperty prop
,
566 * Modifies this set to contain those code points which have the
567 * given value for the given property. Prior contents of this
570 * @param prop a property alias, either short or long. The name is matched
571 * loosely. See PropertyAliases.txt for names and a description of loose
572 * matching. If the value string is empty, then this string is interpreted
573 * as either a General_Category value alias, a Script value alias, a binary
574 * property alias, or a special ID. Special IDs are matched loosely and
575 * correspond to the following sets:
577 * "ANY" = [\\u0000-\\U0010FFFF],
578 * "ASCII" = [\\u0000-\\u007F].
580 * @param value a value alias, either short or long. The name is matched
581 * loosely. See PropertyValueAliases.txt for names and a description of
582 * loose matching. In addition to aliases listed, numeric values and
583 * canonical combining classes may be expressed numerically, e.g., ("nv",
584 * "0.5") or ("ccc", "220"). The value string may also be empty.
586 * @param ec error code input/output parameter
588 * @return a reference to this set
592 UnicodeSet
& applyPropertyAlias(const UnicodeString
& prop
,
593 const UnicodeString
& value
,
597 * Returns the number of elements in this set (its cardinality).
598 * Note than the elements of a set may include both individual
599 * codepoints and strings.
601 * @return the number of elements in this set (its cardinality).
604 virtual int32_t size(void) const;
607 * Returns <tt>true</tt> if this set contains no elements.
609 * @return <tt>true</tt> if this set contains no elements.
612 virtual UBool
isEmpty(void) const;
615 * Returns true if this set contains the given character.
616 * @param c character to be checked for containment
617 * @return true if the test condition is met
620 virtual UBool
contains(UChar32 c
) const;
623 * Returns true if this set contains every character
624 * of the given range.
625 * @param start first character, inclusive, of the range
626 * @param end last character, inclusive, of the range
627 * @return true if the test condition is met
630 virtual UBool
contains(UChar32 start
, UChar32 end
) const;
633 * Returns <tt>true</tt> if this set contains the given
634 * multicharacter string.
635 * @param s string to be checked for containment
636 * @return <tt>true</tt> if this set contains the specified string
639 UBool
contains(const UnicodeString
& s
) const;
642 * Returns true if this set contains all the characters and strings
644 * @param c set to be checked for containment
645 * @return true if the test condition is met
648 virtual UBool
containsAll(const UnicodeSet
& c
) const;
651 * Returns true if this set contains all the characters
652 * of the given string.
653 * @param s string containing characters to be checked for containment
654 * @return true if the test condition is met
657 UBool
containsAll(const UnicodeString
& s
) const;
660 * Returns true if this set contains none of the characters
661 * of the given range.
662 * @param start first character, inclusive, of the range
663 * @param end last character, inclusive, of the range
664 * @return true if the test condition is met
667 UBool
containsNone(UChar32 start
, UChar32 end
) const;
670 * Returns true if this set contains none of the characters and strings
672 * @param c set to be checked for containment
673 * @return true if the test condition is met
676 UBool
containsNone(const UnicodeSet
& c
) const;
679 * Returns true if this set contains none of the characters
680 * of the given string.
681 * @param s string containing characters to be checked for containment
682 * @return true if the test condition is met
685 UBool
containsNone(const UnicodeString
& s
) const;
688 * Returns true if this set contains one or more of the characters
689 * in the given range.
690 * @param start first character, inclusive, of the range
691 * @param end last character, inclusive, of the range
692 * @return true if the condition is met
695 inline UBool
containsSome(UChar32 start
, UChar32 end
) const;
698 * Returns true if this set contains one or more of the characters
699 * and strings of the given set.
700 * @param s The set to be checked for containment
701 * @return true if the condition is met
704 inline UBool
containsSome(const UnicodeSet
& s
) const;
707 * Returns true if this set contains one or more of the characters
708 * of the given string.
709 * @param s string containing characters to be checked for containment
710 * @return true if the condition is met
713 inline UBool
containsSome(const UnicodeString
& s
) const;
716 * Implement UnicodeMatcher::matches()
719 virtual UMatchDegree
matches(const Replaceable
& text
,
726 * Returns the longest match for s in text at the given position.
727 * If limit > start then match forward from start+1 to limit
728 * matching all characters except s.charAt(0). If limit < start,
729 * go backward starting from start-1 matching all characters
730 * except s.charAt(s.length()-1). This method assumes that the
731 * first character, text.charAt(start), matches s, so it does not
733 * @param text the text to match
734 * @param start the first character to match. In the forward
735 * direction, text.charAt(start) is matched against s.charAt(0).
736 * In the reverse direction, it is matched against
737 * s.charAt(s.length()-1).
738 * @param limit the limit offset for matching, either last+1 in
739 * the forward direction, or last-1 in the reverse direction,
740 * where last is the index of the last character to match.
741 * @return If part of s matches up to the limit, return |limit -
742 * start|. If all of s matches before reaching the limit, return
743 * s.length(). If there is a mismatch between s and text, return
746 static int32_t matchRest(const Replaceable
& text
,
747 int32_t start
, int32_t limit
,
748 const UnicodeString
& s
);
751 * Returns the smallest value i such that c < list[i]. Caller
752 * must ensure that c is a legal value or this method will enter
753 * an infinite loop. This method performs a binary search.
754 * @param c a character in the range MIN_VALUE..MAX_VALUE
756 * @return the smallest integer i in the range 0..len-1,
757 * inclusive, such that c < list[i]
759 int32_t findCodePoint(UChar32 c
) const;
764 * Implementation of UnicodeMatcher API. Union the set of all
765 * characters that may be matched by this object into the given
767 * @param toUnionTo the set into which to union the source characters
770 virtual void addMatchSetTo(UnicodeSet
& toUnionTo
) const;
773 * Returns the index of the given character within this set, where
774 * the set is ordered by ascending code point. If the character
775 * is not in this set, return -1. The inverse of this method is
776 * <code>charAt()</code>.
777 * @return an index from 0..size()-1, or -1
780 int32_t indexOf(UChar32 c
) const;
783 * Returns the character at the given index within this set, where
784 * the set is ordered by ascending code point. If the index is
785 * out of range, return (UChar32)-1. The inverse of this method is
786 * <code>indexOf()</code>.
787 * @param index an index from 0..size()-1
788 * @return the character at the given index, or (UChar32)-1.
791 UChar32
charAt(int32_t index
) const;
794 * Adds the specified range to this set if it is not already
795 * present. If this set already contains the specified range,
796 * the call leaves this set unchanged. If <code>end > start</code>
797 * then an empty range is added, leaving the set unchanged.
798 * This is equivalent to a boolean logic OR, or a set UNION.
800 * @param start first character, inclusive, of range to be added
802 * @param end last character, inclusive, of range to be added
806 virtual UnicodeSet
& add(UChar32 start
, UChar32 end
);
809 * Adds the specified character to this set if it is not already
810 * present. If this set already contains the specified character,
811 * the call leaves this set unchanged.
814 UnicodeSet
& add(UChar32 c
);
817 * Adds the specified multicharacter to this set if it is not already
818 * present. If this set already contains the multicharacter,
819 * the call leaves this set unchanged.
820 * Thus "ch" => {"ch"}
821 * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
822 * @param s the source string
823 * @return this object, for chaining
826 UnicodeSet
& add(const UnicodeString
& s
);
830 * @return a code point IF the string consists of a single one.
831 * otherwise returns -1.
832 * @param string to test
834 static int32_t getSingleCP(const UnicodeString
& s
);
836 void _add(const UnicodeString
& s
);
840 * Adds each of the characters in this string to the set. Thus "ch" => {"c", "h"}
841 * If this set already any particular character, it has no effect on that character.
842 * @param s the source string
843 * @return this object, for chaining
846 UnicodeSet
& addAll(const UnicodeString
& s
);
849 * Retains EACH of the characters in this string. Note: "ch" == {"c", "h"}
850 * If this set already any particular character, it has no effect on that character.
851 * @param s the source string
852 * @return this object, for chaining
855 UnicodeSet
& retainAll(const UnicodeString
& s
);
858 * Complement EACH of the characters in this string. Note: "ch" == {"c", "h"}
859 * If this set already any particular character, it has no effect on that character.
860 * @param s the source string
861 * @return this object, for chaining
864 UnicodeSet
& complementAll(const UnicodeString
& s
);
867 * Remove EACH of the characters in this string. Note: "ch" == {"c", "h"}
868 * If this set already any particular character, it has no effect on that character.
869 * @param s the source string
870 * @return this object, for chaining
873 UnicodeSet
& removeAll(const UnicodeString
& s
);
876 * Makes a set from a multicharacter string. Thus "ch" => {"ch"}
877 * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
878 * @param s the source string
879 * @return a newly created set containing the given string.
880 * The caller owns the return object and is responsible for deleting it.
883 static UnicodeSet
* U_EXPORT2
createFrom(const UnicodeString
& s
);
887 * Makes a set from each of the characters in the string. Thus "ch" => {"c", "h"}
888 * @param s the source string
889 * @return a newly created set containing the given characters
890 * The caller owns the return object and is responsible for deleting it.
893 static UnicodeSet
* U_EXPORT2
createFromAll(const UnicodeString
& s
);
896 * Retain only the elements in this set that are contained in the
897 * specified range. If <code>end > start</code> then an empty range is
898 * retained, leaving the set empty. This is equivalent to
899 * a boolean logic AND, or a set INTERSECTION.
901 * @param start first character, inclusive, of range to be retained
903 * @param end last character, inclusive, of range to be retained
907 virtual UnicodeSet
& retain(UChar32 start
, UChar32 end
);
911 * Retain the specified character from this set if it is present.
914 UnicodeSet
& retain(UChar32 c
);
917 * Removes the specified range from this set if it is present.
918 * The set will not contain the specified range once the call
919 * returns. If <code>end > start</code> then an empty range is
920 * removed, leaving the set unchanged.
922 * @param start first character, inclusive, of range to be removed
924 * @param end last character, inclusive, of range to be removed
928 virtual UnicodeSet
& remove(UChar32 start
, UChar32 end
);
931 * Removes the specified character from this set if it is present.
932 * The set will not contain the specified range once the call
936 UnicodeSet
& remove(UChar32 c
);
939 * Removes the specified string from this set if it is present.
940 * The set will not contain the specified character once the call
942 * @param s the source string
943 * @return this object, for chaining
946 UnicodeSet
& remove(const UnicodeString
& s
);
949 * Inverts this set. This operation modifies this set so that
950 * its value is its complement. This is equivalent to
951 * <code>complement(MIN_VALUE, MAX_VALUE)</code>.
954 virtual UnicodeSet
& complement(void);
957 * Complements the specified range in this set. Any character in
958 * the range will be removed if it is in this set, or will be
959 * added if it is not in this set. If <code>end > start</code>
960 * then an empty range is complemented, leaving the set unchanged.
961 * This is equivalent to a boolean logic XOR.
963 * @param start first character, inclusive, of range to be removed
965 * @param end last character, inclusive, of range to be removed
969 virtual UnicodeSet
& complement(UChar32 start
, UChar32 end
);
972 * Complements the specified character in this set. The character
973 * will be removed if it is in this set, or will be added if it is
977 UnicodeSet
& complement(UChar32 c
);
980 * Complement the specified string in this set.
981 * The set will not contain the specified string once the call
983 * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
984 * @param s the string to complement
985 * @return this object, for chaining
988 UnicodeSet
& complement(const UnicodeString
& s
);
991 * Adds all of the elements in the specified set to this set if
992 * they're not already present. This operation effectively
993 * modifies this set so that its value is the <i>union</i> of the two
994 * sets. The behavior of this operation is unspecified if the specified
995 * collection is modified while the operation is in progress.
997 * @param c set whose elements are to be added to this set.
998 * @see #add(char, char)
1001 virtual UnicodeSet
& addAll(const UnicodeSet
& c
);
1004 * Retains only the elements in this set that are contained in the
1005 * specified set. In other words, removes from this set all of
1006 * its elements that are not contained in the specified set. This
1007 * operation effectively modifies this set so that its value is
1008 * the <i>intersection</i> of the two sets.
1010 * @param c set that defines which elements this set will retain.
1013 virtual UnicodeSet
& retainAll(const UnicodeSet
& c
);
1016 * Removes from this set all of its elements that are contained in the
1017 * specified set. This operation effectively modifies this
1018 * set so that its value is the <i>asymmetric set difference</i> of
1021 * @param c set that defines which elements will be removed from
1025 virtual UnicodeSet
& removeAll(const UnicodeSet
& c
);
1028 * Complements in this set all elements contained in the specified
1029 * set. Any character in the other set will be removed if it is
1030 * in this set, or will be added if it is not in this set.
1032 * @param c set that defines which elements will be xor'ed from
1036 virtual UnicodeSet
& complementAll(const UnicodeSet
& c
);
1039 * Removes all of the elements from this set. This set will be
1040 * empty after this call returns.
1043 virtual UnicodeSet
& clear(void);
1046 * Close this set over the given attribute. For the attribute
1047 * USET_CASE, the result is to modify this set so that:
1049 * 1. For each character or string 'a' in this set, all strings or
1050 * characters 'b' such that foldCase(a) == foldCase(b) are added
1053 * 2. For each string 'e' in the resulting set, if e !=
1054 * foldCase(e), 'e' will be removed.
1056 * Example: [aq\\u00DF{Bc}{bC}{Fi}] => [aAqQ\\u00DF\\uFB01{ss}{bc}{fi}]
1058 * (Here foldCase(x) refers to the operation u_strFoldCase, and a
1059 * == b denotes that the contents are the same, not pointer
1062 * @param attribute bitmask for attributes to close over.
1063 * Currently only the USET_CASE bit is supported. Any undefined bits
1065 * @return a reference to this set.
1068 UnicodeSet
& closeOver(int32_t attribute
);
1071 * Iteration method that returns the number of ranges contained in
1073 * @see #getRangeStart
1077 virtual int32_t getRangeCount(void) const;
1080 * Iteration method that returns the first character in the
1081 * specified range of this set.
1082 * @see #getRangeCount
1086 virtual UChar32
getRangeStart(int32_t index
) const;
1089 * Iteration method that returns the last character in the
1090 * specified range of this set.
1091 * @see #getRangeStart
1095 virtual UChar32
getRangeEnd(int32_t index
) const;
1098 * Serializes this set into an array of 16-bit integers. Serialization
1099 * (currently) only records the characters in the set; multicharacter
1100 * strings are ignored.
1102 * The array has following format (each line is one 16-bit
1105 * length = (n+2*m) | (m!=0?0x8000:0)
1106 * bmpLength = n; present if m!=0
1119 * The array starts with a header. After the header are n bmp
1120 * code points, then m supplementary code points. Either n or m
1121 * or both may be zero. n+2*m is always <= 0x7FFF.
1123 * If there are no supplementary characters (if m==0) then the
1124 * header is one 16-bit integer, 'length', with value n.
1126 * If there are supplementary characters (if m!=0) then the header
1127 * is two 16-bit integers. The first, 'length', has value
1128 * (n+2*m)|0x8000. The second, 'bmpLength', has value n.
1130 * After the header the code points are stored in ascending order.
1131 * Supplementary code points are stored as most significant 16
1132 * bits followed by least significant 16 bits.
1134 * @param dest pointer to buffer of destCapacity 16-bit integers.
1135 * May be NULL only if destCapacity is zero.
1136 * @param destCapacity size of dest, or zero. Must not be negative.
1137 * @param ec error code. Will be set to U_INDEX_OUTOFBOUNDS_ERROR
1138 * if n+2*m > 0x7FFF. Will be set to U_BUFFER_OVERFLOW_ERROR if
1139 * n+2*m+(m!=0?2:1) > destCapacity.
1140 * @return the total length of the serialized format, including
1141 * the header, that is, n+2*m+(m!=0?2:1), or 0 on error other
1142 * than U_BUFFER_OVERFLOW_ERROR.
1145 int32_t serialize(uint16_t *dest
, int32_t destCapacity
, UErrorCode
& ec
) const;
1148 * Reallocate this objects internal structures to take up the least
1149 * possible space, without changing this object's value.
1152 virtual UnicodeSet
& compact();
1155 * Return the class ID for this class. This is useful only for
1156 * comparing to a return value from getDynamicClassID(). For example:
1158 * . Base* polymorphic_pointer = createPolymorphicObject();
1159 * . if (polymorphic_pointer->getDynamicClassID() ==
1160 * . Derived::getStaticClassID()) ...
1162 * @return The class ID for all objects of this class.
1165 static UClassID U_EXPORT2
getStaticClassID(void);
1168 * Implement UnicodeFunctor API.
1170 * @return The class ID for this object. All objects of a given
1171 * class have the same class ID. Objects of other classes have
1172 * different class IDs.
1175 virtual UClassID
getDynamicClassID(void) const;
1179 // Private API for the USet API
1181 friend class USetAccess
;
1183 int32_t getStringCount() const;
1185 const UnicodeString
* getString(int32_t index
) const;
1187 //----------------------------------------------------------------
1188 // RuleBasedTransliterator support
1189 //----------------------------------------------------------------
1194 * Returns <tt>true</tt> if this set contains any character whose low byte
1195 * is the given value. This is used by <tt>RuleBasedTransliterator</tt> for
1198 virtual UBool
matchesIndexValue(uint8_t v
) const;
1202 //----------------------------------------------------------------
1203 // Implementation: Pattern parsing
1204 //----------------------------------------------------------------
1206 void applyPattern(RuleCharacterIterator
& chars
,
1207 const SymbolTable
* symbols
,
1208 UnicodeString
& rebuiltPat
,
1212 //----------------------------------------------------------------
1213 // Implementation: Utility methods
1214 //----------------------------------------------------------------
1216 void ensureCapacity(int32_t newLen
);
1218 void ensureBufferCapacity(int32_t newLen
);
1220 void swapBuffers(void);
1222 UBool
allocateStrings();
1224 UnicodeString
& _toPattern(UnicodeString
& result
,
1225 UBool escapeUnprintable
) const;
1227 UnicodeString
& _generatePattern(UnicodeString
& result
,
1228 UBool escapeUnprintable
) const;
1230 static void _appendToPat(UnicodeString
& buf
, const UnicodeString
& s
, UBool escapeUnprintable
);
1232 static void _appendToPat(UnicodeString
& buf
, UChar32 c
, UBool escapeUnprintable
);
1234 //----------------------------------------------------------------
1235 // Implementation: Fundamental operators
1236 //----------------------------------------------------------------
1238 void exclusiveOr(const UChar32
* other
, int32_t otherLen
, int8_t polarity
);
1240 void add(const UChar32
* other
, int32_t otherLen
, int8_t polarity
);
1242 void retain(const UChar32
* other
, int32_t otherLen
, int8_t polarity
);
1245 * Return true if the given position, in the given pattern, appears
1246 * to be the start of a property set pattern [:foo:], \\p{foo}, or
1247 * \\P{foo}, or \\N{name}.
1249 static UBool
resemblesPropertyPattern(const UnicodeString
& pattern
,
1252 static UBool
resemblesPropertyPattern(RuleCharacterIterator
& chars
,
1256 * Parse the given property pattern at the given parse position
1257 * and set this UnicodeSet to the result.
1259 * The original design document is out of date, but still useful.
1260 * Ignore the property and value names:
1261 * http://oss.software.ibm.com/cvs/icu/~checkout~/icuhtml/design/unicodeset_properties.html
1263 * Recognized syntax:
1265 * [:foo:] [:^foo:] - white space not allowed within "[:" or ":]"
1266 * \\p{foo} \\P{foo} - white space not allowed within "\\p" or "\\P"
1267 * \\N{name} - white space not allowed within "\\N"
1269 * Other than the above restrictions, white space is ignored. Case
1270 * is ignored except in "\\p" and "\\P" and "\\N". In 'name' leading
1271 * and trailing space is deleted, and internal runs of whitespace
1272 * are collapsed to a single space.
1274 * We support binary properties, enumerated properties, and the
1275 * following non-enumerated properties:
1281 * @param pattern the pattern string
1282 * @param ppos on entry, the position at which to begin parsing.
1283 * This should be one of the locations marked '^':
1285 * [:blah:] \\p{blah} \\P{blah} \\N{name}
1288 * On return, the position after the last character parsed, that is,
1289 * the locations marked '%'. If the parse fails, ppos is returned
1291 * @return a reference to this.
1293 UnicodeSet
& applyPropertyPattern(const UnicodeString
& pattern
,
1294 ParsePosition
& ppos
,
1297 void applyPropertyPattern(RuleCharacterIterator
& chars
,
1298 UnicodeString
& rebuiltPat
,
1302 * A filter that returns TRUE if the given code point should be
1303 * included in the UnicodeSet being constructed.
1305 typedef UBool (*Filter
)(UChar32 codePoint
, void* context
);
1308 * Given a filter, set this UnicodeSet to the code points
1309 * contained by that filter. The filter MUST be
1310 * property-conformant. That is, if it returns value v for one
1311 * code point, then it must return v for all affiliated code
1312 * points, as defined by the inclusions list. See
1314 * src is a UPropertySource value.
1316 void applyFilter(Filter filter
,
1319 UErrorCode
&status
);
1322 * Return a cached copy of the inclusions list for the property source.
1324 static const UnicodeSet
* getInclusions(int32_t src
, UErrorCode
&errorCode
);
1326 friend class UnicodeSetIterator
;
1328 //----------------------------------------------------------------
1329 // Implementation: closeOver
1330 //----------------------------------------------------------------
1332 void caseCloseOne(const UnicodeString
& folded
);
1334 void caseCloseOne(const CaseEquivClass
& c
);
1336 void caseCloseOne(UChar folded
);
1338 static const CaseEquivClass
* getCaseMapOf(const UnicodeString
& folded
);
1340 static const CaseEquivClass
* getCaseMapOf(UChar folded
);
1343 inline UBool
UnicodeSet::operator!=(const UnicodeSet
& o
) const {
1344 return !operator==(o
);
1347 inline UBool
UnicodeSet::containsSome(UChar32 start
, UChar32 end
) const {
1348 return !containsNone(start
, end
);
1351 inline UBool
UnicodeSet::containsSome(const UnicodeSet
& s
) const {
1352 return !containsNone(s
);
1355 inline UBool
UnicodeSet::containsSome(const UnicodeString
& s
) const {
1356 return !containsNone(s
);