1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/util/regex/Pattern.java Mon Oct 07 16:13:27 2013 +0200
1.3 @@ -0,0 +1,5648 @@
1.4 +/*
1.5 + * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.util.regex;
1.30 +
1.31 +import java.security.AccessController;
1.32 +import java.security.PrivilegedAction;
1.33 +import java.text.CharacterIterator;
1.34 +import java.text.Normalizer;
1.35 +import java.util.Locale;
1.36 +import java.util.Map;
1.37 +import java.util.ArrayList;
1.38 +import java.util.HashMap;
1.39 +import java.util.Arrays;
1.40 +
1.41 +
1.42 +/**
1.43 + * A compiled representation of a regular expression.
1.44 + *
1.45 + * <p> A regular expression, specified as a string, must first be compiled into
1.46 + * an instance of this class. The resulting pattern can then be used to create
1.47 + * a {@link Matcher} object that can match arbitrary {@link
1.48 + * java.lang.CharSequence </code>character sequences<code>} against the regular
1.49 + * expression. All of the state involved in performing a match resides in the
1.50 + * matcher, so many matchers can share the same pattern.
1.51 + *
1.52 + * <p> A typical invocation sequence is thus
1.53 + *
1.54 + * <blockquote><pre>
1.55 + * Pattern p = Pattern.{@link #compile compile}("a*b");
1.56 + * Matcher m = p.{@link #matcher matcher}("aaaaab");
1.57 + * boolean b = m.{@link Matcher#matches matches}();</pre></blockquote>
1.58 + *
1.59 + * <p> A {@link #matches matches} method is defined by this class as a
1.60 + * convenience for when a regular expression is used just once. This method
1.61 + * compiles an expression and matches an input sequence against it in a single
1.62 + * invocation. The statement
1.63 + *
1.64 + * <blockquote><pre>
1.65 + * boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote>
1.66 + *
1.67 + * is equivalent to the three statements above, though for repeated matches it
1.68 + * is less efficient since it does not allow the compiled pattern to be reused.
1.69 + *
1.70 + * <p> Instances of this class are immutable and are safe for use by multiple
1.71 + * concurrent threads. Instances of the {@link Matcher} class are not safe for
1.72 + * such use.
1.73 + *
1.74 + *
1.75 + * <a name="sum">
1.76 + * <h4> Summary of regular-expression constructs </h4>
1.77 + *
1.78 + * <table border="0" cellpadding="1" cellspacing="0"
1.79 + * summary="Regular expression constructs, and what they match">
1.80 + *
1.81 + * <tr align="left">
1.82 + * <th bgcolor="#CCCCFF" align="left" id="construct">Construct</th>
1.83 + * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
1.84 + * </tr>
1.85 + *
1.86 + * <tr><th> </th></tr>
1.87 + * <tr align="left"><th colspan="2" id="characters">Characters</th></tr>
1.88 + *
1.89 + * <tr><td valign="top" headers="construct characters"><i>x</i></td>
1.90 + * <td headers="matches">The character <i>x</i></td></tr>
1.91 + * <tr><td valign="top" headers="construct characters"><tt>\\</tt></td>
1.92 + * <td headers="matches">The backslash character</td></tr>
1.93 + * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>n</i></td>
1.94 + * <td headers="matches">The character with octal value <tt>0</tt><i>n</i>
1.95 + * (0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr>
1.96 + * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>nn</i></td>
1.97 + * <td headers="matches">The character with octal value <tt>0</tt><i>nn</i>
1.98 + * (0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr>
1.99 + * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>mnn</i></td>
1.100 + * <td headers="matches">The character with octal value <tt>0</tt><i>mnn</i>
1.101 + * (0 <tt><=</tt> <i>m</i> <tt><=</tt> 3,
1.102 + * 0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr>
1.103 + * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>hh</i></td>
1.104 + * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>hh</i></td></tr>
1.105 + * <tr><td valign="top" headers="construct characters"><tt>\u</tt><i>hhhh</i></td>
1.106 + * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>hhhh</i></td></tr>
1.107 + * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>{h...h}</i></td>
1.108 + * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>h...h</i>
1.109 + * ({@link java.lang.Character#MIN_CODE_POINT Character.MIN_CODE_POINT}
1.110 + * <= <tt>0x</tt><i>h...h</i> <= 
1.111 + * {@link java.lang.Character#MAX_CODE_POINT Character.MAX_CODE_POINT})</td></tr>
1.112 + * <tr><td valign="top" headers="matches"><tt>\t</tt></td>
1.113 + * <td headers="matches">The tab character (<tt>'\u0009'</tt>)</td></tr>
1.114 + * <tr><td valign="top" headers="construct characters"><tt>\n</tt></td>
1.115 + * <td headers="matches">The newline (line feed) character (<tt>'\u000A'</tt>)</td></tr>
1.116 + * <tr><td valign="top" headers="construct characters"><tt>\r</tt></td>
1.117 + * <td headers="matches">The carriage-return character (<tt>'\u000D'</tt>)</td></tr>
1.118 + * <tr><td valign="top" headers="construct characters"><tt>\f</tt></td>
1.119 + * <td headers="matches">The form-feed character (<tt>'\u000C'</tt>)</td></tr>
1.120 + * <tr><td valign="top" headers="construct characters"><tt>\a</tt></td>
1.121 + * <td headers="matches">The alert (bell) character (<tt>'\u0007'</tt>)</td></tr>
1.122 + * <tr><td valign="top" headers="construct characters"><tt>\e</tt></td>
1.123 + * <td headers="matches">The escape character (<tt>'\u001B'</tt>)</td></tr>
1.124 + * <tr><td valign="top" headers="construct characters"><tt>\c</tt><i>x</i></td>
1.125 + * <td headers="matches">The control character corresponding to <i>x</i></td></tr>
1.126 + *
1.127 + * <tr><th> </th></tr>
1.128 + * <tr align="left"><th colspan="2" id="classes">Character classes</th></tr>
1.129 + *
1.130 + * <tr><td valign="top" headers="construct classes"><tt>[abc]</tt></td>
1.131 + * <td headers="matches"><tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (simple class)</td></tr>
1.132 + * <tr><td valign="top" headers="construct classes"><tt>[^abc]</tt></td>
1.133 + * <td headers="matches">Any character except <tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (negation)</td></tr>
1.134 + * <tr><td valign="top" headers="construct classes"><tt>[a-zA-Z]</tt></td>
1.135 + * <td headers="matches"><tt>a</tt> through <tt>z</tt>
1.136 + * or <tt>A</tt> through <tt>Z</tt>, inclusive (range)</td></tr>
1.137 + * <tr><td valign="top" headers="construct classes"><tt>[a-d[m-p]]</tt></td>
1.138 + * <td headers="matches"><tt>a</tt> through <tt>d</tt>,
1.139 + * or <tt>m</tt> through <tt>p</tt>: <tt>[a-dm-p]</tt> (union)</td></tr>
1.140 + * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[def]]</tt></td>
1.141 + * <td headers="matches"><tt>d</tt>, <tt>e</tt>, or <tt>f</tt> (intersection)</tr>
1.142 + * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^bc]]</tt></td>
1.143 + * <td headers="matches"><tt>a</tt> through <tt>z</tt>,
1.144 + * except for <tt>b</tt> and <tt>c</tt>: <tt>[ad-z]</tt> (subtraction)</td></tr>
1.145 + * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^m-p]]</tt></td>
1.146 + * <td headers="matches"><tt>a</tt> through <tt>z</tt>,
1.147 + * and not <tt>m</tt> through <tt>p</tt>: <tt>[a-lq-z]</tt>(subtraction)</td></tr>
1.148 + * <tr><th> </th></tr>
1.149 + *
1.150 + * <tr align="left"><th colspan="2" id="predef">Predefined character classes</th></tr>
1.151 + *
1.152 + * <tr><td valign="top" headers="construct predef"><tt>.</tt></td>
1.153 + * <td headers="matches">Any character (may or may not match <a href="#lt">line terminators</a>)</td></tr>
1.154 + * <tr><td valign="top" headers="construct predef"><tt>\d</tt></td>
1.155 + * <td headers="matches">A digit: <tt>[0-9]</tt></td></tr>
1.156 + * <tr><td valign="top" headers="construct predef"><tt>\D</tt></td>
1.157 + * <td headers="matches">A non-digit: <tt>[^0-9]</tt></td></tr>
1.158 + * <tr><td valign="top" headers="construct predef"><tt>\s</tt></td>
1.159 + * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
1.160 + * <tr><td valign="top" headers="construct predef"><tt>\S</tt></td>
1.161 + * <td headers="matches">A non-whitespace character: <tt>[^\s]</tt></td></tr>
1.162 + * <tr><td valign="top" headers="construct predef"><tt>\w</tt></td>
1.163 + * <td headers="matches">A word character: <tt>[a-zA-Z_0-9]</tt></td></tr>
1.164 + * <tr><td valign="top" headers="construct predef"><tt>\W</tt></td>
1.165 + * <td headers="matches">A non-word character: <tt>[^\w]</tt></td></tr>
1.166 + *
1.167 + * <tr><th> </th></tr>
1.168 + * <tr align="left"><th colspan="2" id="posix">POSIX character classes</b> (US-ASCII only)<b></th></tr>
1.169 + *
1.170 + * <tr><td valign="top" headers="construct posix"><tt>\p{Lower}</tt></td>
1.171 + * <td headers="matches">A lower-case alphabetic character: <tt>[a-z]</tt></td></tr>
1.172 + * <tr><td valign="top" headers="construct posix"><tt>\p{Upper}</tt></td>
1.173 + * <td headers="matches">An upper-case alphabetic character:<tt>[A-Z]</tt></td></tr>
1.174 + * <tr><td valign="top" headers="construct posix"><tt>\p{ASCII}</tt></td>
1.175 + * <td headers="matches">All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
1.176 + * <tr><td valign="top" headers="construct posix"><tt>\p{Alpha}</tt></td>
1.177 + * <td headers="matches">An alphabetic character:<tt>[\p{Lower}\p{Upper}]</tt></td></tr>
1.178 + * <tr><td valign="top" headers="construct posix"><tt>\p{Digit}</tt></td>
1.179 + * <td headers="matches">A decimal digit: <tt>[0-9]</tt></td></tr>
1.180 + * <tr><td valign="top" headers="construct posix"><tt>\p{Alnum}</tt></td>
1.181 + * <td headers="matches">An alphanumeric character:<tt>[\p{Alpha}\p{Digit}]</tt></td></tr>
1.182 + * <tr><td valign="top" headers="construct posix"><tt>\p{Punct}</tt></td>
1.183 + * <td headers="matches">Punctuation: One of <tt>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</tt></td></tr>
1.184 + * <!-- <tt>[\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]</tt>
1.185 + * <tt>[\X21-\X2F\X31-\X40\X5B-\X60\X7B-\X7E]</tt> -->
1.186 + * <tr><td valign="top" headers="construct posix"><tt>\p{Graph}</tt></td>
1.187 + * <td headers="matches">A visible character: <tt>[\p{Alnum}\p{Punct}]</tt></td></tr>
1.188 + * <tr><td valign="top" headers="construct posix"><tt>\p{Print}</tt></td>
1.189 + * <td headers="matches">A printable character: <tt>[\p{Graph}\x20]</tt></td></tr>
1.190 + * <tr><td valign="top" headers="construct posix"><tt>\p{Blank}</tt></td>
1.191 + * <td headers="matches">A space or a tab: <tt>[ \t]</tt></td></tr>
1.192 + * <tr><td valign="top" headers="construct posix"><tt>\p{Cntrl}</tt></td>
1.193 + * <td headers="matches">A control character: <tt>[\x00-\x1F\x7F]</tt></td></tr>
1.194 + * <tr><td valign="top" headers="construct posix"><tt>\p{XDigit}</tt></td>
1.195 + * <td headers="matches">A hexadecimal digit: <tt>[0-9a-fA-F]</tt></td></tr>
1.196 + * <tr><td valign="top" headers="construct posix"><tt>\p{Space}</tt></td>
1.197 + * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
1.198 + *
1.199 + * <tr><th> </th></tr>
1.200 + * <tr align="left"><th colspan="2">java.lang.Character classes (simple <a href="#jcc">java character type</a>)</th></tr>
1.201 + *
1.202 + * <tr><td valign="top"><tt>\p{javaLowerCase}</tt></td>
1.203 + * <td>Equivalent to java.lang.Character.isLowerCase()</td></tr>
1.204 + * <tr><td valign="top"><tt>\p{javaUpperCase}</tt></td>
1.205 + * <td>Equivalent to java.lang.Character.isUpperCase()</td></tr>
1.206 + * <tr><td valign="top"><tt>\p{javaWhitespace}</tt></td>
1.207 + * <td>Equivalent to java.lang.Character.isWhitespace()</td></tr>
1.208 + * <tr><td valign="top"><tt>\p{javaMirrored}</tt></td>
1.209 + * <td>Equivalent to java.lang.Character.isMirrored()</td></tr>
1.210 + *
1.211 + * <tr><th> </th></tr>
1.212 + * <tr align="left"><th colspan="2" id="unicode">Classes for Unicode scripts, blocks, categories and binary properties</th></tr>
1.213 + * * <tr><td valign="top" headers="construct unicode"><tt>\p{IsLatin}</tt></td>
1.214 + * <td headers="matches">A Latin script character (<a href="#usc">script</a>)</td></tr>
1.215 + * <tr><td valign="top" headers="construct unicode"><tt>\p{InGreek}</tt></td>
1.216 + * <td headers="matches">A character in the Greek block (<a href="#ubc">block</a>)</td></tr>
1.217 + * <tr><td valign="top" headers="construct unicode"><tt>\p{Lu}</tt></td>
1.218 + * <td headers="matches">An uppercase letter (<a href="#ucc">category</a>)</td></tr>
1.219 + * <tr><td valign="top" headers="construct unicode"><tt>\p{IsAlphabetic}</tt></td>
1.220 + * <td headers="matches">An alphabetic character (<a href="#ubpc">binary property</a>)</td></tr>
1.221 + * <tr><td valign="top" headers="construct unicode"><tt>\p{Sc}</tt></td>
1.222 + * <td headers="matches">A currency symbol</td></tr>
1.223 + * <tr><td valign="top" headers="construct unicode"><tt>\P{InGreek}</tt></td>
1.224 + * <td headers="matches">Any character except one in the Greek block (negation)</td></tr>
1.225 + * <tr><td valign="top" headers="construct unicode"><tt>[\p{L}&&[^\p{Lu}]] </tt></td>
1.226 + * <td headers="matches">Any letter except an uppercase letter (subtraction)</td></tr>
1.227 + *
1.228 + * <tr><th> </th></tr>
1.229 + * <tr align="left"><th colspan="2" id="bounds">Boundary matchers</th></tr>
1.230 + *
1.231 + * <tr><td valign="top" headers="construct bounds"><tt>^</tt></td>
1.232 + * <td headers="matches">The beginning of a line</td></tr>
1.233 + * <tr><td valign="top" headers="construct bounds"><tt>$</tt></td>
1.234 + * <td headers="matches">The end of a line</td></tr>
1.235 + * <tr><td valign="top" headers="construct bounds"><tt>\b</tt></td>
1.236 + * <td headers="matches">A word boundary</td></tr>
1.237 + * <tr><td valign="top" headers="construct bounds"><tt>\B</tt></td>
1.238 + * <td headers="matches">A non-word boundary</td></tr>
1.239 + * <tr><td valign="top" headers="construct bounds"><tt>\A</tt></td>
1.240 + * <td headers="matches">The beginning of the input</td></tr>
1.241 + * <tr><td valign="top" headers="construct bounds"><tt>\G</tt></td>
1.242 + * <td headers="matches">The end of the previous match</td></tr>
1.243 + * <tr><td valign="top" headers="construct bounds"><tt>\Z</tt></td>
1.244 + * <td headers="matches">The end of the input but for the final
1.245 + * <a href="#lt">terminator</a>, if any</td></tr>
1.246 + * <tr><td valign="top" headers="construct bounds"><tt>\z</tt></td>
1.247 + * <td headers="matches">The end of the input</td></tr>
1.248 + *
1.249 + * <tr><th> </th></tr>
1.250 + * <tr align="left"><th colspan="2" id="greedy">Greedy quantifiers</th></tr>
1.251 + *
1.252 + * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>?</tt></td>
1.253 + * <td headers="matches"><i>X</i>, once or not at all</td></tr>
1.254 + * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>*</tt></td>
1.255 + * <td headers="matches"><i>X</i>, zero or more times</td></tr>
1.256 + * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>+</tt></td>
1.257 + * <td headers="matches"><i>X</i>, one or more times</td></tr>
1.258 + * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>}</tt></td>
1.259 + * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
1.260 + * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,}</tt></td>
1.261 + * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
1.262 + * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}</tt></td>
1.263 + * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
1.264 + *
1.265 + * <tr><th> </th></tr>
1.266 + * <tr align="left"><th colspan="2" id="reluc">Reluctant quantifiers</th></tr>
1.267 + *
1.268 + * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>??</tt></td>
1.269 + * <td headers="matches"><i>X</i>, once or not at all</td></tr>
1.270 + * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>*?</tt></td>
1.271 + * <td headers="matches"><i>X</i>, zero or more times</td></tr>
1.272 + * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>+?</tt></td>
1.273 + * <td headers="matches"><i>X</i>, one or more times</td></tr>
1.274 + * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>}?</tt></td>
1.275 + * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
1.276 + * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,}?</tt></td>
1.277 + * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
1.278 + * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}?</tt></td>
1.279 + * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
1.280 + *
1.281 + * <tr><th> </th></tr>
1.282 + * <tr align="left"><th colspan="2" id="poss">Possessive quantifiers</th></tr>
1.283 + *
1.284 + * <tr><td valign="top" headers="construct poss"><i>X</i><tt>?+</tt></td>
1.285 + * <td headers="matches"><i>X</i>, once or not at all</td></tr>
1.286 + * <tr><td valign="top" headers="construct poss"><i>X</i><tt>*+</tt></td>
1.287 + * <td headers="matches"><i>X</i>, zero or more times</td></tr>
1.288 + * <tr><td valign="top" headers="construct poss"><i>X</i><tt>++</tt></td>
1.289 + * <td headers="matches"><i>X</i>, one or more times</td></tr>
1.290 + * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>}+</tt></td>
1.291 + * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
1.292 + * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,}+</tt></td>
1.293 + * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
1.294 + * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}+</tt></td>
1.295 + * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
1.296 + *
1.297 + * <tr><th> </th></tr>
1.298 + * <tr align="left"><th colspan="2" id="logical">Logical operators</th></tr>
1.299 + *
1.300 + * <tr><td valign="top" headers="construct logical"><i>XY</i></td>
1.301 + * <td headers="matches"><i>X</i> followed by <i>Y</i></td></tr>
1.302 + * <tr><td valign="top" headers="construct logical"><i>X</i><tt>|</tt><i>Y</i></td>
1.303 + * <td headers="matches">Either <i>X</i> or <i>Y</i></td></tr>
1.304 + * <tr><td valign="top" headers="construct logical"><tt>(</tt><i>X</i><tt>)</tt></td>
1.305 + * <td headers="matches">X, as a <a href="#cg">capturing group</a></td></tr>
1.306 + *
1.307 + * <tr><th> </th></tr>
1.308 + * <tr align="left"><th colspan="2" id="backref">Back references</th></tr>
1.309 + *
1.310 + * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>n</i></td>
1.311 + * <td valign="bottom" headers="matches">Whatever the <i>n</i><sup>th</sup>
1.312 + * <a href="#cg">capturing group</a> matched</td></tr>
1.313 + *
1.314 + * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>k</i><<i>name</i>></td>
1.315 + * <td valign="bottom" headers="matches">Whatever the
1.316 + * <a href="#groupname">named-capturing group</a> "name" matched</td></tr>
1.317 + *
1.318 + * <tr><th> </th></tr>
1.319 + * <tr align="left"><th colspan="2" id="quot">Quotation</th></tr>
1.320 + *
1.321 + * <tr><td valign="top" headers="construct quot"><tt>\</tt></td>
1.322 + * <td headers="matches">Nothing, but quotes the following character</td></tr>
1.323 + * <tr><td valign="top" headers="construct quot"><tt>\Q</tt></td>
1.324 + * <td headers="matches">Nothing, but quotes all characters until <tt>\E</tt></td></tr>
1.325 + * <tr><td valign="top" headers="construct quot"><tt>\E</tt></td>
1.326 + * <td headers="matches">Nothing, but ends quoting started by <tt>\Q</tt></td></tr>
1.327 + * <!-- Metachars: !$()*+.<>?[\]^{|} -->
1.328 + *
1.329 + * <tr><th> </th></tr>
1.330 + * <tr align="left"><th colspan="2" id="special">Special constructs (named-capturing and non-capturing)</th></tr>
1.331 + *
1.332 + * <tr><td valign="top" headers="construct special"><tt>(?<<a href="#groupname">name</a>></tt><i>X</i><tt>)</tt></td>
1.333 + * <td headers="matches"><i>X</i>, as a named-capturing group</td></tr>
1.334 + * <tr><td valign="top" headers="construct special"><tt>(?:</tt><i>X</i><tt>)</tt></td>
1.335 + * <td headers="matches"><i>X</i>, as a non-capturing group</td></tr>
1.336 + * <tr><td valign="top" headers="construct special"><tt>(?idmsuxU-idmsuxU) </tt></td>
1.337 + * <td headers="matches">Nothing, but turns match flags <a href="#CASE_INSENSITIVE">i</a>
1.338 + * <a href="#UNIX_LINES">d</a> <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a>
1.339 + * <a href="#UNICODE_CASE">u</a> <a href="#COMMENTS">x</a> <a href="#UNICODE_CHARACTER_CLASS">U</a>
1.340 + * on - off</td></tr>
1.341 + * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux:</tt><i>X</i><tt>)</tt> </td>
1.342 + * <td headers="matches"><i>X</i>, as a <a href="#cg">non-capturing group</a> with the
1.343 + * given flags <a href="#CASE_INSENSITIVE">i</a> <a href="#UNIX_LINES">d</a>
1.344 + * <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> <a href="#UNICODE_CASE">u</a >
1.345 + * <a href="#COMMENTS">x</a> on - off</td></tr>
1.346 + * <tr><td valign="top" headers="construct special"><tt>(?=</tt><i>X</i><tt>)</tt></td>
1.347 + * <td headers="matches"><i>X</i>, via zero-width positive lookahead</td></tr>
1.348 + * <tr><td valign="top" headers="construct special"><tt>(?!</tt><i>X</i><tt>)</tt></td>
1.349 + * <td headers="matches"><i>X</i>, via zero-width negative lookahead</td></tr>
1.350 + * <tr><td valign="top" headers="construct special"><tt>(?<=</tt><i>X</i><tt>)</tt></td>
1.351 + * <td headers="matches"><i>X</i>, via zero-width positive lookbehind</td></tr>
1.352 + * <tr><td valign="top" headers="construct special"><tt>(?<!</tt><i>X</i><tt>)</tt></td>
1.353 + * <td headers="matches"><i>X</i>, via zero-width negative lookbehind</td></tr>
1.354 + * <tr><td valign="top" headers="construct special"><tt>(?></tt><i>X</i><tt>)</tt></td>
1.355 + * <td headers="matches"><i>X</i>, as an independent, non-capturing group</td></tr>
1.356 + *
1.357 + * </table>
1.358 + *
1.359 + * <hr>
1.360 + *
1.361 + *
1.362 + * <a name="bs">
1.363 + * <h4> Backslashes, escapes, and quoting </h4>
1.364 + *
1.365 + * <p> The backslash character (<tt>'\'</tt>) serves to introduce escaped
1.366 + * constructs, as defined in the table above, as well as to quote characters
1.367 + * that otherwise would be interpreted as unescaped constructs. Thus the
1.368 + * expression <tt>\\</tt> matches a single backslash and <tt>\{</tt> matches a
1.369 + * left brace.
1.370 + *
1.371 + * <p> It is an error to use a backslash prior to any alphabetic character that
1.372 + * does not denote an escaped construct; these are reserved for future
1.373 + * extensions to the regular-expression language. A backslash may be used
1.374 + * prior to a non-alphabetic character regardless of whether that character is
1.375 + * part of an unescaped construct.
1.376 + *
1.377 + * <p> Backslashes within string literals in Java source code are interpreted
1.378 + * as required by
1.379 + * <cite>The Java™ Language Specification</cite>
1.380 + * as either Unicode escapes (section 3.3) or other character escapes (section 3.10.6)
1.381 + * It is therefore necessary to double backslashes in string
1.382 + * literals that represent regular expressions to protect them from
1.383 + * interpretation by the Java bytecode compiler. The string literal
1.384 + * <tt>"\b"</tt>, for example, matches a single backspace character when
1.385 + * interpreted as a regular expression, while <tt>"\\b"</tt> matches a
1.386 + * word boundary. The string literal <tt>"\(hello\)"</tt> is illegal
1.387 + * and leads to a compile-time error; in order to match the string
1.388 + * <tt>(hello)</tt> the string literal <tt>"\\(hello\\)"</tt>
1.389 + * must be used.
1.390 + *
1.391 + * <a name="cc">
1.392 + * <h4> Character Classes </h4>
1.393 + *
1.394 + * <p> Character classes may appear within other character classes, and
1.395 + * may be composed by the union operator (implicit) and the intersection
1.396 + * operator (<tt>&&</tt>).
1.397 + * The union operator denotes a class that contains every character that is
1.398 + * in at least one of its operand classes. The intersection operator
1.399 + * denotes a class that contains every character that is in both of its
1.400 + * operand classes.
1.401 + *
1.402 + * <p> The precedence of character-class operators is as follows, from
1.403 + * highest to lowest:
1.404 + *
1.405 + * <blockquote><table border="0" cellpadding="1" cellspacing="0"
1.406 + * summary="Precedence of character class operators.">
1.407 + * <tr><th>1 </th>
1.408 + * <td>Literal escape </td>
1.409 + * <td><tt>\x</tt></td></tr>
1.410 + * <tr><th>2 </th>
1.411 + * <td>Grouping</td>
1.412 + * <td><tt>[...]</tt></td></tr>
1.413 + * <tr><th>3 </th>
1.414 + * <td>Range</td>
1.415 + * <td><tt>a-z</tt></td></tr>
1.416 + * <tr><th>4 </th>
1.417 + * <td>Union</td>
1.418 + * <td><tt>[a-e][i-u]</tt></td></tr>
1.419 + * <tr><th>5 </th>
1.420 + * <td>Intersection</td>
1.421 + * <td><tt>[a-z&&[aeiou]]</tt></td></tr>
1.422 + * </table></blockquote>
1.423 + *
1.424 + * <p> Note that a different set of metacharacters are in effect inside
1.425 + * a character class than outside a character class. For instance, the
1.426 + * regular expression <tt>.</tt> loses its special meaning inside a
1.427 + * character class, while the expression <tt>-</tt> becomes a range
1.428 + * forming metacharacter.
1.429 + *
1.430 + * <a name="lt">
1.431 + * <h4> Line terminators </h4>
1.432 + *
1.433 + * <p> A <i>line terminator</i> is a one- or two-character sequence that marks
1.434 + * the end of a line of the input character sequence. The following are
1.435 + * recognized as line terminators:
1.436 + *
1.437 + * <ul>
1.438 + *
1.439 + * <li> A newline (line feed) character (<tt>'\n'</tt>),
1.440 + *
1.441 + * <li> A carriage-return character followed immediately by a newline
1.442 + * character (<tt>"\r\n"</tt>),
1.443 + *
1.444 + * <li> A standalone carriage-return character (<tt>'\r'</tt>),
1.445 + *
1.446 + * <li> A next-line character (<tt>'\u0085'</tt>),
1.447 + *
1.448 + * <li> A line-separator character (<tt>'\u2028'</tt>), or
1.449 + *
1.450 + * <li> A paragraph-separator character (<tt>'\u2029</tt>).
1.451 + *
1.452 + * </ul>
1.453 + * <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators
1.454 + * recognized are newline characters.
1.455 + *
1.456 + * <p> The regular expression <tt>.</tt> matches any character except a line
1.457 + * terminator unless the {@link #DOTALL} flag is specified.
1.458 + *
1.459 + * <p> By default, the regular expressions <tt>^</tt> and <tt>$</tt> ignore
1.460 + * line terminators and only match at the beginning and the end, respectively,
1.461 + * of the entire input sequence. If {@link #MULTILINE} mode is activated then
1.462 + * <tt>^</tt> matches at the beginning of input and after any line terminator
1.463 + * except at the end of input. When in {@link #MULTILINE} mode <tt>$</tt>
1.464 + * matches just before a line terminator or the end of the input sequence.
1.465 + *
1.466 + * <a name="cg">
1.467 + * <h4> Groups and capturing </h4>
1.468 + *
1.469 + * <a name="gnumber">
1.470 + * <h5> Group number </h5>
1.471 + * <p> Capturing groups are numbered by counting their opening parentheses from
1.472 + * left to right. In the expression <tt>((A)(B(C)))</tt>, for example, there
1.473 + * are four such groups: </p>
1.474 + *
1.475 + * <blockquote><table cellpadding=1 cellspacing=0 summary="Capturing group numberings">
1.476 + * <tr><th>1 </th>
1.477 + * <td><tt>((A)(B(C)))</tt></td></tr>
1.478 + * <tr><th>2 </th>
1.479 + * <td><tt>(A)</tt></td></tr>
1.480 + * <tr><th>3 </th>
1.481 + * <td><tt>(B(C))</tt></td></tr>
1.482 + * <tr><th>4 </th>
1.483 + * <td><tt>(C)</tt></td></tr>
1.484 + * </table></blockquote>
1.485 + *
1.486 + * <p> Group zero always stands for the entire expression.
1.487 + *
1.488 + * <p> Capturing groups are so named because, during a match, each subsequence
1.489 + * of the input sequence that matches such a group is saved. The captured
1.490 + * subsequence may be used later in the expression, via a back reference, and
1.491 + * may also be retrieved from the matcher once the match operation is complete.
1.492 + *
1.493 + * <a name="groupname">
1.494 + * <h5> Group name </h5>
1.495 + * <p>A capturing group can also be assigned a "name", a <tt>named-capturing group</tt>,
1.496 + * and then be back-referenced later by the "name". Group names are composed of
1.497 + * the following characters. The first character must be a <tt>letter</tt>.
1.498 + *
1.499 + * <ul>
1.500 + * <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt>
1.501 + * (<tt>'\u0041'</tt> through <tt>'\u005a'</tt>),
1.502 + * <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt>
1.503 + * (<tt>'\u0061'</tt> through <tt>'\u007a'</tt>),
1.504 + * <li> The digits <tt>'0'</tt> through <tt>'9'</tt>
1.505 + * (<tt>'\u0030'</tt> through <tt>'\u0039'</tt>),
1.506 + * </ul>
1.507 + *
1.508 + * <p> A <tt>named-capturing group</tt> is still numbered as described in
1.509 + * <a href="#gnumber">Group number</a>.
1.510 + *
1.511 + * <p> The captured input associated with a group is always the subsequence
1.512 + * that the group most recently matched. If a group is evaluated a second time
1.513 + * because of quantification then its previously-captured value, if any, will
1.514 + * be retained if the second evaluation fails. Matching the string
1.515 + * <tt>"aba"</tt> against the expression <tt>(a(b)?)+</tt>, for example, leaves
1.516 + * group two set to <tt>"b"</tt>. All captured input is discarded at the
1.517 + * beginning of each match.
1.518 + *
1.519 + * <p> Groups beginning with <tt>(?</tt> are either pure, <i>non-capturing</i> groups
1.520 + * that do not capture text and do not count towards the group total, or
1.521 + * <i>named-capturing</i> group.
1.522 + *
1.523 + * <h4> Unicode support </h4>
1.524 + *
1.525 + * <p> This class is in conformance with Level 1 of <a
1.526 + * href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
1.527 + * Standard #18: Unicode Regular Expression</i></a>, plus RL2.1
1.528 + * Canonical Equivalents.
1.529 + * <p>
1.530 + * <b>Unicode escape sequences</b> such as <tt>\u2014</tt> in Java source code
1.531 + * are processed as described in section 3.3 of
1.532 + * <cite>The Java™ Language Specification</cite>.
1.533 + * Such escape sequences are also implemented directly by the regular-expression
1.534 + * parser so that Unicode escapes can be used in expressions that are read from
1.535 + * files or from the keyboard. Thus the strings <tt>"\u2014"</tt> and
1.536 + * <tt>"\\u2014"</tt>, while not equal, compile into the same pattern, which
1.537 + * matches the character with hexadecimal value <tt>0x2014</tt>.
1.538 + * <p>
1.539 + * A Unicode character can also be represented in a regular-expression by
1.540 + * using its <b>Hex notation</b>(hexadecimal code point value) directly as described in construct
1.541 + * <tt>\x{...}</tt>, for example a supplementary character U+2011F
1.542 + * can be specified as <tt>\x{2011F}</tt>, instead of two consecutive
1.543 + * Unicode escape sequences of the surrogate pair
1.544 + * <tt>\uD840</tt><tt>\uDD1F</tt>.
1.545 + * <p>
1.546 + * Unicode scripts, blocks, categories and binary properties are written with
1.547 + * the <tt>\p</tt> and <tt>\P</tt> constructs as in Perl.
1.548 + * <tt>\p{</tt><i>prop</i><tt>}</tt> matches if
1.549 + * the input has the property <i>prop</i>, while <tt>\P{</tt><i>prop</i><tt>}</tt>
1.550 + * does not match if the input has that property.
1.551 + * <p>
1.552 + * Scripts, blocks, categories and binary properties can be used both inside
1.553 + * and outside of a character class.
1.554 + * <a name="usc">
1.555 + * <p>
1.556 + * <b>Scripts</b> are specified either with the prefix {@code Is}, as in
1.557 + * {@code IsHiragana}, or by using the {@code script} keyword (or its short
1.558 + * form {@code sc})as in {@code script=Hiragana} or {@code sc=Hiragana}.
1.559 + * <p>
1.560 + * The script names supported by <code>Pattern</code> are the valid script names
1.561 + * accepted and defined by
1.562 + * {@link java.lang.Character.UnicodeScript#forName(String) UnicodeScript.forName}.
1.563 + * <a name="ubc">
1.564 + * <p>
1.565 + * <b>Blocks</b> are specified with the prefix {@code In}, as in
1.566 + * {@code InMongolian}, or by using the keyword {@code block} (or its short
1.567 + * form {@code blk}) as in {@code block=Mongolian} or {@code blk=Mongolian}.
1.568 + * <p>
1.569 + * The block names supported by <code>Pattern</code> are the valid block names
1.570 + * accepted and defined by
1.571 + * {@link java.lang.Character.UnicodeBlock#forName(String) UnicodeBlock.forName}.
1.572 + * <p>
1.573 + * <a name="ucc">
1.574 + * <b>Categories</b> may be specified with the optional prefix {@code Is}:
1.575 + * Both {@code \p{L}} and {@code \p{IsL}} denote the category of Unicode
1.576 + * letters. Same as scripts and blocks, categories can also be specified
1.577 + * by using the keyword {@code general_category} (or its short form
1.578 + * {@code gc}) as in {@code general_category=Lu} or {@code gc=Lu}.
1.579 + * <p>
1.580 + * The supported categories are those of
1.581 + * <a href="http://www.unicode.org/unicode/standard/standard.html">
1.582 + * <i>The Unicode Standard</i></a> in the version specified by the
1.583 + * {@link java.lang.Character Character} class. The category names are those
1.584 + * defined in the Standard, both normative and informative.
1.585 + * <p>
1.586 + * <a name="ubpc">
1.587 + * <b>Binary properties</b> are specified with the prefix {@code Is}, as in
1.588 + * {@code IsAlphabetic}. The supported binary properties by <code>Pattern</code>
1.589 + * are
1.590 + * <ul>
1.591 + * <li> Alphabetic
1.592 + * <li> Ideographic
1.593 + * <li> Letter
1.594 + * <li> Lowercase
1.595 + * <li> Uppercase
1.596 + * <li> Titlecase
1.597 + * <li> Punctuation
1.598 + * <Li> Control
1.599 + * <li> White_Space
1.600 + * <li> Digit
1.601 + * <li> Hex_Digit
1.602 + * <li> Noncharacter_Code_Point
1.603 + * <li> Assigned
1.604 + * </ul>
1.605 +
1.606 +
1.607 + * <p>
1.608 + * <b>Predefined Character classes</b> and <b>POSIX character classes</b> are in
1.609 + * conformance with the recommendation of <i>Annex C: Compatibility Properties</i>
1.610 + * of <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Regular Expression
1.611 + * </i></a>, when {@link #UNICODE_CHARACTER_CLASS} flag is specified.
1.612 + * <p>
1.613 + * <table border="0" cellpadding="1" cellspacing="0"
1.614 + * summary="predefined and posix character classes in Unicode mode">
1.615 + * <tr align="left">
1.616 + * <th bgcolor="#CCCCFF" align="left" id="classes">Classes</th>
1.617 + * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
1.618 + *</tr>
1.619 + * <tr><td><tt>\p{Lower}</tt></td>
1.620 + * <td>A lowercase character:<tt>\p{IsLowercase}</tt></td></tr>
1.621 + * <tr><td><tt>\p{Upper}</tt></td>
1.622 + * <td>An uppercase character:<tt>\p{IsUppercase}</tt></td></tr>
1.623 + * <tr><td><tt>\p{ASCII}</tt></td>
1.624 + * <td>All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
1.625 + * <tr><td><tt>\p{Alpha}</tt></td>
1.626 + * <td>An alphabetic character:<tt>\p{IsAlphabetic}</tt></td></tr>
1.627 + * <tr><td><tt>\p{Digit}</tt></td>
1.628 + * <td>A decimal digit character:<tt>p{IsDigit}</tt></td></tr>
1.629 + * <tr><td><tt>\p{Alnum}</tt></td>
1.630 + * <td>An alphanumeric character:<tt>[\p{IsAlphabetic}\p{IsDigit}]</tt></td></tr>
1.631 + * <tr><td><tt>\p{Punct}</tt></td>
1.632 + * <td>A punctuation character:<tt>p{IsPunctuation}</tt></td></tr>
1.633 + * <tr><td><tt>\p{Graph}</tt></td>
1.634 + * <td>A visible character: <tt>[^\p{IsWhite_Space}\p{gc=Cc}\p{gc=Cs}\p{gc=Cn}]</tt></td></tr>
1.635 + * <tr><td><tt>\p{Print}</tt></td>
1.636 + * <td>A printable character: <tt>[\p{Graph}\p{Blank}&&[^\p{Cntrl}]]</tt></td></tr>
1.637 + * <tr><td><tt>\p{Blank}</tt></td>
1.638 + * <td>A space or a tab: <tt>[\p{IsWhite_Space}&&[^\p{gc=Zl}\p{gc=Zp}\x0a\x0b\x0c\x0d\x85]]</tt></td></tr>
1.639 + * <tr><td><tt>\p{Cntrl}</tt></td>
1.640 + * <td>A control character: <tt>\p{gc=Cc}</tt></td></tr>
1.641 + * <tr><td><tt>\p{XDigit}</tt></td>
1.642 + * <td>A hexadecimal digit: <tt>[\p{gc=Nd}\p{IsHex_Digit}]</tt></td></tr>
1.643 + * <tr><td><tt>\p{Space}</tt></td>
1.644 + * <td>A whitespace character:<tt>\p{IsWhite_Space}</tt></td></tr>
1.645 + * <tr><td><tt>\d</tt></td>
1.646 + * <td>A digit: <tt>\p{IsDigit}</tt></td></tr>
1.647 + * <tr><td><tt>\D</tt></td>
1.648 + * <td>A non-digit: <tt>[^\d]</tt></td></tr>
1.649 + * <tr><td><tt>\s</tt></td>
1.650 + * <td>A whitespace character: <tt>\p{IsWhite_Space}</tt></td></tr>
1.651 + * <tr><td><tt>\S</tt></td>
1.652 + * <td>A non-whitespace character: <tt>[^\s]</tt></td></tr>
1.653 + * <tr><td><tt>\w</tt></td>
1.654 + * <td>A word character: <tt>[\p{Alpha}\p{gc=Mn}\p{gc=Me}\p{gc=Mc}\p{Digit}\p{gc=Pc}]</tt></td></tr>
1.655 + * <tr><td><tt>\W</tt></td>
1.656 + * <td>A non-word character: <tt>[^\w]</tt></td></tr>
1.657 + * </table>
1.658 + * <p>
1.659 + * <a name="jcc">
1.660 + * Categories that behave like the java.lang.Character
1.661 + * boolean is<i>methodname</i> methods (except for the deprecated ones) are
1.662 + * available through the same <tt>\p{</tt><i>prop</i><tt>}</tt> syntax where
1.663 + * the specified property has the name <tt>java<i>methodname</i></tt>.
1.664 + *
1.665 + * <h4> Comparison to Perl 5 </h4>
1.666 + *
1.667 + * <p>The <code>Pattern</code> engine performs traditional NFA-based matching
1.668 + * with ordered alternation as occurs in Perl 5.
1.669 + *
1.670 + * <p> Perl constructs not supported by this class: </p>
1.671 + *
1.672 + * <ul>
1.673 + * <li><p> Predefined character classes (Unicode character)
1.674 + * <p><tt>\h </tt>A horizontal whitespace
1.675 + * <p><tt>\H </tt>A non horizontal whitespace
1.676 + * <p><tt>\v </tt>A vertical whitespace
1.677 + * <p><tt>\V </tt>A non vertical whitespace
1.678 + * <p><tt>\R </tt>Any Unicode linebreak sequence
1.679 + * <tt>\u005cu000D\u005cu000A|[\u005cu000A\u005cu000B\u005cu000C\u005cu000D\u005cu0085\u005cu2028\u005cu2029]</tt>
1.680 + * <p><tt>\X </tt>Match Unicode
1.681 + * <a href="http://www.unicode.org/reports/tr18/#Default_Grapheme_Clusters">
1.682 + * <i>extended grapheme cluster</i></a>
1.683 + * </p></li>
1.684 + *
1.685 + * <li><p> The backreference constructs, <tt>\g{</tt><i>n</i><tt>}</tt> for
1.686 + * the <i>n</i><sup>th</sup><a href="#cg">capturing group</a> and
1.687 + * <tt>\g{</tt><i>name</i><tt>}</tt> for
1.688 + * <a href="#groupname">named-capturing group</a>.
1.689 + * </p></li>
1.690 + *
1.691 + * <li><p> The named character construct, <tt>\N{</tt><i>name</i><tt>}</tt>
1.692 + * for a Unicode character by its name.
1.693 + * </p></li>
1.694 + *
1.695 + * <li><p> The conditional constructs
1.696 + * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>)</tt> and
1.697 + * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>|</tt><i>Y</i><tt>)</tt>,
1.698 + * </p></li>
1.699 + *
1.700 + * <li><p> The embedded code constructs <tt>(?{</tt><i>code</i><tt>})</tt>
1.701 + * and <tt>(??{</tt><i>code</i><tt>})</tt>,</p></li>
1.702 + *
1.703 + * <li><p> The embedded comment syntax <tt>(?#comment)</tt>, and </p></li>
1.704 + *
1.705 + * <li><p> The preprocessing operations <tt>\l</tt> <tt>\u</tt>,
1.706 + * <tt>\L</tt>, and <tt>\U</tt>. </p></li>
1.707 + *
1.708 + * </ul>
1.709 + *
1.710 + * <p> Constructs supported by this class but not by Perl: </p>
1.711 + *
1.712 + * <ul>
1.713 + *
1.714 + * <li><p> Character-class union and intersection as described
1.715 + * <a href="#cc">above</a>.</p></li>
1.716 + *
1.717 + * </ul>
1.718 + *
1.719 + * <p> Notable differences from Perl: </p>
1.720 + *
1.721 + * <ul>
1.722 + *
1.723 + * <li><p> In Perl, <tt>\1</tt> through <tt>\9</tt> are always interpreted
1.724 + * as back references; a backslash-escaped number greater than <tt>9</tt> is
1.725 + * treated as a back reference if at least that many subexpressions exist,
1.726 + * otherwise it is interpreted, if possible, as an octal escape. In this
1.727 + * class octal escapes must always begin with a zero. In this class,
1.728 + * <tt>\1</tt> through <tt>\9</tt> are always interpreted as back
1.729 + * references, and a larger number is accepted as a back reference if at
1.730 + * least that many subexpressions exist at that point in the regular
1.731 + * expression, otherwise the parser will drop digits until the number is
1.732 + * smaller or equal to the existing number of groups or it is one digit.
1.733 + * </p></li>
1.734 + *
1.735 + * <li><p> Perl uses the <tt>g</tt> flag to request a match that resumes
1.736 + * where the last match left off. This functionality is provided implicitly
1.737 + * by the {@link Matcher} class: Repeated invocations of the {@link
1.738 + * Matcher#find find} method will resume where the last match left off,
1.739 + * unless the matcher is reset. </p></li>
1.740 + *
1.741 + * <li><p> In Perl, embedded flags at the top level of an expression affect
1.742 + * the whole expression. In this class, embedded flags always take effect
1.743 + * at the point at which they appear, whether they are at the top level or
1.744 + * within a group; in the latter case, flags are restored at the end of the
1.745 + * group just as in Perl. </p></li>
1.746 + *
1.747 + * </ul>
1.748 + *
1.749 + *
1.750 + * <p> For a more precise description of the behavior of regular expression
1.751 + * constructs, please see <a href="http://www.oreilly.com/catalog/regex3/">
1.752 + * <i>Mastering Regular Expressions, 3nd Edition</i>, Jeffrey E. F. Friedl,
1.753 + * O'Reilly and Associates, 2006.</a>
1.754 + * </p>
1.755 + *
1.756 + * @see java.lang.String#split(String, int)
1.757 + * @see java.lang.String#split(String)
1.758 + *
1.759 + * @author Mike McCloskey
1.760 + * @author Mark Reinhold
1.761 + * @author JSR-51 Expert Group
1.762 + * @since 1.4
1.763 + * @spec JSR-51
1.764 + */
1.765 +
1.766 +public final class Pattern
1.767 + implements java.io.Serializable
1.768 +{
1.769 +
1.770 + /**
1.771 + * Regular expression modifier values. Instead of being passed as
1.772 + * arguments, they can also be passed as inline modifiers.
1.773 + * For example, the following statements have the same effect.
1.774 + * <pre>
1.775 + * RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M);
1.776 + * RegExp r2 = RegExp.compile("(?im)abc", 0);
1.777 + * </pre>
1.778 + *
1.779 + * The flags are duplicated so that the familiar Perl match flag
1.780 + * names are available.
1.781 + */
1.782 +
1.783 + /**
1.784 + * Enables Unix lines mode.
1.785 + *
1.786 + * <p> In this mode, only the <tt>'\n'</tt> line terminator is recognized
1.787 + * in the behavior of <tt>.</tt>, <tt>^</tt>, and <tt>$</tt>.
1.788 + *
1.789 + * <p> Unix lines mode can also be enabled via the embedded flag
1.790 + * expression <tt>(?d)</tt>.
1.791 + */
1.792 + public static final int UNIX_LINES = 0x01;
1.793 +
1.794 + /**
1.795 + * Enables case-insensitive matching.
1.796 + *
1.797 + * <p> By default, case-insensitive matching assumes that only characters
1.798 + * in the US-ASCII charset are being matched. Unicode-aware
1.799 + * case-insensitive matching can be enabled by specifying the {@link
1.800 + * #UNICODE_CASE} flag in conjunction with this flag.
1.801 + *
1.802 + * <p> Case-insensitive matching can also be enabled via the embedded flag
1.803 + * expression <tt>(?i)</tt>.
1.804 + *
1.805 + * <p> Specifying this flag may impose a slight performance penalty. </p>
1.806 + */
1.807 + public static final int CASE_INSENSITIVE = 0x02;
1.808 +
1.809 + /**
1.810 + * Permits whitespace and comments in pattern.
1.811 + *
1.812 + * <p> In this mode, whitespace is ignored, and embedded comments starting
1.813 + * with <tt>#</tt> are ignored until the end of a line.
1.814 + *
1.815 + * <p> Comments mode can also be enabled via the embedded flag
1.816 + * expression <tt>(?x)</tt>.
1.817 + */
1.818 + public static final int COMMENTS = 0x04;
1.819 +
1.820 + /**
1.821 + * Enables multiline mode.
1.822 + *
1.823 + * <p> In multiline mode the expressions <tt>^</tt> and <tt>$</tt> match
1.824 + * just after or just before, respectively, a line terminator or the end of
1.825 + * the input sequence. By default these expressions only match at the
1.826 + * beginning and the end of the entire input sequence.
1.827 + *
1.828 + * <p> Multiline mode can also be enabled via the embedded flag
1.829 + * expression <tt>(?m)</tt>. </p>
1.830 + */
1.831 + public static final int MULTILINE = 0x08;
1.832 +
1.833 + /**
1.834 + * Enables literal parsing of the pattern.
1.835 + *
1.836 + * <p> When this flag is specified then the input string that specifies
1.837 + * the pattern is treated as a sequence of literal characters.
1.838 + * Metacharacters or escape sequences in the input sequence will be
1.839 + * given no special meaning.
1.840 + *
1.841 + * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on
1.842 + * matching when used in conjunction with this flag. The other flags
1.843 + * become superfluous.
1.844 + *
1.845 + * <p> There is no embedded flag character for enabling literal parsing.
1.846 + * @since 1.5
1.847 + */
1.848 + public static final int LITERAL = 0x10;
1.849 +
1.850 + /**
1.851 + * Enables dotall mode.
1.852 + *
1.853 + * <p> In dotall mode, the expression <tt>.</tt> matches any character,
1.854 + * including a line terminator. By default this expression does not match
1.855 + * line terminators.
1.856 + *
1.857 + * <p> Dotall mode can also be enabled via the embedded flag
1.858 + * expression <tt>(?s)</tt>. (The <tt>s</tt> is a mnemonic for
1.859 + * "single-line" mode, which is what this is called in Perl.) </p>
1.860 + */
1.861 + public static final int DOTALL = 0x20;
1.862 +
1.863 + /**
1.864 + * Enables Unicode-aware case folding.
1.865 + *
1.866 + * <p> When this flag is specified then case-insensitive matching, when
1.867 + * enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner
1.868 + * consistent with the Unicode Standard. By default, case-insensitive
1.869 + * matching assumes that only characters in the US-ASCII charset are being
1.870 + * matched.
1.871 + *
1.872 + * <p> Unicode-aware case folding can also be enabled via the embedded flag
1.873 + * expression <tt>(?u)</tt>.
1.874 + *
1.875 + * <p> Specifying this flag may impose a performance penalty. </p>
1.876 + */
1.877 + public static final int UNICODE_CASE = 0x40;
1.878 +
1.879 + /**
1.880 + * Enables canonical equivalence.
1.881 + *
1.882 + * <p> When this flag is specified then two characters will be considered
1.883 + * to match if, and only if, their full canonical decompositions match.
1.884 + * The expression <tt>"a\u030A"</tt>, for example, will match the
1.885 + * string <tt>"\u00E5"</tt> when this flag is specified. By default,
1.886 + * matching does not take canonical equivalence into account.
1.887 + *
1.888 + * <p> There is no embedded flag character for enabling canonical
1.889 + * equivalence.
1.890 + *
1.891 + * <p> Specifying this flag may impose a performance penalty. </p>
1.892 + */
1.893 + public static final int CANON_EQ = 0x80;
1.894 +
1.895 + /**
1.896 + * Enables the Unicode version of <i>Predefined character classes</i> and
1.897 + * <i>POSIX character classes</i>.
1.898 + *
1.899 + * <p> When this flag is specified then the (US-ASCII only)
1.900 + * <i>Predefined character classes</i> and <i>POSIX character classes</i>
1.901 + * are in conformance with
1.902 + * <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
1.903 + * Standard #18: Unicode Regular Expression</i></a>
1.904 + * <i>Annex C: Compatibility Properties</i>.
1.905 + * <p>
1.906 + * The UNICODE_CHARACTER_CLASS mode can also be enabled via the embedded
1.907 + * flag expression <tt>(?U)</tt>.
1.908 + * <p>
1.909 + * The flag implies UNICODE_CASE, that is, it enables Unicode-aware case
1.910 + * folding.
1.911 + * <p>
1.912 + * Specifying this flag may impose a performance penalty. </p>
1.913 + * @since 1.7
1.914 + */
1.915 + public static final int UNICODE_CHARACTER_CLASS = 0x100;
1.916 +
1.917 + /* Pattern has only two serialized components: The pattern string
1.918 + * and the flags, which are all that is needed to recompile the pattern
1.919 + * when it is deserialized.
1.920 + */
1.921 +
1.922 + /** use serialVersionUID from Merlin b59 for interoperability */
1.923 + private static final long serialVersionUID = 5073258162644648461L;
1.924 +
1.925 + /**
1.926 + * The original regular-expression pattern string.
1.927 + *
1.928 + * @serial
1.929 + */
1.930 + private String pattern;
1.931 +
1.932 + /**
1.933 + * The original pattern flags.
1.934 + *
1.935 + * @serial
1.936 + */
1.937 + private int flags;
1.938 +
1.939 + /**
1.940 + * Boolean indicating this Pattern is compiled; this is necessary in order
1.941 + * to lazily compile deserialized Patterns.
1.942 + */
1.943 + private transient volatile boolean compiled = false;
1.944 +
1.945 + /**
1.946 + * The normalized pattern string.
1.947 + */
1.948 + private transient String normalizedPattern;
1.949 +
1.950 + /**
1.951 + * The starting point of state machine for the find operation. This allows
1.952 + * a match to start anywhere in the input.
1.953 + */
1.954 + transient Node root;
1.955 +
1.956 + /**
1.957 + * The root of object tree for a match operation. The pattern is matched
1.958 + * at the beginning. This may include a find that uses BnM or a First
1.959 + * node.
1.960 + */
1.961 + transient Node matchRoot;
1.962 +
1.963 + /**
1.964 + * Temporary storage used by parsing pattern slice.
1.965 + */
1.966 + transient int[] buffer;
1.967 +
1.968 + /**
1.969 + * Map the "name" of the "named capturing group" to its group id
1.970 + * node.
1.971 + */
1.972 + transient volatile Map<String, Integer> namedGroups;
1.973 +
1.974 + /**
1.975 + * Temporary storage used while parsing group references.
1.976 + */
1.977 + transient GroupHead[] groupNodes;
1.978 +
1.979 + /**
1.980 + * Temporary null terminated code point array used by pattern compiling.
1.981 + */
1.982 + private transient int[] temp;
1.983 +
1.984 + /**
1.985 + * The number of capturing groups in this Pattern. Used by matchers to
1.986 + * allocate storage needed to perform a match.
1.987 + */
1.988 + transient int capturingGroupCount;
1.989 +
1.990 + /**
1.991 + * The local variable count used by parsing tree. Used by matchers to
1.992 + * allocate storage needed to perform a match.
1.993 + */
1.994 + transient int localCount;
1.995 +
1.996 + /**
1.997 + * Index into the pattern string that keeps track of how much has been
1.998 + * parsed.
1.999 + */
1.1000 + private transient int cursor;
1.1001 +
1.1002 + /**
1.1003 + * Holds the length of the pattern string.
1.1004 + */
1.1005 + private transient int patternLength;
1.1006 +
1.1007 + /**
1.1008 + * If the Start node might possibly match supplementary characters.
1.1009 + * It is set to true during compiling if
1.1010 + * (1) There is supplementary char in pattern, or
1.1011 + * (2) There is complement node of Category or Block
1.1012 + */
1.1013 + private transient boolean hasSupplementary;
1.1014 +
1.1015 + /**
1.1016 + * Compiles the given regular expression into a pattern. </p>
1.1017 + *
1.1018 + * @param regex
1.1019 + * The expression to be compiled
1.1020 + *
1.1021 + * @throws PatternSyntaxException
1.1022 + * If the expression's syntax is invalid
1.1023 + */
1.1024 + public static Pattern compile(String regex) {
1.1025 + return new Pattern(regex, 0);
1.1026 + }
1.1027 +
1.1028 + /**
1.1029 + * Compiles the given regular expression into a pattern with the given
1.1030 + * flags. </p>
1.1031 + *
1.1032 + * @param regex
1.1033 + * The expression to be compiled
1.1034 + *
1.1035 + * @param flags
1.1036 + * Match flags, a bit mask that may include
1.1037 + * {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL},
1.1038 + * {@link #UNICODE_CASE}, {@link #CANON_EQ}, {@link #UNIX_LINES},
1.1039 + * {@link #LITERAL}, {@link #UNICODE_CHARACTER_CLASS}
1.1040 + * and {@link #COMMENTS}
1.1041 + *
1.1042 + * @throws IllegalArgumentException
1.1043 + * If bit values other than those corresponding to the defined
1.1044 + * match flags are set in <tt>flags</tt>
1.1045 + *
1.1046 + * @throws PatternSyntaxException
1.1047 + * If the expression's syntax is invalid
1.1048 + */
1.1049 + public static Pattern compile(String regex, int flags) {
1.1050 + return new Pattern(regex, flags);
1.1051 + }
1.1052 +
1.1053 + /**
1.1054 + * Returns the regular expression from which this pattern was compiled.
1.1055 + * </p>
1.1056 + *
1.1057 + * @return The source of this pattern
1.1058 + */
1.1059 + public String pattern() {
1.1060 + return pattern;
1.1061 + }
1.1062 +
1.1063 + /**
1.1064 + * <p>Returns the string representation of this pattern. This
1.1065 + * is the regular expression from which this pattern was
1.1066 + * compiled.</p>
1.1067 + *
1.1068 + * @return The string representation of this pattern
1.1069 + * @since 1.5
1.1070 + */
1.1071 + public String toString() {
1.1072 + return pattern;
1.1073 + }
1.1074 +
1.1075 + /**
1.1076 + * Creates a matcher that will match the given input against this pattern.
1.1077 + * </p>
1.1078 + *
1.1079 + * @param input
1.1080 + * The character sequence to be matched
1.1081 + *
1.1082 + * @return A new matcher for this pattern
1.1083 + */
1.1084 + public Matcher matcher(CharSequence input) {
1.1085 + if (!compiled) {
1.1086 + synchronized(this) {
1.1087 + if (!compiled)
1.1088 + compile();
1.1089 + }
1.1090 + }
1.1091 + Matcher m = new Matcher(this, input);
1.1092 + return m;
1.1093 + }
1.1094 +
1.1095 + /**
1.1096 + * Returns this pattern's match flags. </p>
1.1097 + *
1.1098 + * @return The match flags specified when this pattern was compiled
1.1099 + */
1.1100 + public int flags() {
1.1101 + return flags;
1.1102 + }
1.1103 +
1.1104 + /**
1.1105 + * Compiles the given regular expression and attempts to match the given
1.1106 + * input against it.
1.1107 + *
1.1108 + * <p> An invocation of this convenience method of the form
1.1109 + *
1.1110 + * <blockquote><pre>
1.1111 + * Pattern.matches(regex, input);</pre></blockquote>
1.1112 + *
1.1113 + * behaves in exactly the same way as the expression
1.1114 + *
1.1115 + * <blockquote><pre>
1.1116 + * Pattern.compile(regex).matcher(input).matches()</pre></blockquote>
1.1117 + *
1.1118 + * <p> If a pattern is to be used multiple times, compiling it once and reusing
1.1119 + * it will be more efficient than invoking this method each time. </p>
1.1120 + *
1.1121 + * @param regex
1.1122 + * The expression to be compiled
1.1123 + *
1.1124 + * @param input
1.1125 + * The character sequence to be matched
1.1126 + *
1.1127 + * @throws PatternSyntaxException
1.1128 + * If the expression's syntax is invalid
1.1129 + */
1.1130 + public static boolean matches(String regex, CharSequence input) {
1.1131 + Pattern p = Pattern.compile(regex);
1.1132 + Matcher m = p.matcher(input);
1.1133 + return m.matches();
1.1134 + }
1.1135 +
1.1136 + /**
1.1137 + * Splits the given input sequence around matches of this pattern.
1.1138 + *
1.1139 + * <p> The array returned by this method contains each substring of the
1.1140 + * input sequence that is terminated by another subsequence that matches
1.1141 + * this pattern or is terminated by the end of the input sequence. The
1.1142 + * substrings in the array are in the order in which they occur in the
1.1143 + * input. If this pattern does not match any subsequence of the input then
1.1144 + * the resulting array has just one element, namely the input sequence in
1.1145 + * string form.
1.1146 + *
1.1147 + * <p> The <tt>limit</tt> parameter controls the number of times the
1.1148 + * pattern is applied and therefore affects the length of the resulting
1.1149 + * array. If the limit <i>n</i> is greater than zero then the pattern
1.1150 + * will be applied at most <i>n</i> - 1 times, the array's
1.1151 + * length will be no greater than <i>n</i>, and the array's last entry
1.1152 + * will contain all input beyond the last matched delimiter. If <i>n</i>
1.1153 + * is non-positive then the pattern will be applied as many times as
1.1154 + * possible and the array can have any length. If <i>n</i> is zero then
1.1155 + * the pattern will be applied as many times as possible, the array can
1.1156 + * have any length, and trailing empty strings will be discarded.
1.1157 + *
1.1158 + * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
1.1159 + * results with these parameters:
1.1160 + *
1.1161 + * <blockquote><table cellpadding=1 cellspacing=0
1.1162 + * summary="Split examples showing regex, limit, and result">
1.1163 + * <tr><th><P align="left"><i>Regex </i></th>
1.1164 + * <th><P align="left"><i>Limit </i></th>
1.1165 + * <th><P align="left"><i>Result </i></th></tr>
1.1166 + * <tr><td align=center>:</td>
1.1167 + * <td align=center>2</td>
1.1168 + * <td><tt>{ "boo", "and:foo" }</tt></td></tr>
1.1169 + * <tr><td align=center>:</td>
1.1170 + * <td align=center>5</td>
1.1171 + * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
1.1172 + * <tr><td align=center>:</td>
1.1173 + * <td align=center>-2</td>
1.1174 + * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
1.1175 + * <tr><td align=center>o</td>
1.1176 + * <td align=center>5</td>
1.1177 + * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
1.1178 + * <tr><td align=center>o</td>
1.1179 + * <td align=center>-2</td>
1.1180 + * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
1.1181 + * <tr><td align=center>o</td>
1.1182 + * <td align=center>0</td>
1.1183 + * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
1.1184 + * </table></blockquote>
1.1185 + *
1.1186 + *
1.1187 + * @param input
1.1188 + * The character sequence to be split
1.1189 + *
1.1190 + * @param limit
1.1191 + * The result threshold, as described above
1.1192 + *
1.1193 + * @return The array of strings computed by splitting the input
1.1194 + * around matches of this pattern
1.1195 + */
1.1196 + public String[] split(CharSequence input, int limit) {
1.1197 + int index = 0;
1.1198 + boolean matchLimited = limit > 0;
1.1199 + ArrayList<String> matchList = new ArrayList<>();
1.1200 + Matcher m = matcher(input);
1.1201 +
1.1202 + // Add segments before each match found
1.1203 + while(m.find()) {
1.1204 + if (!matchLimited || matchList.size() < limit - 1) {
1.1205 + String match = input.subSequence(index, m.start()).toString();
1.1206 + matchList.add(match);
1.1207 + index = m.end();
1.1208 + } else if (matchList.size() == limit - 1) { // last one
1.1209 + String match = input.subSequence(index,
1.1210 + input.length()).toString();
1.1211 + matchList.add(match);
1.1212 + index = m.end();
1.1213 + }
1.1214 + }
1.1215 +
1.1216 + // If no match was found, return this
1.1217 + if (index == 0)
1.1218 + return new String[] {input.toString()};
1.1219 +
1.1220 + // Add remaining segment
1.1221 + if (!matchLimited || matchList.size() < limit)
1.1222 + matchList.add(input.subSequence(index, input.length()).toString());
1.1223 +
1.1224 + // Construct result
1.1225 + int resultSize = matchList.size();
1.1226 + if (limit == 0)
1.1227 + while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
1.1228 + resultSize--;
1.1229 + String[] result = new String[resultSize];
1.1230 + return matchList.subList(0, resultSize).toArray(result);
1.1231 + }
1.1232 +
1.1233 + /**
1.1234 + * Splits the given input sequence around matches of this pattern.
1.1235 + *
1.1236 + * <p> This method works as if by invoking the two-argument {@link
1.1237 + * #split(java.lang.CharSequence, int) split} method with the given input
1.1238 + * sequence and a limit argument of zero. Trailing empty strings are
1.1239 + * therefore not included in the resulting array. </p>
1.1240 + *
1.1241 + * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
1.1242 + * results with these expressions:
1.1243 + *
1.1244 + * <blockquote><table cellpadding=1 cellspacing=0
1.1245 + * summary="Split examples showing regex and result">
1.1246 + * <tr><th><P align="left"><i>Regex </i></th>
1.1247 + * <th><P align="left"><i>Result</i></th></tr>
1.1248 + * <tr><td align=center>:</td>
1.1249 + * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
1.1250 + * <tr><td align=center>o</td>
1.1251 + * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
1.1252 + * </table></blockquote>
1.1253 + *
1.1254 + *
1.1255 + * @param input
1.1256 + * The character sequence to be split
1.1257 + *
1.1258 + * @return The array of strings computed by splitting the input
1.1259 + * around matches of this pattern
1.1260 + */
1.1261 + public String[] split(CharSequence input) {
1.1262 + return split(input, 0);
1.1263 + }
1.1264 +
1.1265 + /**
1.1266 + * Returns a literal pattern <code>String</code> for the specified
1.1267 + * <code>String</code>.
1.1268 + *
1.1269 + * <p>This method produces a <code>String</code> that can be used to
1.1270 + * create a <code>Pattern</code> that would match the string
1.1271 + * <code>s</code> as if it were a literal pattern.</p> Metacharacters
1.1272 + * or escape sequences in the input sequence will be given no special
1.1273 + * meaning.
1.1274 + *
1.1275 + * @param s The string to be literalized
1.1276 + * @return A literal string replacement
1.1277 + * @since 1.5
1.1278 + */
1.1279 + public static String quote(String s) {
1.1280 + int slashEIndex = s.indexOf("\\E");
1.1281 + if (slashEIndex == -1)
1.1282 + return "\\Q" + s + "\\E";
1.1283 +
1.1284 + StringBuilder sb = new StringBuilder(s.length() * 2);
1.1285 + sb.append("\\Q");
1.1286 + slashEIndex = 0;
1.1287 + int current = 0;
1.1288 + while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
1.1289 + sb.append(s.substring(current, slashEIndex));
1.1290 + current = slashEIndex + 2;
1.1291 + sb.append("\\E\\\\E\\Q");
1.1292 + }
1.1293 + sb.append(s.substring(current, s.length()));
1.1294 + sb.append("\\E");
1.1295 + return sb.toString();
1.1296 + }
1.1297 +
1.1298 + /**
1.1299 + * Recompile the Pattern instance from a stream. The original pattern
1.1300 + * string is read in and the object tree is recompiled from it.
1.1301 + */
1.1302 + private void readObject(java.io.ObjectInputStream s)
1.1303 + throws java.io.IOException, ClassNotFoundException {
1.1304 +
1.1305 + // Read in all fields
1.1306 + s.defaultReadObject();
1.1307 +
1.1308 + // Initialize counts
1.1309 + capturingGroupCount = 1;
1.1310 + localCount = 0;
1.1311 +
1.1312 + // if length > 0, the Pattern is lazily compiled
1.1313 + compiled = false;
1.1314 + if (pattern.length() == 0) {
1.1315 + root = new Start(lastAccept);
1.1316 + matchRoot = lastAccept;
1.1317 + compiled = true;
1.1318 + }
1.1319 + }
1.1320 +
1.1321 + /**
1.1322 + * This private constructor is used to create all Patterns. The pattern
1.1323 + * string and match flags are all that is needed to completely describe
1.1324 + * a Pattern. An empty pattern string results in an object tree with
1.1325 + * only a Start node and a LastNode node.
1.1326 + */
1.1327 + private Pattern(String p, int f) {
1.1328 + pattern = p;
1.1329 + flags = f;
1.1330 +
1.1331 + // to use UNICODE_CASE if UNICODE_CHARACTER_CLASS present
1.1332 + if ((flags & UNICODE_CHARACTER_CLASS) != 0)
1.1333 + flags |= UNICODE_CASE;
1.1334 +
1.1335 + // Reset group index count
1.1336 + capturingGroupCount = 1;
1.1337 + localCount = 0;
1.1338 +
1.1339 + if (pattern.length() > 0) {
1.1340 + compile();
1.1341 + } else {
1.1342 + root = new Start(lastAccept);
1.1343 + matchRoot = lastAccept;
1.1344 + }
1.1345 + }
1.1346 +
1.1347 + /**
1.1348 + * The pattern is converted to normalizedD form and then a pure group
1.1349 + * is constructed to match canonical equivalences of the characters.
1.1350 + */
1.1351 + private void normalize() {
1.1352 + boolean inCharClass = false;
1.1353 + int lastCodePoint = -1;
1.1354 +
1.1355 + // Convert pattern into normalizedD form
1.1356 + normalizedPattern = Normalizer.normalize(pattern, Normalizer.Form.NFD);
1.1357 + patternLength = normalizedPattern.length();
1.1358 +
1.1359 + // Modify pattern to match canonical equivalences
1.1360 + StringBuilder newPattern = new StringBuilder(patternLength);
1.1361 + for(int i=0; i<patternLength; ) {
1.1362 + int c = normalizedPattern.codePointAt(i);
1.1363 + StringBuilder sequenceBuffer;
1.1364 + if ((Character.getType(c) == Character.NON_SPACING_MARK)
1.1365 + && (lastCodePoint != -1)) {
1.1366 + sequenceBuffer = new StringBuilder();
1.1367 + sequenceBuffer.appendCodePoint(lastCodePoint);
1.1368 + sequenceBuffer.appendCodePoint(c);
1.1369 + while(Character.getType(c) == Character.NON_SPACING_MARK) {
1.1370 + i += Character.charCount(c);
1.1371 + if (i >= patternLength)
1.1372 + break;
1.1373 + c = normalizedPattern.codePointAt(i);
1.1374 + sequenceBuffer.appendCodePoint(c);
1.1375 + }
1.1376 + String ea = produceEquivalentAlternation(
1.1377 + sequenceBuffer.toString());
1.1378 + newPattern.setLength(newPattern.length()-Character.charCount(lastCodePoint));
1.1379 + newPattern.append("(?:").append(ea).append(")");
1.1380 + } else if (c == '[' && lastCodePoint != '\\') {
1.1381 + i = normalizeCharClass(newPattern, i);
1.1382 + } else {
1.1383 + newPattern.appendCodePoint(c);
1.1384 + }
1.1385 + lastCodePoint = c;
1.1386 + i += Character.charCount(c);
1.1387 + }
1.1388 + normalizedPattern = newPattern.toString();
1.1389 + }
1.1390 +
1.1391 + /**
1.1392 + * Complete the character class being parsed and add a set
1.1393 + * of alternations to it that will match the canonical equivalences
1.1394 + * of the characters within the class.
1.1395 + */
1.1396 + private int normalizeCharClass(StringBuilder newPattern, int i) {
1.1397 + StringBuilder charClass = new StringBuilder();
1.1398 + StringBuilder eq = null;
1.1399 + int lastCodePoint = -1;
1.1400 + String result;
1.1401 +
1.1402 + i++;
1.1403 + charClass.append("[");
1.1404 + while(true) {
1.1405 + int c = normalizedPattern.codePointAt(i);
1.1406 + StringBuilder sequenceBuffer;
1.1407 +
1.1408 + if (c == ']' && lastCodePoint != '\\') {
1.1409 + charClass.append((char)c);
1.1410 + break;
1.1411 + } else if (Character.getType(c) == Character.NON_SPACING_MARK) {
1.1412 + sequenceBuffer = new StringBuilder();
1.1413 + sequenceBuffer.appendCodePoint(lastCodePoint);
1.1414 + while(Character.getType(c) == Character.NON_SPACING_MARK) {
1.1415 + sequenceBuffer.appendCodePoint(c);
1.1416 + i += Character.charCount(c);
1.1417 + if (i >= normalizedPattern.length())
1.1418 + break;
1.1419 + c = normalizedPattern.codePointAt(i);
1.1420 + }
1.1421 + String ea = produceEquivalentAlternation(
1.1422 + sequenceBuffer.toString());
1.1423 +
1.1424 + charClass.setLength(charClass.length()-Character.charCount(lastCodePoint));
1.1425 + if (eq == null)
1.1426 + eq = new StringBuilder();
1.1427 + eq.append('|');
1.1428 + eq.append(ea);
1.1429 + } else {
1.1430 + charClass.appendCodePoint(c);
1.1431 + i++;
1.1432 + }
1.1433 + if (i == normalizedPattern.length())
1.1434 + throw error("Unclosed character class");
1.1435 + lastCodePoint = c;
1.1436 + }
1.1437 +
1.1438 + if (eq != null) {
1.1439 + result = "(?:"+charClass.toString()+eq.toString()+")";
1.1440 + } else {
1.1441 + result = charClass.toString();
1.1442 + }
1.1443 +
1.1444 + newPattern.append(result);
1.1445 + return i;
1.1446 + }
1.1447 +
1.1448 + /**
1.1449 + * Given a specific sequence composed of a regular character and
1.1450 + * combining marks that follow it, produce the alternation that will
1.1451 + * match all canonical equivalences of that sequence.
1.1452 + */
1.1453 + private String produceEquivalentAlternation(String source) {
1.1454 + int len = countChars(source, 0, 1);
1.1455 + if (source.length() == len)
1.1456 + // source has one character.
1.1457 + return source;
1.1458 +
1.1459 + String base = source.substring(0,len);
1.1460 + String combiningMarks = source.substring(len);
1.1461 +
1.1462 + String[] perms = producePermutations(combiningMarks);
1.1463 + StringBuilder result = new StringBuilder(source);
1.1464 +
1.1465 + // Add combined permutations
1.1466 + for(int x=0; x<perms.length; x++) {
1.1467 + String next = base + perms[x];
1.1468 + if (x>0)
1.1469 + result.append("|"+next);
1.1470 + next = composeOneStep(next);
1.1471 + if (next != null)
1.1472 + result.append("|"+produceEquivalentAlternation(next));
1.1473 + }
1.1474 + return result.toString();
1.1475 + }
1.1476 +
1.1477 + /**
1.1478 + * Returns an array of strings that have all the possible
1.1479 + * permutations of the characters in the input string.
1.1480 + * This is used to get a list of all possible orderings
1.1481 + * of a set of combining marks. Note that some of the permutations
1.1482 + * are invalid because of combining class collisions, and these
1.1483 + * possibilities must be removed because they are not canonically
1.1484 + * equivalent.
1.1485 + */
1.1486 + private String[] producePermutations(String input) {
1.1487 + if (input.length() == countChars(input, 0, 1))
1.1488 + return new String[] {input};
1.1489 +
1.1490 + if (input.length() == countChars(input, 0, 2)) {
1.1491 + int c0 = Character.codePointAt(input, 0);
1.1492 + int c1 = Character.codePointAt(input, Character.charCount(c0));
1.1493 + if (getClass(c1) == getClass(c0)) {
1.1494 + return new String[] {input};
1.1495 + }
1.1496 + String[] result = new String[2];
1.1497 + result[0] = input;
1.1498 + StringBuilder sb = new StringBuilder(2);
1.1499 + sb.appendCodePoint(c1);
1.1500 + sb.appendCodePoint(c0);
1.1501 + result[1] = sb.toString();
1.1502 + return result;
1.1503 + }
1.1504 +
1.1505 + int length = 1;
1.1506 + int nCodePoints = countCodePoints(input);
1.1507 + for(int x=1; x<nCodePoints; x++)
1.1508 + length = length * (x+1);
1.1509 +
1.1510 + String[] temp = new String[length];
1.1511 +
1.1512 + int combClass[] = new int[nCodePoints];
1.1513 + for(int x=0, i=0; x<nCodePoints; x++) {
1.1514 + int c = Character.codePointAt(input, i);
1.1515 + combClass[x] = getClass(c);
1.1516 + i += Character.charCount(c);
1.1517 + }
1.1518 +
1.1519 + // For each char, take it out and add the permutations
1.1520 + // of the remaining chars
1.1521 + int index = 0;
1.1522 + int len;
1.1523 + // offset maintains the index in code units.
1.1524 +loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
1.1525 + len = countChars(input, offset, 1);
1.1526 + boolean skip = false;
1.1527 + for(int y=x-1; y>=0; y--) {
1.1528 + if (combClass[y] == combClass[x]) {
1.1529 + continue loop;
1.1530 + }
1.1531 + }
1.1532 + StringBuilder sb = new StringBuilder(input);
1.1533 + String otherChars = sb.delete(offset, offset+len).toString();
1.1534 + String[] subResult = producePermutations(otherChars);
1.1535 +
1.1536 + String prefix = input.substring(offset, offset+len);
1.1537 + for(int y=0; y<subResult.length; y++)
1.1538 + temp[index++] = prefix + subResult[y];
1.1539 + }
1.1540 + String[] result = new String[index];
1.1541 + for (int x=0; x<index; x++)
1.1542 + result[x] = temp[x];
1.1543 + return result;
1.1544 + }
1.1545 +
1.1546 + private int getClass(int c) {
1.1547 + return sun.text.Normalizer.getCombiningClass(c);
1.1548 + }
1.1549 +
1.1550 + /**
1.1551 + * Attempts to compose input by combining the first character
1.1552 + * with the first combining mark following it. Returns a String
1.1553 + * that is the composition of the leading character with its first
1.1554 + * combining mark followed by the remaining combining marks. Returns
1.1555 + * null if the first two characters cannot be further composed.
1.1556 + */
1.1557 + private String composeOneStep(String input) {
1.1558 + int len = countChars(input, 0, 2);
1.1559 + String firstTwoCharacters = input.substring(0, len);
1.1560 + String result = Normalizer.normalize(firstTwoCharacters, Normalizer.Form.NFC);
1.1561 +
1.1562 + if (result.equals(firstTwoCharacters))
1.1563 + return null;
1.1564 + else {
1.1565 + String remainder = input.substring(len);
1.1566 + return result + remainder;
1.1567 + }
1.1568 + }
1.1569 +
1.1570 + /**
1.1571 + * Preprocess any \Q...\E sequences in `temp', meta-quoting them.
1.1572 + * See the description of `quotemeta' in perlfunc(1).
1.1573 + */
1.1574 + private void RemoveQEQuoting() {
1.1575 + final int pLen = patternLength;
1.1576 + int i = 0;
1.1577 + while (i < pLen-1) {
1.1578 + if (temp[i] != '\\')
1.1579 + i += 1;
1.1580 + else if (temp[i + 1] != 'Q')
1.1581 + i += 2;
1.1582 + else
1.1583 + break;
1.1584 + }
1.1585 + if (i >= pLen - 1) // No \Q sequence found
1.1586 + return;
1.1587 + int j = i;
1.1588 + i += 2;
1.1589 + int[] newtemp = new int[j + 2*(pLen-i) + 2];
1.1590 + System.arraycopy(temp, 0, newtemp, 0, j);
1.1591 +
1.1592 + boolean inQuote = true;
1.1593 + while (i < pLen) {
1.1594 + int c = temp[i++];
1.1595 + if (! ASCII.isAscii(c) || ASCII.isAlnum(c)) {
1.1596 + newtemp[j++] = c;
1.1597 + } else if (c != '\\') {
1.1598 + if (inQuote) newtemp[j++] = '\\';
1.1599 + newtemp[j++] = c;
1.1600 + } else if (inQuote) {
1.1601 + if (temp[i] == 'E') {
1.1602 + i++;
1.1603 + inQuote = false;
1.1604 + } else {
1.1605 + newtemp[j++] = '\\';
1.1606 + newtemp[j++] = '\\';
1.1607 + }
1.1608 + } else {
1.1609 + if (temp[i] == 'Q') {
1.1610 + i++;
1.1611 + inQuote = true;
1.1612 + } else {
1.1613 + newtemp[j++] = c;
1.1614 + if (i != pLen)
1.1615 + newtemp[j++] = temp[i++];
1.1616 + }
1.1617 + }
1.1618 + }
1.1619 +
1.1620 + patternLength = j;
1.1621 + temp = Arrays.copyOf(newtemp, j + 2); // double zero termination
1.1622 + }
1.1623 +
1.1624 + /**
1.1625 + * Copies regular expression to an int array and invokes the parsing
1.1626 + * of the expression which will create the object tree.
1.1627 + */
1.1628 + private void compile() {
1.1629 + // Handle canonical equivalences
1.1630 + if (has(CANON_EQ) && !has(LITERAL)) {
1.1631 + normalize();
1.1632 + } else {
1.1633 + normalizedPattern = pattern;
1.1634 + }
1.1635 + patternLength = normalizedPattern.length();
1.1636 +
1.1637 + // Copy pattern to int array for convenience
1.1638 + // Use double zero to terminate pattern
1.1639 + temp = new int[patternLength + 2];
1.1640 +
1.1641 + hasSupplementary = false;
1.1642 + int c, count = 0;
1.1643 + // Convert all chars into code points
1.1644 + for (int x = 0; x < patternLength; x += Character.charCount(c)) {
1.1645 + c = normalizedPattern.codePointAt(x);
1.1646 + if (isSupplementary(c)) {
1.1647 + hasSupplementary = true;
1.1648 + }
1.1649 + temp[count++] = c;
1.1650 + }
1.1651 +
1.1652 + patternLength = count; // patternLength now in code points
1.1653 +
1.1654 + if (! has(LITERAL))
1.1655 + RemoveQEQuoting();
1.1656 +
1.1657 + // Allocate all temporary objects here.
1.1658 + buffer = new int[32];
1.1659 + groupNodes = new GroupHead[10];
1.1660 + namedGroups = null;
1.1661 +
1.1662 + if (has(LITERAL)) {
1.1663 + // Literal pattern handling
1.1664 + matchRoot = newSlice(temp, patternLength, hasSupplementary);
1.1665 + matchRoot.next = lastAccept;
1.1666 + } else {
1.1667 + // Start recursive descent parsing
1.1668 + matchRoot = expr(lastAccept);
1.1669 + // Check extra pattern characters
1.1670 + if (patternLength != cursor) {
1.1671 + if (peek() == ')') {
1.1672 + throw error("Unmatched closing ')'");
1.1673 + } else {
1.1674 + throw error("Unexpected internal error");
1.1675 + }
1.1676 + }
1.1677 + }
1.1678 +
1.1679 + // Peephole optimization
1.1680 + if (matchRoot instanceof Slice) {
1.1681 + root = BnM.optimize(matchRoot);
1.1682 + if (root == matchRoot) {
1.1683 + root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
1.1684 + }
1.1685 + } else if (matchRoot instanceof Begin || matchRoot instanceof First) {
1.1686 + root = matchRoot;
1.1687 + } else {
1.1688 + root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
1.1689 + }
1.1690 +
1.1691 + // Release temporary storage
1.1692 + temp = null;
1.1693 + buffer = null;
1.1694 + groupNodes = null;
1.1695 + patternLength = 0;
1.1696 + compiled = true;
1.1697 + }
1.1698 +
1.1699 + Map<String, Integer> namedGroups() {
1.1700 + if (namedGroups == null)
1.1701 + namedGroups = new HashMap<>(2);
1.1702 + return namedGroups;
1.1703 + }
1.1704 +
1.1705 + /**
1.1706 + * Used to print out a subtree of the Pattern to help with debugging.
1.1707 + */
1.1708 + private static void printObjectTree(Node node) {
1.1709 + while(node != null) {
1.1710 + if (node instanceof Prolog) {
1.1711 + System.out.println(node);
1.1712 + printObjectTree(((Prolog)node).loop);
1.1713 + System.out.println("**** end contents prolog loop");
1.1714 + } else if (node instanceof Loop) {
1.1715 + System.out.println(node);
1.1716 + printObjectTree(((Loop)node).body);
1.1717 + System.out.println("**** end contents Loop body");
1.1718 + } else if (node instanceof Curly) {
1.1719 + System.out.println(node);
1.1720 + printObjectTree(((Curly)node).atom);
1.1721 + System.out.println("**** end contents Curly body");
1.1722 + } else if (node instanceof GroupCurly) {
1.1723 + System.out.println(node);
1.1724 + printObjectTree(((GroupCurly)node).atom);
1.1725 + System.out.println("**** end contents GroupCurly body");
1.1726 + } else if (node instanceof GroupTail) {
1.1727 + System.out.println(node);
1.1728 + System.out.println("Tail next is "+node.next);
1.1729 + return;
1.1730 + } else {
1.1731 + System.out.println(node);
1.1732 + }
1.1733 + node = node.next;
1.1734 + if (node != null)
1.1735 + System.out.println("->next:");
1.1736 + if (node == Pattern.accept) {
1.1737 + System.out.println("Accept Node");
1.1738 + node = null;
1.1739 + }
1.1740 + }
1.1741 + }
1.1742 +
1.1743 + /**
1.1744 + * Used to accumulate information about a subtree of the object graph
1.1745 + * so that optimizations can be applied to the subtree.
1.1746 + */
1.1747 + static final class TreeInfo {
1.1748 + int minLength;
1.1749 + int maxLength;
1.1750 + boolean maxValid;
1.1751 + boolean deterministic;
1.1752 +
1.1753 + TreeInfo() {
1.1754 + reset();
1.1755 + }
1.1756 + void reset() {
1.1757 + minLength = 0;
1.1758 + maxLength = 0;
1.1759 + maxValid = true;
1.1760 + deterministic = true;
1.1761 + }
1.1762 + }
1.1763 +
1.1764 + /*
1.1765 + * The following private methods are mainly used to improve the
1.1766 + * readability of the code. In order to let the Java compiler easily
1.1767 + * inline them, we should not put many assertions or error checks in them.
1.1768 + */
1.1769 +
1.1770 + /**
1.1771 + * Indicates whether a particular flag is set or not.
1.1772 + */
1.1773 + private boolean has(int f) {
1.1774 + return (flags & f) != 0;
1.1775 + }
1.1776 +
1.1777 + /**
1.1778 + * Match next character, signal error if failed.
1.1779 + */
1.1780 + private void accept(int ch, String s) {
1.1781 + int testChar = temp[cursor++];
1.1782 + if (has(COMMENTS))
1.1783 + testChar = parsePastWhitespace(testChar);
1.1784 + if (ch != testChar) {
1.1785 + throw error(s);
1.1786 + }
1.1787 + }
1.1788 +
1.1789 + /**
1.1790 + * Mark the end of pattern with a specific character.
1.1791 + */
1.1792 + private void mark(int c) {
1.1793 + temp[patternLength] = c;
1.1794 + }
1.1795 +
1.1796 + /**
1.1797 + * Peek the next character, and do not advance the cursor.
1.1798 + */
1.1799 + private int peek() {
1.1800 + int ch = temp[cursor];
1.1801 + if (has(COMMENTS))
1.1802 + ch = peekPastWhitespace(ch);
1.1803 + return ch;
1.1804 + }
1.1805 +
1.1806 + /**
1.1807 + * Read the next character, and advance the cursor by one.
1.1808 + */
1.1809 + private int read() {
1.1810 + int ch = temp[cursor++];
1.1811 + if (has(COMMENTS))
1.1812 + ch = parsePastWhitespace(ch);
1.1813 + return ch;
1.1814 + }
1.1815 +
1.1816 + /**
1.1817 + * Read the next character, and advance the cursor by one,
1.1818 + * ignoring the COMMENTS setting
1.1819 + */
1.1820 + private int readEscaped() {
1.1821 + int ch = temp[cursor++];
1.1822 + return ch;
1.1823 + }
1.1824 +
1.1825 + /**
1.1826 + * Advance the cursor by one, and peek the next character.
1.1827 + */
1.1828 + private int next() {
1.1829 + int ch = temp[++cursor];
1.1830 + if (has(COMMENTS))
1.1831 + ch = peekPastWhitespace(ch);
1.1832 + return ch;
1.1833 + }
1.1834 +
1.1835 + /**
1.1836 + * Advance the cursor by one, and peek the next character,
1.1837 + * ignoring the COMMENTS setting
1.1838 + */
1.1839 + private int nextEscaped() {
1.1840 + int ch = temp[++cursor];
1.1841 + return ch;
1.1842 + }
1.1843 +
1.1844 + /**
1.1845 + * If in xmode peek past whitespace and comments.
1.1846 + */
1.1847 + private int peekPastWhitespace(int ch) {
1.1848 + while (ASCII.isSpace(ch) || ch == '#') {
1.1849 + while (ASCII.isSpace(ch))
1.1850 + ch = temp[++cursor];
1.1851 + if (ch == '#') {
1.1852 + ch = peekPastLine();
1.1853 + }
1.1854 + }
1.1855 + return ch;
1.1856 + }
1.1857 +
1.1858 + /**
1.1859 + * If in xmode parse past whitespace and comments.
1.1860 + */
1.1861 + private int parsePastWhitespace(int ch) {
1.1862 + while (ASCII.isSpace(ch) || ch == '#') {
1.1863 + while (ASCII.isSpace(ch))
1.1864 + ch = temp[cursor++];
1.1865 + if (ch == '#')
1.1866 + ch = parsePastLine();
1.1867 + }
1.1868 + return ch;
1.1869 + }
1.1870 +
1.1871 + /**
1.1872 + * xmode parse past comment to end of line.
1.1873 + */
1.1874 + private int parsePastLine() {
1.1875 + int ch = temp[cursor++];
1.1876 + while (ch != 0 && !isLineSeparator(ch))
1.1877 + ch = temp[cursor++];
1.1878 + return ch;
1.1879 + }
1.1880 +
1.1881 + /**
1.1882 + * xmode peek past comment to end of line.
1.1883 + */
1.1884 + private int peekPastLine() {
1.1885 + int ch = temp[++cursor];
1.1886 + while (ch != 0 && !isLineSeparator(ch))
1.1887 + ch = temp[++cursor];
1.1888 + return ch;
1.1889 + }
1.1890 +
1.1891 + /**
1.1892 + * Determines if character is a line separator in the current mode
1.1893 + */
1.1894 + private boolean isLineSeparator(int ch) {
1.1895 + if (has(UNIX_LINES)) {
1.1896 + return ch == '\n';
1.1897 + } else {
1.1898 + return (ch == '\n' ||
1.1899 + ch == '\r' ||
1.1900 + (ch|1) == '\u2029' ||
1.1901 + ch == '\u0085');
1.1902 + }
1.1903 + }
1.1904 +
1.1905 + /**
1.1906 + * Read the character after the next one, and advance the cursor by two.
1.1907 + */
1.1908 + private int skip() {
1.1909 + int i = cursor;
1.1910 + int ch = temp[i+1];
1.1911 + cursor = i + 2;
1.1912 + return ch;
1.1913 + }
1.1914 +
1.1915 + /**
1.1916 + * Unread one next character, and retreat cursor by one.
1.1917 + */
1.1918 + private void unread() {
1.1919 + cursor--;
1.1920 + }
1.1921 +
1.1922 + /**
1.1923 + * Internal method used for handling all syntax errors. The pattern is
1.1924 + * displayed with a pointer to aid in locating the syntax error.
1.1925 + */
1.1926 + private PatternSyntaxException error(String s) {
1.1927 + return new PatternSyntaxException(s, normalizedPattern, cursor - 1);
1.1928 + }
1.1929 +
1.1930 + /**
1.1931 + * Determines if there is any supplementary character or unpaired
1.1932 + * surrogate in the specified range.
1.1933 + */
1.1934 + private boolean findSupplementary(int start, int end) {
1.1935 + for (int i = start; i < end; i++) {
1.1936 + if (isSupplementary(temp[i]))
1.1937 + return true;
1.1938 + }
1.1939 + return false;
1.1940 + }
1.1941 +
1.1942 + /**
1.1943 + * Determines if the specified code point is a supplementary
1.1944 + * character or unpaired surrogate.
1.1945 + */
1.1946 + private static final boolean isSupplementary(int ch) {
1.1947 + return ch >= Character.MIN_SUPPLEMENTARY_CODE_POINT ||
1.1948 + Character.isSurrogate((char)ch);
1.1949 + }
1.1950 +
1.1951 + /**
1.1952 + * The following methods handle the main parsing. They are sorted
1.1953 + * according to their precedence order, the lowest one first.
1.1954 + */
1.1955 +
1.1956 + /**
1.1957 + * The expression is parsed with branch nodes added for alternations.
1.1958 + * This may be called recursively to parse sub expressions that may
1.1959 + * contain alternations.
1.1960 + */
1.1961 + private Node expr(Node end) {
1.1962 + Node prev = null;
1.1963 + Node firstTail = null;
1.1964 + Node branchConn = null;
1.1965 +
1.1966 + for (;;) {
1.1967 + Node node = sequence(end);
1.1968 + Node nodeTail = root; //double return
1.1969 + if (prev == null) {
1.1970 + prev = node;
1.1971 + firstTail = nodeTail;
1.1972 + } else {
1.1973 + // Branch
1.1974 + if (branchConn == null) {
1.1975 + branchConn = new BranchConn();
1.1976 + branchConn.next = end;
1.1977 + }
1.1978 + if (node == end) {
1.1979 + // if the node returned from sequence() is "end"
1.1980 + // we have an empty expr, set a null atom into
1.1981 + // the branch to indicate to go "next" directly.
1.1982 + node = null;
1.1983 + } else {
1.1984 + // the "tail.next" of each atom goes to branchConn
1.1985 + nodeTail.next = branchConn;
1.1986 + }
1.1987 + if (prev instanceof Branch) {
1.1988 + ((Branch)prev).add(node);
1.1989 + } else {
1.1990 + if (prev == end) {
1.1991 + prev = null;
1.1992 + } else {
1.1993 + // replace the "end" with "branchConn" at its tail.next
1.1994 + // when put the "prev" into the branch as the first atom.
1.1995 + firstTail.next = branchConn;
1.1996 + }
1.1997 + prev = new Branch(prev, node, branchConn);
1.1998 + }
1.1999 + }
1.2000 + if (peek() != '|') {
1.2001 + return prev;
1.2002 + }
1.2003 + next();
1.2004 + }
1.2005 + }
1.2006 +
1.2007 + /**
1.2008 + * Parsing of sequences between alternations.
1.2009 + */
1.2010 + private Node sequence(Node end) {
1.2011 + Node head = null;
1.2012 + Node tail = null;
1.2013 + Node node = null;
1.2014 + LOOP:
1.2015 + for (;;) {
1.2016 + int ch = peek();
1.2017 + switch (ch) {
1.2018 + case '(':
1.2019 + // Because group handles its own closure,
1.2020 + // we need to treat it differently
1.2021 + node = group0();
1.2022 + // Check for comment or flag group
1.2023 + if (node == null)
1.2024 + continue;
1.2025 + if (head == null)
1.2026 + head = node;
1.2027 + else
1.2028 + tail.next = node;
1.2029 + // Double return: Tail was returned in root
1.2030 + tail = root;
1.2031 + continue;
1.2032 + case '[':
1.2033 + node = clazz(true);
1.2034 + break;
1.2035 + case '\\':
1.2036 + ch = nextEscaped();
1.2037 + if (ch == 'p' || ch == 'P') {
1.2038 + boolean oneLetter = true;
1.2039 + boolean comp = (ch == 'P');
1.2040 + ch = next(); // Consume { if present
1.2041 + if (ch != '{') {
1.2042 + unread();
1.2043 + } else {
1.2044 + oneLetter = false;
1.2045 + }
1.2046 + node = family(oneLetter, comp);
1.2047 + } else {
1.2048 + unread();
1.2049 + node = atom();
1.2050 + }
1.2051 + break;
1.2052 + case '^':
1.2053 + next();
1.2054 + if (has(MULTILINE)) {
1.2055 + if (has(UNIX_LINES))
1.2056 + node = new UnixCaret();
1.2057 + else
1.2058 + node = new Caret();
1.2059 + } else {
1.2060 + node = new Begin();
1.2061 + }
1.2062 + break;
1.2063 + case '$':
1.2064 + next();
1.2065 + if (has(UNIX_LINES))
1.2066 + node = new UnixDollar(has(MULTILINE));
1.2067 + else
1.2068 + node = new Dollar(has(MULTILINE));
1.2069 + break;
1.2070 + case '.':
1.2071 + next();
1.2072 + if (has(DOTALL)) {
1.2073 + node = new All();
1.2074 + } else {
1.2075 + if (has(UNIX_LINES))
1.2076 + node = new UnixDot();
1.2077 + else {
1.2078 + node = new Dot();
1.2079 + }
1.2080 + }
1.2081 + break;
1.2082 + case '|':
1.2083 + case ')':
1.2084 + break LOOP;
1.2085 + case ']': // Now interpreting dangling ] and } as literals
1.2086 + case '}':
1.2087 + node = atom();
1.2088 + break;
1.2089 + case '?':
1.2090 + case '*':
1.2091 + case '+':
1.2092 + next();
1.2093 + throw error("Dangling meta character '" + ((char)ch) + "'");
1.2094 + case 0:
1.2095 + if (cursor >= patternLength) {
1.2096 + break LOOP;
1.2097 + }
1.2098 + // Fall through
1.2099 + default:
1.2100 + node = atom();
1.2101 + break;
1.2102 + }
1.2103 +
1.2104 + node = closure(node);
1.2105 +
1.2106 + if (head == null) {
1.2107 + head = tail = node;
1.2108 + } else {
1.2109 + tail.next = node;
1.2110 + tail = node;
1.2111 + }
1.2112 + }
1.2113 + if (head == null) {
1.2114 + return end;
1.2115 + }
1.2116 + tail.next = end;
1.2117 + root = tail; //double return
1.2118 + return head;
1.2119 + }
1.2120 +
1.2121 + /**
1.2122 + * Parse and add a new Single or Slice.
1.2123 + */
1.2124 + private Node atom() {
1.2125 + int first = 0;
1.2126 + int prev = -1;
1.2127 + boolean hasSupplementary = false;
1.2128 + int ch = peek();
1.2129 + for (;;) {
1.2130 + switch (ch) {
1.2131 + case '*':
1.2132 + case '+':
1.2133 + case '?':
1.2134 + case '{':
1.2135 + if (first > 1) {
1.2136 + cursor = prev; // Unwind one character
1.2137 + first--;
1.2138 + }
1.2139 + break;
1.2140 + case '$':
1.2141 + case '.':
1.2142 + case '^':
1.2143 + case '(':
1.2144 + case '[':
1.2145 + case '|':
1.2146 + case ')':
1.2147 + break;
1.2148 + case '\\':
1.2149 + ch = nextEscaped();
1.2150 + if (ch == 'p' || ch == 'P') { // Property
1.2151 + if (first > 0) { // Slice is waiting; handle it first
1.2152 + unread();
1.2153 + break;
1.2154 + } else { // No slice; just return the family node
1.2155 + boolean comp = (ch == 'P');
1.2156 + boolean oneLetter = true;
1.2157 + ch = next(); // Consume { if present
1.2158 + if (ch != '{')
1.2159 + unread();
1.2160 + else
1.2161 + oneLetter = false;
1.2162 + return family(oneLetter, comp);
1.2163 + }
1.2164 + }
1.2165 + unread();
1.2166 + prev = cursor;
1.2167 + ch = escape(false, first == 0);
1.2168 + if (ch >= 0) {
1.2169 + append(ch, first);
1.2170 + first++;
1.2171 + if (isSupplementary(ch)) {
1.2172 + hasSupplementary = true;
1.2173 + }
1.2174 + ch = peek();
1.2175 + continue;
1.2176 + } else if (first == 0) {
1.2177 + return root;
1.2178 + }
1.2179 + // Unwind meta escape sequence
1.2180 + cursor = prev;
1.2181 + break;
1.2182 + case 0:
1.2183 + if (cursor >= patternLength) {
1.2184 + break;
1.2185 + }
1.2186 + // Fall through
1.2187 + default:
1.2188 + prev = cursor;
1.2189 + append(ch, first);
1.2190 + first++;
1.2191 + if (isSupplementary(ch)) {
1.2192 + hasSupplementary = true;
1.2193 + }
1.2194 + ch = next();
1.2195 + continue;
1.2196 + }
1.2197 + break;
1.2198 + }
1.2199 + if (first == 1) {
1.2200 + return newSingle(buffer[0]);
1.2201 + } else {
1.2202 + return newSlice(buffer, first, hasSupplementary);
1.2203 + }
1.2204 + }
1.2205 +
1.2206 + private void append(int ch, int len) {
1.2207 + if (len >= buffer.length) {
1.2208 + int[] tmp = new int[len+len];
1.2209 + System.arraycopy(buffer, 0, tmp, 0, len);
1.2210 + buffer = tmp;
1.2211 + }
1.2212 + buffer[len] = ch;
1.2213 + }
1.2214 +
1.2215 + /**
1.2216 + * Parses a backref greedily, taking as many numbers as it
1.2217 + * can. The first digit is always treated as a backref, but
1.2218 + * multi digit numbers are only treated as a backref if at
1.2219 + * least that many backrefs exist at this point in the regex.
1.2220 + */
1.2221 + private Node ref(int refNum) {
1.2222 + boolean done = false;
1.2223 + while(!done) {
1.2224 + int ch = peek();
1.2225 + switch(ch) {
1.2226 + case '0':
1.2227 + case '1':
1.2228 + case '2':
1.2229 + case '3':
1.2230 + case '4':
1.2231 + case '5':
1.2232 + case '6':
1.2233 + case '7':
1.2234 + case '8':
1.2235 + case '9':
1.2236 + int newRefNum = (refNum * 10) + (ch - '0');
1.2237 + // Add another number if it doesn't make a group
1.2238 + // that doesn't exist
1.2239 + if (capturingGroupCount - 1 < newRefNum) {
1.2240 + done = true;
1.2241 + break;
1.2242 + }
1.2243 + refNum = newRefNum;
1.2244 + read();
1.2245 + break;
1.2246 + default:
1.2247 + done = true;
1.2248 + break;
1.2249 + }
1.2250 + }
1.2251 + if (has(CASE_INSENSITIVE))
1.2252 + return new CIBackRef(refNum, has(UNICODE_CASE));
1.2253 + else
1.2254 + return new BackRef(refNum);
1.2255 + }
1.2256 +
1.2257 + /**
1.2258 + * Parses an escape sequence to determine the actual value that needs
1.2259 + * to be matched.
1.2260 + * If -1 is returned and create was true a new object was added to the tree
1.2261 + * to handle the escape sequence.
1.2262 + * If the returned value is greater than zero, it is the value that
1.2263 + * matches the escape sequence.
1.2264 + */
1.2265 + private int escape(boolean inclass, boolean create) {
1.2266 + int ch = skip();
1.2267 + switch (ch) {
1.2268 + case '0':
1.2269 + return o();
1.2270 + case '1':
1.2271 + case '2':
1.2272 + case '3':
1.2273 + case '4':
1.2274 + case '5':
1.2275 + case '6':
1.2276 + case '7':
1.2277 + case '8':
1.2278 + case '9':
1.2279 + if (inclass) break;
1.2280 + if (create) {
1.2281 + root = ref((ch - '0'));
1.2282 + }
1.2283 + return -1;
1.2284 + case 'A':
1.2285 + if (inclass) break;
1.2286 + if (create) root = new Begin();
1.2287 + return -1;
1.2288 + case 'B':
1.2289 + if (inclass) break;
1.2290 + if (create) root = new Bound(Bound.NONE, has(UNICODE_CHARACTER_CLASS));
1.2291 + return -1;
1.2292 + case 'C':
1.2293 + break;
1.2294 + case 'D':
1.2295 + if (create) root = has(UNICODE_CHARACTER_CLASS)
1.2296 + ? new Utype(UnicodeProp.DIGIT).complement()
1.2297 + : new Ctype(ASCII.DIGIT).complement();
1.2298 + return -1;
1.2299 + case 'E':
1.2300 + case 'F':
1.2301 + break;
1.2302 + case 'G':
1.2303 + if (inclass) break;
1.2304 + if (create) root = new LastMatch();
1.2305 + return -1;
1.2306 + case 'H':
1.2307 + case 'I':
1.2308 + case 'J':
1.2309 + case 'K':
1.2310 + case 'L':
1.2311 + case 'M':
1.2312 + case 'N':
1.2313 + case 'O':
1.2314 + case 'P':
1.2315 + case 'Q':
1.2316 + case 'R':
1.2317 + break;
1.2318 + case 'S':
1.2319 + if (create) root = has(UNICODE_CHARACTER_CLASS)
1.2320 + ? new Utype(UnicodeProp.WHITE_SPACE).complement()
1.2321 + : new Ctype(ASCII.SPACE).complement();
1.2322 + return -1;
1.2323 + case 'T':
1.2324 + case 'U':
1.2325 + case 'V':
1.2326 + break;
1.2327 + case 'W':
1.2328 + if (create) root = has(UNICODE_CHARACTER_CLASS)
1.2329 + ? new Utype(UnicodeProp.WORD).complement()
1.2330 + : new Ctype(ASCII.WORD).complement();
1.2331 + return -1;
1.2332 + case 'X':
1.2333 + case 'Y':
1.2334 + break;
1.2335 + case 'Z':
1.2336 + if (inclass) break;
1.2337 + if (create) {
1.2338 + if (has(UNIX_LINES))
1.2339 + root = new UnixDollar(false);
1.2340 + else
1.2341 + root = new Dollar(false);
1.2342 + }
1.2343 + return -1;
1.2344 + case 'a':
1.2345 + return '\007';
1.2346 + case 'b':
1.2347 + if (inclass) break;
1.2348 + if (create) root = new Bound(Bound.BOTH, has(UNICODE_CHARACTER_CLASS));
1.2349 + return -1;
1.2350 + case 'c':
1.2351 + return c();
1.2352 + case 'd':
1.2353 + if (create) root = has(UNICODE_CHARACTER_CLASS)
1.2354 + ? new Utype(UnicodeProp.DIGIT)
1.2355 + : new Ctype(ASCII.DIGIT);
1.2356 + return -1;
1.2357 + case 'e':
1.2358 + return '\033';
1.2359 + case 'f':
1.2360 + return '\f';
1.2361 + case 'g':
1.2362 + case 'h':
1.2363 + case 'i':
1.2364 + case 'j':
1.2365 + break;
1.2366 + case 'k':
1.2367 + if (inclass)
1.2368 + break;
1.2369 + if (read() != '<')
1.2370 + throw error("\\k is not followed by '<' for named capturing group");
1.2371 + String name = groupname(read());
1.2372 + if (!namedGroups().containsKey(name))
1.2373 + throw error("(named capturing group <"+ name+"> does not exit");
1.2374 + if (create) {
1.2375 + if (has(CASE_INSENSITIVE))
1.2376 + root = new CIBackRef(namedGroups().get(name), has(UNICODE_CASE));
1.2377 + else
1.2378 + root = new BackRef(namedGroups().get(name));
1.2379 + }
1.2380 + return -1;
1.2381 + case 'l':
1.2382 + case 'm':
1.2383 + break;
1.2384 + case 'n':
1.2385 + return '\n';
1.2386 + case 'o':
1.2387 + case 'p':
1.2388 + case 'q':
1.2389 + break;
1.2390 + case 'r':
1.2391 + return '\r';
1.2392 + case 's':
1.2393 + if (create) root = has(UNICODE_CHARACTER_CLASS)
1.2394 + ? new Utype(UnicodeProp.WHITE_SPACE)
1.2395 + : new Ctype(ASCII.SPACE);
1.2396 + return -1;
1.2397 + case 't':
1.2398 + return '\t';
1.2399 + case 'u':
1.2400 + return u();
1.2401 + case 'v':
1.2402 + return '\013';
1.2403 + case 'w':
1.2404 + if (create) root = has(UNICODE_CHARACTER_CLASS)
1.2405 + ? new Utype(UnicodeProp.WORD)
1.2406 + : new Ctype(ASCII.WORD);
1.2407 + return -1;
1.2408 + case 'x':
1.2409 + return x();
1.2410 + case 'y':
1.2411 + break;
1.2412 + case 'z':
1.2413 + if (inclass) break;
1.2414 + if (create) root = new End();
1.2415 + return -1;
1.2416 + default:
1.2417 + return ch;
1.2418 + }
1.2419 + throw error("Illegal/unsupported escape sequence");
1.2420 + }
1.2421 +
1.2422 + /**
1.2423 + * Parse a character class, and return the node that matches it.
1.2424 + *
1.2425 + * Consumes a ] on the way out if consume is true. Usually consume
1.2426 + * is true except for the case of [abc&&def] where def is a separate
1.2427 + * right hand node with "understood" brackets.
1.2428 + */
1.2429 + private CharProperty clazz(boolean consume) {
1.2430 + CharProperty prev = null;
1.2431 + CharProperty node = null;
1.2432 + BitClass bits = new BitClass();
1.2433 + boolean include = true;
1.2434 + boolean firstInClass = true;
1.2435 + int ch = next();
1.2436 + for (;;) {
1.2437 + switch (ch) {
1.2438 + case '^':
1.2439 + // Negates if first char in a class, otherwise literal
1.2440 + if (firstInClass) {
1.2441 + if (temp[cursor-1] != '[')
1.2442 + break;
1.2443 + ch = next();
1.2444 + include = !include;
1.2445 + continue;
1.2446 + } else {
1.2447 + // ^ not first in class, treat as literal
1.2448 + break;
1.2449 + }
1.2450 + case '[':
1.2451 + firstInClass = false;
1.2452 + node = clazz(true);
1.2453 + if (prev == null)
1.2454 + prev = node;
1.2455 + else
1.2456 + prev = union(prev, node);
1.2457 + ch = peek();
1.2458 + continue;
1.2459 + case '&':
1.2460 + firstInClass = false;
1.2461 + ch = next();
1.2462 + if (ch == '&') {
1.2463 + ch = next();
1.2464 + CharProperty rightNode = null;
1.2465 + while (ch != ']' && ch != '&') {
1.2466 + if (ch == '[') {
1.2467 + if (rightNode == null)
1.2468 + rightNode = clazz(true);
1.2469 + else
1.2470 + rightNode = union(rightNode, clazz(true));
1.2471 + } else { // abc&&def
1.2472 + unread();
1.2473 + rightNode = clazz(false);
1.2474 + }
1.2475 + ch = peek();
1.2476 + }
1.2477 + if (rightNode != null)
1.2478 + node = rightNode;
1.2479 + if (prev == null) {
1.2480 + if (rightNode == null)
1.2481 + throw error("Bad class syntax");
1.2482 + else
1.2483 + prev = rightNode;
1.2484 + } else {
1.2485 + prev = intersection(prev, node);
1.2486 + }
1.2487 + } else {
1.2488 + // treat as a literal &
1.2489 + unread();
1.2490 + break;
1.2491 + }
1.2492 + continue;
1.2493 + case 0:
1.2494 + firstInClass = false;
1.2495 + if (cursor >= patternLength)
1.2496 + throw error("Unclosed character class");
1.2497 + break;
1.2498 + case ']':
1.2499 + firstInClass = false;
1.2500 + if (prev != null) {
1.2501 + if (consume)
1.2502 + next();
1.2503 + return prev;
1.2504 + }
1.2505 + break;
1.2506 + default:
1.2507 + firstInClass = false;
1.2508 + break;
1.2509 + }
1.2510 + node = range(bits);
1.2511 + if (include) {
1.2512 + if (prev == null) {
1.2513 + prev = node;
1.2514 + } else {
1.2515 + if (prev != node)
1.2516 + prev = union(prev, node);
1.2517 + }
1.2518 + } else {
1.2519 + if (prev == null) {
1.2520 + prev = node.complement();
1.2521 + } else {
1.2522 + if (prev != node)
1.2523 + prev = setDifference(prev, node);
1.2524 + }
1.2525 + }
1.2526 + ch = peek();
1.2527 + }
1.2528 + }
1.2529 +
1.2530 + private CharProperty bitsOrSingle(BitClass bits, int ch) {
1.2531 + /* Bits can only handle codepoints in [u+0000-u+00ff] range.
1.2532 + Use "single" node instead of bits when dealing with unicode
1.2533 + case folding for codepoints listed below.
1.2534 + (1)Uppercase out of range: u+00ff, u+00b5
1.2535 + toUpperCase(u+00ff) -> u+0178
1.2536 + toUpperCase(u+00b5) -> u+039c
1.2537 + (2)LatinSmallLetterLongS u+17f
1.2538 + toUpperCase(u+017f) -> u+0053
1.2539 + (3)LatinSmallLetterDotlessI u+131
1.2540 + toUpperCase(u+0131) -> u+0049
1.2541 + (4)LatinCapitalLetterIWithDotAbove u+0130
1.2542 + toLowerCase(u+0130) -> u+0069
1.2543 + (5)KelvinSign u+212a
1.2544 + toLowerCase(u+212a) ==> u+006B
1.2545 + (6)AngstromSign u+212b
1.2546 + toLowerCase(u+212b) ==> u+00e5
1.2547 + */
1.2548 + int d;
1.2549 + if (ch < 256 &&
1.2550 + !(has(CASE_INSENSITIVE) && has(UNICODE_CASE) &&
1.2551 + (ch == 0xff || ch == 0xb5 ||
1.2552 + ch == 0x49 || ch == 0x69 || //I and i
1.2553 + ch == 0x53 || ch == 0x73 || //S and s
1.2554 + ch == 0x4b || ch == 0x6b || //K and k
1.2555 + ch == 0xc5 || ch == 0xe5))) //A+ring
1.2556 + return bits.add(ch, flags());
1.2557 + return newSingle(ch);
1.2558 + }
1.2559 +
1.2560 + /**
1.2561 + * Parse a single character or a character range in a character class
1.2562 + * and return its representative node.
1.2563 + */
1.2564 + private CharProperty range(BitClass bits) {
1.2565 + int ch = peek();
1.2566 + if (ch == '\\') {
1.2567 + ch = nextEscaped();
1.2568 + if (ch == 'p' || ch == 'P') { // A property
1.2569 + boolean comp = (ch == 'P');
1.2570 + boolean oneLetter = true;
1.2571 + // Consume { if present
1.2572 + ch = next();
1.2573 + if (ch != '{')
1.2574 + unread();
1.2575 + else
1.2576 + oneLetter = false;
1.2577 + return family(oneLetter, comp);
1.2578 + } else { // ordinary escape
1.2579 + unread();
1.2580 + ch = escape(true, true);
1.2581 + if (ch == -1)
1.2582 + return (CharProperty) root;
1.2583 + }
1.2584 + } else {
1.2585 + ch = single();
1.2586 + }
1.2587 + if (ch >= 0) {
1.2588 + if (peek() == '-') {
1.2589 + int endRange = temp[cursor+1];
1.2590 + if (endRange == '[') {
1.2591 + return bitsOrSingle(bits, ch);
1.2592 + }
1.2593 + if (endRange != ']') {
1.2594 + next();
1.2595 + int m = single();
1.2596 + if (m < ch)
1.2597 + throw error("Illegal character range");
1.2598 + if (has(CASE_INSENSITIVE))
1.2599 + return caseInsensitiveRangeFor(ch, m);
1.2600 + else
1.2601 + return rangeFor(ch, m);
1.2602 + }
1.2603 + }
1.2604 + return bitsOrSingle(bits, ch);
1.2605 + }
1.2606 + throw error("Unexpected character '"+((char)ch)+"'");
1.2607 + }
1.2608 +
1.2609 + private int single() {
1.2610 + int ch = peek();
1.2611 + switch (ch) {
1.2612 + case '\\':
1.2613 + return escape(true, false);
1.2614 + default:
1.2615 + next();
1.2616 + return ch;
1.2617 + }
1.2618 + }
1.2619 +
1.2620 + /**
1.2621 + * Parses a Unicode character family and returns its representative node.
1.2622 + */
1.2623 + private CharProperty family(boolean singleLetter,
1.2624 + boolean maybeComplement)
1.2625 + {
1.2626 + next();
1.2627 + String name;
1.2628 + CharProperty node = null;
1.2629 +
1.2630 + if (singleLetter) {
1.2631 + int c = temp[cursor];
1.2632 + if (!Character.isSupplementaryCodePoint(c)) {
1.2633 + name = String.valueOf((char)c);
1.2634 + } else {
1.2635 + name = new String(temp, cursor, 1);
1.2636 + }
1.2637 + read();
1.2638 + } else {
1.2639 + int i = cursor;
1.2640 + mark('}');
1.2641 + while(read() != '}') {
1.2642 + }
1.2643 + mark('\000');
1.2644 + int j = cursor;
1.2645 + if (j > patternLength)
1.2646 + throw error("Unclosed character family");
1.2647 + if (i + 1 >= j)
1.2648 + throw error("Empty character family");
1.2649 + name = new String(temp, i, j-i-1);
1.2650 + }
1.2651 +
1.2652 + int i = name.indexOf('=');
1.2653 + if (i != -1) {
1.2654 + // property construct \p{name=value}
1.2655 + String value = name.substring(i + 1);
1.2656 + name = name.substring(0, i).toLowerCase(Locale.ENGLISH);
1.2657 + if ("sc".equals(name) || "script".equals(name)) {
1.2658 + node = unicodeScriptPropertyFor(value);
1.2659 + } else if ("blk".equals(name) || "block".equals(name)) {
1.2660 + node = unicodeBlockPropertyFor(value);
1.2661 + } else if ("gc".equals(name) || "general_category".equals(name)) {
1.2662 + node = charPropertyNodeFor(value);
1.2663 + } else {
1.2664 + throw error("Unknown Unicode property {name=<" + name + ">, "
1.2665 + + "value=<" + value + ">}");
1.2666 + }
1.2667 + } else {
1.2668 + if (name.startsWith("In")) {
1.2669 + // \p{inBlockName}
1.2670 + node = unicodeBlockPropertyFor(name.substring(2));
1.2671 + } else if (name.startsWith("Is")) {
1.2672 + // \p{isGeneralCategory} and \p{isScriptName}
1.2673 + name = name.substring(2);
1.2674 + UnicodeProp uprop = UnicodeProp.forName(name);
1.2675 + if (uprop != null)
1.2676 + node = new Utype(uprop);
1.2677 + if (node == null)
1.2678 + node = CharPropertyNames.charPropertyFor(name);
1.2679 + if (node == null)
1.2680 + node = unicodeScriptPropertyFor(name);
1.2681 + } else {
1.2682 + if (has(UNICODE_CHARACTER_CLASS)) {
1.2683 + UnicodeProp uprop = UnicodeProp.forPOSIXName(name);
1.2684 + if (uprop != null)
1.2685 + node = new Utype(uprop);
1.2686 + }
1.2687 + if (node == null)
1.2688 + node = charPropertyNodeFor(name);
1.2689 + }
1.2690 + }
1.2691 + if (maybeComplement) {
1.2692 + if (node instanceof Category || node instanceof Block)
1.2693 + hasSupplementary = true;
1.2694 + node = node.complement();
1.2695 + }
1.2696 + return node;
1.2697 + }
1.2698 +
1.2699 +
1.2700 + /**
1.2701 + * Returns a CharProperty matching all characters belong to
1.2702 + * a UnicodeScript.
1.2703 + */
1.2704 + private CharProperty unicodeScriptPropertyFor(String name) {
1.2705 + final Character.UnicodeScript script;
1.2706 + try {
1.2707 + script = Character.UnicodeScript.forName(name);
1.2708 + } catch (IllegalArgumentException iae) {
1.2709 + throw error("Unknown character script name {" + name + "}");
1.2710 + }
1.2711 + return new Script(script);
1.2712 + }
1.2713 +
1.2714 + /**
1.2715 + * Returns a CharProperty matching all characters in a UnicodeBlock.
1.2716 + */
1.2717 + private CharProperty unicodeBlockPropertyFor(String name) {
1.2718 + final Character.UnicodeBlock block;
1.2719 + try {
1.2720 + block = Character.UnicodeBlock.forName(name);
1.2721 + } catch (IllegalArgumentException iae) {
1.2722 + throw error("Unknown character block name {" + name + "}");
1.2723 + }
1.2724 + return new Block(block);
1.2725 + }
1.2726 +
1.2727 + /**
1.2728 + * Returns a CharProperty matching all characters in a named property.
1.2729 + */
1.2730 + private CharProperty charPropertyNodeFor(String name) {
1.2731 + CharProperty p = CharPropertyNames.charPropertyFor(name);
1.2732 + if (p == null)
1.2733 + throw error("Unknown character property name {" + name + "}");
1.2734 + return p;
1.2735 + }
1.2736 +
1.2737 + /**
1.2738 + * Parses and returns the name of a "named capturing group", the trailing
1.2739 + * ">" is consumed after parsing.
1.2740 + */
1.2741 + private String groupname(int ch) {
1.2742 + StringBuilder sb = new StringBuilder();
1.2743 + sb.append(Character.toChars(ch));
1.2744 + while (ASCII.isLower(ch=read()) || ASCII.isUpper(ch) ||
1.2745 + ASCII.isDigit(ch)) {
1.2746 + sb.append(Character.toChars(ch));
1.2747 + }
1.2748 + if (sb.length() == 0)
1.2749 + throw error("named capturing group has 0 length name");
1.2750 + if (ch != '>')
1.2751 + throw error("named capturing group is missing trailing '>'");
1.2752 + return sb.toString();
1.2753 + }
1.2754 +
1.2755 + /**
1.2756 + * Parses a group and returns the head node of a set of nodes that process
1.2757 + * the group. Sometimes a double return system is used where the tail is
1.2758 + * returned in root.
1.2759 + */
1.2760 + private Node group0() {
1.2761 + boolean capturingGroup = false;
1.2762 + Node head = null;
1.2763 + Node tail = null;
1.2764 + int save = flags;
1.2765 + root = null;
1.2766 + int ch = next();
1.2767 + if (ch == '?') {
1.2768 + ch = skip();
1.2769 + switch (ch) {
1.2770 + case ':': // (?:xxx) pure group
1.2771 + head = createGroup(true);
1.2772 + tail = root;
1.2773 + head.next = expr(tail);
1.2774 + break;
1.2775 + case '=': // (?=xxx) and (?!xxx) lookahead
1.2776 + case '!':
1.2777 + head = createGroup(true);
1.2778 + tail = root;
1.2779 + head.next = expr(tail);
1.2780 + if (ch == '=') {
1.2781 + head = tail = new Pos(head);
1.2782 + } else {
1.2783 + head = tail = new Neg(head);
1.2784 + }
1.2785 + break;
1.2786 + case '>': // (?>xxx) independent group
1.2787 + head = createGroup(true);
1.2788 + tail = root;
1.2789 + head.next = expr(tail);
1.2790 + head = tail = new Ques(head, INDEPENDENT);
1.2791 + break;
1.2792 + case '<': // (?<xxx) look behind
1.2793 + ch = read();
1.2794 + if (ASCII.isLower(ch) || ASCII.isUpper(ch)) {
1.2795 + // named captured group
1.2796 + String name = groupname(ch);
1.2797 + if (namedGroups().containsKey(name))
1.2798 + throw error("Named capturing group <" + name
1.2799 + + "> is already defined");
1.2800 + capturingGroup = true;
1.2801 + head = createGroup(false);
1.2802 + tail = root;
1.2803 + namedGroups().put(name, capturingGroupCount-1);
1.2804 + head.next = expr(tail);
1.2805 + break;
1.2806 + }
1.2807 + int start = cursor;
1.2808 + head = createGroup(true);
1.2809 + tail = root;
1.2810 + head.next = expr(tail);
1.2811 + tail.next = lookbehindEnd;
1.2812 + TreeInfo info = new TreeInfo();
1.2813 + head.study(info);
1.2814 + if (info.maxValid == false) {
1.2815 + throw error("Look-behind group does not have "
1.2816 + + "an obvious maximum length");
1.2817 + }
1.2818 + boolean hasSupplementary = findSupplementary(start, patternLength);
1.2819 + if (ch == '=') {
1.2820 + head = tail = (hasSupplementary ?
1.2821 + new BehindS(head, info.maxLength,
1.2822 + info.minLength) :
1.2823 + new Behind(head, info.maxLength,
1.2824 + info.minLength));
1.2825 + } else if (ch == '!') {
1.2826 + head = tail = (hasSupplementary ?
1.2827 + new NotBehindS(head, info.maxLength,
1.2828 + info.minLength) :
1.2829 + new NotBehind(head, info.maxLength,
1.2830 + info.minLength));
1.2831 + } else {
1.2832 + throw error("Unknown look-behind group");
1.2833 + }
1.2834 + break;
1.2835 + case '$':
1.2836 + case '@':
1.2837 + throw error("Unknown group type");
1.2838 + default: // (?xxx:) inlined match flags
1.2839 + unread();
1.2840 + addFlag();
1.2841 + ch = read();
1.2842 + if (ch == ')') {
1.2843 + return null; // Inline modifier only
1.2844 + }
1.2845 + if (ch != ':') {
1.2846 + throw error("Unknown inline modifier");
1.2847 + }
1.2848 + head = createGroup(true);
1.2849 + tail = root;
1.2850 + head.next = expr(tail);
1.2851 + break;
1.2852 + }
1.2853 + } else { // (xxx) a regular group
1.2854 + capturingGroup = true;
1.2855 + head = createGroup(false);
1.2856 + tail = root;
1.2857 + head.next = expr(tail);
1.2858 + }
1.2859 +
1.2860 + accept(')', "Unclosed group");
1.2861 + flags = save;
1.2862 +
1.2863 + // Check for quantifiers
1.2864 + Node node = closure(head);
1.2865 + if (node == head) { // No closure
1.2866 + root = tail;
1.2867 + return node; // Dual return
1.2868 + }
1.2869 + if (head == tail) { // Zero length assertion
1.2870 + root = node;
1.2871 + return node; // Dual return
1.2872 + }
1.2873 +
1.2874 + if (node instanceof Ques) {
1.2875 + Ques ques = (Ques) node;
1.2876 + if (ques.type == POSSESSIVE) {
1.2877 + root = node;
1.2878 + return node;
1.2879 + }
1.2880 + tail.next = new BranchConn();
1.2881 + tail = tail.next;
1.2882 + if (ques.type == GREEDY) {
1.2883 + head = new Branch(head, null, tail);
1.2884 + } else { // Reluctant quantifier
1.2885 + head = new Branch(null, head, tail);
1.2886 + }
1.2887 + root = tail;
1.2888 + return head;
1.2889 + } else if (node instanceof Curly) {
1.2890 + Curly curly = (Curly) node;
1.2891 + if (curly.type == POSSESSIVE) {
1.2892 + root = node;
1.2893 + return node;
1.2894 + }
1.2895 + // Discover if the group is deterministic
1.2896 + TreeInfo info = new TreeInfo();
1.2897 + if (head.study(info)) { // Deterministic
1.2898 + GroupTail temp = (GroupTail) tail;
1.2899 + head = root = new GroupCurly(head.next, curly.cmin,
1.2900 + curly.cmax, curly.type,
1.2901 + ((GroupTail)tail).localIndex,
1.2902 + ((GroupTail)tail).groupIndex,
1.2903 + capturingGroup);
1.2904 + return head;
1.2905 + } else { // Non-deterministic
1.2906 + int temp = ((GroupHead) head).localIndex;
1.2907 + Loop loop;
1.2908 + if (curly.type == GREEDY)
1.2909 + loop = new Loop(this.localCount, temp);
1.2910 + else // Reluctant Curly
1.2911 + loop = new LazyLoop(this.localCount, temp);
1.2912 + Prolog prolog = new Prolog(loop);
1.2913 + this.localCount += 1;
1.2914 + loop.cmin = curly.cmin;
1.2915 + loop.cmax = curly.cmax;
1.2916 + loop.body = head;
1.2917 + tail.next = loop;
1.2918 + root = loop;
1.2919 + return prolog; // Dual return
1.2920 + }
1.2921 + }
1.2922 + throw error("Internal logic error");
1.2923 + }
1.2924 +
1.2925 + /**
1.2926 + * Create group head and tail nodes using double return. If the group is
1.2927 + * created with anonymous true then it is a pure group and should not
1.2928 + * affect group counting.
1.2929 + */
1.2930 + private Node createGroup(boolean anonymous) {
1.2931 + int localIndex = localCount++;
1.2932 + int groupIndex = 0;
1.2933 + if (!anonymous)
1.2934 + groupIndex = capturingGroupCount++;
1.2935 + GroupHead head = new GroupHead(localIndex);
1.2936 + root = new GroupTail(localIndex, groupIndex);
1.2937 + if (!anonymous && groupIndex < 10)
1.2938 + groupNodes[groupIndex] = head;
1.2939 + return head;
1.2940 + }
1.2941 +
1.2942 + /**
1.2943 + * Parses inlined match flags and set them appropriately.
1.2944 + */
1.2945 + private void addFlag() {
1.2946 + int ch = peek();
1.2947 + for (;;) {
1.2948 + switch (ch) {
1.2949 + case 'i':
1.2950 + flags |= CASE_INSENSITIVE;
1.2951 + break;
1.2952 + case 'm':
1.2953 + flags |= MULTILINE;
1.2954 + break;
1.2955 + case 's':
1.2956 + flags |= DOTALL;
1.2957 + break;
1.2958 + case 'd':
1.2959 + flags |= UNIX_LINES;
1.2960 + break;
1.2961 + case 'u':
1.2962 + flags |= UNICODE_CASE;
1.2963 + break;
1.2964 + case 'c':
1.2965 + flags |= CANON_EQ;
1.2966 + break;
1.2967 + case 'x':
1.2968 + flags |= COMMENTS;
1.2969 + break;
1.2970 + case 'U':
1.2971 + flags |= (UNICODE_CHARACTER_CLASS | UNICODE_CASE);
1.2972 + break;
1.2973 + case '-': // subFlag then fall through
1.2974 + ch = next();
1.2975 + subFlag();
1.2976 + default:
1.2977 + return;
1.2978 + }
1.2979 + ch = next();
1.2980 + }
1.2981 + }
1.2982 +
1.2983 + /**
1.2984 + * Parses the second part of inlined match flags and turns off
1.2985 + * flags appropriately.
1.2986 + */
1.2987 + private void subFlag() {
1.2988 + int ch = peek();
1.2989 + for (;;) {
1.2990 + switch (ch) {
1.2991 + case 'i':
1.2992 + flags &= ~CASE_INSENSITIVE;
1.2993 + break;
1.2994 + case 'm':
1.2995 + flags &= ~MULTILINE;
1.2996 + break;
1.2997 + case 's':
1.2998 + flags &= ~DOTALL;
1.2999 + break;
1.3000 + case 'd':
1.3001 + flags &= ~UNIX_LINES;
1.3002 + break;
1.3003 + case 'u':
1.3004 + flags &= ~UNICODE_CASE;
1.3005 + break;
1.3006 + case 'c':
1.3007 + flags &= ~CANON_EQ;
1.3008 + break;
1.3009 + case 'x':
1.3010 + flags &= ~COMMENTS;
1.3011 + break;
1.3012 + case 'U':
1.3013 + flags &= ~(UNICODE_CHARACTER_CLASS | UNICODE_CASE);
1.3014 + default:
1.3015 + return;
1.3016 + }
1.3017 + ch = next();
1.3018 + }
1.3019 + }
1.3020 +
1.3021 + static final int MAX_REPS = 0x7FFFFFFF;
1.3022 +
1.3023 + static final int GREEDY = 0;
1.3024 +
1.3025 + static final int LAZY = 1;
1.3026 +
1.3027 + static final int POSSESSIVE = 2;
1.3028 +
1.3029 + static final int INDEPENDENT = 3;
1.3030 +
1.3031 + /**
1.3032 + * Processes repetition. If the next character peeked is a quantifier
1.3033 + * then new nodes must be appended to handle the repetition.
1.3034 + * Prev could be a single or a group, so it could be a chain of nodes.
1.3035 + */
1.3036 + private Node closure(Node prev) {
1.3037 + Node atom;
1.3038 + int ch = peek();
1.3039 + switch (ch) {
1.3040 + case '?':
1.3041 + ch = next();
1.3042 + if (ch == '?') {
1.3043 + next();
1.3044 + return new Ques(prev, LAZY);
1.3045 + } else if (ch == '+') {
1.3046 + next();
1.3047 + return new Ques(prev, POSSESSIVE);
1.3048 + }
1.3049 + return new Ques(prev, GREEDY);
1.3050 + case '*':
1.3051 + ch = next();
1.3052 + if (ch == '?') {
1.3053 + next();
1.3054 + return new Curly(prev, 0, MAX_REPS, LAZY);
1.3055 + } else if (ch == '+') {
1.3056 + next();
1.3057 + return new Curly(prev, 0, MAX_REPS, POSSESSIVE);
1.3058 + }
1.3059 + return new Curly(prev, 0, MAX_REPS, GREEDY);
1.3060 + case '+':
1.3061 + ch = next();
1.3062 + if (ch == '?') {
1.3063 + next();
1.3064 + return new Curly(prev, 1, MAX_REPS, LAZY);
1.3065 + } else if (ch == '+') {
1.3066 + next();
1.3067 + return new Curly(prev, 1, MAX_REPS, POSSESSIVE);
1.3068 + }
1.3069 + return new Curly(prev, 1, MAX_REPS, GREEDY);
1.3070 + case '{':
1.3071 + ch = temp[cursor+1];
1.3072 + if (ASCII.isDigit(ch)) {
1.3073 + skip();
1.3074 + int cmin = 0;
1.3075 + do {
1.3076 + cmin = cmin * 10 + (ch - '0');
1.3077 + } while (ASCII.isDigit(ch = read()));
1.3078 + int cmax = cmin;
1.3079 + if (ch == ',') {
1.3080 + ch = read();
1.3081 + cmax = MAX_REPS;
1.3082 + if (ch != '}') {
1.3083 + cmax = 0;
1.3084 + while (ASCII.isDigit(ch)) {
1.3085 + cmax = cmax * 10 + (ch - '0');
1.3086 + ch = read();
1.3087 + }
1.3088 + }
1.3089 + }
1.3090 + if (ch != '}')
1.3091 + throw error("Unclosed counted closure");
1.3092 + if (((cmin) | (cmax) | (cmax - cmin)) < 0)
1.3093 + throw error("Illegal repetition range");
1.3094 + Curly curly;
1.3095 + ch = peek();
1.3096 + if (ch == '?') {
1.3097 + next();
1.3098 + curly = new Curly(prev, cmin, cmax, LAZY);
1.3099 + } else if (ch == '+') {
1.3100 + next();
1.3101 + curly = new Curly(prev, cmin, cmax, POSSESSIVE);
1.3102 + } else {
1.3103 + curly = new Curly(prev, cmin, cmax, GREEDY);
1.3104 + }
1.3105 + return curly;
1.3106 + } else {
1.3107 + throw error("Illegal repetition");
1.3108 + }
1.3109 + default:
1.3110 + return prev;
1.3111 + }
1.3112 + }
1.3113 +
1.3114 + /**
1.3115 + * Utility method for parsing control escape sequences.
1.3116 + */
1.3117 + private int c() {
1.3118 + if (cursor < patternLength) {
1.3119 + return read() ^ 64;
1.3120 + }
1.3121 + throw error("Illegal control escape sequence");
1.3122 + }
1.3123 +
1.3124 + /**
1.3125 + * Utility method for parsing octal escape sequences.
1.3126 + */
1.3127 + private int o() {
1.3128 + int n = read();
1.3129 + if (((n-'0')|('7'-n)) >= 0) {
1.3130 + int m = read();
1.3131 + if (((m-'0')|('7'-m)) >= 0) {
1.3132 + int o = read();
1.3133 + if ((((o-'0')|('7'-o)) >= 0) && (((n-'0')|('3'-n)) >= 0)) {
1.3134 + return (n - '0') * 64 + (m - '0') * 8 + (o - '0');
1.3135 + }
1.3136 + unread();
1.3137 + return (n - '0') * 8 + (m - '0');
1.3138 + }
1.3139 + unread();
1.3140 + return (n - '0');
1.3141 + }
1.3142 + throw error("Illegal octal escape sequence");
1.3143 + }
1.3144 +
1.3145 + /**
1.3146 + * Utility method for parsing hexadecimal escape sequences.
1.3147 + */
1.3148 + private int x() {
1.3149 + int n = read();
1.3150 + if (ASCII.isHexDigit(n)) {
1.3151 + int m = read();
1.3152 + if (ASCII.isHexDigit(m)) {
1.3153 + return ASCII.toDigit(n) * 16 + ASCII.toDigit(m);
1.3154 + }
1.3155 + } else if (n == '{' && ASCII.isHexDigit(peek())) {
1.3156 + int ch = 0;
1.3157 + while (ASCII.isHexDigit(n = read())) {
1.3158 + ch = (ch << 4) + ASCII.toDigit(n);
1.3159 + if (ch > Character.MAX_CODE_POINT)
1.3160 + throw error("Hexadecimal codepoint is too big");
1.3161 + }
1.3162 + if (n != '}')
1.3163 + throw error("Unclosed hexadecimal escape sequence");
1.3164 + return ch;
1.3165 + }
1.3166 + throw error("Illegal hexadecimal escape sequence");
1.3167 + }
1.3168 +
1.3169 + /**
1.3170 + * Utility method for parsing unicode escape sequences.
1.3171 + */
1.3172 + private int cursor() {
1.3173 + return cursor;
1.3174 + }
1.3175 +
1.3176 + private void setcursor(int pos) {
1.3177 + cursor = pos;
1.3178 + }
1.3179 +
1.3180 + private int uxxxx() {
1.3181 + int n = 0;
1.3182 + for (int i = 0; i < 4; i++) {
1.3183 + int ch = read();
1.3184 + if (!ASCII.isHexDigit(ch)) {
1.3185 + throw error("Illegal Unicode escape sequence");
1.3186 + }
1.3187 + n = n * 16 + ASCII.toDigit(ch);
1.3188 + }
1.3189 + return n;
1.3190 + }
1.3191 +
1.3192 + private int u() {
1.3193 + int n = uxxxx();
1.3194 + if (Character.isHighSurrogate((char)n)) {
1.3195 + int cur = cursor();
1.3196 + if (read() == '\\' && read() == 'u') {
1.3197 + int n2 = uxxxx();
1.3198 + if (Character.isLowSurrogate((char)n2))
1.3199 + return Character.toCodePoint((char)n, (char)n2);
1.3200 + }
1.3201 + setcursor(cur);
1.3202 + }
1.3203 + return n;
1.3204 + }
1.3205 +
1.3206 + //
1.3207 + // Utility methods for code point support
1.3208 + //
1.3209 +
1.3210 + private static final int countChars(CharSequence seq, int index,
1.3211 + int lengthInCodePoints) {
1.3212 + // optimization
1.3213 + if (lengthInCodePoints == 1 && !Character.isHighSurrogate(seq.charAt(index))) {
1.3214 + assert (index >= 0 && index < seq.length());
1.3215 + return 1;
1.3216 + }
1.3217 + int length = seq.length();
1.3218 + int x = index;
1.3219 + if (lengthInCodePoints >= 0) {
1.3220 + assert (index >= 0 && index < length);
1.3221 + for (int i = 0; x < length && i < lengthInCodePoints; i++) {
1.3222 + if (Character.isHighSurrogate(seq.charAt(x++))) {
1.3223 + if (x < length && Character.isLowSurrogate(seq.charAt(x))) {
1.3224 + x++;
1.3225 + }
1.3226 + }
1.3227 + }
1.3228 + return x - index;
1.3229 + }
1.3230 +
1.3231 + assert (index >= 0 && index <= length);
1.3232 + if (index == 0) {
1.3233 + return 0;
1.3234 + }
1.3235 + int len = -lengthInCodePoints;
1.3236 + for (int i = 0; x > 0 && i < len; i++) {
1.3237 + if (Character.isLowSurrogate(seq.charAt(--x))) {
1.3238 + if (x > 0 && Character.isHighSurrogate(seq.charAt(x-1))) {
1.3239 + x--;
1.3240 + }
1.3241 + }
1.3242 + }
1.3243 + return index - x;
1.3244 + }
1.3245 +
1.3246 + private static final int countCodePoints(CharSequence seq) {
1.3247 + int length = seq.length();
1.3248 + int n = 0;
1.3249 + for (int i = 0; i < length; ) {
1.3250 + n++;
1.3251 + if (Character.isHighSurrogate(seq.charAt(i++))) {
1.3252 + if (i < length && Character.isLowSurrogate(seq.charAt(i))) {
1.3253 + i++;
1.3254 + }
1.3255 + }
1.3256 + }
1.3257 + return n;
1.3258 + }
1.3259 +
1.3260 + /**
1.3261 + * Creates a bit vector for matching Latin-1 values. A normal BitClass
1.3262 + * never matches values above Latin-1, and a complemented BitClass always
1.3263 + * matches values above Latin-1.
1.3264 + */
1.3265 + private static final class BitClass extends BmpCharProperty {
1.3266 + final boolean[] bits;
1.3267 + BitClass() { bits = new boolean[256]; }
1.3268 + private BitClass(boolean[] bits) { this.bits = bits; }
1.3269 + BitClass add(int c, int flags) {
1.3270 + assert c >= 0 && c <= 255;
1.3271 + if ((flags & CASE_INSENSITIVE) != 0) {
1.3272 + if (ASCII.isAscii(c)) {
1.3273 + bits[ASCII.toUpper(c)] = true;
1.3274 + bits[ASCII.toLower(c)] = true;
1.3275 + } else if ((flags & UNICODE_CASE) != 0) {
1.3276 + bits[Character.toLowerCase(c)] = true;
1.3277 + bits[Character.toUpperCase(c)] = true;
1.3278 + }
1.3279 + }
1.3280 + bits[c] = true;
1.3281 + return this;
1.3282 + }
1.3283 + boolean isSatisfiedBy(int ch) {
1.3284 + return ch < 256 && bits[ch];
1.3285 + }
1.3286 + }
1.3287 +
1.3288 + /**
1.3289 + * Returns a suitably optimized, single character matcher.
1.3290 + */
1.3291 + private CharProperty newSingle(final int ch) {
1.3292 + if (has(CASE_INSENSITIVE)) {
1.3293 + int lower, upper;
1.3294 + if (has(UNICODE_CASE)) {
1.3295 + upper = Character.toUpperCase(ch);
1.3296 + lower = Character.toLowerCase(upper);
1.3297 + if (upper != lower)
1.3298 + return new SingleU(lower);
1.3299 + } else if (ASCII.isAscii(ch)) {
1.3300 + lower = ASCII.toLower(ch);
1.3301 + upper = ASCII.toUpper(ch);
1.3302 + if (lower != upper)
1.3303 + return new SingleI(lower, upper);
1.3304 + }
1.3305 + }
1.3306 + if (isSupplementary(ch))
1.3307 + return new SingleS(ch); // Match a given Unicode character
1.3308 + return new Single(ch); // Match a given BMP character
1.3309 + }
1.3310 +
1.3311 + /**
1.3312 + * Utility method for creating a string slice matcher.
1.3313 + */
1.3314 + private Node newSlice(int[] buf, int count, boolean hasSupplementary) {
1.3315 + int[] tmp = new int[count];
1.3316 + if (has(CASE_INSENSITIVE)) {
1.3317 + if (has(UNICODE_CASE)) {
1.3318 + for (int i = 0; i < count; i++) {
1.3319 + tmp[i] = Character.toLowerCase(
1.3320 + Character.toUpperCase(buf[i]));
1.3321 + }
1.3322 + return hasSupplementary? new SliceUS(tmp) : new SliceU(tmp);
1.3323 + }
1.3324 + for (int i = 0; i < count; i++) {
1.3325 + tmp[i] = ASCII.toLower(buf[i]);
1.3326 + }
1.3327 + return hasSupplementary? new SliceIS(tmp) : new SliceI(tmp);
1.3328 + }
1.3329 + for (int i = 0; i < count; i++) {
1.3330 + tmp[i] = buf[i];
1.3331 + }
1.3332 + return hasSupplementary ? new SliceS(tmp) : new Slice(tmp);
1.3333 + }
1.3334 +
1.3335 + /**
1.3336 + * The following classes are the building components of the object
1.3337 + * tree that represents a compiled regular expression. The object tree
1.3338 + * is made of individual elements that handle constructs in the Pattern.
1.3339 + * Each type of object knows how to match its equivalent construct with
1.3340 + * the match() method.
1.3341 + */
1.3342 +
1.3343 + /**
1.3344 + * Base class for all node classes. Subclasses should override the match()
1.3345 + * method as appropriate. This class is an accepting node, so its match()
1.3346 + * always returns true.
1.3347 + */
1.3348 + static class Node extends Object {
1.3349 + Node next;
1.3350 + Node() {
1.3351 + next = Pattern.accept;
1.3352 + }
1.3353 + /**
1.3354 + * This method implements the classic accept node.
1.3355 + */
1.3356 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3357 + matcher.last = i;
1.3358 + matcher.groups[0] = matcher.first;
1.3359 + matcher.groups[1] = matcher.last;
1.3360 + return true;
1.3361 + }
1.3362 + /**
1.3363 + * This method is good for all zero length assertions.
1.3364 + */
1.3365 + boolean study(TreeInfo info) {
1.3366 + if (next != null) {
1.3367 + return next.study(info);
1.3368 + } else {
1.3369 + return info.deterministic;
1.3370 + }
1.3371 + }
1.3372 + }
1.3373 +
1.3374 + static class LastNode extends Node {
1.3375 + /**
1.3376 + * This method implements the classic accept node with
1.3377 + * the addition of a check to see if the match occurred
1.3378 + * using all of the input.
1.3379 + */
1.3380 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3381 + if (matcher.acceptMode == Matcher.ENDANCHOR && i != matcher.to)
1.3382 + return false;
1.3383 + matcher.last = i;
1.3384 + matcher.groups[0] = matcher.first;
1.3385 + matcher.groups[1] = matcher.last;
1.3386 + return true;
1.3387 + }
1.3388 + }
1.3389 +
1.3390 + /**
1.3391 + * Used for REs that can start anywhere within the input string.
1.3392 + * This basically tries to match repeatedly at each spot in the
1.3393 + * input string, moving forward after each try. An anchored search
1.3394 + * or a BnM will bypass this node completely.
1.3395 + */
1.3396 + static class Start extends Node {
1.3397 + int minLength;
1.3398 + Start(Node node) {
1.3399 + this.next = node;
1.3400 + TreeInfo info = new TreeInfo();
1.3401 + next.study(info);
1.3402 + minLength = info.minLength;
1.3403 + }
1.3404 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3405 + if (i > matcher.to - minLength) {
1.3406 + matcher.hitEnd = true;
1.3407 + return false;
1.3408 + }
1.3409 + int guard = matcher.to - minLength;
1.3410 + for (; i <= guard; i++) {
1.3411 + if (next.match(matcher, i, seq)) {
1.3412 + matcher.first = i;
1.3413 + matcher.groups[0] = matcher.first;
1.3414 + matcher.groups[1] = matcher.last;
1.3415 + return true;
1.3416 + }
1.3417 + }
1.3418 + matcher.hitEnd = true;
1.3419 + return false;
1.3420 + }
1.3421 + boolean study(TreeInfo info) {
1.3422 + next.study(info);
1.3423 + info.maxValid = false;
1.3424 + info.deterministic = false;
1.3425 + return false;
1.3426 + }
1.3427 + }
1.3428 +
1.3429 + /*
1.3430 + * StartS supports supplementary characters, including unpaired surrogates.
1.3431 + */
1.3432 + static final class StartS extends Start {
1.3433 + StartS(Node node) {
1.3434 + super(node);
1.3435 + }
1.3436 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3437 + if (i > matcher.to - minLength) {
1.3438 + matcher.hitEnd = true;
1.3439 + return false;
1.3440 + }
1.3441 + int guard = matcher.to - minLength;
1.3442 + while (i <= guard) {
1.3443 + //if ((ret = next.match(matcher, i, seq)) || i == guard)
1.3444 + if (next.match(matcher, i, seq)) {
1.3445 + matcher.first = i;
1.3446 + matcher.groups[0] = matcher.first;
1.3447 + matcher.groups[1] = matcher.last;
1.3448 + return true;
1.3449 + }
1.3450 + if (i == guard)
1.3451 + break;
1.3452 + // Optimization to move to the next character. This is
1.3453 + // faster than countChars(seq, i, 1).
1.3454 + if (Character.isHighSurrogate(seq.charAt(i++))) {
1.3455 + if (i < seq.length() &&
1.3456 + Character.isLowSurrogate(seq.charAt(i))) {
1.3457 + i++;
1.3458 + }
1.3459 + }
1.3460 + }
1.3461 + matcher.hitEnd = true;
1.3462 + return false;
1.3463 + }
1.3464 + }
1.3465 +
1.3466 + /**
1.3467 + * Node to anchor at the beginning of input. This object implements the
1.3468 + * match for a \A sequence, and the caret anchor will use this if not in
1.3469 + * multiline mode.
1.3470 + */
1.3471 + static final class Begin extends Node {
1.3472 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3473 + int fromIndex = (matcher.anchoringBounds) ?
1.3474 + matcher.from : 0;
1.3475 + if (i == fromIndex && next.match(matcher, i, seq)) {
1.3476 + matcher.first = i;
1.3477 + matcher.groups[0] = i;
1.3478 + matcher.groups[1] = matcher.last;
1.3479 + return true;
1.3480 + } else {
1.3481 + return false;
1.3482 + }
1.3483 + }
1.3484 + }
1.3485 +
1.3486 + /**
1.3487 + * Node to anchor at the end of input. This is the absolute end, so this
1.3488 + * should not match at the last newline before the end as $ will.
1.3489 + */
1.3490 + static final class End extends Node {
1.3491 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3492 + int endIndex = (matcher.anchoringBounds) ?
1.3493 + matcher.to : matcher.getTextLength();
1.3494 + if (i == endIndex) {
1.3495 + matcher.hitEnd = true;
1.3496 + return next.match(matcher, i, seq);
1.3497 + }
1.3498 + return false;
1.3499 + }
1.3500 + }
1.3501 +
1.3502 + /**
1.3503 + * Node to anchor at the beginning of a line. This is essentially the
1.3504 + * object to match for the multiline ^.
1.3505 + */
1.3506 + static final class Caret extends Node {
1.3507 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3508 + int startIndex = matcher.from;
1.3509 + int endIndex = matcher.to;
1.3510 + if (!matcher.anchoringBounds) {
1.3511 + startIndex = 0;
1.3512 + endIndex = matcher.getTextLength();
1.3513 + }
1.3514 + // Perl does not match ^ at end of input even after newline
1.3515 + if (i == endIndex) {
1.3516 + matcher.hitEnd = true;
1.3517 + return false;
1.3518 + }
1.3519 + if (i > startIndex) {
1.3520 + char ch = seq.charAt(i-1);
1.3521 + if (ch != '\n' && ch != '\r'
1.3522 + && (ch|1) != '\u2029'
1.3523 + && ch != '\u0085' ) {
1.3524 + return false;
1.3525 + }
1.3526 + // Should treat /r/n as one newline
1.3527 + if (ch == '\r' && seq.charAt(i) == '\n')
1.3528 + return false;
1.3529 + }
1.3530 + return next.match(matcher, i, seq);
1.3531 + }
1.3532 + }
1.3533 +
1.3534 + /**
1.3535 + * Node to anchor at the beginning of a line when in unixdot mode.
1.3536 + */
1.3537 + static final class UnixCaret extends Node {
1.3538 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3539 + int startIndex = matcher.from;
1.3540 + int endIndex = matcher.to;
1.3541 + if (!matcher.anchoringBounds) {
1.3542 + startIndex = 0;
1.3543 + endIndex = matcher.getTextLength();
1.3544 + }
1.3545 + // Perl does not match ^ at end of input even after newline
1.3546 + if (i == endIndex) {
1.3547 + matcher.hitEnd = true;
1.3548 + return false;
1.3549 + }
1.3550 + if (i > startIndex) {
1.3551 + char ch = seq.charAt(i-1);
1.3552 + if (ch != '\n') {
1.3553 + return false;
1.3554 + }
1.3555 + }
1.3556 + return next.match(matcher, i, seq);
1.3557 + }
1.3558 + }
1.3559 +
1.3560 + /**
1.3561 + * Node to match the location where the last match ended.
1.3562 + * This is used for the \G construct.
1.3563 + */
1.3564 + static final class LastMatch extends Node {
1.3565 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3566 + if (i != matcher.oldLast)
1.3567 + return false;
1.3568 + return next.match(matcher, i, seq);
1.3569 + }
1.3570 + }
1.3571 +
1.3572 + /**
1.3573 + * Node to anchor at the end of a line or the end of input based on the
1.3574 + * multiline mode.
1.3575 + *
1.3576 + * When not in multiline mode, the $ can only match at the very end
1.3577 + * of the input, unless the input ends in a line terminator in which
1.3578 + * it matches right before the last line terminator.
1.3579 + *
1.3580 + * Note that \r\n is considered an atomic line terminator.
1.3581 + *
1.3582 + * Like ^ the $ operator matches at a position, it does not match the
1.3583 + * line terminators themselves.
1.3584 + */
1.3585 + static final class Dollar extends Node {
1.3586 + boolean multiline;
1.3587 + Dollar(boolean mul) {
1.3588 + multiline = mul;
1.3589 + }
1.3590 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3591 + int endIndex = (matcher.anchoringBounds) ?
1.3592 + matcher.to : matcher.getTextLength();
1.3593 + if (!multiline) {
1.3594 + if (i < endIndex - 2)
1.3595 + return false;
1.3596 + if (i == endIndex - 2) {
1.3597 + char ch = seq.charAt(i);
1.3598 + if (ch != '\r')
1.3599 + return false;
1.3600 + ch = seq.charAt(i + 1);
1.3601 + if (ch != '\n')
1.3602 + return false;
1.3603 + }
1.3604 + }
1.3605 + // Matches before any line terminator; also matches at the
1.3606 + // end of input
1.3607 + // Before line terminator:
1.3608 + // If multiline, we match here no matter what
1.3609 + // If not multiline, fall through so that the end
1.3610 + // is marked as hit; this must be a /r/n or a /n
1.3611 + // at the very end so the end was hit; more input
1.3612 + // could make this not match here
1.3613 + if (i < endIndex) {
1.3614 + char ch = seq.charAt(i);
1.3615 + if (ch == '\n') {
1.3616 + // No match between \r\n
1.3617 + if (i > 0 && seq.charAt(i-1) == '\r')
1.3618 + return false;
1.3619 + if (multiline)
1.3620 + return next.match(matcher, i, seq);
1.3621 + } else if (ch == '\r' || ch == '\u0085' ||
1.3622 + (ch|1) == '\u2029') {
1.3623 + if (multiline)
1.3624 + return next.match(matcher, i, seq);
1.3625 + } else { // No line terminator, no match
1.3626 + return false;
1.3627 + }
1.3628 + }
1.3629 + // Matched at current end so hit end
1.3630 + matcher.hitEnd = true;
1.3631 + // If a $ matches because of end of input, then more input
1.3632 + // could cause it to fail!
1.3633 + matcher.requireEnd = true;
1.3634 + return next.match(matcher, i, seq);
1.3635 + }
1.3636 + boolean study(TreeInfo info) {
1.3637 + next.study(info);
1.3638 + return info.deterministic;
1.3639 + }
1.3640 + }
1.3641 +
1.3642 + /**
1.3643 + * Node to anchor at the end of a line or the end of input based on the
1.3644 + * multiline mode when in unix lines mode.
1.3645 + */
1.3646 + static final class UnixDollar extends Node {
1.3647 + boolean multiline;
1.3648 + UnixDollar(boolean mul) {
1.3649 + multiline = mul;
1.3650 + }
1.3651 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3652 + int endIndex = (matcher.anchoringBounds) ?
1.3653 + matcher.to : matcher.getTextLength();
1.3654 + if (i < endIndex) {
1.3655 + char ch = seq.charAt(i);
1.3656 + if (ch == '\n') {
1.3657 + // If not multiline, then only possible to
1.3658 + // match at very end or one before end
1.3659 + if (multiline == false && i != endIndex - 1)
1.3660 + return false;
1.3661 + // If multiline return next.match without setting
1.3662 + // matcher.hitEnd
1.3663 + if (multiline)
1.3664 + return next.match(matcher, i, seq);
1.3665 + } else {
1.3666 + return false;
1.3667 + }
1.3668 + }
1.3669 + // Matching because at the end or 1 before the end;
1.3670 + // more input could change this so set hitEnd
1.3671 + matcher.hitEnd = true;
1.3672 + // If a $ matches because of end of input, then more input
1.3673 + // could cause it to fail!
1.3674 + matcher.requireEnd = true;
1.3675 + return next.match(matcher, i, seq);
1.3676 + }
1.3677 + boolean study(TreeInfo info) {
1.3678 + next.study(info);
1.3679 + return info.deterministic;
1.3680 + }
1.3681 + }
1.3682 +
1.3683 + /**
1.3684 + * Abstract node class to match one character satisfying some
1.3685 + * boolean property.
1.3686 + */
1.3687 + private static abstract class CharProperty extends Node {
1.3688 + abstract boolean isSatisfiedBy(int ch);
1.3689 + CharProperty complement() {
1.3690 + return new CharProperty() {
1.3691 + boolean isSatisfiedBy(int ch) {
1.3692 + return ! CharProperty.this.isSatisfiedBy(ch);}};
1.3693 + }
1.3694 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3695 + if (i < matcher.to) {
1.3696 + int ch = Character.codePointAt(seq, i);
1.3697 + return isSatisfiedBy(ch)
1.3698 + && next.match(matcher, i+Character.charCount(ch), seq);
1.3699 + } else {
1.3700 + matcher.hitEnd = true;
1.3701 + return false;
1.3702 + }
1.3703 + }
1.3704 + boolean study(TreeInfo info) {
1.3705 + info.minLength++;
1.3706 + info.maxLength++;
1.3707 + return next.study(info);
1.3708 + }
1.3709 + }
1.3710 +
1.3711 + /**
1.3712 + * Optimized version of CharProperty that works only for
1.3713 + * properties never satisfied by Supplementary characters.
1.3714 + */
1.3715 + private static abstract class BmpCharProperty extends CharProperty {
1.3716 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3717 + if (i < matcher.to) {
1.3718 + return isSatisfiedBy(seq.charAt(i))
1.3719 + && next.match(matcher, i+1, seq);
1.3720 + } else {
1.3721 + matcher.hitEnd = true;
1.3722 + return false;
1.3723 + }
1.3724 + }
1.3725 + }
1.3726 +
1.3727 + /**
1.3728 + * Node class that matches a Supplementary Unicode character
1.3729 + */
1.3730 + static final class SingleS extends CharProperty {
1.3731 + final int c;
1.3732 + SingleS(int c) { this.c = c; }
1.3733 + boolean isSatisfiedBy(int ch) {
1.3734 + return ch == c;
1.3735 + }
1.3736 + }
1.3737 +
1.3738 + /**
1.3739 + * Optimization -- matches a given BMP character
1.3740 + */
1.3741 + static final class Single extends BmpCharProperty {
1.3742 + final int c;
1.3743 + Single(int c) { this.c = c; }
1.3744 + boolean isSatisfiedBy(int ch) {
1.3745 + return ch == c;
1.3746 + }
1.3747 + }
1.3748 +
1.3749 + /**
1.3750 + * Case insensitive matches a given BMP character
1.3751 + */
1.3752 + static final class SingleI extends BmpCharProperty {
1.3753 + final int lower;
1.3754 + final int upper;
1.3755 + SingleI(int lower, int upper) {
1.3756 + this.lower = lower;
1.3757 + this.upper = upper;
1.3758 + }
1.3759 + boolean isSatisfiedBy(int ch) {
1.3760 + return ch == lower || ch == upper;
1.3761 + }
1.3762 + }
1.3763 +
1.3764 + /**
1.3765 + * Unicode case insensitive matches a given Unicode character
1.3766 + */
1.3767 + static final class SingleU extends CharProperty {
1.3768 + final int lower;
1.3769 + SingleU(int lower) {
1.3770 + this.lower = lower;
1.3771 + }
1.3772 + boolean isSatisfiedBy(int ch) {
1.3773 + return lower == ch ||
1.3774 + lower == Character.toLowerCase(Character.toUpperCase(ch));
1.3775 + }
1.3776 + }
1.3777 +
1.3778 +
1.3779 + /**
1.3780 + * Node class that matches a Unicode block.
1.3781 + */
1.3782 + static final class Block extends CharProperty {
1.3783 + final Character.UnicodeBlock block;
1.3784 + Block(Character.UnicodeBlock block) {
1.3785 + this.block = block;
1.3786 + }
1.3787 + boolean isSatisfiedBy(int ch) {
1.3788 + return block == Character.UnicodeBlock.of(ch);
1.3789 + }
1.3790 + }
1.3791 +
1.3792 + /**
1.3793 + * Node class that matches a Unicode script
1.3794 + */
1.3795 + static final class Script extends CharProperty {
1.3796 + final Character.UnicodeScript script;
1.3797 + Script(Character.UnicodeScript script) {
1.3798 + this.script = script;
1.3799 + }
1.3800 + boolean isSatisfiedBy(int ch) {
1.3801 + return script == Character.UnicodeScript.of(ch);
1.3802 + }
1.3803 + }
1.3804 +
1.3805 + /**
1.3806 + * Node class that matches a Unicode category.
1.3807 + */
1.3808 + static final class Category extends CharProperty {
1.3809 + final int typeMask;
1.3810 + Category(int typeMask) { this.typeMask = typeMask; }
1.3811 + boolean isSatisfiedBy(int ch) {
1.3812 + return (typeMask & (1 << Character.getType(ch))) != 0;
1.3813 + }
1.3814 + }
1.3815 +
1.3816 + /**
1.3817 + * Node class that matches a Unicode "type"
1.3818 + */
1.3819 + static final class Utype extends CharProperty {
1.3820 + final UnicodeProp uprop;
1.3821 + Utype(UnicodeProp uprop) { this.uprop = uprop; }
1.3822 + boolean isSatisfiedBy(int ch) {
1.3823 + return uprop.is(ch);
1.3824 + }
1.3825 + }
1.3826 +
1.3827 +
1.3828 + /**
1.3829 + * Node class that matches a POSIX type.
1.3830 + */
1.3831 + static final class Ctype extends BmpCharProperty {
1.3832 + final int ctype;
1.3833 + Ctype(int ctype) { this.ctype = ctype; }
1.3834 + boolean isSatisfiedBy(int ch) {
1.3835 + return ch < 128 && ASCII.isType(ch, ctype);
1.3836 + }
1.3837 + }
1.3838 +
1.3839 + /**
1.3840 + * Base class for all Slice nodes
1.3841 + */
1.3842 + static class SliceNode extends Node {
1.3843 + int[] buffer;
1.3844 + SliceNode(int[] buf) {
1.3845 + buffer = buf;
1.3846 + }
1.3847 + boolean study(TreeInfo info) {
1.3848 + info.minLength += buffer.length;
1.3849 + info.maxLength += buffer.length;
1.3850 + return next.study(info);
1.3851 + }
1.3852 + }
1.3853 +
1.3854 + /**
1.3855 + * Node class for a case sensitive/BMP-only sequence of literal
1.3856 + * characters.
1.3857 + */
1.3858 + static final class Slice extends SliceNode {
1.3859 + Slice(int[] buf) {
1.3860 + super(buf);
1.3861 + }
1.3862 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3863 + int[] buf = buffer;
1.3864 + int len = buf.length;
1.3865 + for (int j=0; j<len; j++) {
1.3866 + if ((i+j) >= matcher.to) {
1.3867 + matcher.hitEnd = true;
1.3868 + return false;
1.3869 + }
1.3870 + if (buf[j] != seq.charAt(i+j))
1.3871 + return false;
1.3872 + }
1.3873 + return next.match(matcher, i+len, seq);
1.3874 + }
1.3875 + }
1.3876 +
1.3877 + /**
1.3878 + * Node class for a case_insensitive/BMP-only sequence of literal
1.3879 + * characters.
1.3880 + */
1.3881 + static class SliceI extends SliceNode {
1.3882 + SliceI(int[] buf) {
1.3883 + super(buf);
1.3884 + }
1.3885 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3886 + int[] buf = buffer;
1.3887 + int len = buf.length;
1.3888 + for (int j=0; j<len; j++) {
1.3889 + if ((i+j) >= matcher.to) {
1.3890 + matcher.hitEnd = true;
1.3891 + return false;
1.3892 + }
1.3893 + int c = seq.charAt(i+j);
1.3894 + if (buf[j] != c &&
1.3895 + buf[j] != ASCII.toLower(c))
1.3896 + return false;
1.3897 + }
1.3898 + return next.match(matcher, i+len, seq);
1.3899 + }
1.3900 + }
1.3901 +
1.3902 + /**
1.3903 + * Node class for a unicode_case_insensitive/BMP-only sequence of
1.3904 + * literal characters. Uses unicode case folding.
1.3905 + */
1.3906 + static final class SliceU extends SliceNode {
1.3907 + SliceU(int[] buf) {
1.3908 + super(buf);
1.3909 + }
1.3910 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3911 + int[] buf = buffer;
1.3912 + int len = buf.length;
1.3913 + for (int j=0; j<len; j++) {
1.3914 + if ((i+j) >= matcher.to) {
1.3915 + matcher.hitEnd = true;
1.3916 + return false;
1.3917 + }
1.3918 + int c = seq.charAt(i+j);
1.3919 + if (buf[j] != c &&
1.3920 + buf[j] != Character.toLowerCase(Character.toUpperCase(c)))
1.3921 + return false;
1.3922 + }
1.3923 + return next.match(matcher, i+len, seq);
1.3924 + }
1.3925 + }
1.3926 +
1.3927 + /**
1.3928 + * Node class for a case sensitive sequence of literal characters
1.3929 + * including supplementary characters.
1.3930 + */
1.3931 + static final class SliceS extends SliceNode {
1.3932 + SliceS(int[] buf) {
1.3933 + super(buf);
1.3934 + }
1.3935 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3936 + int[] buf = buffer;
1.3937 + int x = i;
1.3938 + for (int j = 0; j < buf.length; j++) {
1.3939 + if (x >= matcher.to) {
1.3940 + matcher.hitEnd = true;
1.3941 + return false;
1.3942 + }
1.3943 + int c = Character.codePointAt(seq, x);
1.3944 + if (buf[j] != c)
1.3945 + return false;
1.3946 + x += Character.charCount(c);
1.3947 + if (x > matcher.to) {
1.3948 + matcher.hitEnd = true;
1.3949 + return false;
1.3950 + }
1.3951 + }
1.3952 + return next.match(matcher, x, seq);
1.3953 + }
1.3954 + }
1.3955 +
1.3956 + /**
1.3957 + * Node class for a case insensitive sequence of literal characters
1.3958 + * including supplementary characters.
1.3959 + */
1.3960 + static class SliceIS extends SliceNode {
1.3961 + SliceIS(int[] buf) {
1.3962 + super(buf);
1.3963 + }
1.3964 + int toLower(int c) {
1.3965 + return ASCII.toLower(c);
1.3966 + }
1.3967 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.3968 + int[] buf = buffer;
1.3969 + int x = i;
1.3970 + for (int j = 0; j < buf.length; j++) {
1.3971 + if (x >= matcher.to) {
1.3972 + matcher.hitEnd = true;
1.3973 + return false;
1.3974 + }
1.3975 + int c = Character.codePointAt(seq, x);
1.3976 + if (buf[j] != c && buf[j] != toLower(c))
1.3977 + return false;
1.3978 + x += Character.charCount(c);
1.3979 + if (x > matcher.to) {
1.3980 + matcher.hitEnd = true;
1.3981 + return false;
1.3982 + }
1.3983 + }
1.3984 + return next.match(matcher, x, seq);
1.3985 + }
1.3986 + }
1.3987 +
1.3988 + /**
1.3989 + * Node class for a case insensitive sequence of literal characters.
1.3990 + * Uses unicode case folding.
1.3991 + */
1.3992 + static final class SliceUS extends SliceIS {
1.3993 + SliceUS(int[] buf) {
1.3994 + super(buf);
1.3995 + }
1.3996 + int toLower(int c) {
1.3997 + return Character.toLowerCase(Character.toUpperCase(c));
1.3998 + }
1.3999 + }
1.4000 +
1.4001 + private static boolean inRange(int lower, int ch, int upper) {
1.4002 + return lower <= ch && ch <= upper;
1.4003 + }
1.4004 +
1.4005 + /**
1.4006 + * Returns node for matching characters within an explicit value range.
1.4007 + */
1.4008 + private static CharProperty rangeFor(final int lower,
1.4009 + final int upper) {
1.4010 + return new CharProperty() {
1.4011 + boolean isSatisfiedBy(int ch) {
1.4012 + return inRange(lower, ch, upper);}};
1.4013 + }
1.4014 +
1.4015 + /**
1.4016 + * Returns node for matching characters within an explicit value
1.4017 + * range in a case insensitive manner.
1.4018 + */
1.4019 + private CharProperty caseInsensitiveRangeFor(final int lower,
1.4020 + final int upper) {
1.4021 + if (has(UNICODE_CASE))
1.4022 + return new CharProperty() {
1.4023 + boolean isSatisfiedBy(int ch) {
1.4024 + if (inRange(lower, ch, upper))
1.4025 + return true;
1.4026 + int up = Character.toUpperCase(ch);
1.4027 + return inRange(lower, up, upper) ||
1.4028 + inRange(lower, Character.toLowerCase(up), upper);}};
1.4029 + return new CharProperty() {
1.4030 + boolean isSatisfiedBy(int ch) {
1.4031 + return inRange(lower, ch, upper) ||
1.4032 + ASCII.isAscii(ch) &&
1.4033 + (inRange(lower, ASCII.toUpper(ch), upper) ||
1.4034 + inRange(lower, ASCII.toLower(ch), upper));
1.4035 + }};
1.4036 + }
1.4037 +
1.4038 + /**
1.4039 + * Implements the Unicode category ALL and the dot metacharacter when
1.4040 + * in dotall mode.
1.4041 + */
1.4042 + static final class All extends CharProperty {
1.4043 + boolean isSatisfiedBy(int ch) {
1.4044 + return true;
1.4045 + }
1.4046 + }
1.4047 +
1.4048 + /**
1.4049 + * Node class for the dot metacharacter when dotall is not enabled.
1.4050 + */
1.4051 + static final class Dot extends CharProperty {
1.4052 + boolean isSatisfiedBy(int ch) {
1.4053 + return (ch != '\n' && ch != '\r'
1.4054 + && (ch|1) != '\u2029'
1.4055 + && ch != '\u0085');
1.4056 + }
1.4057 + }
1.4058 +
1.4059 + /**
1.4060 + * Node class for the dot metacharacter when dotall is not enabled
1.4061 + * but UNIX_LINES is enabled.
1.4062 + */
1.4063 + static final class UnixDot extends CharProperty {
1.4064 + boolean isSatisfiedBy(int ch) {
1.4065 + return ch != '\n';
1.4066 + }
1.4067 + }
1.4068 +
1.4069 + /**
1.4070 + * The 0 or 1 quantifier. This one class implements all three types.
1.4071 + */
1.4072 + static final class Ques extends Node {
1.4073 + Node atom;
1.4074 + int type;
1.4075 + Ques(Node node, int type) {
1.4076 + this.atom = node;
1.4077 + this.type = type;
1.4078 + }
1.4079 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4080 + switch (type) {
1.4081 + case GREEDY:
1.4082 + return (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq))
1.4083 + || next.match(matcher, i, seq);
1.4084 + case LAZY:
1.4085 + return next.match(matcher, i, seq)
1.4086 + || (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq));
1.4087 + case POSSESSIVE:
1.4088 + if (atom.match(matcher, i, seq)) i = matcher.last;
1.4089 + return next.match(matcher, i, seq);
1.4090 + default:
1.4091 + return atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq);
1.4092 + }
1.4093 + }
1.4094 + boolean study(TreeInfo info) {
1.4095 + if (type != INDEPENDENT) {
1.4096 + int minL = info.minLength;
1.4097 + atom.study(info);
1.4098 + info.minLength = minL;
1.4099 + info.deterministic = false;
1.4100 + return next.study(info);
1.4101 + } else {
1.4102 + atom.study(info);
1.4103 + return next.study(info);
1.4104 + }
1.4105 + }
1.4106 + }
1.4107 +
1.4108 + /**
1.4109 + * Handles the curly-brace style repetition with a specified minimum and
1.4110 + * maximum occurrences. The * quantifier is handled as a special case.
1.4111 + * This class handles the three types.
1.4112 + */
1.4113 + static final class Curly extends Node {
1.4114 + Node atom;
1.4115 + int type;
1.4116 + int cmin;
1.4117 + int cmax;
1.4118 +
1.4119 + Curly(Node node, int cmin, int cmax, int type) {
1.4120 + this.atom = node;
1.4121 + this.type = type;
1.4122 + this.cmin = cmin;
1.4123 + this.cmax = cmax;
1.4124 + }
1.4125 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4126 + int j;
1.4127 + for (j = 0; j < cmin; j++) {
1.4128 + if (atom.match(matcher, i, seq)) {
1.4129 + i = matcher.last;
1.4130 + continue;
1.4131 + }
1.4132 + return false;
1.4133 + }
1.4134 + if (type == GREEDY)
1.4135 + return match0(matcher, i, j, seq);
1.4136 + else if (type == LAZY)
1.4137 + return match1(matcher, i, j, seq);
1.4138 + else
1.4139 + return match2(matcher, i, j, seq);
1.4140 + }
1.4141 + // Greedy match.
1.4142 + // i is the index to start matching at
1.4143 + // j is the number of atoms that have matched
1.4144 + boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
1.4145 + if (j >= cmax) {
1.4146 + // We have matched the maximum... continue with the rest of
1.4147 + // the regular expression
1.4148 + return next.match(matcher, i, seq);
1.4149 + }
1.4150 + int backLimit = j;
1.4151 + while (atom.match(matcher, i, seq)) {
1.4152 + // k is the length of this match
1.4153 + int k = matcher.last - i;
1.4154 + if (k == 0) // Zero length match
1.4155 + break;
1.4156 + // Move up index and number matched
1.4157 + i = matcher.last;
1.4158 + j++;
1.4159 + // We are greedy so match as many as we can
1.4160 + while (j < cmax) {
1.4161 + if (!atom.match(matcher, i, seq))
1.4162 + break;
1.4163 + if (i + k != matcher.last) {
1.4164 + if (match0(matcher, matcher.last, j+1, seq))
1.4165 + return true;
1.4166 + break;
1.4167 + }
1.4168 + i += k;
1.4169 + j++;
1.4170 + }
1.4171 + // Handle backing off if match fails
1.4172 + while (j >= backLimit) {
1.4173 + if (next.match(matcher, i, seq))
1.4174 + return true;
1.4175 + i -= k;
1.4176 + j--;
1.4177 + }
1.4178 + return false;
1.4179 + }
1.4180 + return next.match(matcher, i, seq);
1.4181 + }
1.4182 + // Reluctant match. At this point, the minimum has been satisfied.
1.4183 + // i is the index to start matching at
1.4184 + // j is the number of atoms that have matched
1.4185 + boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
1.4186 + for (;;) {
1.4187 + // Try finishing match without consuming any more
1.4188 + if (next.match(matcher, i, seq))
1.4189 + return true;
1.4190 + // At the maximum, no match found
1.4191 + if (j >= cmax)
1.4192 + return false;
1.4193 + // Okay, must try one more atom
1.4194 + if (!atom.match(matcher, i, seq))
1.4195 + return false;
1.4196 + // If we haven't moved forward then must break out
1.4197 + if (i == matcher.last)
1.4198 + return false;
1.4199 + // Move up index and number matched
1.4200 + i = matcher.last;
1.4201 + j++;
1.4202 + }
1.4203 + }
1.4204 + boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
1.4205 + for (; j < cmax; j++) {
1.4206 + if (!atom.match(matcher, i, seq))
1.4207 + break;
1.4208 + if (i == matcher.last)
1.4209 + break;
1.4210 + i = matcher.last;
1.4211 + }
1.4212 + return next.match(matcher, i, seq);
1.4213 + }
1.4214 + boolean study(TreeInfo info) {
1.4215 + // Save original info
1.4216 + int minL = info.minLength;
1.4217 + int maxL = info.maxLength;
1.4218 + boolean maxV = info.maxValid;
1.4219 + boolean detm = info.deterministic;
1.4220 + info.reset();
1.4221 +
1.4222 + atom.study(info);
1.4223 +
1.4224 + int temp = info.minLength * cmin + minL;
1.4225 + if (temp < minL) {
1.4226 + temp = 0xFFFFFFF; // arbitrary large number
1.4227 + }
1.4228 + info.minLength = temp;
1.4229 +
1.4230 + if (maxV & info.maxValid) {
1.4231 + temp = info.maxLength * cmax + maxL;
1.4232 + info.maxLength = temp;
1.4233 + if (temp < maxL) {
1.4234 + info.maxValid = false;
1.4235 + }
1.4236 + } else {
1.4237 + info.maxValid = false;
1.4238 + }
1.4239 +
1.4240 + if (info.deterministic && cmin == cmax)
1.4241 + info.deterministic = detm;
1.4242 + else
1.4243 + info.deterministic = false;
1.4244 +
1.4245 + return next.study(info);
1.4246 + }
1.4247 + }
1.4248 +
1.4249 + /**
1.4250 + * Handles the curly-brace style repetition with a specified minimum and
1.4251 + * maximum occurrences in deterministic cases. This is an iterative
1.4252 + * optimization over the Prolog and Loop system which would handle this
1.4253 + * in a recursive way. The * quantifier is handled as a special case.
1.4254 + * If capture is true then this class saves group settings and ensures
1.4255 + * that groups are unset when backing off of a group match.
1.4256 + */
1.4257 + static final class GroupCurly extends Node {
1.4258 + Node atom;
1.4259 + int type;
1.4260 + int cmin;
1.4261 + int cmax;
1.4262 + int localIndex;
1.4263 + int groupIndex;
1.4264 + boolean capture;
1.4265 +
1.4266 + GroupCurly(Node node, int cmin, int cmax, int type, int local,
1.4267 + int group, boolean capture) {
1.4268 + this.atom = node;
1.4269 + this.type = type;
1.4270 + this.cmin = cmin;
1.4271 + this.cmax = cmax;
1.4272 + this.localIndex = local;
1.4273 + this.groupIndex = group;
1.4274 + this.capture = capture;
1.4275 + }
1.4276 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4277 + int[] groups = matcher.groups;
1.4278 + int[] locals = matcher.locals;
1.4279 + int save0 = locals[localIndex];
1.4280 + int save1 = 0;
1.4281 + int save2 = 0;
1.4282 +
1.4283 + if (capture) {
1.4284 + save1 = groups[groupIndex];
1.4285 + save2 = groups[groupIndex+1];
1.4286 + }
1.4287 +
1.4288 + // Notify GroupTail there is no need to setup group info
1.4289 + // because it will be set here
1.4290 + locals[localIndex] = -1;
1.4291 +
1.4292 + boolean ret = true;
1.4293 + for (int j = 0; j < cmin; j++) {
1.4294 + if (atom.match(matcher, i, seq)) {
1.4295 + if (capture) {
1.4296 + groups[groupIndex] = i;
1.4297 + groups[groupIndex+1] = matcher.last;
1.4298 + }
1.4299 + i = matcher.last;
1.4300 + } else {
1.4301 + ret = false;
1.4302 + break;
1.4303 + }
1.4304 + }
1.4305 + if (ret) {
1.4306 + if (type == GREEDY) {
1.4307 + ret = match0(matcher, i, cmin, seq);
1.4308 + } else if (type == LAZY) {
1.4309 + ret = match1(matcher, i, cmin, seq);
1.4310 + } else {
1.4311 + ret = match2(matcher, i, cmin, seq);
1.4312 + }
1.4313 + }
1.4314 + if (!ret) {
1.4315 + locals[localIndex] = save0;
1.4316 + if (capture) {
1.4317 + groups[groupIndex] = save1;
1.4318 + groups[groupIndex+1] = save2;
1.4319 + }
1.4320 + }
1.4321 + return ret;
1.4322 + }
1.4323 + // Aggressive group match
1.4324 + boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
1.4325 + int[] groups = matcher.groups;
1.4326 + int save0 = 0;
1.4327 + int save1 = 0;
1.4328 + if (capture) {
1.4329 + save0 = groups[groupIndex];
1.4330 + save1 = groups[groupIndex+1];
1.4331 + }
1.4332 + for (;;) {
1.4333 + if (j >= cmax)
1.4334 + break;
1.4335 + if (!atom.match(matcher, i, seq))
1.4336 + break;
1.4337 + int k = matcher.last - i;
1.4338 + if (k <= 0) {
1.4339 + if (capture) {
1.4340 + groups[groupIndex] = i;
1.4341 + groups[groupIndex+1] = i + k;
1.4342 + }
1.4343 + i = i + k;
1.4344 + break;
1.4345 + }
1.4346 + for (;;) {
1.4347 + if (capture) {
1.4348 + groups[groupIndex] = i;
1.4349 + groups[groupIndex+1] = i + k;
1.4350 + }
1.4351 + i = i + k;
1.4352 + if (++j >= cmax)
1.4353 + break;
1.4354 + if (!atom.match(matcher, i, seq))
1.4355 + break;
1.4356 + if (i + k != matcher.last) {
1.4357 + if (match0(matcher, i, j, seq))
1.4358 + return true;
1.4359 + break;
1.4360 + }
1.4361 + }
1.4362 + while (j > cmin) {
1.4363 + if (next.match(matcher, i, seq)) {
1.4364 + if (capture) {
1.4365 + groups[groupIndex+1] = i;
1.4366 + groups[groupIndex] = i - k;
1.4367 + }
1.4368 + i = i - k;
1.4369 + return true;
1.4370 + }
1.4371 + // backing off
1.4372 + if (capture) {
1.4373 + groups[groupIndex+1] = i;
1.4374 + groups[groupIndex] = i - k;
1.4375 + }
1.4376 + i = i - k;
1.4377 + j--;
1.4378 + }
1.4379 + break;
1.4380 + }
1.4381 + if (capture) {
1.4382 + groups[groupIndex] = save0;
1.4383 + groups[groupIndex+1] = save1;
1.4384 + }
1.4385 + return next.match(matcher, i, seq);
1.4386 + }
1.4387 + // Reluctant matching
1.4388 + boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
1.4389 + for (;;) {
1.4390 + if (next.match(matcher, i, seq))
1.4391 + return true;
1.4392 + if (j >= cmax)
1.4393 + return false;
1.4394 + if (!atom.match(matcher, i, seq))
1.4395 + return false;
1.4396 + if (i == matcher.last)
1.4397 + return false;
1.4398 + if (capture) {
1.4399 + matcher.groups[groupIndex] = i;
1.4400 + matcher.groups[groupIndex+1] = matcher.last;
1.4401 + }
1.4402 + i = matcher.last;
1.4403 + j++;
1.4404 + }
1.4405 + }
1.4406 + // Possessive matching
1.4407 + boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
1.4408 + for (; j < cmax; j++) {
1.4409 + if (!atom.match(matcher, i, seq)) {
1.4410 + break;
1.4411 + }
1.4412 + if (capture) {
1.4413 + matcher.groups[groupIndex] = i;
1.4414 + matcher.groups[groupIndex+1] = matcher.last;
1.4415 + }
1.4416 + if (i == matcher.last) {
1.4417 + break;
1.4418 + }
1.4419 + i = matcher.last;
1.4420 + }
1.4421 + return next.match(matcher, i, seq);
1.4422 + }
1.4423 + boolean study(TreeInfo info) {
1.4424 + // Save original info
1.4425 + int minL = info.minLength;
1.4426 + int maxL = info.maxLength;
1.4427 + boolean maxV = info.maxValid;
1.4428 + boolean detm = info.deterministic;
1.4429 + info.reset();
1.4430 +
1.4431 + atom.study(info);
1.4432 +
1.4433 + int temp = info.minLength * cmin + minL;
1.4434 + if (temp < minL) {
1.4435 + temp = 0xFFFFFFF; // Arbitrary large number
1.4436 + }
1.4437 + info.minLength = temp;
1.4438 +
1.4439 + if (maxV & info.maxValid) {
1.4440 + temp = info.maxLength * cmax + maxL;
1.4441 + info.maxLength = temp;
1.4442 + if (temp < maxL) {
1.4443 + info.maxValid = false;
1.4444 + }
1.4445 + } else {
1.4446 + info.maxValid = false;
1.4447 + }
1.4448 +
1.4449 + if (info.deterministic && cmin == cmax) {
1.4450 + info.deterministic = detm;
1.4451 + } else {
1.4452 + info.deterministic = false;
1.4453 + }
1.4454 +
1.4455 + return next.study(info);
1.4456 + }
1.4457 + }
1.4458 +
1.4459 + /**
1.4460 + * A Guard node at the end of each atom node in a Branch. It
1.4461 + * serves the purpose of chaining the "match" operation to
1.4462 + * "next" but not the "study", so we can collect the TreeInfo
1.4463 + * of each atom node without including the TreeInfo of the
1.4464 + * "next".
1.4465 + */
1.4466 + static final class BranchConn extends Node {
1.4467 + BranchConn() {};
1.4468 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4469 + return next.match(matcher, i, seq);
1.4470 + }
1.4471 + boolean study(TreeInfo info) {
1.4472 + return info.deterministic;
1.4473 + }
1.4474 + }
1.4475 +
1.4476 + /**
1.4477 + * Handles the branching of alternations. Note this is also used for
1.4478 + * the ? quantifier to branch between the case where it matches once
1.4479 + * and where it does not occur.
1.4480 + */
1.4481 + static final class Branch extends Node {
1.4482 + Node[] atoms = new Node[2];
1.4483 + int size = 2;
1.4484 + Node conn;
1.4485 + Branch(Node first, Node second, Node branchConn) {
1.4486 + conn = branchConn;
1.4487 + atoms[0] = first;
1.4488 + atoms[1] = second;
1.4489 + }
1.4490 +
1.4491 + void add(Node node) {
1.4492 + if (size >= atoms.length) {
1.4493 + Node[] tmp = new Node[atoms.length*2];
1.4494 + System.arraycopy(atoms, 0, tmp, 0, atoms.length);
1.4495 + atoms = tmp;
1.4496 + }
1.4497 + atoms[size++] = node;
1.4498 + }
1.4499 +
1.4500 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4501 + for (int n = 0; n < size; n++) {
1.4502 + if (atoms[n] == null) {
1.4503 + if (conn.next.match(matcher, i, seq))
1.4504 + return true;
1.4505 + } else if (atoms[n].match(matcher, i, seq)) {
1.4506 + return true;
1.4507 + }
1.4508 + }
1.4509 + return false;
1.4510 + }
1.4511 +
1.4512 + boolean study(TreeInfo info) {
1.4513 + int minL = info.minLength;
1.4514 + int maxL = info.maxLength;
1.4515 + boolean maxV = info.maxValid;
1.4516 +
1.4517 + int minL2 = Integer.MAX_VALUE; //arbitrary large enough num
1.4518 + int maxL2 = -1;
1.4519 + for (int n = 0; n < size; n++) {
1.4520 + info.reset();
1.4521 + if (atoms[n] != null)
1.4522 + atoms[n].study(info);
1.4523 + minL2 = Math.min(minL2, info.minLength);
1.4524 + maxL2 = Math.max(maxL2, info.maxLength);
1.4525 + maxV = (maxV & info.maxValid);
1.4526 + }
1.4527 +
1.4528 + minL += minL2;
1.4529 + maxL += maxL2;
1.4530 +
1.4531 + info.reset();
1.4532 + conn.next.study(info);
1.4533 +
1.4534 + info.minLength += minL;
1.4535 + info.maxLength += maxL;
1.4536 + info.maxValid &= maxV;
1.4537 + info.deterministic = false;
1.4538 + return false;
1.4539 + }
1.4540 + }
1.4541 +
1.4542 + /**
1.4543 + * The GroupHead saves the location where the group begins in the locals
1.4544 + * and restores them when the match is done.
1.4545 + *
1.4546 + * The matchRef is used when a reference to this group is accessed later
1.4547 + * in the expression. The locals will have a negative value in them to
1.4548 + * indicate that we do not want to unset the group if the reference
1.4549 + * doesn't match.
1.4550 + */
1.4551 + static final class GroupHead extends Node {
1.4552 + int localIndex;
1.4553 + GroupHead(int localCount) {
1.4554 + localIndex = localCount;
1.4555 + }
1.4556 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4557 + int save = matcher.locals[localIndex];
1.4558 + matcher.locals[localIndex] = i;
1.4559 + boolean ret = next.match(matcher, i, seq);
1.4560 + matcher.locals[localIndex] = save;
1.4561 + return ret;
1.4562 + }
1.4563 + boolean matchRef(Matcher matcher, int i, CharSequence seq) {
1.4564 + int save = matcher.locals[localIndex];
1.4565 + matcher.locals[localIndex] = ~i; // HACK
1.4566 + boolean ret = next.match(matcher, i, seq);
1.4567 + matcher.locals[localIndex] = save;
1.4568 + return ret;
1.4569 + }
1.4570 + }
1.4571 +
1.4572 + /**
1.4573 + * Recursive reference to a group in the regular expression. It calls
1.4574 + * matchRef because if the reference fails to match we would not unset
1.4575 + * the group.
1.4576 + */
1.4577 + static final class GroupRef extends Node {
1.4578 + GroupHead head;
1.4579 + GroupRef(GroupHead head) {
1.4580 + this.head = head;
1.4581 + }
1.4582 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4583 + return head.matchRef(matcher, i, seq)
1.4584 + && next.match(matcher, matcher.last, seq);
1.4585 + }
1.4586 + boolean study(TreeInfo info) {
1.4587 + info.maxValid = false;
1.4588 + info.deterministic = false;
1.4589 + return next.study(info);
1.4590 + }
1.4591 + }
1.4592 +
1.4593 + /**
1.4594 + * The GroupTail handles the setting of group beginning and ending
1.4595 + * locations when groups are successfully matched. It must also be able to
1.4596 + * unset groups that have to be backed off of.
1.4597 + *
1.4598 + * The GroupTail node is also used when a previous group is referenced,
1.4599 + * and in that case no group information needs to be set.
1.4600 + */
1.4601 + static final class GroupTail extends Node {
1.4602 + int localIndex;
1.4603 + int groupIndex;
1.4604 + GroupTail(int localCount, int groupCount) {
1.4605 + localIndex = localCount;
1.4606 + groupIndex = groupCount + groupCount;
1.4607 + }
1.4608 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4609 + int tmp = matcher.locals[localIndex];
1.4610 + if (tmp >= 0) { // This is the normal group case.
1.4611 + // Save the group so we can unset it if it
1.4612 + // backs off of a match.
1.4613 + int groupStart = matcher.groups[groupIndex];
1.4614 + int groupEnd = matcher.groups[groupIndex+1];
1.4615 +
1.4616 + matcher.groups[groupIndex] = tmp;
1.4617 + matcher.groups[groupIndex+1] = i;
1.4618 + if (next.match(matcher, i, seq)) {
1.4619 + return true;
1.4620 + }
1.4621 + matcher.groups[groupIndex] = groupStart;
1.4622 + matcher.groups[groupIndex+1] = groupEnd;
1.4623 + return false;
1.4624 + } else {
1.4625 + // This is a group reference case. We don't need to save any
1.4626 + // group info because it isn't really a group.
1.4627 + matcher.last = i;
1.4628 + return true;
1.4629 + }
1.4630 + }
1.4631 + }
1.4632 +
1.4633 + /**
1.4634 + * This sets up a loop to handle a recursive quantifier structure.
1.4635 + */
1.4636 + static final class Prolog extends Node {
1.4637 + Loop loop;
1.4638 + Prolog(Loop loop) {
1.4639 + this.loop = loop;
1.4640 + }
1.4641 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4642 + return loop.matchInit(matcher, i, seq);
1.4643 + }
1.4644 + boolean study(TreeInfo info) {
1.4645 + return loop.study(info);
1.4646 + }
1.4647 + }
1.4648 +
1.4649 + /**
1.4650 + * Handles the repetition count for a greedy Curly. The matchInit
1.4651 + * is called from the Prolog to save the index of where the group
1.4652 + * beginning is stored. A zero length group check occurs in the
1.4653 + * normal match but is skipped in the matchInit.
1.4654 + */
1.4655 + static class Loop extends Node {
1.4656 + Node body;
1.4657 + int countIndex; // local count index in matcher locals
1.4658 + int beginIndex; // group beginning index
1.4659 + int cmin, cmax;
1.4660 + Loop(int countIndex, int beginIndex) {
1.4661 + this.countIndex = countIndex;
1.4662 + this.beginIndex = beginIndex;
1.4663 + }
1.4664 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4665 + // Avoid infinite loop in zero-length case.
1.4666 + if (i > matcher.locals[beginIndex]) {
1.4667 + int count = matcher.locals[countIndex];
1.4668 +
1.4669 + // This block is for before we reach the minimum
1.4670 + // iterations required for the loop to match
1.4671 + if (count < cmin) {
1.4672 + matcher.locals[countIndex] = count + 1;
1.4673 + boolean b = body.match(matcher, i, seq);
1.4674 + // If match failed we must backtrack, so
1.4675 + // the loop count should NOT be incremented
1.4676 + if (!b)
1.4677 + matcher.locals[countIndex] = count;
1.4678 + // Return success or failure since we are under
1.4679 + // minimum
1.4680 + return b;
1.4681 + }
1.4682 + // This block is for after we have the minimum
1.4683 + // iterations required for the loop to match
1.4684 + if (count < cmax) {
1.4685 + matcher.locals[countIndex] = count + 1;
1.4686 + boolean b = body.match(matcher, i, seq);
1.4687 + // If match failed we must backtrack, so
1.4688 + // the loop count should NOT be incremented
1.4689 + if (!b)
1.4690 + matcher.locals[countIndex] = count;
1.4691 + else
1.4692 + return true;
1.4693 + }
1.4694 + }
1.4695 + return next.match(matcher, i, seq);
1.4696 + }
1.4697 + boolean matchInit(Matcher matcher, int i, CharSequence seq) {
1.4698 + int save = matcher.locals[countIndex];
1.4699 + boolean ret = false;
1.4700 + if (0 < cmin) {
1.4701 + matcher.locals[countIndex] = 1;
1.4702 + ret = body.match(matcher, i, seq);
1.4703 + } else if (0 < cmax) {
1.4704 + matcher.locals[countIndex] = 1;
1.4705 + ret = body.match(matcher, i, seq);
1.4706 + if (ret == false)
1.4707 + ret = next.match(matcher, i, seq);
1.4708 + } else {
1.4709 + ret = next.match(matcher, i, seq);
1.4710 + }
1.4711 + matcher.locals[countIndex] = save;
1.4712 + return ret;
1.4713 + }
1.4714 + boolean study(TreeInfo info) {
1.4715 + info.maxValid = false;
1.4716 + info.deterministic = false;
1.4717 + return false;
1.4718 + }
1.4719 + }
1.4720 +
1.4721 + /**
1.4722 + * Handles the repetition count for a reluctant Curly. The matchInit
1.4723 + * is called from the Prolog to save the index of where the group
1.4724 + * beginning is stored. A zero length group check occurs in the
1.4725 + * normal match but is skipped in the matchInit.
1.4726 + */
1.4727 + static final class LazyLoop extends Loop {
1.4728 + LazyLoop(int countIndex, int beginIndex) {
1.4729 + super(countIndex, beginIndex);
1.4730 + }
1.4731 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4732 + // Check for zero length group
1.4733 + if (i > matcher.locals[beginIndex]) {
1.4734 + int count = matcher.locals[countIndex];
1.4735 + if (count < cmin) {
1.4736 + matcher.locals[countIndex] = count + 1;
1.4737 + boolean result = body.match(matcher, i, seq);
1.4738 + // If match failed we must backtrack, so
1.4739 + // the loop count should NOT be incremented
1.4740 + if (!result)
1.4741 + matcher.locals[countIndex] = count;
1.4742 + return result;
1.4743 + }
1.4744 + if (next.match(matcher, i, seq))
1.4745 + return true;
1.4746 + if (count < cmax) {
1.4747 + matcher.locals[countIndex] = count + 1;
1.4748 + boolean result = body.match(matcher, i, seq);
1.4749 + // If match failed we must backtrack, so
1.4750 + // the loop count should NOT be incremented
1.4751 + if (!result)
1.4752 + matcher.locals[countIndex] = count;
1.4753 + return result;
1.4754 + }
1.4755 + return false;
1.4756 + }
1.4757 + return next.match(matcher, i, seq);
1.4758 + }
1.4759 + boolean matchInit(Matcher matcher, int i, CharSequence seq) {
1.4760 + int save = matcher.locals[countIndex];
1.4761 + boolean ret = false;
1.4762 + if (0 < cmin) {
1.4763 + matcher.locals[countIndex] = 1;
1.4764 + ret = body.match(matcher, i, seq);
1.4765 + } else if (next.match(matcher, i, seq)) {
1.4766 + ret = true;
1.4767 + } else if (0 < cmax) {
1.4768 + matcher.locals[countIndex] = 1;
1.4769 + ret = body.match(matcher, i, seq);
1.4770 + }
1.4771 + matcher.locals[countIndex] = save;
1.4772 + return ret;
1.4773 + }
1.4774 + boolean study(TreeInfo info) {
1.4775 + info.maxValid = false;
1.4776 + info.deterministic = false;
1.4777 + return false;
1.4778 + }
1.4779 + }
1.4780 +
1.4781 + /**
1.4782 + * Refers to a group in the regular expression. Attempts to match
1.4783 + * whatever the group referred to last matched.
1.4784 + */
1.4785 + static class BackRef extends Node {
1.4786 + int groupIndex;
1.4787 + BackRef(int groupCount) {
1.4788 + super();
1.4789 + groupIndex = groupCount + groupCount;
1.4790 + }
1.4791 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4792 + int j = matcher.groups[groupIndex];
1.4793 + int k = matcher.groups[groupIndex+1];
1.4794 +
1.4795 + int groupSize = k - j;
1.4796 +
1.4797 + // If the referenced group didn't match, neither can this
1.4798 + if (j < 0)
1.4799 + return false;
1.4800 +
1.4801 + // If there isn't enough input left no match
1.4802 + if (i + groupSize > matcher.to) {
1.4803 + matcher.hitEnd = true;
1.4804 + return false;
1.4805 + }
1.4806 +
1.4807 + // Check each new char to make sure it matches what the group
1.4808 + // referenced matched last time around
1.4809 + for (int index=0; index<groupSize; index++)
1.4810 + if (seq.charAt(i+index) != seq.charAt(j+index))
1.4811 + return false;
1.4812 +
1.4813 + return next.match(matcher, i+groupSize, seq);
1.4814 + }
1.4815 + boolean study(TreeInfo info) {
1.4816 + info.maxValid = false;
1.4817 + return next.study(info);
1.4818 + }
1.4819 + }
1.4820 +
1.4821 + static class CIBackRef extends Node {
1.4822 + int groupIndex;
1.4823 + boolean doUnicodeCase;
1.4824 + CIBackRef(int groupCount, boolean doUnicodeCase) {
1.4825 + super();
1.4826 + groupIndex = groupCount + groupCount;
1.4827 + this.doUnicodeCase = doUnicodeCase;
1.4828 + }
1.4829 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4830 + int j = matcher.groups[groupIndex];
1.4831 + int k = matcher.groups[groupIndex+1];
1.4832 +
1.4833 + int groupSize = k - j;
1.4834 +
1.4835 + // If the referenced group didn't match, neither can this
1.4836 + if (j < 0)
1.4837 + return false;
1.4838 +
1.4839 + // If there isn't enough input left no match
1.4840 + if (i + groupSize > matcher.to) {
1.4841 + matcher.hitEnd = true;
1.4842 + return false;
1.4843 + }
1.4844 +
1.4845 + // Check each new char to make sure it matches what the group
1.4846 + // referenced matched last time around
1.4847 + int x = i;
1.4848 + for (int index=0; index<groupSize; index++) {
1.4849 + int c1 = Character.codePointAt(seq, x);
1.4850 + int c2 = Character.codePointAt(seq, j);
1.4851 + if (c1 != c2) {
1.4852 + if (doUnicodeCase) {
1.4853 + int cc1 = Character.toUpperCase(c1);
1.4854 + int cc2 = Character.toUpperCase(c2);
1.4855 + if (cc1 != cc2 &&
1.4856 + Character.toLowerCase(cc1) !=
1.4857 + Character.toLowerCase(cc2))
1.4858 + return false;
1.4859 + } else {
1.4860 + if (ASCII.toLower(c1) != ASCII.toLower(c2))
1.4861 + return false;
1.4862 + }
1.4863 + }
1.4864 + x += Character.charCount(c1);
1.4865 + j += Character.charCount(c2);
1.4866 + }
1.4867 +
1.4868 + return next.match(matcher, i+groupSize, seq);
1.4869 + }
1.4870 + boolean study(TreeInfo info) {
1.4871 + info.maxValid = false;
1.4872 + return next.study(info);
1.4873 + }
1.4874 + }
1.4875 +
1.4876 + /**
1.4877 + * Searches until the next instance of its atom. This is useful for
1.4878 + * finding the atom efficiently without passing an instance of it
1.4879 + * (greedy problem) and without a lot of wasted search time (reluctant
1.4880 + * problem).
1.4881 + */
1.4882 + static final class First extends Node {
1.4883 + Node atom;
1.4884 + First(Node node) {
1.4885 + this.atom = BnM.optimize(node);
1.4886 + }
1.4887 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4888 + if (atom instanceof BnM) {
1.4889 + return atom.match(matcher, i, seq)
1.4890 + && next.match(matcher, matcher.last, seq);
1.4891 + }
1.4892 + for (;;) {
1.4893 + if (i > matcher.to) {
1.4894 + matcher.hitEnd = true;
1.4895 + return false;
1.4896 + }
1.4897 + if (atom.match(matcher, i, seq)) {
1.4898 + return next.match(matcher, matcher.last, seq);
1.4899 + }
1.4900 + i += countChars(seq, i, 1);
1.4901 + matcher.first++;
1.4902 + }
1.4903 + }
1.4904 + boolean study(TreeInfo info) {
1.4905 + atom.study(info);
1.4906 + info.maxValid = false;
1.4907 + info.deterministic = false;
1.4908 + return next.study(info);
1.4909 + }
1.4910 + }
1.4911 +
1.4912 + static final class Conditional extends Node {
1.4913 + Node cond, yes, not;
1.4914 + Conditional(Node cond, Node yes, Node not) {
1.4915 + this.cond = cond;
1.4916 + this.yes = yes;
1.4917 + this.not = not;
1.4918 + }
1.4919 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4920 + if (cond.match(matcher, i, seq)) {
1.4921 + return yes.match(matcher, i, seq);
1.4922 + } else {
1.4923 + return not.match(matcher, i, seq);
1.4924 + }
1.4925 + }
1.4926 + boolean study(TreeInfo info) {
1.4927 + int minL = info.minLength;
1.4928 + int maxL = info.maxLength;
1.4929 + boolean maxV = info.maxValid;
1.4930 + info.reset();
1.4931 + yes.study(info);
1.4932 +
1.4933 + int minL2 = info.minLength;
1.4934 + int maxL2 = info.maxLength;
1.4935 + boolean maxV2 = info.maxValid;
1.4936 + info.reset();
1.4937 + not.study(info);
1.4938 +
1.4939 + info.minLength = minL + Math.min(minL2, info.minLength);
1.4940 + info.maxLength = maxL + Math.max(maxL2, info.maxLength);
1.4941 + info.maxValid = (maxV & maxV2 & info.maxValid);
1.4942 + info.deterministic = false;
1.4943 + return next.study(info);
1.4944 + }
1.4945 + }
1.4946 +
1.4947 + /**
1.4948 + * Zero width positive lookahead.
1.4949 + */
1.4950 + static final class Pos extends Node {
1.4951 + Node cond;
1.4952 + Pos(Node cond) {
1.4953 + this.cond = cond;
1.4954 + }
1.4955 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4956 + int savedTo = matcher.to;
1.4957 + boolean conditionMatched = false;
1.4958 +
1.4959 + // Relax transparent region boundaries for lookahead
1.4960 + if (matcher.transparentBounds)
1.4961 + matcher.to = matcher.getTextLength();
1.4962 + try {
1.4963 + conditionMatched = cond.match(matcher, i, seq);
1.4964 + } finally {
1.4965 + // Reinstate region boundaries
1.4966 + matcher.to = savedTo;
1.4967 + }
1.4968 + return conditionMatched && next.match(matcher, i, seq);
1.4969 + }
1.4970 + }
1.4971 +
1.4972 + /**
1.4973 + * Zero width negative lookahead.
1.4974 + */
1.4975 + static final class Neg extends Node {
1.4976 + Node cond;
1.4977 + Neg(Node cond) {
1.4978 + this.cond = cond;
1.4979 + }
1.4980 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.4981 + int savedTo = matcher.to;
1.4982 + boolean conditionMatched = false;
1.4983 +
1.4984 + // Relax transparent region boundaries for lookahead
1.4985 + if (matcher.transparentBounds)
1.4986 + matcher.to = matcher.getTextLength();
1.4987 + try {
1.4988 + if (i < matcher.to) {
1.4989 + conditionMatched = !cond.match(matcher, i, seq);
1.4990 + } else {
1.4991 + // If a negative lookahead succeeds then more input
1.4992 + // could cause it to fail!
1.4993 + matcher.requireEnd = true;
1.4994 + conditionMatched = !cond.match(matcher, i, seq);
1.4995 + }
1.4996 + } finally {
1.4997 + // Reinstate region boundaries
1.4998 + matcher.to = savedTo;
1.4999 + }
1.5000 + return conditionMatched && next.match(matcher, i, seq);
1.5001 + }
1.5002 + }
1.5003 +
1.5004 + /**
1.5005 + * For use with lookbehinds; matches the position where the lookbehind
1.5006 + * was encountered.
1.5007 + */
1.5008 + static Node lookbehindEnd = new Node() {
1.5009 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.5010 + return i == matcher.lookbehindTo;
1.5011 + }
1.5012 + };
1.5013 +
1.5014 + /**
1.5015 + * Zero width positive lookbehind.
1.5016 + */
1.5017 + static class Behind extends Node {
1.5018 + Node cond;
1.5019 + int rmax, rmin;
1.5020 + Behind(Node cond, int rmax, int rmin) {
1.5021 + this.cond = cond;
1.5022 + this.rmax = rmax;
1.5023 + this.rmin = rmin;
1.5024 + }
1.5025 +
1.5026 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.5027 + int savedFrom = matcher.from;
1.5028 + boolean conditionMatched = false;
1.5029 + int startIndex = (!matcher.transparentBounds) ?
1.5030 + matcher.from : 0;
1.5031 + int from = Math.max(i - rmax, startIndex);
1.5032 + // Set end boundary
1.5033 + int savedLBT = matcher.lookbehindTo;
1.5034 + matcher.lookbehindTo = i;
1.5035 + // Relax transparent region boundaries for lookbehind
1.5036 + if (matcher.transparentBounds)
1.5037 + matcher.from = 0;
1.5038 + for (int j = i - rmin; !conditionMatched && j >= from; j--) {
1.5039 + conditionMatched = cond.match(matcher, j, seq);
1.5040 + }
1.5041 + matcher.from = savedFrom;
1.5042 + matcher.lookbehindTo = savedLBT;
1.5043 + return conditionMatched && next.match(matcher, i, seq);
1.5044 + }
1.5045 + }
1.5046 +
1.5047 + /**
1.5048 + * Zero width positive lookbehind, including supplementary
1.5049 + * characters or unpaired surrogates.
1.5050 + */
1.5051 + static final class BehindS extends Behind {
1.5052 + BehindS(Node cond, int rmax, int rmin) {
1.5053 + super(cond, rmax, rmin);
1.5054 + }
1.5055 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.5056 + int rmaxChars = countChars(seq, i, -rmax);
1.5057 + int rminChars = countChars(seq, i, -rmin);
1.5058 + int savedFrom = matcher.from;
1.5059 + int startIndex = (!matcher.transparentBounds) ?
1.5060 + matcher.from : 0;
1.5061 + boolean conditionMatched = false;
1.5062 + int from = Math.max(i - rmaxChars, startIndex);
1.5063 + // Set end boundary
1.5064 + int savedLBT = matcher.lookbehindTo;
1.5065 + matcher.lookbehindTo = i;
1.5066 + // Relax transparent region boundaries for lookbehind
1.5067 + if (matcher.transparentBounds)
1.5068 + matcher.from = 0;
1.5069 +
1.5070 + for (int j = i - rminChars;
1.5071 + !conditionMatched && j >= from;
1.5072 + j -= j>from ? countChars(seq, j, -1) : 1) {
1.5073 + conditionMatched = cond.match(matcher, j, seq);
1.5074 + }
1.5075 + matcher.from = savedFrom;
1.5076 + matcher.lookbehindTo = savedLBT;
1.5077 + return conditionMatched && next.match(matcher, i, seq);
1.5078 + }
1.5079 + }
1.5080 +
1.5081 + /**
1.5082 + * Zero width negative lookbehind.
1.5083 + */
1.5084 + static class NotBehind extends Node {
1.5085 + Node cond;
1.5086 + int rmax, rmin;
1.5087 + NotBehind(Node cond, int rmax, int rmin) {
1.5088 + this.cond = cond;
1.5089 + this.rmax = rmax;
1.5090 + this.rmin = rmin;
1.5091 + }
1.5092 +
1.5093 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.5094 + int savedLBT = matcher.lookbehindTo;
1.5095 + int savedFrom = matcher.from;
1.5096 + boolean conditionMatched = false;
1.5097 + int startIndex = (!matcher.transparentBounds) ?
1.5098 + matcher.from : 0;
1.5099 + int from = Math.max(i - rmax, startIndex);
1.5100 + matcher.lookbehindTo = i;
1.5101 + // Relax transparent region boundaries for lookbehind
1.5102 + if (matcher.transparentBounds)
1.5103 + matcher.from = 0;
1.5104 + for (int j = i - rmin; !conditionMatched && j >= from; j--) {
1.5105 + conditionMatched = cond.match(matcher, j, seq);
1.5106 + }
1.5107 + // Reinstate region boundaries
1.5108 + matcher.from = savedFrom;
1.5109 + matcher.lookbehindTo = savedLBT;
1.5110 + return !conditionMatched && next.match(matcher, i, seq);
1.5111 + }
1.5112 + }
1.5113 +
1.5114 + /**
1.5115 + * Zero width negative lookbehind, including supplementary
1.5116 + * characters or unpaired surrogates.
1.5117 + */
1.5118 + static final class NotBehindS extends NotBehind {
1.5119 + NotBehindS(Node cond, int rmax, int rmin) {
1.5120 + super(cond, rmax, rmin);
1.5121 + }
1.5122 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.5123 + int rmaxChars = countChars(seq, i, -rmax);
1.5124 + int rminChars = countChars(seq, i, -rmin);
1.5125 + int savedFrom = matcher.from;
1.5126 + int savedLBT = matcher.lookbehindTo;
1.5127 + boolean conditionMatched = false;
1.5128 + int startIndex = (!matcher.transparentBounds) ?
1.5129 + matcher.from : 0;
1.5130 + int from = Math.max(i - rmaxChars, startIndex);
1.5131 + matcher.lookbehindTo = i;
1.5132 + // Relax transparent region boundaries for lookbehind
1.5133 + if (matcher.transparentBounds)
1.5134 + matcher.from = 0;
1.5135 + for (int j = i - rminChars;
1.5136 + !conditionMatched && j >= from;
1.5137 + j -= j>from ? countChars(seq, j, -1) : 1) {
1.5138 + conditionMatched = cond.match(matcher, j, seq);
1.5139 + }
1.5140 + //Reinstate region boundaries
1.5141 + matcher.from = savedFrom;
1.5142 + matcher.lookbehindTo = savedLBT;
1.5143 + return !conditionMatched && next.match(matcher, i, seq);
1.5144 + }
1.5145 + }
1.5146 +
1.5147 + /**
1.5148 + * Returns the set union of two CharProperty nodes.
1.5149 + */
1.5150 + private static CharProperty union(final CharProperty lhs,
1.5151 + final CharProperty rhs) {
1.5152 + return new CharProperty() {
1.5153 + boolean isSatisfiedBy(int ch) {
1.5154 + return lhs.isSatisfiedBy(ch) || rhs.isSatisfiedBy(ch);}};
1.5155 + }
1.5156 +
1.5157 + /**
1.5158 + * Returns the set intersection of two CharProperty nodes.
1.5159 + */
1.5160 + private static CharProperty intersection(final CharProperty lhs,
1.5161 + final CharProperty rhs) {
1.5162 + return new CharProperty() {
1.5163 + boolean isSatisfiedBy(int ch) {
1.5164 + return lhs.isSatisfiedBy(ch) && rhs.isSatisfiedBy(ch);}};
1.5165 + }
1.5166 +
1.5167 + /**
1.5168 + * Returns the set difference of two CharProperty nodes.
1.5169 + */
1.5170 + private static CharProperty setDifference(final CharProperty lhs,
1.5171 + final CharProperty rhs) {
1.5172 + return new CharProperty() {
1.5173 + boolean isSatisfiedBy(int ch) {
1.5174 + return ! rhs.isSatisfiedBy(ch) && lhs.isSatisfiedBy(ch);}};
1.5175 + }
1.5176 +
1.5177 + /**
1.5178 + * Handles word boundaries. Includes a field to allow this one class to
1.5179 + * deal with the different types of word boundaries we can match. The word
1.5180 + * characters include underscores, letters, and digits. Non spacing marks
1.5181 + * can are also part of a word if they have a base character, otherwise
1.5182 + * they are ignored for purposes of finding word boundaries.
1.5183 + */
1.5184 + static final class Bound extends Node {
1.5185 + static int LEFT = 0x1;
1.5186 + static int RIGHT= 0x2;
1.5187 + static int BOTH = 0x3;
1.5188 + static int NONE = 0x4;
1.5189 + int type;
1.5190 + boolean useUWORD;
1.5191 + Bound(int n, boolean useUWORD) {
1.5192 + type = n;
1.5193 + this.useUWORD = useUWORD;
1.5194 + }
1.5195 +
1.5196 + boolean isWord(int ch) {
1.5197 + return useUWORD ? UnicodeProp.WORD.is(ch)
1.5198 + : (ch == '_' || Character.isLetterOrDigit(ch));
1.5199 + }
1.5200 +
1.5201 + int check(Matcher matcher, int i, CharSequence seq) {
1.5202 + int ch;
1.5203 + boolean left = false;
1.5204 + int startIndex = matcher.from;
1.5205 + int endIndex = matcher.to;
1.5206 + if (matcher.transparentBounds) {
1.5207 + startIndex = 0;
1.5208 + endIndex = matcher.getTextLength();
1.5209 + }
1.5210 + if (i > startIndex) {
1.5211 + ch = Character.codePointBefore(seq, i);
1.5212 + left = (isWord(ch) ||
1.5213 + ((Character.getType(ch) == Character.NON_SPACING_MARK)
1.5214 + && hasBaseCharacter(matcher, i-1, seq)));
1.5215 + }
1.5216 + boolean right = false;
1.5217 + if (i < endIndex) {
1.5218 + ch = Character.codePointAt(seq, i);
1.5219 + right = (isWord(ch) ||
1.5220 + ((Character.getType(ch) == Character.NON_SPACING_MARK)
1.5221 + && hasBaseCharacter(matcher, i, seq)));
1.5222 + } else {
1.5223 + // Tried to access char past the end
1.5224 + matcher.hitEnd = true;
1.5225 + // The addition of another char could wreck a boundary
1.5226 + matcher.requireEnd = true;
1.5227 + }
1.5228 + return ((left ^ right) ? (right ? LEFT : RIGHT) : NONE);
1.5229 + }
1.5230 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.5231 + return (check(matcher, i, seq) & type) > 0
1.5232 + && next.match(matcher, i, seq);
1.5233 + }
1.5234 + }
1.5235 +
1.5236 + /**
1.5237 + * Non spacing marks only count as word characters in bounds calculations
1.5238 + * if they have a base character.
1.5239 + */
1.5240 + private static boolean hasBaseCharacter(Matcher matcher, int i,
1.5241 + CharSequence seq)
1.5242 + {
1.5243 + int start = (!matcher.transparentBounds) ?
1.5244 + matcher.from : 0;
1.5245 + for (int x=i; x >= start; x--) {
1.5246 + int ch = Character.codePointAt(seq, x);
1.5247 + if (Character.isLetterOrDigit(ch))
1.5248 + return true;
1.5249 + if (Character.getType(ch) == Character.NON_SPACING_MARK)
1.5250 + continue;
1.5251 + return false;
1.5252 + }
1.5253 + return false;
1.5254 + }
1.5255 +
1.5256 + /**
1.5257 + * Attempts to match a slice in the input using the Boyer-Moore string
1.5258 + * matching algorithm. The algorithm is based on the idea that the
1.5259 + * pattern can be shifted farther ahead in the search text if it is
1.5260 + * matched right to left.
1.5261 + * <p>
1.5262 + * The pattern is compared to the input one character at a time, from
1.5263 + * the rightmost character in the pattern to the left. If the characters
1.5264 + * all match the pattern has been found. If a character does not match,
1.5265 + * the pattern is shifted right a distance that is the maximum of two
1.5266 + * functions, the bad character shift and the good suffix shift. This
1.5267 + * shift moves the attempted match position through the input more
1.5268 + * quickly than a naive one position at a time check.
1.5269 + * <p>
1.5270 + * The bad character shift is based on the character from the text that
1.5271 + * did not match. If the character does not appear in the pattern, the
1.5272 + * pattern can be shifted completely beyond the bad character. If the
1.5273 + * character does occur in the pattern, the pattern can be shifted to
1.5274 + * line the pattern up with the next occurrence of that character.
1.5275 + * <p>
1.5276 + * The good suffix shift is based on the idea that some subset on the right
1.5277 + * side of the pattern has matched. When a bad character is found, the
1.5278 + * pattern can be shifted right by the pattern length if the subset does
1.5279 + * not occur again in pattern, or by the amount of distance to the
1.5280 + * next occurrence of the subset in the pattern.
1.5281 + *
1.5282 + * Boyer-Moore search methods adapted from code by Amy Yu.
1.5283 + */
1.5284 + static class BnM extends Node {
1.5285 + int[] buffer;
1.5286 + int[] lastOcc;
1.5287 + int[] optoSft;
1.5288 +
1.5289 + /**
1.5290 + * Pre calculates arrays needed to generate the bad character
1.5291 + * shift and the good suffix shift. Only the last seven bits
1.5292 + * are used to see if chars match; This keeps the tables small
1.5293 + * and covers the heavily used ASCII range, but occasionally
1.5294 + * results in an aliased match for the bad character shift.
1.5295 + */
1.5296 + static Node optimize(Node node) {
1.5297 + if (!(node instanceof Slice)) {
1.5298 + return node;
1.5299 + }
1.5300 +
1.5301 + int[] src = ((Slice) node).buffer;
1.5302 + int patternLength = src.length;
1.5303 + // The BM algorithm requires a bit of overhead;
1.5304 + // If the pattern is short don't use it, since
1.5305 + // a shift larger than the pattern length cannot
1.5306 + // be used anyway.
1.5307 + if (patternLength < 4) {
1.5308 + return node;
1.5309 + }
1.5310 + int i, j, k;
1.5311 + int[] lastOcc = new int[128];
1.5312 + int[] optoSft = new int[patternLength];
1.5313 + // Precalculate part of the bad character shift
1.5314 + // It is a table for where in the pattern each
1.5315 + // lower 7-bit value occurs
1.5316 + for (i = 0; i < patternLength; i++) {
1.5317 + lastOcc[src[i]&0x7F] = i + 1;
1.5318 + }
1.5319 + // Precalculate the good suffix shift
1.5320 + // i is the shift amount being considered
1.5321 +NEXT: for (i = patternLength; i > 0; i--) {
1.5322 + // j is the beginning index of suffix being considered
1.5323 + for (j = patternLength - 1; j >= i; j--) {
1.5324 + // Testing for good suffix
1.5325 + if (src[j] == src[j-i]) {
1.5326 + // src[j..len] is a good suffix
1.5327 + optoSft[j-1] = i;
1.5328 + } else {
1.5329 + // No match. The array has already been
1.5330 + // filled up with correct values before.
1.5331 + continue NEXT;
1.5332 + }
1.5333 + }
1.5334 + // This fills up the remaining of optoSft
1.5335 + // any suffix can not have larger shift amount
1.5336 + // then its sub-suffix. Why???
1.5337 + while (j > 0) {
1.5338 + optoSft[--j] = i;
1.5339 + }
1.5340 + }
1.5341 + // Set the guard value because of unicode compression
1.5342 + optoSft[patternLength-1] = 1;
1.5343 + if (node instanceof SliceS)
1.5344 + return new BnMS(src, lastOcc, optoSft, node.next);
1.5345 + return new BnM(src, lastOcc, optoSft, node.next);
1.5346 + }
1.5347 + BnM(int[] src, int[] lastOcc, int[] optoSft, Node next) {
1.5348 + this.buffer = src;
1.5349 + this.lastOcc = lastOcc;
1.5350 + this.optoSft = optoSft;
1.5351 + this.next = next;
1.5352 + }
1.5353 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.5354 + int[] src = buffer;
1.5355 + int patternLength = src.length;
1.5356 + int last = matcher.to - patternLength;
1.5357 +
1.5358 + // Loop over all possible match positions in text
1.5359 +NEXT: while (i <= last) {
1.5360 + // Loop over pattern from right to left
1.5361 + for (int j = patternLength - 1; j >= 0; j--) {
1.5362 + int ch = seq.charAt(i+j);
1.5363 + if (ch != src[j]) {
1.5364 + // Shift search to the right by the maximum of the
1.5365 + // bad character shift and the good suffix shift
1.5366 + i += Math.max(j + 1 - lastOcc[ch&0x7F], optoSft[j]);
1.5367 + continue NEXT;
1.5368 + }
1.5369 + }
1.5370 + // Entire pattern matched starting at i
1.5371 + matcher.first = i;
1.5372 + boolean ret = next.match(matcher, i + patternLength, seq);
1.5373 + if (ret) {
1.5374 + matcher.first = i;
1.5375 + matcher.groups[0] = matcher.first;
1.5376 + matcher.groups[1] = matcher.last;
1.5377 + return true;
1.5378 + }
1.5379 + i++;
1.5380 + }
1.5381 + // BnM is only used as the leading node in the unanchored case,
1.5382 + // and it replaced its Start() which always searches to the end
1.5383 + // if it doesn't find what it's looking for, so hitEnd is true.
1.5384 + matcher.hitEnd = true;
1.5385 + return false;
1.5386 + }
1.5387 + boolean study(TreeInfo info) {
1.5388 + info.minLength += buffer.length;
1.5389 + info.maxValid = false;
1.5390 + return next.study(info);
1.5391 + }
1.5392 + }
1.5393 +
1.5394 + /**
1.5395 + * Supplementary support version of BnM(). Unpaired surrogates are
1.5396 + * also handled by this class.
1.5397 + */
1.5398 + static final class BnMS extends BnM {
1.5399 + int lengthInChars;
1.5400 +
1.5401 + BnMS(int[] src, int[] lastOcc, int[] optoSft, Node next) {
1.5402 + super(src, lastOcc, optoSft, next);
1.5403 + for (int x = 0; x < buffer.length; x++) {
1.5404 + lengthInChars += Character.charCount(buffer[x]);
1.5405 + }
1.5406 + }
1.5407 + boolean match(Matcher matcher, int i, CharSequence seq) {
1.5408 + int[] src = buffer;
1.5409 + int patternLength = src.length;
1.5410 + int last = matcher.to - lengthInChars;
1.5411 +
1.5412 + // Loop over all possible match positions in text
1.5413 +NEXT: while (i <= last) {
1.5414 + // Loop over pattern from right to left
1.5415 + int ch;
1.5416 + for (int j = countChars(seq, i, patternLength), x = patternLength - 1;
1.5417 + j > 0; j -= Character.charCount(ch), x--) {
1.5418 + ch = Character.codePointBefore(seq, i+j);
1.5419 + if (ch != src[x]) {
1.5420 + // Shift search to the right by the maximum of the
1.5421 + // bad character shift and the good suffix shift
1.5422 + int n = Math.max(x + 1 - lastOcc[ch&0x7F], optoSft[x]);
1.5423 + i += countChars(seq, i, n);
1.5424 + continue NEXT;
1.5425 + }
1.5426 + }
1.5427 + // Entire pattern matched starting at i
1.5428 + matcher.first = i;
1.5429 + boolean ret = next.match(matcher, i + lengthInChars, seq);
1.5430 + if (ret) {
1.5431 + matcher.first = i;
1.5432 + matcher.groups[0] = matcher.first;
1.5433 + matcher.groups[1] = matcher.last;
1.5434 + return true;
1.5435 + }
1.5436 + i += countChars(seq, i, 1);
1.5437 + }
1.5438 + matcher.hitEnd = true;
1.5439 + return false;
1.5440 + }
1.5441 + }
1.5442 +
1.5443 +///////////////////////////////////////////////////////////////////////////////
1.5444 +///////////////////////////////////////////////////////////////////////////////
1.5445 +
1.5446 + /**
1.5447 + * This must be the very first initializer.
1.5448 + */
1.5449 + static Node accept = new Node();
1.5450 +
1.5451 + static Node lastAccept = new LastNode();
1.5452 +
1.5453 + private static class CharPropertyNames {
1.5454 +
1.5455 + static CharProperty charPropertyFor(String name) {
1.5456 + CharPropertyFactory m = map.get(name);
1.5457 + return m == null ? null : m.make();
1.5458 + }
1.5459 +
1.5460 + private static abstract class CharPropertyFactory {
1.5461 + abstract CharProperty make();
1.5462 + }
1.5463 +
1.5464 + private static void defCategory(String name,
1.5465 + final int typeMask) {
1.5466 + map.put(name, new CharPropertyFactory() {
1.5467 + CharProperty make() { return new Category(typeMask);}});
1.5468 + }
1.5469 +
1.5470 + private static void defRange(String name,
1.5471 + final int lower, final int upper) {
1.5472 + map.put(name, new CharPropertyFactory() {
1.5473 + CharProperty make() { return rangeFor(lower, upper);}});
1.5474 + }
1.5475 +
1.5476 + private static void defCtype(String name,
1.5477 + final int ctype) {
1.5478 + map.put(name, new CharPropertyFactory() {
1.5479 + CharProperty make() { return new Ctype(ctype);}});
1.5480 + }
1.5481 +
1.5482 + private static abstract class CloneableProperty
1.5483 + extends CharProperty implements Cloneable
1.5484 + {
1.5485 + public CloneableProperty clone() {
1.5486 + try {
1.5487 + return (CloneableProperty) super.clone();
1.5488 + } catch (CloneNotSupportedException e) {
1.5489 + throw new AssertionError(e);
1.5490 + }
1.5491 + }
1.5492 + }
1.5493 +
1.5494 + private static void defClone(String name,
1.5495 + final CloneableProperty p) {
1.5496 + map.put(name, new CharPropertyFactory() {
1.5497 + CharProperty make() { return p.clone();}});
1.5498 + }
1.5499 +
1.5500 + private static final HashMap<String, CharPropertyFactory> map
1.5501 + = new HashMap<>();
1.5502 +
1.5503 + static {
1.5504 + // Unicode character property aliases, defined in
1.5505 + // http://www.unicode.org/Public/UNIDATA/PropertyValueAliases.txt
1.5506 + defCategory("Cn", 1<<Character.UNASSIGNED);
1.5507 + defCategory("Lu", 1<<Character.UPPERCASE_LETTER);
1.5508 + defCategory("Ll", 1<<Character.LOWERCASE_LETTER);
1.5509 + defCategory("Lt", 1<<Character.TITLECASE_LETTER);
1.5510 + defCategory("Lm", 1<<Character.MODIFIER_LETTER);
1.5511 + defCategory("Lo", 1<<Character.OTHER_LETTER);
1.5512 + defCategory("Mn", 1<<Character.NON_SPACING_MARK);
1.5513 + defCategory("Me", 1<<Character.ENCLOSING_MARK);
1.5514 + defCategory("Mc", 1<<Character.COMBINING_SPACING_MARK);
1.5515 + defCategory("Nd", 1<<Character.DECIMAL_DIGIT_NUMBER);
1.5516 + defCategory("Nl", 1<<Character.LETTER_NUMBER);
1.5517 + defCategory("No", 1<<Character.OTHER_NUMBER);
1.5518 + defCategory("Zs", 1<<Character.SPACE_SEPARATOR);
1.5519 + defCategory("Zl", 1<<Character.LINE_SEPARATOR);
1.5520 + defCategory("Zp", 1<<Character.PARAGRAPH_SEPARATOR);
1.5521 + defCategory("Cc", 1<<Character.CONTROL);
1.5522 + defCategory("Cf", 1<<Character.FORMAT);
1.5523 + defCategory("Co", 1<<Character.PRIVATE_USE);
1.5524 + defCategory("Cs", 1<<Character.SURROGATE);
1.5525 + defCategory("Pd", 1<<Character.DASH_PUNCTUATION);
1.5526 + defCategory("Ps", 1<<Character.START_PUNCTUATION);
1.5527 + defCategory("Pe", 1<<Character.END_PUNCTUATION);
1.5528 + defCategory("Pc", 1<<Character.CONNECTOR_PUNCTUATION);
1.5529 + defCategory("Po", 1<<Character.OTHER_PUNCTUATION);
1.5530 + defCategory("Sm", 1<<Character.MATH_SYMBOL);
1.5531 + defCategory("Sc", 1<<Character.CURRENCY_SYMBOL);
1.5532 + defCategory("Sk", 1<<Character.MODIFIER_SYMBOL);
1.5533 + defCategory("So", 1<<Character.OTHER_SYMBOL);
1.5534 + defCategory("Pi", 1<<Character.INITIAL_QUOTE_PUNCTUATION);
1.5535 + defCategory("Pf", 1<<Character.FINAL_QUOTE_PUNCTUATION);
1.5536 + defCategory("L", ((1<<Character.UPPERCASE_LETTER) |
1.5537 + (1<<Character.LOWERCASE_LETTER) |
1.5538 + (1<<Character.TITLECASE_LETTER) |
1.5539 + (1<<Character.MODIFIER_LETTER) |
1.5540 + (1<<Character.OTHER_LETTER)));
1.5541 + defCategory("M", ((1<<Character.NON_SPACING_MARK) |
1.5542 + (1<<Character.ENCLOSING_MARK) |
1.5543 + (1<<Character.COMBINING_SPACING_MARK)));
1.5544 + defCategory("N", ((1<<Character.DECIMAL_DIGIT_NUMBER) |
1.5545 + (1<<Character.LETTER_NUMBER) |
1.5546 + (1<<Character.OTHER_NUMBER)));
1.5547 + defCategory("Z", ((1<<Character.SPACE_SEPARATOR) |
1.5548 + (1<<Character.LINE_SEPARATOR) |
1.5549 + (1<<Character.PARAGRAPH_SEPARATOR)));
1.5550 + defCategory("C", ((1<<Character.CONTROL) |
1.5551 + (1<<Character.FORMAT) |
1.5552 + (1<<Character.PRIVATE_USE) |
1.5553 + (1<<Character.SURROGATE))); // Other
1.5554 + defCategory("P", ((1<<Character.DASH_PUNCTUATION) |
1.5555 + (1<<Character.START_PUNCTUATION) |
1.5556 + (1<<Character.END_PUNCTUATION) |
1.5557 + (1<<Character.CONNECTOR_PUNCTUATION) |
1.5558 + (1<<Character.OTHER_PUNCTUATION) |
1.5559 + (1<<Character.INITIAL_QUOTE_PUNCTUATION) |
1.5560 + (1<<Character.FINAL_QUOTE_PUNCTUATION)));
1.5561 + defCategory("S", ((1<<Character.MATH_SYMBOL) |
1.5562 + (1<<Character.CURRENCY_SYMBOL) |
1.5563 + (1<<Character.MODIFIER_SYMBOL) |
1.5564 + (1<<Character.OTHER_SYMBOL)));
1.5565 + defCategory("LC", ((1<<Character.UPPERCASE_LETTER) |
1.5566 + (1<<Character.LOWERCASE_LETTER) |
1.5567 + (1<<Character.TITLECASE_LETTER)));
1.5568 + defCategory("LD", ((1<<Character.UPPERCASE_LETTER) |
1.5569 + (1<<Character.LOWERCASE_LETTER) |
1.5570 + (1<<Character.TITLECASE_LETTER) |
1.5571 + (1<<Character.MODIFIER_LETTER) |
1.5572 + (1<<Character.OTHER_LETTER) |
1.5573 + (1<<Character.DECIMAL_DIGIT_NUMBER)));
1.5574 + defRange("L1", 0x00, 0xFF); // Latin-1
1.5575 + map.put("all", new CharPropertyFactory() {
1.5576 + CharProperty make() { return new All(); }});
1.5577 +
1.5578 + // Posix regular expression character classes, defined in
1.5579 + // http://www.unix.org/onlinepubs/009695399/basedefs/xbd_chap09.html
1.5580 + defRange("ASCII", 0x00, 0x7F); // ASCII
1.5581 + defCtype("Alnum", ASCII.ALNUM); // Alphanumeric characters
1.5582 + defCtype("Alpha", ASCII.ALPHA); // Alphabetic characters
1.5583 + defCtype("Blank", ASCII.BLANK); // Space and tab characters
1.5584 + defCtype("Cntrl", ASCII.CNTRL); // Control characters
1.5585 + defRange("Digit", '0', '9'); // Numeric characters
1.5586 + defCtype("Graph", ASCII.GRAPH); // printable and visible
1.5587 + defRange("Lower", 'a', 'z'); // Lower-case alphabetic
1.5588 + defRange("Print", 0x20, 0x7E); // Printable characters
1.5589 + defCtype("Punct", ASCII.PUNCT); // Punctuation characters
1.5590 + defCtype("Space", ASCII.SPACE); // Space characters
1.5591 + defRange("Upper", 'A', 'Z'); // Upper-case alphabetic
1.5592 + defCtype("XDigit",ASCII.XDIGIT); // hexadecimal digits
1.5593 +
1.5594 + // Java character properties, defined by methods in Character.java
1.5595 + defClone("javaLowerCase", new CloneableProperty() {
1.5596 + boolean isSatisfiedBy(int ch) {
1.5597 + return Character.isLowerCase(ch);}});
1.5598 + defClone("javaUpperCase", new CloneableProperty() {
1.5599 + boolean isSatisfiedBy(int ch) {
1.5600 + return Character.isUpperCase(ch);}});
1.5601 + defClone("javaAlphabetic", new CloneableProperty() {
1.5602 + boolean isSatisfiedBy(int ch) {
1.5603 + return Character.isAlphabetic(ch);}});
1.5604 + defClone("javaIdeographic", new CloneableProperty() {
1.5605 + boolean isSatisfiedBy(int ch) {
1.5606 + return Character.isIdeographic(ch);}});
1.5607 + defClone("javaTitleCase", new CloneableProperty() {
1.5608 + boolean isSatisfiedBy(int ch) {
1.5609 + return Character.isTitleCase(ch);}});
1.5610 + defClone("javaDigit", new CloneableProperty() {
1.5611 + boolean isSatisfiedBy(int ch) {
1.5612 + return Character.isDigit(ch);}});
1.5613 + defClone("javaDefined", new CloneableProperty() {
1.5614 + boolean isSatisfiedBy(int ch) {
1.5615 + return Character.isDefined(ch);}});
1.5616 + defClone("javaLetter", new CloneableProperty() {
1.5617 + boolean isSatisfiedBy(int ch) {
1.5618 + return Character.isLetter(ch);}});
1.5619 + defClone("javaLetterOrDigit", new CloneableProperty() {
1.5620 + boolean isSatisfiedBy(int ch) {
1.5621 + return Character.isLetterOrDigit(ch);}});
1.5622 + defClone("javaJavaIdentifierStart", new CloneableProperty() {
1.5623 + boolean isSatisfiedBy(int ch) {
1.5624 + return Character.isJavaIdentifierStart(ch);}});
1.5625 + defClone("javaJavaIdentifierPart", new CloneableProperty() {
1.5626 + boolean isSatisfiedBy(int ch) {
1.5627 + return Character.isJavaIdentifierPart(ch);}});
1.5628 + defClone("javaUnicodeIdentifierStart", new CloneableProperty() {
1.5629 + boolean isSatisfiedBy(int ch) {
1.5630 + return Character.isUnicodeIdentifierStart(ch);}});
1.5631 + defClone("javaUnicodeIdentifierPart", new CloneableProperty() {
1.5632 + boolean isSatisfiedBy(int ch) {
1.5633 + return Character.isUnicodeIdentifierPart(ch);}});
1.5634 + defClone("javaIdentifierIgnorable", new CloneableProperty() {
1.5635 + boolean isSatisfiedBy(int ch) {
1.5636 + return Character.isIdentifierIgnorable(ch);}});
1.5637 + defClone("javaSpaceChar", new CloneableProperty() {
1.5638 + boolean isSatisfiedBy(int ch) {
1.5639 + return Character.isSpaceChar(ch);}});
1.5640 + defClone("javaWhitespace", new CloneableProperty() {
1.5641 + boolean isSatisfiedBy(int ch) {
1.5642 + return Character.isWhitespace(ch);}});
1.5643 + defClone("javaISOControl", new CloneableProperty() {
1.5644 + boolean isSatisfiedBy(int ch) {
1.5645 + return Character.isISOControl(ch);}});
1.5646 + defClone("javaMirrored", new CloneableProperty() {
1.5647 + boolean isSatisfiedBy(int ch) {
1.5648 + return Character.isMirrored(ch);}});
1.5649 + }
1.5650 + }
1.5651 +}