-//##header\r
-/*\r
- *******************************************************************************\r
- * Copyright (C) 1996-2009, International Business Machines Corporation and *\r
- * others. All Rights Reserved. *\r
- *******************************************************************************\r
- */\r
-package com.ibm.icu.text;\r
-\r
-import java.text.*;\r
-import com.ibm.icu.lang.*;\r
-\r
-import java.io.IOException;\r
-\r
-import com.ibm.icu.impl.NormalizerImpl;\r
-import com.ibm.icu.impl.Utility;\r
-import com.ibm.icu.impl.UCharacterProperty;\r
-import com.ibm.icu.impl.UBiDiProps;\r
-import com.ibm.icu.impl.UCaseProps;\r
-import com.ibm.icu.impl.UPropertyAliases;\r
-import com.ibm.icu.impl.SortedSetRelation;\r
-import com.ibm.icu.impl.RuleCharacterIterator;\r
-\r
-import com.ibm.icu.util.Freezable;\r
-import com.ibm.icu.util.ULocale;\r
-import com.ibm.icu.util.VersionInfo;\r
-\r
-import com.ibm.icu.text.BreakIterator;\r
-\r
-import java.util.MissingResourceException;\r
-import java.util.TreeSet;\r
-import java.util.Iterator;\r
-import java.util.Collection;\r
-\r
-/**\r
- * A mutable set of Unicode characters and multicharacter strings. Objects of this class\r
- * represent <em>character classes</em> used in regular expressions.\r
- * A character specifies a subset of Unicode code points. Legal\r
- * code points are U+0000 to U+10FFFF, inclusive.\r
- *\r
- * <p>The UnicodeSet class is not designed to be subclassed.\r
- *\r
- * <p><code>UnicodeSet</code> supports two APIs. The first is the\r
- * <em>operand</em> API that allows the caller to modify the value of\r
- * a <code>UnicodeSet</code> object. It conforms to Java 2's\r
- * <code>java.util.Set</code> interface, although\r
- * <code>UnicodeSet</code> does not actually implement that\r
- * interface. All methods of <code>Set</code> are supported, with the\r
- * modification that they take a character range or single character\r
- * instead of an <code>Object</code>, and they take a\r
- * <code>UnicodeSet</code> instead of a <code>Collection</code>. The\r
- * operand API may be thought of in terms of boolean logic: a boolean\r
- * OR is implemented by <code>add</code>, a boolean AND is implemented\r
- * by <code>retain</code>, a boolean XOR is implemented by\r
- * <code>complement</code> taking an argument, and a boolean NOT is\r
- * implemented by <code>complement</code> with no argument. In terms\r
- * of traditional set theory function names, <code>add</code> is a\r
- * union, <code>retain</code> is an intersection, <code>remove</code>\r
- * is an asymmetric difference, and <code>complement</code> with no\r
- * argument is a set complement with respect to the superset range\r
- * <code>MIN_VALUE-MAX_VALUE</code>\r
- *\r
- * <p>The second API is the\r
- * <code>applyPattern()</code>/<code>toPattern()</code> API from the\r
- * <code>java.text.Format</code>-derived classes. Unlike the\r
- * methods that add characters, add categories, and control the logic\r
- * of the set, the method <code>applyPattern()</code> sets all\r
- * attributes of a <code>UnicodeSet</code> at once, based on a\r
- * string pattern.\r
- *\r
- * <p><b>Pattern syntax</b></p>\r
- *\r
- * Patterns are accepted by the constructors and the\r
- * <code>applyPattern()</code> methods and returned by the\r
- * <code>toPattern()</code> method. These patterns follow a syntax\r
- * similar to that employed by version 8 regular expression character\r
- * classes. Here are some simple examples:\r
- *\r
- * <blockquote>\r
- * <table>\r
- * <tr align="top">\r
- * <td nowrap valign="top" align="left"><code>[]</code></td>\r
- * <td valign="top">No characters</td>\r
- * </tr><tr align="top">\r
- * <td nowrap valign="top" align="left"><code>[a]</code></td>\r
- * <td valign="top">The character 'a'</td>\r
- * </tr><tr align="top">\r
- * <td nowrap valign="top" align="left"><code>[ae]</code></td>\r
- * <td valign="top">The characters 'a' and 'e'</td>\r
- * </tr>\r
- * <tr>\r
- * <td nowrap valign="top" align="left"><code>[a-e]</code></td>\r
- * <td valign="top">The characters 'a' through 'e' inclusive, in Unicode code\r
- * point order</td>\r
- * </tr>\r
- * <tr>\r
- * <td nowrap valign="top" align="left"><code>[\\u4E01]</code></td>\r
- * <td valign="top">The character U+4E01</td>\r
- * </tr>\r
- * <tr>\r
- * <td nowrap valign="top" align="left"><code>[a{ab}{ac}]</code></td>\r
- * <td valign="top">The character 'a' and the multicharacter strings "ab" and\r
- * "ac"</td>\r
- * </tr>\r
- * <tr>\r
- * <td nowrap valign="top" align="left"><code>[\p{Lu}]</code></td>\r
- * <td valign="top">All characters in the general category Uppercase Letter</td>\r
- * </tr>\r
- * </table>\r
- * </blockquote>\r
- *\r
- * Any character may be preceded by a backslash in order to remove any special\r
- * meaning. White space characters, as defined by UCharacterProperty.isRuleWhiteSpace(), are\r
- * ignored, unless they are escaped.\r
- *\r
- * <p>Property patterns specify a set of characters having a certain\r
- * property as defined by the Unicode standard. Both the POSIX-like\r
- * "[:Lu:]" and the Perl-like syntax "\p{Lu}" are recognized. For a\r
- * complete list of supported property patterns, see the User's Guide\r
- * for UnicodeSet at\r
- * <a href="http://www.icu-project.org/userguide/unicodeSet.html">\r
- * http://www.icu-project.org/userguide/unicodeSet.html</a>.\r
- * Actual determination of property data is defined by the underlying\r
- * Unicode database as implemented by UCharacter.\r
- *\r
- * <p>Patterns specify individual characters, ranges of characters, and\r
- * Unicode property sets. When elements are concatenated, they\r
- * specify their union. To complement a set, place a '^' immediately\r
- * after the opening '['. Property patterns are inverted by modifying\r
- * their delimiters; "[:^foo]" and "\P{foo}". In any other location,\r
- * '^' has no special meaning.\r
- *\r
- * <p>Ranges are indicated by placing two a '-' between two\r
- * characters, as in "a-z". This specifies the range of all\r
- * characters from the left to the right, in Unicode order. If the\r
- * left character is greater than or equal to the\r
- * right character it is a syntax error. If a '-' occurs as the first\r
- * character after the opening '[' or '[^', or if it occurs as the\r
- * last character before the closing ']', then it is taken as a\r
- * literal. Thus "[a\\-b]", "[-ab]", and "[ab-]" all indicate the same\r
- * set of three characters, 'a', 'b', and '-'.\r
- *\r
- * <p>Sets may be intersected using the '&' operator or the asymmetric\r
- * set difference may be taken using the '-' operator, for example,\r
- * "[[:L:]&[\\u0000-\\u0FFF]]" indicates the set of all Unicode letters\r
- * with values less than 4096. Operators ('&' and '|') have equal\r
- * precedence and bind left-to-right. Thus\r
- * "[[:L:]-[a-z]-[\\u0100-\\u01FF]]" is equivalent to\r
- * "[[[:L:]-[a-z]]-[\\u0100-\\u01FF]]". This only really matters for\r
- * difference; intersection is commutative.\r
- *\r
- * <table>\r
- * <tr valign=top><td nowrap><code>[a]</code><td>The set containing 'a'\r
- * <tr valign=top><td nowrap><code>[a-z]</code><td>The set containing 'a'\r
- * through 'z' and all letters in between, in Unicode order\r
- * <tr valign=top><td nowrap><code>[^a-z]</code><td>The set containing\r
- * all characters but 'a' through 'z',\r
- * that is, U+0000 through 'a'-1 and 'z'+1 through U+10FFFF\r
- * <tr valign=top><td nowrap><code>[[<em>pat1</em>][<em>pat2</em>]]</code>\r
- * <td>The union of sets specified by <em>pat1</em> and <em>pat2</em>\r
- * <tr valign=top><td nowrap><code>[[<em>pat1</em>]&[<em>pat2</em>]]</code>\r
- * <td>The intersection of sets specified by <em>pat1</em> and <em>pat2</em>\r
- * <tr valign=top><td nowrap><code>[[<em>pat1</em>]-[<em>pat2</em>]]</code>\r
- * <td>The asymmetric difference of sets specified by <em>pat1</em> and\r
- * <em>pat2</em>\r
- * <tr valign=top><td nowrap><code>[:Lu:] or \p{Lu}</code>\r
- * <td>The set of characters having the specified\r
- * Unicode property; in\r
- * this case, Unicode uppercase letters\r
- * <tr valign=top><td nowrap><code>[:^Lu:] or \P{Lu}</code>\r
- * <td>The set of characters <em>not</em> having the given\r
- * Unicode property\r
- * </table>\r
- *\r
- * <p><b>Warning</b>: you cannot add an empty string ("") to a UnicodeSet.</p>\r
- *\r
- * <p><b>Formal syntax</b></p>\r
- *\r
- * <blockquote>\r
- * <table>\r
- * <tr align="top">\r
- * <td nowrap valign="top" align="right"><code>pattern := </code></td>\r
- * <td valign="top"><code>('[' '^'? item* ']') |\r
- * property</code></td>\r
- * </tr>\r
- * <tr align="top">\r
- * <td nowrap valign="top" align="right"><code>item := </code></td>\r
- * <td valign="top"><code>char | (char '-' char) | pattern-expr<br>\r
- * </code></td>\r
- * </tr>\r
- * <tr align="top">\r
- * <td nowrap valign="top" align="right"><code>pattern-expr := </code></td>\r
- * <td valign="top"><code>pattern | pattern-expr pattern |\r
- * pattern-expr op pattern<br>\r
- * </code></td>\r
- * </tr>\r
- * <tr align="top">\r
- * <td nowrap valign="top" align="right"><code>op := </code></td>\r
- * <td valign="top"><code>'&' | '-'<br>\r
- * </code></td>\r
- * </tr>\r
- * <tr align="top">\r
- * <td nowrap valign="top" align="right"><code>special := </code></td>\r
- * <td valign="top"><code>'[' | ']' | '-'<br>\r
- * </code></td>\r
- * </tr>\r
- * <tr align="top">\r
- * <td nowrap valign="top" align="right"><code>char := </code></td>\r
- * <td valign="top"><em>any character that is not</em><code> special<br>\r
- * | ('\\' </code><em>any character</em><code>)<br>\r
- * | ('\u' hex hex hex hex)<br>\r
- * </code></td>\r
- * </tr>\r
- * <tr align="top">\r
- * <td nowrap valign="top" align="right"><code>hex := </code></td>\r
- * <td valign="top"><em>any character for which\r
- * </em><code>Character.digit(c, 16)</code><em>\r
- * returns a non-negative result</em></td>\r
- * </tr>\r
- * <tr>\r
- * <td nowrap valign="top" align="right"><code>property := </code></td>\r
- * <td valign="top"><em>a Unicode property set pattern</td>\r
- * </tr>\r
- * </table>\r
- * <br>\r
- * <table border="1">\r
- * <tr>\r
- * <td>Legend: <table>\r
- * <tr>\r
- * <td nowrap valign="top"><code>a := b</code></td>\r
- * <td width="20" valign="top"> </td>\r
- * <td valign="top"><code>a</code> may be replaced by <code>b</code> </td>\r
- * </tr>\r
- * <tr>\r
- * <td nowrap valign="top"><code>a?</code></td>\r
- * <td valign="top"></td>\r
- * <td valign="top">zero or one instance of <code>a</code><br>\r
- * </td>\r
- * </tr>\r
- * <tr>\r
- * <td nowrap valign="top"><code>a*</code></td>\r
- * <td valign="top"></td>\r
- * <td valign="top">one or more instances of <code>a</code><br>\r
- * </td>\r
- * </tr>\r
- * <tr>\r
- * <td nowrap valign="top"><code>a | b</code></td>\r
- * <td valign="top"></td>\r
- * <td valign="top">either <code>a</code> or <code>b</code><br>\r
- * </td>\r
- * </tr>\r
- * <tr>\r
- * <td nowrap valign="top"><code>'a'</code></td>\r
- * <td valign="top"></td>\r
- * <td valign="top">the literal string between the quotes </td>\r
- * </tr>\r
- * </table>\r
- * </td>\r
- * </tr>\r
- * </table>\r
- * </blockquote>\r
- * <p>To iterate over contents of UnicodeSet, use UnicodeSetIterator class.\r
- *\r
- * @author Alan Liu\r
- * @stable ICU 2.0\r
- * @see UnicodeSetIterator\r
- */\r
-public class UnicodeSet extends UnicodeFilter implements Freezable {\r
-\r
- private static final int LOW = 0x000000; // LOW <= all valid values. ZERO for codepoints\r
- private static final int HIGH = 0x110000; // HIGH > all valid values. 10000 for code units.\r
- // 110000 for codepoints\r
-\r
- /**\r
- * Minimum value that can be stored in a UnicodeSet.\r
- * @stable ICU 2.0\r
- */\r
- public static final int MIN_VALUE = LOW;\r
-\r
- /**\r
- * Maximum value that can be stored in a UnicodeSet.\r
- * @stable ICU 2.0\r
- */\r
- public static final int MAX_VALUE = HIGH - 1;\r
-\r
- private int len; // length used; list may be longer to minimize reallocs\r
- private int[] list; // MUST be terminated with HIGH\r
- private int[] rangeList; // internal buffer\r
- private int[] buffer; // internal buffer\r
-\r
- // NOTE: normally the field should be of type SortedSet; but that is missing a public clone!!\r
- // is not private so that UnicodeSetIterator can get access\r
- TreeSet strings = new TreeSet();\r
-\r
- /**\r
- * The pattern representation of this set. This may not be the\r
- * most economical pattern. It is the pattern supplied to\r
- * applyPattern(), with variables substituted and whitespace\r
- * removed. For sets constructed without applyPattern(), or\r
- * modified using the non-pattern API, this string will be null,\r
- * indicating that toPattern() must generate a pattern\r
- * representation from the inversion list.\r
- */\r
- private String pat = null;\r
-\r
- private static final int START_EXTRA = 16; // initial storage. Must be >= 0\r
- private static final int GROW_EXTRA = START_EXTRA; // extra amount for growth. Must be >= 0\r
-\r
- // Special property set IDs\r
- private static final String ANY_ID = "ANY"; // [\u0000-\U0010FFFF]\r
- private static final String ASCII_ID = "ASCII"; // [\u0000-\u007F]\r
- private static final String ASSIGNED = "Assigned"; // [:^Cn:]\r
-\r
- /**\r
- * A set of all characters _except_ the second through last characters of\r
- * certain ranges. These ranges are ranges of characters whose\r
- * properties are all exactly alike, e.g. CJK Ideographs from\r
- * U+4E00 to U+9FA5.\r
- */\r
- private static UnicodeSet INCLUSIONS[] = null;\r
-\r
- //----------------------------------------------------------------\r
- // Public API\r
- //----------------------------------------------------------------\r
-\r
- /**\r
- * Constructs an empty set.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet() {\r
- list = new int[1 + START_EXTRA];\r
- list[len++] = HIGH;\r
- }\r
-\r
- /**\r
- * Constructs a copy of an existing set.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet(UnicodeSet other) {\r
- set(other);\r
- }\r
-\r
- /**\r
- * Constructs a set containing the given range. If <code>end >\r
- * start</code> then an empty set is created.\r
- *\r
- * @param start first character, inclusive, of range\r
- * @param end last character, inclusive, of range\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet(int start, int end) {\r
- this();\r
- complement(start, end);\r
- }\r
-\r
- /**\r
- * Constructs a set from the given pattern. See the class description\r
- * for the syntax of the pattern language. Whitespace is ignored.\r
- * @param pattern a string specifying what characters are in the set\r
- * @exception java.lang.IllegalArgumentException if the pattern contains\r
- * a syntax error.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet(String pattern) {\r
- this();\r
- applyPattern(pattern, null, null, IGNORE_SPACE);\r
- }\r
-\r
- /**\r
- * Constructs a set from the given pattern. See the class description\r
- * for the syntax of the pattern language.\r
- * @param pattern a string specifying what characters are in the set\r
- * @param ignoreWhitespace if true, ignore characters for which\r
- * UCharacterProperty.isRuleWhiteSpace() returns true\r
- * @exception java.lang.IllegalArgumentException if the pattern contains\r
- * a syntax error.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet(String pattern, boolean ignoreWhitespace) {\r
- this();\r
- applyPattern(pattern, null, null, ignoreWhitespace ? IGNORE_SPACE : 0);\r
- }\r
-\r
- /**\r
- * Constructs a set from the given pattern. See the class description\r
- * for the syntax of the pattern language.\r
- * @param pattern a string specifying what characters are in the set\r
- * @param options a bitmask indicating which options to apply.\r
- * Valid options are IGNORE_SPACE and CASE.\r
- * @exception java.lang.IllegalArgumentException if the pattern contains\r
- * a syntax error.\r
- * @stable ICU 3.8\r
- */\r
- public UnicodeSet(String pattern, int options) {\r
- this();\r
- applyPattern(pattern, null, null, options);\r
- }\r
-\r
- /**\r
- * Constructs a set from the given pattern. See the class description\r
- * for the syntax of the pattern language.\r
- * @param pattern a string specifying what characters are in the set\r
- * @param pos on input, the position in pattern at which to start parsing.\r
- * On output, the position after the last character parsed.\r
- * @param symbols a symbol table mapping variables to char[] arrays\r
- * and chars to UnicodeSets\r
- * @exception java.lang.IllegalArgumentException if the pattern\r
- * contains a syntax error.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet(String pattern, ParsePosition pos, SymbolTable symbols) {\r
- this();\r
- applyPattern(pattern, pos, symbols, IGNORE_SPACE);\r
- }\r
-\r
- /**\r
- * Constructs a set from the given pattern. See the class description\r
- * for the syntax of the pattern language.\r
- * @param pattern a string specifying what characters are in the set\r
- * @param pos on input, the position in pattern at which to start parsing.\r
- * On output, the position after the last character parsed.\r
- * @param symbols a symbol table mapping variables to char[] arrays\r
- * and chars to UnicodeSets\r
- * @param options a bitmask indicating which options to apply.\r
- * Valid options are IGNORE_SPACE and CASE.\r
- * @exception java.lang.IllegalArgumentException if the pattern\r
- * contains a syntax error.\r
- * @stable ICU 3.2\r
- */\r
- public UnicodeSet(String pattern, ParsePosition pos, SymbolTable symbols, int options) {\r
- this();\r
- applyPattern(pattern, pos, symbols, options);\r
- }\r
-\r
-\r
- /**\r
- * Return a new set that is equivalent to this one.\r
- * @stable ICU 2.0\r
- */\r
- public Object clone() {\r
- UnicodeSet result = new UnicodeSet(this);\r
- result.frozen = this.frozen;\r
- return result;\r
- }\r
-\r
- /**\r
- * Make this object represent the range <code>start - end</code>.\r
- * If <code>end > start</code> then this object is set to an\r
- * an empty range.\r
- *\r
- * @param start first character in the set, inclusive\r
- * @param end last character in the set, inclusive\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet set(int start, int end) {\r
- checkFrozen();\r
- clear();\r
- complement(start, end);\r
- return this;\r
- }\r
-\r
- /**\r
- * Make this object represent the same set as <code>other</code>.\r
- * @param other a <code>UnicodeSet</code> whose value will be\r
- * copied to this object\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet set(UnicodeSet other) {\r
- checkFrozen();\r
- list = (int[]) other.list.clone();\r
- len = other.len;\r
- pat = other.pat;\r
- strings = (TreeSet)other.strings.clone();\r
- return this;\r
- }\r
-\r
- /**\r
- * Modifies this set to represent the set specified by the given pattern.\r
- * See the class description for the syntax of the pattern language.\r
- * Whitespace is ignored.\r
- * @param pattern a string specifying what characters are in the set\r
- * @exception java.lang.IllegalArgumentException if the pattern\r
- * contains a syntax error.\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet applyPattern(String pattern) {\r
- checkFrozen();\r
- return applyPattern(pattern, null, null, IGNORE_SPACE);\r
- }\r
-\r
- /**\r
- * Modifies this set to represent the set specified by the given pattern,\r
- * optionally ignoring whitespace.\r
- * See the class description for the syntax of the pattern language.\r
- * @param pattern a string specifying what characters are in the set\r
- * @param ignoreWhitespace if true then characters for which\r
- * UCharacterProperty.isRuleWhiteSpace() returns true are ignored\r
- * @exception java.lang.IllegalArgumentException if the pattern\r
- * contains a syntax error.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet applyPattern(String pattern, boolean ignoreWhitespace) {\r
- checkFrozen();\r
- return applyPattern(pattern, null, null, ignoreWhitespace ? IGNORE_SPACE : 0);\r
- }\r
-\r
- /**\r
- * Modifies this set to represent the set specified by the given pattern,\r
- * optionally ignoring whitespace.\r
- * See the class description for the syntax of the pattern language.\r
- * @param pattern a string specifying what characters are in the set\r
- * @param options a bitmask indicating which options to apply.\r
- * Valid options are IGNORE_SPACE and CASE.\r
- * @exception java.lang.IllegalArgumentException if the pattern\r
- * contains a syntax error.\r
- * @stable ICU 3.8\r
- */\r
- public UnicodeSet applyPattern(String pattern, int options) {\r
- checkFrozen();\r
- return applyPattern(pattern, null, null, options);\r
- }\r
-\r
- /**\r
- * Return true if the given position, in the given pattern, appears\r
- * to be the start of a UnicodeSet pattern.\r
- * @stable ICU 2.0\r
- */\r
- public static boolean resemblesPattern(String pattern, int pos) {\r
- return ((pos+1) < pattern.length() &&\r
- pattern.charAt(pos) == '[') ||\r
- resemblesPropertyPattern(pattern, pos);\r
- }\r
-\r
- /**\r
- * Append the <code>toPattern()</code> representation of a\r
- * string to the given <code>StringBuffer</code>.\r
- */\r
- private static void _appendToPat(StringBuffer buf, String s, boolean escapeUnprintable) {\r
- for (int i = 0; i < s.length(); i += UTF16.getCharCount(i)) {\r
- _appendToPat(buf, UTF16.charAt(s, i), escapeUnprintable);\r
- }\r
- }\r
-\r
- /**\r
- * Append the <code>toPattern()</code> representation of a\r
- * character to the given <code>StringBuffer</code>.\r
- */\r
- private static void _appendToPat(StringBuffer buf, int c, boolean escapeUnprintable) {\r
- if (escapeUnprintable && Utility.isUnprintable(c)) {\r
- // Use hex escape notation (<backslash>uxxxx or <backslash>Uxxxxxxxx) for anything\r
- // unprintable\r
- if (Utility.escapeUnprintable(buf, c)) {\r
- return;\r
- }\r
- }\r
- // Okay to let ':' pass through\r
- switch (c) {\r
- case '[': // SET_OPEN:\r
- case ']': // SET_CLOSE:\r
- case '-': // HYPHEN:\r
- case '^': // COMPLEMENT:\r
- case '&': // INTERSECTION:\r
- case '\\': //BACKSLASH:\r
- case '{':\r
- case '}':\r
- case '$':\r
- case ':':\r
- buf.append('\\');\r
- break;\r
- default:\r
- // Escape whitespace\r
- if (UCharacterProperty.isRuleWhiteSpace(c)) {\r
- buf.append('\\');\r
- }\r
- break;\r
- }\r
- UTF16.append(buf, c);\r
- }\r
-\r
- /**\r
- * Returns a string representation of this set. If the result of\r
- * calling this function is passed to a UnicodeSet constructor, it\r
- * will produce another set that is equal to this one.\r
- * @stable ICU 2.0\r
- */\r
- public String toPattern(boolean escapeUnprintable) {\r
- StringBuffer result = new StringBuffer();\r
- return _toPattern(result, escapeUnprintable).toString();\r
- }\r
-\r
- /**\r
- * Append a string representation of this set to result. This will be\r
- * a cleaned version of the string passed to applyPattern(), if there\r
- * is one. Otherwise it will be generated.\r
- */\r
- private StringBuffer _toPattern(StringBuffer result,\r
- boolean escapeUnprintable) {\r
- if (pat != null) {\r
- int i;\r
- int backslashCount = 0;\r
- for (i=0; i<pat.length(); ) {\r
- int c = UTF16.charAt(pat, i);\r
- i += UTF16.getCharCount(c);\r
- if (escapeUnprintable && Utility.isUnprintable(c)) {\r
- // If the unprintable character is preceded by an odd\r
- // number of backslashes, then it has been escaped.\r
- // Before unescaping it, we delete the final\r
- // backslash.\r
- if ((backslashCount % 2) == 1) {\r
- result.setLength(result.length() - 1);\r
- }\r
- Utility.escapeUnprintable(result, c);\r
- backslashCount = 0;\r
- } else {\r
- UTF16.append(result, c);\r
- if (c == '\\') {\r
- ++backslashCount;\r
- } else {\r
- backslashCount = 0;\r
- }\r
- }\r
- }\r
- return result;\r
- }\r
-\r
- return _generatePattern(result, escapeUnprintable, true);\r
- }\r
-\r
- /**\r
- * Generate and append a string representation of this set to result.\r
- * This does not use this.pat, the cleaned up copy of the string\r
- * passed to applyPattern().\r
- * @param result the buffer into which to generate the pattern\r
- * @param escapeUnprintable escape unprintable characters if true\r
- * @stable ICU 2.0\r
- */\r
- public StringBuffer _generatePattern(StringBuffer result, boolean escapeUnprintable) {\r
- return _generatePattern(result, escapeUnprintable, true);\r
- }\r
-\r
- /**\r
- * Generate and append a string representation of this set to result.\r
- * This does not use this.pat, the cleaned up copy of the string\r
- * passed to applyPattern().\r
- * @param includeStrings if false, doesn't include the strings.\r
- * @stable ICU 3.8\r
- */\r
- public StringBuffer _generatePattern(StringBuffer result,\r
- boolean escapeUnprintable, boolean includeStrings) {\r
- result.append('[');\r
-\r
-// // Check against the predefined categories. We implicitly build\r
-// // up ALL category sets the first time toPattern() is called.\r
-// for (int cat=0; cat<CATEGORY_COUNT; ++cat) {\r
-// if (this.equals(getCategorySet(cat))) {\r
-// result.append(':');\r
-// result.append(CATEGORY_NAMES.substring(cat*2, cat*2+2));\r
-// return result.append(":]");\r
-// }\r
-// }\r
-\r
- int count = getRangeCount();\r
-\r
- // If the set contains at least 2 intervals and includes both\r
- // MIN_VALUE and MAX_VALUE, then the inverse representation will\r
- // be more economical.\r
- if (count > 1 &&\r
- getRangeStart(0) == MIN_VALUE &&\r
- getRangeEnd(count-1) == MAX_VALUE) {\r
-\r
- // Emit the inverse\r
- result.append('^');\r
-\r
- for (int i = 1; i < count; ++i) {\r
- int start = getRangeEnd(i-1)+1;\r
- int end = getRangeStart(i)-1;\r
- _appendToPat(result, start, escapeUnprintable);\r
- if (start != end) {\r
- if ((start+1) != end) {\r
- result.append('-');\r
- }\r
- _appendToPat(result, end, escapeUnprintable);\r
- }\r
- }\r
- }\r
-\r
- // Default; emit the ranges as pairs\r
- else {\r
- for (int i = 0; i < count; ++i) {\r
- int start = getRangeStart(i);\r
- int end = getRangeEnd(i);\r
- _appendToPat(result, start, escapeUnprintable);\r
- if (start != end) {\r
- if ((start+1) != end) {\r
- result.append('-');\r
- }\r
- _appendToPat(result, end, escapeUnprintable);\r
- }\r
- }\r
- }\r
-\r
- if (includeStrings && strings.size() > 0) {\r
- Iterator it = strings.iterator();\r
- while (it.hasNext()) {\r
- result.append('{');\r
- _appendToPat(result, (String) it.next(), escapeUnprintable);\r
- result.append('}');\r
- }\r
- }\r
- return result.append(']');\r
- }\r
-\r
- /**\r
- * Returns the number of elements in this set (its cardinality)\r
- * Note than the elements of a set may include both individual\r
- * codepoints and strings.\r
- *\r
- * @return the number of elements in this set (its cardinality).\r
- * @stable ICU 2.0\r
- */\r
- public int size() {\r
- int n = 0;\r
- int count = getRangeCount();\r
- for (int i = 0; i < count; ++i) {\r
- n += getRangeEnd(i) - getRangeStart(i) + 1;\r
- }\r
- return n + strings.size();\r
- }\r
-\r
- /**\r
- * Returns <tt>true</tt> if this set contains no elements.\r
- *\r
- * @return <tt>true</tt> if this set contains no elements.\r
- * @stable ICU 2.0\r
- */\r
- public boolean isEmpty() {\r
- return len == 1 && strings.size() == 0;\r
- }\r
-\r
- /**\r
- * Implementation of UnicodeMatcher API. Returns <tt>true</tt> if\r
- * this set contains any character whose low byte is the given\r
- * value. This is used by <tt>RuleBasedTransliterator</tt> for\r
- * indexing.\r
- * @stable ICU 2.0\r
- */\r
- public boolean matchesIndexValue(int v) {\r
- /* The index value v, in the range [0,255], is contained in this set if\r
- * it is contained in any pair of this set. Pairs either have the high\r
- * bytes equal, or unequal. If the high bytes are equal, then we have\r
- * aaxx..aayy, where aa is the high byte. Then v is contained if xx <=\r
- * v <= yy. If the high bytes are unequal we have aaxx..bbyy, bb>aa.\r
- * Then v is contained if xx <= v || v <= yy. (This is identical to the\r
- * time zone month containment logic.)\r
- */\r
- for (int i=0; i<getRangeCount(); ++i) {\r
- int low = getRangeStart(i);\r
- int high = getRangeEnd(i);\r
- if ((low & ~0xFF) == (high & ~0xFF)) {\r
- if ((low & 0xFF) <= v && v <= (high & 0xFF)) {\r
- return true;\r
- }\r
- } else if ((low & 0xFF) <= v || v <= (high & 0xFF)) {\r
- return true;\r
- }\r
- }\r
- if (strings.size() != 0) {\r
- Iterator it = strings.iterator();\r
- while (it.hasNext()) {\r
- String s = (String) it.next();\r
- //if (s.length() == 0) {\r
- // // Empty strings match everything\r
- // return true;\r
- //}\r
- // assert(s.length() != 0); // We enforce this elsewhere\r
- int c = UTF16.charAt(s, 0);\r
- if ((c & 0xFF) == v) {\r
- return true;\r
- }\r
- }\r
- }\r
- return false;\r
- }\r
-\r
- /**\r
- * Implementation of UnicodeMatcher.matches(). Always matches the\r
- * longest possible multichar string.\r
- * @stable ICU 2.0\r
- */\r
- public int matches(Replaceable text,\r
- int[] offset,\r
- int limit,\r
- boolean incremental) {\r
-\r
- if (offset[0] == limit) {\r
- // Strings, if any, have length != 0, so we don't worry\r
- // about them here. If we ever allow zero-length strings\r
- // we much check for them here.\r
- if (contains(UnicodeMatcher.ETHER)) {\r
- return incremental ? U_PARTIAL_MATCH : U_MATCH;\r
- } else {\r
- return U_MISMATCH;\r
- }\r
- } else {\r
- if (strings.size() != 0) { // try strings first\r
-\r
- // might separate forward and backward loops later\r
- // for now they are combined\r
-\r
- // TODO Improve efficiency of this, at least in the forward\r
- // direction, if not in both. In the forward direction we\r
- // can assume the strings are sorted.\r
-\r
- Iterator it = strings.iterator();\r
- boolean forward = offset[0] < limit;\r
-\r
- // firstChar is the leftmost char to match in the\r
- // forward direction or the rightmost char to match in\r
- // the reverse direction.\r
- char firstChar = text.charAt(offset[0]);\r
-\r
- // If there are multiple strings that can match we\r
- // return the longest match.\r
- int highWaterLength = 0;\r
-\r
- while (it.hasNext()) {\r
- String trial = (String) it.next();\r
-\r
- //if (trial.length() == 0) {\r
- // return U_MATCH; // null-string always matches\r
- //}\r
- // assert(trial.length() != 0); // We ensure this elsewhere\r
-\r
- char c = trial.charAt(forward ? 0 : trial.length() - 1);\r
-\r
- // Strings are sorted, so we can optimize in the\r
- // forward direction.\r
- if (forward && c > firstChar) break;\r
- if (c != firstChar) continue;\r
-\r
- int length = matchRest(text, offset[0], limit, trial);\r
-\r
- if (incremental) {\r
- int maxLen = forward ? limit-offset[0] : offset[0]-limit;\r
- if (length == maxLen) {\r
- // We have successfully matched but only up to limit.\r
- return U_PARTIAL_MATCH;\r
- }\r
- }\r
-\r
- if (length == trial.length()) {\r
- // We have successfully matched the whole string.\r
- if (length > highWaterLength) {\r
- highWaterLength = length;\r
- }\r
- // In the forward direction we know strings\r
- // are sorted so we can bail early.\r
- if (forward && length < highWaterLength) {\r
- break;\r
- }\r
- continue;\r
- }\r
- }\r
-\r
- // We've checked all strings without a partial match.\r
- // If we have full matches, return the longest one.\r
- if (highWaterLength != 0) {\r
- offset[0] += forward ? highWaterLength : -highWaterLength;\r
- return U_MATCH;\r
- }\r
- }\r
- return super.matches(text, offset, limit, incremental);\r
- }\r
- }\r
-\r
- /**\r
- * Returns the longest match for s in text at the given position.\r
- * If limit > start then match forward from start+1 to limit\r
- * matching all characters except s.charAt(0). If limit < start,\r
- * go backward starting from start-1 matching all characters\r
- * except s.charAt(s.length()-1). This method assumes that the\r
- * first character, text.charAt(start), matches s, so it does not\r
- * check it.\r
- * @param text the text to match\r
- * @param start the first character to match. In the forward\r
- * direction, text.charAt(start) is matched against s.charAt(0).\r
- * In the reverse direction, it is matched against\r
- * s.charAt(s.length()-1).\r
- * @param limit the limit offset for matching, either last+1 in\r
- * the forward direction, or last-1 in the reverse direction,\r
- * where last is the index of the last character to match.\r
- * @return If part of s matches up to the limit, return |limit -\r
- * start|. If all of s matches before reaching the limit, return\r
- * s.length(). If there is a mismatch between s and text, return\r
- * 0\r
- */\r
- private static int matchRest (Replaceable text, int start, int limit, String s) {\r
- int maxLen;\r
- int slen = s.length();\r
- if (start < limit) {\r
- maxLen = limit - start;\r
- if (maxLen > slen) maxLen = slen;\r
- for (int i = 1; i < maxLen; ++i) {\r
- if (text.charAt(start + i) != s.charAt(i)) return 0;\r
- }\r
- } else {\r
- maxLen = start - limit;\r
- if (maxLen > slen) maxLen = slen;\r
- --slen; // <=> slen = s.length() - 1;\r
- for (int i = 1; i < maxLen; ++i) {\r
- if (text.charAt(start - i) != s.charAt(slen - i)) return 0;\r
- }\r
- }\r
- return maxLen;\r
- }\r
-\r
-//#if defined(FOUNDATION10) || defined(J2SE13)\r
-//#else\r
- /**\r
- * Tests whether the text matches at the offset. If so, returns the end of the longest substring that it matches. If not, returns -1. \r
- * @internal\r
- * @deprecated This API is ICU internal only.\r
- */\r
- public int matchesAt(CharSequence text, int offset) {\r
- int lastLen = -1;\r
- strings:\r
- if (strings.size() != 0) {\r
- char firstChar = text.charAt(offset);\r
- String trial = null;\r
- // find the first string starting with firstChar\r
- Iterator it = strings.iterator();\r
- while (it.hasNext()) {\r
- trial = (String) it.next();\r
- char firstStringChar = trial.charAt(0);\r
- if (firstStringChar < firstChar) continue;\r
- if (firstStringChar > firstChar) break strings;\r
- }\r
- // now keep checking string until we get the longest one\r
- for (;;) {\r
- int tempLen = matchesAt(text, offset, trial);\r
- if (lastLen > tempLen) break strings;\r
- lastLen = tempLen;\r
- if (!it.hasNext()) break;\r
- trial = (String) it.next();\r
- }\r
- }\r
- if (lastLen < 2) {\r
- int cp = UTF16.charAt(text, offset);\r
- if (contains(cp)) {\r
- lastLen = UTF16.getCharCount(cp);\r
- }\r
- }\r
- return offset+lastLen;\r
- }\r
-\r
- /**\r
- * Does one string contain another, starting at a specific offset?\r
- * @param text\r
- * @param offset\r
- * @param other\r
- * @return\r
- */\r
- // Note: This method was moved from CollectionUtilities\r
- private static int matchesAt(CharSequence text, int offset, CharSequence other) {\r
- int len = other.length();\r
- int i = 0;\r
- int j = offset;\r
- for (; i < len; ++i, ++j) {\r
- char pc = other.charAt(i);\r
- char tc = text.charAt(j);\r
- if (pc != tc) return -1;\r
- }\r
- return i;\r
- }\r
-//#endif\r
-\r
- /**\r
- * Implementation of UnicodeMatcher API. Union the set of all\r
- * characters that may be matched by this object into the given\r
- * set.\r
- * @param toUnionTo the set into which to union the source characters\r
- * @stable ICU 2.2\r
- */\r
- public void addMatchSetTo(UnicodeSet toUnionTo) {\r
- toUnionTo.addAll(this);\r
- }\r
-\r
- /**\r
- * Returns the index of the given character within this set, where\r
- * the set is ordered by ascending code point. If the character\r
- * is not in this set, return -1. The inverse of this method is\r
- * <code>charAt()</code>.\r
- * @return an index from 0..size()-1, or -1\r
- * @stable ICU 2.0\r
- */\r
- public int indexOf(int c) {\r
- if (c < MIN_VALUE || c > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(c, 6));\r
- }\r
- int i = 0;\r
- int n = 0;\r
- for (;;) {\r
- int start = list[i++];\r
- if (c < start) {\r
- return -1;\r
- }\r
- int limit = list[i++];\r
- if (c < limit) {\r
- return n + c - start;\r
- }\r
- n += limit - start;\r
- }\r
- }\r
-\r
- /**\r
- * Returns the character at the given index within this set, where\r
- * the set is ordered by ascending code point. If the index is\r
- * out of range, return -1. The inverse of this method is\r
- * <code>indexOf()</code>.\r
- * @param index an index from 0..size()-1\r
- * @return the character at the given index, or -1.\r
- * @stable ICU 2.0\r
- */\r
- public int charAt(int index) {\r
- if (index >= 0) {\r
- // len2 is the largest even integer <= len, that is, it is len\r
- // for even values and len-1 for odd values. With odd values\r
- // the last entry is UNICODESET_HIGH.\r
- int len2 = len & ~1;\r
- for (int i=0; i < len2;) {\r
- int start = list[i++];\r
- int count = list[i++] - start;\r
- if (index < count) {\r
- return start + index;\r
- }\r
- index -= count;\r
- }\r
- }\r
- return -1;\r
- }\r
-\r
- /**\r
- * Adds the specified range to this set if it is not already\r
- * present. If this set already contains the specified range,\r
- * the call leaves this set unchanged. If <code>end > start</code>\r
- * then an empty range is added, leaving the set unchanged.\r
- *\r
- * @param start first character, inclusive, of range to be added\r
- * to this set.\r
- * @param end last character, inclusive, of range to be added\r
- * to this set.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet add(int start, int end) {\r
- checkFrozen();\r
- return add_unchecked(start, end);\r
- }\r
- \r
- // for internal use, after checkFrozen has been called\r
- private UnicodeSet add_unchecked(int start, int end) {\r
- if (start < MIN_VALUE || start > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
- }\r
- if (end < MIN_VALUE || end > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
- }\r
- if (start < end) {\r
- add(range(start, end), 2, 0);\r
- } else if (start == end) {\r
- add(start);\r
- }\r
- return this;\r
- }\r
-\r
-// /**\r
-// * Format out the inversion list as a string, for debugging. Uncomment when\r
-// * needed.\r
-// */\r
-// public final String dump() {\r
-// StringBuffer buf = new StringBuffer("[");\r
-// for (int i=0; i<len; ++i) {\r
-// if (i != 0) buf.append(", ");\r
-// int c = list[i];\r
-// //if (c <= 0x7F && c != '\n' && c != '\r' && c != '\t' && c != ' ') {\r
-// // buf.append((char) c);\r
-// //} else {\r
-// buf.append("U+").append(Utility.hex(c, (c<0x10000)?4:6));\r
-// //}\r
-// }\r
-// buf.append("]");\r
-// return buf.toString();\r
-// }\r
-\r
- /**\r
- * Adds the specified character to this set if it is not already\r
- * present. If this set already contains the specified character,\r
- * the call leaves this set unchanged.\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet add(int c) {\r
- checkFrozen();\r
- return add_unchecked(c);\r
- }\r
- \r
- // for internal use only, after checkFrozen has been called\r
- private final UnicodeSet add_unchecked(int c) {\r
- if (c < MIN_VALUE || c > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(c, 6));\r
- }\r
-\r
- // find smallest i such that c < list[i]\r
- // if odd, then it is IN the set\r
- // if even, then it is OUT of the set\r
- int i = findCodePoint(c);\r
-\r
- // already in set?\r
- if ((i & 1) != 0) return this;\r
-\r
- // HIGH is 0x110000\r
- // assert(list[len-1] == HIGH);\r
-\r
- // empty = [HIGH]\r
- // [start_0, limit_0, start_1, limit_1, HIGH]\r
-\r
- // [..., start_k-1, limit_k-1, start_k, limit_k, ..., HIGH]\r
- // ^\r
- // list[i]\r
-\r
- // i == 0 means c is before the first range\r
-\r
- if (c == list[i]-1) {\r
- // c is before start of next range\r
- list[i] = c;\r
- // if we touched the HIGH mark, then add a new one\r
- if (c == MAX_VALUE) {\r
- ensureCapacity(len+1);\r
- list[len++] = HIGH;\r
- }\r
- if (i > 0 && c == list[i-1]) {\r
- // collapse adjacent ranges\r
-\r
- // [..., start_k-1, c, c, limit_k, ..., HIGH]\r
- // ^\r
- // list[i]\r
- System.arraycopy(list, i+1, list, i-1, len-i-1);\r
- len -= 2;\r
- }\r
- }\r
-\r
- else if (i > 0 && c == list[i-1]) {\r
- // c is after end of prior range\r
- list[i-1]++;\r
- // no need to chcek for collapse here\r
- }\r
-\r
- else {\r
- // At this point we know the new char is not adjacent to\r
- // any existing ranges, and it is not 10FFFF.\r
-\r
-\r
- // [..., start_k-1, limit_k-1, start_k, limit_k, ..., HIGH]\r
- // ^\r
- // list[i]\r
-\r
- // [..., start_k-1, limit_k-1, c, c+1, start_k, limit_k, ..., HIGH]\r
- // ^\r
- // list[i]\r
-\r
- // Don't use ensureCapacity() to save on copying.\r
- // NOTE: This has no measurable impact on performance,\r
- // but it might help in some usage patterns.\r
- if (len+2 > list.length) {\r
- int[] temp = new int[len + 2 + GROW_EXTRA];\r
- if (i != 0) System.arraycopy(list, 0, temp, 0, i);\r
- System.arraycopy(list, i, temp, i+2, len-i);\r
- list = temp;\r
- } else {\r
- System.arraycopy(list, i, list, i+2, len-i);\r
- }\r
-\r
- list[i] = c;\r
- list[i+1] = c+1;\r
- len += 2;\r
- }\r
-\r
- pat = null;\r
- return this;\r
- }\r
-\r
- /**\r
- * Adds the specified multicharacter to this set if it is not already\r
- * present. If this set already contains the multicharacter,\r
- * the call leaves this set unchanged.\r
- * Thus "ch" => {"ch"}\r
- * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>\r
- * @param s the source string\r
- * @return this object, for chaining\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet add(String s) {\r
- checkFrozen();\r
- int cp = getSingleCP(s);\r
- if (cp < 0) {\r
- strings.add(s);\r
- pat = null;\r
- } else {\r
- add_unchecked(cp, cp);\r
- }\r
- return this;\r
- }\r
- \r
- /**\r
- * @return a code point IF the string consists of a single one.\r
- * otherwise returns -1.\r
- * @param string to test\r
- */\r
- private static int getSingleCP(String s) {\r
- if (s.length() < 1) {\r
- throw new IllegalArgumentException("Can't use zero-length strings in UnicodeSet");\r
- }\r
- if (s.length() > 2) return -1;\r
- if (s.length() == 1) return s.charAt(0);\r
-\r
- // at this point, len = 2\r
- int cp = UTF16.charAt(s, 0);\r
- if (cp > 0xFFFF) { // is surrogate pair\r
- return cp;\r
- }\r
- return -1;\r
- }\r
-\r
- /**\r
- * Adds each of the characters in this string to the set. Thus "ch" => {"c", "h"}\r
- * If this set already any particular character, it has no effect on that character.\r
- * @param s the source string\r
- * @return this object, for chaining\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet addAll(String s) {\r
- checkFrozen();\r
- int cp;\r
- for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {\r
- cp = UTF16.charAt(s, i);\r
- add_unchecked(cp, cp);\r
- }\r
- return this;\r
- }\r
-\r
- /**\r
- * Retains EACH of the characters in this string. Note: "ch" == {"c", "h"}\r
- * If this set already any particular character, it has no effect on that character.\r
- * @param s the source string\r
- * @return this object, for chaining\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet retainAll(String s) {\r
- return retainAll(fromAll(s));\r
- }\r
-\r
- /**\r
- * Complement EACH of the characters in this string. Note: "ch" == {"c", "h"}\r
- * If this set already any particular character, it has no effect on that character.\r
- * @param s the source string\r
- * @return this object, for chaining\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet complementAll(String s) {\r
- return complementAll(fromAll(s));\r
- }\r
-\r
- /**\r
- * Remove EACH of the characters in this string. Note: "ch" == {"c", "h"}\r
- * If this set already any particular character, it has no effect on that character.\r
- * @param s the source string\r
- * @return this object, for chaining\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet removeAll(String s) {\r
- return removeAll(fromAll(s));\r
- }\r
-\r
- /**\r
- * Remove all strings from this UnicodeSet\r
- * @return this object, for chaining\r
- * @draft ICU 4.2\r
- * @provisional This API might change or be removed in a future release.\r
- */\r
- public final UnicodeSet removeAllStrings() {\r
- checkFrozen();\r
- if (strings.size() != 0) {\r
- strings.clear();\r
- pat = null;\r
- }\r
- return this;\r
- }\r
- \r
- /**\r
- * Makes a set from a multicharacter string. Thus "ch" => {"ch"}\r
- * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>\r
- * @param s the source string\r
- * @return a newly created set containing the given string\r
- * @stable ICU 2.0\r
- */\r
- public static UnicodeSet from(String s) {\r
- return new UnicodeSet().add(s);\r
- }\r
-\r
-\r
- /**\r
- * Makes a set from each of the characters in the string. Thus "ch" => {"c", "h"}\r
- * @param s the source string\r
- * @return a newly created set containing the given characters\r
- * @stable ICU 2.0\r
- */\r
- public static UnicodeSet fromAll(String s) {\r
- return new UnicodeSet().addAll(s);\r
- }\r
-\r
-\r
- /**\r
- * Retain only the elements in this set that are contained in the\r
- * specified range. If <code>end > start</code> then an empty range is\r
- * retained, leaving the set empty.\r
- *\r
- * @param start first character, inclusive, of range to be retained\r
- * to this set.\r
- * @param end last character, inclusive, of range to be retained\r
- * to this set.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet retain(int start, int end) {\r
- checkFrozen();\r
- if (start < MIN_VALUE || start > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
- }\r
- if (end < MIN_VALUE || end > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
- }\r
- if (start <= end) {\r
- retain(range(start, end), 2, 0);\r
- } else {\r
- clear();\r
- }\r
- return this;\r
- }\r
-\r
- /**\r
- * Retain the specified character from this set if it is present.\r
- * Upon return this set will be empty if it did not contain c, or\r
- * will only contain c if it did contain c.\r
- * @param c the character to be retained\r
- * @return this object, for chaining\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet retain(int c) {\r
- return retain(c, c);\r
- }\r
-\r
- /**\r
- * Retain the specified string in this set if it is present.\r
- * Upon return this set will be empty if it did not contain s, or\r
- * will only contain s if it did contain s.\r
- * @param s the string to be retained\r
- * @return this object, for chaining\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet retain(String s) {\r
- int cp = getSingleCP(s);\r
- if (cp < 0) {\r
- boolean isIn = strings.contains(s);\r
- if (isIn && size() == 1) {\r
- return this;\r
- }\r
- clear();\r
- strings.add(s);\r
- pat = null;\r
- } else {\r
- retain(cp, cp);\r
- }\r
- return this;\r
- }\r
-\r
- /**\r
- * Removes the specified range from this set if it is present.\r
- * The set will not contain the specified range once the call\r
- * returns. If <code>end > start</code> then an empty range is\r
- * removed, leaving the set unchanged.\r
- *\r
- * @param start first character, inclusive, of range to be removed\r
- * from this set.\r
- * @param end last character, inclusive, of range to be removed\r
- * from this set.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet remove(int start, int end) {\r
- checkFrozen();\r
- if (start < MIN_VALUE || start > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
- }\r
- if (end < MIN_VALUE || end > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
- }\r
- if (start <= end) {\r
- retain(range(start, end), 2, 2);\r
- }\r
- return this;\r
- }\r
-\r
- /**\r
- * Removes the specified character from this set if it is present.\r
- * The set will not contain the specified character once the call\r
- * returns.\r
- * @param c the character to be removed\r
- * @return this object, for chaining\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet remove(int c) {\r
- return remove(c, c);\r
- }\r
-\r
- /**\r
- * Removes the specified string from this set if it is present.\r
- * The set will not contain the specified string once the call\r
- * returns.\r
- * @param s the string to be removed\r
- * @return this object, for chaining\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet remove(String s) {\r
- int cp = getSingleCP(s);\r
- if (cp < 0) {\r
- strings.remove(s);\r
- pat = null;\r
- } else {\r
- remove(cp, cp);\r
- }\r
- return this;\r
- }\r
-\r
- /**\r
- * Complements the specified range in this set. Any character in\r
- * the range will be removed if it is in this set, or will be\r
- * added if it is not in this set. If <code>end > start</code>\r
- * then an empty range is complemented, leaving the set unchanged.\r
- *\r
- * @param start first character, inclusive, of range to be removed\r
- * from this set.\r
- * @param end last character, inclusive, of range to be removed\r
- * from this set.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet complement(int start, int end) {\r
- checkFrozen();\r
- if (start < MIN_VALUE || start > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
- }\r
- if (end < MIN_VALUE || end > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
- }\r
- if (start <= end) {\r
- xor(range(start, end), 2, 0);\r
- }\r
- pat = null;\r
- return this;\r
- }\r
-\r
- /**\r
- * Complements the specified character in this set. The character\r
- * will be removed if it is in this set, or will be added if it is\r
- * not in this set.\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet complement(int c) {\r
- return complement(c, c);\r
- }\r
-\r
- /**\r
- * This is equivalent to\r
- * <code>complement(MIN_VALUE, MAX_VALUE)</code>.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet complement() {\r
- checkFrozen();\r
- if (list[0] == LOW) {\r
- System.arraycopy(list, 1, list, 0, len-1);\r
- --len;\r
- } else {\r
- ensureCapacity(len+1);\r
- System.arraycopy(list, 0, list, 1, len);\r
- list[0] = LOW;\r
- ++len;\r
- }\r
- pat = null;\r
- return this;\r
- }\r
-\r
- /**\r
- * Complement the specified string in this set.\r
- * The set will not contain the specified string once the call\r
- * returns.\r
- * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>\r
- * @param s the string to complement\r
- * @return this object, for chaining\r
- * @stable ICU 2.0\r
- */\r
- public final UnicodeSet complement(String s) {\r
- checkFrozen();\r
- int cp = getSingleCP(s);\r
- if (cp < 0) {\r
- if (strings.contains(s)) strings.remove(s);\r
- else strings.add(s);\r
- pat = null;\r
- } else {\r
- complement(cp, cp);\r
- }\r
- return this;\r
- }\r
-\r
- /**\r
- * Returns true if this set contains the given character.\r
- * @param c character to be checked for containment\r
- * @return true if the test condition is met\r
- * @stable ICU 2.0\r
- */\r
- public boolean contains(int c) {\r
- if (c < MIN_VALUE || c > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(c, 6));\r
- }\r
-\r
- /*\r
- // Set i to the index of the start item greater than ch\r
- // We know we will terminate without length test!\r
- int i = -1;\r
- while (true) {\r
- if (c < list[++i]) break;\r
- }\r
- */\r
-\r
- int i = findCodePoint(c);\r
-\r
- return ((i & 1) != 0); // return true if odd\r
- }\r
-\r
- /**\r
- * Returns the smallest value i such that c < list[i]. Caller\r
- * must ensure that c is a legal value or this method will enter\r
- * an infinite loop. This method performs a binary search.\r
- * @param c a character in the range MIN_VALUE..MAX_VALUE\r
- * inclusive\r
- * @return the smallest integer i in the range 0..len-1,\r
- * inclusive, such that c < list[i]\r
- */\r
- private final int findCodePoint(int c) {\r
- /* Examples:\r
- findCodePoint(c)\r
- set list[] c=0 1 3 4 7 8\r
- === ============== ===========\r
- [] [110000] 0 0 0 0 0 0\r
- [\u0000-\u0003] [0, 4, 110000] 1 1 1 2 2 2\r
- [\u0004-\u0007] [4, 8, 110000] 0 0 0 1 1 2\r
- [:all:] [0, 110000] 1 1 1 1 1 1\r
- */\r
-\r
- // Return the smallest i such that c < list[i]. Assume\r
- // list[len - 1] == HIGH and that c is legal (0..HIGH-1).\r
- if (c < list[0]) return 0;\r
- // High runner test. c is often after the last range, so an\r
- // initial check for this condition pays off.\r
- if (len >= 2 && c >= list[len-2]) return len-1;\r
- int lo = 0;\r
- int hi = len - 1;\r
- // invariant: c >= list[lo]\r
- // invariant: c < list[hi]\r
- for (;;) {\r
- int i = (lo + hi) >>> 1;\r
- if (i == lo) return hi;\r
- if (c < list[i]) {\r
- hi = i;\r
- } else {\r
- lo = i;\r
- }\r
- }\r
- }\r
-\r
-// //----------------------------------------------------------------\r
-// // Unrolled binary search\r
-// //----------------------------------------------------------------\r
-//\r
-// private int validLen = -1; // validated value of len\r
-// private int topOfLow;\r
-// private int topOfHigh;\r
-// private int power;\r
-// private int deltaStart;\r
-//\r
-// private void validate() {\r
-// if (len <= 1) {\r
-// throw new IllegalArgumentException("list.len==" + len + "; must be >1");\r
-// }\r
-//\r
-// // find greatest power of 2 less than or equal to len\r
-// for (power = exp2.length-1; power > 0 && exp2[power] > len; power--) {}\r
-//\r
-// // assert(exp2[power] <= len);\r
-//\r
-// // determine the starting points\r
-// topOfLow = exp2[power] - 1;\r
-// topOfHigh = len - 1;\r
-// deltaStart = exp2[power-1];\r
-// validLen = len;\r
-// }\r
-//\r
-// private static final int exp2[] = {\r
-// 0x1, 0x2, 0x4, 0x8,\r
-// 0x10, 0x20, 0x40, 0x80,\r
-// 0x100, 0x200, 0x400, 0x800,\r
-// 0x1000, 0x2000, 0x4000, 0x8000,\r
-// 0x10000, 0x20000, 0x40000, 0x80000,\r
-// 0x100000, 0x200000, 0x400000, 0x800000,\r
-// 0x1000000, 0x2000000, 0x4000000, 0x8000000,\r
-// 0x10000000, 0x20000000 // , 0x40000000 // no unsigned int in Java\r
-// };\r
-//\r
-// /**\r
-// * Unrolled lowest index GT.\r
-// */\r
-// private final int leastIndexGT(int searchValue) {\r
-//\r
-// if (len != validLen) {\r
-// if (len == 1) return 0;\r
-// validate();\r
-// }\r
-// int temp;\r
-//\r
-// // set up initial range to search. Each subrange is a power of two in length\r
-// int high = searchValue < list[topOfLow] ? topOfLow : topOfHigh;\r
-//\r
-// // Completely unrolled binary search, folhighing "Programming Pearls"\r
-// // Each case deliberately falls through to the next\r
-// // Logically, list[-1] < all_search_values && list[count] > all_search_values\r
-// // although the values -1 and count are never actually touched.\r
-//\r
-// // The bounds at each point are low & high,\r
-// // where low == high - delta*2\r
-// // so high - delta is the midpoint\r
-//\r
-// // The invariant AFTER each line is that list[low] < searchValue <= list[high]\r
-//\r
-// switch (power) {\r
-// //case 31: if (searchValue < list[temp = high-0x40000000]) high = temp; // no unsigned int in Java\r
-// case 30: if (searchValue < list[temp = high-0x20000000]) high = temp;\r
-// case 29: if (searchValue < list[temp = high-0x10000000]) high = temp;\r
-//\r
-// case 28: if (searchValue < list[temp = high- 0x8000000]) high = temp;\r
-// case 27: if (searchValue < list[temp = high- 0x4000000]) high = temp;\r
-// case 26: if (searchValue < list[temp = high- 0x2000000]) high = temp;\r
-// case 25: if (searchValue < list[temp = high- 0x1000000]) high = temp;\r
-//\r
-// case 24: if (searchValue < list[temp = high- 0x800000]) high = temp;\r
-// case 23: if (searchValue < list[temp = high- 0x400000]) high = temp;\r
-// case 22: if (searchValue < list[temp = high- 0x200000]) high = temp;\r
-// case 21: if (searchValue < list[temp = high- 0x100000]) high = temp;\r
-//\r
-// case 20: if (searchValue < list[temp = high- 0x80000]) high = temp;\r
-// case 19: if (searchValue < list[temp = high- 0x40000]) high = temp;\r
-// case 18: if (searchValue < list[temp = high- 0x20000]) high = temp;\r
-// case 17: if (searchValue < list[temp = high- 0x10000]) high = temp;\r
-//\r
-// case 16: if (searchValue < list[temp = high- 0x8000]) high = temp;\r
-// case 15: if (searchValue < list[temp = high- 0x4000]) high = temp;\r
-// case 14: if (searchValue < list[temp = high- 0x2000]) high = temp;\r
-// case 13: if (searchValue < list[temp = high- 0x1000]) high = temp;\r
-//\r
-// case 12: if (searchValue < list[temp = high- 0x800]) high = temp;\r
-// case 11: if (searchValue < list[temp = high- 0x400]) high = temp;\r
-// case 10: if (searchValue < list[temp = high- 0x200]) high = temp;\r
-// case 9: if (searchValue < list[temp = high- 0x100]) high = temp;\r
-//\r
-// case 8: if (searchValue < list[temp = high- 0x80]) high = temp;\r
-// case 7: if (searchValue < list[temp = high- 0x40]) high = temp;\r
-// case 6: if (searchValue < list[temp = high- 0x20]) high = temp;\r
-// case 5: if (searchValue < list[temp = high- 0x10]) high = temp;\r
-//\r
-// case 4: if (searchValue < list[temp = high- 0x8]) high = temp;\r
-// case 3: if (searchValue < list[temp = high- 0x4]) high = temp;\r
-// case 2: if (searchValue < list[temp = high- 0x2]) high = temp;\r
-// case 1: if (searchValue < list[temp = high- 0x1]) high = temp;\r
-// }\r
-//\r
-// return high;\r
-// }\r
-//\r
-// // For debugging only\r
-// public int len() {\r
-// return len;\r
-// }\r
-//\r
-// //----------------------------------------------------------------\r
-// //----------------------------------------------------------------\r
-\r
- /**\r
- * Returns true if this set contains every character\r
- * of the given range.\r
- * @param start first character, inclusive, of the range\r
- * @param end last character, inclusive, of the range\r
- * @return true if the test condition is met\r
- * @stable ICU 2.0\r
- */\r
- public boolean contains(int start, int end) {\r
- if (start < MIN_VALUE || start > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
- }\r
- if (end < MIN_VALUE || end > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
- }\r
- //int i = -1;\r
- //while (true) {\r
- // if (start < list[++i]) break;\r
- //}\r
- int i = findCodePoint(start);\r
- return ((i & 1) != 0 && end < list[i]);\r
- }\r
-\r
- /**\r
- * Returns <tt>true</tt> if this set contains the given\r
- * multicharacter string.\r
- * @param s string to be checked for containment\r
- * @return <tt>true</tt> if this set contains the specified string\r
- * @stable ICU 2.0\r
- */\r
- public final boolean contains(String s) {\r
-\r
- int cp = getSingleCP(s);\r
- if (cp < 0) {\r
- return strings.contains(s);\r
- } else {\r
- return contains(cp);\r
- }\r
- }\r
-\r
- /**\r
- * Returns true if this set contains all the characters and strings\r
- * of the given set.\r
- * @param b set to be checked for containment\r
- * @return true if the test condition is met\r
- * @stable ICU 2.0\r
- */\r
- public boolean containsAll(UnicodeSet b) {\r
- // The specified set is a subset if all of its pairs are contained in\r
- // this set. This implementation accesses the lists directly for speed.\r
- // TODO: this could be faster if size() were cached. But that would affect building speed\r
- // so it needs investigation.\r
- int[] listB = b.list;\r
- boolean needA = true;\r
- boolean needB = true;\r
- int aPtr = 0;\r
- int bPtr = 0;\r
- int aLen = len - 1;\r
- int bLen = b.len - 1;\r
- int startA = 0, startB = 0, limitA = 0, limitB = 0;\r
- while (true) {\r
- // double iterations are such a pain...\r
- if (needA) {\r
- if (aPtr >= aLen) {\r
- // ran out of A. If B is also exhausted, then break;\r
- if (needB && bPtr >= bLen) {\r
- break;\r
- }\r
- return false;\r
- }\r
- startA = list[aPtr++];\r
- limitA = list[aPtr++];\r
- }\r
- if (needB) {\r
- if (bPtr >= bLen) {\r
- // ran out of B. Since we got this far, we have an A and we are ok so far\r
- break;\r
- }\r
- startB = listB[bPtr++];\r
- limitB = listB[bPtr++];\r
- }\r
- // if B doesn't overlap and is greater than A, get new A\r
- if (startB >= limitA) {\r
- needA = true;\r
- needB = false;\r
- continue;\r
- }\r
- // if B is wholy contained in A, then get a new B\r
- if (startB >= startA && limitB <= limitA) {\r
- needA = false;\r
- needB = true;\r
- continue;\r
- }\r
- // all other combinations mean we fail\r
- return false;\r
- }\r
-\r
- if (!strings.containsAll(b.strings)) return false;\r
- return true;\r
- }\r
-\r
-// /**\r
-// * Returns true if this set contains all the characters and strings\r
-// * of the given set.\r
-// * @param c set to be checked for containment\r
-// * @return true if the test condition is met\r
-// * @stable ICU 2.0\r
-// */\r
-// public boolean containsAllOld(UnicodeSet c) {\r
-// // The specified set is a subset if all of its pairs are contained in\r
-// // this set. It's possible to code this more efficiently in terms of\r
-// // direct manipulation of the inversion lists if the need arises.\r
-// int n = c.getRangeCount();\r
-// for (int i=0; i<n; ++i) {\r
-// if (!contains(c.getRangeStart(i), c.getRangeEnd(i))) {\r
-// return false;\r
-// }\r
-// }\r
-// if (!strings.containsAll(c.strings)) return false;\r
-// return true;\r
-// }\r
-\r
- /**\r
- * Returns true if there is a partition of the string such that this set contains each of the partitioned strings.\r
- * For example, for the Unicode set [a{bc}{cd}]<br>\r
- * containsAll is true for each of: "a", "bc", ""cdbca"<br>\r
- * containsAll is false for each of: "acb", "bcda", "bcx"<br>\r
- * @param s string containing characters to be checked for containment\r
- * @return true if the test condition is met\r
- * @stable ICU 2.0\r
- */\r
- public boolean containsAll(String s) {\r
- int cp;\r
- for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {\r
- cp = UTF16.charAt(s, i);\r
- if (!contains(cp)) {\r
- if (strings.size() == 0) {\r
- return false;\r
- }\r
- return containsAll(s, 0);\r
- }\r
- }\r
- return true;\r
- }\r
-\r
- /**\r
- * Recursive routine called if we fail to find a match in containsAll, and there are strings\r
- * @param s source string\r
- * @param i point to match to the end on\r
- * @return true if ok\r
- */\r
- private boolean containsAll(String s, int i) {\r
- if (i >= s.length()) {\r
- return true;\r
- }\r
- int cp= UTF16.charAt(s, i);\r
- if (contains(cp) && containsAll(s, i+UTF16.getCharCount(cp))) {\r
- return true;\r
- }\r
- \r
- Iterator it = strings.iterator();\r
- while (it.hasNext()) {\r
- String setStr = (String)it.next();\r
- if (s.startsWith(setStr, i) && containsAll(s, i+setStr.length())) {\r
- return true;\r
- }\r
- }\r
- return false;\r
- \r
- }\r
-\r
- /**\r
- * Get the Regex equivalent for this UnicodeSet\r
- * @return regex pattern equivalent to this UnicodeSet\r
- * @internal\r
- * @deprecated This API is ICU internal only.\r
- */\r
- public String getRegexEquivalent() {\r
- if (strings.size() == 0) return toString();\r
- StringBuffer result = new StringBuffer("(?:");\r
- _generatePattern(result, true, false);\r
- Iterator it = strings.iterator();\r
- while (it.hasNext()) {\r
- result.append('|');\r
- _appendToPat(result, (String) it.next(), true);\r
- }\r
- return result.append(")").toString();\r
- }\r
-\r
- /**\r
- * Returns true if this set contains none of the characters\r
- * of the given range.\r
- * @param start first character, inclusive, of the range\r
- * @param end last character, inclusive, of the range\r
- * @return true if the test condition is met\r
- * @stable ICU 2.0\r
- */\r
- public boolean containsNone(int start, int end) {\r
- if (start < MIN_VALUE || start > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));\r
- }\r
- if (end < MIN_VALUE || end > MAX_VALUE) {\r
- throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));\r
- }\r
- int i = -1;\r
- while (true) {\r
- if (start < list[++i]) break;\r
- }\r
- return ((i & 1) == 0 && end < list[i]);\r
- }\r
-\r
- /**\r
- * Returns true if none of the characters or strings in this UnicodeSet appears in the string.\r
- * For example, for the Unicode set [a{bc}{cd}]<br>\r
- * containsNone is true for: "xy", "cb"<br>\r
- * containsNone is false for: "a", "bc", "bcd"<br>\r
- * @param b set to be checked for containment\r
- * @return true if the test condition is met\r
- * @stable ICU 2.0\r
- */\r
- public boolean containsNone(UnicodeSet b) {\r
- // The specified set is a subset if some of its pairs overlap with some of this set's pairs.\r
- // This implementation accesses the lists directly for speed.\r
- int[] listB = b.list;\r
- boolean needA = true;\r
- boolean needB = true;\r
- int aPtr = 0;\r
- int bPtr = 0;\r
- int aLen = len - 1;\r
- int bLen = b.len - 1;\r
- int startA = 0, startB = 0, limitA = 0, limitB = 0;\r
- while (true) {\r
- // double iterations are such a pain...\r
- if (needA) {\r
- if (aPtr >= aLen) {\r
- // ran out of A: break so we test strings\r
- break;\r
- }\r
- startA = list[aPtr++];\r
- limitA = list[aPtr++];\r
- }\r
- if (needB) {\r
- if (bPtr >= bLen) {\r
- // ran out of B: break so we test strings\r
- break;\r
- }\r
- startB = listB[bPtr++];\r
- limitB = listB[bPtr++];\r
- }\r
- // if B is higher than any part of A, get new A\r
- if (startB >= limitA) {\r
- needA = true;\r
- needB = false;\r
- continue;\r
- }\r
- // if A is higher than any part of B, get new B\r
- if (startA >= limitB) {\r
- needA = false;\r
- needB = true;\r
- continue;\r
- }\r
- // all other combinations mean we fail\r
- return false;\r
- }\r
-\r
- if (!SortedSetRelation.hasRelation(strings, SortedSetRelation.DISJOINT, b.strings)) return false;\r
- return true;\r
- }\r
-\r
-// /**\r
-// * Returns true if none of the characters or strings in this UnicodeSet appears in the string.\r
-// * For example, for the Unicode set [a{bc}{cd}]<br>\r
-// * containsNone is true for: "xy", "cb"<br>\r
-// * containsNone is false for: "a", "bc", "bcd"<br>\r
-// * @param c set to be checked for containment\r
-// * @return true if the test condition is met\r
-// * @stable ICU 2.0\r
-// */\r
-// public boolean containsNoneOld(UnicodeSet c) {\r
-// // The specified set is a subset if all of its pairs are contained in\r
-// // this set. It's possible to code this more efficiently in terms of\r
-// // direct manipulation of the inversion lists if the need arises.\r
-// int n = c.getRangeCount();\r
-// for (int i=0; i<n; ++i) {\r
-// if (!containsNone(c.getRangeStart(i), c.getRangeEnd(i))) {\r
-// return false;\r
-// }\r
-// }\r
-// if (!SortedSetRelation.hasRelation(strings, SortedSetRelation.DISJOINT, c.strings)) return false;\r
-// return true;\r
-// }\r
-\r
- /**\r
- * Returns true if this set contains none of the characters\r
- * of the given string.\r
- * @param s string containing characters to be checked for containment\r
- * @return true if the test condition is met\r
- * @stable ICU 2.0\r
- */\r
- public boolean containsNone(String s) {\r
- int cp;\r
- for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {\r
- cp = UTF16.charAt(s, i);\r
- if (contains(cp)) return false;\r
- }\r
- if (strings.size() == 0) return true;\r
- // do a last check to make sure no strings are in.\r
- for (Iterator it = strings.iterator(); it.hasNext();) {\r
- String item = (String)it.next();\r
- if (s.indexOf(item) >= 0) return false;\r
- }\r
- return true;\r
- }\r
-\r
- /**\r
- * Returns true if this set contains one or more of the characters\r
- * in the given range.\r
- * @param start first character, inclusive, of the range\r
- * @param end last character, inclusive, of the range\r
- * @return true if the condition is met\r
- * @stable ICU 2.0\r
- */\r
- public final boolean containsSome(int start, int end) {\r
- return !containsNone(start, end);\r
- }\r
-\r
- /**\r
- * Returns true if this set contains one or more of the characters\r
- * and strings of the given set.\r
- * @param s set to be checked for containment\r
- * @return true if the condition is met\r
- * @stable ICU 2.0\r
- */\r
- public final boolean containsSome(UnicodeSet s) {\r
- return !containsNone(s);\r
- }\r
-\r
- /**\r
- * Returns true if this set contains one or more of the characters\r
- * of the given string.\r
- * @param s string containing characters to be checked for containment\r
- * @return true if the condition is met\r
- * @stable ICU 2.0\r
- */\r
- public final boolean containsSome(String s) {\r
- return !containsNone(s);\r
- }\r
-\r
-\r
- /**\r
- * Adds all of the elements in the specified set to this set if\r
- * they're not already present. This operation effectively\r
- * modifies this set so that its value is the <i>union</i> of the two\r
- * sets. The behavior of this operation is unspecified if the specified\r
- * collection is modified while the operation is in progress.\r
- *\r
- * @param c set whose elements are to be added to this set.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet addAll(UnicodeSet c) {\r
- checkFrozen();\r
- add(c.list, c.len, 0);\r
- strings.addAll(c.strings);\r
- return this;\r
- }\r
-\r
- /**\r
- * Retains only the elements in this set that are contained in the\r
- * specified set. In other words, removes from this set all of\r
- * its elements that are not contained in the specified set. This\r
- * operation effectively modifies this set so that its value is\r
- * the <i>intersection</i> of the two sets.\r
- *\r
- * @param c set that defines which elements this set will retain.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet retainAll(UnicodeSet c) {\r
- checkFrozen();\r
- retain(c.list, c.len, 0);\r
- strings.retainAll(c.strings);\r
- return this;\r
- }\r
-\r
- /**\r
- * Removes from this set all of its elements that are contained in the\r
- * specified set. This operation effectively modifies this\r
- * set so that its value is the <i>asymmetric set difference</i> of\r
- * the two sets.\r
- *\r
- * @param c set that defines which elements will be removed from\r
- * this set.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet removeAll(UnicodeSet c) {\r
- checkFrozen();\r
- retain(c.list, c.len, 2);\r
- strings.removeAll(c.strings);\r
- return this;\r
- }\r
-\r
- /**\r
- * Complements in this set all elements contained in the specified\r
- * set. Any character in the other set will be removed if it is\r
- * in this set, or will be added if it is not in this set.\r
- *\r
- * @param c set that defines which elements will be complemented from\r
- * this set.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet complementAll(UnicodeSet c) {\r
- checkFrozen();\r
- xor(c.list, c.len, 0);\r
- SortedSetRelation.doOperation(strings, SortedSetRelation.COMPLEMENTALL, c.strings);\r
- return this;\r
- }\r
-\r
- /**\r
- * Removes all of the elements from this set. This set will be\r
- * empty after this call returns.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet clear() {\r
- checkFrozen();\r
- list[0] = HIGH;\r
- len = 1;\r
- pat = null;\r
- strings.clear();\r
- return this;\r
- }\r
-\r
- /**\r
- * Iteration method that returns the number of ranges contained in\r
- * this set.\r
- * @see #getRangeStart\r
- * @see #getRangeEnd\r
- * @stable ICU 2.0\r
- */\r
- public int getRangeCount() {\r
- return len/2;\r
- }\r
-\r
- /**\r
- * Iteration method that returns the first character in the\r
- * specified range of this set.\r
- * @exception ArrayIndexOutOfBoundsException if index is outside\r
- * the range <code>0..getRangeCount()-1</code>\r
- * @see #getRangeCount\r
- * @see #getRangeEnd\r
- * @stable ICU 2.0\r
- */\r
- public int getRangeStart(int index) {\r
- return list[index*2];\r
- }\r
-\r
- /**\r
- * Iteration method that returns the last character in the\r
- * specified range of this set.\r
- * @exception ArrayIndexOutOfBoundsException if index is outside\r
- * the range <code>0..getRangeCount()-1</code>\r
- * @see #getRangeStart\r
- * @see #getRangeEnd\r
- * @stable ICU 2.0\r
- */\r
- public int getRangeEnd(int index) {\r
- return (list[index*2 + 1] - 1);\r
- }\r
-\r
- /**\r
- * Reallocate this objects internal structures to take up the least\r
- * possible space, without changing this object's value.\r
- * @stable ICU 2.0\r
- */\r
- public UnicodeSet compact() {\r
- checkFrozen();\r
- if (len != list.length) {\r
- int[] temp = new int[len];\r
- System.arraycopy(list, 0, temp, 0, len);\r
- list = temp;\r
- }\r
- rangeList = null;\r
- buffer = null;\r
- return this;\r
- }\r
-\r
- /**\r
- * Compares the specified object with this set for equality. Returns\r
- * <tt>true</tt> if the specified object is also a set, the two sets\r
- * have the same size, and every member of the specified set is\r
- * contained in this set (or equivalently, every member of this set is\r
- * contained in the specified set).\r
- *\r
- * @param o Object to be compared for equality with this set.\r
- * @return <tt>true</tt> if the specified Object is equal to this set.\r
- * @stable ICU 2.0\r
- */\r
- public boolean equals(Object o) {\r
- try {\r
- UnicodeSet that = (UnicodeSet) o;\r
- if (len != that.len) return false;\r
- for (int i = 0; i < len; ++i) {\r
- if (list[i] != that.list[i]) return false;\r
- }\r
- if (!strings.equals(that.strings)) return false;\r
- } catch (Exception e) {\r
- return false;\r
- }\r
- return true;\r
- }\r
-\r
- /**\r
- * Returns the hash code value for this set.\r
- *\r
- * @return the hash code value for this set.\r
- * @see java.lang.Object#hashCode()\r
- * @stable ICU 2.0\r
- */\r
- public int hashCode() {\r
- int result = len;\r
- for (int i = 0; i < len; ++i) {\r
- result *= 1000003;\r
- result += list[i];\r
- }\r
- return result;\r
- }\r
-\r
- /**\r
- * Return a programmer-readable string representation of this object.\r
- * @stable ICU 2.0\r
- */\r
- public String toString() {\r
- return toPattern(true);\r
- }\r
-\r
- //----------------------------------------------------------------\r
- // Implementation: Pattern parsing\r
- //----------------------------------------------------------------\r
-\r
- /**\r
- * Parses the given pattern, starting at the given position. The character\r
- * at pattern.charAt(pos.getIndex()) must be '[', or the parse fails.\r
- * Parsing continues until the corresponding closing ']'. If a syntax error\r
- * is encountered between the opening and closing brace, the parse fails.\r
- * Upon return from a successful parse, the ParsePosition is updated to\r
- * point to the character following the closing ']', and an inversion\r
- * list for the parsed pattern is returned. This method\r
- * calls itself recursively to parse embedded subpatterns.\r
- *\r
- * @param pattern the string containing the pattern to be parsed. The\r
- * portion of the string from pos.getIndex(), which must be a '[', to the\r
- * corresponding closing ']', is parsed.\r
- * @param pos upon entry, the position at which to being parsing. The\r
- * character at pattern.charAt(pos.getIndex()) must be a '['. Upon return\r
- * from a successful parse, pos.getIndex() is either the character after the\r
- * closing ']' of the parsed pattern, or pattern.length() if the closing ']'\r
- * is the last character of the pattern string.\r
- * @return an inversion list for the parsed substring\r
- * of <code>pattern</code>\r
- * @exception java.lang.IllegalArgumentException if the parse fails.\r
- * @internal\r
- * @deprecated - for internal use only\r
- */\r
- public UnicodeSet applyPattern(String pattern,\r
- ParsePosition pos,\r
- SymbolTable symbols,\r
- int options) {\r
-\r
- // Need to build the pattern in a temporary string because\r
- // _applyPattern calls add() etc., which set pat to empty.\r
- boolean parsePositionWasNull = pos == null;\r
- if (parsePositionWasNull) {\r
- pos = new ParsePosition(0);\r
- }\r
-\r
- StringBuffer rebuiltPat = new StringBuffer();\r
- RuleCharacterIterator chars =\r
- new RuleCharacterIterator(pattern, symbols, pos);\r
- applyPattern(chars, symbols, rebuiltPat, options);\r
- if (chars.inVariable()) {\r
- syntaxError(chars, "Extra chars in variable value");\r
- }\r
- pat = rebuiltPat.toString();\r
- if (parsePositionWasNull) {\r
- int i = pos.getIndex();\r
-\r
- // Skip over trailing whitespace\r
- if ((options & IGNORE_SPACE) != 0) {\r
- i = Utility.skipWhitespace(pattern, i);\r
- }\r
-\r
- if (i != pattern.length()) {\r
- throw new IllegalArgumentException("Parse of \"" + pattern +\r
- "\" failed at " + i);\r
- }\r
- }\r
- return this;\r
- }\r
-\r
- /**\r
- * Parse the pattern from the given RuleCharacterIterator. The\r
- * iterator is advanced over the parsed pattern.\r
- * @param chars iterator over the pattern characters. Upon return\r
- * it will be advanced to the first character after the parsed\r
- * pattern, or the end of the iteration if all characters are\r
- * parsed.\r
- * @param symbols symbol table to use to parse and dereference\r
- * variables, or null if none.\r
- * @param rebuiltPat the pattern that was parsed, rebuilt or\r
- * copied from the input pattern, as appropriate.\r
- * @param options a bit mask of zero or more of the following:\r
- * IGNORE_SPACE, CASE.\r
- */\r
- void applyPattern(RuleCharacterIterator chars, SymbolTable symbols,\r
- StringBuffer rebuiltPat, int options) {\r
-\r
- // Syntax characters: [ ] ^ - & { }\r
-\r
- // Recognized special forms for chars, sets: c-c s-s s&s\r
-\r
- int opts = RuleCharacterIterator.PARSE_VARIABLES |\r
- RuleCharacterIterator.PARSE_ESCAPES;\r
- if ((options & IGNORE_SPACE) != 0) {\r
- opts |= RuleCharacterIterator.SKIP_WHITESPACE;\r
- }\r
-\r
- StringBuffer patBuf = new StringBuffer(), buf = null;\r
- boolean usePat = false;\r
- UnicodeSet scratch = null;\r
- Object backup = null;\r
-\r
- // mode: 0=before [, 1=between [...], 2=after ]\r
- // lastItem: 0=none, 1=char, 2=set\r
- int lastItem = 0, lastChar = 0, mode = 0;\r
- char op = 0;\r
-\r
- boolean invert = false;\r
-\r
- clear();\r
-\r
- while (mode != 2 && !chars.atEnd()) {\r
- if (false) {\r
- // Debugging assertion\r
- if (!((lastItem == 0 && op == 0) ||\r
- (lastItem == 1 && (op == 0 || op == '-')) ||\r
- (lastItem == 2 && (op == 0 || op == '-' || op == '&')))) {\r
- throw new IllegalArgumentException();\r
- }\r
- }\r
-\r
- int c = 0;\r
- boolean literal = false;\r
- UnicodeSet nested = null;\r
-\r
- // -------- Check for property pattern\r
-\r
- // setMode: 0=none, 1=unicodeset, 2=propertypat, 3=preparsed\r
- int setMode = 0;\r
- if (resemblesPropertyPattern(chars, opts)) {\r
- setMode = 2;\r
- }\r
-\r
- // -------- Parse '[' of opening delimiter OR nested set.\r
- // If there is a nested set, use `setMode' to define how\r
- // the set should be parsed. If the '[' is part of the\r
- // opening delimiter for this pattern, parse special\r
- // strings "[", "[^", "[-", and "[^-". Check for stand-in\r
- // characters representing a nested set in the symbol\r
- // table.\r
-\r
- else {\r
- // Prepare to backup if necessary\r
- backup = chars.getPos(backup);\r
- c = chars.next(opts);\r
- literal = chars.isEscaped();\r
-\r
- if (c == '[' && !literal) {\r
- if (mode == 1) {\r
- chars.setPos(backup); // backup\r
- setMode = 1;\r
- } else {\r
- // Handle opening '[' delimiter\r
- mode = 1;\r
- patBuf.append('[');\r
- backup = chars.getPos(backup); // prepare to backup\r
- c = chars.next(opts);\r
- literal = chars.isEscaped();\r
- if (c == '^' && !literal) {\r
- invert = true;\r
- patBuf.append('^');\r
- backup = chars.getPos(backup); // prepare to backup\r
- c = chars.next(opts);\r
- literal = chars.isEscaped();\r
- }\r
- // Fall through to handle special leading '-';\r
- // otherwise restart loop for nested [], \p{}, etc.\r
- if (c == '-') {\r
- literal = true;\r
- // Fall through to handle literal '-' below\r
- } else {\r
- chars.setPos(backup); // backup\r
- continue;\r
- }\r
- }\r
- } else if (symbols != null) {\r
- UnicodeMatcher m = symbols.lookupMatcher(c); // may be null\r
- if (m != null) {\r
- try {\r
- nested = (UnicodeSet) m;\r
- setMode = 3;\r
- } catch (ClassCastException e) {\r
- syntaxError(chars, "Syntax error");\r
- }\r
- }\r
- }\r
- }\r
-\r
- // -------- Handle a nested set. This either is inline in\r
- // the pattern or represented by a stand-in that has\r
- // previously been parsed and was looked up in the symbol\r
- // table.\r
-\r
- if (setMode != 0) {\r
- if (lastItem == 1) {\r
- if (op != 0) {\r
- syntaxError(chars, "Char expected after operator");\r
- }\r
- add_unchecked(lastChar, lastChar);\r
- _appendToPat(patBuf, lastChar, false);\r
- lastItem = op = 0;\r
- }\r
-\r
- if (op == '-' || op == '&') {\r
- patBuf.append(op);\r
- }\r
-\r
- if (nested == null) {\r
- if (scratch == null) scratch = new UnicodeSet();\r
- nested = scratch;\r
- }\r
- switch (setMode) {\r
- case 1:\r
- nested.applyPattern(chars, symbols, patBuf, options);\r
- break;\r
- case 2:\r
- chars.skipIgnored(opts);\r
- nested.applyPropertyPattern(chars, patBuf, symbols);\r
- break;\r
- case 3: // `nested' already parsed\r
- nested._toPattern(patBuf, false);\r
- break;\r
- }\r
-\r
- usePat = true;\r
-\r
- if (mode == 0) {\r
- // Entire pattern is a category; leave parse loop\r
- set(nested);\r
- mode = 2;\r
- break;\r
- }\r
-\r
- switch (op) {\r
- case '-':\r
- removeAll(nested);\r
- break;\r
- case '&':\r
- retainAll(nested);\r
- break;\r
- case 0:\r
- addAll(nested);\r
- break;\r
- }\r
-\r
- op = 0;\r
- lastItem = 2;\r
-\r
- continue;\r
- }\r
-\r
- if (mode == 0) {\r
- syntaxError(chars, "Missing '['");\r
- }\r
-\r
- // -------- Parse special (syntax) characters. If the\r
- // current character is not special, or if it is escaped,\r
- // then fall through and handle it below.\r
-\r
- if (!literal) {\r
- switch (c) {\r
- case ']':\r
- if (lastItem == 1) {\r
- add_unchecked(lastChar, lastChar);\r
- _appendToPat(patBuf, lastChar, false);\r
- }\r
- // Treat final trailing '-' as a literal\r
- if (op == '-') {\r
- add_unchecked(op, op);\r
- patBuf.append(op);\r
- } else if (op == '&') {\r
- syntaxError(chars, "Trailing '&'");\r
- }\r
- patBuf.append(']');\r
- mode = 2;\r
- continue;\r
- case '-':\r
- if (op == 0) {\r
- if (lastItem != 0) {\r
- op = (char) c;\r
- continue;\r
- } else {\r
- // Treat final trailing '-' as a literal\r
- add_unchecked(c, c);\r
- c = chars.next(opts);\r
- literal = chars.isEscaped();\r
- if (c == ']' && !literal) {\r
- patBuf.append("-]");\r
- mode = 2;\r
- continue;\r
- }\r
- }\r
- }\r
- syntaxError(chars, "'-' not after char or set");\r
- case '&':\r
- if (lastItem == 2 && op == 0) {\r
- op = (char) c;\r
- continue;\r
- }\r
- syntaxError(chars, "'&' not after set");\r
- case '^':\r
- syntaxError(chars, "'^' not after '['");\r
- case '{':\r
- if (op != 0) {\r
- syntaxError(chars, "Missing operand after operator");\r
- }\r
- if (lastItem == 1) {\r
- add_unchecked(lastChar, lastChar);\r
- _appendToPat(patBuf, lastChar, false);\r
- }\r
- lastItem = 0;\r
- if (buf == null) {\r
- buf = new StringBuffer();\r
- } else {\r
- buf.setLength(0);\r
- }\r
- boolean ok = false;\r
- while (!chars.atEnd()) {\r
- c = chars.next(opts);\r
- literal = chars.isEscaped();\r
- if (c == '}' && !literal) {\r
- ok = true;\r
- break;\r
- }\r
- UTF16.append(buf, c);\r
- }\r
- if (buf.length() < 1 || !ok) {\r
- syntaxError(chars, "Invalid multicharacter string");\r
- }\r
- // We have new string. Add it to set and continue;\r
- // we don't need to drop through to the further\r
- // processing\r
- add(buf.toString());\r
- patBuf.append('{');\r
- _appendToPat(patBuf, buf.toString(), false);\r
- patBuf.append('}');\r
- continue;\r
- case SymbolTable.SYMBOL_REF:\r
- // symbols nosymbols\r
- // [a-$] error error (ambiguous)\r
- // [a$] anchor anchor\r
- // [a-$x] var "x"* literal '$'\r
- // [a-$.] error literal '$'\r
- // *We won't get here in the case of var "x"\r
- backup = chars.getPos(backup);\r
- c = chars.next(opts);\r
- literal = chars.isEscaped();\r
- boolean anchor = (c == ']' && !literal);\r
- if (symbols == null && !anchor) {\r
- c = SymbolTable.SYMBOL_REF;\r
- chars.setPos(backup);\r
- break; // literal '$'\r
- }\r
- if (anchor && op == 0) {\r
- if (lastItem == 1) {\r
- add_unchecked(lastChar, lastChar);\r
- _appendToPat(patBuf, lastChar, false);\r
- }\r
- add_unchecked(UnicodeMatcher.ETHER);\r
- usePat = true;\r
- patBuf.append(SymbolTable.SYMBOL_REF).append(']');\r
- mode = 2;\r
- continue;\r
- }\r
- syntaxError(chars, "Unquoted '$'");\r
- default:\r
- break;\r
- }\r
- }\r
-\r
- // -------- Parse literal characters. This includes both\r
- // escaped chars ("\u4E01") and non-syntax characters\r
- // ("a").\r
-\r
- switch (lastItem) {\r
- case 0:\r
- lastItem = 1;\r
- lastChar = c;\r
- break;\r
- case 1:\r
- if (op == '-') {\r
- if (lastChar >= c) {\r
- // Don't allow redundant (a-a) or empty (b-a) ranges;\r
- // these are most likely typos.\r
- syntaxError(chars, "Invalid range");\r
- }\r
- add_unchecked(lastChar, c);\r
- _appendToPat(patBuf, lastChar, false);\r
- patBuf.append(op);\r
- _appendToPat(patBuf, c, false);\r
- lastItem = op = 0;\r
- } else {\r
- add_unchecked(lastChar, lastChar);\r
- _appendToPat(patBuf, lastChar, false);\r
- lastChar = c;\r
- }\r
- break;\r
- case 2:\r
- if (op != 0) {\r
- syntaxError(chars, "Set expected after operator");\r
- }\r
- lastChar = c;\r
- lastItem = 1;\r
- break;\r
- }\r
- }\r
-\r
- if (mode != 2) {\r
- syntaxError(chars, "Missing ']'");\r
- }\r
-\r
- chars.skipIgnored(opts);\r
-\r
- /**\r
- * Handle global flags (invert, case insensitivity). If this\r
- * pattern should be compiled case-insensitive, then we need\r
- * to close over case BEFORE COMPLEMENTING. This makes\r
- * patterns like /[^abc]/i work.\r
- */\r
- if ((options & CASE) != 0) {\r
- closeOver(CASE);\r
- }\r
- if (invert) {\r
- complement();\r
- }\r
-\r
- // Use the rebuilt pattern (pat) only if necessary. Prefer the\r
- // generated pattern.\r
- if (usePat) {\r
- rebuiltPat.append(patBuf.toString());\r
- } else {\r
- _generatePattern(rebuiltPat, false, true);\r
- }\r
- }\r
-\r
- private static void syntaxError(RuleCharacterIterator chars, String msg) {\r
- throw new IllegalArgumentException("Error: " + msg + " at \"" +\r
- Utility.escape(chars.toString()) +\r
- '"');\r
- }\r
-\r
- /**\r
- * Add the contents of the UnicodeSet (as strings) into a collection.\r
- * @param target collection to add into\r
- * @stable ICU 2.8\r
- */\r
- public void addAllTo(Collection target) {\r
- UnicodeSetIterator it = new UnicodeSetIterator(this);\r
- while (it.next()) {\r
- target.add(it.getString());\r
- }\r
- }\r
-\r
- /**\r
- * Add the contents of the collection (as strings) into this UnicodeSet.\r
- * @param source the collection to add\r
- * @stable ICU 2.8\r
- */\r
- public void addAll(Collection source) {\r
- checkFrozen();\r
- Iterator it = source.iterator();\r
- while (it.hasNext()) {\r
- add(it.next().toString());\r
- }\r
- }\r
-\r
- //----------------------------------------------------------------\r
- // Implementation: Utility methods\r
- //----------------------------------------------------------------\r
-\r
- private void ensureCapacity(int newLen) {\r
- if (newLen <= list.length) return;\r
- int[] temp = new int[newLen + GROW_EXTRA];\r
- System.arraycopy(list, 0, temp, 0, len);\r
- list = temp;\r
- }\r
-\r
- private void ensureBufferCapacity(int newLen) {\r
- if (buffer != null && newLen <= buffer.length) return;\r
- buffer = new int[newLen + GROW_EXTRA];\r
- }\r
-\r
- /**\r
- * Assumes start <= end.\r
- */\r
- private int[] range(int start, int end) {\r
- if (rangeList == null) {\r
- rangeList = new int[] { start, end+1, HIGH };\r
- } else {\r
- rangeList[0] = start;\r
- rangeList[1] = end+1;\r
- }\r
- return rangeList;\r
- }\r
-\r
- //----------------------------------------------------------------\r
- // Implementation: Fundamental operations\r
- //----------------------------------------------------------------\r
-\r
- // polarity = 0, 3 is normal: x xor y\r
- // polarity = 1, 2: x xor ~y == x === y\r
-\r
- private UnicodeSet xor(int[] other, int otherLen, int polarity) {\r
- ensureBufferCapacity(len + otherLen);\r
- int i = 0, j = 0, k = 0;\r
- int a = list[i++];\r
- int b;\r
- if (polarity == 1 || polarity == 2) {\r
- b = LOW;\r
- if (other[j] == LOW) { // skip base if already LOW\r
- ++j;\r
- b = other[j];\r
- }\r
- } else {\r
- b = other[j++];\r
- }\r
- // simplest of all the routines\r
- // sort the values, discarding identicals!\r
- while (true) {\r
- if (a < b) {\r
- buffer[k++] = a;\r
- a = list[i++];\r
- } else if (b < a) {\r
- buffer[k++] = b;\r
- b = other[j++];\r
- } else if (a != HIGH) { // at this point, a == b\r
- // discard both values!\r
- a = list[i++];\r
- b = other[j++];\r
- } else { // DONE!\r
- buffer[k++] = HIGH;\r
- len = k;\r
- break;\r
- }\r
- }\r
- // swap list and buffer\r
- int[] temp = list;\r
- list = buffer;\r
- buffer = temp;\r
- pat = null;\r
- return this;\r
- }\r
-\r
- // polarity = 0 is normal: x union y\r
- // polarity = 2: x union ~y\r
- // polarity = 1: ~x union y\r
- // polarity = 3: ~x union ~y\r
-\r
- private UnicodeSet add(int[] other, int otherLen, int polarity) {\r
- ensureBufferCapacity(len + otherLen);\r
- int i = 0, j = 0, k = 0;\r
- int a = list[i++];\r
- int b = other[j++];\r
- // change from xor is that we have to check overlapping pairs\r
- // polarity bit 1 means a is second, bit 2 means b is.\r
- main:\r
- while (true) {\r
- switch (polarity) {\r
- case 0: // both first; take lower if unequal\r
- if (a < b) { // take a\r
- // Back up over overlapping ranges in buffer[]\r
- if (k > 0 && a <= buffer[k-1]) {\r
- // Pick latter end value in buffer[] vs. list[]\r
- a = max(list[i], buffer[--k]);\r
- } else {\r
- // No overlap\r
- buffer[k++] = a;\r
- a = list[i];\r
- }\r
- i++; // Common if/else code factored out\r
- polarity ^= 1;\r
- } else if (b < a) { // take b\r
- if (k > 0 && b <= buffer[k-1]) {\r
- b = max(other[j], buffer[--k]);\r
- } else {\r
- buffer[k++] = b;\r
- b = other[j];\r
- }\r
- j++;\r
- polarity ^= 2;\r
- } else { // a == b, take a, drop b\r
- if (a == HIGH) break main;\r
- // This is symmetrical; it doesn't matter if\r
- // we backtrack with a or b. - liu\r
- if (k > 0 && a <= buffer[k-1]) {\r
- a = max(list[i], buffer[--k]);\r
- } else {\r
- // No overlap\r
- buffer[k++] = a;\r
- a = list[i];\r
- }\r
- i++;\r
- polarity ^= 1;\r
- b = other[j++]; polarity ^= 2;\r
- }\r
- break;\r
- case 3: // both second; take higher if unequal, and drop other\r
- if (b <= a) { // take a\r
- if (a == HIGH) break main;\r
- buffer[k++] = a;\r
- } else { // take b\r
- if (b == HIGH) break main;\r
- buffer[k++] = b;\r
- }\r
- a = list[i++]; polarity ^= 1; // factored common code\r
- b = other[j++]; polarity ^= 2;\r
- break;\r
- case 1: // a second, b first; if b < a, overlap\r
- if (a < b) { // no overlap, take a\r
- buffer[k++] = a; a = list[i++]; polarity ^= 1;\r
- } else if (b < a) { // OVERLAP, drop b\r
- b = other[j++]; polarity ^= 2;\r
- } else { // a == b, drop both!\r
- if (a == HIGH) break main;\r
- a = list[i++]; polarity ^= 1;\r
- b = other[j++]; polarity ^= 2;\r
- }\r
- break;\r
- case 2: // a first, b second; if a < b, overlap\r
- if (b < a) { // no overlap, take b\r
- buffer[k++] = b; b = other[j++]; polarity ^= 2;\r
- } else if (a < b) { // OVERLAP, drop a\r
- a = list[i++]; polarity ^= 1;\r
- } else { // a == b, drop both!\r
- if (a == HIGH) break main;\r
- a = list[i++]; polarity ^= 1;\r
- b = other[j++]; polarity ^= 2;\r
- }\r
- break;\r
- }\r
- }\r
- buffer[k++] = HIGH; // terminate\r
- len = k;\r
- // swap list and buffer\r
- int[] temp = list;\r
- list = buffer;\r
- buffer = temp;\r
- pat = null;\r
- return this;\r
- }\r
-\r
- // polarity = 0 is normal: x intersect y\r
- // polarity = 2: x intersect ~y == set-minus\r
- // polarity = 1: ~x intersect y\r
- // polarity = 3: ~x intersect ~y\r
-\r
- private UnicodeSet retain(int[] other, int otherLen, int polarity) {\r
- ensureBufferCapacity(len + otherLen);\r
- int i = 0, j = 0, k = 0;\r
- int a = list[i++];\r
- int b = other[j++];\r
- // change from xor is that we have to check overlapping pairs\r
- // polarity bit 1 means a is second, bit 2 means b is.\r
- main:\r
- while (true) {\r
- switch (polarity) {\r
- case 0: // both first; drop the smaller\r
- if (a < b) { // drop a\r
- a = list[i++]; polarity ^= 1;\r
- } else if (b < a) { // drop b\r
- b = other[j++]; polarity ^= 2;\r
- } else { // a == b, take one, drop other\r
- if (a == HIGH) break main;\r
- buffer[k++] = a; a = list[i++]; polarity ^= 1;\r
- b = other[j++]; polarity ^= 2;\r
- }\r
- break;\r
- case 3: // both second; take lower if unequal\r
- if (a < b) { // take a\r
- buffer[k++] = a; a = list[i++]; polarity ^= 1;\r
- } else if (b < a) { // take b\r
- buffer[k++] = b; b = other[j++]; polarity ^= 2;\r
- } else { // a == b, take one, drop other\r
- if (a == HIGH) break main;\r
- buffer[k++] = a; a = list[i++]; polarity ^= 1;\r
- b = other[j++]; polarity ^= 2;\r
- }\r
- break;\r
- case 1: // a second, b first;\r
- if (a < b) { // NO OVERLAP, drop a\r
- a = list[i++]; polarity ^= 1;\r
- } else if (b < a) { // OVERLAP, take b\r
- buffer[k++] = b; b = other[j++]; polarity ^= 2;\r
- } else { // a == b, drop both!\r
- if (a == HIGH) break main;\r
- a = list[i++]; polarity ^= 1;\r
- b = other[j++]; polarity ^= 2;\r
- }\r
- break;\r
- case 2: // a first, b second; if a < b, overlap\r
- if (b < a) { // no overlap, drop b\r
- b = other[j++]; polarity ^= 2;\r
- } else if (a < b) { // OVERLAP, take a\r
- buffer[k++] = a; a = list[i++]; polarity ^= 1;\r
- } else { // a == b, drop both!\r
- if (a == HIGH) break main;\r
- a = list[i++]; polarity ^= 1;\r
- b = other[j++]; polarity ^= 2;\r
- }\r
- break;\r
- }\r
- }\r
- buffer[k++] = HIGH; // terminate\r
- len = k;\r
- // swap list and buffer\r
- int[] temp = list;\r
- list = buffer;\r
- buffer = temp;\r
- pat = null;\r
- return this;\r
- }\r
-\r
- private static final int max(int a, int b) {\r
- return (a > b) ? a : b;\r
- }\r
-\r
- //----------------------------------------------------------------\r
- // Generic filter-based scanning code\r
- //----------------------------------------------------------------\r
-\r
- private static interface Filter {\r
- boolean contains(int codePoint);\r
- }\r
-\r
- private static class NumericValueFilter implements Filter {\r
- double value;\r
- NumericValueFilter(double value) { this.value = value; }\r
- public boolean contains(int ch) {\r
- return UCharacter.getUnicodeNumericValue(ch) == value;\r
- }\r
- }\r
-\r
- private static class GeneralCategoryMaskFilter implements Filter {\r
- int mask;\r
- GeneralCategoryMaskFilter(int mask) { this.mask = mask; }\r
- public boolean contains(int ch) {\r
- return ((1 << UCharacter.getType(ch)) & mask) != 0;\r
- }\r
- }\r
-\r
- private static class IntPropertyFilter implements Filter {\r
- int prop;\r
- int value;\r
- IntPropertyFilter(int prop, int value) {\r
- this.prop = prop;\r
- this.value = value;\r
- }\r
- public boolean contains(int ch) {\r
- return UCharacter.getIntPropertyValue(ch, prop) == value;\r
- }\r
- }\r
-\r
- // VersionInfo for unassigned characters\r
- static final VersionInfo NO_VERSION = VersionInfo.getInstance(0, 0, 0, 0);\r
-\r
- private static class VersionFilter implements Filter {\r
- VersionInfo version;\r
- VersionFilter(VersionInfo version) { this.version = version; }\r
- public boolean contains(int ch) {\r
- VersionInfo v = UCharacter.getAge(ch);\r
- // Reference comparison ok; VersionInfo caches and reuses\r
- // unique objects.\r
- return v != NO_VERSION &&\r
- v.compareTo(version) <= 0;\r
- }\r
- }\r
-\r
- private static synchronized UnicodeSet getInclusions(int src) {\r
- if (INCLUSIONS == null) {\r
- INCLUSIONS = new UnicodeSet[UCharacterProperty.SRC_COUNT];\r
- }\r
- if(INCLUSIONS[src] == null) {\r
- UnicodeSet incl = new UnicodeSet();\r
- switch(src) {\r
- case UCharacterProperty.SRC_CHAR:\r
- UCharacterProperty.getInstance().addPropertyStarts(incl);\r
- break;\r
- case UCharacterProperty.SRC_PROPSVEC:\r
- UCharacterProperty.getInstance().upropsvec_addPropertyStarts(incl);\r
- break;\r
- case UCharacterProperty.SRC_CHAR_AND_PROPSVEC:\r
- UCharacterProperty.getInstance().addPropertyStarts(incl);\r
- UCharacterProperty.getInstance().upropsvec_addPropertyStarts(incl);\r
- break;\r
- case UCharacterProperty.SRC_HST:\r
- UCharacterProperty.getInstance().uhst_addPropertyStarts(incl);\r
- break;\r
- case UCharacterProperty.SRC_NORM:\r
- NormalizerImpl.addPropertyStarts(incl);\r
- break;\r
- case UCharacterProperty.SRC_CASE:\r
- try {\r
- UCaseProps.getSingleton().addPropertyStarts(incl);\r
- } catch(IOException e) {\r
- throw new MissingResourceException(e.getMessage(),"","");\r
- }\r
- break;\r
- case UCharacterProperty.SRC_BIDI:\r
- try {\r
- UBiDiProps.getSingleton().addPropertyStarts(incl);\r
- } catch(IOException e) {\r
- throw new MissingResourceException(e.getMessage(),"","");\r
- }\r
- break;\r
- default:\r
- throw new IllegalStateException("UnicodeSet.getInclusions(unknown src "+src+")");\r
- }\r
- INCLUSIONS[src] = incl;\r
- }\r
- return INCLUSIONS[src];\r
- }\r
-\r
- /**\r
- * Generic filter-based scanning code for UCD property UnicodeSets.\r
- */\r
- private UnicodeSet applyFilter(Filter filter, int src) {\r
- // Walk through all Unicode characters, noting the start\r
- // and end of each range for which filter.contain(c) is\r
- // true. Add each range to a set.\r
- //\r
- // To improve performance, use the INCLUSIONS set, which\r
- // encodes information about character ranges that are known\r
- // to have identical properties, such as the CJK Ideographs\r
- // from U+4E00 to U+9FA5. INCLUSIONS contains all characters\r
- // except the first characters of such ranges.\r
- //\r
- // TODO Where possible, instead of scanning over code points,\r
- // use internal property data to initialize UnicodeSets for\r
- // those properties. Scanning code points is slow.\r
-\r
- clear();\r
-\r
- int startHasProperty = -1;\r
- UnicodeSet inclusions = getInclusions(src);\r
- int limitRange = inclusions.getRangeCount();\r
-\r
- for (int j=0; j<limitRange; ++j) {\r
- // get current range\r
- int start = inclusions.getRangeStart(j);\r
- int end = inclusions.getRangeEnd(j);\r
-\r
- // for all the code points in the range, process\r
- for (int ch = start; ch <= end; ++ch) {\r
- // only add to the unicodeset on inflection points --\r
- // where the hasProperty value changes to false\r
- if (filter.contains(ch)) {\r
- if (startHasProperty < 0) {\r
- startHasProperty = ch;\r
- }\r
- } else if (startHasProperty >= 0) {\r
- add_unchecked(startHasProperty, ch-1);\r
- startHasProperty = -1;\r
- }\r
- }\r
- }\r
- if (startHasProperty >= 0) {\r
- add_unchecked(startHasProperty, 0x10FFFF);\r
- }\r
-\r
- return this;\r
- }\r
-\r
-\r
- /**\r
- * Remove leading and trailing rule white space and compress\r
- * internal rule white space to a single space character.\r
- *\r
- * @see UCharacterProperty#isRuleWhiteSpace\r
- */\r
- private static String mungeCharName(String source) {\r
- StringBuffer buf = new StringBuffer();\r
- for (int i=0; i<source.length(); ) {\r
- int ch = UTF16.charAt(source, i);\r
- i += UTF16.getCharCount(ch);\r
- if (UCharacterProperty.isRuleWhiteSpace(ch)) {\r
- if (buf.length() == 0 ||\r
- buf.charAt(buf.length() - 1) == ' ') {\r
- continue;\r
- }\r
- ch = ' '; // convert to ' '\r
- }\r
- UTF16.append(buf, ch);\r
- }\r
- if (buf.length() != 0 &&\r
- buf.charAt(buf.length() - 1) == ' ') {\r
- buf.setLength(buf.length() - 1);\r
- }\r
- return buf.toString();\r
- }\r
-\r
- //----------------------------------------------------------------\r
- // Property set API\r
- //----------------------------------------------------------------\r
-\r
- /**\r
- * Modifies this set to contain those code points which have the\r
- * given value for the given binary or enumerated property, as\r
- * returned by UCharacter.getIntPropertyValue. Prior contents of\r
- * this set are lost.\r
- *\r
- * @param prop a property in the range\r
- * UProperty.BIN_START..UProperty.BIN_LIMIT-1 or\r
- * UProperty.INT_START..UProperty.INT_LIMIT-1 or.\r
- * UProperty.MASK_START..UProperty.MASK_LIMIT-1.\r
- *\r
- * @param value a value in the range\r
- * UCharacter.getIntPropertyMinValue(prop)..\r
- * UCharacter.getIntPropertyMaxValue(prop), with one exception.\r
- * If prop is UProperty.GENERAL_CATEGORY_MASK, then value should not be\r
- * a UCharacter.getType() result, but rather a mask value produced\r
- * by logically ORing (1 << UCharacter.getType()) values together.\r
- * This allows grouped categories such as [:L:] to be represented.\r
- *\r
- * @return a reference to this set\r
- *\r
- * @stable ICU 2.4\r
- */\r
- public UnicodeSet applyIntPropertyValue(int prop, int value) {\r
- checkFrozen();\r
- if (prop == UProperty.GENERAL_CATEGORY_MASK) {\r
- applyFilter(new GeneralCategoryMaskFilter(value), UCharacterProperty.SRC_CHAR);\r
- } else {\r
- applyFilter(new IntPropertyFilter(prop, value), UCharacterProperty.getInstance().getSource(prop));\r
- }\r
- return this;\r
- }\r
-\r
-\r
-\r
- /**\r
- * Modifies this set to contain those code points which have the\r
- * given value for the given property. Prior contents of this\r
- * set are lost.\r
- *\r
- * @param propertyAlias a property alias, either short or long.\r
- * The name is matched loosely. See PropertyAliases.txt for names\r
- * and a description of loose matching. If the value string is\r
- * empty, then this string is interpreted as either a\r
- * General_Category value alias, a Script value alias, a binary\r
- * property alias, or a special ID. Special IDs are matched\r
- * loosely and correspond to the following sets:\r
- *\r
- * "ANY" = [\u0000-\U0010FFFF],\r
- * "ASCII" = [\u0000-\u007F].\r
- *\r
- * @param valueAlias a value alias, either short or long. The\r
- * name is matched loosely. See PropertyValueAliases.txt for\r
- * names and a description of loose matching. In addition to\r
- * aliases listed, numeric values and canonical combining classes\r
- * may be expressed numerically, e.g., ("nv", "0.5") or ("ccc",\r
- * "220"). The value string may also be empty.\r
- *\r
- * @return a reference to this set\r
- *\r
- * @stable ICU 2.4\r
- */\r
- public UnicodeSet applyPropertyAlias(String propertyAlias, String valueAlias) {\r
- return applyPropertyAlias(propertyAlias, valueAlias, null);\r
- }\r
-\r
- /**\r
- * Modifies this set to contain those code points which have the\r
- * given value for the given property. Prior contents of this\r
- * set are lost.\r
- * @param propertyAlias\r
- * @param valueAlias\r
- * @param symbols if not null, then symbols are first called to see if a property\r
- * is available. If true, then everything else is skipped.\r
- * @return this set\r
- * @stable ICU 3.2\r
- */\r
- public UnicodeSet applyPropertyAlias(String propertyAlias,\r
- String valueAlias, SymbolTable symbols) {\r
- checkFrozen();\r
- int p;\r
- int v;\r
- boolean mustNotBeEmpty = false, invert = false;\r
-\r
- if (symbols != null\r
- && (symbols instanceof XSymbolTable)\r
- && ((XSymbolTable)symbols).applyPropertyAlias(propertyAlias, valueAlias, this)) {\r
- return this;\r
- }\r
-\r
- if (valueAlias.length() > 0) {\r
- p = UCharacter.getPropertyEnum(propertyAlias);\r
-\r
- // Treat gc as gcm\r
- if (p == UProperty.GENERAL_CATEGORY) {\r
- p = UProperty.GENERAL_CATEGORY_MASK;\r
- }\r
-\r
- if ((p >= UProperty.BINARY_START && p < UProperty.BINARY_LIMIT) ||\r
- (p >= UProperty.INT_START && p < UProperty.INT_LIMIT) ||\r
- (p >= UProperty.MASK_START && p < UProperty.MASK_LIMIT)) {\r
- try {\r
- v = UCharacter.getPropertyValueEnum(p, valueAlias);\r
- } catch (IllegalArgumentException e) {\r
- // Handle numeric CCC\r
- if (p == UProperty.CANONICAL_COMBINING_CLASS ||\r
- p == UProperty.LEAD_CANONICAL_COMBINING_CLASS ||\r
- p == UProperty.TRAIL_CANONICAL_COMBINING_CLASS) {\r
- v = Integer.parseInt(Utility.deleteRuleWhiteSpace(valueAlias));\r
- // If the resultant set is empty then the numeric value\r
- // was invalid.\r
- //mustNotBeEmpty = true;\r
- // old code was wrong; anything between 0 and 255 is valid even if unused.\r
- if (v < 0 || v > 255) throw e;\r
- } else {\r
- throw e;\r
- }\r
- }\r
- }\r
-\r
- else {\r
-\r
- switch (p) {\r
- case UProperty.NUMERIC_VALUE:\r
- {\r
- double value = Double.parseDouble(Utility.deleteRuleWhiteSpace(valueAlias));\r
- applyFilter(new NumericValueFilter(value), UCharacterProperty.SRC_CHAR);\r
- return this;\r
- }\r
- case UProperty.NAME:\r
- case UProperty.UNICODE_1_NAME:\r
- {\r
- // Must munge name, since\r
- // UCharacter.charFromName() does not do\r
- // 'loose' matching.\r
- String buf = mungeCharName(valueAlias);\r
- int ch =\r
- (p == UProperty.NAME) ?\r
- UCharacter.getCharFromExtendedName(buf) :\r
- UCharacter.getCharFromName1_0(buf);\r
- if (ch == -1) {\r
- throw new IllegalArgumentException("Invalid character name");\r
- }\r
- clear();\r
- add_unchecked(ch);\r
- return this;\r
- }\r
- case UProperty.AGE:\r
- {\r
- // Must munge name, since\r
- // VersionInfo.getInstance() does not do\r
- // 'loose' matching.\r
- VersionInfo version = VersionInfo.getInstance(mungeCharName(valueAlias));\r
- applyFilter(new VersionFilter(version), UCharacterProperty.SRC_PROPSVEC);\r
- return this;\r
- }\r
- }\r
-\r
- // p is a non-binary, non-enumerated property that we\r
- // don't support (yet).\r
- throw new IllegalArgumentException("Unsupported property");\r
- }\r
- }\r
-\r
- else {\r
- // valueAlias is empty. Interpret as General Category, Script,\r
- // Binary property, or ANY or ASCII. Upon success, p and v will\r
- // be set.\r
- try {\r
- p = UProperty.GENERAL_CATEGORY_MASK;\r
- v = UCharacter.getPropertyValueEnum(p, propertyAlias);\r
- } catch (IllegalArgumentException e) {\r
- try {\r
- p = UProperty.SCRIPT;\r
- v = UCharacter.getPropertyValueEnum(p, propertyAlias);\r
- } catch (IllegalArgumentException e2) {\r
- try {\r
- p = UCharacter.getPropertyEnum(propertyAlias);\r
- } catch (IllegalArgumentException e3) {\r
- p = -1;\r
- }\r
- if (p >= UProperty.BINARY_START && p < UProperty.BINARY_LIMIT) {\r
- v = 1;\r
- } else if (p == -1) {\r
- if (0 == UPropertyAliases.compare(ANY_ID, propertyAlias)) {\r
- set(MIN_VALUE, MAX_VALUE);\r
- return this;\r
- } else if (0 == UPropertyAliases.compare(ASCII_ID, propertyAlias)) {\r
- set(0, 0x7F);\r
- return this;\r
- } else if (0 == UPropertyAliases.compare(ASSIGNED, propertyAlias)) {\r
- // [:Assigned:]=[:^Cn:]\r
- p = UProperty.GENERAL_CATEGORY_MASK;\r
- v = (1<<UCharacter.UNASSIGNED);\r
- invert = true;\r
- } else {\r
- // Property name was never matched.\r
- throw new IllegalArgumentException("Invalid property alias: " + propertyAlias + "=" + valueAlias);\r
- }\r
- } else {\r
- // Valid propery name, but it isn't binary, so the value\r
- // must be supplied.\r
- throw new IllegalArgumentException("Missing property value");\r
- }\r
- }\r
- }\r
- }\r
-\r
- applyIntPropertyValue(p, v);\r
- if(invert) {\r
- complement();\r
- }\r
-\r
- if (mustNotBeEmpty && isEmpty()) {\r
- // mustNotBeEmpty is set to true if an empty set indicates\r
- // invalid input.\r
- throw new IllegalArgumentException("Invalid property value");\r
- }\r
-\r
- return this;\r
- }\r
-\r
- //----------------------------------------------------------------\r
- // Property set patterns\r
- //----------------------------------------------------------------\r
-\r
- /**\r
- * Return true if the given position, in the given pattern, appears\r
- * to be the start of a property set pattern.\r
- */\r
- private static boolean resemblesPropertyPattern(String pattern, int pos) {\r
- // Patterns are at least 5 characters long\r
- if ((pos+5) > pattern.length()) {\r
- return false;\r
- }\r
-\r
- // Look for an opening [:, [:^, \p, or \P\r
- return pattern.regionMatches(pos, "[:", 0, 2) ||\r
- pattern.regionMatches(true, pos, "\\p", 0, 2) ||\r
- pattern.regionMatches(pos, "\\N", 0, 2);\r
- }\r
-\r
- /**\r
- * Return true if the given iterator appears to point at a\r
- * property pattern. Regardless of the result, return with the\r
- * iterator unchanged.\r
- * @param chars iterator over the pattern characters. Upon return\r
- * it will be unchanged.\r
- * @param iterOpts RuleCharacterIterator options\r
- */\r
- private static boolean resemblesPropertyPattern(RuleCharacterIterator chars,\r
- int iterOpts) {\r
- boolean result = false;\r
- iterOpts &= ~RuleCharacterIterator.PARSE_ESCAPES;\r
- Object pos = chars.getPos(null);\r
- int c = chars.next(iterOpts);\r
- if (c == '[' || c == '\\') {\r
- int d = chars.next(iterOpts & ~RuleCharacterIterator.SKIP_WHITESPACE);\r
- result = (c == '[') ? (d == ':') :\r
- (d == 'N' || d == 'p' || d == 'P');\r
- }\r
- chars.setPos(pos);\r
- return result;\r
- }\r
-\r
- /**\r
- * Parse the given property pattern at the given parse position.\r
- * @param symbols TODO\r
- */\r
- private UnicodeSet applyPropertyPattern(String pattern, ParsePosition ppos, SymbolTable symbols) {\r
- int pos = ppos.getIndex();\r
-\r
- // On entry, ppos should point to one of the following locations:\r
-\r
- // Minimum length is 5 characters, e.g. \p{L}\r
- if ((pos+5) > pattern.length()) {\r
- return null;\r
- }\r
-\r
- boolean posix = false; // true for [:pat:], false for \p{pat} \P{pat} \N{pat}\r
- boolean isName = false; // true for \N{pat}, o/w false\r
- boolean invert = false;\r
-\r
- // Look for an opening [:, [:^, \p, or \P\r
- if (pattern.regionMatches(pos, "[:", 0, 2)) {\r
- posix = true;\r
- pos = Utility.skipWhitespace(pattern, pos+2);\r
- if (pos < pattern.length() && pattern.charAt(pos) == '^') {\r
- ++pos;\r
- invert = true;\r
- }\r
- } else if (pattern.regionMatches(true, pos, "\\p", 0, 2) ||\r
- pattern.regionMatches(pos, "\\N", 0, 2)) {\r
- char c = pattern.charAt(pos+1);\r
- invert = (c == 'P');\r
- isName = (c == 'N');\r
- pos = Utility.skipWhitespace(pattern, pos+2);\r
- if (pos == pattern.length() || pattern.charAt(pos++) != '{') {\r
- // Syntax error; "\p" or "\P" not followed by "{"\r
- return null;\r
- }\r
- } else {\r
- // Open delimiter not seen\r
- return null;\r
- }\r
-\r
- // Look for the matching close delimiter, either :] or }\r
- int close = pattern.indexOf(posix ? ":]" : "}", pos);\r
- if (close < 0) {\r
- // Syntax error; close delimiter missing\r
- return null;\r
- }\r
-\r
- // Look for an '=' sign. If this is present, we will parse a\r
- // medium \p{gc=Cf} or long \p{GeneralCategory=Format}\r
- // pattern.\r
- int equals = pattern.indexOf('=', pos);\r
- String propName, valueName;\r
- if (equals >= 0 && equals < close && !isName) {\r
- // Equals seen; parse medium/long pattern\r
- propName = pattern.substring(pos, equals);\r
- valueName = pattern.substring(equals+1, close);\r
- }\r
-\r
- else {\r
- // Handle case where no '=' is seen, and \N{}\r
- propName = pattern.substring(pos, close);\r
- valueName = "";\r
-\r
- // Handle \N{name}\r
- if (isName) {\r
- // This is a little inefficient since it means we have to\r
- // parse "na" back to UProperty.NAME even though we already\r
- // know it's UProperty.NAME. If we refactor the API to\r
- // support args of (int, String) then we can remove\r
- // "na" and make this a little more efficient.\r
- valueName = propName;\r
- propName = "na";\r
- }\r
- }\r
-\r
- applyPropertyAlias(propName, valueName, symbols);\r
-\r
- if (invert) {\r
- complement();\r
- }\r
-\r
- // Move to the limit position after the close delimiter\r
- ppos.setIndex(close + (posix ? 2 : 1));\r
-\r
- return this;\r
- }\r
-\r
- /**\r
- * Parse a property pattern.\r
- * @param chars iterator over the pattern characters. Upon return\r
- * it will be advanced to the first character after the parsed\r
- * pattern, or the end of the iteration if all characters are\r
- * parsed.\r
- * @param rebuiltPat the pattern that was parsed, rebuilt or\r
- * copied from the input pattern, as appropriate.\r
- * @param symbols TODO\r
- */\r
- private void applyPropertyPattern(RuleCharacterIterator chars,\r
- StringBuffer rebuiltPat, SymbolTable symbols) {\r
- String patStr = chars.lookahead();\r
- ParsePosition pos = new ParsePosition(0);\r
- applyPropertyPattern(patStr, pos, symbols);\r
- if (pos.getIndex() == 0) {\r
- syntaxError(chars, "Invalid property pattern");\r
- }\r
- chars.jumpahead(pos.getIndex());\r
- rebuiltPat.append(patStr.substring(0, pos.getIndex()));\r
- }\r
-\r
- //----------------------------------------------------------------\r
- // Case folding API\r
- //----------------------------------------------------------------\r
-\r
- /**\r
- * Bitmask for constructor and applyPattern() indicating that\r
- * white space should be ignored. If set, ignore characters for\r
- * which UCharacterProperty.isRuleWhiteSpace() returns true,\r
- * unless they are quoted or escaped. This may be ORed together\r
- * with other selectors.\r
- * @stable ICU 3.8\r
- */\r
- public static final int IGNORE_SPACE = 1;\r
-\r
- /**\r
- * Bitmask for constructor, applyPattern(), and closeOver()\r
- * indicating letter case. This may be ORed together with other\r
- * selectors.\r
- *\r
- * Enable case insensitive matching. E.g., "[ab]" with this flag\r
- * will match 'a', 'A', 'b', and 'B'. "[^ab]" with this flag will\r
- * match all except 'a', 'A', 'b', and 'B'. This performs a full\r
- * closure over case mappings, e.g. U+017F for s.\r
- *\r
- * The resulting set is a superset of the input for the code points but\r
- * not for the strings.\r
- * It performs a case mapping closure of the code points and adds\r
- * full case folding strings for the code points, and reduces strings of\r
- * the original set to their full case folding equivalents.\r
- *\r
- * This is designed for case-insensitive matches, for example\r
- * in regular expressions. The full code point case closure allows checking of\r
- * an input character directly against the closure set.\r
- * Strings are matched by comparing the case-folded form from the closure\r
- * set with an incremental case folding of the string in question.\r
- *\r
- * The closure set will also contain single code points if the original\r
- * set contained case-equivalent strings (like U+00DF for "ss" or "Ss" etc.).\r
- * This is not necessary (that is, redundant) for the above matching method\r
- * but results in the same closure sets regardless of whether the original\r
- * set contained the code point or a string.\r
- * @stable ICU 3.8\r
- */\r
- public static final int CASE = 2;\r
-\r
- /**\r
- * Alias for UnicodeSet.CASE, for ease of porting from C++ where ICU4C\r
- * also has both USET_CASE and USET_CASE_INSENSITIVE (see uset.h).\r
- * @see #CASE\r
- * @stable ICU 3.4\r
- */\r
- public static final int CASE_INSENSITIVE = 2;\r
-\r
- /**\r
- * Bitmask for constructor, applyPattern(), and closeOver()\r
- * indicating letter case. This may be ORed together with other\r
- * selectors.\r
- *\r
- * Enable case insensitive matching. E.g., "[ab]" with this flag\r
- * will match 'a', 'A', 'b', and 'B'. "[^ab]" with this flag will\r
- * match all except 'a', 'A', 'b', and 'B'. This adds the lower-,\r
- * title-, and uppercase mappings as well as the case folding\r
- * of each existing element in the set.\r
- * @stable ICU 3.4\r
- */\r
- public static final int ADD_CASE_MAPPINGS = 4;\r
-\r
- // add the result of a full case mapping to the set\r
- // use str as a temporary string to avoid constructing one\r
- private static final void addCaseMapping(UnicodeSet set, int result, StringBuffer full) {\r
- if(result >= 0) {\r
- if(result > UCaseProps.MAX_STRING_LENGTH) {\r
- // add a single-code point case mapping\r
- set.add(result);\r
- } else {\r
- // add a string case mapping from full with length result\r
- set.add(full.toString());\r
- full.setLength(0);\r
- }\r
- }\r
- // result < 0: the code point mapped to itself, no need to add it\r
- // see UCaseProps\r
- }\r
-\r
- /**\r
- * Close this set over the given attribute. For the attribute\r
- * CASE, the result is to modify this set so that:\r
- *\r
- * 1. For each character or string 'a' in this set, all strings\r
- * 'b' such that foldCase(a) == foldCase(b) are added to this set.\r
- * (For most 'a' that are single characters, 'b' will have\r
- * b.length() == 1.)\r
- *\r
- * 2. For each string 'e' in the resulting set, if e !=\r
- * foldCase(e), 'e' will be removed.\r
- *\r
- * Example: [aq\u00DF{Bc}{bC}{Fi}] => [aAqQ\u00DF\uFB01{ss}{bc}{fi}]\r
- *\r
- * (Here foldCase(x) refers to the operation\r
- * UCharacter.foldCase(x, true), and a == b actually denotes\r
- * a.equals(b), not pointer comparison.)\r
- *\r
- * @param attribute bitmask for attributes to close over.\r
- * Currently only the CASE bit is supported. Any undefined bits\r
- * are ignored.\r
- * @return a reference to this set.\r
- * @stable ICU 3.8\r
- */\r
- public UnicodeSet closeOver(int attribute) {\r
- checkFrozen();\r
- if ((attribute & (CASE | ADD_CASE_MAPPINGS)) != 0) {\r
- UCaseProps csp;\r
- try {\r
- csp = UCaseProps.getSingleton();\r
- } catch(IOException e) {\r
- return this;\r
- }\r
- UnicodeSet foldSet = new UnicodeSet(this);\r
- ULocale root = ULocale.ROOT;\r
-\r
- // start with input set to guarantee inclusion\r
- // CASE: remove strings because the strings will actually be reduced (folded);\r
- // therefore, start with no strings and add only those needed\r
- if((attribute & CASE) != 0) {\r
- foldSet.strings.clear();\r
- }\r
-\r
- int n = getRangeCount();\r
- int result;\r
- StringBuffer full = new StringBuffer();\r
- int locCache[] = new int[1];\r
-\r
- for (int i=0; i<n; ++i) {\r
- int start = getRangeStart(i);\r
- int end = getRangeEnd(i);\r
-\r
- if((attribute & CASE) != 0) {\r
- // full case closure\r
- for (int cp=start; cp<=end; ++cp) {\r
- csp.addCaseClosure(cp, foldSet);\r
- }\r
- } else {\r
- // add case mappings\r
- // (does not add long s for regular s, or Kelvin for k, for example)\r
- for (int cp=start; cp<=end; ++cp) {\r
- result = csp.toFullLower(cp, null, full, root, locCache);\r
- addCaseMapping(foldSet, result, full);\r
-\r
- result = csp.toFullTitle(cp, null, full, root, locCache);\r
- addCaseMapping(foldSet, result, full);\r
-\r
- result = csp.toFullUpper(cp, null, full, root, locCache);\r
- addCaseMapping(foldSet, result, full);\r
-\r
- result = csp.toFullFolding(cp, full, 0);\r
- addCaseMapping(foldSet, result, full);\r
- }\r
- }\r
- }\r
- if (!strings.isEmpty()) {\r
- String str;\r
- if ((attribute & CASE) != 0) {\r
- Iterator it = strings.iterator();\r
- while (it.hasNext()) {\r
- str = UCharacter.foldCase((String)it.next(), 0);\r
- if(!csp.addStringCaseClosure(str, foldSet)) {\r
- foldSet.add(str); // does not map to code points: add the folded string itself\r
- }\r
- }\r
- } else {\r
- BreakIterator bi = BreakIterator.getWordInstance(root);\r
- Iterator it = strings.iterator();\r
- while (it.hasNext()) {\r
- str = (String)it.next();\r
- foldSet.add(UCharacter.toLowerCase(root, str));\r
- foldSet.add(UCharacter.toTitleCase(root, str, bi));\r
- foldSet.add(UCharacter.toUpperCase(root, str));\r
- foldSet.add(UCharacter.foldCase(str, 0));\r
- }\r
- }\r
- }\r
- set(foldSet);\r
- }\r
- return this;\r
- }\r
-\r
- /**\r
- * Internal class for customizing UnicodeSet parsing of properties.\r
- * TODO: extend to allow customizing of codepoint ranges\r
- * @draft ICU3.8\r
- * @provisional This API might change or be removed in a future release.\r
- * @author medavis\r
- */\r
- abstract public static class XSymbolTable implements SymbolTable {\r
- /**\r
- * Default constructor\r
- * @draft ICU3.8\r
- * @provisional This API might change or be removed in a future release.\r
- */\r
- public XSymbolTable(){}\r
- /**\r
- * Supplies default implementation for SymbolTable (no action).\r
- * @draft ICU3.8\r
- * @provisional This API might change or be removed in a future release.\r
- */\r
- public UnicodeMatcher lookupMatcher(int i) {\r
- return null;\r
- }\r
- /**\r
- * Apply a new property alias. Is called when parsing [:xxx=yyy:]. Results are to put into result.\r
- * @param propertyName the xxx in [:xxx=yyy:]\r
- * @param propertyValue the yyy in [:xxx=yyy:]\r
- * @param result where the result is placed\r
- * @return true if handled\r
- * @draft ICU3.8\r
- * @provisional This API might change or be removed in a future release.\r
- */\r
- public boolean applyPropertyAlias(String propertyName, String propertyValue, UnicodeSet result) {\r
- return false;\r
- }\r
- /**\r
- * Supplies default implementation for SymbolTable (no action).\r
- * @draft ICU3.8\r
- * @provisional This API might change or be removed in a future release.\r
- */\r
- public char[] lookup(String s) {\r
- return null;\r
- }\r
- /**\r
- * Supplies default implementation for SymbolTable (no action).\r
- * @draft ICU3.8\r
- * @provisional This API might change or be removed in a future release.\r
- */\r
- public String parseReference(String text, ParsePosition pos, int limit) {\r
- return null;\r
- }\r
- }\r
-\r
- private boolean frozen;\r
- \r
- /**\r
- * Is this frozen, according to the Freezable interface?\r
- * @return value\r
- * @stable ICU 3.8\r
- */\r
- public boolean isFrozen() {\r
- return frozen;\r
- }\r
-\r
- /**\r
- * Freeze this class, according to the Freezable interface.\r
- * @return this\r
- * @stable ICU 3.8\r
- */\r
- public Object freeze() {\r
- frozen = true;\r
- return this;\r
- }\r
- \r
- /**\r
- * Clone a thawed version of this class, according to the Freezable interface.\r
- * @return this\r
- * @stable ICU 3.8\r
- */\r
- public Object cloneAsThawed() {\r
- UnicodeSet result = (UnicodeSet) clone();\r
- result.frozen = false;\r
- return result;\r
- }\r
- \r
- // internal function\r
- private void checkFrozen() {\r
- if (frozen) {\r
- throw new UnsupportedOperationException("Attempt to modify frozen object");\r
- }\r
- }\r
-}\r
-//eof\r
+//##header J2SE15
+/*
+ *******************************************************************************
+ * Copyright (C) 1996-2009, International Business Machines Corporation and *
+ * others. All Rights Reserved. *
+ *******************************************************************************
+ */
+package com.ibm.icu.text;
+
+import java.text.*;
+import com.ibm.icu.lang.*;
+
+import java.io.IOException;
+
+import com.ibm.icu.impl.NormalizerImpl;
+import com.ibm.icu.impl.Utility;
+import com.ibm.icu.impl.UCharacterProperty;
+import com.ibm.icu.impl.UBiDiProps;
+import com.ibm.icu.impl.UCaseProps;
+import com.ibm.icu.impl.UPropertyAliases;
+import com.ibm.icu.impl.SortedSetRelation;
+import com.ibm.icu.impl.RuleCharacterIterator;
+
+import com.ibm.icu.util.Freezable;
+import com.ibm.icu.util.ULocale;
+import com.ibm.icu.util.VersionInfo;
+
+import com.ibm.icu.text.BreakIterator;
+
+import java.util.MissingResourceException;
+import java.util.TreeSet;
+import java.util.Iterator;
+import java.util.Collection;
+
+/**
+ * A mutable set of Unicode characters and multicharacter strings. Objects of this class
+ * represent <em>character classes</em> used in regular expressions.
+ * A character specifies a subset of Unicode code points. Legal
+ * code points are U+0000 to U+10FFFF, inclusive.
+ *
+ * <p>The UnicodeSet class is not designed to be subclassed.
+ *
+ * <p><code>UnicodeSet</code> supports two APIs. The first is the
+ * <em>operand</em> API that allows the caller to modify the value of
+ * a <code>UnicodeSet</code> object. It conforms to Java 2's
+ * <code>java.util.Set</code> interface, although
+ * <code>UnicodeSet</code> does not actually implement that
+ * interface. All methods of <code>Set</code> are supported, with the
+ * modification that they take a character range or single character
+ * instead of an <code>Object</code>, and they take a
+ * <code>UnicodeSet</code> instead of a <code>Collection</code>. The
+ * operand API may be thought of in terms of boolean logic: a boolean
+ * OR is implemented by <code>add</code>, a boolean AND is implemented
+ * by <code>retain</code>, a boolean XOR is implemented by
+ * <code>complement</code> taking an argument, and a boolean NOT is
+ * implemented by <code>complement</code> with no argument. In terms
+ * of traditional set theory function names, <code>add</code> is a
+ * union, <code>retain</code> is an intersection, <code>remove</code>
+ * is an asymmetric difference, and <code>complement</code> with no
+ * argument is a set complement with respect to the superset range
+ * <code>MIN_VALUE-MAX_VALUE</code>
+ *
+ * <p>The second API is the
+ * <code>applyPattern()</code>/<code>toPattern()</code> API from the
+ * <code>java.text.Format</code>-derived classes. Unlike the
+ * methods that add characters, add categories, and control the logic
+ * of the set, the method <code>applyPattern()</code> sets all
+ * attributes of a <code>UnicodeSet</code> at once, based on a
+ * string pattern.
+ *
+ * <p><b>Pattern syntax</b></p>
+ *
+ * Patterns are accepted by the constructors and the
+ * <code>applyPattern()</code> methods and returned by the
+ * <code>toPattern()</code> method. These patterns follow a syntax
+ * similar to that employed by version 8 regular expression character
+ * classes. Here are some simple examples:
+ *
+ * <blockquote>
+ * <table>
+ * <tr align="top">
+ * <td nowrap valign="top" align="left"><code>[]</code></td>
+ * <td valign="top">No characters</td>
+ * </tr><tr align="top">
+ * <td nowrap valign="top" align="left"><code>[a]</code></td>
+ * <td valign="top">The character 'a'</td>
+ * </tr><tr align="top">
+ * <td nowrap valign="top" align="left"><code>[ae]</code></td>
+ * <td valign="top">The characters 'a' and 'e'</td>
+ * </tr>
+ * <tr>
+ * <td nowrap valign="top" align="left"><code>[a-e]</code></td>
+ * <td valign="top">The characters 'a' through 'e' inclusive, in Unicode code
+ * point order</td>
+ * </tr>
+ * <tr>
+ * <td nowrap valign="top" align="left"><code>[\\u4E01]</code></td>
+ * <td valign="top">The character U+4E01</td>
+ * </tr>
+ * <tr>
+ * <td nowrap valign="top" align="left"><code>[a{ab}{ac}]</code></td>
+ * <td valign="top">The character 'a' and the multicharacter strings "ab" and
+ * "ac"</td>
+ * </tr>
+ * <tr>
+ * <td nowrap valign="top" align="left"><code>[\p{Lu}]</code></td>
+ * <td valign="top">All characters in the general category Uppercase Letter</td>
+ * </tr>
+ * </table>
+ * </blockquote>
+ *
+ * Any character may be preceded by a backslash in order to remove any special
+ * meaning. White space characters, as defined by UCharacterProperty.isRuleWhiteSpace(), are
+ * ignored, unless they are escaped.
+ *
+ * <p>Property patterns specify a set of characters having a certain
+ * property as defined by the Unicode standard. Both the POSIX-like
+ * "[:Lu:]" and the Perl-like syntax "\p{Lu}" are recognized. For a
+ * complete list of supported property patterns, see the User's Guide
+ * for UnicodeSet at
+ * <a href="http://www.icu-project.org/userguide/unicodeSet.html">
+ * http://www.icu-project.org/userguide/unicodeSet.html</a>.
+ * Actual determination of property data is defined by the underlying
+ * Unicode database as implemented by UCharacter.
+ *
+ * <p>Patterns specify individual characters, ranges of characters, and
+ * Unicode property sets. When elements are concatenated, they
+ * specify their union. To complement a set, place a '^' immediately
+ * after the opening '['. Property patterns are inverted by modifying
+ * their delimiters; "[:^foo]" and "\P{foo}". In any other location,
+ * '^' has no special meaning.
+ *
+ * <p>Ranges are indicated by placing two a '-' between two
+ * characters, as in "a-z". This specifies the range of all
+ * characters from the left to the right, in Unicode order. If the
+ * left character is greater than or equal to the
+ * right character it is a syntax error. If a '-' occurs as the first
+ * character after the opening '[' or '[^', or if it occurs as the
+ * last character before the closing ']', then it is taken as a
+ * literal. Thus "[a\\-b]", "[-ab]", and "[ab-]" all indicate the same
+ * set of three characters, 'a', 'b', and '-'.
+ *
+ * <p>Sets may be intersected using the '&' operator or the asymmetric
+ * set difference may be taken using the '-' operator, for example,
+ * "[[:L:]&[\\u0000-\\u0FFF]]" indicates the set of all Unicode letters
+ * with values less than 4096. Operators ('&' and '|') have equal
+ * precedence and bind left-to-right. Thus
+ * "[[:L:]-[a-z]-[\\u0100-\\u01FF]]" is equivalent to
+ * "[[[:L:]-[a-z]]-[\\u0100-\\u01FF]]". This only really matters for
+ * difference; intersection is commutative.
+ *
+ * <table>
+ * <tr valign=top><td nowrap><code>[a]</code><td>The set containing 'a'
+ * <tr valign=top><td nowrap><code>[a-z]</code><td>The set containing 'a'
+ * through 'z' and all letters in between, in Unicode order
+ * <tr valign=top><td nowrap><code>[^a-z]</code><td>The set containing
+ * all characters but 'a' through 'z',
+ * that is, U+0000 through 'a'-1 and 'z'+1 through U+10FFFF
+ * <tr valign=top><td nowrap><code>[[<em>pat1</em>][<em>pat2</em>]]</code>
+ * <td>The union of sets specified by <em>pat1</em> and <em>pat2</em>
+ * <tr valign=top><td nowrap><code>[[<em>pat1</em>]&[<em>pat2</em>]]</code>
+ * <td>The intersection of sets specified by <em>pat1</em> and <em>pat2</em>
+ * <tr valign=top><td nowrap><code>[[<em>pat1</em>]-[<em>pat2</em>]]</code>
+ * <td>The asymmetric difference of sets specified by <em>pat1</em> and
+ * <em>pat2</em>
+ * <tr valign=top><td nowrap><code>[:Lu:] or \p{Lu}</code>
+ * <td>The set of characters having the specified
+ * Unicode property; in
+ * this case, Unicode uppercase letters
+ * <tr valign=top><td nowrap><code>[:^Lu:] or \P{Lu}</code>
+ * <td>The set of characters <em>not</em> having the given
+ * Unicode property
+ * </table>
+ *
+ * <p><b>Warning</b>: you cannot add an empty string ("") to a UnicodeSet.</p>
+ *
+ * <p><b>Formal syntax</b></p>
+ *
+ * <blockquote>
+ * <table>
+ * <tr align="top">
+ * <td nowrap valign="top" align="right"><code>pattern := </code></td>
+ * <td valign="top"><code>('[' '^'? item* ']') |
+ * property</code></td>
+ * </tr>
+ * <tr align="top">
+ * <td nowrap valign="top" align="right"><code>item := </code></td>
+ * <td valign="top"><code>char | (char '-' char) | pattern-expr<br>
+ * </code></td>
+ * </tr>
+ * <tr align="top">
+ * <td nowrap valign="top" align="right"><code>pattern-expr := </code></td>
+ * <td valign="top"><code>pattern | pattern-expr pattern |
+ * pattern-expr op pattern<br>
+ * </code></td>
+ * </tr>
+ * <tr align="top">
+ * <td nowrap valign="top" align="right"><code>op := </code></td>
+ * <td valign="top"><code>'&' | '-'<br>
+ * </code></td>
+ * </tr>
+ * <tr align="top">
+ * <td nowrap valign="top" align="right"><code>special := </code></td>
+ * <td valign="top"><code>'[' | ']' | '-'<br>
+ * </code></td>
+ * </tr>
+ * <tr align="top">
+ * <td nowrap valign="top" align="right"><code>char := </code></td>
+ * <td valign="top"><em>any character that is not</em><code> special<br>
+ * | ('\\' </code><em>any character</em><code>)<br>
+ * | ('\u' hex hex hex hex)<br>
+ * </code></td>
+ * </tr>
+ * <tr align="top">
+ * <td nowrap valign="top" align="right"><code>hex := </code></td>
+ * <td valign="top"><em>any character for which
+ * </em><code>Character.digit(c, 16)</code><em>
+ * returns a non-negative result</em></td>
+ * </tr>
+ * <tr>
+ * <td nowrap valign="top" align="right"><code>property := </code></td>
+ * <td valign="top"><em>a Unicode property set pattern</td>
+ * </tr>
+ * </table>
+ * <br>
+ * <table border="1">
+ * <tr>
+ * <td>Legend: <table>
+ * <tr>
+ * <td nowrap valign="top"><code>a := b</code></td>
+ * <td width="20" valign="top"> </td>
+ * <td valign="top"><code>a</code> may be replaced by <code>b</code> </td>
+ * </tr>
+ * <tr>
+ * <td nowrap valign="top"><code>a?</code></td>
+ * <td valign="top"></td>
+ * <td valign="top">zero or one instance of <code>a</code><br>
+ * </td>
+ * </tr>
+ * <tr>
+ * <td nowrap valign="top"><code>a*</code></td>
+ * <td valign="top"></td>
+ * <td valign="top">one or more instances of <code>a</code><br>
+ * </td>
+ * </tr>
+ * <tr>
+ * <td nowrap valign="top"><code>a | b</code></td>
+ * <td valign="top"></td>
+ * <td valign="top">either <code>a</code> or <code>b</code><br>
+ * </td>
+ * </tr>
+ * <tr>
+ * <td nowrap valign="top"><code>'a'</code></td>
+ * <td valign="top"></td>
+ * <td valign="top">the literal string between the quotes </td>
+ * </tr>
+ * </table>
+ * </td>
+ * </tr>
+ * </table>
+ * </blockquote>
+ * <p>To iterate over contents of UnicodeSet, use UnicodeSetIterator class.
+ *
+ * @author Alan Liu
+ * @stable ICU 2.0
+ * @see UnicodeSetIterator
+ */
+public class UnicodeSet extends UnicodeFilter implements Freezable {
+
+ private static final int LOW = 0x000000; // LOW <= all valid values. ZERO for codepoints
+ private static final int HIGH = 0x110000; // HIGH > all valid values. 10000 for code units.
+ // 110000 for codepoints
+
+ /**
+ * Minimum value that can be stored in a UnicodeSet.
+ * @stable ICU 2.0
+ */
+ public static final int MIN_VALUE = LOW;
+
+ /**
+ * Maximum value that can be stored in a UnicodeSet.
+ * @stable ICU 2.0
+ */
+ public static final int MAX_VALUE = HIGH - 1;
+
+ private int len; // length used; list may be longer to minimize reallocs
+ private int[] list; // MUST be terminated with HIGH
+ private int[] rangeList; // internal buffer
+ private int[] buffer; // internal buffer
+
+ // NOTE: normally the field should be of type SortedSet; but that is missing a public clone!!
+ // is not private so that UnicodeSetIterator can get access
+ TreeSet strings = new TreeSet();
+
+ /**
+ * The pattern representation of this set. This may not be the
+ * most economical pattern. It is the pattern supplied to
+ * applyPattern(), with variables substituted and whitespace
+ * removed. For sets constructed without applyPattern(), or
+ * modified using the non-pattern API, this string will be null,
+ * indicating that toPattern() must generate a pattern
+ * representation from the inversion list.
+ */
+ private String pat = null;
+
+ private static final int START_EXTRA = 16; // initial storage. Must be >= 0
+ private static final int GROW_EXTRA = START_EXTRA; // extra amount for growth. Must be >= 0
+
+ // Special property set IDs
+ private static final String ANY_ID = "ANY"; // [\u0000-\U0010FFFF]
+ private static final String ASCII_ID = "ASCII"; // [\u0000-\u007F]
+ private static final String ASSIGNED = "Assigned"; // [:^Cn:]
+
+ /**
+ * A set of all characters _except_ the second through last characters of
+ * certain ranges. These ranges are ranges of characters whose
+ * properties are all exactly alike, e.g. CJK Ideographs from
+ * U+4E00 to U+9FA5.
+ */
+ private static UnicodeSet INCLUSIONS[] = null;
+
+ //----------------------------------------------------------------
+ // Public API
+ //----------------------------------------------------------------
+
+ /**
+ * Constructs an empty set.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet() {
+ list = new int[1 + START_EXTRA];
+ list[len++] = HIGH;
+ }
+
+ /**
+ * Constructs a copy of an existing set.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet(UnicodeSet other) {
+ set(other);
+ }
+
+ /**
+ * Constructs a set containing the given range. If <code>end >
+ * start</code> then an empty set is created.
+ *
+ * @param start first character, inclusive, of range
+ * @param end last character, inclusive, of range
+ * @stable ICU 2.0
+ */
+ public UnicodeSet(int start, int end) {
+ this();
+ complement(start, end);
+ }
+
+ /**
+ * Constructs a set from the given pattern. See the class description
+ * for the syntax of the pattern language. Whitespace is ignored.
+ * @param pattern a string specifying what characters are in the set
+ * @exception java.lang.IllegalArgumentException if the pattern contains
+ * a syntax error.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet(String pattern) {
+ this();
+ applyPattern(pattern, null, null, IGNORE_SPACE);
+ }
+
+ /**
+ * Constructs a set from the given pattern. See the class description
+ * for the syntax of the pattern language.
+ * @param pattern a string specifying what characters are in the set
+ * @param ignoreWhitespace if true, ignore characters for which
+ * UCharacterProperty.isRuleWhiteSpace() returns true
+ * @exception java.lang.IllegalArgumentException if the pattern contains
+ * a syntax error.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet(String pattern, boolean ignoreWhitespace) {
+ this();
+ applyPattern(pattern, null, null, ignoreWhitespace ? IGNORE_SPACE : 0);
+ }
+
+ /**
+ * Constructs a set from the given pattern. See the class description
+ * for the syntax of the pattern language.
+ * @param pattern a string specifying what characters are in the set
+ * @param options a bitmask indicating which options to apply.
+ * Valid options are IGNORE_SPACE and CASE.
+ * @exception java.lang.IllegalArgumentException if the pattern contains
+ * a syntax error.
+ * @stable ICU 3.8
+ */
+ public UnicodeSet(String pattern, int options) {
+ this();
+ applyPattern(pattern, null, null, options);
+ }
+
+ /**
+ * Constructs a set from the given pattern. See the class description
+ * for the syntax of the pattern language.
+ * @param pattern a string specifying what characters are in the set
+ * @param pos on input, the position in pattern at which to start parsing.
+ * On output, the position after the last character parsed.
+ * @param symbols a symbol table mapping variables to char[] arrays
+ * and chars to UnicodeSets
+ * @exception java.lang.IllegalArgumentException if the pattern
+ * contains a syntax error.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet(String pattern, ParsePosition pos, SymbolTable symbols) {
+ this();
+ applyPattern(pattern, pos, symbols, IGNORE_SPACE);
+ }
+
+ /**
+ * Constructs a set from the given pattern. See the class description
+ * for the syntax of the pattern language.
+ * @param pattern a string specifying what characters are in the set
+ * @param pos on input, the position in pattern at which to start parsing.
+ * On output, the position after the last character parsed.
+ * @param symbols a symbol table mapping variables to char[] arrays
+ * and chars to UnicodeSets
+ * @param options a bitmask indicating which options to apply.
+ * Valid options are IGNORE_SPACE and CASE.
+ * @exception java.lang.IllegalArgumentException if the pattern
+ * contains a syntax error.
+ * @stable ICU 3.2
+ */
+ public UnicodeSet(String pattern, ParsePosition pos, SymbolTable symbols, int options) {
+ this();
+ applyPattern(pattern, pos, symbols, options);
+ }
+
+
+ /**
+ * Return a new set that is equivalent to this one.
+ * @stable ICU 2.0
+ */
+ public Object clone() {
+ UnicodeSet result = new UnicodeSet(this);
+ result.frozen = this.frozen;
+ return result;
+ }
+
+ /**
+ * Make this object represent the range <code>start - end</code>.
+ * If <code>end > start</code> then this object is set to an
+ * an empty range.
+ *
+ * @param start first character in the set, inclusive
+ * @param end last character in the set, inclusive
+ * @stable ICU 2.0
+ */
+ public UnicodeSet set(int start, int end) {
+ checkFrozen();
+ clear();
+ complement(start, end);
+ return this;
+ }
+
+ /**
+ * Make this object represent the same set as <code>other</code>.
+ * @param other a <code>UnicodeSet</code> whose value will be
+ * copied to this object
+ * @stable ICU 2.0
+ */
+ public UnicodeSet set(UnicodeSet other) {
+ checkFrozen();
+ list = (int[]) other.list.clone();
+ len = other.len;
+ pat = other.pat;
+ strings = (TreeSet)other.strings.clone();
+ return this;
+ }
+
+ /**
+ * Modifies this set to represent the set specified by the given pattern.
+ * See the class description for the syntax of the pattern language.
+ * Whitespace is ignored.
+ * @param pattern a string specifying what characters are in the set
+ * @exception java.lang.IllegalArgumentException if the pattern
+ * contains a syntax error.
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet applyPattern(String pattern) {
+ checkFrozen();
+ return applyPattern(pattern, null, null, IGNORE_SPACE);
+ }
+
+ /**
+ * Modifies this set to represent the set specified by the given pattern,
+ * optionally ignoring whitespace.
+ * See the class description for the syntax of the pattern language.
+ * @param pattern a string specifying what characters are in the set
+ * @param ignoreWhitespace if true then characters for which
+ * UCharacterProperty.isRuleWhiteSpace() returns true are ignored
+ * @exception java.lang.IllegalArgumentException if the pattern
+ * contains a syntax error.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet applyPattern(String pattern, boolean ignoreWhitespace) {
+ checkFrozen();
+ return applyPattern(pattern, null, null, ignoreWhitespace ? IGNORE_SPACE : 0);
+ }
+
+ /**
+ * Modifies this set to represent the set specified by the given pattern,
+ * optionally ignoring whitespace.
+ * See the class description for the syntax of the pattern language.
+ * @param pattern a string specifying what characters are in the set
+ * @param options a bitmask indicating which options to apply.
+ * Valid options are IGNORE_SPACE and CASE.
+ * @exception java.lang.IllegalArgumentException if the pattern
+ * contains a syntax error.
+ * @stable ICU 3.8
+ */
+ public UnicodeSet applyPattern(String pattern, int options) {
+ checkFrozen();
+ return applyPattern(pattern, null, null, options);
+ }
+
+ /**
+ * Return true if the given position, in the given pattern, appears
+ * to be the start of a UnicodeSet pattern.
+ * @stable ICU 2.0
+ */
+ public static boolean resemblesPattern(String pattern, int pos) {
+ return ((pos+1) < pattern.length() &&
+ pattern.charAt(pos) == '[') ||
+ resemblesPropertyPattern(pattern, pos);
+ }
+
+ /**
+ * Append the <code>toPattern()</code> representation of a
+ * string to the given <code>StringBuffer</code>.
+ */
+ private static void _appendToPat(StringBuffer buf, String s, boolean escapeUnprintable) {
+ for (int i = 0; i < s.length(); i += UTF16.getCharCount(i)) {
+ _appendToPat(buf, UTF16.charAt(s, i), escapeUnprintable);
+ }
+ }
+
+ /**
+ * Append the <code>toPattern()</code> representation of a
+ * character to the given <code>StringBuffer</code>.
+ */
+ private static void _appendToPat(StringBuffer buf, int c, boolean escapeUnprintable) {
+ if (escapeUnprintable && Utility.isUnprintable(c)) {
+ // Use hex escape notation (<backslash>uxxxx or <backslash>Uxxxxxxxx) for anything
+ // unprintable
+ if (Utility.escapeUnprintable(buf, c)) {
+ return;
+ }
+ }
+ // Okay to let ':' pass through
+ switch (c) {
+ case '[': // SET_OPEN:
+ case ']': // SET_CLOSE:
+ case '-': // HYPHEN:
+ case '^': // COMPLEMENT:
+ case '&': // INTERSECTION:
+ case '\\': //BACKSLASH:
+ case '{':
+ case '}':
+ case '$':
+ case ':':
+ buf.append('\\');
+ break;
+ default:
+ // Escape whitespace
+ if (UCharacterProperty.isRuleWhiteSpace(c)) {
+ buf.append('\\');
+ }
+ break;
+ }
+ UTF16.append(buf, c);
+ }
+
+ /**
+ * Returns a string representation of this set. If the result of
+ * calling this function is passed to a UnicodeSet constructor, it
+ * will produce another set that is equal to this one.
+ * @stable ICU 2.0
+ */
+ public String toPattern(boolean escapeUnprintable) {
+ StringBuffer result = new StringBuffer();
+ return _toPattern(result, escapeUnprintable).toString();
+ }
+
+ /**
+ * Append a string representation of this set to result. This will be
+ * a cleaned version of the string passed to applyPattern(), if there
+ * is one. Otherwise it will be generated.
+ */
+ private StringBuffer _toPattern(StringBuffer result,
+ boolean escapeUnprintable) {
+ if (pat != null) {
+ int i;
+ int backslashCount = 0;
+ for (i=0; i<pat.length(); ) {
+ int c = UTF16.charAt(pat, i);
+ i += UTF16.getCharCount(c);
+ if (escapeUnprintable && Utility.isUnprintable(c)) {
+ // If the unprintable character is preceded by an odd
+ // number of backslashes, then it has been escaped.
+ // Before unescaping it, we delete the final
+ // backslash.
+ if ((backslashCount % 2) == 1) {
+ result.setLength(result.length() - 1);
+ }
+ Utility.escapeUnprintable(result, c);
+ backslashCount = 0;
+ } else {
+ UTF16.append(result, c);
+ if (c == '\\') {
+ ++backslashCount;
+ } else {
+ backslashCount = 0;
+ }
+ }
+ }
+ return result;
+ }
+
+ return _generatePattern(result, escapeUnprintable, true);
+ }
+
+ /**
+ * Generate and append a string representation of this set to result.
+ * This does not use this.pat, the cleaned up copy of the string
+ * passed to applyPattern().
+ * @param result the buffer into which to generate the pattern
+ * @param escapeUnprintable escape unprintable characters if true
+ * @stable ICU 2.0
+ */
+ public StringBuffer _generatePattern(StringBuffer result, boolean escapeUnprintable) {
+ return _generatePattern(result, escapeUnprintable, true);
+ }
+
+ /**
+ * Generate and append a string representation of this set to result.
+ * This does not use this.pat, the cleaned up copy of the string
+ * passed to applyPattern().
+ * @param includeStrings if false, doesn't include the strings.
+ * @stable ICU 3.8
+ */
+ public StringBuffer _generatePattern(StringBuffer result,
+ boolean escapeUnprintable, boolean includeStrings) {
+ result.append('[');
+
+// // Check against the predefined categories. We implicitly build
+// // up ALL category sets the first time toPattern() is called.
+// for (int cat=0; cat<CATEGORY_COUNT; ++cat) {
+// if (this.equals(getCategorySet(cat))) {
+// result.append(':');
+// result.append(CATEGORY_NAMES.substring(cat*2, cat*2+2));
+// return result.append(":]");
+// }
+// }
+
+ int count = getRangeCount();
+
+ // If the set contains at least 2 intervals and includes both
+ // MIN_VALUE and MAX_VALUE, then the inverse representation will
+ // be more economical.
+ if (count > 1 &&
+ getRangeStart(0) == MIN_VALUE &&
+ getRangeEnd(count-1) == MAX_VALUE) {
+
+ // Emit the inverse
+ result.append('^');
+
+ for (int i = 1; i < count; ++i) {
+ int start = getRangeEnd(i-1)+1;
+ int end = getRangeStart(i)-1;
+ _appendToPat(result, start, escapeUnprintable);
+ if (start != end) {
+ if ((start+1) != end) {
+ result.append('-');
+ }
+ _appendToPat(result, end, escapeUnprintable);
+ }
+ }
+ }
+
+ // Default; emit the ranges as pairs
+ else {
+ for (int i = 0; i < count; ++i) {
+ int start = getRangeStart(i);
+ int end = getRangeEnd(i);
+ _appendToPat(result, start, escapeUnprintable);
+ if (start != end) {
+ if ((start+1) != end) {
+ result.append('-');
+ }
+ _appendToPat(result, end, escapeUnprintable);
+ }
+ }
+ }
+
+ if (includeStrings && strings.size() > 0) {
+ Iterator it = strings.iterator();
+ while (it.hasNext()) {
+ result.append('{');
+ _appendToPat(result, (String) it.next(), escapeUnprintable);
+ result.append('}');
+ }
+ }
+ return result.append(']');
+ }
+
+ /**
+ * Returns the number of elements in this set (its cardinality)
+ * Note than the elements of a set may include both individual
+ * codepoints and strings.
+ *
+ * @return the number of elements in this set (its cardinality).
+ * @stable ICU 2.0
+ */
+ public int size() {
+ int n = 0;
+ int count = getRangeCount();
+ for (int i = 0; i < count; ++i) {
+ n += getRangeEnd(i) - getRangeStart(i) + 1;
+ }
+ return n + strings.size();
+ }
+
+ /**
+ * Returns <tt>true</tt> if this set contains no elements.
+ *
+ * @return <tt>true</tt> if this set contains no elements.
+ * @stable ICU 2.0
+ */
+ public boolean isEmpty() {
+ return len == 1 && strings.size() == 0;
+ }
+
+ /**
+ * Implementation of UnicodeMatcher API. Returns <tt>true</tt> if
+ * this set contains any character whose low byte is the given
+ * value. This is used by <tt>RuleBasedTransliterator</tt> for
+ * indexing.
+ * @stable ICU 2.0
+ */
+ public boolean matchesIndexValue(int v) {
+ /* The index value v, in the range [0,255], is contained in this set if
+ * it is contained in any pair of this set. Pairs either have the high
+ * bytes equal, or unequal. If the high bytes are equal, then we have
+ * aaxx..aayy, where aa is the high byte. Then v is contained if xx <=
+ * v <= yy. If the high bytes are unequal we have aaxx..bbyy, bb>aa.
+ * Then v is contained if xx <= v || v <= yy. (This is identical to the
+ * time zone month containment logic.)
+ */
+ for (int i=0; i<getRangeCount(); ++i) {
+ int low = getRangeStart(i);
+ int high = getRangeEnd(i);
+ if ((low & ~0xFF) == (high & ~0xFF)) {
+ if ((low & 0xFF) <= v && v <= (high & 0xFF)) {
+ return true;
+ }
+ } else if ((low & 0xFF) <= v || v <= (high & 0xFF)) {
+ return true;
+ }
+ }
+ if (strings.size() != 0) {
+ Iterator it = strings.iterator();
+ while (it.hasNext()) {
+ String s = (String) it.next();
+ //if (s.length() == 0) {
+ // // Empty strings match everything
+ // return true;
+ //}
+ // assert(s.length() != 0); // We enforce this elsewhere
+ int c = UTF16.charAt(s, 0);
+ if ((c & 0xFF) == v) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Implementation of UnicodeMatcher.matches(). Always matches the
+ * longest possible multichar string.
+ * @stable ICU 2.0
+ */
+ public int matches(Replaceable text,
+ int[] offset,
+ int limit,
+ boolean incremental) {
+
+ if (offset[0] == limit) {
+ // Strings, if any, have length != 0, so we don't worry
+ // about them here. If we ever allow zero-length strings
+ // we much check for them here.
+ if (contains(UnicodeMatcher.ETHER)) {
+ return incremental ? U_PARTIAL_MATCH : U_MATCH;
+ } else {
+ return U_MISMATCH;
+ }
+ } else {
+ if (strings.size() != 0) { // try strings first
+
+ // might separate forward and backward loops later
+ // for now they are combined
+
+ // TODO Improve efficiency of this, at least in the forward
+ // direction, if not in both. In the forward direction we
+ // can assume the strings are sorted.
+
+ Iterator it = strings.iterator();
+ boolean forward = offset[0] < limit;
+
+ // firstChar is the leftmost char to match in the
+ // forward direction or the rightmost char to match in
+ // the reverse direction.
+ char firstChar = text.charAt(offset[0]);
+
+ // If there are multiple strings that can match we
+ // return the longest match.
+ int highWaterLength = 0;
+
+ while (it.hasNext()) {
+ String trial = (String) it.next();
+
+ //if (trial.length() == 0) {
+ // return U_MATCH; // null-string always matches
+ //}
+ // assert(trial.length() != 0); // We ensure this elsewhere
+
+ char c = trial.charAt(forward ? 0 : trial.length() - 1);
+
+ // Strings are sorted, so we can optimize in the
+ // forward direction.
+ if (forward && c > firstChar) break;
+ if (c != firstChar) continue;
+
+ int length = matchRest(text, offset[0], limit, trial);
+
+ if (incremental) {
+ int maxLen = forward ? limit-offset[0] : offset[0]-limit;
+ if (length == maxLen) {
+ // We have successfully matched but only up to limit.
+ return U_PARTIAL_MATCH;
+ }
+ }
+
+ if (length == trial.length()) {
+ // We have successfully matched the whole string.
+ if (length > highWaterLength) {
+ highWaterLength = length;
+ }
+ // In the forward direction we know strings
+ // are sorted so we can bail early.
+ if (forward && length < highWaterLength) {
+ break;
+ }
+ continue;
+ }
+ }
+
+ // We've checked all strings without a partial match.
+ // If we have full matches, return the longest one.
+ if (highWaterLength != 0) {
+ offset[0] += forward ? highWaterLength : -highWaterLength;
+ return U_MATCH;
+ }
+ }
+ return super.matches(text, offset, limit, incremental);
+ }
+ }
+
+ /**
+ * Returns the longest match for s in text at the given position.
+ * If limit > start then match forward from start+1 to limit
+ * matching all characters except s.charAt(0). If limit < start,
+ * go backward starting from start-1 matching all characters
+ * except s.charAt(s.length()-1). This method assumes that the
+ * first character, text.charAt(start), matches s, so it does not
+ * check it.
+ * @param text the text to match
+ * @param start the first character to match. In the forward
+ * direction, text.charAt(start) is matched against s.charAt(0).
+ * In the reverse direction, it is matched against
+ * s.charAt(s.length()-1).
+ * @param limit the limit offset for matching, either last+1 in
+ * the forward direction, or last-1 in the reverse direction,
+ * where last is the index of the last character to match.
+ * @return If part of s matches up to the limit, return |limit -
+ * start|. If all of s matches before reaching the limit, return
+ * s.length(). If there is a mismatch between s and text, return
+ * 0
+ */
+ private static int matchRest (Replaceable text, int start, int limit, String s) {
+ int maxLen;
+ int slen = s.length();
+ if (start < limit) {
+ maxLen = limit - start;
+ if (maxLen > slen) maxLen = slen;
+ for (int i = 1; i < maxLen; ++i) {
+ if (text.charAt(start + i) != s.charAt(i)) return 0;
+ }
+ } else {
+ maxLen = start - limit;
+ if (maxLen > slen) maxLen = slen;
+ --slen; // <=> slen = s.length() - 1;
+ for (int i = 1; i < maxLen; ++i) {
+ if (text.charAt(start - i) != s.charAt(slen - i)) return 0;
+ }
+ }
+ return maxLen;
+ }
+
+//#if defined(FOUNDATION10) || defined(J2SE13)
+//#else
+ /**
+ * Tests whether the text matches at the offset. If so, returns the end of the longest substring that it matches. If not, returns -1.
+ * @internal
+ * @deprecated This API is ICU internal only.
+ */
+ public int matchesAt(CharSequence text, int offset) {
+ int lastLen = -1;
+ strings:
+ if (strings.size() != 0) {
+ char firstChar = text.charAt(offset);
+ String trial = null;
+ // find the first string starting with firstChar
+ Iterator it = strings.iterator();
+ while (it.hasNext()) {
+ trial = (String) it.next();
+ char firstStringChar = trial.charAt(0);
+ if (firstStringChar < firstChar) continue;
+ if (firstStringChar > firstChar) break strings;
+ }
+ // now keep checking string until we get the longest one
+ for (;;) {
+ int tempLen = matchesAt(text, offset, trial);
+ if (lastLen > tempLen) break strings;
+ lastLen = tempLen;
+ if (!it.hasNext()) break;
+ trial = (String) it.next();
+ }
+ }
+ if (lastLen < 2) {
+ int cp = UTF16.charAt(text, offset);
+ if (contains(cp)) {
+ lastLen = UTF16.getCharCount(cp);
+ }
+ }
+ return offset+lastLen;
+ }
+
+ /**
+ * Does one string contain another, starting at a specific offset?
+ * @param text
+ * @param offset
+ * @param other
+ * @return
+ */
+ // Note: This method was moved from CollectionUtilities
+ private static int matchesAt(CharSequence text, int offset, CharSequence other) {
+ int len = other.length();
+ int i = 0;
+ int j = offset;
+ for (; i < len; ++i, ++j) {
+ char pc = other.charAt(i);
+ char tc = text.charAt(j);
+ if (pc != tc) return -1;
+ }
+ return i;
+ }
+//#endif
+
+ /**
+ * Implementation of UnicodeMatcher API. Union the set of all
+ * characters that may be matched by this object into the given
+ * set.
+ * @param toUnionTo the set into which to union the source characters
+ * @stable ICU 2.2
+ */
+ public void addMatchSetTo(UnicodeSet toUnionTo) {
+ toUnionTo.addAll(this);
+ }
+
+ /**
+ * Returns the index of the given character within this set, where
+ * the set is ordered by ascending code point. If the character
+ * is not in this set, return -1. The inverse of this method is
+ * <code>charAt()</code>.
+ * @return an index from 0..size()-1, or -1
+ * @stable ICU 2.0
+ */
+ public int indexOf(int c) {
+ if (c < MIN_VALUE || c > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(c, 6));
+ }
+ int i = 0;
+ int n = 0;
+ for (;;) {
+ int start = list[i++];
+ if (c < start) {
+ return -1;
+ }
+ int limit = list[i++];
+ if (c < limit) {
+ return n + c - start;
+ }
+ n += limit - start;
+ }
+ }
+
+ /**
+ * Returns the character at the given index within this set, where
+ * the set is ordered by ascending code point. If the index is
+ * out of range, return -1. The inverse of this method is
+ * <code>indexOf()</code>.
+ * @param index an index from 0..size()-1
+ * @return the character at the given index, or -1.
+ * @stable ICU 2.0
+ */
+ public int charAt(int index) {
+ if (index >= 0) {
+ // len2 is the largest even integer <= len, that is, it is len
+ // for even values and len-1 for odd values. With odd values
+ // the last entry is UNICODESET_HIGH.
+ int len2 = len & ~1;
+ for (int i=0; i < len2;) {
+ int start = list[i++];
+ int count = list[i++] - start;
+ if (index < count) {
+ return start + index;
+ }
+ index -= count;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Adds the specified range to this set if it is not already
+ * present. If this set already contains the specified range,
+ * the call leaves this set unchanged. If <code>end > start</code>
+ * then an empty range is added, leaving the set unchanged.
+ *
+ * @param start first character, inclusive, of range to be added
+ * to this set.
+ * @param end last character, inclusive, of range to be added
+ * to this set.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet add(int start, int end) {
+ checkFrozen();
+ return add_unchecked(start, end);
+ }
+
+ // for internal use, after checkFrozen has been called
+ private UnicodeSet add_unchecked(int start, int end) {
+ if (start < MIN_VALUE || start > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));
+ }
+ if (end < MIN_VALUE || end > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
+ }
+ if (start < end) {
+ add(range(start, end), 2, 0);
+ } else if (start == end) {
+ add(start);
+ }
+ return this;
+ }
+
+// /**
+// * Format out the inversion list as a string, for debugging. Uncomment when
+// * needed.
+// */
+// public final String dump() {
+// StringBuffer buf = new StringBuffer("[");
+// for (int i=0; i<len; ++i) {
+// if (i != 0) buf.append(", ");
+// int c = list[i];
+// //if (c <= 0x7F && c != '\n' && c != '\r' && c != '\t' && c != ' ') {
+// // buf.append((char) c);
+// //} else {
+// buf.append("U+").append(Utility.hex(c, (c<0x10000)?4:6));
+// //}
+// }
+// buf.append("]");
+// return buf.toString();
+// }
+
+ /**
+ * Adds the specified character to this set if it is not already
+ * present. If this set already contains the specified character,
+ * the call leaves this set unchanged.
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet add(int c) {
+ checkFrozen();
+ return add_unchecked(c);
+ }
+
+ // for internal use only, after checkFrozen has been called
+ private final UnicodeSet add_unchecked(int c) {
+ if (c < MIN_VALUE || c > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(c, 6));
+ }
+
+ // find smallest i such that c < list[i]
+ // if odd, then it is IN the set
+ // if even, then it is OUT of the set
+ int i = findCodePoint(c);
+
+ // already in set?
+ if ((i & 1) != 0) return this;
+
+ // HIGH is 0x110000
+ // assert(list[len-1] == HIGH);
+
+ // empty = [HIGH]
+ // [start_0, limit_0, start_1, limit_1, HIGH]
+
+ // [..., start_k-1, limit_k-1, start_k, limit_k, ..., HIGH]
+ // ^
+ // list[i]
+
+ // i == 0 means c is before the first range
+
+ if (c == list[i]-1) {
+ // c is before start of next range
+ list[i] = c;
+ // if we touched the HIGH mark, then add a new one
+ if (c == MAX_VALUE) {
+ ensureCapacity(len+1);
+ list[len++] = HIGH;
+ }
+ if (i > 0 && c == list[i-1]) {
+ // collapse adjacent ranges
+
+ // [..., start_k-1, c, c, limit_k, ..., HIGH]
+ // ^
+ // list[i]
+ System.arraycopy(list, i+1, list, i-1, len-i-1);
+ len -= 2;
+ }
+ }
+
+ else if (i > 0 && c == list[i-1]) {
+ // c is after end of prior range
+ list[i-1]++;
+ // no need to chcek for collapse here
+ }
+
+ else {
+ // At this point we know the new char is not adjacent to
+ // any existing ranges, and it is not 10FFFF.
+
+
+ // [..., start_k-1, limit_k-1, start_k, limit_k, ..., HIGH]
+ // ^
+ // list[i]
+
+ // [..., start_k-1, limit_k-1, c, c+1, start_k, limit_k, ..., HIGH]
+ // ^
+ // list[i]
+
+ // Don't use ensureCapacity() to save on copying.
+ // NOTE: This has no measurable impact on performance,
+ // but it might help in some usage patterns.
+ if (len+2 > list.length) {
+ int[] temp = new int[len + 2 + GROW_EXTRA];
+ if (i != 0) System.arraycopy(list, 0, temp, 0, i);
+ System.arraycopy(list, i, temp, i+2, len-i);
+ list = temp;
+ } else {
+ System.arraycopy(list, i, list, i+2, len-i);
+ }
+
+ list[i] = c;
+ list[i+1] = c+1;
+ len += 2;
+ }
+
+ pat = null;
+ return this;
+ }
+
+ /**
+ * Adds the specified multicharacter to this set if it is not already
+ * present. If this set already contains the multicharacter,
+ * the call leaves this set unchanged.
+ * Thus "ch" => {"ch"}
+ * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
+ * @param s the source string
+ * @return this object, for chaining
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet add(String s) {
+ checkFrozen();
+ int cp = getSingleCP(s);
+ if (cp < 0) {
+ strings.add(s);
+ pat = null;
+ } else {
+ add_unchecked(cp, cp);
+ }
+ return this;
+ }
+
+ /**
+ * @return a code point IF the string consists of a single one.
+ * otherwise returns -1.
+ * @param string to test
+ */
+ private static int getSingleCP(String s) {
+ if (s.length() < 1) {
+ throw new IllegalArgumentException("Can't use zero-length strings in UnicodeSet");
+ }
+ if (s.length() > 2) return -1;
+ if (s.length() == 1) return s.charAt(0);
+
+ // at this point, len = 2
+ int cp = UTF16.charAt(s, 0);
+ if (cp > 0xFFFF) { // is surrogate pair
+ return cp;
+ }
+ return -1;
+ }
+
+ /**
+ * Adds each of the characters in this string to the set. Thus "ch" => {"c", "h"}
+ * If this set already any particular character, it has no effect on that character.
+ * @param s the source string
+ * @return this object, for chaining
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet addAll(String s) {
+ checkFrozen();
+ int cp;
+ for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {
+ cp = UTF16.charAt(s, i);
+ add_unchecked(cp, cp);
+ }
+ return this;
+ }
+
+ /**
+ * Retains EACH of the characters in this string. Note: "ch" == {"c", "h"}
+ * If this set already any particular character, it has no effect on that character.
+ * @param s the source string
+ * @return this object, for chaining
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet retainAll(String s) {
+ return retainAll(fromAll(s));
+ }
+
+ /**
+ * Complement EACH of the characters in this string. Note: "ch" == {"c", "h"}
+ * If this set already any particular character, it has no effect on that character.
+ * @param s the source string
+ * @return this object, for chaining
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet complementAll(String s) {
+ return complementAll(fromAll(s));
+ }
+
+ /**
+ * Remove EACH of the characters in this string. Note: "ch" == {"c", "h"}
+ * If this set already any particular character, it has no effect on that character.
+ * @param s the source string
+ * @return this object, for chaining
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet removeAll(String s) {
+ return removeAll(fromAll(s));
+ }
+
+ /**
+ * Remove all strings from this UnicodeSet
+ * @return this object, for chaining
+ * @draft ICU 4.2
+ * @provisional This API might change or be removed in a future release.
+ */
+ public final UnicodeSet removeAllStrings() {
+ checkFrozen();
+ if (strings.size() != 0) {
+ strings.clear();
+ pat = null;
+ }
+ return this;
+ }
+
+ /**
+ * Makes a set from a multicharacter string. Thus "ch" => {"ch"}
+ * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
+ * @param s the source string
+ * @return a newly created set containing the given string
+ * @stable ICU 2.0
+ */
+ public static UnicodeSet from(String s) {
+ return new UnicodeSet().add(s);
+ }
+
+
+ /**
+ * Makes a set from each of the characters in the string. Thus "ch" => {"c", "h"}
+ * @param s the source string
+ * @return a newly created set containing the given characters
+ * @stable ICU 2.0
+ */
+ public static UnicodeSet fromAll(String s) {
+ return new UnicodeSet().addAll(s);
+ }
+
+
+ /**
+ * Retain only the elements in this set that are contained in the
+ * specified range. If <code>end > start</code> then an empty range is
+ * retained, leaving the set empty.
+ *
+ * @param start first character, inclusive, of range to be retained
+ * to this set.
+ * @param end last character, inclusive, of range to be retained
+ * to this set.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet retain(int start, int end) {
+ checkFrozen();
+ if (start < MIN_VALUE || start > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));
+ }
+ if (end < MIN_VALUE || end > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
+ }
+ if (start <= end) {
+ retain(range(start, end), 2, 0);
+ } else {
+ clear();
+ }
+ return this;
+ }
+
+ /**
+ * Retain the specified character from this set if it is present.
+ * Upon return this set will be empty if it did not contain c, or
+ * will only contain c if it did contain c.
+ * @param c the character to be retained
+ * @return this object, for chaining
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet retain(int c) {
+ return retain(c, c);
+ }
+
+ /**
+ * Retain the specified string in this set if it is present.
+ * Upon return this set will be empty if it did not contain s, or
+ * will only contain s if it did contain s.
+ * @param s the string to be retained
+ * @return this object, for chaining
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet retain(String s) {
+ int cp = getSingleCP(s);
+ if (cp < 0) {
+ boolean isIn = strings.contains(s);
+ if (isIn && size() == 1) {
+ return this;
+ }
+ clear();
+ strings.add(s);
+ pat = null;
+ } else {
+ retain(cp, cp);
+ }
+ return this;
+ }
+
+ /**
+ * Removes the specified range from this set if it is present.
+ * The set will not contain the specified range once the call
+ * returns. If <code>end > start</code> then an empty range is
+ * removed, leaving the set unchanged.
+ *
+ * @param start first character, inclusive, of range to be removed
+ * from this set.
+ * @param end last character, inclusive, of range to be removed
+ * from this set.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet remove(int start, int end) {
+ checkFrozen();
+ if (start < MIN_VALUE || start > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));
+ }
+ if (end < MIN_VALUE || end > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
+ }
+ if (start <= end) {
+ retain(range(start, end), 2, 2);
+ }
+ return this;
+ }
+
+ /**
+ * Removes the specified character from this set if it is present.
+ * The set will not contain the specified character once the call
+ * returns.
+ * @param c the character to be removed
+ * @return this object, for chaining
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet remove(int c) {
+ return remove(c, c);
+ }
+
+ /**
+ * Removes the specified string from this set if it is present.
+ * The set will not contain the specified string once the call
+ * returns.
+ * @param s the string to be removed
+ * @return this object, for chaining
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet remove(String s) {
+ int cp = getSingleCP(s);
+ if (cp < 0) {
+ strings.remove(s);
+ pat = null;
+ } else {
+ remove(cp, cp);
+ }
+ return this;
+ }
+
+ /**
+ * Complements the specified range in this set. Any character in
+ * the range will be removed if it is in this set, or will be
+ * added if it is not in this set. If <code>end > start</code>
+ * then an empty range is complemented, leaving the set unchanged.
+ *
+ * @param start first character, inclusive, of range to be removed
+ * from this set.
+ * @param end last character, inclusive, of range to be removed
+ * from this set.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet complement(int start, int end) {
+ checkFrozen();
+ if (start < MIN_VALUE || start > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));
+ }
+ if (end < MIN_VALUE || end > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
+ }
+ if (start <= end) {
+ xor(range(start, end), 2, 0);
+ }
+ pat = null;
+ return this;
+ }
+
+ /**
+ * Complements the specified character in this set. The character
+ * will be removed if it is in this set, or will be added if it is
+ * not in this set.
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet complement(int c) {
+ return complement(c, c);
+ }
+
+ /**
+ * This is equivalent to
+ * <code>complement(MIN_VALUE, MAX_VALUE)</code>.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet complement() {
+ checkFrozen();
+ if (list[0] == LOW) {
+ System.arraycopy(list, 1, list, 0, len-1);
+ --len;
+ } else {
+ ensureCapacity(len+1);
+ System.arraycopy(list, 0, list, 1, len);
+ list[0] = LOW;
+ ++len;
+ }
+ pat = null;
+ return this;
+ }
+
+ /**
+ * Complement the specified string in this set.
+ * The set will not contain the specified string once the call
+ * returns.
+ * <br><b>Warning: you cannot add an empty string ("") to a UnicodeSet.</b>
+ * @param s the string to complement
+ * @return this object, for chaining
+ * @stable ICU 2.0
+ */
+ public final UnicodeSet complement(String s) {
+ checkFrozen();
+ int cp = getSingleCP(s);
+ if (cp < 0) {
+ if (strings.contains(s)) strings.remove(s);
+ else strings.add(s);
+ pat = null;
+ } else {
+ complement(cp, cp);
+ }
+ return this;
+ }
+
+ /**
+ * Returns true if this set contains the given character.
+ * @param c character to be checked for containment
+ * @return true if the test condition is met
+ * @stable ICU 2.0
+ */
+ public boolean contains(int c) {
+ if (c < MIN_VALUE || c > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(c, 6));
+ }
+
+ /*
+ // Set i to the index of the start item greater than ch
+ // We know we will terminate without length test!
+ int i = -1;
+ while (true) {
+ if (c < list[++i]) break;
+ }
+ */
+
+ int i = findCodePoint(c);
+
+ return ((i & 1) != 0); // return true if odd
+ }
+
+ /**
+ * Returns the smallest value i such that c < list[i]. Caller
+ * must ensure that c is a legal value or this method will enter
+ * an infinite loop. This method performs a binary search.
+ * @param c a character in the range MIN_VALUE..MAX_VALUE
+ * inclusive
+ * @return the smallest integer i in the range 0..len-1,
+ * inclusive, such that c < list[i]
+ */
+ private final int findCodePoint(int c) {
+ /* Examples:
+ findCodePoint(c)
+ set list[] c=0 1 3 4 7 8
+ === ============== ===========
+ [] [110000] 0 0 0 0 0 0
+ [\u0000-\u0003] [0, 4, 110000] 1 1 1 2 2 2
+ [\u0004-\u0007] [4, 8, 110000] 0 0 0 1 1 2
+ [:all:] [0, 110000] 1 1 1 1 1 1
+ */
+
+ // Return the smallest i such that c < list[i]. Assume
+ // list[len - 1] == HIGH and that c is legal (0..HIGH-1).
+ if (c < list[0]) return 0;
+ // High runner test. c is often after the last range, so an
+ // initial check for this condition pays off.
+ if (len >= 2 && c >= list[len-2]) return len-1;
+ int lo = 0;
+ int hi = len - 1;
+ // invariant: c >= list[lo]
+ // invariant: c < list[hi]
+ for (;;) {
+ int i = (lo + hi) >>> 1;
+ if (i == lo) return hi;
+ if (c < list[i]) {
+ hi = i;
+ } else {
+ lo = i;
+ }
+ }
+ }
+
+// //----------------------------------------------------------------
+// // Unrolled binary search
+// //----------------------------------------------------------------
+//
+// private int validLen = -1; // validated value of len
+// private int topOfLow;
+// private int topOfHigh;
+// private int power;
+// private int deltaStart;
+//
+// private void validate() {
+// if (len <= 1) {
+// throw new IllegalArgumentException("list.len==" + len + "; must be >1");
+// }
+//
+// // find greatest power of 2 less than or equal to len
+// for (power = exp2.length-1; power > 0 && exp2[power] > len; power--) {}
+//
+// // assert(exp2[power] <= len);
+//
+// // determine the starting points
+// topOfLow = exp2[power] - 1;
+// topOfHigh = len - 1;
+// deltaStart = exp2[power-1];
+// validLen = len;
+// }
+//
+// private static final int exp2[] = {
+// 0x1, 0x2, 0x4, 0x8,
+// 0x10, 0x20, 0x40, 0x80,
+// 0x100, 0x200, 0x400, 0x800,
+// 0x1000, 0x2000, 0x4000, 0x8000,
+// 0x10000, 0x20000, 0x40000, 0x80000,
+// 0x100000, 0x200000, 0x400000, 0x800000,
+// 0x1000000, 0x2000000, 0x4000000, 0x8000000,
+// 0x10000000, 0x20000000 // , 0x40000000 // no unsigned int in Java
+// };
+//
+// /**
+// * Unrolled lowest index GT.
+// */
+// private final int leastIndexGT(int searchValue) {
+//
+// if (len != validLen) {
+// if (len == 1) return 0;
+// validate();
+// }
+// int temp;
+//
+// // set up initial range to search. Each subrange is a power of two in length
+// int high = searchValue < list[topOfLow] ? topOfLow : topOfHigh;
+//
+// // Completely unrolled binary search, folhighing "Programming Pearls"
+// // Each case deliberately falls through to the next
+// // Logically, list[-1] < all_search_values && list[count] > all_search_values
+// // although the values -1 and count are never actually touched.
+//
+// // The bounds at each point are low & high,
+// // where low == high - delta*2
+// // so high - delta is the midpoint
+//
+// // The invariant AFTER each line is that list[low] < searchValue <= list[high]
+//
+// switch (power) {
+// //case 31: if (searchValue < list[temp = high-0x40000000]) high = temp; // no unsigned int in Java
+// case 30: if (searchValue < list[temp = high-0x20000000]) high = temp;
+// case 29: if (searchValue < list[temp = high-0x10000000]) high = temp;
+//
+// case 28: if (searchValue < list[temp = high- 0x8000000]) high = temp;
+// case 27: if (searchValue < list[temp = high- 0x4000000]) high = temp;
+// case 26: if (searchValue < list[temp = high- 0x2000000]) high = temp;
+// case 25: if (searchValue < list[temp = high- 0x1000000]) high = temp;
+//
+// case 24: if (searchValue < list[temp = high- 0x800000]) high = temp;
+// case 23: if (searchValue < list[temp = high- 0x400000]) high = temp;
+// case 22: if (searchValue < list[temp = high- 0x200000]) high = temp;
+// case 21: if (searchValue < list[temp = high- 0x100000]) high = temp;
+//
+// case 20: if (searchValue < list[temp = high- 0x80000]) high = temp;
+// case 19: if (searchValue < list[temp = high- 0x40000]) high = temp;
+// case 18: if (searchValue < list[temp = high- 0x20000]) high = temp;
+// case 17: if (searchValue < list[temp = high- 0x10000]) high = temp;
+//
+// case 16: if (searchValue < list[temp = high- 0x8000]) high = temp;
+// case 15: if (searchValue < list[temp = high- 0x4000]) high = temp;
+// case 14: if (searchValue < list[temp = high- 0x2000]) high = temp;
+// case 13: if (searchValue < list[temp = high- 0x1000]) high = temp;
+//
+// case 12: if (searchValue < list[temp = high- 0x800]) high = temp;
+// case 11: if (searchValue < list[temp = high- 0x400]) high = temp;
+// case 10: if (searchValue < list[temp = high- 0x200]) high = temp;
+// case 9: if (searchValue < list[temp = high- 0x100]) high = temp;
+//
+// case 8: if (searchValue < list[temp = high- 0x80]) high = temp;
+// case 7: if (searchValue < list[temp = high- 0x40]) high = temp;
+// case 6: if (searchValue < list[temp = high- 0x20]) high = temp;
+// case 5: if (searchValue < list[temp = high- 0x10]) high = temp;
+//
+// case 4: if (searchValue < list[temp = high- 0x8]) high = temp;
+// case 3: if (searchValue < list[temp = high- 0x4]) high = temp;
+// case 2: if (searchValue < list[temp = high- 0x2]) high = temp;
+// case 1: if (searchValue < list[temp = high- 0x1]) high = temp;
+// }
+//
+// return high;
+// }
+//
+// // For debugging only
+// public int len() {
+// return len;
+// }
+//
+// //----------------------------------------------------------------
+// //----------------------------------------------------------------
+
+ /**
+ * Returns true if this set contains every character
+ * of the given range.
+ * @param start first character, inclusive, of the range
+ * @param end last character, inclusive, of the range
+ * @return true if the test condition is met
+ * @stable ICU 2.0
+ */
+ public boolean contains(int start, int end) {
+ if (start < MIN_VALUE || start > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));
+ }
+ if (end < MIN_VALUE || end > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
+ }
+ //int i = -1;
+ //while (true) {
+ // if (start < list[++i]) break;
+ //}
+ int i = findCodePoint(start);
+ return ((i & 1) != 0 && end < list[i]);
+ }
+
+ /**
+ * Returns <tt>true</tt> if this set contains the given
+ * multicharacter string.
+ * @param s string to be checked for containment
+ * @return <tt>true</tt> if this set contains the specified string
+ * @stable ICU 2.0
+ */
+ public final boolean contains(String s) {
+
+ int cp = getSingleCP(s);
+ if (cp < 0) {
+ return strings.contains(s);
+ } else {
+ return contains(cp);
+ }
+ }
+
+ /**
+ * Returns true if this set contains all the characters and strings
+ * of the given set.
+ * @param b set to be checked for containment
+ * @return true if the test condition is met
+ * @stable ICU 2.0
+ */
+ public boolean containsAll(UnicodeSet b) {
+ // The specified set is a subset if all of its pairs are contained in
+ // this set. This implementation accesses the lists directly for speed.
+ // TODO: this could be faster if size() were cached. But that would affect building speed
+ // so it needs investigation.
+ int[] listB = b.list;
+ boolean needA = true;
+ boolean needB = true;
+ int aPtr = 0;
+ int bPtr = 0;
+ int aLen = len - 1;
+ int bLen = b.len - 1;
+ int startA = 0, startB = 0, limitA = 0, limitB = 0;
+ while (true) {
+ // double iterations are such a pain...
+ if (needA) {
+ if (aPtr >= aLen) {
+ // ran out of A. If B is also exhausted, then break;
+ if (needB && bPtr >= bLen) {
+ break;
+ }
+ return false;
+ }
+ startA = list[aPtr++];
+ limitA = list[aPtr++];
+ }
+ if (needB) {
+ if (bPtr >= bLen) {
+ // ran out of B. Since we got this far, we have an A and we are ok so far
+ break;
+ }
+ startB = listB[bPtr++];
+ limitB = listB[bPtr++];
+ }
+ // if B doesn't overlap and is greater than A, get new A
+ if (startB >= limitA) {
+ needA = true;
+ needB = false;
+ continue;
+ }
+ // if B is wholy contained in A, then get a new B
+ if (startB >= startA && limitB <= limitA) {
+ needA = false;
+ needB = true;
+ continue;
+ }
+ // all other combinations mean we fail
+ return false;
+ }
+
+ if (!strings.containsAll(b.strings)) return false;
+ return true;
+ }
+
+// /**
+// * Returns true if this set contains all the characters and strings
+// * of the given set.
+// * @param c set to be checked for containment
+// * @return true if the test condition is met
+// * @stable ICU 2.0
+// */
+// public boolean containsAllOld(UnicodeSet c) {
+// // The specified set is a subset if all of its pairs are contained in
+// // this set. It's possible to code this more efficiently in terms of
+// // direct manipulation of the inversion lists if the need arises.
+// int n = c.getRangeCount();
+// for (int i=0; i<n; ++i) {
+// if (!contains(c.getRangeStart(i), c.getRangeEnd(i))) {
+// return false;
+// }
+// }
+// if (!strings.containsAll(c.strings)) return false;
+// return true;
+// }
+
+ /**
+ * Returns true if there is a partition of the string such that this set contains each of the partitioned strings.
+ * For example, for the Unicode set [a{bc}{cd}]<br>
+ * containsAll is true for each of: "a", "bc", ""cdbca"<br>
+ * containsAll is false for each of: "acb", "bcda", "bcx"<br>
+ * @param s string containing characters to be checked for containment
+ * @return true if the test condition is met
+ * @stable ICU 2.0
+ */
+ public boolean containsAll(String s) {
+ int cp;
+ for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {
+ cp = UTF16.charAt(s, i);
+ if (!contains(cp)) {
+ if (strings.size() == 0) {
+ return false;
+ }
+ return containsAll(s, 0);
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Recursive routine called if we fail to find a match in containsAll, and there are strings
+ * @param s source string
+ * @param i point to match to the end on
+ * @return true if ok
+ */
+ private boolean containsAll(String s, int i) {
+ if (i >= s.length()) {
+ return true;
+ }
+ int cp= UTF16.charAt(s, i);
+ if (contains(cp) && containsAll(s, i+UTF16.getCharCount(cp))) {
+ return true;
+ }
+
+ Iterator it = strings.iterator();
+ while (it.hasNext()) {
+ String setStr = (String)it.next();
+ if (s.startsWith(setStr, i) && containsAll(s, i+setStr.length())) {
+ return true;
+ }
+ }
+ return false;
+
+ }
+
+ /**
+ * Get the Regex equivalent for this UnicodeSet
+ * @return regex pattern equivalent to this UnicodeSet
+ * @internal
+ * @deprecated This API is ICU internal only.
+ */
+ public String getRegexEquivalent() {
+ if (strings.size() == 0) return toString();
+ StringBuffer result = new StringBuffer("(?:");
+ _generatePattern(result, true, false);
+ Iterator it = strings.iterator();
+ while (it.hasNext()) {
+ result.append('|');
+ _appendToPat(result, (String) it.next(), true);
+ }
+ return result.append(")").toString();
+ }
+
+ /**
+ * Returns true if this set contains none of the characters
+ * of the given range.
+ * @param start first character, inclusive, of the range
+ * @param end last character, inclusive, of the range
+ * @return true if the test condition is met
+ * @stable ICU 2.0
+ */
+ public boolean containsNone(int start, int end) {
+ if (start < MIN_VALUE || start > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(start, 6));
+ }
+ if (end < MIN_VALUE || end > MAX_VALUE) {
+ throw new IllegalArgumentException("Invalid code point U+" + Utility.hex(end, 6));
+ }
+ int i = -1;
+ while (true) {
+ if (start < list[++i]) break;
+ }
+ return ((i & 1) == 0 && end < list[i]);
+ }
+
+ /**
+ * Returns true if none of the characters or strings in this UnicodeSet appears in the string.
+ * For example, for the Unicode set [a{bc}{cd}]<br>
+ * containsNone is true for: "xy", "cb"<br>
+ * containsNone is false for: "a", "bc", "bcd"<br>
+ * @param b set to be checked for containment
+ * @return true if the test condition is met
+ * @stable ICU 2.0
+ */
+ public boolean containsNone(UnicodeSet b) {
+ // The specified set is a subset if some of its pairs overlap with some of this set's pairs.
+ // This implementation accesses the lists directly for speed.
+ int[] listB = b.list;
+ boolean needA = true;
+ boolean needB = true;
+ int aPtr = 0;
+ int bPtr = 0;
+ int aLen = len - 1;
+ int bLen = b.len - 1;
+ int startA = 0, startB = 0, limitA = 0, limitB = 0;
+ while (true) {
+ // double iterations are such a pain...
+ if (needA) {
+ if (aPtr >= aLen) {
+ // ran out of A: break so we test strings
+ break;
+ }
+ startA = list[aPtr++];
+ limitA = list[aPtr++];
+ }
+ if (needB) {
+ if (bPtr >= bLen) {
+ // ran out of B: break so we test strings
+ break;
+ }
+ startB = listB[bPtr++];
+ limitB = listB[bPtr++];
+ }
+ // if B is higher than any part of A, get new A
+ if (startB >= limitA) {
+ needA = true;
+ needB = false;
+ continue;
+ }
+ // if A is higher than any part of B, get new B
+ if (startA >= limitB) {
+ needA = false;
+ needB = true;
+ continue;
+ }
+ // all other combinations mean we fail
+ return false;
+ }
+
+ if (!SortedSetRelation.hasRelation(strings, SortedSetRelation.DISJOINT, b.strings)) return false;
+ return true;
+ }
+
+// /**
+// * Returns true if none of the characters or strings in this UnicodeSet appears in the string.
+// * For example, for the Unicode set [a{bc}{cd}]<br>
+// * containsNone is true for: "xy", "cb"<br>
+// * containsNone is false for: "a", "bc", "bcd"<br>
+// * @param c set to be checked for containment
+// * @return true if the test condition is met
+// * @stable ICU 2.0
+// */
+// public boolean containsNoneOld(UnicodeSet c) {
+// // The specified set is a subset if all of its pairs are contained in
+// // this set. It's possible to code this more efficiently in terms of
+// // direct manipulation of the inversion lists if the need arises.
+// int n = c.getRangeCount();
+// for (int i=0; i<n; ++i) {
+// if (!containsNone(c.getRangeStart(i), c.getRangeEnd(i))) {
+// return false;
+// }
+// }
+// if (!SortedSetRelation.hasRelation(strings, SortedSetRelation.DISJOINT, c.strings)) return false;
+// return true;
+// }
+
+ /**
+ * Returns true if this set contains none of the characters
+ * of the given string.
+ * @param s string containing characters to be checked for containment
+ * @return true if the test condition is met
+ * @stable ICU 2.0
+ */
+ public boolean containsNone(String s) {
+ int cp;
+ for (int i = 0; i < s.length(); i += UTF16.getCharCount(cp)) {
+ cp = UTF16.charAt(s, i);
+ if (contains(cp)) return false;
+ }
+ if (strings.size() == 0) return true;
+ // do a last check to make sure no strings are in.
+ for (Iterator it = strings.iterator(); it.hasNext();) {
+ String item = (String)it.next();
+ if (s.indexOf(item) >= 0) return false;
+ }
+ return true;
+ }
+
+ /**
+ * Returns true if this set contains one or more of the characters
+ * in the given range.
+ * @param start first character, inclusive, of the range
+ * @param end last character, inclusive, of the range
+ * @return true if the condition is met
+ * @stable ICU 2.0
+ */
+ public final boolean containsSome(int start, int end) {
+ return !containsNone(start, end);
+ }
+
+ /**
+ * Returns true if this set contains one or more of the characters
+ * and strings of the given set.
+ * @param s set to be checked for containment
+ * @return true if the condition is met
+ * @stable ICU 2.0
+ */
+ public final boolean containsSome(UnicodeSet s) {
+ return !containsNone(s);
+ }
+
+ /**
+ * Returns true if this set contains one or more of the characters
+ * of the given string.
+ * @param s string containing characters to be checked for containment
+ * @return true if the condition is met
+ * @stable ICU 2.0
+ */
+ public final boolean containsSome(String s) {
+ return !containsNone(s);
+ }
+
+
+ /**
+ * Adds all of the elements in the specified set to this set if
+ * they're not already present. This operation effectively
+ * modifies this set so that its value is the <i>union</i> of the two
+ * sets. The behavior of this operation is unspecified if the specified
+ * collection is modified while the operation is in progress.
+ *
+ * @param c set whose elements are to be added to this set.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet addAll(UnicodeSet c) {
+ checkFrozen();
+ add(c.list, c.len, 0);
+ strings.addAll(c.strings);
+ return this;
+ }
+
+ /**
+ * Retains only the elements in this set that are contained in the
+ * specified set. In other words, removes from this set all of
+ * its elements that are not contained in the specified set. This
+ * operation effectively modifies this set so that its value is
+ * the <i>intersection</i> of the two sets.
+ *
+ * @param c set that defines which elements this set will retain.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet retainAll(UnicodeSet c) {
+ checkFrozen();
+ retain(c.list, c.len, 0);
+ strings.retainAll(c.strings);
+ return this;
+ }
+
+ /**
+ * Removes from this set all of its elements that are contained in the
+ * specified set. This operation effectively modifies this
+ * set so that its value is the <i>asymmetric set difference</i> of
+ * the two sets.
+ *
+ * @param c set that defines which elements will be removed from
+ * this set.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet removeAll(UnicodeSet c) {
+ checkFrozen();
+ retain(c.list, c.len, 2);
+ strings.removeAll(c.strings);
+ return this;
+ }
+
+ /**
+ * Complements in this set all elements contained in the specified
+ * set. Any character in the other set will be removed if it is
+ * in this set, or will be added if it is not in this set.
+ *
+ * @param c set that defines which elements will be complemented from
+ * this set.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet complementAll(UnicodeSet c) {
+ checkFrozen();
+ xor(c.list, c.len, 0);
+ SortedSetRelation.doOperation(strings, SortedSetRelation.COMPLEMENTALL, c.strings);
+ return this;
+ }
+
+ /**
+ * Removes all of the elements from this set. This set will be
+ * empty after this call returns.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet clear() {
+ checkFrozen();
+ list[0] = HIGH;
+ len = 1;
+ pat = null;
+ strings.clear();
+ return this;
+ }
+
+ /**
+ * Iteration method that returns the number of ranges contained in
+ * this set.
+ * @see #getRangeStart
+ * @see #getRangeEnd
+ * @stable ICU 2.0
+ */
+ public int getRangeCount() {
+ return len/2;
+ }
+
+ /**
+ * Iteration method that returns the first character in the
+ * specified range of this set.
+ * @exception ArrayIndexOutOfBoundsException if index is outside
+ * the range <code>0..getRangeCount()-1</code>
+ * @see #getRangeCount
+ * @see #getRangeEnd
+ * @stable ICU 2.0
+ */
+ public int getRangeStart(int index) {
+ return list[index*2];
+ }
+
+ /**
+ * Iteration method that returns the last character in the
+ * specified range of this set.
+ * @exception ArrayIndexOutOfBoundsException if index is outside
+ * the range <code>0..getRangeCount()-1</code>
+ * @see #getRangeStart
+ * @see #getRangeEnd
+ * @stable ICU 2.0
+ */
+ public int getRangeEnd(int index) {
+ return (list[index*2 + 1] - 1);
+ }
+
+ /**
+ * Reallocate this objects internal structures to take up the least
+ * possible space, without changing this object's value.
+ * @stable ICU 2.0
+ */
+ public UnicodeSet compact() {
+ checkFrozen();
+ if (len != list.length) {
+ int[] temp = new int[len];
+ System.arraycopy(list, 0, temp, 0, len);
+ list = temp;
+ }
+ rangeList = null;
+ buffer = null;
+ return this;
+ }
+
+ /**
+ * Compares the specified object with this set for equality. Returns
+ * <tt>true</tt> if the specified object is also a set, the two sets
+ * have the same size, and every member of the specified set is
+ * contained in this set (or equivalently, every member of this set is
+ * contained in the specified set).
+ *
+ * @param o Object to be compared for equality with this set.
+ * @return <tt>true</tt> if the specified Object is equal to this set.
+ * @stable ICU 2.0
+ */
+ public boolean equals(Object o) {
+ try {
+ UnicodeSet that = (UnicodeSet) o;
+ if (len != that.len) return false;
+ for (int i = 0; i < len; ++i) {
+ if (list[i] != that.list[i]) return false;
+ }
+ if (!strings.equals(that.strings)) return false;
+ } catch (Exception e) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Returns the hash code value for this set.
+ *
+ * @return the hash code value for this set.
+ * @see java.lang.Object#hashCode()
+ * @stable ICU 2.0
+ */
+ public int hashCode() {
+ int result = len;
+ for (int i = 0; i < len; ++i) {
+ result *= 1000003;
+ result += list[i];
+ }
+ return result;
+ }
+
+ /**
+ * Return a programmer-readable string representation of this object.
+ * @stable ICU 2.0
+ */
+ public String toString() {
+ return toPattern(true);
+ }
+
+ //----------------------------------------------------------------
+ // Implementation: Pattern parsing
+ //----------------------------------------------------------------
+
+ /**
+ * Parses the given pattern, starting at the given position. The character
+ * at pattern.charAt(pos.getIndex()) must be '[', or the parse fails.
+ * Parsing continues until the corresponding closing ']'. If a syntax error
+ * is encountered between the opening and closing brace, the parse fails.
+ * Upon return from a successful parse, the ParsePosition is updated to
+ * point to the character following the closing ']', and an inversion
+ * list for the parsed pattern is returned. This method
+ * calls itself recursively to parse embedded subpatterns.
+ *
+ * @param pattern the string containing the pattern to be parsed. The
+ * portion of the string from pos.getIndex(), which must be a '[', to the
+ * corresponding closing ']', is parsed.
+ * @param pos upon entry, the position at which to being parsing. The
+ * character at pattern.charAt(pos.getIndex()) must be a '['. Upon return
+ * from a successful parse, pos.getIndex() is either the character after the
+ * closing ']' of the parsed pattern, or pattern.length() if the closing ']'
+ * is the last character of the pattern string.
+ * @return an inversion list for the parsed substring
+ * of <code>pattern</code>
+ * @exception java.lang.IllegalArgumentException if the parse fails.
+ * @internal
+ * @deprecated - for internal use only
+ */
+ public UnicodeSet applyPattern(String pattern,
+ ParsePosition pos,
+ SymbolTable symbols,
+ int options) {
+
+ // Need to build the pattern in a temporary string because
+ // _applyPattern calls add() etc., which set pat to empty.
+ boolean parsePositionWasNull = pos == null;
+ if (parsePositionWasNull) {
+ pos = new ParsePosition(0);
+ }
+
+ StringBuffer rebuiltPat = new StringBuffer();
+ RuleCharacterIterator chars =
+ new RuleCharacterIterator(pattern, symbols, pos);
+ applyPattern(chars, symbols, rebuiltPat, options);
+ if (chars.inVariable()) {
+ syntaxError(chars, "Extra chars in variable value");
+ }
+ pat = rebuiltPat.toString();
+ if (parsePositionWasNull) {
+ int i = pos.getIndex();
+
+ // Skip over trailing whitespace
+ if ((options & IGNORE_SPACE) != 0) {
+ i = Utility.skipWhitespace(pattern, i);
+ }
+
+ if (i != pattern.length()) {
+ throw new IllegalArgumentException("Parse of \"" + pattern +
+ "\" failed at " + i);
+ }
+ }
+ return this;
+ }
+
+ /**
+ * Parse the pattern from the given RuleCharacterIterator. The
+ * iterator is advanced over the parsed pattern.
+ * @param chars iterator over the pattern characters. Upon return
+ * it will be advanced to the first character after the parsed
+ * pattern, or the end of the iteration if all characters are
+ * parsed.
+ * @param symbols symbol table to use to parse and dereference
+ * variables, or null if none.
+ * @param rebuiltPat the pattern that was parsed, rebuilt or
+ * copied from the input pattern, as appropriate.
+ * @param options a bit mask of zero or more of the following:
+ * IGNORE_SPACE, CASE.
+ */
+ void applyPattern(RuleCharacterIterator chars, SymbolTable symbols,
+ StringBuffer rebuiltPat, int options) {
+
+ // Syntax characters: [ ] ^ - & { }
+
+ // Recognized special forms for chars, sets: c-c s-s s&s
+
+ int opts = RuleCharacterIterator.PARSE_VARIABLES |
+ RuleCharacterIterator.PARSE_ESCAPES;
+ if ((options & IGNORE_SPACE) != 0) {
+ opts |= RuleCharacterIterator.SKIP_WHITESPACE;
+ }
+
+ StringBuffer patBuf = new StringBuffer(), buf = null;
+ boolean usePat = false;
+ UnicodeSet scratch = null;
+ Object backup = null;
+
+ // mode: 0=before [, 1=between [...], 2=after ]
+ // lastItem: 0=none, 1=char, 2=set
+ int lastItem = 0, lastChar = 0, mode = 0;
+ char op = 0;
+
+ boolean invert = false;
+
+ clear();
+
+ while (mode != 2 && !chars.atEnd()) {
+ if (false) {
+ // Debugging assertion
+ if (!((lastItem == 0 && op == 0) ||
+ (lastItem == 1 && (op == 0 || op == '-')) ||
+ (lastItem == 2 && (op == 0 || op == '-' || op == '&')))) {
+ throw new IllegalArgumentException();
+ }
+ }
+
+ int c = 0;
+ boolean literal = false;
+ UnicodeSet nested = null;
+
+ // -------- Check for property pattern
+
+ // setMode: 0=none, 1=unicodeset, 2=propertypat, 3=preparsed
+ int setMode = 0;
+ if (resemblesPropertyPattern(chars, opts)) {
+ setMode = 2;
+ }
+
+ // -------- Parse '[' of opening delimiter OR nested set.
+ // If there is a nested set, use `setMode' to define how
+ // the set should be parsed. If the '[' is part of the
+ // opening delimiter for this pattern, parse special
+ // strings "[", "[^", "[-", and "[^-". Check for stand-in
+ // characters representing a nested set in the symbol
+ // table.
+
+ else {
+ // Prepare to backup if necessary
+ backup = chars.getPos(backup);
+ c = chars.next(opts);
+ literal = chars.isEscaped();
+
+ if (c == '[' && !literal) {
+ if (mode == 1) {
+ chars.setPos(backup); // backup
+ setMode = 1;
+ } else {
+ // Handle opening '[' delimiter
+ mode = 1;
+ patBuf.append('[');
+ backup = chars.getPos(backup); // prepare to backup
+ c = chars.next(opts);
+ literal = chars.isEscaped();
+ if (c == '^' && !literal) {
+ invert = true;
+ patBuf.append('^');
+ backup = chars.getPos(backup); // prepare to backup
+ c = chars.next(opts);
+ literal = chars.isEscaped();
+ }
+ // Fall through to handle special leading '-';
+ // otherwise restart loop for nested [], \p{}, etc.
+ if (c == '-') {
+ literal = true;
+ // Fall through to handle literal '-' below
+ } else {
+ chars.setPos(backup); // backup
+ continue;
+ }
+ }
+ } else if (symbols != null) {
+ UnicodeMatcher m = symbols.lookupMatcher(c); // may be null
+ if (m != null) {
+ try {
+ nested = (UnicodeSet) m;
+ setMode = 3;
+ } catch (ClassCastException e) {
+ syntaxError(chars, "Syntax error");
+ }
+ }
+ }
+ }
+
+ // -------- Handle a nested set. This either is inline in
+ // the pattern or represented by a stand-in that has
+ // previously been parsed and was looked up in the symbol
+ // table.
+
+ if (setMode != 0) {
+ if (lastItem == 1) {
+ if (op != 0) {
+ syntaxError(chars, "Char expected after operator");
+ }
+ add_unchecked(lastChar, lastChar);
+ _appendToPat(patBuf, lastChar, false);
+ lastItem = op = 0;
+ }
+
+ if (op == '-' || op == '&') {
+ patBuf.append(op);
+ }
+
+ if (nested == null) {
+ if (scratch == null) scratch = new UnicodeSet();
+ nested = scratch;
+ }
+ switch (setMode) {
+ case 1:
+ nested.applyPattern(chars, symbols, patBuf, options);
+ break;
+ case 2:
+ chars.skipIgnored(opts);
+ nested.applyPropertyPattern(chars, patBuf, symbols);
+ break;
+ case 3: // `nested' already parsed
+ nested._toPattern(patBuf, false);
+ break;
+ }
+
+ usePat = true;
+
+ if (mode == 0) {
+ // Entire pattern is a category; leave parse loop
+ set(nested);
+ mode = 2;
+ break;
+ }
+
+ switch (op) {
+ case '-':
+ removeAll(nested);
+ break;
+ case '&':
+ retainAll(nested);
+ break;
+ case 0:
+ addAll(nested);
+ break;
+ }
+
+ op = 0;
+ lastItem = 2;
+
+ continue;
+ }
+
+ if (mode == 0) {
+ syntaxError(chars, "Missing '['");
+ }
+
+ // -------- Parse special (syntax) characters. If the
+ // current character is not special, or if it is escaped,
+ // then fall through and handle it below.
+
+ if (!literal) {
+ switch (c) {
+ case ']':
+ if (lastItem == 1) {
+ add_unchecked(lastChar, lastChar);
+ _appendToPat(patBuf, lastChar, false);
+ }
+ // Treat final trailing '-' as a literal
+ if (op == '-') {
+ add_unchecked(op, op);
+ patBuf.append(op);
+ } else if (op == '&') {
+ syntaxError(chars, "Trailing '&'");
+ }
+ patBuf.append(']');
+ mode = 2;
+ continue;
+ case '-':
+ if (op == 0) {
+ if (lastItem != 0) {
+ op = (char) c;
+ continue;
+ } else {
+ // Treat final trailing '-' as a literal
+ add_unchecked(c, c);
+ c = chars.next(opts);
+ literal = chars.isEscaped();
+ if (c == ']' && !literal) {
+ patBuf.append("-]");
+ mode = 2;
+ continue;
+ }
+ }
+ }
+ syntaxError(chars, "'-' not after char or set");
+ case '&':
+ if (lastItem == 2 && op == 0) {
+ op = (char) c;
+ continue;
+ }
+ syntaxError(chars, "'&' not after set");
+ case '^':
+ syntaxError(chars, "'^' not after '['");
+ case '{':
+ if (op != 0) {
+ syntaxError(chars, "Missing operand after operator");
+ }
+ if (lastItem == 1) {
+ add_unchecked(lastChar, lastChar);
+ _appendToPat(patBuf, lastChar, false);
+ }
+ lastItem = 0;
+ if (buf == null) {
+ buf = new StringBuffer();
+ } else {
+ buf.setLength(0);
+ }
+ boolean ok = false;
+ while (!chars.atEnd()) {
+ c = chars.next(opts);
+ literal = chars.isEscaped();
+ if (c == '}' && !literal) {
+ ok = true;
+ break;
+ }
+ UTF16.append(buf, c);
+ }
+ if (buf.length() < 1 || !ok) {
+ syntaxError(chars, "Invalid multicharacter string");
+ }
+ // We have new string. Add it to set and continue;
+ // we don't need to drop through to the further
+ // processing
+ add(buf.toString());
+ patBuf.append('{');
+ _appendToPat(patBuf, buf.toString(), false);
+ patBuf.append('}');
+ continue;
+ case SymbolTable.SYMBOL_REF:
+ // symbols nosymbols
+ // [a-$] error error (ambiguous)
+ // [a$] anchor anchor
+ // [a-$x] var "x"* literal '$'
+ // [a-$.] error literal '$'
+ // *We won't get here in the case of var "x"
+ backup = chars.getPos(backup);
+ c = chars.next(opts);
+ literal = chars.isEscaped();
+ boolean anchor = (c == ']' && !literal);
+ if (symbols == null && !anchor) {
+ c = SymbolTable.SYMBOL_REF;
+ chars.setPos(backup);
+ break; // literal '$'
+ }
+ if (anchor && op == 0) {
+ if (lastItem == 1) {
+ add_unchecked(lastChar, lastChar);
+ _appendToPat(patBuf, lastChar, false);
+ }
+ add_unchecked(UnicodeMatcher.ETHER);
+ usePat = true;
+ patBuf.append(SymbolTable.SYMBOL_REF).append(']');
+ mode = 2;
+ continue;
+ }
+ syntaxError(chars, "Unquoted '$'");
+ default:
+ break;
+ }
+ }
+
+ // -------- Parse literal characters. This includes both
+ // escaped chars ("\u4E01") and non-syntax characters
+ // ("a").
+
+ switch (lastItem) {
+ case 0:
+ lastItem = 1;
+ lastChar = c;
+ break;
+ case 1:
+ if (op == '-') {
+ if (lastChar >= c) {
+ // Don't allow redundant (a-a) or empty (b-a) ranges;
+ // these are most likely typos.
+ syntaxError(chars, "Invalid range");
+ }
+ add_unchecked(lastChar, c);
+ _appendToPat(patBuf, lastChar, false);
+ patBuf.append(op);
+ _appendToPat(patBuf, c, false);
+ lastItem = op = 0;
+ } else {
+ add_unchecked(lastChar, lastChar);
+ _appendToPat(patBuf, lastChar, false);
+ lastChar = c;
+ }
+ break;
+ case 2:
+ if (op != 0) {
+ syntaxError(chars, "Set expected after operator");
+ }
+ lastChar = c;
+ lastItem = 1;
+ break;
+ }
+ }
+
+ if (mode != 2) {
+ syntaxError(chars, "Missing ']'");
+ }
+
+ chars.skipIgnored(opts);
+
+ /**
+ * Handle global flags (invert, case insensitivity). If this
+ * pattern should be compiled case-insensitive, then we need
+ * to close over case BEFORE COMPLEMENTING. This makes
+ * patterns like /[^abc]/i work.
+ */
+ if ((options & CASE) != 0) {
+ closeOver(CASE);
+ }
+ if (invert) {
+ complement();
+ }
+
+ // Use the rebuilt pattern (pat) only if necessary. Prefer the
+ // generated pattern.
+ if (usePat) {
+ rebuiltPat.append(patBuf.toString());
+ } else {
+ _generatePattern(rebuiltPat, false, true);
+ }
+ }
+
+ private static void syntaxError(RuleCharacterIterator chars, String msg) {
+ throw new IllegalArgumentException("Error: " + msg + " at \"" +
+ Utility.escape(chars.toString()) +
+ '"');
+ }
+
+ /**
+ * Add the contents of the UnicodeSet (as strings) into a collection.
+ * @param target collection to add into
+ * @stable ICU 2.8
+ */
+ public void addAllTo(Collection target) {
+ UnicodeSetIterator it = new UnicodeSetIterator(this);
+ while (it.next()) {
+ target.add(it.getString());
+ }
+ }
+
+ /**
+ * Add the contents of the collection (as strings) into this UnicodeSet.
+ * @param source the collection to add
+ * @stable ICU 2.8
+ */
+ public void addAll(Collection source) {
+ checkFrozen();
+ Iterator it = source.iterator();
+ while (it.hasNext()) {
+ add(it.next().toString());
+ }
+ }
+
+ //----------------------------------------------------------------
+ // Implementation: Utility methods
+ //----------------------------------------------------------------
+
+ private void ensureCapacity(int newLen) {
+ if (newLen <= list.length) return;
+ int[] temp = new int[newLen + GROW_EXTRA];
+ System.arraycopy(list, 0, temp, 0, len);
+ list = temp;
+ }
+
+ private void ensureBufferCapacity(int newLen) {
+ if (buffer != null && newLen <= buffer.length) return;
+ buffer = new int[newLen + GROW_EXTRA];
+ }
+
+ /**
+ * Assumes start <= end.
+ */
+ private int[] range(int start, int end) {
+ if (rangeList == null) {
+ rangeList = new int[] { start, end+1, HIGH };
+ } else {
+ rangeList[0] = start;
+ rangeList[1] = end+1;
+ }
+ return rangeList;
+ }
+
+ //----------------------------------------------------------------
+ // Implementation: Fundamental operations
+ //----------------------------------------------------------------
+
+ // polarity = 0, 3 is normal: x xor y
+ // polarity = 1, 2: x xor ~y == x === y
+
+ private UnicodeSet xor(int[] other, int otherLen, int polarity) {
+ ensureBufferCapacity(len + otherLen);
+ int i = 0, j = 0, k = 0;
+ int a = list[i++];
+ int b;
+ if (polarity == 1 || polarity == 2) {
+ b = LOW;
+ if (other[j] == LOW) { // skip base if already LOW
+ ++j;
+ b = other[j];
+ }
+ } else {
+ b = other[j++];
+ }
+ // simplest of all the routines
+ // sort the values, discarding identicals!
+ while (true) {
+ if (a < b) {
+ buffer[k++] = a;
+ a = list[i++];
+ } else if (b < a) {
+ buffer[k++] = b;
+ b = other[j++];
+ } else if (a != HIGH) { // at this point, a == b
+ // discard both values!
+ a = list[i++];
+ b = other[j++];
+ } else { // DONE!
+ buffer[k++] = HIGH;
+ len = k;
+ break;
+ }
+ }
+ // swap list and buffer
+ int[] temp = list;
+ list = buffer;
+ buffer = temp;
+ pat = null;
+ return this;
+ }
+
+ // polarity = 0 is normal: x union y
+ // polarity = 2: x union ~y
+ // polarity = 1: ~x union y
+ // polarity = 3: ~x union ~y
+
+ private UnicodeSet add(int[] other, int otherLen, int polarity) {
+ ensureBufferCapacity(len + otherLen);
+ int i = 0, j = 0, k = 0;
+ int a = list[i++];
+ int b = other[j++];
+ // change from xor is that we have to check overlapping pairs
+ // polarity bit 1 means a is second, bit 2 means b is.
+ main:
+ while (true) {
+ switch (polarity) {
+ case 0: // both first; take lower if unequal
+ if (a < b) { // take a
+ // Back up over overlapping ranges in buffer[]
+ if (k > 0 && a <= buffer[k-1]) {
+ // Pick latter end value in buffer[] vs. list[]
+ a = max(list[i], buffer[--k]);
+ } else {
+ // No overlap
+ buffer[k++] = a;
+ a = list[i];
+ }
+ i++; // Common if/else code factored out
+ polarity ^= 1;
+ } else if (b < a) { // take b
+ if (k > 0 && b <= buffer[k-1]) {
+ b = max(other[j], buffer[--k]);
+ } else {
+ buffer[k++] = b;
+ b = other[j];
+ }
+ j++;
+ polarity ^= 2;
+ } else { // a == b, take a, drop b
+ if (a == HIGH) break main;
+ // This is symmetrical; it doesn't matter if
+ // we backtrack with a or b. - liu
+ if (k > 0 && a <= buffer[k-1]) {
+ a = max(list[i], buffer[--k]);
+ } else {
+ // No overlap
+ buffer[k++] = a;
+ a = list[i];
+ }
+ i++;
+ polarity ^= 1;
+ b = other[j++]; polarity ^= 2;
+ }
+ break;
+ case 3: // both second; take higher if unequal, and drop other
+ if (b <= a) { // take a
+ if (a == HIGH) break main;
+ buffer[k++] = a;
+ } else { // take b
+ if (b == HIGH) break main;
+ buffer[k++] = b;
+ }
+ a = list[i++]; polarity ^= 1; // factored common code
+ b = other[j++]; polarity ^= 2;
+ break;
+ case 1: // a second, b first; if b < a, overlap
+ if (a < b) { // no overlap, take a
+ buffer[k++] = a; a = list[i++]; polarity ^= 1;
+ } else if (b < a) { // OVERLAP, drop b
+ b = other[j++]; polarity ^= 2;
+ } else { // a == b, drop both!
+ if (a == HIGH) break main;
+ a = list[i++]; polarity ^= 1;
+ b = other[j++]; polarity ^= 2;
+ }
+ break;
+ case 2: // a first, b second; if a < b, overlap
+ if (b < a) { // no overlap, take b
+ buffer[k++] = b; b = other[j++]; polarity ^= 2;
+ } else if (a < b) { // OVERLAP, drop a
+ a = list[i++]; polarity ^= 1;
+ } else { // a == b, drop both!
+ if (a == HIGH) break main;
+ a = list[i++]; polarity ^= 1;
+ b = other[j++]; polarity ^= 2;
+ }
+ break;
+ }
+ }
+ buffer[k++] = HIGH; // terminate
+ len = k;
+ // swap list and buffer
+ int[] temp = list;
+ list = buffer;
+ buffer = temp;
+ pat = null;
+ return this;
+ }
+
+ // polarity = 0 is normal: x intersect y
+ // polarity = 2: x intersect ~y == set-minus
+ // polarity = 1: ~x intersect y
+ // polarity = 3: ~x intersect ~y
+
+ private UnicodeSet retain(int[] other, int otherLen, int polarity) {
+ ensureBufferCapacity(len + otherLen);
+ int i = 0, j = 0, k = 0;
+ int a = list[i++];
+ int b = other[j++];
+ // change from xor is that we have to check overlapping pairs
+ // polarity bit 1 means a is second, bit 2 means b is.
+ main:
+ while (true) {
+ switch (polarity) {
+ case 0: // both first; drop the smaller
+ if (a < b) { // drop a
+ a = list[i++]; polarity ^= 1;
+ } else if (b < a) { // drop b
+ b = other[j++]; polarity ^= 2;
+ } else { // a == b, take one, drop other
+ if (a == HIGH) break main;
+ buffer[k++] = a; a = list[i++]; polarity ^= 1;
+ b = other[j++]; polarity ^= 2;
+ }
+ break;
+ case 3: // both second; take lower if unequal
+ if (a < b) { // take a
+ buffer[k++] = a; a = list[i++]; polarity ^= 1;
+ } else if (b < a) { // take b
+ buffer[k++] = b; b = other[j++]; polarity ^= 2;
+ } else { // a == b, take one, drop other
+ if (a == HIGH) break main;
+ buffer[k++] = a; a = list[i++]; polarity ^= 1;
+ b = other[j++]; polarity ^= 2;
+ }
+ break;
+ case 1: // a second, b first;
+ if (a < b) { // NO OVERLAP, drop a
+ a = list[i++]; polarity ^= 1;
+ } else if (b < a) { // OVERLAP, take b
+ buffer[k++] = b; b = other[j++]; polarity ^= 2;
+ } else { // a == b, drop both!
+ if (a == HIGH) break main;
+ a = list[i++]; polarity ^= 1;
+ b = other[j++]; polarity ^= 2;
+ }
+ break;
+ case 2: // a first, b second; if a < b, overlap
+ if (b < a) { // no overlap, drop b
+ b = other[j++]; polarity ^= 2;
+ } else if (a < b) { // OVERLAP, take a
+ buffer[k++] = a; a = list[i++]; polarity ^= 1;
+ } else { // a == b, drop both!
+ if (a == HIGH) break main;
+ a = list[i++]; polarity ^= 1;
+ b = other[j++]; polarity ^= 2;
+ }
+ break;
+ }
+ }
+ buffer[k++] = HIGH; // terminate
+ len = k;
+ // swap list and buffer
+ int[] temp = list;
+ list = buffer;
+ buffer = temp;
+ pat = null;
+ return this;
+ }
+
+ private static final int max(int a, int b) {
+ return (a > b) ? a : b;
+ }
+
+ //----------------------------------------------------------------
+ // Generic filter-based scanning code
+ //----------------------------------------------------------------
+
+ private static interface Filter {
+ boolean contains(int codePoint);
+ }
+
+ private static class NumericValueFilter implements Filter {
+ double value;
+ NumericValueFilter(double value) { this.value = value; }
+ public boolean contains(int ch) {
+ return UCharacter.getUnicodeNumericValue(ch) == value;
+ }
+ }
+
+ private static class GeneralCategoryMaskFilter implements Filter {
+ int mask;
+ GeneralCategoryMaskFilter(int mask) { this.mask = mask; }
+ public boolean contains(int ch) {
+ return ((1 << UCharacter.getType(ch)) & mask) != 0;
+ }
+ }
+
+ private static class IntPropertyFilter implements Filter {
+ int prop;
+ int value;
+ IntPropertyFilter(int prop, int value) {
+ this.prop = prop;
+ this.value = value;
+ }
+ public boolean contains(int ch) {
+ return UCharacter.getIntPropertyValue(ch, prop) == value;
+ }
+ }
+
+ // VersionInfo for unassigned characters
+ static final VersionInfo NO_VERSION = VersionInfo.getInstance(0, 0, 0, 0);
+
+ private static class VersionFilter implements Filter {
+ VersionInfo version;
+ VersionFilter(VersionInfo version) { this.version = version; }
+ public boolean contains(int ch) {
+ VersionInfo v = UCharacter.getAge(ch);
+ // Reference comparison ok; VersionInfo caches and reuses
+ // unique objects.
+ return v != NO_VERSION &&
+ v.compareTo(version) <= 0;
+ }
+ }
+
+ private static synchronized UnicodeSet getInclusions(int src) {
+ if (INCLUSIONS == null) {
+ INCLUSIONS = new UnicodeSet[UCharacterProperty.SRC_COUNT];
+ }
+ if(INCLUSIONS[src] == null) {
+ UnicodeSet incl = new UnicodeSet();
+ switch(src) {
+ case UCharacterProperty.SRC_CHAR:
+ UCharacterProperty.getInstance().addPropertyStarts(incl);
+ break;
+ case UCharacterProperty.SRC_PROPSVEC:
+ UCharacterProperty.getInstance().upropsvec_addPropertyStarts(incl);
+ break;
+ case UCharacterProperty.SRC_CHAR_AND_PROPSVEC:
+ UCharacterProperty.getInstance().addPropertyStarts(incl);
+ UCharacterProperty.getInstance().upropsvec_addPropertyStarts(incl);
+ break;
+ case UCharacterProperty.SRC_HST:
+ UCharacterProperty.getInstance().uhst_addPropertyStarts(incl);
+ break;
+ case UCharacterProperty.SRC_NORM:
+ NormalizerImpl.addPropertyStarts(incl);
+ break;
+ case UCharacterProperty.SRC_CASE:
+ try {
+ UCaseProps.getSingleton().addPropertyStarts(incl);
+ } catch(IOException e) {
+ throw new MissingResourceException(e.getMessage(),"","");
+ }
+ break;
+ case UCharacterProperty.SRC_BIDI:
+ try {
+ UBiDiProps.getSingleton().addPropertyStarts(incl);
+ } catch(IOException e) {
+ throw new MissingResourceException(e.getMessage(),"","");
+ }
+ break;
+ default:
+ throw new IllegalStateException("UnicodeSet.getInclusions(unknown src "+src+")");
+ }
+ INCLUSIONS[src] = incl;
+ }
+ return INCLUSIONS[src];
+ }
+
+ /**
+ * Generic filter-based scanning code for UCD property UnicodeSets.
+ */
+ private UnicodeSet applyFilter(Filter filter, int src) {
+ // Walk through all Unicode characters, noting the start
+ // and end of each range for which filter.contain(c) is
+ // true. Add each range to a set.
+ //
+ // To improve performance, use the INCLUSIONS set, which
+ // encodes information about character ranges that are known
+ // to have identical properties, such as the CJK Ideographs
+ // from U+4E00 to U+9FA5. INCLUSIONS contains all characters
+ // except the first characters of such ranges.
+ //
+ // TODO Where possible, instead of scanning over code points,
+ // use internal property data to initialize UnicodeSets for
+ // those properties. Scanning code points is slow.
+
+ clear();
+
+ int startHasProperty = -1;
+ UnicodeSet inclusions = getInclusions(src);
+ int limitRange = inclusions.getRangeCount();
+
+ for (int j=0; j<limitRange; ++j) {
+ // get current range
+ int start = inclusions.getRangeStart(j);
+ int end = inclusions.getRangeEnd(j);
+
+ // for all the code points in the range, process
+ for (int ch = start; ch <= end; ++ch) {
+ // only add to the unicodeset on inflection points --
+ // where the hasProperty value changes to false
+ if (filter.contains(ch)) {
+ if (startHasProperty < 0) {
+ startHasProperty = ch;
+ }
+ } else if (startHasProperty >= 0) {
+ add_unchecked(startHasProperty, ch-1);
+ startHasProperty = -1;
+ }
+ }
+ }
+ if (startHasProperty >= 0) {
+ add_unchecked(startHasProperty, 0x10FFFF);
+ }
+
+ return this;
+ }
+
+
+ /**
+ * Remove leading and trailing rule white space and compress
+ * internal rule white space to a single space character.
+ *
+ * @see UCharacterProperty#isRuleWhiteSpace
+ */
+ private static String mungeCharName(String source) {
+ StringBuffer buf = new StringBuffer();
+ for (int i=0; i<source.length(); ) {
+ int ch = UTF16.charAt(source, i);
+ i += UTF16.getCharCount(ch);
+ if (UCharacterProperty.isRuleWhiteSpace(ch)) {
+ if (buf.length() == 0 ||
+ buf.charAt(buf.length() - 1) == ' ') {
+ continue;
+ }
+ ch = ' '; // convert to ' '
+ }
+ UTF16.append(buf, ch);
+ }
+ if (buf.length() != 0 &&
+ buf.charAt(buf.length() - 1) == ' ') {
+ buf.setLength(buf.length() - 1);
+ }
+ return buf.toString();
+ }
+
+ //----------------------------------------------------------------
+ // Property set API
+ //----------------------------------------------------------------
+
+ /**
+ * Modifies this set to contain those code points which have the
+ * given value for the given binary or enumerated property, as
+ * returned by UCharacter.getIntPropertyValue. Prior contents of
+ * this set are lost.
+ *
+ * @param prop a property in the range
+ * UProperty.BIN_START..UProperty.BIN_LIMIT-1 or
+ * UProperty.INT_START..UProperty.INT_LIMIT-1 or.
+ * UProperty.MASK_START..UProperty.MASK_LIMIT-1.
+ *
+ * @param value a value in the range
+ * UCharacter.getIntPropertyMinValue(prop)..
+ * UCharacter.getIntPropertyMaxValue(prop), with one exception.
+ * If prop is UProperty.GENERAL_CATEGORY_MASK, then value should not be
+ * a UCharacter.getType() result, but rather a mask value produced
+ * by logically ORing (1 << UCharacter.getType()) values together.
+ * This allows grouped categories such as [:L:] to be represented.
+ *
+ * @return a reference to this set
+ *
+ * @stable ICU 2.4
+ */
+ public UnicodeSet applyIntPropertyValue(int prop, int value) {
+ checkFrozen();
+ if (prop == UProperty.GENERAL_CATEGORY_MASK) {
+ applyFilter(new GeneralCategoryMaskFilter(value), UCharacterProperty.SRC_CHAR);
+ } else {
+ applyFilter(new IntPropertyFilter(prop, value), UCharacterProperty.getInstance().getSource(prop));
+ }
+ return this;
+ }
+
+
+
+ /**
+ * Modifies this set to contain those code points which have the
+ * given value for the given property. Prior contents of this
+ * set are lost.
+ *
+ * @param propertyAlias a property alias, either short or long.
+ * The name is matched loosely. See PropertyAliases.txt for names
+ * and a description of loose matching. If the value string is
+ * empty, then this string is interpreted as either a
+ * General_Category value alias, a Script value alias, a binary
+ * property alias, or a special ID. Special IDs are matched
+ * loosely and correspond to the following sets:
+ *
+ * "ANY" = [\u0000-\U0010FFFF],
+ * "ASCII" = [\u0000-\u007F].
+ *
+ * @param valueAlias a value alias, either short or long. The
+ * name is matched loosely. See PropertyValueAliases.txt for
+ * names and a description of loose matching. In addition to
+ * aliases listed, numeric values and canonical combining classes
+ * may be expressed numerically, e.g., ("nv", "0.5") or ("ccc",
+ * "220"). The value string may also be empty.
+ *
+ * @return a reference to this set
+ *
+ * @stable ICU 2.4
+ */
+ public UnicodeSet applyPropertyAlias(String propertyAlias, String valueAlias) {
+ return applyPropertyAlias(propertyAlias, valueAlias, null);
+ }
+
+ /**
+ * Modifies this set to contain those code points which have the
+ * given value for the given property. Prior contents of this
+ * set are lost.
+ * @param propertyAlias
+ * @param valueAlias
+ * @param symbols if not null, then symbols are first called to see if a property
+ * is available. If true, then everything else is skipped.
+ * @return this set
+ * @stable ICU 3.2
+ */
+ public UnicodeSet applyPropertyAlias(String propertyAlias,
+ String valueAlias, SymbolTable symbols) {
+ checkFrozen();
+ int p;
+ int v;
+ boolean mustNotBeEmpty = false, invert = false;
+
+ if (symbols != null
+ && (symbols instanceof XSymbolTable)
+ && ((XSymbolTable)symbols).applyPropertyAlias(propertyAlias, valueAlias, this)) {
+ return this;
+ }
+
+ if (valueAlias.length() > 0) {
+ p = UCharacter.getPropertyEnum(propertyAlias);
+
+ // Treat gc as gcm
+ if (p == UProperty.GENERAL_CATEGORY) {
+ p = UProperty.GENERAL_CATEGORY_MASK;
+ }
+
+ if ((p >= UProperty.BINARY_START && p < UProperty.BINARY_LIMIT) ||
+ (p >= UProperty.INT_START && p < UProperty.INT_LIMIT) ||
+ (p >= UProperty.MASK_START && p < UProperty.MASK_LIMIT)) {
+ try {
+ v = UCharacter.getPropertyValueEnum(p, valueAlias);
+ } catch (IllegalArgumentException e) {
+ // Handle numeric CCC
+ if (p == UProperty.CANONICAL_COMBINING_CLASS ||
+ p == UProperty.LEAD_CANONICAL_COMBINING_CLASS ||
+ p == UProperty.TRAIL_CANONICAL_COMBINING_CLASS) {
+ v = Integer.parseInt(Utility.deleteRuleWhiteSpace(valueAlias));
+ // If the resultant set is empty then the numeric value
+ // was invalid.
+ //mustNotBeEmpty = true;
+ // old code was wrong; anything between 0 and 255 is valid even if unused.
+ if (v < 0 || v > 255) throw e;
+ } else {
+ throw e;
+ }
+ }
+ }
+
+ else {
+
+ switch (p) {
+ case UProperty.NUMERIC_VALUE:
+ {
+ double value = Double.parseDouble(Utility.deleteRuleWhiteSpace(valueAlias));
+ applyFilter(new NumericValueFilter(value), UCharacterProperty.SRC_CHAR);
+ return this;
+ }
+ case UProperty.NAME:
+ case UProperty.UNICODE_1_NAME:
+ {
+ // Must munge name, since
+ // UCharacter.charFromName() does not do
+ // 'loose' matching.
+ String buf = mungeCharName(valueAlias);
+ int ch =
+ (p == UProperty.NAME) ?
+ UCharacter.getCharFromExtendedName(buf) :
+ UCharacter.getCharFromName1_0(buf);
+ if (ch == -1) {
+ throw new IllegalArgumentException("Invalid character name");
+ }
+ clear();
+ add_unchecked(ch);
+ return this;
+ }
+ case UProperty.AGE:
+ {
+ // Must munge name, since
+ // VersionInfo.getInstance() does not do
+ // 'loose' matching.
+ VersionInfo version = VersionInfo.getInstance(mungeCharName(valueAlias));
+ applyFilter(new VersionFilter(version), UCharacterProperty.SRC_PROPSVEC);
+ return this;
+ }
+ }
+
+ // p is a non-binary, non-enumerated property that we
+ // don't support (yet).
+ throw new IllegalArgumentException("Unsupported property");
+ }
+ }
+
+ else {
+ // valueAlias is empty. Interpret as General Category, Script,
+ // Binary property, or ANY or ASCII. Upon success, p and v will
+ // be set.
+ try {
+ p = UProperty.GENERAL_CATEGORY_MASK;
+ v = UCharacter.getPropertyValueEnum(p, propertyAlias);
+ } catch (IllegalArgumentException e) {
+ try {
+ p = UProperty.SCRIPT;
+ v = UCharacter.getPropertyValueEnum(p, propertyAlias);
+ } catch (IllegalArgumentException e2) {
+ try {
+ p = UCharacter.getPropertyEnum(propertyAlias);
+ } catch (IllegalArgumentException e3) {
+ p = -1;
+ }
+ if (p >= UProperty.BINARY_START && p < UProperty.BINARY_LIMIT) {
+ v = 1;
+ } else if (p == -1) {
+ if (0 == UPropertyAliases.compare(ANY_ID, propertyAlias)) {
+ set(MIN_VALUE, MAX_VALUE);
+ return this;
+ } else if (0 == UPropertyAliases.compare(ASCII_ID, propertyAlias)) {
+ set(0, 0x7F);
+ return this;
+ } else if (0 == UPropertyAliases.compare(ASSIGNED, propertyAlias)) {
+ // [:Assigned:]=[:^Cn:]
+ p = UProperty.GENERAL_CATEGORY_MASK;
+ v = (1<<UCharacter.UNASSIGNED);
+ invert = true;
+ } else {
+ // Property name was never matched.
+ throw new IllegalArgumentException("Invalid property alias: " + propertyAlias + "=" + valueAlias);
+ }
+ } else {
+ // Valid propery name, but it isn't binary, so the value
+ // must be supplied.
+ throw new IllegalArgumentException("Missing property value");
+ }
+ }
+ }
+ }
+
+ applyIntPropertyValue(p, v);
+ if(invert) {
+ complement();
+ }
+
+ if (mustNotBeEmpty && isEmpty()) {
+ // mustNotBeEmpty is set to true if an empty set indicates
+ // invalid input.
+ throw new IllegalArgumentException("Invalid property value");
+ }
+
+ return this;
+ }
+
+ //----------------------------------------------------------------
+ // Property set patterns
+ //----------------------------------------------------------------
+
+ /**
+ * Return true if the given position, in the given pattern, appears
+ * to be the start of a property set pattern.
+ */
+ private static boolean resemblesPropertyPattern(String pattern, int pos) {
+ // Patterns are at least 5 characters long
+ if ((pos+5) > pattern.length()) {
+ return false;
+ }
+
+ // Look for an opening [:, [:^, \p, or \P
+ return pattern.regionMatches(pos, "[:", 0, 2) ||
+ pattern.regionMatches(true, pos, "\\p", 0, 2) ||
+ pattern.regionMatches(pos, "\\N", 0, 2);
+ }
+
+ /**
+ * Return true if the given iterator appears to point at a
+ * property pattern. Regardless of the result, return with the
+ * iterator unchanged.
+ * @param chars iterator over the pattern characters. Upon return
+ * it will be unchanged.
+ * @param iterOpts RuleCharacterIterator options
+ */
+ private static boolean resemblesPropertyPattern(RuleCharacterIterator chars,
+ int iterOpts) {
+ boolean result = false;
+ iterOpts &= ~RuleCharacterIterator.PARSE_ESCAPES;
+ Object pos = chars.getPos(null);
+ int c = chars.next(iterOpts);
+ if (c == '[' || c == '\\') {
+ int d = chars.next(iterOpts & ~RuleCharacterIterator.SKIP_WHITESPACE);
+ result = (c == '[') ? (d == ':') :
+ (d == 'N' || d == 'p' || d == 'P');
+ }
+ chars.setPos(pos);
+ return result;
+ }
+
+ /**
+ * Parse the given property pattern at the given parse position.
+ * @param symbols TODO
+ */
+ private UnicodeSet applyPropertyPattern(String pattern, ParsePosition ppos, SymbolTable symbols) {
+ int pos = ppos.getIndex();
+
+ // On entry, ppos should point to one of the following locations:
+
+ // Minimum length is 5 characters, e.g. \p{L}
+ if ((pos+5) > pattern.length()) {
+ return null;
+ }
+
+ boolean posix = false; // true for [:pat:], false for \p{pat} \P{pat} \N{pat}
+ boolean isName = false; // true for \N{pat}, o/w false
+ boolean invert = false;
+
+ // Look for an opening [:, [:^, \p, or \P
+ if (pattern.regionMatches(pos, "[:", 0, 2)) {
+ posix = true;
+ pos = Utility.skipWhitespace(pattern, pos+2);
+ if (pos < pattern.length() && pattern.charAt(pos) == '^') {
+ ++pos;
+ invert = true;
+ }
+ } else if (pattern.regionMatches(true, pos, "\\p", 0, 2) ||
+ pattern.regionMatches(pos, "\\N", 0, 2)) {
+ char c = pattern.charAt(pos+1);
+ invert = (c == 'P');
+ isName = (c == 'N');
+ pos = Utility.skipWhitespace(pattern, pos+2);
+ if (pos == pattern.length() || pattern.charAt(pos++) != '{') {
+ // Syntax error; "\p" or "\P" not followed by "{"
+ return null;
+ }
+ } else {
+ // Open delimiter not seen
+ return null;
+ }
+
+ // Look for the matching close delimiter, either :] or }
+ int close = pattern.indexOf(posix ? ":]" : "}", pos);
+ if (close < 0) {
+ // Syntax error; close delimiter missing
+ return null;
+ }
+
+ // Look for an '=' sign. If this is present, we will parse a
+ // medium \p{gc=Cf} or long \p{GeneralCategory=Format}
+ // pattern.
+ int equals = pattern.indexOf('=', pos);
+ String propName, valueName;
+ if (equals >= 0 && equals < close && !isName) {
+ // Equals seen; parse medium/long pattern
+ propName = pattern.substring(pos, equals);
+ valueName = pattern.substring(equals+1, close);
+ }
+
+ else {
+ // Handle case where no '=' is seen, and \N{}
+ propName = pattern.substring(pos, close);
+ valueName = "";
+
+ // Handle \N{name}
+ if (isName) {
+ // This is a little inefficient since it means we have to
+ // parse "na" back to UProperty.NAME even though we already
+ // know it's UProperty.NAME. If we refactor the API to
+ // support args of (int, String) then we can remove
+ // "na" and make this a little more efficient.
+ valueName = propName;
+ propName = "na";
+ }
+ }
+
+ applyPropertyAlias(propName, valueName, symbols);
+
+ if (invert) {
+ complement();
+ }
+
+ // Move to the limit position after the close delimiter
+ ppos.setIndex(close + (posix ? 2 : 1));
+
+ return this;
+ }
+
+ /**
+ * Parse a property pattern.
+ * @param chars iterator over the pattern characters. Upon return
+ * it will be advanced to the first character after the parsed
+ * pattern, or the end of the iteration if all characters are
+ * parsed.
+ * @param rebuiltPat the pattern that was parsed, rebuilt or
+ * copied from the input pattern, as appropriate.
+ * @param symbols TODO
+ */
+ private void applyPropertyPattern(RuleCharacterIterator chars,
+ StringBuffer rebuiltPat, SymbolTable symbols) {
+ String patStr = chars.lookahead();
+ ParsePosition pos = new ParsePosition(0);
+ applyPropertyPattern(patStr, pos, symbols);
+ if (pos.getIndex() == 0) {
+ syntaxError(chars, "Invalid property pattern");
+ }
+ chars.jumpahead(pos.getIndex());
+ rebuiltPat.append(patStr.substring(0, pos.getIndex()));
+ }
+
+ //----------------------------------------------------------------
+ // Case folding API
+ //----------------------------------------------------------------
+
+ /**
+ * Bitmask for constructor and applyPattern() indicating that
+ * white space should be ignored. If set, ignore characters for
+ * which UCharacterProperty.isRuleWhiteSpace() returns true,
+ * unless they are quoted or escaped. This may be ORed together
+ * with other selectors.
+ * @stable ICU 3.8
+ */
+ public static final int IGNORE_SPACE = 1;
+
+ /**
+ * Bitmask for constructor, applyPattern(), and closeOver()
+ * indicating letter case. This may be ORed together with other
+ * selectors.
+ *
+ * Enable case insensitive matching. E.g., "[ab]" with this flag
+ * will match 'a', 'A', 'b', and 'B'. "[^ab]" with this flag will
+ * match all except 'a', 'A', 'b', and 'B'. This performs a full
+ * closure over case mappings, e.g. U+017F for s.
+ *
+ * The resulting set is a superset of the input for the code points but
+ * not for the strings.
+ * It performs a case mapping closure of the code points and adds
+ * full case folding strings for the code points, and reduces strings of
+ * the original set to their full case folding equivalents.
+ *
+ * This is designed for case-insensitive matches, for example
+ * in regular expressions. The full code point case closure allows checking of
+ * an input character directly against the closure set.
+ * Strings are matched by comparing the case-folded form from the closure
+ * set with an incremental case folding of the string in question.
+ *
+ * The closure set will also contain single code points if the original
+ * set contained case-equivalent strings (like U+00DF for "ss" or "Ss" etc.).
+ * This is not necessary (that is, redundant) for the above matching method
+ * but results in the same closure sets regardless of whether the original
+ * set contained the code point or a string.
+ * @stable ICU 3.8
+ */
+ public static final int CASE = 2;
+
+ /**
+ * Alias for UnicodeSet.CASE, for ease of porting from C++ where ICU4C
+ * also has both USET_CASE and USET_CASE_INSENSITIVE (see uset.h).
+ * @see #CASE
+ * @stable ICU 3.4
+ */
+ public static final int CASE_INSENSITIVE = 2;
+
+ /**
+ * Bitmask for constructor, applyPattern(), and closeOver()
+ * indicating letter case. This may be ORed together with other
+ * selectors.
+ *
+ * Enable case insensitive matching. E.g., "[ab]" with this flag
+ * will match 'a', 'A', 'b', and 'B'. "[^ab]" with this flag will
+ * match all except 'a', 'A', 'b', and 'B'. This adds the lower-,
+ * title-, and uppercase mappings as well as the case folding
+ * of each existing element in the set.
+ * @stable ICU 3.4
+ */
+ public static final int ADD_CASE_MAPPINGS = 4;
+
+ // add the result of a full case mapping to the set
+ // use str as a temporary string to avoid constructing one
+ private static final void addCaseMapping(UnicodeSet set, int result, StringBuffer full) {
+ if(result >= 0) {
+ if(result > UCaseProps.MAX_STRING_LENGTH) {
+ // add a single-code point case mapping
+ set.add(result);
+ } else {
+ // add a string case mapping from full with length result
+ set.add(full.toString());
+ full.setLength(0);
+ }
+ }
+ // result < 0: the code point mapped to itself, no need to add it
+ // see UCaseProps
+ }
+
+ /**
+ * Close this set over the given attribute. For the attribute
+ * CASE, the result is to modify this set so that:
+ *
+ * 1. For each character or string 'a' in this set, all strings
+ * 'b' such that foldCase(a) == foldCase(b) are added to this set.
+ * (For most 'a' that are single characters, 'b' will have
+ * b.length() == 1.)
+ *
+ * 2. For each string 'e' in the resulting set, if e !=
+ * foldCase(e), 'e' will be removed.
+ *
+ * Example: [aq\u00DF{Bc}{bC}{Fi}] => [aAqQ\u00DF\uFB01{ss}{bc}{fi}]
+ *
+ * (Here foldCase(x) refers to the operation
+ * UCharacter.foldCase(x, true), and a == b actually denotes
+ * a.equals(b), not pointer comparison.)
+ *
+ * @param attribute bitmask for attributes to close over.
+ * Currently only the CASE bit is supported. Any undefined bits
+ * are ignored.
+ * @return a reference to this set.
+ * @stable ICU 3.8
+ */
+ public UnicodeSet closeOver(int attribute) {
+ checkFrozen();
+ if ((attribute & (CASE | ADD_CASE_MAPPINGS)) != 0) {
+ UCaseProps csp;
+ try {
+ csp = UCaseProps.getSingleton();
+ } catch(IOException e) {
+ return this;
+ }
+ UnicodeSet foldSet = new UnicodeSet(this);
+ ULocale root = ULocale.ROOT;
+
+ // start with input set to guarantee inclusion
+ // CASE: remove strings because the strings will actually be reduced (folded);
+ // therefore, start with no strings and add only those needed
+ if((attribute & CASE) != 0) {
+ foldSet.strings.clear();
+ }
+
+ int n = getRangeCount();
+ int result;
+ StringBuffer full = new StringBuffer();
+ int locCache[] = new int[1];
+
+ for (int i=0; i<n; ++i) {
+ int start = getRangeStart(i);
+ int end = getRangeEnd(i);
+
+ if((attribute & CASE) != 0) {
+ // full case closure
+ for (int cp=start; cp<=end; ++cp) {
+ csp.addCaseClosure(cp, foldSet);
+ }
+ } else {
+ // add case mappings
+ // (does not add long s for regular s, or Kelvin for k, for example)
+ for (int cp=start; cp<=end; ++cp) {
+ result = csp.toFullLower(cp, null, full, root, locCache);
+ addCaseMapping(foldSet, result, full);
+
+ result = csp.toFullTitle(cp, null, full, root, locCache);
+ addCaseMapping(foldSet, result, full);
+
+ result = csp.toFullUpper(cp, null, full, root, locCache);
+ addCaseMapping(foldSet, result, full);
+
+ result = csp.toFullFolding(cp, full, 0);
+ addCaseMapping(foldSet, result, full);
+ }
+ }
+ }
+ if (!strings.isEmpty()) {
+ String str;
+ if ((attribute & CASE) != 0) {
+ Iterator it = strings.iterator();
+ while (it.hasNext()) {
+ str = UCharacter.foldCase((String)it.next(), 0);
+ if(!csp.addStringCaseClosure(str, foldSet)) {
+ foldSet.add(str); // does not map to code points: add the folded string itself
+ }
+ }
+ } else {
+ BreakIterator bi = BreakIterator.getWordInstance(root);
+ Iterator it = strings.iterator();
+ while (it.hasNext()) {
+ str = (String)it.next();
+ foldSet.add(UCharacter.toLowerCase(root, str));
+ foldSet.add(UCharacter.toTitleCase(root, str, bi));
+ foldSet.add(UCharacter.toUpperCase(root, str));
+ foldSet.add(UCharacter.foldCase(str, 0));
+ }
+ }
+ }
+ set(foldSet);
+ }
+ return this;
+ }
+
+ /**
+ * Internal class for customizing UnicodeSet parsing of properties.
+ * TODO: extend to allow customizing of codepoint ranges
+ * @draft ICU3.8
+ * @provisional This API might change or be removed in a future release.
+ * @author medavis
+ */
+ abstract public static class XSymbolTable implements SymbolTable {
+ /**
+ * Default constructor
+ * @draft ICU3.8
+ * @provisional This API might change or be removed in a future release.
+ */
+ public XSymbolTable(){}
+ /**
+ * Supplies default implementation for SymbolTable (no action).
+ * @draft ICU3.8
+ * @provisional This API might change or be removed in a future release.
+ */
+ public UnicodeMatcher lookupMatcher(int i) {
+ return null;
+ }
+ /**
+ * Apply a new property alias. Is called when parsing [:xxx=yyy:]. Results are to put into result.
+ * @param propertyName the xxx in [:xxx=yyy:]
+ * @param propertyValue the yyy in [:xxx=yyy:]
+ * @param result where the result is placed
+ * @return true if handled
+ * @draft ICU3.8
+ * @provisional This API might change or be removed in a future release.
+ */
+ public boolean applyPropertyAlias(String propertyName, String propertyValue, UnicodeSet result) {
+ return false;
+ }
+ /**
+ * Supplies default implementation for SymbolTable (no action).
+ * @draft ICU3.8
+ * @provisional This API might change or be removed in a future release.
+ */
+ public char[] lookup(String s) {
+ return null;
+ }
+ /**
+ * Supplies default implementation for SymbolTable (no action).
+ * @draft ICU3.8
+ * @provisional This API might change or be removed in a future release.
+ */
+ public String parseReference(String text, ParsePosition pos, int limit) {
+ return null;
+ }
+ }
+
+ private boolean frozen;
+
+ /**
+ * Is this frozen, according to the Freezable interface?
+ * @return value
+ * @stable ICU 3.8
+ */
+ public boolean isFrozen() {
+ return frozen;
+ }
+
+ /**
+ * Freeze this class, according to the Freezable interface.
+ * @return this
+ * @stable ICU 3.8
+ */
+ public Object freeze() {
+ frozen = true;
+ return this;
+ }
+
+ /**
+ * Clone a thawed version of this class, according to the Freezable interface.
+ * @return this
+ * @stable ICU 3.8
+ */
+ public Object cloneAsThawed() {
+ UnicodeSet result = (UnicodeSet) clone();
+ result.frozen = false;
+ return result;
+ }
+
+ // internal function
+ private void checkFrozen() {
+ if (frozen) {
+ throw new UnsupportedOperationException("Attempt to modify frozen object");
+ }
+ }
+}
+//eof