0N/A/*
3909N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.util.regex;
0N/A
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.text.CharacterIterator;
0N/Aimport java.text.Normalizer;
2401N/Aimport java.util.Locale;
906N/Aimport java.util.Map;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Arrays;
0N/A
0N/A
0N/A/**
0N/A * A compiled representation of a regular expression.
0N/A *
0N/A * <p> A regular expression, specified as a string, must first be compiled into
0N/A * an instance of this class. The resulting pattern can then be used to create
0N/A * a {@link Matcher} object that can match arbitrary {@link
0N/A * java.lang.CharSequence </code>character sequences<code>} against the regular
0N/A * expression. All of the state involved in performing a match resides in the
0N/A * matcher, so many matchers can share the same pattern.
0N/A *
0N/A * <p> A typical invocation sequence is thus
0N/A *
0N/A * <blockquote><pre>
0N/A * Pattern p = Pattern.{@link #compile compile}("a*b");
0N/A * Matcher m = p.{@link #matcher matcher}("aaaaab");
0N/A * boolean b = m.{@link Matcher#matches matches}();</pre></blockquote>
0N/A *
0N/A * <p> A {@link #matches matches} method is defined by this class as a
0N/A * convenience for when a regular expression is used just once. This method
0N/A * compiles an expression and matches an input sequence against it in a single
0N/A * invocation. The statement
0N/A *
0N/A * <blockquote><pre>
0N/A * boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote>
0N/A *
0N/A * is equivalent to the three statements above, though for repeated matches it
0N/A * is less efficient since it does not allow the compiled pattern to be reused.
0N/A *
0N/A * <p> Instances of this class are immutable and are safe for use by multiple
0N/A * concurrent threads. Instances of the {@link Matcher} class are not safe for
0N/A * such use.
0N/A *
0N/A *
0N/A * <a name="sum">
0N/A * <h4> Summary of regular-expression constructs </h4>
0N/A *
0N/A * <table border="0" cellpadding="1" cellspacing="0"
0N/A * summary="Regular expression constructs, and what they match">
0N/A *
0N/A * <tr align="left">
0N/A * <th bgcolor="#CCCCFF" align="left" id="construct">Construct</th>
0N/A * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
0N/A * </tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2" id="characters">Characters</th></tr>
0N/A *
0N/A * <tr><td valign="top" headers="construct characters"><i>x</i></td>
0N/A * <td headers="matches">The character <i>x</i></td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\\</tt></td>
0N/A * <td headers="matches">The backslash character</td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>n</i></td>
0N/A * <td headers="matches">The character with octal value <tt>0</tt><i>n</i>
0N/A * (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>nn</i></td>
0N/A * <td headers="matches">The character with octal value <tt>0</tt><i>nn</i>
0N/A * (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>mnn</i></td>
0N/A * <td headers="matches">The character with octal value <tt>0</tt><i>mnn</i>
0N/A * (0&nbsp;<tt>&lt;=</tt>&nbsp;<i>m</i>&nbsp;<tt>&lt;=</tt>&nbsp;3,
0N/A * 0&nbsp;<tt>&lt;=</tt>&nbsp;<i>n</i>&nbsp;<tt>&lt;=</tt>&nbsp;7)</td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>hh</i></td>
0N/A * <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>hh</i></td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>&#92;u</tt><i>hhhh</i></td>
0N/A * <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>hhhh</i></td></tr>
3486N/A * <tr><td valign="top" headers="construct characters"><tt>&#92;x</tt><i>{h...h}</i></td>
3486N/A * <td headers="matches">The character with hexadecimal&nbsp;value&nbsp;<tt>0x</tt><i>h...h</i>
3486N/A * ({@link java.lang.Character#MIN_CODE_POINT Character.MIN_CODE_POINT}
3486N/A * &nbsp;&lt;=&nbsp;<tt>0x</tt><i>h...h</i>&nbsp;&lt;=&nbsp
3486N/A * {@link java.lang.Character#MAX_CODE_POINT Character.MAX_CODE_POINT})</td></tr>
0N/A * <tr><td valign="top" headers="matches"><tt>\t</tt></td>
0N/A * <td headers="matches">The tab character (<tt>'&#92;u0009'</tt>)</td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\n</tt></td>
0N/A * <td headers="matches">The newline (line feed) character (<tt>'&#92;u000A'</tt>)</td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\r</tt></td>
0N/A * <td headers="matches">The carriage-return character (<tt>'&#92;u000D'</tt>)</td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\f</tt></td>
0N/A * <td headers="matches">The form-feed character (<tt>'&#92;u000C'</tt>)</td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\a</tt></td>
0N/A * <td headers="matches">The alert (bell) character (<tt>'&#92;u0007'</tt>)</td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\e</tt></td>
0N/A * <td headers="matches">The escape character (<tt>'&#92;u001B'</tt>)</td></tr>
0N/A * <tr><td valign="top" headers="construct characters"><tt>\c</tt><i>x</i></td>
0N/A * <td headers="matches">The control character corresponding to <i>x</i></td></tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2" id="classes">Character classes</th></tr>
0N/A *
0N/A * <tr><td valign="top" headers="construct classes"><tt>[abc]</tt></td>
0N/A * <td headers="matches"><tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (simple class)</td></tr>
0N/A * <tr><td valign="top" headers="construct classes"><tt>[^abc]</tt></td>
0N/A * <td headers="matches">Any character except <tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (negation)</td></tr>
0N/A * <tr><td valign="top" headers="construct classes"><tt>[a-zA-Z]</tt></td>
0N/A * <td headers="matches"><tt>a</tt> through <tt>z</tt>
0N/A * or <tt>A</tt> through <tt>Z</tt>, inclusive (range)</td></tr>
0N/A * <tr><td valign="top" headers="construct classes"><tt>[a-d[m-p]]</tt></td>
0N/A * <td headers="matches"><tt>a</tt> through <tt>d</tt>,
0N/A * or <tt>m</tt> through <tt>p</tt>: <tt>[a-dm-p]</tt> (union)</td></tr>
0N/A * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[def]]</tt></td>
0N/A * <td headers="matches"><tt>d</tt>, <tt>e</tt>, or <tt>f</tt> (intersection)</tr>
0N/A * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^bc]]</tt></td>
0N/A * <td headers="matches"><tt>a</tt> through <tt>z</tt>,
0N/A * except for <tt>b</tt> and <tt>c</tt>: <tt>[ad-z]</tt> (subtraction)</td></tr>
0N/A * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^m-p]]</tt></td>
0N/A * <td headers="matches"><tt>a</tt> through <tt>z</tt>,
0N/A * and not <tt>m</tt> through <tt>p</tt>: <tt>[a-lq-z]</tt>(subtraction)</td></tr>
0N/A * <tr><th>&nbsp;</th></tr>
0N/A *
0N/A * <tr align="left"><th colspan="2" id="predef">Predefined character classes</th></tr>
0N/A *
0N/A * <tr><td valign="top" headers="construct predef"><tt>.</tt></td>
0N/A * <td headers="matches">Any character (may or may not match <a href="#lt">line terminators</a>)</td></tr>
0N/A * <tr><td valign="top" headers="construct predef"><tt>\d</tt></td>
0N/A * <td headers="matches">A digit: <tt>[0-9]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct predef"><tt>\D</tt></td>
0N/A * <td headers="matches">A non-digit: <tt>[^0-9]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct predef"><tt>\s</tt></td>
0N/A * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct predef"><tt>\S</tt></td>
0N/A * <td headers="matches">A non-whitespace character: <tt>[^\s]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct predef"><tt>\w</tt></td>
0N/A * <td headers="matches">A word character: <tt>[a-zA-Z_0-9]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct predef"><tt>\W</tt></td>
0N/A * <td headers="matches">A non-word character: <tt>[^\w]</tt></td></tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2" id="posix">POSIX character classes</b> (US-ASCII only)<b></th></tr>
0N/A *
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Lower}</tt></td>
0N/A * <td headers="matches">A lower-case alphabetic character: <tt>[a-z]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Upper}</tt></td>
0N/A * <td headers="matches">An upper-case alphabetic character:<tt>[A-Z]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{ASCII}</tt></td>
0N/A * <td headers="matches">All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Alpha}</tt></td>
0N/A * <td headers="matches">An alphabetic character:<tt>[\p{Lower}\p{Upper}]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Digit}</tt></td>
0N/A * <td headers="matches">A decimal digit: <tt>[0-9]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Alnum}</tt></td>
0N/A * <td headers="matches">An alphanumeric character:<tt>[\p{Alpha}\p{Digit}]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Punct}</tt></td>
0N/A * <td headers="matches">Punctuation: One of <tt>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</tt></td></tr>
0N/A * <!-- <tt>[\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]</tt>
0N/A * <tt>[\X21-\X2F\X31-\X40\X5B-\X60\X7B-\X7E]</tt> -->
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Graph}</tt></td>
0N/A * <td headers="matches">A visible character: <tt>[\p{Alnum}\p{Punct}]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Print}</tt></td>
0N/A * <td headers="matches">A printable character: <tt>[\p{Graph}\x20]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Blank}</tt></td>
0N/A * <td headers="matches">A space or a tab: <tt>[ \t]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Cntrl}</tt></td>
0N/A * <td headers="matches">A control character: <tt>[\x00-\x1F\x7F]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{XDigit}</tt></td>
0N/A * <td headers="matches">A hexadecimal digit: <tt>[0-9a-fA-F]</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct posix"><tt>\p{Space}</tt></td>
0N/A * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2">java.lang.Character classes (simple <a href="#jcc">java character type</a>)</th></tr>
0N/A *
0N/A * <tr><td valign="top"><tt>\p{javaLowerCase}</tt></td>
0N/A * <td>Equivalent to java.lang.Character.isLowerCase()</td></tr>
0N/A * <tr><td valign="top"><tt>\p{javaUpperCase}</tt></td>
0N/A * <td>Equivalent to java.lang.Character.isUpperCase()</td></tr>
0N/A * <tr><td valign="top"><tt>\p{javaWhitespace}</tt></td>
0N/A * <td>Equivalent to java.lang.Character.isWhitespace()</td></tr>
0N/A * <tr><td valign="top"><tt>\p{javaMirrored}</tt></td>
0N/A * <td>Equivalent to java.lang.Character.isMirrored()</td></tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
4139N/A * <tr align="left"><th colspan="2" id="unicode">Classes for Unicode scripts, blocks, categories and binary properties</th></tr>
2401N/A * * <tr><td valign="top" headers="construct unicode"><tt>\p{IsLatin}</tt></td>
4139N/A * <td headers="matches">A Latin&nbsp;script character (<a href="#usc">script</a>)</td></tr>
0N/A * <tr><td valign="top" headers="construct unicode"><tt>\p{InGreek}</tt></td>
4139N/A * <td headers="matches">A character in the Greek&nbsp;block (<a href="#ubc">block</a>)</td></tr>
0N/A * <tr><td valign="top" headers="construct unicode"><tt>\p{Lu}</tt></td>
4139N/A * <td headers="matches">An uppercase letter (<a href="#ucc">category</a>)</td></tr>
4231N/A * <tr><td valign="top" headers="construct unicode"><tt>\p{IsAlphabetic}</tt></td>
4139N/A * <td headers="matches">An alphabetic character (<a href="#ubpc">binary property</a>)</td></tr>
0N/A * <tr><td valign="top" headers="construct unicode"><tt>\p{Sc}</tt></td>
0N/A * <td headers="matches">A currency symbol</td></tr>
0N/A * <tr><td valign="top" headers="construct unicode"><tt>\P{InGreek}</tt></td>
0N/A * <td headers="matches">Any character except one in the Greek block (negation)</td></tr>
0N/A * <tr><td valign="top" headers="construct unicode"><tt>[\p{L}&&[^\p{Lu}]]&nbsp;</tt></td>
0N/A * <td headers="matches">Any letter except an uppercase letter (subtraction)</td></tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2" id="bounds">Boundary matchers</th></tr>
0N/A *
0N/A * <tr><td valign="top" headers="construct bounds"><tt>^</tt></td>
0N/A * <td headers="matches">The beginning of a line</td></tr>
0N/A * <tr><td valign="top" headers="construct bounds"><tt>$</tt></td>
0N/A * <td headers="matches">The end of a line</td></tr>
0N/A * <tr><td valign="top" headers="construct bounds"><tt>\b</tt></td>
0N/A * <td headers="matches">A word boundary</td></tr>
0N/A * <tr><td valign="top" headers="construct bounds"><tt>\B</tt></td>
0N/A * <td headers="matches">A non-word boundary</td></tr>
0N/A * <tr><td valign="top" headers="construct bounds"><tt>\A</tt></td>
0N/A * <td headers="matches">The beginning of the input</td></tr>
0N/A * <tr><td valign="top" headers="construct bounds"><tt>\G</tt></td>
0N/A * <td headers="matches">The end of the previous match</td></tr>
0N/A * <tr><td valign="top" headers="construct bounds"><tt>\Z</tt></td>
0N/A * <td headers="matches">The end of the input but for the final
0N/A * <a href="#lt">terminator</a>, if&nbsp;any</td></tr>
0N/A * <tr><td valign="top" headers="construct bounds"><tt>\z</tt></td>
0N/A * <td headers="matches">The end of the input</td></tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2" id="greedy">Greedy quantifiers</th></tr>
0N/A *
0N/A * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>?</tt></td>
0N/A * <td headers="matches"><i>X</i>, once or not at all</td></tr>
0N/A * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>*</tt></td>
0N/A * <td headers="matches"><i>X</i>, zero or more times</td></tr>
0N/A * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>+</tt></td>
0N/A * <td headers="matches"><i>X</i>, one or more times</td></tr>
0N/A * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>}</tt></td>
0N/A * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
0N/A * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,}</tt></td>
0N/A * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
0N/A * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}</tt></td>
0N/A * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2" id="reluc">Reluctant quantifiers</th></tr>
0N/A *
0N/A * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>??</tt></td>
0N/A * <td headers="matches"><i>X</i>, once or not at all</td></tr>
0N/A * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>*?</tt></td>
0N/A * <td headers="matches"><i>X</i>, zero or more times</td></tr>
0N/A * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>+?</tt></td>
0N/A * <td headers="matches"><i>X</i>, one or more times</td></tr>
0N/A * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>}?</tt></td>
0N/A * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
0N/A * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,}?</tt></td>
0N/A * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
0N/A * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}?</tt></td>
0N/A * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2" id="poss">Possessive quantifiers</th></tr>
0N/A *
0N/A * <tr><td valign="top" headers="construct poss"><i>X</i><tt>?+</tt></td>
0N/A * <td headers="matches"><i>X</i>, once or not at all</td></tr>
0N/A * <tr><td valign="top" headers="construct poss"><i>X</i><tt>*+</tt></td>
0N/A * <td headers="matches"><i>X</i>, zero or more times</td></tr>
0N/A * <tr><td valign="top" headers="construct poss"><i>X</i><tt>++</tt></td>
0N/A * <td headers="matches"><i>X</i>, one or more times</td></tr>
0N/A * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>}+</tt></td>
0N/A * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
0N/A * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,}+</tt></td>
0N/A * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
0N/A * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}+</tt></td>
0N/A * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2" id="logical">Logical operators</th></tr>
0N/A *
0N/A * <tr><td valign="top" headers="construct logical"><i>XY</i></td>
0N/A * <td headers="matches"><i>X</i> followed by <i>Y</i></td></tr>
0N/A * <tr><td valign="top" headers="construct logical"><i>X</i><tt>|</tt><i>Y</i></td>
0N/A * <td headers="matches">Either <i>X</i> or <i>Y</i></td></tr>
0N/A * <tr><td valign="top" headers="construct logical"><tt>(</tt><i>X</i><tt>)</tt></td>
0N/A * <td headers="matches">X, as a <a href="#cg">capturing group</a></td></tr>
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2" id="backref">Back references</th></tr>
0N/A *
0N/A * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>n</i></td>
0N/A * <td valign="bottom" headers="matches">Whatever the <i>n</i><sup>th</sup>
0N/A * <a href="#cg">capturing group</a> matched</td></tr>
0N/A *
906N/A * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>k</i>&lt;<i>name</i>&gt;</td>
906N/A * <td valign="bottom" headers="matches">Whatever the
906N/A * <a href="#groupname">named-capturing group</a> "name" matched</td></tr>
906N/A *
0N/A * <tr><th>&nbsp;</th></tr>
0N/A * <tr align="left"><th colspan="2" id="quot">Quotation</th></tr>
0N/A *
0N/A * <tr><td valign="top" headers="construct quot"><tt>\</tt></td>
0N/A * <td headers="matches">Nothing, but quotes the following character</td></tr>
0N/A * <tr><td valign="top" headers="construct quot"><tt>\Q</tt></td>
0N/A * <td headers="matches">Nothing, but quotes all characters until <tt>\E</tt></td></tr>
0N/A * <tr><td valign="top" headers="construct quot"><tt>\E</tt></td>
0N/A * <td headers="matches">Nothing, but ends quoting started by <tt>\Q</tt></td></tr>
0N/A * <!-- Metachars: !$()*+.<>?[\]^{|} -->
0N/A *
0N/A * <tr><th>&nbsp;</th></tr>
906N/A * <tr align="left"><th colspan="2" id="special">Special constructs (named-capturing and non-capturing)</th></tr>
0N/A *
906N/A * <tr><td valign="top" headers="construct special"><tt>(?&lt;<a href="#groupname">name</a>&gt;</tt><i>X</i><tt>)</tt></td>
906N/A * <td headers="matches"><i>X</i>, as a named-capturing group</td></tr>
0N/A * <tr><td valign="top" headers="construct special"><tt>(?:</tt><i>X</i><tt>)</tt></td>
0N/A * <td headers="matches"><i>X</i>, as a non-capturing group</td></tr>
4139N/A * <tr><td valign="top" headers="construct special"><tt>(?idmsuxU-idmsuxU)&nbsp;</tt></td>
0N/A * <td headers="matches">Nothing, but turns match flags <a href="#CASE_INSENSITIVE">i</a>
0N/A * <a href="#UNIX_LINES">d</a> <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a>
4139N/A * <a href="#UNICODE_CASE">u</a> <a href="#COMMENTS">x</a> <a href="#UNICODE_CHARACTER_CLASS">U</a>
4139N/A * on - off</td></tr>
0N/A * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux:</tt><i>X</i><tt>)</tt>&nbsp;&nbsp;</td>
0N/A * <td headers="matches"><i>X</i>, as a <a href="#cg">non-capturing group</a> with the
0N/A * given flags <a href="#CASE_INSENSITIVE">i</a> <a href="#UNIX_LINES">d</a>
0N/A * <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> <a href="#UNICODE_CASE">u</a >
0N/A * <a href="#COMMENTS">x</a> on - off</td></tr>
0N/A * <tr><td valign="top" headers="construct special"><tt>(?=</tt><i>X</i><tt>)</tt></td>
0N/A * <td headers="matches"><i>X</i>, via zero-width positive lookahead</td></tr>
0N/A * <tr><td valign="top" headers="construct special"><tt>(?!</tt><i>X</i><tt>)</tt></td>
0N/A * <td headers="matches"><i>X</i>, via zero-width negative lookahead</td></tr>
0N/A * <tr><td valign="top" headers="construct special"><tt>(?&lt;=</tt><i>X</i><tt>)</tt></td>
0N/A * <td headers="matches"><i>X</i>, via zero-width positive lookbehind</td></tr>
0N/A * <tr><td valign="top" headers="construct special"><tt>(?&lt;!</tt><i>X</i><tt>)</tt></td>
0N/A * <td headers="matches"><i>X</i>, via zero-width negative lookbehind</td></tr>
0N/A * <tr><td valign="top" headers="construct special"><tt>(?&gt;</tt><i>X</i><tt>)</tt></td>
0N/A * <td headers="matches"><i>X</i>, as an independent, non-capturing group</td></tr>
0N/A *
0N/A * </table>
0N/A *
0N/A * <hr>
0N/A *
0N/A *
0N/A * <a name="bs">
0N/A * <h4> Backslashes, escapes, and quoting </h4>
0N/A *
0N/A * <p> The backslash character (<tt>'\'</tt>) serves to introduce escaped
0N/A * constructs, as defined in the table above, as well as to quote characters
0N/A * that otherwise would be interpreted as unescaped constructs. Thus the
0N/A * expression <tt>\\</tt> matches a single backslash and <tt>\{</tt> matches a
0N/A * left brace.
0N/A *
0N/A * <p> It is an error to use a backslash prior to any alphabetic character that
0N/A * does not denote an escaped construct; these are reserved for future
0N/A * extensions to the regular-expression language. A backslash may be used
0N/A * prior to a non-alphabetic character regardless of whether that character is
0N/A * part of an unescaped construct.
0N/A *
0N/A * <p> Backslashes within string literals in Java source code are interpreted
4008N/A * as required by
4008N/A * <cite>The Java&trade; Language Specification</cite>
4008N/A * as either Unicode escapes (section 3.3) or other character escapes (section 3.10.6)
4008N/A * It is therefore necessary to double backslashes in string
0N/A * literals that represent regular expressions to protect them from
0N/A * interpretation by the Java bytecode compiler. The string literal
0N/A * <tt>"&#92;b"</tt>, for example, matches a single backspace character when
0N/A * interpreted as a regular expression, while <tt>"&#92;&#92;b"</tt> matches a
0N/A * word boundary. The string literal <tt>"&#92;(hello&#92;)"</tt> is illegal
0N/A * and leads to a compile-time error; in order to match the string
0N/A * <tt>(hello)</tt> the string literal <tt>"&#92;&#92;(hello&#92;&#92;)"</tt>
0N/A * must be used.
0N/A *
0N/A * <a name="cc">
0N/A * <h4> Character Classes </h4>
0N/A *
0N/A * <p> Character classes may appear within other character classes, and
0N/A * may be composed by the union operator (implicit) and the intersection
0N/A * operator (<tt>&amp;&amp;</tt>).
0N/A * The union operator denotes a class that contains every character that is
0N/A * in at least one of its operand classes. The intersection operator
0N/A * denotes a class that contains every character that is in both of its
0N/A * operand classes.
0N/A *
0N/A * <p> The precedence of character-class operators is as follows, from
0N/A * highest to lowest:
0N/A *
0N/A * <blockquote><table border="0" cellpadding="1" cellspacing="0"
0N/A * summary="Precedence of character class operators.">
0N/A * <tr><th>1&nbsp;&nbsp;&nbsp;&nbsp;</th>
0N/A * <td>Literal escape&nbsp;&nbsp;&nbsp;&nbsp;</td>
0N/A * <td><tt>\x</tt></td></tr>
0N/A * <tr><th>2&nbsp;&nbsp;&nbsp;&nbsp;</th>
0N/A * <td>Grouping</td>
0N/A * <td><tt>[...]</tt></td></tr>
0N/A * <tr><th>3&nbsp;&nbsp;&nbsp;&nbsp;</th>
0N/A * <td>Range</td>
0N/A * <td><tt>a-z</tt></td></tr>
0N/A * <tr><th>4&nbsp;&nbsp;&nbsp;&nbsp;</th>
0N/A * <td>Union</td>
0N/A * <td><tt>[a-e][i-u]</tt></td></tr>
0N/A * <tr><th>5&nbsp;&nbsp;&nbsp;&nbsp;</th>
0N/A * <td>Intersection</td>
0N/A * <td><tt>[a-z&&[aeiou]]</tt></td></tr>
0N/A * </table></blockquote>
0N/A *
0N/A * <p> Note that a different set of metacharacters are in effect inside
0N/A * a character class than outside a character class. For instance, the
0N/A * regular expression <tt>.</tt> loses its special meaning inside a
0N/A * character class, while the expression <tt>-</tt> becomes a range
0N/A * forming metacharacter.
0N/A *
0N/A * <a name="lt">
0N/A * <h4> Line terminators </h4>
0N/A *
0N/A * <p> A <i>line terminator</i> is a one- or two-character sequence that marks
0N/A * the end of a line of the input character sequence. The following are
0N/A * recognized as line terminators:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li> A newline (line feed) character&nbsp;(<tt>'\n'</tt>),
0N/A *
0N/A * <li> A carriage-return character followed immediately by a newline
0N/A * character&nbsp;(<tt>"\r\n"</tt>),
0N/A *
0N/A * <li> A standalone carriage-return character&nbsp;(<tt>'\r'</tt>),
0N/A *
0N/A * <li> A next-line character&nbsp;(<tt>'&#92;u0085'</tt>),
0N/A *
0N/A * <li> A line-separator character&nbsp;(<tt>'&#92;u2028'</tt>), or
0N/A *
0N/A * <li> A paragraph-separator character&nbsp;(<tt>'&#92;u2029</tt>).
0N/A *
0N/A * </ul>
0N/A * <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators
0N/A * recognized are newline characters.
0N/A *
0N/A * <p> The regular expression <tt>.</tt> matches any character except a line
0N/A * terminator unless the {@link #DOTALL} flag is specified.
0N/A *
0N/A * <p> By default, the regular expressions <tt>^</tt> and <tt>$</tt> ignore
0N/A * line terminators and only match at the beginning and the end, respectively,
0N/A * of the entire input sequence. If {@link #MULTILINE} mode is activated then
0N/A * <tt>^</tt> matches at the beginning of input and after any line terminator
0N/A * except at the end of input. When in {@link #MULTILINE} mode <tt>$</tt>
0N/A * matches just before a line terminator or the end of the input sequence.
0N/A *
0N/A * <a name="cg">
0N/A * <h4> Groups and capturing </h4>
0N/A *
906N/A * <a name="gnumber">
906N/A * <h5> Group number </h5>
0N/A * <p> Capturing groups are numbered by counting their opening parentheses from
0N/A * left to right. In the expression <tt>((A)(B(C)))</tt>, for example, there
0N/A * are four such groups: </p>
0N/A *
0N/A * <blockquote><table cellpadding=1 cellspacing=0 summary="Capturing group numberings">
0N/A * <tr><th>1&nbsp;&nbsp;&nbsp;&nbsp;</th>
0N/A * <td><tt>((A)(B(C)))</tt></td></tr>
0N/A * <tr><th>2&nbsp;&nbsp;&nbsp;&nbsp;</th>
0N/A * <td><tt>(A)</tt></td></tr>
0N/A * <tr><th>3&nbsp;&nbsp;&nbsp;&nbsp;</th>
0N/A * <td><tt>(B(C))</tt></td></tr>
0N/A * <tr><th>4&nbsp;&nbsp;&nbsp;&nbsp;</th>
0N/A * <td><tt>(C)</tt></td></tr>
0N/A * </table></blockquote>
0N/A *
0N/A * <p> Group zero always stands for the entire expression.
0N/A *
0N/A * <p> Capturing groups are so named because, during a match, each subsequence
0N/A * of the input sequence that matches such a group is saved. The captured
0N/A * subsequence may be used later in the expression, via a back reference, and
0N/A * may also be retrieved from the matcher once the match operation is complete.
0N/A *
906N/A * <a name="groupname">
906N/A * <h5> Group name </h5>
906N/A * <p>A capturing group can also be assigned a "name", a <tt>named-capturing group</tt>,
906N/A * and then be back-referenced later by the "name". Group names are composed of
1795N/A * the following characters. The first character must be a <tt>letter</tt>.
906N/A *
906N/A * <ul>
906N/A * <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt>
906N/A * (<tt>'&#92;u0041'</tt>&nbsp;through&nbsp;<tt>'&#92;u005a'</tt>),
906N/A * <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt>
906N/A * (<tt>'&#92;u0061'</tt>&nbsp;through&nbsp;<tt>'&#92;u007a'</tt>),
906N/A * <li> The digits <tt>'0'</tt> through <tt>'9'</tt>
906N/A * (<tt>'&#92;u0030'</tt>&nbsp;through&nbsp;<tt>'&#92;u0039'</tt>),
906N/A * </ul>
906N/A *
906N/A * <p> A <tt>named-capturing group</tt> is still numbered as described in
906N/A * <a href="#gnumber">Group number</a>.
906N/A *
0N/A * <p> The captured input associated with a group is always the subsequence
0N/A * that the group most recently matched. If a group is evaluated a second time
0N/A * because of quantification then its previously-captured value, if any, will
0N/A * be retained if the second evaluation fails. Matching the string
0N/A * <tt>"aba"</tt> against the expression <tt>(a(b)?)+</tt>, for example, leaves
0N/A * group two set to <tt>"b"</tt>. All captured input is discarded at the
0N/A * beginning of each match.
0N/A *
906N/A * <p> Groups beginning with <tt>(?</tt> are either pure, <i>non-capturing</i> groups
906N/A * that do not capture text and do not count towards the group total, or
906N/A * <i>named-capturing</i> group.
0N/A *
0N/A * <h4> Unicode support </h4>
0N/A *
0N/A * <p> This class is in conformance with Level 1 of <a
0N/A * href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
4139N/A * Standard #18: Unicode Regular Expression</i></a>, plus RL2.1
0N/A * Canonical Equivalents.
4139N/A * <p>
4139N/A * <b>Unicode escape sequences</b> such as <tt>&#92;u2014</tt> in Java source code
4008N/A * are processed as described in section 3.3 of
4008N/A * <cite>The Java&trade; Language Specification</cite>.
4139N/A * Such escape sequences are also implemented directly by the regular-expression
4139N/A * parser so that Unicode escapes can be used in expressions that are read from
4139N/A * files or from the keyboard. Thus the strings <tt>"&#92;u2014"</tt> and
4139N/A * <tt>"\\u2014"</tt>, while not equal, compile into the same pattern, which
4139N/A * matches the character with hexadecimal value <tt>0x2014</tt>.
4139N/A * <p>
4139N/A * A Unicode character can also be represented in a regular-expression by
4139N/A * using its <b>Hex notation</b>(hexadecimal code point value) directly as described in construct
3486N/A * <tt>&#92;x{...}</tt>, for example a supplementary character U+2011F
3486N/A * can be specified as <tt>&#92;x{2011F}</tt>, instead of two consecutive
3486N/A * Unicode escape sequences of the surrogate pair
3486N/A * <tt>&#92;uD840</tt><tt>&#92;uDD1F</tt>.
4139N/A * <p>
4139N/A * Unicode scripts, blocks, categories and binary properties are written with
4139N/A * the <tt>\p</tt> and <tt>\P</tt> constructs as in Perl.
4139N/A * <tt>\p{</tt><i>prop</i><tt>}</tt> matches if
2401N/A * the input has the property <i>prop</i>, while <tt>\P{</tt><i>prop</i><tt>}</tt>
2401N/A * does not match if the input has that property.
2401N/A * <p>
4139N/A * Scripts, blocks, categories and binary properties can be used both inside
4139N/A * and outside of a character class.
4139N/A * <a name="usc">
4139N/A * <p>
4139N/A * <b>Scripts</b> are specified either with the prefix {@code Is}, as in
2401N/A * {@code IsHiragana}, or by using the {@code script} keyword (or its short
2401N/A * form {@code sc})as in {@code script=Hiragana} or {@code sc=Hiragana}.
2401N/A * <p>
4139N/A * The script names supported by <code>Pattern</code> are the valid script names
4139N/A * accepted and defined by
4139N/A * {@link java.lang.Character.UnicodeScript#forName(String) UnicodeScript.forName}.
4139N/A * <a name="ubc">
4139N/A * <p>
4139N/A * <b>Blocks</b> are specified with the prefix {@code In}, as in
2401N/A * {@code InMongolian}, or by using the keyword {@code block} (or its short
2401N/A * form {@code blk}) as in {@code block=Mongolian} or {@code blk=Mongolian}.
2401N/A * <p>
4139N/A * The block names supported by <code>Pattern</code> are the valid block names
4139N/A * accepted and defined by
4139N/A * {@link java.lang.Character.UnicodeBlock#forName(String) UnicodeBlock.forName}.
4139N/A * <p>
4139N/A * <a name="ucc">
4139N/A * <b>Categories</b> may be specified with the optional prefix {@code Is}:
2401N/A * Both {@code \p{L}} and {@code \p{IsL}} denote the category of Unicode
2401N/A * letters. Same as scripts and blocks, categories can also be specified
2401N/A * by using the keyword {@code general_category} (or its short form
2401N/A * {@code gc}) as in {@code general_category=Lu} or {@code gc=Lu}.
2401N/A * <p>
4139N/A * The supported categories are those of
0N/A * <a href="http://www.unicode.org/unicode/standard/standard.html">
0N/A * <i>The Unicode Standard</i></a> in the version specified by the
0N/A * {@link java.lang.Character Character} class. The category names are those
0N/A * defined in the Standard, both normative and informative.
4139N/A * <p>
4139N/A * <a name="ubpc">
4139N/A * <b>Binary properties</b> are specified with the prefix {@code Is}, as in
4139N/A * {@code IsAlphabetic}. The supported binary properties by <code>Pattern</code>
4139N/A * are
4139N/A * <ul>
4139N/A * <li> Alphabetic
4139N/A * <li> Ideographic
4139N/A * <li> Letter
4139N/A * <li> Lowercase
4139N/A * <li> Uppercase
4139N/A * <li> Titlecase
4139N/A * <li> Punctuation
4139N/A * <Li> Control
4139N/A * <li> White_Space
4139N/A * <li> Digit
4139N/A * <li> Hex_Digit
4139N/A * <li> Noncharacter_Code_Point
4139N/A * <li> Assigned
4139N/A * </ul>
4139N/A
4139N/A
4139N/A * <p>
4139N/A * <b>Predefined Character classes</b> and <b>POSIX character classes</b> are in
4139N/A * conformance with the recommendation of <i>Annex C: Compatibility Properties</i>
4139N/A * of <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Regular Expression
4139N/A * </i></a>, when {@link #UNICODE_CHARACTER_CLASS} flag is specified.
2401N/A * <p>
4139N/A * <table border="0" cellpadding="1" cellspacing="0"
4139N/A * summary="predefined and posix character classes in Unicode mode">
4139N/A * <tr align="left">
4139N/A * <th bgcolor="#CCCCFF" align="left" id="classes">Classes</th>
4139N/A * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
4139N/A *</tr>
4139N/A * <tr><td><tt>\p{Lower}</tt></td>
4139N/A * <td>A lowercase character:<tt>\p{IsLowercase}</tt></td></tr>
4139N/A * <tr><td><tt>\p{Upper}</tt></td>
4139N/A * <td>An uppercase character:<tt>\p{IsUppercase}</tt></td></tr>
4139N/A * <tr><td><tt>\p{ASCII}</tt></td>
4139N/A * <td>All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
4139N/A * <tr><td><tt>\p{Alpha}</tt></td>
4139N/A * <td>An alphabetic character:<tt>\p{IsAlphabetic}</tt></td></tr>
4139N/A * <tr><td><tt>\p{Digit}</tt></td>
4139N/A * <td>A decimal digit character:<tt>p{IsDigit}</tt></td></tr>
4139N/A * <tr><td><tt>\p{Alnum}</tt></td>
4139N/A * <td>An alphanumeric character:<tt>[\p{IsAlphabetic}\p{IsDigit}]</tt></td></tr>
4139N/A * <tr><td><tt>\p{Punct}</tt></td>
4139N/A * <td>A punctuation character:<tt>p{IsPunctuation}</tt></td></tr>
4139N/A * <tr><td><tt>\p{Graph}</tt></td>
4139N/A * <td>A visible character: <tt>[^\p{IsWhite_Space}\p{gc=Cc}\p{gc=Cs}\p{gc=Cn}]</tt></td></tr>
4139N/A * <tr><td><tt>\p{Print}</tt></td>
4139N/A * <td>A printable character: <tt>[\p{Graph}\p{Blank}&&[^\p{Cntrl}]]</tt></td></tr>
4139N/A * <tr><td><tt>\p{Blank}</tt></td>
4139N/A * <td>A space or a tab: <tt>[\p{IsWhite_Space}&&[^\p{gc=Zl}\p{gc=Zp}\x0a\x0b\x0c\x0d\x85]]</tt></td></tr>
4139N/A * <tr><td><tt>\p{Cntrl}</tt></td>
4139N/A * <td>A control character: <tt>\p{gc=Cc}</tt></td></tr>
4139N/A * <tr><td><tt>\p{XDigit}</tt></td>
4139N/A * <td>A hexadecimal digit: <tt>[\p{gc=Nd}\p{IsHex_Digit}]</tt></td></tr>
4139N/A * <tr><td><tt>\p{Space}</tt></td>
4139N/A * <td>A whitespace character:<tt>\p{IsWhite_Space}</tt></td></tr>
4139N/A * <tr><td><tt>\d</tt></td>
4139N/A * <td>A digit: <tt>\p{IsDigit}</tt></td></tr>
4139N/A * <tr><td><tt>\D</tt></td>
4139N/A * <td>A non-digit: <tt>[^\d]</tt></td></tr>
4139N/A * <tr><td><tt>\s</tt></td>
4139N/A * <td>A whitespace character: <tt>\p{IsWhite_Space}</tt></td></tr>
4139N/A * <tr><td><tt>\S</tt></td>
4139N/A * <td>A non-whitespace character: <tt>[^\s]</tt></td></tr>
4139N/A * <tr><td><tt>\w</tt></td>
4139N/A * <td>A word character: <tt>[\p{Alpha}\p{gc=Mn}\p{gc=Me}\p{gc=Mc}\p{Digit}\p{gc=Pc}]</tt></td></tr>
4139N/A * <tr><td><tt>\W</tt></td>
4139N/A * <td>A non-word character: <tt>[^\w]</tt></td></tr>
4139N/A * </table>
4139N/A * <p>
4139N/A * <a name="jcc">
4139N/A * Categories that behave like the java.lang.Character
0N/A * boolean is<i>methodname</i> methods (except for the deprecated ones) are
0N/A * available through the same <tt>\p{</tt><i>prop</i><tt>}</tt> syntax where
0N/A * the specified property has the name <tt>java<i>methodname</i></tt>.
0N/A *
0N/A * <h4> Comparison to Perl 5 </h4>
0N/A *
0N/A * <p>The <code>Pattern</code> engine performs traditional NFA-based matching
0N/A * with ordered alternation as occurs in Perl 5.
0N/A *
0N/A * <p> Perl constructs not supported by this class: </p>
0N/A *
0N/A * <ul>
4146N/A * <li><p> Predefined character classes (Unicode character)
4146N/A * <p><tt>\h&nbsp;&nbsp;&nbsp;&nbsp;</tt>A horizontal whitespace
4146N/A * <p><tt>\H&nbsp;&nbsp;&nbsp;&nbsp;</tt>A non horizontal whitespace
4146N/A * <p><tt>\v&nbsp;&nbsp;&nbsp;&nbsp;</tt>A vertical whitespace
4146N/A * <p><tt>\V&nbsp;&nbsp;&nbsp;&nbsp;</tt>A non vertical whitespace
4146N/A * <p><tt>\R&nbsp;&nbsp;&nbsp;&nbsp;</tt>Any Unicode linebreak sequence
4146N/A * <tt>\u005cu000D\u005cu000A|[\u005cu000A\u005cu000B\u005cu000C\u005cu000D\u005cu0085\u005cu2028\u005cu2029]</tt>
4146N/A * <p><tt>\X&nbsp;&nbsp;&nbsp;&nbsp;</tt>Match Unicode
4146N/A * <a href="http://www.unicode.org/reports/tr18/#Default_Grapheme_Clusters">
4146N/A * <i>extended grapheme cluster</i></a>
4146N/A * </p></li>
0N/A *
4146N/A * <li><p> The backreference constructs, <tt>\g{</tt><i>n</i><tt>}</tt> for
4146N/A * the <i>n</i><sup>th</sup><a href="#cg">capturing group</a> and
4146N/A * <tt>\g{</tt><i>name</i><tt>}</tt> for
4146N/A * <a href="#groupname">named-capturing group</a>.
4146N/A * </p></li>
4146N/A *
4146N/A * <li><p> The named character construct, <tt>\N{</tt><i>name</i><tt>}</tt>
4146N/A * for a Unicode character by its name.
4146N/A * </p></li>
4146N/A *
4146N/A * <li><p> The conditional constructs
4146N/A * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>)</tt> and
0N/A * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>|</tt><i>Y</i><tt>)</tt>,
0N/A * </p></li>
0N/A *
0N/A * <li><p> The embedded code constructs <tt>(?{</tt><i>code</i><tt>})</tt>
0N/A * and <tt>(??{</tt><i>code</i><tt>})</tt>,</p></li>
0N/A *
0N/A * <li><p> The embedded comment syntax <tt>(?#comment)</tt>, and </p></li>
0N/A *
0N/A * <li><p> The preprocessing operations <tt>\l</tt> <tt>&#92;u</tt>,
0N/A * <tt>\L</tt>, and <tt>\U</tt>. </p></li>
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p> Constructs supported by this class but not by Perl: </p>
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li><p> Character-class union and intersection as described
0N/A * <a href="#cc">above</a>.</p></li>
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p> Notable differences from Perl: </p>
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li><p> In Perl, <tt>\1</tt> through <tt>\9</tt> are always interpreted
0N/A * as back references; a backslash-escaped number greater than <tt>9</tt> is
0N/A * treated as a back reference if at least that many subexpressions exist,
0N/A * otherwise it is interpreted, if possible, as an octal escape. In this
0N/A * class octal escapes must always begin with a zero. In this class,
0N/A * <tt>\1</tt> through <tt>\9</tt> are always interpreted as back
0N/A * references, and a larger number is accepted as a back reference if at
0N/A * least that many subexpressions exist at that point in the regular
0N/A * expression, otherwise the parser will drop digits until the number is
0N/A * smaller or equal to the existing number of groups or it is one digit.
0N/A * </p></li>
0N/A *
0N/A * <li><p> Perl uses the <tt>g</tt> flag to request a match that resumes
0N/A * where the last match left off. This functionality is provided implicitly
0N/A * by the {@link Matcher} class: Repeated invocations of the {@link
0N/A * Matcher#find find} method will resume where the last match left off,
0N/A * unless the matcher is reset. </p></li>
0N/A *
0N/A * <li><p> In Perl, embedded flags at the top level of an expression affect
0N/A * the whole expression. In this class, embedded flags always take effect
0N/A * at the point at which they appear, whether they are at the top level or
0N/A * within a group; in the latter case, flags are restored at the end of the
0N/A * group just as in Perl. </p></li>
0N/A *
0N/A * </ul>
0N/A *
0N/A *
0N/A * <p> For a more precise description of the behavior of regular expression
0N/A * constructs, please see <a href="http://www.oreilly.com/catalog/regex3/">
0N/A * <i>Mastering Regular Expressions, 3nd Edition</i>, Jeffrey E. F. Friedl,
0N/A * O'Reilly and Associates, 2006.</a>
0N/A * </p>
0N/A *
0N/A * @see java.lang.String#split(String, int)
0N/A * @see java.lang.String#split(String)
0N/A *
0N/A * @author Mike McCloskey
0N/A * @author Mark Reinhold
0N/A * @author JSR-51 Expert Group
0N/A * @since 1.4
0N/A * @spec JSR-51
0N/A */
0N/A
0N/Apublic final class Pattern
0N/A implements java.io.Serializable
0N/A{
0N/A
0N/A /**
0N/A * Regular expression modifier values. Instead of being passed as
0N/A * arguments, they can also be passed as inline modifiers.
0N/A * For example, the following statements have the same effect.
0N/A * <pre>
0N/A * RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M);
0N/A * RegExp r2 = RegExp.compile("(?im)abc", 0);
0N/A * </pre>
0N/A *
0N/A * The flags are duplicated so that the familiar Perl match flag
0N/A * names are available.
0N/A */
0N/A
0N/A /**
0N/A * Enables Unix lines mode.
0N/A *
0N/A * <p> In this mode, only the <tt>'\n'</tt> line terminator is recognized
0N/A * in the behavior of <tt>.</tt>, <tt>^</tt>, and <tt>$</tt>.
0N/A *
0N/A * <p> Unix lines mode can also be enabled via the embedded flag
0N/A * expression&nbsp;<tt>(?d)</tt>.
0N/A */
0N/A public static final int UNIX_LINES = 0x01;
0N/A
0N/A /**
0N/A * Enables case-insensitive matching.
0N/A *
0N/A * <p> By default, case-insensitive matching assumes that only characters
0N/A * in the US-ASCII charset are being matched. Unicode-aware
0N/A * case-insensitive matching can be enabled by specifying the {@link
0N/A * #UNICODE_CASE} flag in conjunction with this flag.
0N/A *
0N/A * <p> Case-insensitive matching can also be enabled via the embedded flag
0N/A * expression&nbsp;<tt>(?i)</tt>.
0N/A *
0N/A * <p> Specifying this flag may impose a slight performance penalty. </p>
0N/A */
0N/A public static final int CASE_INSENSITIVE = 0x02;
0N/A
0N/A /**
0N/A * Permits whitespace and comments in pattern.
0N/A *
0N/A * <p> In this mode, whitespace is ignored, and embedded comments starting
0N/A * with <tt>#</tt> are ignored until the end of a line.
0N/A *
0N/A * <p> Comments mode can also be enabled via the embedded flag
0N/A * expression&nbsp;<tt>(?x)</tt>.
0N/A */
0N/A public static final int COMMENTS = 0x04;
0N/A
0N/A /**
0N/A * Enables multiline mode.
0N/A *
0N/A * <p> In multiline mode the expressions <tt>^</tt> and <tt>$</tt> match
0N/A * just after or just before, respectively, a line terminator or the end of
0N/A * the input sequence. By default these expressions only match at the
0N/A * beginning and the end of the entire input sequence.
0N/A *
0N/A * <p> Multiline mode can also be enabled via the embedded flag
0N/A * expression&nbsp;<tt>(?m)</tt>. </p>
0N/A */
0N/A public static final int MULTILINE = 0x08;
0N/A
0N/A /**
0N/A * Enables literal parsing of the pattern.
0N/A *
0N/A * <p> When this flag is specified then the input string that specifies
0N/A * the pattern is treated as a sequence of literal characters.
0N/A * Metacharacters or escape sequences in the input sequence will be
0N/A * given no special meaning.
0N/A *
0N/A * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on
0N/A * matching when used in conjunction with this flag. The other flags
0N/A * become superfluous.
0N/A *
0N/A * <p> There is no embedded flag character for enabling literal parsing.
0N/A * @since 1.5
0N/A */
0N/A public static final int LITERAL = 0x10;
0N/A
0N/A /**
0N/A * Enables dotall mode.
0N/A *
0N/A * <p> In dotall mode, the expression <tt>.</tt> matches any character,
0N/A * including a line terminator. By default this expression does not match
0N/A * line terminators.
0N/A *
0N/A * <p> Dotall mode can also be enabled via the embedded flag
0N/A * expression&nbsp;<tt>(?s)</tt>. (The <tt>s</tt> is a mnemonic for
0N/A * "single-line" mode, which is what this is called in Perl.) </p>
0N/A */
0N/A public static final int DOTALL = 0x20;
0N/A
0N/A /**
0N/A * Enables Unicode-aware case folding.
0N/A *
0N/A * <p> When this flag is specified then case-insensitive matching, when
0N/A * enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner
0N/A * consistent with the Unicode Standard. By default, case-insensitive
0N/A * matching assumes that only characters in the US-ASCII charset are being
0N/A * matched.
0N/A *
0N/A * <p> Unicode-aware case folding can also be enabled via the embedded flag
0N/A * expression&nbsp;<tt>(?u)</tt>.
0N/A *
0N/A * <p> Specifying this flag may impose a performance penalty. </p>
0N/A */
0N/A public static final int UNICODE_CASE = 0x40;
0N/A
0N/A /**
0N/A * Enables canonical equivalence.
0N/A *
0N/A * <p> When this flag is specified then two characters will be considered
0N/A * to match if, and only if, their full canonical decompositions match.
0N/A * The expression <tt>"a&#92;u030A"</tt>, for example, will match the
0N/A * string <tt>"&#92;u00E5"</tt> when this flag is specified. By default,
0N/A * matching does not take canonical equivalence into account.
0N/A *
0N/A * <p> There is no embedded flag character for enabling canonical
0N/A * equivalence.
0N/A *
0N/A * <p> Specifying this flag may impose a performance penalty. </p>
0N/A */
0N/A public static final int CANON_EQ = 0x80;
0N/A
4139N/A /**
4139N/A * Enables the Unicode version of <i>Predefined character classes</i> and
4139N/A * <i>POSIX character classes</i>.
4139N/A *
4139N/A * <p> When this flag is specified then the (US-ASCII only)
4139N/A * <i>Predefined character classes</i> and <i>POSIX character classes</i>
4139N/A * are in conformance with
4139N/A * <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
4139N/A * Standard #18: Unicode Regular Expression</i></a>
4139N/A * <i>Annex C: Compatibility Properties</i>.
4139N/A * <p>
4139N/A * The UNICODE_CHARACTER_CLASS mode can also be enabled via the embedded
4139N/A * flag expression&nbsp;<tt>(?U)</tt>.
4139N/A * <p>
4139N/A * The flag implies UNICODE_CASE, that is, it enables Unicode-aware case
4139N/A * folding.
4139N/A * <p>
4139N/A * Specifying this flag may impose a performance penalty. </p>
4139N/A * @since 1.7
4139N/A */
4139N/A public static final int UNICODE_CHARACTER_CLASS = 0x100;
4139N/A
0N/A /* Pattern has only two serialized components: The pattern string
0N/A * and the flags, which are all that is needed to recompile the pattern
0N/A * when it is deserialized.
0N/A */
0N/A
0N/A /** use serialVersionUID from Merlin b59 for interoperability */
0N/A private static final long serialVersionUID = 5073258162644648461L;
0N/A
0N/A /**
0N/A * The original regular-expression pattern string.
0N/A *
0N/A * @serial
0N/A */
0N/A private String pattern;
0N/A
0N/A /**
0N/A * The original pattern flags.
0N/A *
0N/A * @serial
0N/A */
0N/A private int flags;
0N/A
0N/A /**
0N/A * Boolean indicating this Pattern is compiled; this is necessary in order
0N/A * to lazily compile deserialized Patterns.
0N/A */
0N/A private transient volatile boolean compiled = false;
0N/A
0N/A /**
0N/A * The normalized pattern string.
0N/A */
0N/A private transient String normalizedPattern;
0N/A
0N/A /**
0N/A * The starting point of state machine for the find operation. This allows
0N/A * a match to start anywhere in the input.
0N/A */
0N/A transient Node root;
0N/A
0N/A /**
0N/A * The root of object tree for a match operation. The pattern is matched
0N/A * at the beginning. This may include a find that uses BnM or a First
0N/A * node.
0N/A */
0N/A transient Node matchRoot;
0N/A
0N/A /**
0N/A * Temporary storage used by parsing pattern slice.
0N/A */
0N/A transient int[] buffer;
0N/A
0N/A /**
906N/A * Map the "name" of the "named capturing group" to its group id
906N/A * node.
906N/A */
906N/A transient volatile Map<String, Integer> namedGroups;
906N/A
906N/A /**
0N/A * Temporary storage used while parsing group references.
0N/A */
0N/A transient GroupHead[] groupNodes;
0N/A
0N/A /**
0N/A * Temporary null terminated code point array used by pattern compiling.
0N/A */
0N/A private transient int[] temp;
0N/A
0N/A /**
0N/A * The number of capturing groups in this Pattern. Used by matchers to
0N/A * allocate storage needed to perform a match.
0N/A */
0N/A transient int capturingGroupCount;
0N/A
0N/A /**
0N/A * The local variable count used by parsing tree. Used by matchers to
0N/A * allocate storage needed to perform a match.
0N/A */
0N/A transient int localCount;
0N/A
0N/A /**
0N/A * Index into the pattern string that keeps track of how much has been
0N/A * parsed.
0N/A */
0N/A private transient int cursor;
0N/A
0N/A /**
0N/A * Holds the length of the pattern string.
0N/A */
0N/A private transient int patternLength;
0N/A
0N/A /**
2118N/A * If the Start node might possibly match supplementary characters.
2118N/A * It is set to true during compiling if
2118N/A * (1) There is supplementary char in pattern, or
2118N/A * (2) There is complement node of Category or Block
2118N/A */
2118N/A private transient boolean hasSupplementary;
2118N/A
2118N/A /**
0N/A * Compiles the given regular expression into a pattern. </p>
0N/A *
0N/A * @param regex
0N/A * The expression to be compiled
0N/A *
0N/A * @throws PatternSyntaxException
0N/A * If the expression's syntax is invalid
0N/A */
0N/A public static Pattern compile(String regex) {
0N/A return new Pattern(regex, 0);
0N/A }
0N/A
0N/A /**
0N/A * Compiles the given regular expression into a pattern with the given
0N/A * flags. </p>
0N/A *
0N/A * @param regex
0N/A * The expression to be compiled
0N/A *
0N/A * @param flags
0N/A * Match flags, a bit mask that may include
0N/A * {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL},
0N/A * {@link #UNICODE_CASE}, {@link #CANON_EQ}, {@link #UNIX_LINES},
4139N/A * {@link #LITERAL}, {@link #UNICODE_CHARACTER_CLASS}
4139N/A * and {@link #COMMENTS}
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If bit values other than those corresponding to the defined
0N/A * match flags are set in <tt>flags</tt>
0N/A *
0N/A * @throws PatternSyntaxException
0N/A * If the expression's syntax is invalid
0N/A */
0N/A public static Pattern compile(String regex, int flags) {
0N/A return new Pattern(regex, flags);
0N/A }
0N/A
0N/A /**
0N/A * Returns the regular expression from which this pattern was compiled.
0N/A * </p>
0N/A *
0N/A * @return The source of this pattern
0N/A */
0N/A public String pattern() {
0N/A return pattern;
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns the string representation of this pattern. This
0N/A * is the regular expression from which this pattern was
0N/A * compiled.</p>
0N/A *
0N/A * @return The string representation of this pattern
0N/A * @since 1.5
0N/A */
0N/A public String toString() {
0N/A return pattern;
0N/A }
0N/A
0N/A /**
0N/A * Creates a matcher that will match the given input against this pattern.
0N/A * </p>
0N/A *
0N/A * @param input
0N/A * The character sequence to be matched
0N/A *
0N/A * @return A new matcher for this pattern
0N/A */
0N/A public Matcher matcher(CharSequence input) {
0N/A if (!compiled) {
0N/A synchronized(this) {
0N/A if (!compiled)
0N/A compile();
0N/A }
0N/A }
0N/A Matcher m = new Matcher(this, input);
0N/A return m;
0N/A }
0N/A
0N/A /**
0N/A * Returns this pattern's match flags. </p>
0N/A *
0N/A * @return The match flags specified when this pattern was compiled
0N/A */
0N/A public int flags() {
0N/A return flags;
0N/A }
0N/A
0N/A /**
0N/A * Compiles the given regular expression and attempts to match the given
0N/A * input against it.
0N/A *
0N/A * <p> An invocation of this convenience method of the form
0N/A *
0N/A * <blockquote><pre>
0N/A * Pattern.matches(regex, input);</pre></blockquote>
0N/A *
0N/A * behaves in exactly the same way as the expression
0N/A *
0N/A * <blockquote><pre>
0N/A * Pattern.compile(regex).matcher(input).matches()</pre></blockquote>
0N/A *
0N/A * <p> If a pattern is to be used multiple times, compiling it once and reusing
0N/A * it will be more efficient than invoking this method each time. </p>
0N/A *
0N/A * @param regex
0N/A * The expression to be compiled
0N/A *
0N/A * @param input
0N/A * The character sequence to be matched
0N/A *
0N/A * @throws PatternSyntaxException
0N/A * If the expression's syntax is invalid
0N/A */
0N/A public static boolean matches(String regex, CharSequence input) {
0N/A Pattern p = Pattern.compile(regex);
0N/A Matcher m = p.matcher(input);
0N/A return m.matches();
0N/A }
0N/A
0N/A /**
0N/A * Splits the given input sequence around matches of this pattern.
0N/A *
0N/A * <p> The array returned by this method contains each substring of the
0N/A * input sequence that is terminated by another subsequence that matches
0N/A * this pattern or is terminated by the end of the input sequence. The
0N/A * substrings in the array are in the order in which they occur in the
0N/A * input. If this pattern does not match any subsequence of the input then
0N/A * the resulting array has just one element, namely the input sequence in
0N/A * string form.
0N/A *
0N/A * <p> The <tt>limit</tt> parameter controls the number of times the
0N/A * pattern is applied and therefore affects the length of the resulting
0N/A * array. If the limit <i>n</i> is greater than zero then the pattern
0N/A * will be applied at most <i>n</i>&nbsp;-&nbsp;1 times, the array's
0N/A * length will be no greater than <i>n</i>, and the array's last entry
0N/A * will contain all input beyond the last matched delimiter. If <i>n</i>
0N/A * is non-positive then the pattern will be applied as many times as
0N/A * possible and the array can have any length. If <i>n</i> is zero then
0N/A * the pattern will be applied as many times as possible, the array can
0N/A * have any length, and trailing empty strings will be discarded.
0N/A *
0N/A * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
0N/A * results with these parameters:
0N/A *
0N/A * <blockquote><table cellpadding=1 cellspacing=0
0N/A * summary="Split examples showing regex, limit, and result">
0N/A * <tr><th><P align="left"><i>Regex&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
0N/A * <th><P align="left"><i>Limit&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
0N/A * <th><P align="left"><i>Result&nbsp;&nbsp;&nbsp;&nbsp;</i></th></tr>
0N/A * <tr><td align=center>:</td>
0N/A * <td align=center>2</td>
0N/A * <td><tt>{ "boo", "and:foo" }</tt></td></tr>
0N/A * <tr><td align=center>:</td>
0N/A * <td align=center>5</td>
0N/A * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
0N/A * <tr><td align=center>:</td>
0N/A * <td align=center>-2</td>
0N/A * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
0N/A * <tr><td align=center>o</td>
0N/A * <td align=center>5</td>
0N/A * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
0N/A * <tr><td align=center>o</td>
0N/A * <td align=center>-2</td>
0N/A * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
0N/A * <tr><td align=center>o</td>
0N/A * <td align=center>0</td>
0N/A * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
0N/A * </table></blockquote>
0N/A *
0N/A *
0N/A * @param input
0N/A * The character sequence to be split
0N/A *
0N/A * @param limit
0N/A * The result threshold, as described above
0N/A *
0N/A * @return The array of strings computed by splitting the input
0N/A * around matches of this pattern
0N/A */
0N/A public String[] split(CharSequence input, int limit) {
0N/A int index = 0;
0N/A boolean matchLimited = limit > 0;
3323N/A ArrayList<String> matchList = new ArrayList<>();
0N/A Matcher m = matcher(input);
0N/A
0N/A // Add segments before each match found
0N/A while(m.find()) {
0N/A if (!matchLimited || matchList.size() < limit - 1) {
0N/A String match = input.subSequence(index, m.start()).toString();
0N/A matchList.add(match);
0N/A index = m.end();
0N/A } else if (matchList.size() == limit - 1) { // last one
0N/A String match = input.subSequence(index,
0N/A input.length()).toString();
0N/A matchList.add(match);
0N/A index = m.end();
0N/A }
0N/A }
0N/A
0N/A // If no match was found, return this
0N/A if (index == 0)
0N/A return new String[] {input.toString()};
0N/A
0N/A // Add remaining segment
0N/A if (!matchLimited || matchList.size() < limit)
0N/A matchList.add(input.subSequence(index, input.length()).toString());
0N/A
0N/A // Construct result
0N/A int resultSize = matchList.size();
0N/A if (limit == 0)
0N/A while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
0N/A resultSize--;
0N/A String[] result = new String[resultSize];
0N/A return matchList.subList(0, resultSize).toArray(result);
0N/A }
0N/A
0N/A /**
0N/A * Splits the given input sequence around matches of this pattern.
0N/A *
0N/A * <p> This method works as if by invoking the two-argument {@link
0N/A * #split(java.lang.CharSequence, int) split} method with the given input
0N/A * sequence and a limit argument of zero. Trailing empty strings are
0N/A * therefore not included in the resulting array. </p>
0N/A *
0N/A * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
0N/A * results with these expressions:
0N/A *
0N/A * <blockquote><table cellpadding=1 cellspacing=0
0N/A * summary="Split examples showing regex and result">
0N/A * <tr><th><P align="left"><i>Regex&nbsp;&nbsp;&nbsp;&nbsp;</i></th>
0N/A * <th><P align="left"><i>Result</i></th></tr>
0N/A * <tr><td align=center>:</td>
0N/A * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
0N/A * <tr><td align=center>o</td>
0N/A * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
0N/A * </table></blockquote>
0N/A *
0N/A *
0N/A * @param input
0N/A * The character sequence to be split
0N/A *
0N/A * @return The array of strings computed by splitting the input
0N/A * around matches of this pattern
0N/A */
0N/A public String[] split(CharSequence input) {
0N/A return split(input, 0);
0N/A }
0N/A
0N/A /**
0N/A * Returns a literal pattern <code>String</code> for the specified
0N/A * <code>String</code>.
0N/A *
0N/A * <p>This method produces a <code>String</code> that can be used to
0N/A * create a <code>Pattern</code> that would match the string
0N/A * <code>s</code> as if it were a literal pattern.</p> Metacharacters
0N/A * or escape sequences in the input sequence will be given no special
0N/A * meaning.
0N/A *
0N/A * @param s The string to be literalized
0N/A * @return A literal string replacement
0N/A * @since 1.5
0N/A */
0N/A public static String quote(String s) {
0N/A int slashEIndex = s.indexOf("\\E");
0N/A if (slashEIndex == -1)
0N/A return "\\Q" + s + "\\E";
0N/A
0N/A StringBuilder sb = new StringBuilder(s.length() * 2);
0N/A sb.append("\\Q");
0N/A slashEIndex = 0;
0N/A int current = 0;
0N/A while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
0N/A sb.append(s.substring(current, slashEIndex));
0N/A current = slashEIndex + 2;
0N/A sb.append("\\E\\\\E\\Q");
0N/A }
0N/A sb.append(s.substring(current, s.length()));
0N/A sb.append("\\E");
0N/A return sb.toString();
0N/A }
0N/A
0N/A /**
0N/A * Recompile the Pattern instance from a stream. The original pattern
0N/A * string is read in and the object tree is recompiled from it.
0N/A */
0N/A private void readObject(java.io.ObjectInputStream s)
0N/A throws java.io.IOException, ClassNotFoundException {
0N/A
0N/A // Read in all fields
0N/A s.defaultReadObject();
0N/A
0N/A // Initialize counts
0N/A capturingGroupCount = 1;
0N/A localCount = 0;
0N/A
0N/A // if length > 0, the Pattern is lazily compiled
0N/A compiled = false;
0N/A if (pattern.length() == 0) {
0N/A root = new Start(lastAccept);
0N/A matchRoot = lastAccept;
0N/A compiled = true;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This private constructor is used to create all Patterns. The pattern
0N/A * string and match flags are all that is needed to completely describe
0N/A * a Pattern. An empty pattern string results in an object tree with
0N/A * only a Start node and a LastNode node.
0N/A */
0N/A private Pattern(String p, int f) {
0N/A pattern = p;
0N/A flags = f;
0N/A
4139N/A // to use UNICODE_CASE if UNICODE_CHARACTER_CLASS present
4139N/A if ((flags & UNICODE_CHARACTER_CLASS) != 0)
4139N/A flags |= UNICODE_CASE;
4139N/A
0N/A // Reset group index count
0N/A capturingGroupCount = 1;
0N/A localCount = 0;
0N/A
0N/A if (pattern.length() > 0) {
0N/A compile();
0N/A } else {
0N/A root = new Start(lastAccept);
0N/A matchRoot = lastAccept;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The pattern is converted to normalizedD form and then a pure group
0N/A * is constructed to match canonical equivalences of the characters.
0N/A */
0N/A private void normalize() {
0N/A boolean inCharClass = false;
0N/A int lastCodePoint = -1;
0N/A
0N/A // Convert pattern into normalizedD form
0N/A normalizedPattern = Normalizer.normalize(pattern, Normalizer.Form.NFD);
0N/A patternLength = normalizedPattern.length();
0N/A
0N/A // Modify pattern to match canonical equivalences
0N/A StringBuilder newPattern = new StringBuilder(patternLength);
0N/A for(int i=0; i<patternLength; ) {
0N/A int c = normalizedPattern.codePointAt(i);
0N/A StringBuilder sequenceBuffer;
0N/A if ((Character.getType(c) == Character.NON_SPACING_MARK)
0N/A && (lastCodePoint != -1)) {
0N/A sequenceBuffer = new StringBuilder();
0N/A sequenceBuffer.appendCodePoint(lastCodePoint);
0N/A sequenceBuffer.appendCodePoint(c);
0N/A while(Character.getType(c) == Character.NON_SPACING_MARK) {
0N/A i += Character.charCount(c);
0N/A if (i >= patternLength)
0N/A break;
0N/A c = normalizedPattern.codePointAt(i);
0N/A sequenceBuffer.appendCodePoint(c);
0N/A }
0N/A String ea = produceEquivalentAlternation(
0N/A sequenceBuffer.toString());
0N/A newPattern.setLength(newPattern.length()-Character.charCount(lastCodePoint));
0N/A newPattern.append("(?:").append(ea).append(")");
0N/A } else if (c == '[' && lastCodePoint != '\\') {
0N/A i = normalizeCharClass(newPattern, i);
0N/A } else {
0N/A newPattern.appendCodePoint(c);
0N/A }
0N/A lastCodePoint = c;
0N/A i += Character.charCount(c);
0N/A }
0N/A normalizedPattern = newPattern.toString();
0N/A }
0N/A
0N/A /**
0N/A * Complete the character class being parsed and add a set
0N/A * of alternations to it that will match the canonical equivalences
0N/A * of the characters within the class.
0N/A */
0N/A private int normalizeCharClass(StringBuilder newPattern, int i) {
0N/A StringBuilder charClass = new StringBuilder();
0N/A StringBuilder eq = null;
0N/A int lastCodePoint = -1;
0N/A String result;
0N/A
0N/A i++;
0N/A charClass.append("[");
0N/A while(true) {
0N/A int c = normalizedPattern.codePointAt(i);
0N/A StringBuilder sequenceBuffer;
0N/A
0N/A if (c == ']' && lastCodePoint != '\\') {
0N/A charClass.append((char)c);
0N/A break;
0N/A } else if (Character.getType(c) == Character.NON_SPACING_MARK) {
0N/A sequenceBuffer = new StringBuilder();
0N/A sequenceBuffer.appendCodePoint(lastCodePoint);
0N/A while(Character.getType(c) == Character.NON_SPACING_MARK) {
0N/A sequenceBuffer.appendCodePoint(c);
0N/A i += Character.charCount(c);
0N/A if (i >= normalizedPattern.length())
0N/A break;
0N/A c = normalizedPattern.codePointAt(i);
0N/A }
0N/A String ea = produceEquivalentAlternation(
0N/A sequenceBuffer.toString());
0N/A
0N/A charClass.setLength(charClass.length()-Character.charCount(lastCodePoint));
0N/A if (eq == null)
0N/A eq = new StringBuilder();
0N/A eq.append('|');
0N/A eq.append(ea);
0N/A } else {
0N/A charClass.appendCodePoint(c);
0N/A i++;
0N/A }
0N/A if (i == normalizedPattern.length())
0N/A throw error("Unclosed character class");
0N/A lastCodePoint = c;
0N/A }
0N/A
0N/A if (eq != null) {
0N/A result = "(?:"+charClass.toString()+eq.toString()+")";
0N/A } else {
0N/A result = charClass.toString();
0N/A }
0N/A
0N/A newPattern.append(result);
0N/A return i;
0N/A }
0N/A
0N/A /**
0N/A * Given a specific sequence composed of a regular character and
0N/A * combining marks that follow it, produce the alternation that will
0N/A * match all canonical equivalences of that sequence.
0N/A */
0N/A private String produceEquivalentAlternation(String source) {
0N/A int len = countChars(source, 0, 1);
0N/A if (source.length() == len)
0N/A // source has one character.
0N/A return source;
0N/A
0N/A String base = source.substring(0,len);
0N/A String combiningMarks = source.substring(len);
0N/A
0N/A String[] perms = producePermutations(combiningMarks);
0N/A StringBuilder result = new StringBuilder(source);
0N/A
0N/A // Add combined permutations
0N/A for(int x=0; x<perms.length; x++) {
0N/A String next = base + perms[x];
0N/A if (x>0)
0N/A result.append("|"+next);
0N/A next = composeOneStep(next);
0N/A if (next != null)
0N/A result.append("|"+produceEquivalentAlternation(next));
0N/A }
0N/A return result.toString();
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of strings that have all the possible
0N/A * permutations of the characters in the input string.
0N/A * This is used to get a list of all possible orderings
0N/A * of a set of combining marks. Note that some of the permutations
0N/A * are invalid because of combining class collisions, and these
0N/A * possibilities must be removed because they are not canonically
0N/A * equivalent.
0N/A */
0N/A private String[] producePermutations(String input) {
0N/A if (input.length() == countChars(input, 0, 1))
0N/A return new String[] {input};
0N/A
0N/A if (input.length() == countChars(input, 0, 2)) {
0N/A int c0 = Character.codePointAt(input, 0);
0N/A int c1 = Character.codePointAt(input, Character.charCount(c0));
0N/A if (getClass(c1) == getClass(c0)) {
0N/A return new String[] {input};
0N/A }
0N/A String[] result = new String[2];
0N/A result[0] = input;
0N/A StringBuilder sb = new StringBuilder(2);
0N/A sb.appendCodePoint(c1);
0N/A sb.appendCodePoint(c0);
0N/A result[1] = sb.toString();
0N/A return result;
0N/A }
0N/A
0N/A int length = 1;
0N/A int nCodePoints = countCodePoints(input);
0N/A for(int x=1; x<nCodePoints; x++)
0N/A length = length * (x+1);
0N/A
0N/A String[] temp = new String[length];
0N/A
0N/A int combClass[] = new int[nCodePoints];
0N/A for(int x=0, i=0; x<nCodePoints; x++) {
0N/A int c = Character.codePointAt(input, i);
0N/A combClass[x] = getClass(c);
0N/A i += Character.charCount(c);
0N/A }
0N/A
0N/A // For each char, take it out and add the permutations
0N/A // of the remaining chars
0N/A int index = 0;
0N/A int len;
0N/A // offset maintains the index in code units.
0N/Aloop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
0N/A len = countChars(input, offset, 1);
0N/A boolean skip = false;
0N/A for(int y=x-1; y>=0; y--) {
0N/A if (combClass[y] == combClass[x]) {
0N/A continue loop;
0N/A }
0N/A }
0N/A StringBuilder sb = new StringBuilder(input);
0N/A String otherChars = sb.delete(offset, offset+len).toString();
0N/A String[] subResult = producePermutations(otherChars);
0N/A
0N/A String prefix = input.substring(offset, offset+len);
0N/A for(int y=0; y<subResult.length; y++)
0N/A temp[index++] = prefix + subResult[y];
0N/A }
0N/A String[] result = new String[index];
0N/A for (int x=0; x<index; x++)
0N/A result[x] = temp[x];
0N/A return result;
0N/A }
0N/A
0N/A private int getClass(int c) {
0N/A return sun.text.Normalizer.getCombiningClass(c);
0N/A }
0N/A
0N/A /**
0N/A * Attempts to compose input by combining the first character
0N/A * with the first combining mark following it. Returns a String
0N/A * that is the composition of the leading character with its first
0N/A * combining mark followed by the remaining combining marks. Returns
0N/A * null if the first two characters cannot be further composed.
0N/A */
0N/A private String composeOneStep(String input) {
0N/A int len = countChars(input, 0, 2);
0N/A String firstTwoCharacters = input.substring(0, len);
0N/A String result = Normalizer.normalize(firstTwoCharacters, Normalizer.Form.NFC);
0N/A
0N/A if (result.equals(firstTwoCharacters))
0N/A return null;
0N/A else {
0N/A String remainder = input.substring(len);
0N/A return result + remainder;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Preprocess any \Q...\E sequences in `temp', meta-quoting them.
0N/A * See the description of `quotemeta' in perlfunc(1).
0N/A */
0N/A private void RemoveQEQuoting() {
0N/A final int pLen = patternLength;
0N/A int i = 0;
0N/A while (i < pLen-1) {
0N/A if (temp[i] != '\\')
0N/A i += 1;
0N/A else if (temp[i + 1] != 'Q')
0N/A i += 2;
0N/A else
0N/A break;
0N/A }
0N/A if (i >= pLen - 1) // No \Q sequence found
0N/A return;
0N/A int j = i;
0N/A i += 2;
0N/A int[] newtemp = new int[j + 2*(pLen-i) + 2];
0N/A System.arraycopy(temp, 0, newtemp, 0, j);
0N/A
0N/A boolean inQuote = true;
0N/A while (i < pLen) {
0N/A int c = temp[i++];
0N/A if (! ASCII.isAscii(c) || ASCII.isAlnum(c)) {
0N/A newtemp[j++] = c;
0N/A } else if (c != '\\') {
0N/A if (inQuote) newtemp[j++] = '\\';
0N/A newtemp[j++] = c;
0N/A } else if (inQuote) {
0N/A if (temp[i] == 'E') {
0N/A i++;
0N/A inQuote = false;
0N/A } else {
0N/A newtemp[j++] = '\\';
0N/A newtemp[j++] = '\\';
0N/A }
0N/A } else {
0N/A if (temp[i] == 'Q') {
0N/A i++;
0N/A inQuote = true;
0N/A } else {
0N/A newtemp[j++] = c;
0N/A if (i != pLen)
0N/A newtemp[j++] = temp[i++];
0N/A }
0N/A }
0N/A }
0N/A
0N/A patternLength = j;
0N/A temp = Arrays.copyOf(newtemp, j + 2); // double zero termination
0N/A }
0N/A
0N/A /**
0N/A * Copies regular expression to an int array and invokes the parsing
0N/A * of the expression which will create the object tree.
0N/A */
0N/A private void compile() {
0N/A // Handle canonical equivalences
0N/A if (has(CANON_EQ) && !has(LITERAL)) {
0N/A normalize();
0N/A } else {
0N/A normalizedPattern = pattern;
0N/A }
0N/A patternLength = normalizedPattern.length();
0N/A
0N/A // Copy pattern to int array for convenience
0N/A // Use double zero to terminate pattern
0N/A temp = new int[patternLength + 2];
0N/A
2118N/A hasSupplementary = false;
0N/A int c, count = 0;
0N/A // Convert all chars into code points
0N/A for (int x = 0; x < patternLength; x += Character.charCount(c)) {
0N/A c = normalizedPattern.codePointAt(x);
0N/A if (isSupplementary(c)) {
0N/A hasSupplementary = true;
0N/A }
0N/A temp[count++] = c;
0N/A }
0N/A
0N/A patternLength = count; // patternLength now in code points
0N/A
0N/A if (! has(LITERAL))
0N/A RemoveQEQuoting();
0N/A
0N/A // Allocate all temporary objects here.
0N/A buffer = new int[32];
0N/A groupNodes = new GroupHead[10];
906N/A namedGroups = null;
0N/A
0N/A if (has(LITERAL)) {
0N/A // Literal pattern handling
0N/A matchRoot = newSlice(temp, patternLength, hasSupplementary);
0N/A matchRoot.next = lastAccept;
0N/A } else {
0N/A // Start recursive descent parsing
0N/A matchRoot = expr(lastAccept);
0N/A // Check extra pattern characters
0N/A if (patternLength != cursor) {
0N/A if (peek() == ')') {
0N/A throw error("Unmatched closing ')'");
0N/A } else {
0N/A throw error("Unexpected internal error");
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Peephole optimization
0N/A if (matchRoot instanceof Slice) {
0N/A root = BnM.optimize(matchRoot);
0N/A if (root == matchRoot) {
0N/A root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
0N/A }
0N/A } else if (matchRoot instanceof Begin || matchRoot instanceof First) {
0N/A root = matchRoot;
0N/A } else {
0N/A root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
0N/A }
0N/A
0N/A // Release temporary storage
0N/A temp = null;
0N/A buffer = null;
0N/A groupNodes = null;
0N/A patternLength = 0;
0N/A compiled = true;
0N/A }
0N/A
906N/A Map<String, Integer> namedGroups() {
906N/A if (namedGroups == null)
3323N/A namedGroups = new HashMap<>(2);
906N/A return namedGroups;
906N/A }
906N/A
0N/A /**
0N/A * Used to print out a subtree of the Pattern to help with debugging.
0N/A */
0N/A private static void printObjectTree(Node node) {
0N/A while(node != null) {
0N/A if (node instanceof Prolog) {
0N/A System.out.println(node);
0N/A printObjectTree(((Prolog)node).loop);
0N/A System.out.println("**** end contents prolog loop");
0N/A } else if (node instanceof Loop) {
0N/A System.out.println(node);
0N/A printObjectTree(((Loop)node).body);
0N/A System.out.println("**** end contents Loop body");
0N/A } else if (node instanceof Curly) {
0N/A System.out.println(node);
0N/A printObjectTree(((Curly)node).atom);
0N/A System.out.println("**** end contents Curly body");
0N/A } else if (node instanceof GroupCurly) {
0N/A System.out.println(node);
0N/A printObjectTree(((GroupCurly)node).atom);
0N/A System.out.println("**** end contents GroupCurly body");
0N/A } else if (node instanceof GroupTail) {
0N/A System.out.println(node);
0N/A System.out.println("Tail next is "+node.next);
0N/A return;
0N/A } else {
0N/A System.out.println(node);
0N/A }
0N/A node = node.next;
0N/A if (node != null)
0N/A System.out.println("->next:");
0N/A if (node == Pattern.accept) {
0N/A System.out.println("Accept Node");
0N/A node = null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Used to accumulate information about a subtree of the object graph
0N/A * so that optimizations can be applied to the subtree.
0N/A */
0N/A static final class TreeInfo {
0N/A int minLength;
0N/A int maxLength;
0N/A boolean maxValid;
0N/A boolean deterministic;
0N/A
0N/A TreeInfo() {
0N/A reset();
0N/A }
0N/A void reset() {
0N/A minLength = 0;
0N/A maxLength = 0;
0N/A maxValid = true;
0N/A deterministic = true;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * The following private methods are mainly used to improve the
0N/A * readability of the code. In order to let the Java compiler easily
0N/A * inline them, we should not put many assertions or error checks in them.
0N/A */
0N/A
0N/A /**
0N/A * Indicates whether a particular flag is set or not.
0N/A */
0N/A private boolean has(int f) {
0N/A return (flags & f) != 0;
0N/A }
0N/A
0N/A /**
0N/A * Match next character, signal error if failed.
0N/A */
0N/A private void accept(int ch, String s) {
0N/A int testChar = temp[cursor++];
0N/A if (has(COMMENTS))
0N/A testChar = parsePastWhitespace(testChar);
0N/A if (ch != testChar) {
0N/A throw error(s);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Mark the end of pattern with a specific character.
0N/A */
0N/A private void mark(int c) {
0N/A temp[patternLength] = c;
0N/A }
0N/A
0N/A /**
0N/A * Peek the next character, and do not advance the cursor.
0N/A */
0N/A private int peek() {
0N/A int ch = temp[cursor];
0N/A if (has(COMMENTS))
0N/A ch = peekPastWhitespace(ch);
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * Read the next character, and advance the cursor by one.
0N/A */
0N/A private int read() {
0N/A int ch = temp[cursor++];
0N/A if (has(COMMENTS))
0N/A ch = parsePastWhitespace(ch);
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * Read the next character, and advance the cursor by one,
0N/A * ignoring the COMMENTS setting
0N/A */
0N/A private int readEscaped() {
0N/A int ch = temp[cursor++];
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * Advance the cursor by one, and peek the next character.
0N/A */
0N/A private int next() {
0N/A int ch = temp[++cursor];
0N/A if (has(COMMENTS))
0N/A ch = peekPastWhitespace(ch);
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * Advance the cursor by one, and peek the next character,
0N/A * ignoring the COMMENTS setting
0N/A */
0N/A private int nextEscaped() {
0N/A int ch = temp[++cursor];
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * If in xmode peek past whitespace and comments.
0N/A */
0N/A private int peekPastWhitespace(int ch) {
0N/A while (ASCII.isSpace(ch) || ch == '#') {
0N/A while (ASCII.isSpace(ch))
0N/A ch = temp[++cursor];
0N/A if (ch == '#') {
0N/A ch = peekPastLine();
0N/A }
0N/A }
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * If in xmode parse past whitespace and comments.
0N/A */
0N/A private int parsePastWhitespace(int ch) {
0N/A while (ASCII.isSpace(ch) || ch == '#') {
0N/A while (ASCII.isSpace(ch))
0N/A ch = temp[cursor++];
0N/A if (ch == '#')
0N/A ch = parsePastLine();
0N/A }
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * xmode parse past comment to end of line.
0N/A */
0N/A private int parsePastLine() {
0N/A int ch = temp[cursor++];
0N/A while (ch != 0 && !isLineSeparator(ch))
0N/A ch = temp[cursor++];
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * xmode peek past comment to end of line.
0N/A */
0N/A private int peekPastLine() {
0N/A int ch = temp[++cursor];
0N/A while (ch != 0 && !isLineSeparator(ch))
0N/A ch = temp[++cursor];
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * Determines if character is a line separator in the current mode
0N/A */
0N/A private boolean isLineSeparator(int ch) {
0N/A if (has(UNIX_LINES)) {
0N/A return ch == '\n';
0N/A } else {
0N/A return (ch == '\n' ||
0N/A ch == '\r' ||
0N/A (ch|1) == '\u2029' ||
0N/A ch == '\u0085');
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Read the character after the next one, and advance the cursor by two.
0N/A */
0N/A private int skip() {
0N/A int i = cursor;
0N/A int ch = temp[i+1];
0N/A cursor = i + 2;
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * Unread one next character, and retreat cursor by one.
0N/A */
0N/A private void unread() {
0N/A cursor--;
0N/A }
0N/A
0N/A /**
0N/A * Internal method used for handling all syntax errors. The pattern is
0N/A * displayed with a pointer to aid in locating the syntax error.
0N/A */
0N/A private PatternSyntaxException error(String s) {
0N/A return new PatternSyntaxException(s, normalizedPattern, cursor - 1);
0N/A }
0N/A
0N/A /**
0N/A * Determines if there is any supplementary character or unpaired
0N/A * surrogate in the specified range.
0N/A */
0N/A private boolean findSupplementary(int start, int end) {
0N/A for (int i = start; i < end; i++) {
0N/A if (isSupplementary(temp[i]))
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Determines if the specified code point is a supplementary
0N/A * character or unpaired surrogate.
0N/A */
0N/A private static final boolean isSupplementary(int ch) {
2118N/A return ch >= Character.MIN_SUPPLEMENTARY_CODE_POINT ||
2118N/A Character.isSurrogate((char)ch);
0N/A }
0N/A
0N/A /**
0N/A * The following methods handle the main parsing. They are sorted
0N/A * according to their precedence order, the lowest one first.
0N/A */
0N/A
0N/A /**
0N/A * The expression is parsed with branch nodes added for alternations.
0N/A * This may be called recursively to parse sub expressions that may
0N/A * contain alternations.
0N/A */
0N/A private Node expr(Node end) {
0N/A Node prev = null;
0N/A Node firstTail = null;
0N/A Node branchConn = null;
0N/A
0N/A for (;;) {
0N/A Node node = sequence(end);
0N/A Node nodeTail = root; //double return
0N/A if (prev == null) {
0N/A prev = node;
0N/A firstTail = nodeTail;
0N/A } else {
0N/A // Branch
0N/A if (branchConn == null) {
0N/A branchConn = new BranchConn();
0N/A branchConn.next = end;
0N/A }
0N/A if (node == end) {
0N/A // if the node returned from sequence() is "end"
0N/A // we have an empty expr, set a null atom into
0N/A // the branch to indicate to go "next" directly.
0N/A node = null;
0N/A } else {
0N/A // the "tail.next" of each atom goes to branchConn
0N/A nodeTail.next = branchConn;
0N/A }
0N/A if (prev instanceof Branch) {
0N/A ((Branch)prev).add(node);
0N/A } else {
0N/A if (prev == end) {
0N/A prev = null;
0N/A } else {
0N/A // replace the "end" with "branchConn" at its tail.next
0N/A // when put the "prev" into the branch as the first atom.
0N/A firstTail.next = branchConn;
0N/A }
0N/A prev = new Branch(prev, node, branchConn);
0N/A }
0N/A }
0N/A if (peek() != '|') {
0N/A return prev;
0N/A }
0N/A next();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Parsing of sequences between alternations.
0N/A */
0N/A private Node sequence(Node end) {
0N/A Node head = null;
0N/A Node tail = null;
0N/A Node node = null;
0N/A LOOP:
0N/A for (;;) {
0N/A int ch = peek();
0N/A switch (ch) {
0N/A case '(':
0N/A // Because group handles its own closure,
0N/A // we need to treat it differently
0N/A node = group0();
0N/A // Check for comment or flag group
0N/A if (node == null)
0N/A continue;
0N/A if (head == null)
0N/A head = node;
0N/A else
0N/A tail.next = node;
0N/A // Double return: Tail was returned in root
0N/A tail = root;
0N/A continue;
0N/A case '[':
0N/A node = clazz(true);
0N/A break;
0N/A case '\\':
0N/A ch = nextEscaped();
0N/A if (ch == 'p' || ch == 'P') {
0N/A boolean oneLetter = true;
0N/A boolean comp = (ch == 'P');
0N/A ch = next(); // Consume { if present
0N/A if (ch != '{') {
0N/A unread();
0N/A } else {
0N/A oneLetter = false;
0N/A }
2118N/A node = family(oneLetter, comp);
0N/A } else {
0N/A unread();
0N/A node = atom();
0N/A }
0N/A break;
0N/A case '^':
0N/A next();
0N/A if (has(MULTILINE)) {
0N/A if (has(UNIX_LINES))
0N/A node = new UnixCaret();
0N/A else
0N/A node = new Caret();
0N/A } else {
0N/A node = new Begin();
0N/A }
0N/A break;
0N/A case '$':
0N/A next();
0N/A if (has(UNIX_LINES))
0N/A node = new UnixDollar(has(MULTILINE));
0N/A else
0N/A node = new Dollar(has(MULTILINE));
0N/A break;
0N/A case '.':
0N/A next();
0N/A if (has(DOTALL)) {
0N/A node = new All();
0N/A } else {
0N/A if (has(UNIX_LINES))
0N/A node = new UnixDot();
0N/A else {
0N/A node = new Dot();
0N/A }
0N/A }
0N/A break;
0N/A case '|':
0N/A case ')':
0N/A break LOOP;
0N/A case ']': // Now interpreting dangling ] and } as literals
0N/A case '}':
0N/A node = atom();
0N/A break;
0N/A case '?':
0N/A case '*':
0N/A case '+':
0N/A next();
0N/A throw error("Dangling meta character '" + ((char)ch) + "'");
0N/A case 0:
0N/A if (cursor >= patternLength) {
0N/A break LOOP;
0N/A }
0N/A // Fall through
0N/A default:
0N/A node = atom();
0N/A break;
0N/A }
0N/A
0N/A node = closure(node);
0N/A
0N/A if (head == null) {
0N/A head = tail = node;
0N/A } else {
0N/A tail.next = node;
0N/A tail = node;
0N/A }
0N/A }
0N/A if (head == null) {
0N/A return end;
0N/A }
0N/A tail.next = end;
0N/A root = tail; //double return
0N/A return head;
0N/A }
0N/A
0N/A /**
0N/A * Parse and add a new Single or Slice.
0N/A */
0N/A private Node atom() {
0N/A int first = 0;
0N/A int prev = -1;
0N/A boolean hasSupplementary = false;
0N/A int ch = peek();
0N/A for (;;) {
0N/A switch (ch) {
0N/A case '*':
0N/A case '+':
0N/A case '?':
0N/A case '{':
0N/A if (first > 1) {
0N/A cursor = prev; // Unwind one character
0N/A first--;
0N/A }
0N/A break;
0N/A case '$':
0N/A case '.':
0N/A case '^':
0N/A case '(':
0N/A case '[':
0N/A case '|':
0N/A case ')':
0N/A break;
0N/A case '\\':
0N/A ch = nextEscaped();
0N/A if (ch == 'p' || ch == 'P') { // Property
0N/A if (first > 0) { // Slice is waiting; handle it first
0N/A unread();
0N/A break;
0N/A } else { // No slice; just return the family node
0N/A boolean comp = (ch == 'P');
0N/A boolean oneLetter = true;
0N/A ch = next(); // Consume { if present
0N/A if (ch != '{')
0N/A unread();
0N/A else
0N/A oneLetter = false;
2118N/A return family(oneLetter, comp);
0N/A }
0N/A }
0N/A unread();
0N/A prev = cursor;
0N/A ch = escape(false, first == 0);
0N/A if (ch >= 0) {
0N/A append(ch, first);
0N/A first++;
0N/A if (isSupplementary(ch)) {
0N/A hasSupplementary = true;
0N/A }
0N/A ch = peek();
0N/A continue;
0N/A } else if (first == 0) {
0N/A return root;
0N/A }
0N/A // Unwind meta escape sequence
0N/A cursor = prev;
0N/A break;
0N/A case 0:
0N/A if (cursor >= patternLength) {
0N/A break;
0N/A }
0N/A // Fall through
0N/A default:
0N/A prev = cursor;
0N/A append(ch, first);
0N/A first++;
0N/A if (isSupplementary(ch)) {
0N/A hasSupplementary = true;
0N/A }
0N/A ch = next();
0N/A continue;
0N/A }
0N/A break;
0N/A }
0N/A if (first == 1) {
0N/A return newSingle(buffer[0]);
0N/A } else {
0N/A return newSlice(buffer, first, hasSupplementary);
0N/A }
0N/A }
0N/A
0N/A private void append(int ch, int len) {
0N/A if (len >= buffer.length) {
0N/A int[] tmp = new int[len+len];
0N/A System.arraycopy(buffer, 0, tmp, 0, len);
0N/A buffer = tmp;
0N/A }
0N/A buffer[len] = ch;
0N/A }
0N/A
0N/A /**
0N/A * Parses a backref greedily, taking as many numbers as it
0N/A * can. The first digit is always treated as a backref, but
0N/A * multi digit numbers are only treated as a backref if at
0N/A * least that many backrefs exist at this point in the regex.
0N/A */
0N/A private Node ref(int refNum) {
0N/A boolean done = false;
0N/A while(!done) {
0N/A int ch = peek();
0N/A switch(ch) {
0N/A case '0':
0N/A case '1':
0N/A case '2':
0N/A case '3':
0N/A case '4':
0N/A case '5':
0N/A case '6':
0N/A case '7':
0N/A case '8':
0N/A case '9':
0N/A int newRefNum = (refNum * 10) + (ch - '0');
0N/A // Add another number if it doesn't make a group
0N/A // that doesn't exist
0N/A if (capturingGroupCount - 1 < newRefNum) {
0N/A done = true;
0N/A break;
0N/A }
0N/A refNum = newRefNum;
0N/A read();
0N/A break;
0N/A default:
0N/A done = true;
0N/A break;
0N/A }
0N/A }
0N/A if (has(CASE_INSENSITIVE))
0N/A return new CIBackRef(refNum, has(UNICODE_CASE));
0N/A else
0N/A return new BackRef(refNum);
0N/A }
0N/A
0N/A /**
0N/A * Parses an escape sequence to determine the actual value that needs
0N/A * to be matched.
0N/A * If -1 is returned and create was true a new object was added to the tree
0N/A * to handle the escape sequence.
0N/A * If the returned value is greater than zero, it is the value that
0N/A * matches the escape sequence.
0N/A */
0N/A private int escape(boolean inclass, boolean create) {
0N/A int ch = skip();
0N/A switch (ch) {
0N/A case '0':
0N/A return o();
0N/A case '1':
0N/A case '2':
0N/A case '3':
0N/A case '4':
0N/A case '5':
0N/A case '6':
0N/A case '7':
0N/A case '8':
0N/A case '9':
0N/A if (inclass) break;
0N/A if (create) {
0N/A root = ref((ch - '0'));
0N/A }
0N/A return -1;
0N/A case 'A':
0N/A if (inclass) break;
0N/A if (create) root = new Begin();
0N/A return -1;
0N/A case 'B':
0N/A if (inclass) break;
4139N/A if (create) root = new Bound(Bound.NONE, has(UNICODE_CHARACTER_CLASS));
0N/A return -1;
0N/A case 'C':
0N/A break;
0N/A case 'D':
4139N/A if (create) root = has(UNICODE_CHARACTER_CLASS)
4139N/A ? new Utype(UnicodeProp.DIGIT).complement()
4139N/A : new Ctype(ASCII.DIGIT).complement();
0N/A return -1;
0N/A case 'E':
0N/A case 'F':
0N/A break;
0N/A case 'G':
0N/A if (inclass) break;
0N/A if (create) root = new LastMatch();
0N/A return -1;
0N/A case 'H':
0N/A case 'I':
0N/A case 'J':
0N/A case 'K':
0N/A case 'L':
0N/A case 'M':
0N/A case 'N':
0N/A case 'O':
0N/A case 'P':
0N/A case 'Q':
0N/A case 'R':
0N/A break;
0N/A case 'S':
4139N/A if (create) root = has(UNICODE_CHARACTER_CLASS)
4139N/A ? new Utype(UnicodeProp.WHITE_SPACE).complement()
4139N/A : new Ctype(ASCII.SPACE).complement();
0N/A return -1;
0N/A case 'T':
0N/A case 'U':
0N/A case 'V':
0N/A break;
0N/A case 'W':
4139N/A if (create) root = has(UNICODE_CHARACTER_CLASS)
4139N/A ? new Utype(UnicodeProp.WORD).complement()
4139N/A : new Ctype(ASCII.WORD).complement();
0N/A return -1;
0N/A case 'X':
0N/A case 'Y':
0N/A break;
0N/A case 'Z':
0N/A if (inclass) break;
0N/A if (create) {
0N/A if (has(UNIX_LINES))
0N/A root = new UnixDollar(false);
0N/A else
0N/A root = new Dollar(false);
0N/A }
0N/A return -1;
0N/A case 'a':
0N/A return '\007';
0N/A case 'b':
0N/A if (inclass) break;
4139N/A if (create) root = new Bound(Bound.BOTH, has(UNICODE_CHARACTER_CLASS));
0N/A return -1;
0N/A case 'c':
0N/A return c();
0N/A case 'd':
4139N/A if (create) root = has(UNICODE_CHARACTER_CLASS)
4139N/A ? new Utype(UnicodeProp.DIGIT)
4139N/A : new Ctype(ASCII.DIGIT);
0N/A return -1;
0N/A case 'e':
0N/A return '\033';
0N/A case 'f':
0N/A return '\f';
0N/A case 'g':
0N/A case 'h':
0N/A case 'i':
0N/A case 'j':
906N/A break;
0N/A case 'k':
906N/A if (inclass)
906N/A break;
906N/A if (read() != '<')
906N/A throw error("\\k is not followed by '<' for named capturing group");
906N/A String name = groupname(read());
906N/A if (!namedGroups().containsKey(name))
906N/A throw error("(named capturing group <"+ name+"> does not exit");
906N/A if (create) {
906N/A if (has(CASE_INSENSITIVE))
906N/A root = new CIBackRef(namedGroups().get(name), has(UNICODE_CASE));
906N/A else
906N/A root = new BackRef(namedGroups().get(name));
906N/A }
906N/A return -1;
0N/A case 'l':
0N/A case 'm':
0N/A break;
0N/A case 'n':
0N/A return '\n';
0N/A case 'o':
0N/A case 'p':
0N/A case 'q':
0N/A break;
0N/A case 'r':
0N/A return '\r';
0N/A case 's':
4139N/A if (create) root = has(UNICODE_CHARACTER_CLASS)
4139N/A ? new Utype(UnicodeProp.WHITE_SPACE)
4139N/A : new Ctype(ASCII.SPACE);
0N/A return -1;
0N/A case 't':
0N/A return '\t';
0N/A case 'u':
0N/A return u();
0N/A case 'v':
0N/A return '\013';
0N/A case 'w':
4139N/A if (create) root = has(UNICODE_CHARACTER_CLASS)
4139N/A ? new Utype(UnicodeProp.WORD)
4139N/A : new Ctype(ASCII.WORD);
0N/A return -1;
0N/A case 'x':
0N/A return x();
0N/A case 'y':
0N/A break;
0N/A case 'z':
0N/A if (inclass) break;
0N/A if (create) root = new End();
0N/A return -1;
0N/A default:
0N/A return ch;
0N/A }
0N/A throw error("Illegal/unsupported escape sequence");
0N/A }
0N/A
0N/A /**
0N/A * Parse a character class, and return the node that matches it.
0N/A *
0N/A * Consumes a ] on the way out if consume is true. Usually consume
0N/A * is true except for the case of [abc&&def] where def is a separate
0N/A * right hand node with "understood" brackets.
0N/A */
0N/A private CharProperty clazz(boolean consume) {
0N/A CharProperty prev = null;
0N/A CharProperty node = null;
0N/A BitClass bits = new BitClass();
0N/A boolean include = true;
0N/A boolean firstInClass = true;
0N/A int ch = next();
0N/A for (;;) {
0N/A switch (ch) {
0N/A case '^':
0N/A // Negates if first char in a class, otherwise literal
0N/A if (firstInClass) {
0N/A if (temp[cursor-1] != '[')
0N/A break;
0N/A ch = next();
0N/A include = !include;
0N/A continue;
0N/A } else {
0N/A // ^ not first in class, treat as literal
0N/A break;
0N/A }
0N/A case '[':
0N/A firstInClass = false;
0N/A node = clazz(true);
0N/A if (prev == null)
0N/A prev = node;
0N/A else
0N/A prev = union(prev, node);
0N/A ch = peek();
0N/A continue;
0N/A case '&':
0N/A firstInClass = false;
0N/A ch = next();
0N/A if (ch == '&') {
0N/A ch = next();
0N/A CharProperty rightNode = null;
0N/A while (ch != ']' && ch != '&') {
0N/A if (ch == '[') {
0N/A if (rightNode == null)
0N/A rightNode = clazz(true);
0N/A else
0N/A rightNode = union(rightNode, clazz(true));
0N/A } else { // abc&&def
0N/A unread();
0N/A rightNode = clazz(false);
0N/A }
0N/A ch = peek();
0N/A }
0N/A if (rightNode != null)
0N/A node = rightNode;
0N/A if (prev == null) {
0N/A if (rightNode == null)
0N/A throw error("Bad class syntax");
0N/A else
0N/A prev = rightNode;
0N/A } else {
0N/A prev = intersection(prev, node);
0N/A }
0N/A } else {
0N/A // treat as a literal &
0N/A unread();
0N/A break;
0N/A }
0N/A continue;
0N/A case 0:
0N/A firstInClass = false;
0N/A if (cursor >= patternLength)
0N/A throw error("Unclosed character class");
0N/A break;
0N/A case ']':
0N/A firstInClass = false;
0N/A if (prev != null) {
0N/A if (consume)
0N/A next();
0N/A return prev;
0N/A }
0N/A break;
0N/A default:
0N/A firstInClass = false;
0N/A break;
0N/A }
0N/A node = range(bits);
0N/A if (include) {
0N/A if (prev == null) {
0N/A prev = node;
0N/A } else {
0N/A if (prev != node)
0N/A prev = union(prev, node);
0N/A }
0N/A } else {
0N/A if (prev == null) {
0N/A prev = node.complement();
0N/A } else {
0N/A if (prev != node)
0N/A prev = setDifference(prev, node);
0N/A }
0N/A }
0N/A ch = peek();
0N/A }
0N/A }
0N/A
0N/A private CharProperty bitsOrSingle(BitClass bits, int ch) {
0N/A /* Bits can only handle codepoints in [u+0000-u+00ff] range.
0N/A Use "single" node instead of bits when dealing with unicode
0N/A case folding for codepoints listed below.
0N/A (1)Uppercase out of range: u+00ff, u+00b5
0N/A toUpperCase(u+00ff) -> u+0178
0N/A toUpperCase(u+00b5) -> u+039c
0N/A (2)LatinSmallLetterLongS u+17f
0N/A toUpperCase(u+017f) -> u+0053
0N/A (3)LatinSmallLetterDotlessI u+131
0N/A toUpperCase(u+0131) -> u+0049
0N/A (4)LatinCapitalLetterIWithDotAbove u+0130
0N/A toLowerCase(u+0130) -> u+0069
0N/A (5)KelvinSign u+212a
0N/A toLowerCase(u+212a) ==> u+006B
0N/A (6)AngstromSign u+212b
0N/A toLowerCase(u+212b) ==> u+00e5
0N/A */
0N/A int d;
0N/A if (ch < 256 &&
0N/A !(has(CASE_INSENSITIVE) && has(UNICODE_CASE) &&
0N/A (ch == 0xff || ch == 0xb5 ||
0N/A ch == 0x49 || ch == 0x69 || //I and i
0N/A ch == 0x53 || ch == 0x73 || //S and s
0N/A ch == 0x4b || ch == 0x6b || //K and k
0N/A ch == 0xc5 || ch == 0xe5))) //A+ring
0N/A return bits.add(ch, flags());
0N/A return newSingle(ch);
0N/A }
0N/A
0N/A /**
0N/A * Parse a single character or a character range in a character class
0N/A * and return its representative node.
0N/A */
0N/A private CharProperty range(BitClass bits) {
0N/A int ch = peek();
0N/A if (ch == '\\') {
0N/A ch = nextEscaped();
0N/A if (ch == 'p' || ch == 'P') { // A property
0N/A boolean comp = (ch == 'P');
0N/A boolean oneLetter = true;
0N/A // Consume { if present
0N/A ch = next();
0N/A if (ch != '{')
0N/A unread();
0N/A else
0N/A oneLetter = false;
2118N/A return family(oneLetter, comp);
0N/A } else { // ordinary escape
0N/A unread();
0N/A ch = escape(true, true);
0N/A if (ch == -1)
0N/A return (CharProperty) root;
0N/A }
0N/A } else {
0N/A ch = single();
0N/A }
0N/A if (ch >= 0) {
0N/A if (peek() == '-') {
0N/A int endRange = temp[cursor+1];
0N/A if (endRange == '[') {
0N/A return bitsOrSingle(bits, ch);
0N/A }
0N/A if (endRange != ']') {
0N/A next();
0N/A int m = single();
0N/A if (m < ch)
0N/A throw error("Illegal character range");
0N/A if (has(CASE_INSENSITIVE))
0N/A return caseInsensitiveRangeFor(ch, m);
0N/A else
0N/A return rangeFor(ch, m);
0N/A }
0N/A }
0N/A return bitsOrSingle(bits, ch);
0N/A }
0N/A throw error("Unexpected character '"+((char)ch)+"'");
0N/A }
0N/A
0N/A private int single() {
0N/A int ch = peek();
0N/A switch (ch) {
0N/A case '\\':
0N/A return escape(true, false);
0N/A default:
0N/A next();
0N/A return ch;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Parses a Unicode character family and returns its representative node.
0N/A */
2118N/A private CharProperty family(boolean singleLetter,
2118N/A boolean maybeComplement)
2118N/A {
0N/A next();
0N/A String name;
4139N/A CharProperty node = null;
0N/A
0N/A if (singleLetter) {
0N/A int c = temp[cursor];
0N/A if (!Character.isSupplementaryCodePoint(c)) {
0N/A name = String.valueOf((char)c);
0N/A } else {
0N/A name = new String(temp, cursor, 1);
0N/A }
0N/A read();
0N/A } else {
0N/A int i = cursor;
0N/A mark('}');
0N/A while(read() != '}') {
0N/A }
0N/A mark('\000');
0N/A int j = cursor;
0N/A if (j > patternLength)
0N/A throw error("Unclosed character family");
0N/A if (i + 1 >= j)
0N/A throw error("Empty character family");
0N/A name = new String(temp, i, j-i-1);
0N/A }
0N/A
2401N/A int i = name.indexOf('=');
2401N/A if (i != -1) {
2401N/A // property construct \p{name=value}
2401N/A String value = name.substring(i + 1);
2401N/A name = name.substring(0, i).toLowerCase(Locale.ENGLISH);
2401N/A if ("sc".equals(name) || "script".equals(name)) {
2401N/A node = unicodeScriptPropertyFor(value);
2401N/A } else if ("blk".equals(name) || "block".equals(name)) {
2401N/A node = unicodeBlockPropertyFor(value);
2401N/A } else if ("gc".equals(name) || "general_category".equals(name)) {
2401N/A node = charPropertyNodeFor(value);
2401N/A } else {
2401N/A throw error("Unknown Unicode property {name=<" + name + ">, "
2401N/A + "value=<" + value + ">}");
2401N/A }
0N/A } else {
2401N/A if (name.startsWith("In")) {
2401N/A // \p{inBlockName}
2401N/A node = unicodeBlockPropertyFor(name.substring(2));
2401N/A } else if (name.startsWith("Is")) {
2401N/A // \p{isGeneralCategory} and \p{isScriptName}
0N/A name = name.substring(2);
4139N/A UnicodeProp uprop = UnicodeProp.forName(name);
4139N/A if (uprop != null)
4139N/A node = new Utype(uprop);
4139N/A if (node == null)
4139N/A node = CharPropertyNames.charPropertyFor(name);
2401N/A if (node == null)
2401N/A node = unicodeScriptPropertyFor(name);
2401N/A } else {
4139N/A if (has(UNICODE_CHARACTER_CLASS)) {
4139N/A UnicodeProp uprop = UnicodeProp.forPOSIXName(name);
4139N/A if (uprop != null)
4139N/A node = new Utype(uprop);
4139N/A }
4139N/A if (node == null)
4139N/A node = charPropertyNodeFor(name);
2401N/A }
2118N/A }
2118N/A if (maybeComplement) {
2118N/A if (node instanceof Category || node instanceof Block)
2118N/A hasSupplementary = true;
2118N/A node = node.complement();
2118N/A }
2118N/A return node;
0N/A }
0N/A
2401N/A
2401N/A /**
2401N/A * Returns a CharProperty matching all characters belong to
2401N/A * a UnicodeScript.
2401N/A */
2401N/A private CharProperty unicodeScriptPropertyFor(String name) {
2401N/A final Character.UnicodeScript script;
2401N/A try {
2401N/A script = Character.UnicodeScript.forName(name);
2401N/A } catch (IllegalArgumentException iae) {
2401N/A throw error("Unknown character script name {" + name + "}");
2401N/A }
2401N/A return new Script(script);
2401N/A }
2401N/A
0N/A /**
0N/A * Returns a CharProperty matching all characters in a UnicodeBlock.
0N/A */
0N/A private CharProperty unicodeBlockPropertyFor(String name) {
0N/A final Character.UnicodeBlock block;
0N/A try {
0N/A block = Character.UnicodeBlock.forName(name);
0N/A } catch (IllegalArgumentException iae) {
0N/A throw error("Unknown character block name {" + name + "}");
0N/A }
2118N/A return new Block(block);
0N/A }
0N/A
0N/A /**
0N/A * Returns a CharProperty matching all characters in a named property.
0N/A */
0N/A private CharProperty charPropertyNodeFor(String name) {
0N/A CharProperty p = CharPropertyNames.charPropertyFor(name);
0N/A if (p == null)
0N/A throw error("Unknown character property name {" + name + "}");
0N/A return p;
0N/A }
0N/A
0N/A /**
906N/A * Parses and returns the name of a "named capturing group", the trailing
906N/A * ">" is consumed after parsing.
906N/A */
906N/A private String groupname(int ch) {
906N/A StringBuilder sb = new StringBuilder();
906N/A sb.append(Character.toChars(ch));
906N/A while (ASCII.isLower(ch=read()) || ASCII.isUpper(ch) ||
906N/A ASCII.isDigit(ch)) {
906N/A sb.append(Character.toChars(ch));
906N/A }
906N/A if (sb.length() == 0)
906N/A throw error("named capturing group has 0 length name");
906N/A if (ch != '>')
906N/A throw error("named capturing group is missing trailing '>'");
906N/A return sb.toString();
906N/A }
906N/A
906N/A /**
0N/A * Parses a group and returns the head node of a set of nodes that process
0N/A * the group. Sometimes a double return system is used where the tail is
0N/A * returned in root.
0N/A */
0N/A private Node group0() {
0N/A boolean capturingGroup = false;
0N/A Node head = null;
0N/A Node tail = null;
0N/A int save = flags;
0N/A root = null;
0N/A int ch = next();
0N/A if (ch == '?') {
0N/A ch = skip();
0N/A switch (ch) {
0N/A case ':': // (?:xxx) pure group
0N/A head = createGroup(true);
0N/A tail = root;
0N/A head.next = expr(tail);
0N/A break;
0N/A case '=': // (?=xxx) and (?!xxx) lookahead
0N/A case '!':
0N/A head = createGroup(true);
0N/A tail = root;
0N/A head.next = expr(tail);
0N/A if (ch == '=') {
0N/A head = tail = new Pos(head);
0N/A } else {
0N/A head = tail = new Neg(head);
0N/A }
0N/A break;
0N/A case '>': // (?>xxx) independent group
0N/A head = createGroup(true);
0N/A tail = root;
0N/A head.next = expr(tail);
0N/A head = tail = new Ques(head, INDEPENDENT);
0N/A break;
0N/A case '<': // (?<xxx) look behind
0N/A ch = read();
1795N/A if (ASCII.isLower(ch) || ASCII.isUpper(ch)) {
960N/A // named captured group
906N/A String name = groupname(ch);
906N/A if (namedGroups().containsKey(name))
906N/A throw error("Named capturing group <" + name
906N/A + "> is already defined");
906N/A capturingGroup = true;
906N/A head = createGroup(false);
906N/A tail = root;
906N/A namedGroups().put(name, capturingGroupCount-1);
906N/A head.next = expr(tail);
906N/A break;
906N/A }
0N/A int start = cursor;
0N/A head = createGroup(true);
0N/A tail = root;
0N/A head.next = expr(tail);
0N/A tail.next = lookbehindEnd;
0N/A TreeInfo info = new TreeInfo();
0N/A head.study(info);
0N/A if (info.maxValid == false) {
0N/A throw error("Look-behind group does not have "
0N/A + "an obvious maximum length");
0N/A }
0N/A boolean hasSupplementary = findSupplementary(start, patternLength);
0N/A if (ch == '=') {
0N/A head = tail = (hasSupplementary ?
0N/A new BehindS(head, info.maxLength,
0N/A info.minLength) :
0N/A new Behind(head, info.maxLength,
0N/A info.minLength));
0N/A } else if (ch == '!') {
0N/A head = tail = (hasSupplementary ?
0N/A new NotBehindS(head, info.maxLength,
0N/A info.minLength) :
0N/A new NotBehind(head, info.maxLength,
0N/A info.minLength));
0N/A } else {
0N/A throw error("Unknown look-behind group");
0N/A }
0N/A break;
0N/A case '$':
0N/A case '@':
0N/A throw error("Unknown group type");
0N/A default: // (?xxx:) inlined match flags
0N/A unread();
0N/A addFlag();
0N/A ch = read();
0N/A if (ch == ')') {
0N/A return null; // Inline modifier only
0N/A }
0N/A if (ch != ':') {
0N/A throw error("Unknown inline modifier");
0N/A }
0N/A head = createGroup(true);
0N/A tail = root;
0N/A head.next = expr(tail);
0N/A break;
0N/A }
0N/A } else { // (xxx) a regular group
0N/A capturingGroup = true;
0N/A head = createGroup(false);
0N/A tail = root;
0N/A head.next = expr(tail);
0N/A }
0N/A
0N/A accept(')', "Unclosed group");
0N/A flags = save;
0N/A
0N/A // Check for quantifiers
0N/A Node node = closure(head);
0N/A if (node == head) { // No closure
0N/A root = tail;
0N/A return node; // Dual return
0N/A }
0N/A if (head == tail) { // Zero length assertion
0N/A root = node;
0N/A return node; // Dual return
0N/A }
0N/A
0N/A if (node instanceof Ques) {
0N/A Ques ques = (Ques) node;
0N/A if (ques.type == POSSESSIVE) {
0N/A root = node;
0N/A return node;
0N/A }
0N/A tail.next = new BranchConn();
0N/A tail = tail.next;
0N/A if (ques.type == GREEDY) {
0N/A head = new Branch(head, null, tail);
0N/A } else { // Reluctant quantifier
0N/A head = new Branch(null, head, tail);
0N/A }
0N/A root = tail;
0N/A return head;
0N/A } else if (node instanceof Curly) {
0N/A Curly curly = (Curly) node;
0N/A if (curly.type == POSSESSIVE) {
0N/A root = node;
0N/A return node;
0N/A }
0N/A // Discover if the group is deterministic
0N/A TreeInfo info = new TreeInfo();
0N/A if (head.study(info)) { // Deterministic
0N/A GroupTail temp = (GroupTail) tail;
0N/A head = root = new GroupCurly(head.next, curly.cmin,
0N/A curly.cmax, curly.type,
0N/A ((GroupTail)tail).localIndex,
0N/A ((GroupTail)tail).groupIndex,
0N/A capturingGroup);
0N/A return head;
0N/A } else { // Non-deterministic
0N/A int temp = ((GroupHead) head).localIndex;
0N/A Loop loop;
0N/A if (curly.type == GREEDY)
0N/A loop = new Loop(this.localCount, temp);
0N/A else // Reluctant Curly
0N/A loop = new LazyLoop(this.localCount, temp);
0N/A Prolog prolog = new Prolog(loop);
0N/A this.localCount += 1;
0N/A loop.cmin = curly.cmin;
0N/A loop.cmax = curly.cmax;
0N/A loop.body = head;
0N/A tail.next = loop;
0N/A root = loop;
0N/A return prolog; // Dual return
0N/A }
0N/A }
0N/A throw error("Internal logic error");
0N/A }
0N/A
0N/A /**
0N/A * Create group head and tail nodes using double return. If the group is
0N/A * created with anonymous true then it is a pure group and should not
0N/A * affect group counting.
0N/A */
0N/A private Node createGroup(boolean anonymous) {
0N/A int localIndex = localCount++;
0N/A int groupIndex = 0;
0N/A if (!anonymous)
0N/A groupIndex = capturingGroupCount++;
0N/A GroupHead head = new GroupHead(localIndex);
0N/A root = new GroupTail(localIndex, groupIndex);
0N/A if (!anonymous && groupIndex < 10)
0N/A groupNodes[groupIndex] = head;
0N/A return head;
0N/A }
0N/A
0N/A /**
0N/A * Parses inlined match flags and set them appropriately.
0N/A */
0N/A private void addFlag() {
0N/A int ch = peek();
0N/A for (;;) {
0N/A switch (ch) {
0N/A case 'i':
0N/A flags |= CASE_INSENSITIVE;
0N/A break;
0N/A case 'm':
0N/A flags |= MULTILINE;
0N/A break;
0N/A case 's':
0N/A flags |= DOTALL;
0N/A break;
0N/A case 'd':
0N/A flags |= UNIX_LINES;
0N/A break;
0N/A case 'u':
0N/A flags |= UNICODE_CASE;
0N/A break;
0N/A case 'c':
0N/A flags |= CANON_EQ;
0N/A break;
0N/A case 'x':
0N/A flags |= COMMENTS;
0N/A break;
4139N/A case 'U':
4139N/A flags |= (UNICODE_CHARACTER_CLASS | UNICODE_CASE);
4139N/A break;
0N/A case '-': // subFlag then fall through
0N/A ch = next();
0N/A subFlag();
0N/A default:
0N/A return;
0N/A }
0N/A ch = next();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Parses the second part of inlined match flags and turns off
0N/A * flags appropriately.
0N/A */
0N/A private void subFlag() {
0N/A int ch = peek();
0N/A for (;;) {
0N/A switch (ch) {
0N/A case 'i':
0N/A flags &= ~CASE_INSENSITIVE;
0N/A break;
0N/A case 'm':
0N/A flags &= ~MULTILINE;
0N/A break;
0N/A case 's':
0N/A flags &= ~DOTALL;
0N/A break;
0N/A case 'd':
0N/A flags &= ~UNIX_LINES;
0N/A break;
0N/A case 'u':
0N/A flags &= ~UNICODE_CASE;
0N/A break;
0N/A case 'c':
0N/A flags &= ~CANON_EQ;
0N/A break;
0N/A case 'x':
0N/A flags &= ~COMMENTS;
0N/A break;
4139N/A case 'U':
4139N/A flags &= ~(UNICODE_CHARACTER_CLASS | UNICODE_CASE);
0N/A default:
0N/A return;
0N/A }
0N/A ch = next();
0N/A }
0N/A }
0N/A
0N/A static final int MAX_REPS = 0x7FFFFFFF;
0N/A
0N/A static final int GREEDY = 0;
0N/A
0N/A static final int LAZY = 1;
0N/A
0N/A static final int POSSESSIVE = 2;
0N/A
0N/A static final int INDEPENDENT = 3;
0N/A
0N/A /**
0N/A * Processes repetition. If the next character peeked is a quantifier
0N/A * then new nodes must be appended to handle the repetition.
0N/A * Prev could be a single or a group, so it could be a chain of nodes.
0N/A */
0N/A private Node closure(Node prev) {
0N/A Node atom;
0N/A int ch = peek();
0N/A switch (ch) {
0N/A case '?':
0N/A ch = next();
0N/A if (ch == '?') {
0N/A next();
0N/A return new Ques(prev, LAZY);
0N/A } else if (ch == '+') {
0N/A next();
0N/A return new Ques(prev, POSSESSIVE);
0N/A }
0N/A return new Ques(prev, GREEDY);
0N/A case '*':
0N/A ch = next();
0N/A if (ch == '?') {
0N/A next();
0N/A return new Curly(prev, 0, MAX_REPS, LAZY);
0N/A } else if (ch == '+') {
0N/A next();
0N/A return new Curly(prev, 0, MAX_REPS, POSSESSIVE);
0N/A }
0N/A return new Curly(prev, 0, MAX_REPS, GREEDY);
0N/A case '+':
0N/A ch = next();
0N/A if (ch == '?') {
0N/A next();
0N/A return new Curly(prev, 1, MAX_REPS, LAZY);
0N/A } else if (ch == '+') {
0N/A next();
0N/A return new Curly(prev, 1, MAX_REPS, POSSESSIVE);
0N/A }
0N/A return new Curly(prev, 1, MAX_REPS, GREEDY);
0N/A case '{':
0N/A ch = temp[cursor+1];
0N/A if (ASCII.isDigit(ch)) {
0N/A skip();
0N/A int cmin = 0;
0N/A do {
0N/A cmin = cmin * 10 + (ch - '0');
0N/A } while (ASCII.isDigit(ch = read()));
0N/A int cmax = cmin;
0N/A if (ch == ',') {
0N/A ch = read();
0N/A cmax = MAX_REPS;
0N/A if (ch != '}') {
0N/A cmax = 0;
0N/A while (ASCII.isDigit(ch)) {
0N/A cmax = cmax * 10 + (ch - '0');
0N/A ch = read();
0N/A }
0N/A }
0N/A }
0N/A if (ch != '}')
0N/A throw error("Unclosed counted closure");
0N/A if (((cmin) | (cmax) | (cmax - cmin)) < 0)
0N/A throw error("Illegal repetition range");
0N/A Curly curly;
0N/A ch = peek();
0N/A if (ch == '?') {
0N/A next();
0N/A curly = new Curly(prev, cmin, cmax, LAZY);
0N/A } else if (ch == '+') {
0N/A next();
0N/A curly = new Curly(prev, cmin, cmax, POSSESSIVE);
0N/A } else {
0N/A curly = new Curly(prev, cmin, cmax, GREEDY);
0N/A }
0N/A return curly;
0N/A } else {
0N/A throw error("Illegal repetition");
0N/A }
0N/A default:
0N/A return prev;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Utility method for parsing control escape sequences.
0N/A */
0N/A private int c() {
0N/A if (cursor < patternLength) {
0N/A return read() ^ 64;
0N/A }
0N/A throw error("Illegal control escape sequence");
0N/A }
0N/A
0N/A /**
0N/A * Utility method for parsing octal escape sequences.
0N/A */
0N/A private int o() {
0N/A int n = read();
0N/A if (((n-'0')|('7'-n)) >= 0) {
0N/A int m = read();
0N/A if (((m-'0')|('7'-m)) >= 0) {
0N/A int o = read();
0N/A if ((((o-'0')|('7'-o)) >= 0) && (((n-'0')|('3'-n)) >= 0)) {
0N/A return (n - '0') * 64 + (m - '0') * 8 + (o - '0');
0N/A }
0N/A unread();
0N/A return (n - '0') * 8 + (m - '0');
0N/A }
0N/A unread();
0N/A return (n - '0');
0N/A }
0N/A throw error("Illegal octal escape sequence");
0N/A }
0N/A
0N/A /**
0N/A * Utility method for parsing hexadecimal escape sequences.
0N/A */
0N/A private int x() {
0N/A int n = read();
0N/A if (ASCII.isHexDigit(n)) {
0N/A int m = read();
0N/A if (ASCII.isHexDigit(m)) {
0N/A return ASCII.toDigit(n) * 16 + ASCII.toDigit(m);
0N/A }
3486N/A } else if (n == '{' && ASCII.isHexDigit(peek())) {
3486N/A int ch = 0;
3486N/A while (ASCII.isHexDigit(n = read())) {
3486N/A ch = (ch << 4) + ASCII.toDigit(n);
3486N/A if (ch > Character.MAX_CODE_POINT)
3486N/A throw error("Hexadecimal codepoint is too big");
3486N/A }
3486N/A if (n != '}')
3486N/A throw error("Unclosed hexadecimal escape sequence");
3486N/A return ch;
0N/A }
0N/A throw error("Illegal hexadecimal escape sequence");
0N/A }
0N/A
0N/A /**
0N/A * Utility method for parsing unicode escape sequences.
0N/A */
181N/A private int cursor() {
181N/A return cursor;
181N/A }
181N/A
181N/A private void setcursor(int pos) {
181N/A cursor = pos;
181N/A }
181N/A
181N/A private int uxxxx() {
0N/A int n = 0;
0N/A for (int i = 0; i < 4; i++) {
0N/A int ch = read();
0N/A if (!ASCII.isHexDigit(ch)) {
0N/A throw error("Illegal Unicode escape sequence");
0N/A }
0N/A n = n * 16 + ASCII.toDigit(ch);
0N/A }
0N/A return n;
0N/A }
0N/A
181N/A private int u() {
181N/A int n = uxxxx();
181N/A if (Character.isHighSurrogate((char)n)) {
181N/A int cur = cursor();
181N/A if (read() == '\\' && read() == 'u') {
181N/A int n2 = uxxxx();
181N/A if (Character.isLowSurrogate((char)n2))
181N/A return Character.toCodePoint((char)n, (char)n2);
181N/A }
181N/A setcursor(cur);
181N/A }
181N/A return n;
181N/A }
181N/A
0N/A //
0N/A // Utility methods for code point support
0N/A //
0N/A
0N/A private static final int countChars(CharSequence seq, int index,
0N/A int lengthInCodePoints) {
0N/A // optimization
0N/A if (lengthInCodePoints == 1 && !Character.isHighSurrogate(seq.charAt(index))) {
0N/A assert (index >= 0 && index < seq.length());
0N/A return 1;
0N/A }
0N/A int length = seq.length();
0N/A int x = index;
0N/A if (lengthInCodePoints >= 0) {
0N/A assert (index >= 0 && index < length);
0N/A for (int i = 0; x < length && i < lengthInCodePoints; i++) {
0N/A if (Character.isHighSurrogate(seq.charAt(x++))) {
0N/A if (x < length && Character.isLowSurrogate(seq.charAt(x))) {
0N/A x++;
0N/A }
0N/A }
0N/A }
0N/A return x - index;
0N/A }
0N/A
0N/A assert (index >= 0 && index <= length);
0N/A if (index == 0) {
0N/A return 0;
0N/A }
0N/A int len = -lengthInCodePoints;
0N/A for (int i = 0; x > 0 && i < len; i++) {
0N/A if (Character.isLowSurrogate(seq.charAt(--x))) {
0N/A if (x > 0 && Character.isHighSurrogate(seq.charAt(x-1))) {
0N/A x--;
0N/A }
0N/A }
0N/A }
0N/A return index - x;
0N/A }
0N/A
0N/A private static final int countCodePoints(CharSequence seq) {
0N/A int length = seq.length();
0N/A int n = 0;
0N/A for (int i = 0; i < length; ) {
0N/A n++;
0N/A if (Character.isHighSurrogate(seq.charAt(i++))) {
0N/A if (i < length && Character.isLowSurrogate(seq.charAt(i))) {
0N/A i++;
0N/A }
0N/A }
0N/A }
0N/A return n;
0N/A }
0N/A
0N/A /**
0N/A * Creates a bit vector for matching Latin-1 values. A normal BitClass
0N/A * never matches values above Latin-1, and a complemented BitClass always
0N/A * matches values above Latin-1.
0N/A */
0N/A private static final class BitClass extends BmpCharProperty {
0N/A final boolean[] bits;
0N/A BitClass() { bits = new boolean[256]; }
0N/A private BitClass(boolean[] bits) { this.bits = bits; }
0N/A BitClass add(int c, int flags) {
0N/A assert c >= 0 && c <= 255;
0N/A if ((flags & CASE_INSENSITIVE) != 0) {
0N/A if (ASCII.isAscii(c)) {
0N/A bits[ASCII.toUpper(c)] = true;
0N/A bits[ASCII.toLower(c)] = true;
0N/A } else if ((flags & UNICODE_CASE) != 0) {
0N/A bits[Character.toLowerCase(c)] = true;
0N/A bits[Character.toUpperCase(c)] = true;
0N/A }
0N/A }
0N/A bits[c] = true;
0N/A return this;
0N/A }
0N/A boolean isSatisfiedBy(int ch) {
0N/A return ch < 256 && bits[ch];
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a suitably optimized, single character matcher.
0N/A */
0N/A private CharProperty newSingle(final int ch) {
0N/A if (has(CASE_INSENSITIVE)) {
0N/A int lower, upper;
0N/A if (has(UNICODE_CASE)) {
0N/A upper = Character.toUpperCase(ch);
0N/A lower = Character.toLowerCase(upper);
0N/A if (upper != lower)
0N/A return new SingleU(lower);
0N/A } else if (ASCII.isAscii(ch)) {
0N/A lower = ASCII.toLower(ch);
0N/A upper = ASCII.toUpper(ch);
0N/A if (lower != upper)
0N/A return new SingleI(lower, upper);
0N/A }
0N/A }
0N/A if (isSupplementary(ch))
0N/A return new SingleS(ch); // Match a given Unicode character
0N/A return new Single(ch); // Match a given BMP character
0N/A }
0N/A
0N/A /**
0N/A * Utility method for creating a string slice matcher.
0N/A */
0N/A private Node newSlice(int[] buf, int count, boolean hasSupplementary) {
0N/A int[] tmp = new int[count];
0N/A if (has(CASE_INSENSITIVE)) {
0N/A if (has(UNICODE_CASE)) {
0N/A for (int i = 0; i < count; i++) {
0N/A tmp[i] = Character.toLowerCase(
0N/A Character.toUpperCase(buf[i]));
0N/A }
0N/A return hasSupplementary? new SliceUS(tmp) : new SliceU(tmp);
0N/A }
0N/A for (int i = 0; i < count; i++) {
0N/A tmp[i] = ASCII.toLower(buf[i]);
0N/A }
0N/A return hasSupplementary? new SliceIS(tmp) : new SliceI(tmp);
0N/A }
0N/A for (int i = 0; i < count; i++) {
0N/A tmp[i] = buf[i];
0N/A }
0N/A return hasSupplementary ? new SliceS(tmp) : new Slice(tmp);
0N/A }
0N/A
0N/A /**
0N/A * The following classes are the building components of the object
0N/A * tree that represents a compiled regular expression. The object tree
0N/A * is made of individual elements that handle constructs in the Pattern.
0N/A * Each type of object knows how to match its equivalent construct with
0N/A * the match() method.
0N/A */
0N/A
0N/A /**
0N/A * Base class for all node classes. Subclasses should override the match()
0N/A * method as appropriate. This class is an accepting node, so its match()
0N/A * always returns true.
0N/A */
0N/A static class Node extends Object {
0N/A Node next;
0N/A Node() {
0N/A next = Pattern.accept;
0N/A }
0N/A /**
0N/A * This method implements the classic accept node.
0N/A */
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A matcher.last = i;
0N/A matcher.groups[0] = matcher.first;
0N/A matcher.groups[1] = matcher.last;
0N/A return true;
0N/A }
0N/A /**
0N/A * This method is good for all zero length assertions.
0N/A */
0N/A boolean study(TreeInfo info) {
0N/A if (next != null) {
0N/A return next.study(info);
0N/A } else {
0N/A return info.deterministic;
0N/A }
0N/A }
0N/A }
0N/A
0N/A static class LastNode extends Node {
0N/A /**
0N/A * This method implements the classic accept node with
0N/A * the addition of a check to see if the match occurred
0N/A * using all of the input.
0N/A */
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A if (matcher.acceptMode == Matcher.ENDANCHOR && i != matcher.to)
0N/A return false;
0N/A matcher.last = i;
0N/A matcher.groups[0] = matcher.first;
0N/A matcher.groups[1] = matcher.last;
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Used for REs that can start anywhere within the input string.
0N/A * This basically tries to match repeatedly at each spot in the
0N/A * input string, moving forward after each try. An anchored search
0N/A * or a BnM will bypass this node completely.
0N/A */
0N/A static class Start extends Node {
0N/A int minLength;
0N/A Start(Node node) {
0N/A this.next = node;
0N/A TreeInfo info = new TreeInfo();
0N/A next.study(info);
0N/A minLength = info.minLength;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A if (i > matcher.to - minLength) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A int guard = matcher.to - minLength;
0N/A for (; i <= guard; i++) {
2118N/A if (next.match(matcher, i, seq)) {
2118N/A matcher.first = i;
2118N/A matcher.groups[0] = matcher.first;
2118N/A matcher.groups[1] = matcher.last;
2118N/A return true;
2118N/A }
0N/A }
2118N/A matcher.hitEnd = true;
2118N/A return false;
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A next.study(info);
0N/A info.maxValid = false;
0N/A info.deterministic = false;
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * StartS supports supplementary characters, including unpaired surrogates.
0N/A */
0N/A static final class StartS extends Start {
0N/A StartS(Node node) {
0N/A super(node);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A if (i > matcher.to - minLength) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A int guard = matcher.to - minLength;
0N/A while (i <= guard) {
2118N/A //if ((ret = next.match(matcher, i, seq)) || i == guard)
2118N/A if (next.match(matcher, i, seq)) {
2118N/A matcher.first = i;
2118N/A matcher.groups[0] = matcher.first;
2118N/A matcher.groups[1] = matcher.last;
2118N/A return true;
2118N/A }
2118N/A if (i == guard)
0N/A break;
0N/A // Optimization to move to the next character. This is
0N/A // faster than countChars(seq, i, 1).
0N/A if (Character.isHighSurrogate(seq.charAt(i++))) {
2118N/A if (i < seq.length() &&
2118N/A Character.isLowSurrogate(seq.charAt(i))) {
0N/A i++;
0N/A }
0N/A }
0N/A }
2118N/A matcher.hitEnd = true;
2118N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node to anchor at the beginning of input. This object implements the
0N/A * match for a \A sequence, and the caret anchor will use this if not in
0N/A * multiline mode.
0N/A */
0N/A static final class Begin extends Node {
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int fromIndex = (matcher.anchoringBounds) ?
0N/A matcher.from : 0;
0N/A if (i == fromIndex && next.match(matcher, i, seq)) {
0N/A matcher.first = i;
0N/A matcher.groups[0] = i;
0N/A matcher.groups[1] = matcher.last;
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node to anchor at the end of input. This is the absolute end, so this
0N/A * should not match at the last newline before the end as $ will.
0N/A */
0N/A static final class End extends Node {
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int endIndex = (matcher.anchoringBounds) ?
0N/A matcher.to : matcher.getTextLength();
0N/A if (i == endIndex) {
0N/A matcher.hitEnd = true;
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node to anchor at the beginning of a line. This is essentially the
0N/A * object to match for the multiline ^.
0N/A */
0N/A static final class Caret extends Node {
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int startIndex = matcher.from;
0N/A int endIndex = matcher.to;
0N/A if (!matcher.anchoringBounds) {
0N/A startIndex = 0;
0N/A endIndex = matcher.getTextLength();
0N/A }
0N/A // Perl does not match ^ at end of input even after newline
0N/A if (i == endIndex) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A if (i > startIndex) {
0N/A char ch = seq.charAt(i-1);
0N/A if (ch != '\n' && ch != '\r'
0N/A && (ch|1) != '\u2029'
0N/A && ch != '\u0085' ) {
0N/A return false;
0N/A }
0N/A // Should treat /r/n as one newline
0N/A if (ch == '\r' && seq.charAt(i) == '\n')
0N/A return false;
0N/A }
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node to anchor at the beginning of a line when in unixdot mode.
0N/A */
0N/A static final class UnixCaret extends Node {
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int startIndex = matcher.from;
0N/A int endIndex = matcher.to;
0N/A if (!matcher.anchoringBounds) {
0N/A startIndex = 0;
0N/A endIndex = matcher.getTextLength();
0N/A }
0N/A // Perl does not match ^ at end of input even after newline
0N/A if (i == endIndex) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A if (i > startIndex) {
0N/A char ch = seq.charAt(i-1);
0N/A if (ch != '\n') {
0N/A return false;
0N/A }
0N/A }
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node to match the location where the last match ended.
0N/A * This is used for the \G construct.
0N/A */
0N/A static final class LastMatch extends Node {
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A if (i != matcher.oldLast)
0N/A return false;
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node to anchor at the end of a line or the end of input based on the
0N/A * multiline mode.
0N/A *
0N/A * When not in multiline mode, the $ can only match at the very end
0N/A * of the input, unless the input ends in a line terminator in which
0N/A * it matches right before the last line terminator.
0N/A *
0N/A * Note that \r\n is considered an atomic line terminator.
0N/A *
0N/A * Like ^ the $ operator matches at a position, it does not match the
0N/A * line terminators themselves.
0N/A */
0N/A static final class Dollar extends Node {
0N/A boolean multiline;
0N/A Dollar(boolean mul) {
0N/A multiline = mul;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int endIndex = (matcher.anchoringBounds) ?
0N/A matcher.to : matcher.getTextLength();
0N/A if (!multiline) {
0N/A if (i < endIndex - 2)
0N/A return false;
0N/A if (i == endIndex - 2) {
0N/A char ch = seq.charAt(i);
0N/A if (ch != '\r')
0N/A return false;
0N/A ch = seq.charAt(i + 1);
0N/A if (ch != '\n')
0N/A return false;
0N/A }
0N/A }
0N/A // Matches before any line terminator; also matches at the
0N/A // end of input
0N/A // Before line terminator:
0N/A // If multiline, we match here no matter what
0N/A // If not multiline, fall through so that the end
0N/A // is marked as hit; this must be a /r/n or a /n
0N/A // at the very end so the end was hit; more input
0N/A // could make this not match here
0N/A if (i < endIndex) {
0N/A char ch = seq.charAt(i);
0N/A if (ch == '\n') {
0N/A // No match between \r\n
0N/A if (i > 0 && seq.charAt(i-1) == '\r')
0N/A return false;
0N/A if (multiline)
0N/A return next.match(matcher, i, seq);
0N/A } else if (ch == '\r' || ch == '\u0085' ||
0N/A (ch|1) == '\u2029') {
0N/A if (multiline)
0N/A return next.match(matcher, i, seq);
0N/A } else { // No line terminator, no match
0N/A return false;
0N/A }
0N/A }
0N/A // Matched at current end so hit end
0N/A matcher.hitEnd = true;
0N/A // If a $ matches because of end of input, then more input
0N/A // could cause it to fail!
0N/A matcher.requireEnd = true;
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A next.study(info);
0N/A return info.deterministic;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node to anchor at the end of a line or the end of input based on the
0N/A * multiline mode when in unix lines mode.
0N/A */
0N/A static final class UnixDollar extends Node {
0N/A boolean multiline;
0N/A UnixDollar(boolean mul) {
0N/A multiline = mul;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int endIndex = (matcher.anchoringBounds) ?
0N/A matcher.to : matcher.getTextLength();
0N/A if (i < endIndex) {
0N/A char ch = seq.charAt(i);
0N/A if (ch == '\n') {
0N/A // If not multiline, then only possible to
0N/A // match at very end or one before end
0N/A if (multiline == false && i != endIndex - 1)
0N/A return false;
0N/A // If multiline return next.match without setting
0N/A // matcher.hitEnd
0N/A if (multiline)
0N/A return next.match(matcher, i, seq);
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A // Matching because at the end or 1 before the end;
0N/A // more input could change this so set hitEnd
0N/A matcher.hitEnd = true;
0N/A // If a $ matches because of end of input, then more input
0N/A // could cause it to fail!
0N/A matcher.requireEnd = true;
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A next.study(info);
0N/A return info.deterministic;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Abstract node class to match one character satisfying some
0N/A * boolean property.
0N/A */
0N/A private static abstract class CharProperty extends Node {
0N/A abstract boolean isSatisfiedBy(int ch);
0N/A CharProperty complement() {
0N/A return new CharProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return ! CharProperty.this.isSatisfiedBy(ch);}};
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A if (i < matcher.to) {
0N/A int ch = Character.codePointAt(seq, i);
0N/A return isSatisfiedBy(ch)
0N/A && next.match(matcher, i+Character.charCount(ch), seq);
0N/A } else {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A info.minLength++;
0N/A info.maxLength++;
0N/A return next.study(info);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Optimized version of CharProperty that works only for
0N/A * properties never satisfied by Supplementary characters.
0N/A */
0N/A private static abstract class BmpCharProperty extends CharProperty {
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A if (i < matcher.to) {
0N/A return isSatisfiedBy(seq.charAt(i))
0N/A && next.match(matcher, i+1, seq);
0N/A } else {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node class that matches a Supplementary Unicode character
0N/A */
0N/A static final class SingleS extends CharProperty {
0N/A final int c;
0N/A SingleS(int c) { this.c = c; }
0N/A boolean isSatisfiedBy(int ch) {
0N/A return ch == c;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Optimization -- matches a given BMP character
0N/A */
0N/A static final class Single extends BmpCharProperty {
0N/A final int c;
0N/A Single(int c) { this.c = c; }
0N/A boolean isSatisfiedBy(int ch) {
0N/A return ch == c;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Case insensitive matches a given BMP character
0N/A */
0N/A static final class SingleI extends BmpCharProperty {
0N/A final int lower;
0N/A final int upper;
0N/A SingleI(int lower, int upper) {
0N/A this.lower = lower;
0N/A this.upper = upper;
0N/A }
0N/A boolean isSatisfiedBy(int ch) {
0N/A return ch == lower || ch == upper;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Unicode case insensitive matches a given Unicode character
0N/A */
0N/A static final class SingleU extends CharProperty {
0N/A final int lower;
0N/A SingleU(int lower) {
0N/A this.lower = lower;
0N/A }
0N/A boolean isSatisfiedBy(int ch) {
0N/A return lower == ch ||
0N/A lower == Character.toLowerCase(Character.toUpperCase(ch));
0N/A }
0N/A }
0N/A
2118N/A
2118N/A /**
2118N/A * Node class that matches a Unicode block.
2118N/A */
2118N/A static final class Block extends CharProperty {
2118N/A final Character.UnicodeBlock block;
2118N/A Block(Character.UnicodeBlock block) {
2118N/A this.block = block;
2118N/A }
2118N/A boolean isSatisfiedBy(int ch) {
2118N/A return block == Character.UnicodeBlock.of(ch);
2118N/A }
2118N/A }
2118N/A
0N/A /**
2401N/A * Node class that matches a Unicode script
2401N/A */
2401N/A static final class Script extends CharProperty {
2401N/A final Character.UnicodeScript script;
2401N/A Script(Character.UnicodeScript script) {
2401N/A this.script = script;
2401N/A }
2401N/A boolean isSatisfiedBy(int ch) {
2401N/A return script == Character.UnicodeScript.of(ch);
2401N/A }
2401N/A }
2401N/A
2401N/A /**
0N/A * Node class that matches a Unicode category.
0N/A */
0N/A static final class Category extends CharProperty {
0N/A final int typeMask;
0N/A Category(int typeMask) { this.typeMask = typeMask; }
0N/A boolean isSatisfiedBy(int ch) {
0N/A return (typeMask & (1 << Character.getType(ch))) != 0;
0N/A }
0N/A }
0N/A
0N/A /**
4139N/A * Node class that matches a Unicode "type"
4139N/A */
4139N/A static final class Utype extends CharProperty {
4139N/A final UnicodeProp uprop;
4139N/A Utype(UnicodeProp uprop) { this.uprop = uprop; }
4139N/A boolean isSatisfiedBy(int ch) {
4139N/A return uprop.is(ch);
4139N/A }
4139N/A }
4139N/A
4139N/A
4139N/A /**
0N/A * Node class that matches a POSIX type.
0N/A */
0N/A static final class Ctype extends BmpCharProperty {
0N/A final int ctype;
0N/A Ctype(int ctype) { this.ctype = ctype; }
0N/A boolean isSatisfiedBy(int ch) {
0N/A return ch < 128 && ASCII.isType(ch, ctype);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Base class for all Slice nodes
0N/A */
0N/A static class SliceNode extends Node {
0N/A int[] buffer;
0N/A SliceNode(int[] buf) {
0N/A buffer = buf;
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A info.minLength += buffer.length;
0N/A info.maxLength += buffer.length;
0N/A return next.study(info);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node class for a case sensitive/BMP-only sequence of literal
0N/A * characters.
0N/A */
0N/A static final class Slice extends SliceNode {
0N/A Slice(int[] buf) {
0N/A super(buf);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int[] buf = buffer;
0N/A int len = buf.length;
0N/A for (int j=0; j<len; j++) {
0N/A if ((i+j) >= matcher.to) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A if (buf[j] != seq.charAt(i+j))
0N/A return false;
0N/A }
0N/A return next.match(matcher, i+len, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node class for a case_insensitive/BMP-only sequence of literal
0N/A * characters.
0N/A */
0N/A static class SliceI extends SliceNode {
0N/A SliceI(int[] buf) {
0N/A super(buf);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int[] buf = buffer;
0N/A int len = buf.length;
0N/A for (int j=0; j<len; j++) {
0N/A if ((i+j) >= matcher.to) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A int c = seq.charAt(i+j);
0N/A if (buf[j] != c &&
0N/A buf[j] != ASCII.toLower(c))
0N/A return false;
0N/A }
0N/A return next.match(matcher, i+len, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node class for a unicode_case_insensitive/BMP-only sequence of
0N/A * literal characters. Uses unicode case folding.
0N/A */
0N/A static final class SliceU extends SliceNode {
0N/A SliceU(int[] buf) {
0N/A super(buf);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int[] buf = buffer;
0N/A int len = buf.length;
0N/A for (int j=0; j<len; j++) {
0N/A if ((i+j) >= matcher.to) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A int c = seq.charAt(i+j);
0N/A if (buf[j] != c &&
0N/A buf[j] != Character.toLowerCase(Character.toUpperCase(c)))
0N/A return false;
0N/A }
0N/A return next.match(matcher, i+len, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node class for a case sensitive sequence of literal characters
0N/A * including supplementary characters.
0N/A */
0N/A static final class SliceS extends SliceNode {
0N/A SliceS(int[] buf) {
0N/A super(buf);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int[] buf = buffer;
0N/A int x = i;
0N/A for (int j = 0; j < buf.length; j++) {
0N/A if (x >= matcher.to) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A int c = Character.codePointAt(seq, x);
0N/A if (buf[j] != c)
0N/A return false;
0N/A x += Character.charCount(c);
0N/A if (x > matcher.to) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A }
0N/A return next.match(matcher, x, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node class for a case insensitive sequence of literal characters
0N/A * including supplementary characters.
0N/A */
0N/A static class SliceIS extends SliceNode {
0N/A SliceIS(int[] buf) {
0N/A super(buf);
0N/A }
0N/A int toLower(int c) {
0N/A return ASCII.toLower(c);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int[] buf = buffer;
0N/A int x = i;
0N/A for (int j = 0; j < buf.length; j++) {
0N/A if (x >= matcher.to) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A int c = Character.codePointAt(seq, x);
0N/A if (buf[j] != c && buf[j] != toLower(c))
0N/A return false;
0N/A x += Character.charCount(c);
0N/A if (x > matcher.to) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A }
0N/A return next.match(matcher, x, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node class for a case insensitive sequence of literal characters.
0N/A * Uses unicode case folding.
0N/A */
0N/A static final class SliceUS extends SliceIS {
0N/A SliceUS(int[] buf) {
0N/A super(buf);
0N/A }
0N/A int toLower(int c) {
0N/A return Character.toLowerCase(Character.toUpperCase(c));
0N/A }
0N/A }
0N/A
0N/A private static boolean inRange(int lower, int ch, int upper) {
0N/A return lower <= ch && ch <= upper;
0N/A }
0N/A
0N/A /**
0N/A * Returns node for matching characters within an explicit value range.
0N/A */
0N/A private static CharProperty rangeFor(final int lower,
0N/A final int upper) {
0N/A return new CharProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return inRange(lower, ch, upper);}};
0N/A }
0N/A
0N/A /**
0N/A * Returns node for matching characters within an explicit value
0N/A * range in a case insensitive manner.
0N/A */
0N/A private CharProperty caseInsensitiveRangeFor(final int lower,
0N/A final int upper) {
0N/A if (has(UNICODE_CASE))
0N/A return new CharProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A if (inRange(lower, ch, upper))
0N/A return true;
0N/A int up = Character.toUpperCase(ch);
0N/A return inRange(lower, up, upper) ||
0N/A inRange(lower, Character.toLowerCase(up), upper);}};
0N/A return new CharProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return inRange(lower, ch, upper) ||
0N/A ASCII.isAscii(ch) &&
0N/A (inRange(lower, ASCII.toUpper(ch), upper) ||
0N/A inRange(lower, ASCII.toLower(ch), upper));
0N/A }};
0N/A }
0N/A
0N/A /**
0N/A * Implements the Unicode category ALL and the dot metacharacter when
0N/A * in dotall mode.
0N/A */
0N/A static final class All extends CharProperty {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node class for the dot metacharacter when dotall is not enabled.
0N/A */
0N/A static final class Dot extends CharProperty {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return (ch != '\n' && ch != '\r'
0N/A && (ch|1) != '\u2029'
0N/A && ch != '\u0085');
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Node class for the dot metacharacter when dotall is not enabled
0N/A * but UNIX_LINES is enabled.
0N/A */
0N/A static final class UnixDot extends CharProperty {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return ch != '\n';
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The 0 or 1 quantifier. This one class implements all three types.
0N/A */
0N/A static final class Ques extends Node {
0N/A Node atom;
0N/A int type;
0N/A Ques(Node node, int type) {
0N/A this.atom = node;
0N/A this.type = type;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A switch (type) {
0N/A case GREEDY:
0N/A return (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq))
0N/A || next.match(matcher, i, seq);
0N/A case LAZY:
0N/A return next.match(matcher, i, seq)
0N/A || (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq));
0N/A case POSSESSIVE:
0N/A if (atom.match(matcher, i, seq)) i = matcher.last;
0N/A return next.match(matcher, i, seq);
0N/A default:
0N/A return atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq);
0N/A }
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A if (type != INDEPENDENT) {
0N/A int minL = info.minLength;
0N/A atom.study(info);
0N/A info.minLength = minL;
0N/A info.deterministic = false;
0N/A return next.study(info);
0N/A } else {
0N/A atom.study(info);
0N/A return next.study(info);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handles the curly-brace style repetition with a specified minimum and
0N/A * maximum occurrences. The * quantifier is handled as a special case.
0N/A * This class handles the three types.
0N/A */
0N/A static final class Curly extends Node {
0N/A Node atom;
0N/A int type;
0N/A int cmin;
0N/A int cmax;
0N/A
0N/A Curly(Node node, int cmin, int cmax, int type) {
0N/A this.atom = node;
0N/A this.type = type;
0N/A this.cmin = cmin;
0N/A this.cmax = cmax;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int j;
0N/A for (j = 0; j < cmin; j++) {
0N/A if (atom.match(matcher, i, seq)) {
0N/A i = matcher.last;
0N/A continue;
0N/A }
0N/A return false;
0N/A }
0N/A if (type == GREEDY)
0N/A return match0(matcher, i, j, seq);
0N/A else if (type == LAZY)
0N/A return match1(matcher, i, j, seq);
0N/A else
0N/A return match2(matcher, i, j, seq);
0N/A }
0N/A // Greedy match.
0N/A // i is the index to start matching at
0N/A // j is the number of atoms that have matched
0N/A boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
0N/A if (j >= cmax) {
0N/A // We have matched the maximum... continue with the rest of
0N/A // the regular expression
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A int backLimit = j;
0N/A while (atom.match(matcher, i, seq)) {
0N/A // k is the length of this match
0N/A int k = matcher.last - i;
0N/A if (k == 0) // Zero length match
0N/A break;
0N/A // Move up index and number matched
0N/A i = matcher.last;
0N/A j++;
0N/A // We are greedy so match as many as we can
0N/A while (j < cmax) {
0N/A if (!atom.match(matcher, i, seq))
0N/A break;
0N/A if (i + k != matcher.last) {
0N/A if (match0(matcher, matcher.last, j+1, seq))
0N/A return true;
0N/A break;
0N/A }
0N/A i += k;
0N/A j++;
0N/A }
0N/A // Handle backing off if match fails
0N/A while (j >= backLimit) {
0N/A if (next.match(matcher, i, seq))
0N/A return true;
0N/A i -= k;
0N/A j--;
0N/A }
0N/A return false;
0N/A }
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A // Reluctant match. At this point, the minimum has been satisfied.
0N/A // i is the index to start matching at
0N/A // j is the number of atoms that have matched
0N/A boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
0N/A for (;;) {
0N/A // Try finishing match without consuming any more
0N/A if (next.match(matcher, i, seq))
0N/A return true;
0N/A // At the maximum, no match found
0N/A if (j >= cmax)
0N/A return false;
0N/A // Okay, must try one more atom
0N/A if (!atom.match(matcher, i, seq))
0N/A return false;
0N/A // If we haven't moved forward then must break out
0N/A if (i == matcher.last)
0N/A return false;
0N/A // Move up index and number matched
0N/A i = matcher.last;
0N/A j++;
0N/A }
0N/A }
0N/A boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
0N/A for (; j < cmax; j++) {
0N/A if (!atom.match(matcher, i, seq))
0N/A break;
0N/A if (i == matcher.last)
0N/A break;
0N/A i = matcher.last;
0N/A }
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A // Save original info
0N/A int minL = info.minLength;
0N/A int maxL = info.maxLength;
0N/A boolean maxV = info.maxValid;
0N/A boolean detm = info.deterministic;
0N/A info.reset();
0N/A
0N/A atom.study(info);
0N/A
0N/A int temp = info.minLength * cmin + minL;
0N/A if (temp < minL) {
0N/A temp = 0xFFFFFFF; // arbitrary large number
0N/A }
0N/A info.minLength = temp;
0N/A
0N/A if (maxV & info.maxValid) {
0N/A temp = info.maxLength * cmax + maxL;
0N/A info.maxLength = temp;
0N/A if (temp < maxL) {
0N/A info.maxValid = false;
0N/A }
0N/A } else {
0N/A info.maxValid = false;
0N/A }
0N/A
0N/A if (info.deterministic && cmin == cmax)
0N/A info.deterministic = detm;
0N/A else
0N/A info.deterministic = false;
0N/A
0N/A return next.study(info);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handles the curly-brace style repetition with a specified minimum and
0N/A * maximum occurrences in deterministic cases. This is an iterative
0N/A * optimization over the Prolog and Loop system which would handle this
0N/A * in a recursive way. The * quantifier is handled as a special case.
0N/A * If capture is true then this class saves group settings and ensures
0N/A * that groups are unset when backing off of a group match.
0N/A */
0N/A static final class GroupCurly extends Node {
0N/A Node atom;
0N/A int type;
0N/A int cmin;
0N/A int cmax;
0N/A int localIndex;
0N/A int groupIndex;
0N/A boolean capture;
0N/A
0N/A GroupCurly(Node node, int cmin, int cmax, int type, int local,
0N/A int group, boolean capture) {
0N/A this.atom = node;
0N/A this.type = type;
0N/A this.cmin = cmin;
0N/A this.cmax = cmax;
0N/A this.localIndex = local;
0N/A this.groupIndex = group;
0N/A this.capture = capture;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int[] groups = matcher.groups;
0N/A int[] locals = matcher.locals;
0N/A int save0 = locals[localIndex];
0N/A int save1 = 0;
0N/A int save2 = 0;
0N/A
0N/A if (capture) {
0N/A save1 = groups[groupIndex];
0N/A save2 = groups[groupIndex+1];
0N/A }
0N/A
0N/A // Notify GroupTail there is no need to setup group info
0N/A // because it will be set here
0N/A locals[localIndex] = -1;
0N/A
0N/A boolean ret = true;
0N/A for (int j = 0; j < cmin; j++) {
0N/A if (atom.match(matcher, i, seq)) {
0N/A if (capture) {
0N/A groups[groupIndex] = i;
0N/A groups[groupIndex+1] = matcher.last;
0N/A }
0N/A i = matcher.last;
0N/A } else {
0N/A ret = false;
0N/A break;
0N/A }
0N/A }
0N/A if (ret) {
0N/A if (type == GREEDY) {
0N/A ret = match0(matcher, i, cmin, seq);
0N/A } else if (type == LAZY) {
0N/A ret = match1(matcher, i, cmin, seq);
0N/A } else {
0N/A ret = match2(matcher, i, cmin, seq);
0N/A }
0N/A }
0N/A if (!ret) {
0N/A locals[localIndex] = save0;
0N/A if (capture) {
0N/A groups[groupIndex] = save1;
0N/A groups[groupIndex+1] = save2;
0N/A }
0N/A }
0N/A return ret;
0N/A }
0N/A // Aggressive group match
0N/A boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
0N/A int[] groups = matcher.groups;
0N/A int save0 = 0;
0N/A int save1 = 0;
0N/A if (capture) {
0N/A save0 = groups[groupIndex];
0N/A save1 = groups[groupIndex+1];
0N/A }
0N/A for (;;) {
0N/A if (j >= cmax)
0N/A break;
0N/A if (!atom.match(matcher, i, seq))
0N/A break;
0N/A int k = matcher.last - i;
0N/A if (k <= 0) {
0N/A if (capture) {
0N/A groups[groupIndex] = i;
0N/A groups[groupIndex+1] = i + k;
0N/A }
0N/A i = i + k;
0N/A break;
0N/A }
0N/A for (;;) {
0N/A if (capture) {
0N/A groups[groupIndex] = i;
0N/A groups[groupIndex+1] = i + k;
0N/A }
0N/A i = i + k;
0N/A if (++j >= cmax)
0N/A break;
0N/A if (!atom.match(matcher, i, seq))
0N/A break;
0N/A if (i + k != matcher.last) {
0N/A if (match0(matcher, i, j, seq))
0N/A return true;
0N/A break;
0N/A }
0N/A }
0N/A while (j > cmin) {
0N/A if (next.match(matcher, i, seq)) {
0N/A if (capture) {
0N/A groups[groupIndex+1] = i;
0N/A groups[groupIndex] = i - k;
0N/A }
0N/A i = i - k;
0N/A return true;
0N/A }
0N/A // backing off
0N/A if (capture) {
0N/A groups[groupIndex+1] = i;
0N/A groups[groupIndex] = i - k;
0N/A }
0N/A i = i - k;
0N/A j--;
0N/A }
0N/A break;
0N/A }
0N/A if (capture) {
0N/A groups[groupIndex] = save0;
0N/A groups[groupIndex+1] = save1;
0N/A }
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A // Reluctant matching
0N/A boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
0N/A for (;;) {
0N/A if (next.match(matcher, i, seq))
0N/A return true;
0N/A if (j >= cmax)
0N/A return false;
0N/A if (!atom.match(matcher, i, seq))
0N/A return false;
0N/A if (i == matcher.last)
0N/A return false;
0N/A if (capture) {
0N/A matcher.groups[groupIndex] = i;
0N/A matcher.groups[groupIndex+1] = matcher.last;
0N/A }
0N/A i = matcher.last;
0N/A j++;
0N/A }
0N/A }
0N/A // Possessive matching
0N/A boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
0N/A for (; j < cmax; j++) {
0N/A if (!atom.match(matcher, i, seq)) {
0N/A break;
0N/A }
0N/A if (capture) {
0N/A matcher.groups[groupIndex] = i;
0N/A matcher.groups[groupIndex+1] = matcher.last;
0N/A }
0N/A if (i == matcher.last) {
0N/A break;
0N/A }
0N/A i = matcher.last;
0N/A }
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A // Save original info
0N/A int minL = info.minLength;
0N/A int maxL = info.maxLength;
0N/A boolean maxV = info.maxValid;
0N/A boolean detm = info.deterministic;
0N/A info.reset();
0N/A
0N/A atom.study(info);
0N/A
0N/A int temp = info.minLength * cmin + minL;
0N/A if (temp < minL) {
0N/A temp = 0xFFFFFFF; // Arbitrary large number
0N/A }
0N/A info.minLength = temp;
0N/A
0N/A if (maxV & info.maxValid) {
0N/A temp = info.maxLength * cmax + maxL;
0N/A info.maxLength = temp;
0N/A if (temp < maxL) {
0N/A info.maxValid = false;
0N/A }
0N/A } else {
0N/A info.maxValid = false;
0N/A }
0N/A
0N/A if (info.deterministic && cmin == cmax) {
0N/A info.deterministic = detm;
0N/A } else {
0N/A info.deterministic = false;
0N/A }
0N/A
0N/A return next.study(info);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * A Guard node at the end of each atom node in a Branch. It
0N/A * serves the purpose of chaining the "match" operation to
0N/A * "next" but not the "study", so we can collect the TreeInfo
0N/A * of each atom node without including the TreeInfo of the
0N/A * "next".
0N/A */
0N/A static final class BranchConn extends Node {
0N/A BranchConn() {};
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A return info.deterministic;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handles the branching of alternations. Note this is also used for
0N/A * the ? quantifier to branch between the case where it matches once
0N/A * and where it does not occur.
0N/A */
0N/A static final class Branch extends Node {
0N/A Node[] atoms = new Node[2];
0N/A int size = 2;
0N/A Node conn;
0N/A Branch(Node first, Node second, Node branchConn) {
0N/A conn = branchConn;
0N/A atoms[0] = first;
0N/A atoms[1] = second;
0N/A }
0N/A
0N/A void add(Node node) {
0N/A if (size >= atoms.length) {
0N/A Node[] tmp = new Node[atoms.length*2];
0N/A System.arraycopy(atoms, 0, tmp, 0, atoms.length);
0N/A atoms = tmp;
0N/A }
0N/A atoms[size++] = node;
0N/A }
0N/A
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A for (int n = 0; n < size; n++) {
0N/A if (atoms[n] == null) {
0N/A if (conn.next.match(matcher, i, seq))
0N/A return true;
0N/A } else if (atoms[n].match(matcher, i, seq)) {
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A boolean study(TreeInfo info) {
0N/A int minL = info.minLength;
0N/A int maxL = info.maxLength;
0N/A boolean maxV = info.maxValid;
0N/A
0N/A int minL2 = Integer.MAX_VALUE; //arbitrary large enough num
0N/A int maxL2 = -1;
0N/A for (int n = 0; n < size; n++) {
0N/A info.reset();
0N/A if (atoms[n] != null)
0N/A atoms[n].study(info);
0N/A minL2 = Math.min(minL2, info.minLength);
0N/A maxL2 = Math.max(maxL2, info.maxLength);
0N/A maxV = (maxV & info.maxValid);
0N/A }
0N/A
0N/A minL += minL2;
0N/A maxL += maxL2;
0N/A
0N/A info.reset();
0N/A conn.next.study(info);
0N/A
0N/A info.minLength += minL;
0N/A info.maxLength += maxL;
0N/A info.maxValid &= maxV;
0N/A info.deterministic = false;
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The GroupHead saves the location where the group begins in the locals
0N/A * and restores them when the match is done.
0N/A *
0N/A * The matchRef is used when a reference to this group is accessed later
0N/A * in the expression. The locals will have a negative value in them to
0N/A * indicate that we do not want to unset the group if the reference
0N/A * doesn't match.
0N/A */
0N/A static final class GroupHead extends Node {
0N/A int localIndex;
0N/A GroupHead(int localCount) {
0N/A localIndex = localCount;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int save = matcher.locals[localIndex];
0N/A matcher.locals[localIndex] = i;
0N/A boolean ret = next.match(matcher, i, seq);
0N/A matcher.locals[localIndex] = save;
0N/A return ret;
0N/A }
0N/A boolean matchRef(Matcher matcher, int i, CharSequence seq) {
0N/A int save = matcher.locals[localIndex];
0N/A matcher.locals[localIndex] = ~i; // HACK
0N/A boolean ret = next.match(matcher, i, seq);
0N/A matcher.locals[localIndex] = save;
0N/A return ret;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Recursive reference to a group in the regular expression. It calls
0N/A * matchRef because if the reference fails to match we would not unset
0N/A * the group.
0N/A */
0N/A static final class GroupRef extends Node {
0N/A GroupHead head;
0N/A GroupRef(GroupHead head) {
0N/A this.head = head;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A return head.matchRef(matcher, i, seq)
0N/A && next.match(matcher, matcher.last, seq);
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A info.maxValid = false;
0N/A info.deterministic = false;
0N/A return next.study(info);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The GroupTail handles the setting of group beginning and ending
0N/A * locations when groups are successfully matched. It must also be able to
0N/A * unset groups that have to be backed off of.
0N/A *
0N/A * The GroupTail node is also used when a previous group is referenced,
0N/A * and in that case no group information needs to be set.
0N/A */
0N/A static final class GroupTail extends Node {
0N/A int localIndex;
0N/A int groupIndex;
0N/A GroupTail(int localCount, int groupCount) {
0N/A localIndex = localCount;
0N/A groupIndex = groupCount + groupCount;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int tmp = matcher.locals[localIndex];
0N/A if (tmp >= 0) { // This is the normal group case.
0N/A // Save the group so we can unset it if it
0N/A // backs off of a match.
0N/A int groupStart = matcher.groups[groupIndex];
0N/A int groupEnd = matcher.groups[groupIndex+1];
0N/A
0N/A matcher.groups[groupIndex] = tmp;
0N/A matcher.groups[groupIndex+1] = i;
0N/A if (next.match(matcher, i, seq)) {
0N/A return true;
0N/A }
0N/A matcher.groups[groupIndex] = groupStart;
0N/A matcher.groups[groupIndex+1] = groupEnd;
0N/A return false;
0N/A } else {
0N/A // This is a group reference case. We don't need to save any
0N/A // group info because it isn't really a group.
0N/A matcher.last = i;
0N/A return true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This sets up a loop to handle a recursive quantifier structure.
0N/A */
0N/A static final class Prolog extends Node {
0N/A Loop loop;
0N/A Prolog(Loop loop) {
0N/A this.loop = loop;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A return loop.matchInit(matcher, i, seq);
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A return loop.study(info);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handles the repetition count for a greedy Curly. The matchInit
0N/A * is called from the Prolog to save the index of where the group
0N/A * beginning is stored. A zero length group check occurs in the
0N/A * normal match but is skipped in the matchInit.
0N/A */
0N/A static class Loop extends Node {
0N/A Node body;
0N/A int countIndex; // local count index in matcher locals
0N/A int beginIndex; // group beginning index
0N/A int cmin, cmax;
0N/A Loop(int countIndex, int beginIndex) {
0N/A this.countIndex = countIndex;
0N/A this.beginIndex = beginIndex;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A // Avoid infinite loop in zero-length case.
0N/A if (i > matcher.locals[beginIndex]) {
0N/A int count = matcher.locals[countIndex];
0N/A
0N/A // This block is for before we reach the minimum
0N/A // iterations required for the loop to match
0N/A if (count < cmin) {
0N/A matcher.locals[countIndex] = count + 1;
0N/A boolean b = body.match(matcher, i, seq);
0N/A // If match failed we must backtrack, so
0N/A // the loop count should NOT be incremented
0N/A if (!b)
0N/A matcher.locals[countIndex] = count;
0N/A // Return success or failure since we are under
0N/A // minimum
0N/A return b;
0N/A }
0N/A // This block is for after we have the minimum
0N/A // iterations required for the loop to match
0N/A if (count < cmax) {
0N/A matcher.locals[countIndex] = count + 1;
0N/A boolean b = body.match(matcher, i, seq);
0N/A // If match failed we must backtrack, so
0N/A // the loop count should NOT be incremented
0N/A if (!b)
0N/A matcher.locals[countIndex] = count;
0N/A else
0N/A return true;
0N/A }
0N/A }
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A boolean matchInit(Matcher matcher, int i, CharSequence seq) {
0N/A int save = matcher.locals[countIndex];
0N/A boolean ret = false;
0N/A if (0 < cmin) {
0N/A matcher.locals[countIndex] = 1;
0N/A ret = body.match(matcher, i, seq);
0N/A } else if (0 < cmax) {
0N/A matcher.locals[countIndex] = 1;
0N/A ret = body.match(matcher, i, seq);
0N/A if (ret == false)
0N/A ret = next.match(matcher, i, seq);
0N/A } else {
0N/A ret = next.match(matcher, i, seq);
0N/A }
0N/A matcher.locals[countIndex] = save;
0N/A return ret;
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A info.maxValid = false;
0N/A info.deterministic = false;
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Handles the repetition count for a reluctant Curly. The matchInit
0N/A * is called from the Prolog to save the index of where the group
0N/A * beginning is stored. A zero length group check occurs in the
0N/A * normal match but is skipped in the matchInit.
0N/A */
0N/A static final class LazyLoop extends Loop {
0N/A LazyLoop(int countIndex, int beginIndex) {
0N/A super(countIndex, beginIndex);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A // Check for zero length group
0N/A if (i > matcher.locals[beginIndex]) {
0N/A int count = matcher.locals[countIndex];
0N/A if (count < cmin) {
0N/A matcher.locals[countIndex] = count + 1;
0N/A boolean result = body.match(matcher, i, seq);
0N/A // If match failed we must backtrack, so
0N/A // the loop count should NOT be incremented
0N/A if (!result)
0N/A matcher.locals[countIndex] = count;
0N/A return result;
0N/A }
0N/A if (next.match(matcher, i, seq))
0N/A return true;
0N/A if (count < cmax) {
0N/A matcher.locals[countIndex] = count + 1;
0N/A boolean result = body.match(matcher, i, seq);
0N/A // If match failed we must backtrack, so
0N/A // the loop count should NOT be incremented
0N/A if (!result)
0N/A matcher.locals[countIndex] = count;
0N/A return result;
0N/A }
0N/A return false;
0N/A }
0N/A return next.match(matcher, i, seq);
0N/A }
0N/A boolean matchInit(Matcher matcher, int i, CharSequence seq) {
0N/A int save = matcher.locals[countIndex];
0N/A boolean ret = false;
0N/A if (0 < cmin) {
0N/A matcher.locals[countIndex] = 1;
0N/A ret = body.match(matcher, i, seq);
0N/A } else if (next.match(matcher, i, seq)) {
0N/A ret = true;
0N/A } else if (0 < cmax) {
0N/A matcher.locals[countIndex] = 1;
0N/A ret = body.match(matcher, i, seq);
0N/A }
0N/A matcher.locals[countIndex] = save;
0N/A return ret;
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A info.maxValid = false;
0N/A info.deterministic = false;
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Refers to a group in the regular expression. Attempts to match
0N/A * whatever the group referred to last matched.
0N/A */
0N/A static class BackRef extends Node {
0N/A int groupIndex;
0N/A BackRef(int groupCount) {
0N/A super();
0N/A groupIndex = groupCount + groupCount;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int j = matcher.groups[groupIndex];
0N/A int k = matcher.groups[groupIndex+1];
0N/A
0N/A int groupSize = k - j;
0N/A
0N/A // If the referenced group didn't match, neither can this
0N/A if (j < 0)
0N/A return false;
0N/A
0N/A // If there isn't enough input left no match
0N/A if (i + groupSize > matcher.to) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A
0N/A // Check each new char to make sure it matches what the group
0N/A // referenced matched last time around
0N/A for (int index=0; index<groupSize; index++)
0N/A if (seq.charAt(i+index) != seq.charAt(j+index))
0N/A return false;
0N/A
0N/A return next.match(matcher, i+groupSize, seq);
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A info.maxValid = false;
0N/A return next.study(info);
0N/A }
0N/A }
0N/A
0N/A static class CIBackRef extends Node {
0N/A int groupIndex;
0N/A boolean doUnicodeCase;
0N/A CIBackRef(int groupCount, boolean doUnicodeCase) {
0N/A super();
0N/A groupIndex = groupCount + groupCount;
0N/A this.doUnicodeCase = doUnicodeCase;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int j = matcher.groups[groupIndex];
0N/A int k = matcher.groups[groupIndex+1];
0N/A
0N/A int groupSize = k - j;
0N/A
0N/A // If the referenced group didn't match, neither can this
0N/A if (j < 0)
0N/A return false;
0N/A
0N/A // If there isn't enough input left no match
0N/A if (i + groupSize > matcher.to) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A
0N/A // Check each new char to make sure it matches what the group
0N/A // referenced matched last time around
0N/A int x = i;
0N/A for (int index=0; index<groupSize; index++) {
0N/A int c1 = Character.codePointAt(seq, x);
0N/A int c2 = Character.codePointAt(seq, j);
0N/A if (c1 != c2) {
0N/A if (doUnicodeCase) {
0N/A int cc1 = Character.toUpperCase(c1);
0N/A int cc2 = Character.toUpperCase(c2);
0N/A if (cc1 != cc2 &&
0N/A Character.toLowerCase(cc1) !=
0N/A Character.toLowerCase(cc2))
0N/A return false;
0N/A } else {
0N/A if (ASCII.toLower(c1) != ASCII.toLower(c2))
0N/A return false;
0N/A }
0N/A }
0N/A x += Character.charCount(c1);
0N/A j += Character.charCount(c2);
0N/A }
0N/A
0N/A return next.match(matcher, i+groupSize, seq);
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A info.maxValid = false;
0N/A return next.study(info);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Searches until the next instance of its atom. This is useful for
0N/A * finding the atom efficiently without passing an instance of it
0N/A * (greedy problem) and without a lot of wasted search time (reluctant
0N/A * problem).
0N/A */
0N/A static final class First extends Node {
0N/A Node atom;
0N/A First(Node node) {
0N/A this.atom = BnM.optimize(node);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A if (atom instanceof BnM) {
0N/A return atom.match(matcher, i, seq)
0N/A && next.match(matcher, matcher.last, seq);
0N/A }
0N/A for (;;) {
0N/A if (i > matcher.to) {
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A if (atom.match(matcher, i, seq)) {
0N/A return next.match(matcher, matcher.last, seq);
0N/A }
0N/A i += countChars(seq, i, 1);
0N/A matcher.first++;
0N/A }
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A atom.study(info);
0N/A info.maxValid = false;
0N/A info.deterministic = false;
0N/A return next.study(info);
0N/A }
0N/A }
0N/A
0N/A static final class Conditional extends Node {
0N/A Node cond, yes, not;
0N/A Conditional(Node cond, Node yes, Node not) {
0N/A this.cond = cond;
0N/A this.yes = yes;
0N/A this.not = not;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A if (cond.match(matcher, i, seq)) {
0N/A return yes.match(matcher, i, seq);
0N/A } else {
0N/A return not.match(matcher, i, seq);
0N/A }
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A int minL = info.minLength;
0N/A int maxL = info.maxLength;
0N/A boolean maxV = info.maxValid;
0N/A info.reset();
0N/A yes.study(info);
0N/A
0N/A int minL2 = info.minLength;
0N/A int maxL2 = info.maxLength;
0N/A boolean maxV2 = info.maxValid;
0N/A info.reset();
0N/A not.study(info);
0N/A
0N/A info.minLength = minL + Math.min(minL2, info.minLength);
0N/A info.maxLength = maxL + Math.max(maxL2, info.maxLength);
0N/A info.maxValid = (maxV & maxV2 & info.maxValid);
0N/A info.deterministic = false;
0N/A return next.study(info);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Zero width positive lookahead.
0N/A */
0N/A static final class Pos extends Node {
0N/A Node cond;
0N/A Pos(Node cond) {
0N/A this.cond = cond;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int savedTo = matcher.to;
0N/A boolean conditionMatched = false;
0N/A
0N/A // Relax transparent region boundaries for lookahead
0N/A if (matcher.transparentBounds)
0N/A matcher.to = matcher.getTextLength();
0N/A try {
0N/A conditionMatched = cond.match(matcher, i, seq);
0N/A } finally {
0N/A // Reinstate region boundaries
0N/A matcher.to = savedTo;
0N/A }
0N/A return conditionMatched && next.match(matcher, i, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Zero width negative lookahead.
0N/A */
0N/A static final class Neg extends Node {
0N/A Node cond;
0N/A Neg(Node cond) {
0N/A this.cond = cond;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int savedTo = matcher.to;
0N/A boolean conditionMatched = false;
0N/A
0N/A // Relax transparent region boundaries for lookahead
0N/A if (matcher.transparentBounds)
0N/A matcher.to = matcher.getTextLength();
0N/A try {
0N/A if (i < matcher.to) {
0N/A conditionMatched = !cond.match(matcher, i, seq);
0N/A } else {
0N/A // If a negative lookahead succeeds then more input
0N/A // could cause it to fail!
0N/A matcher.requireEnd = true;
0N/A conditionMatched = !cond.match(matcher, i, seq);
0N/A }
0N/A } finally {
0N/A // Reinstate region boundaries
0N/A matcher.to = savedTo;
0N/A }
0N/A return conditionMatched && next.match(matcher, i, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * For use with lookbehinds; matches the position where the lookbehind
0N/A * was encountered.
0N/A */
0N/A static Node lookbehindEnd = new Node() {
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A return i == matcher.lookbehindTo;
0N/A }
0N/A };
0N/A
0N/A /**
0N/A * Zero width positive lookbehind.
0N/A */
0N/A static class Behind extends Node {
0N/A Node cond;
0N/A int rmax, rmin;
0N/A Behind(Node cond, int rmax, int rmin) {
0N/A this.cond = cond;
0N/A this.rmax = rmax;
0N/A this.rmin = rmin;
0N/A }
0N/A
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int savedFrom = matcher.from;
0N/A boolean conditionMatched = false;
0N/A int startIndex = (!matcher.transparentBounds) ?
0N/A matcher.from : 0;
0N/A int from = Math.max(i - rmax, startIndex);
0N/A // Set end boundary
0N/A int savedLBT = matcher.lookbehindTo;
0N/A matcher.lookbehindTo = i;
0N/A // Relax transparent region boundaries for lookbehind
0N/A if (matcher.transparentBounds)
0N/A matcher.from = 0;
0N/A for (int j = i - rmin; !conditionMatched && j >= from; j--) {
0N/A conditionMatched = cond.match(matcher, j, seq);
0N/A }
0N/A matcher.from = savedFrom;
0N/A matcher.lookbehindTo = savedLBT;
0N/A return conditionMatched && next.match(matcher, i, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Zero width positive lookbehind, including supplementary
0N/A * characters or unpaired surrogates.
0N/A */
0N/A static final class BehindS extends Behind {
0N/A BehindS(Node cond, int rmax, int rmin) {
0N/A super(cond, rmax, rmin);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int rmaxChars = countChars(seq, i, -rmax);
0N/A int rminChars = countChars(seq, i, -rmin);
0N/A int savedFrom = matcher.from;
0N/A int startIndex = (!matcher.transparentBounds) ?
0N/A matcher.from : 0;
0N/A boolean conditionMatched = false;
0N/A int from = Math.max(i - rmaxChars, startIndex);
0N/A // Set end boundary
0N/A int savedLBT = matcher.lookbehindTo;
0N/A matcher.lookbehindTo = i;
0N/A // Relax transparent region boundaries for lookbehind
0N/A if (matcher.transparentBounds)
0N/A matcher.from = 0;
0N/A
0N/A for (int j = i - rminChars;
0N/A !conditionMatched && j >= from;
0N/A j -= j>from ? countChars(seq, j, -1) : 1) {
0N/A conditionMatched = cond.match(matcher, j, seq);
0N/A }
0N/A matcher.from = savedFrom;
0N/A matcher.lookbehindTo = savedLBT;
0N/A return conditionMatched && next.match(matcher, i, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Zero width negative lookbehind.
0N/A */
0N/A static class NotBehind extends Node {
0N/A Node cond;
0N/A int rmax, rmin;
0N/A NotBehind(Node cond, int rmax, int rmin) {
0N/A this.cond = cond;
0N/A this.rmax = rmax;
0N/A this.rmin = rmin;
0N/A }
0N/A
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int savedLBT = matcher.lookbehindTo;
0N/A int savedFrom = matcher.from;
0N/A boolean conditionMatched = false;
0N/A int startIndex = (!matcher.transparentBounds) ?
0N/A matcher.from : 0;
0N/A int from = Math.max(i - rmax, startIndex);
0N/A matcher.lookbehindTo = i;
0N/A // Relax transparent region boundaries for lookbehind
0N/A if (matcher.transparentBounds)
0N/A matcher.from = 0;
0N/A for (int j = i - rmin; !conditionMatched && j >= from; j--) {
0N/A conditionMatched = cond.match(matcher, j, seq);
0N/A }
0N/A // Reinstate region boundaries
0N/A matcher.from = savedFrom;
0N/A matcher.lookbehindTo = savedLBT;
0N/A return !conditionMatched && next.match(matcher, i, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Zero width negative lookbehind, including supplementary
0N/A * characters or unpaired surrogates.
0N/A */
0N/A static final class NotBehindS extends NotBehind {
0N/A NotBehindS(Node cond, int rmax, int rmin) {
0N/A super(cond, rmax, rmin);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int rmaxChars = countChars(seq, i, -rmax);
0N/A int rminChars = countChars(seq, i, -rmin);
0N/A int savedFrom = matcher.from;
0N/A int savedLBT = matcher.lookbehindTo;
0N/A boolean conditionMatched = false;
0N/A int startIndex = (!matcher.transparentBounds) ?
0N/A matcher.from : 0;
0N/A int from = Math.max(i - rmaxChars, startIndex);
0N/A matcher.lookbehindTo = i;
0N/A // Relax transparent region boundaries for lookbehind
0N/A if (matcher.transparentBounds)
0N/A matcher.from = 0;
0N/A for (int j = i - rminChars;
0N/A !conditionMatched && j >= from;
0N/A j -= j>from ? countChars(seq, j, -1) : 1) {
0N/A conditionMatched = cond.match(matcher, j, seq);
0N/A }
0N/A //Reinstate region boundaries
0N/A matcher.from = savedFrom;
0N/A matcher.lookbehindTo = savedLBT;
0N/A return !conditionMatched && next.match(matcher, i, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the set union of two CharProperty nodes.
0N/A */
0N/A private static CharProperty union(final CharProperty lhs,
0N/A final CharProperty rhs) {
0N/A return new CharProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return lhs.isSatisfiedBy(ch) || rhs.isSatisfiedBy(ch);}};
0N/A }
0N/A
0N/A /**
0N/A * Returns the set intersection of two CharProperty nodes.
0N/A */
0N/A private static CharProperty intersection(final CharProperty lhs,
0N/A final CharProperty rhs) {
0N/A return new CharProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return lhs.isSatisfiedBy(ch) && rhs.isSatisfiedBy(ch);}};
0N/A }
0N/A
0N/A /**
0N/A * Returns the set difference of two CharProperty nodes.
0N/A */
0N/A private static CharProperty setDifference(final CharProperty lhs,
0N/A final CharProperty rhs) {
0N/A return new CharProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return ! rhs.isSatisfiedBy(ch) && lhs.isSatisfiedBy(ch);}};
0N/A }
0N/A
0N/A /**
0N/A * Handles word boundaries. Includes a field to allow this one class to
0N/A * deal with the different types of word boundaries we can match. The word
0N/A * characters include underscores, letters, and digits. Non spacing marks
0N/A * can are also part of a word if they have a base character, otherwise
0N/A * they are ignored for purposes of finding word boundaries.
0N/A */
0N/A static final class Bound extends Node {
0N/A static int LEFT = 0x1;
0N/A static int RIGHT= 0x2;
0N/A static int BOTH = 0x3;
0N/A static int NONE = 0x4;
0N/A int type;
4139N/A boolean useUWORD;
4139N/A Bound(int n, boolean useUWORD) {
0N/A type = n;
4139N/A this.useUWORD = useUWORD;
4139N/A }
4139N/A
4139N/A boolean isWord(int ch) {
4139N/A return useUWORD ? UnicodeProp.WORD.is(ch)
4139N/A : (ch == '_' || Character.isLetterOrDigit(ch));
4139N/A }
4139N/A
0N/A int check(Matcher matcher, int i, CharSequence seq) {
0N/A int ch;
0N/A boolean left = false;
0N/A int startIndex = matcher.from;
0N/A int endIndex = matcher.to;
0N/A if (matcher.transparentBounds) {
0N/A startIndex = 0;
0N/A endIndex = matcher.getTextLength();
0N/A }
0N/A if (i > startIndex) {
0N/A ch = Character.codePointBefore(seq, i);
4139N/A left = (isWord(ch) ||
0N/A ((Character.getType(ch) == Character.NON_SPACING_MARK)
0N/A && hasBaseCharacter(matcher, i-1, seq)));
0N/A }
0N/A boolean right = false;
0N/A if (i < endIndex) {
0N/A ch = Character.codePointAt(seq, i);
4139N/A right = (isWord(ch) ||
0N/A ((Character.getType(ch) == Character.NON_SPACING_MARK)
0N/A && hasBaseCharacter(matcher, i, seq)));
0N/A } else {
0N/A // Tried to access char past the end
0N/A matcher.hitEnd = true;
0N/A // The addition of another char could wreck a boundary
0N/A matcher.requireEnd = true;
0N/A }
0N/A return ((left ^ right) ? (right ? LEFT : RIGHT) : NONE);
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A return (check(matcher, i, seq) & type) > 0
0N/A && next.match(matcher, i, seq);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Non spacing marks only count as word characters in bounds calculations
0N/A * if they have a base character.
0N/A */
0N/A private static boolean hasBaseCharacter(Matcher matcher, int i,
0N/A CharSequence seq)
0N/A {
0N/A int start = (!matcher.transparentBounds) ?
0N/A matcher.from : 0;
0N/A for (int x=i; x >= start; x--) {
0N/A int ch = Character.codePointAt(seq, x);
0N/A if (Character.isLetterOrDigit(ch))
0N/A return true;
0N/A if (Character.getType(ch) == Character.NON_SPACING_MARK)
0N/A continue;
0N/A return false;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Attempts to match a slice in the input using the Boyer-Moore string
0N/A * matching algorithm. The algorithm is based on the idea that the
0N/A * pattern can be shifted farther ahead in the search text if it is
0N/A * matched right to left.
0N/A * <p>
0N/A * The pattern is compared to the input one character at a time, from
0N/A * the rightmost character in the pattern to the left. If the characters
0N/A * all match the pattern has been found. If a character does not match,
0N/A * the pattern is shifted right a distance that is the maximum of two
0N/A * functions, the bad character shift and the good suffix shift. This
0N/A * shift moves the attempted match position through the input more
0N/A * quickly than a naive one position at a time check.
0N/A * <p>
0N/A * The bad character shift is based on the character from the text that
0N/A * did not match. If the character does not appear in the pattern, the
0N/A * pattern can be shifted completely beyond the bad character. If the
0N/A * character does occur in the pattern, the pattern can be shifted to
0N/A * line the pattern up with the next occurrence of that character.
0N/A * <p>
0N/A * The good suffix shift is based on the idea that some subset on the right
0N/A * side of the pattern has matched. When a bad character is found, the
0N/A * pattern can be shifted right by the pattern length if the subset does
0N/A * not occur again in pattern, or by the amount of distance to the
0N/A * next occurrence of the subset in the pattern.
0N/A *
0N/A * Boyer-Moore search methods adapted from code by Amy Yu.
0N/A */
0N/A static class BnM extends Node {
0N/A int[] buffer;
0N/A int[] lastOcc;
0N/A int[] optoSft;
0N/A
0N/A /**
0N/A * Pre calculates arrays needed to generate the bad character
0N/A * shift and the good suffix shift. Only the last seven bits
0N/A * are used to see if chars match; This keeps the tables small
0N/A * and covers the heavily used ASCII range, but occasionally
0N/A * results in an aliased match for the bad character shift.
0N/A */
0N/A static Node optimize(Node node) {
0N/A if (!(node instanceof Slice)) {
0N/A return node;
0N/A }
0N/A
0N/A int[] src = ((Slice) node).buffer;
0N/A int patternLength = src.length;
0N/A // The BM algorithm requires a bit of overhead;
0N/A // If the pattern is short don't use it, since
0N/A // a shift larger than the pattern length cannot
0N/A // be used anyway.
0N/A if (patternLength < 4) {
0N/A return node;
0N/A }
0N/A int i, j, k;
0N/A int[] lastOcc = new int[128];
0N/A int[] optoSft = new int[patternLength];
0N/A // Precalculate part of the bad character shift
0N/A // It is a table for where in the pattern each
0N/A // lower 7-bit value occurs
0N/A for (i = 0; i < patternLength; i++) {
0N/A lastOcc[src[i]&0x7F] = i + 1;
0N/A }
0N/A // Precalculate the good suffix shift
0N/A // i is the shift amount being considered
0N/ANEXT: for (i = patternLength; i > 0; i--) {
0N/A // j is the beginning index of suffix being considered
0N/A for (j = patternLength - 1; j >= i; j--) {
0N/A // Testing for good suffix
0N/A if (src[j] == src[j-i]) {
0N/A // src[j..len] is a good suffix
0N/A optoSft[j-1] = i;
0N/A } else {
0N/A // No match. The array has already been
0N/A // filled up with correct values before.
0N/A continue NEXT;
0N/A }
0N/A }
0N/A // This fills up the remaining of optoSft
0N/A // any suffix can not have larger shift amount
0N/A // then its sub-suffix. Why???
0N/A while (j > 0) {
0N/A optoSft[--j] = i;
0N/A }
0N/A }
0N/A // Set the guard value because of unicode compression
0N/A optoSft[patternLength-1] = 1;
0N/A if (node instanceof SliceS)
0N/A return new BnMS(src, lastOcc, optoSft, node.next);
0N/A return new BnM(src, lastOcc, optoSft, node.next);
0N/A }
0N/A BnM(int[] src, int[] lastOcc, int[] optoSft, Node next) {
0N/A this.buffer = src;
0N/A this.lastOcc = lastOcc;
0N/A this.optoSft = optoSft;
0N/A this.next = next;
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int[] src = buffer;
0N/A int patternLength = src.length;
0N/A int last = matcher.to - patternLength;
0N/A
0N/A // Loop over all possible match positions in text
0N/ANEXT: while (i <= last) {
0N/A // Loop over pattern from right to left
0N/A for (int j = patternLength - 1; j >= 0; j--) {
0N/A int ch = seq.charAt(i+j);
0N/A if (ch != src[j]) {
0N/A // Shift search to the right by the maximum of the
0N/A // bad character shift and the good suffix shift
0N/A i += Math.max(j + 1 - lastOcc[ch&0x7F], optoSft[j]);
0N/A continue NEXT;
0N/A }
0N/A }
0N/A // Entire pattern matched starting at i
0N/A matcher.first = i;
0N/A boolean ret = next.match(matcher, i + patternLength, seq);
0N/A if (ret) {
0N/A matcher.first = i;
0N/A matcher.groups[0] = matcher.first;
0N/A matcher.groups[1] = matcher.last;
0N/A return true;
0N/A }
0N/A i++;
0N/A }
0N/A // BnM is only used as the leading node in the unanchored case,
0N/A // and it replaced its Start() which always searches to the end
0N/A // if it doesn't find what it's looking for, so hitEnd is true.
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A boolean study(TreeInfo info) {
0N/A info.minLength += buffer.length;
0N/A info.maxValid = false;
0N/A return next.study(info);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Supplementary support version of BnM(). Unpaired surrogates are
0N/A * also handled by this class.
0N/A */
0N/A static final class BnMS extends BnM {
0N/A int lengthInChars;
0N/A
0N/A BnMS(int[] src, int[] lastOcc, int[] optoSft, Node next) {
0N/A super(src, lastOcc, optoSft, next);
0N/A for (int x = 0; x < buffer.length; x++) {
0N/A lengthInChars += Character.charCount(buffer[x]);
0N/A }
0N/A }
0N/A boolean match(Matcher matcher, int i, CharSequence seq) {
0N/A int[] src = buffer;
0N/A int patternLength = src.length;
0N/A int last = matcher.to - lengthInChars;
0N/A
0N/A // Loop over all possible match positions in text
0N/ANEXT: while (i <= last) {
0N/A // Loop over pattern from right to left
0N/A int ch;
0N/A for (int j = countChars(seq, i, patternLength), x = patternLength - 1;
0N/A j > 0; j -= Character.charCount(ch), x--) {
0N/A ch = Character.codePointBefore(seq, i+j);
0N/A if (ch != src[x]) {
0N/A // Shift search to the right by the maximum of the
0N/A // bad character shift and the good suffix shift
0N/A int n = Math.max(x + 1 - lastOcc[ch&0x7F], optoSft[x]);
0N/A i += countChars(seq, i, n);
0N/A continue NEXT;
0N/A }
0N/A }
0N/A // Entire pattern matched starting at i
0N/A matcher.first = i;
0N/A boolean ret = next.match(matcher, i + lengthInChars, seq);
0N/A if (ret) {
0N/A matcher.first = i;
0N/A matcher.groups[0] = matcher.first;
0N/A matcher.groups[1] = matcher.last;
0N/A return true;
0N/A }
0N/A i += countChars(seq, i, 1);
0N/A }
0N/A matcher.hitEnd = true;
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A///////////////////////////////////////////////////////////////////////////////
0N/A///////////////////////////////////////////////////////////////////////////////
0N/A
0N/A /**
0N/A * This must be the very first initializer.
0N/A */
0N/A static Node accept = new Node();
0N/A
0N/A static Node lastAccept = new LastNode();
0N/A
0N/A private static class CharPropertyNames {
0N/A
0N/A static CharProperty charPropertyFor(String name) {
0N/A CharPropertyFactory m = map.get(name);
0N/A return m == null ? null : m.make();
0N/A }
0N/A
0N/A private static abstract class CharPropertyFactory {
0N/A abstract CharProperty make();
0N/A }
0N/A
0N/A private static void defCategory(String name,
0N/A final int typeMask) {
0N/A map.put(name, new CharPropertyFactory() {
0N/A CharProperty make() { return new Category(typeMask);}});
0N/A }
0N/A
0N/A private static void defRange(String name,
0N/A final int lower, final int upper) {
0N/A map.put(name, new CharPropertyFactory() {
0N/A CharProperty make() { return rangeFor(lower, upper);}});
0N/A }
0N/A
0N/A private static void defCtype(String name,
0N/A final int ctype) {
0N/A map.put(name, new CharPropertyFactory() {
0N/A CharProperty make() { return new Ctype(ctype);}});
0N/A }
0N/A
0N/A private static abstract class CloneableProperty
0N/A extends CharProperty implements Cloneable
0N/A {
0N/A public CloneableProperty clone() {
0N/A try {
0N/A return (CloneableProperty) super.clone();
0N/A } catch (CloneNotSupportedException e) {
0N/A throw new AssertionError(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static void defClone(String name,
0N/A final CloneableProperty p) {
0N/A map.put(name, new CharPropertyFactory() {
0N/A CharProperty make() { return p.clone();}});
0N/A }
0N/A
0N/A private static final HashMap<String, CharPropertyFactory> map
3323N/A = new HashMap<>();
0N/A
0N/A static {
0N/A // Unicode character property aliases, defined in
0N/A // http://www.unicode.org/Public/UNIDATA/PropertyValueAliases.txt
0N/A defCategory("Cn", 1<<Character.UNASSIGNED);
0N/A defCategory("Lu", 1<<Character.UPPERCASE_LETTER);
0N/A defCategory("Ll", 1<<Character.LOWERCASE_LETTER);
0N/A defCategory("Lt", 1<<Character.TITLECASE_LETTER);
0N/A defCategory("Lm", 1<<Character.MODIFIER_LETTER);
0N/A defCategory("Lo", 1<<Character.OTHER_LETTER);
0N/A defCategory("Mn", 1<<Character.NON_SPACING_MARK);
0N/A defCategory("Me", 1<<Character.ENCLOSING_MARK);
0N/A defCategory("Mc", 1<<Character.COMBINING_SPACING_MARK);
0N/A defCategory("Nd", 1<<Character.DECIMAL_DIGIT_NUMBER);
0N/A defCategory("Nl", 1<<Character.LETTER_NUMBER);
0N/A defCategory("No", 1<<Character.OTHER_NUMBER);
0N/A defCategory("Zs", 1<<Character.SPACE_SEPARATOR);
0N/A defCategory("Zl", 1<<Character.LINE_SEPARATOR);
0N/A defCategory("Zp", 1<<Character.PARAGRAPH_SEPARATOR);
0N/A defCategory("Cc", 1<<Character.CONTROL);
0N/A defCategory("Cf", 1<<Character.FORMAT);
0N/A defCategory("Co", 1<<Character.PRIVATE_USE);
0N/A defCategory("Cs", 1<<Character.SURROGATE);
0N/A defCategory("Pd", 1<<Character.DASH_PUNCTUATION);
0N/A defCategory("Ps", 1<<Character.START_PUNCTUATION);
0N/A defCategory("Pe", 1<<Character.END_PUNCTUATION);
0N/A defCategory("Pc", 1<<Character.CONNECTOR_PUNCTUATION);
0N/A defCategory("Po", 1<<Character.OTHER_PUNCTUATION);
0N/A defCategory("Sm", 1<<Character.MATH_SYMBOL);
0N/A defCategory("Sc", 1<<Character.CURRENCY_SYMBOL);
0N/A defCategory("Sk", 1<<Character.MODIFIER_SYMBOL);
0N/A defCategory("So", 1<<Character.OTHER_SYMBOL);
0N/A defCategory("Pi", 1<<Character.INITIAL_QUOTE_PUNCTUATION);
0N/A defCategory("Pf", 1<<Character.FINAL_QUOTE_PUNCTUATION);
0N/A defCategory("L", ((1<<Character.UPPERCASE_LETTER) |
0N/A (1<<Character.LOWERCASE_LETTER) |
0N/A (1<<Character.TITLECASE_LETTER) |
0N/A (1<<Character.MODIFIER_LETTER) |
0N/A (1<<Character.OTHER_LETTER)));
0N/A defCategory("M", ((1<<Character.NON_SPACING_MARK) |
0N/A (1<<Character.ENCLOSING_MARK) |
0N/A (1<<Character.COMBINING_SPACING_MARK)));
0N/A defCategory("N", ((1<<Character.DECIMAL_DIGIT_NUMBER) |
0N/A (1<<Character.LETTER_NUMBER) |
0N/A (1<<Character.OTHER_NUMBER)));
0N/A defCategory("Z", ((1<<Character.SPACE_SEPARATOR) |
0N/A (1<<Character.LINE_SEPARATOR) |
0N/A (1<<Character.PARAGRAPH_SEPARATOR)));
0N/A defCategory("C", ((1<<Character.CONTROL) |
0N/A (1<<Character.FORMAT) |
0N/A (1<<Character.PRIVATE_USE) |
0N/A (1<<Character.SURROGATE))); // Other
0N/A defCategory("P", ((1<<Character.DASH_PUNCTUATION) |
0N/A (1<<Character.START_PUNCTUATION) |
0N/A (1<<Character.END_PUNCTUATION) |
0N/A (1<<Character.CONNECTOR_PUNCTUATION) |
0N/A (1<<Character.OTHER_PUNCTUATION) |
0N/A (1<<Character.INITIAL_QUOTE_PUNCTUATION) |
0N/A (1<<Character.FINAL_QUOTE_PUNCTUATION)));
0N/A defCategory("S", ((1<<Character.MATH_SYMBOL) |
0N/A (1<<Character.CURRENCY_SYMBOL) |
0N/A (1<<Character.MODIFIER_SYMBOL) |
0N/A (1<<Character.OTHER_SYMBOL)));
0N/A defCategory("LC", ((1<<Character.UPPERCASE_LETTER) |
0N/A (1<<Character.LOWERCASE_LETTER) |
0N/A (1<<Character.TITLECASE_LETTER)));
0N/A defCategory("LD", ((1<<Character.UPPERCASE_LETTER) |
0N/A (1<<Character.LOWERCASE_LETTER) |
0N/A (1<<Character.TITLECASE_LETTER) |
0N/A (1<<Character.MODIFIER_LETTER) |
0N/A (1<<Character.OTHER_LETTER) |
0N/A (1<<Character.DECIMAL_DIGIT_NUMBER)));
0N/A defRange("L1", 0x00, 0xFF); // Latin-1
0N/A map.put("all", new CharPropertyFactory() {
0N/A CharProperty make() { return new All(); }});
0N/A
0N/A // Posix regular expression character classes, defined in
0N/A // http://www.unix.org/onlinepubs/009695399/basedefs/xbd_chap09.html
0N/A defRange("ASCII", 0x00, 0x7F); // ASCII
0N/A defCtype("Alnum", ASCII.ALNUM); // Alphanumeric characters
0N/A defCtype("Alpha", ASCII.ALPHA); // Alphabetic characters
0N/A defCtype("Blank", ASCII.BLANK); // Space and tab characters
0N/A defCtype("Cntrl", ASCII.CNTRL); // Control characters
0N/A defRange("Digit", '0', '9'); // Numeric characters
0N/A defCtype("Graph", ASCII.GRAPH); // printable and visible
0N/A defRange("Lower", 'a', 'z'); // Lower-case alphabetic
0N/A defRange("Print", 0x20, 0x7E); // Printable characters
0N/A defCtype("Punct", ASCII.PUNCT); // Punctuation characters
0N/A defCtype("Space", ASCII.SPACE); // Space characters
0N/A defRange("Upper", 'A', 'Z'); // Upper-case alphabetic
0N/A defCtype("XDigit",ASCII.XDIGIT); // hexadecimal digits
0N/A
0N/A // Java character properties, defined by methods in Character.java
0N/A defClone("javaLowerCase", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isLowerCase(ch);}});
0N/A defClone("javaUpperCase", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isUpperCase(ch);}});
4139N/A defClone("javaAlphabetic", new CloneableProperty() {
4139N/A boolean isSatisfiedBy(int ch) {
4139N/A return Character.isAlphabetic(ch);}});
4139N/A defClone("javaIdeographic", new CloneableProperty() {
4139N/A boolean isSatisfiedBy(int ch) {
4139N/A return Character.isIdeographic(ch);}});
0N/A defClone("javaTitleCase", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isTitleCase(ch);}});
0N/A defClone("javaDigit", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isDigit(ch);}});
0N/A defClone("javaDefined", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isDefined(ch);}});
0N/A defClone("javaLetter", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isLetter(ch);}});
0N/A defClone("javaLetterOrDigit", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isLetterOrDigit(ch);}});
0N/A defClone("javaJavaIdentifierStart", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isJavaIdentifierStart(ch);}});
0N/A defClone("javaJavaIdentifierPart", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isJavaIdentifierPart(ch);}});
0N/A defClone("javaUnicodeIdentifierStart", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isUnicodeIdentifierStart(ch);}});
0N/A defClone("javaUnicodeIdentifierPart", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isUnicodeIdentifierPart(ch);}});
0N/A defClone("javaIdentifierIgnorable", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isIdentifierIgnorable(ch);}});
0N/A defClone("javaSpaceChar", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isSpaceChar(ch);}});
0N/A defClone("javaWhitespace", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isWhitespace(ch);}});
0N/A defClone("javaISOControl", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isISOControl(ch);}});
0N/A defClone("javaMirrored", new CloneableProperty() {
0N/A boolean isSatisfiedBy(int ch) {
0N/A return Character.isMirrored(ch);}});
0N/A }
0N/A }
0N/A}