2 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
26 package java.util.regex;
28 import java.security.AccessController;
29 import java.security.PrivilegedAction;
30 import java.text.CharacterIterator;
31 import java.text.Normalizer;
32 import java.util.Locale;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.Arrays;
40 * A compiled representation of a regular expression.
42 * <p> A regular expression, specified as a string, must first be compiled into
43 * an instance of this class. The resulting pattern can then be used to create
44 * a {@link Matcher} object that can match arbitrary {@link
45 * java.lang.CharSequence </code>character sequences<code>} against the regular
46 * expression. All of the state involved in performing a match resides in the
47 * matcher, so many matchers can share the same pattern.
49 * <p> A typical invocation sequence is thus
52 * Pattern p = Pattern.{@link #compile compile}("a*b");
53 * Matcher m = p.{@link #matcher matcher}("aaaaab");
54 * boolean b = m.{@link Matcher#matches matches}();</pre></blockquote>
56 * <p> A {@link #matches matches} method is defined by this class as a
57 * convenience for when a regular expression is used just once. This method
58 * compiles an expression and matches an input sequence against it in a single
59 * invocation. The statement
62 * boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote>
64 * is equivalent to the three statements above, though for repeated matches it
65 * is less efficient since it does not allow the compiled pattern to be reused.
67 * <p> Instances of this class are immutable and are safe for use by multiple
68 * concurrent threads. Instances of the {@link Matcher} class are not safe for
73 * <h4> Summary of regular-expression constructs </h4>
75 * <table border="0" cellpadding="1" cellspacing="0"
76 * summary="Regular expression constructs, and what they match">
79 * <th bgcolor="#CCCCFF" align="left" id="construct">Construct</th>
80 * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
83 * <tr><th> </th></tr>
84 * <tr align="left"><th colspan="2" id="characters">Characters</th></tr>
86 * <tr><td valign="top" headers="construct characters"><i>x</i></td>
87 * <td headers="matches">The character <i>x</i></td></tr>
88 * <tr><td valign="top" headers="construct characters"><tt>\\</tt></td>
89 * <td headers="matches">The backslash character</td></tr>
90 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>n</i></td>
91 * <td headers="matches">The character with octal value <tt>0</tt><i>n</i>
92 * (0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr>
93 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>nn</i></td>
94 * <td headers="matches">The character with octal value <tt>0</tt><i>nn</i>
95 * (0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr>
96 * <tr><td valign="top" headers="construct characters"><tt>\0</tt><i>mnn</i></td>
97 * <td headers="matches">The character with octal value <tt>0</tt><i>mnn</i>
98 * (0 <tt><=</tt> <i>m</i> <tt><=</tt> 3,
99 * 0 <tt><=</tt> <i>n</i> <tt><=</tt> 7)</td></tr>
100 * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>hh</i></td>
101 * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>hh</i></td></tr>
102 * <tr><td valign="top" headers="construct characters"><tt>\u</tt><i>hhhh</i></td>
103 * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>hhhh</i></td></tr>
104 * <tr><td valign="top" headers="construct characters"><tt>\x</tt><i>{h...h}</i></td>
105 * <td headers="matches">The character with hexadecimal value <tt>0x</tt><i>h...h</i>
106 * ({@link java.lang.Character#MIN_CODE_POINT Character.MIN_CODE_POINT}
107 * <= <tt>0x</tt><i>h...h</i> <= 
108 * {@link java.lang.Character#MAX_CODE_POINT Character.MAX_CODE_POINT})</td></tr>
109 * <tr><td valign="top" headers="matches"><tt>\t</tt></td>
110 * <td headers="matches">The tab character (<tt>'\u0009'</tt>)</td></tr>
111 * <tr><td valign="top" headers="construct characters"><tt>\n</tt></td>
112 * <td headers="matches">The newline (line feed) character (<tt>'\u000A'</tt>)</td></tr>
113 * <tr><td valign="top" headers="construct characters"><tt>\r</tt></td>
114 * <td headers="matches">The carriage-return character (<tt>'\u000D'</tt>)</td></tr>
115 * <tr><td valign="top" headers="construct characters"><tt>\f</tt></td>
116 * <td headers="matches">The form-feed character (<tt>'\u000C'</tt>)</td></tr>
117 * <tr><td valign="top" headers="construct characters"><tt>\a</tt></td>
118 * <td headers="matches">The alert (bell) character (<tt>'\u0007'</tt>)</td></tr>
119 * <tr><td valign="top" headers="construct characters"><tt>\e</tt></td>
120 * <td headers="matches">The escape character (<tt>'\u001B'</tt>)</td></tr>
121 * <tr><td valign="top" headers="construct characters"><tt>\c</tt><i>x</i></td>
122 * <td headers="matches">The control character corresponding to <i>x</i></td></tr>
124 * <tr><th> </th></tr>
125 * <tr align="left"><th colspan="2" id="classes">Character classes</th></tr>
127 * <tr><td valign="top" headers="construct classes"><tt>[abc]</tt></td>
128 * <td headers="matches"><tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (simple class)</td></tr>
129 * <tr><td valign="top" headers="construct classes"><tt>[^abc]</tt></td>
130 * <td headers="matches">Any character except <tt>a</tt>, <tt>b</tt>, or <tt>c</tt> (negation)</td></tr>
131 * <tr><td valign="top" headers="construct classes"><tt>[a-zA-Z]</tt></td>
132 * <td headers="matches"><tt>a</tt> through <tt>z</tt>
133 * or <tt>A</tt> through <tt>Z</tt>, inclusive (range)</td></tr>
134 * <tr><td valign="top" headers="construct classes"><tt>[a-d[m-p]]</tt></td>
135 * <td headers="matches"><tt>a</tt> through <tt>d</tt>,
136 * or <tt>m</tt> through <tt>p</tt>: <tt>[a-dm-p]</tt> (union)</td></tr>
137 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[def]]</tt></td>
138 * <td headers="matches"><tt>d</tt>, <tt>e</tt>, or <tt>f</tt> (intersection)</tr>
139 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^bc]]</tt></td>
140 * <td headers="matches"><tt>a</tt> through <tt>z</tt>,
141 * except for <tt>b</tt> and <tt>c</tt>: <tt>[ad-z]</tt> (subtraction)</td></tr>
142 * <tr><td valign="top" headers="construct classes"><tt>[a-z&&[^m-p]]</tt></td>
143 * <td headers="matches"><tt>a</tt> through <tt>z</tt>,
144 * and not <tt>m</tt> through <tt>p</tt>: <tt>[a-lq-z]</tt>(subtraction)</td></tr>
145 * <tr><th> </th></tr>
147 * <tr align="left"><th colspan="2" id="predef">Predefined character classes</th></tr>
149 * <tr><td valign="top" headers="construct predef"><tt>.</tt></td>
150 * <td headers="matches">Any character (may or may not match <a href="#lt">line terminators</a>)</td></tr>
151 * <tr><td valign="top" headers="construct predef"><tt>\d</tt></td>
152 * <td headers="matches">A digit: <tt>[0-9]</tt></td></tr>
153 * <tr><td valign="top" headers="construct predef"><tt>\D</tt></td>
154 * <td headers="matches">A non-digit: <tt>[^0-9]</tt></td></tr>
155 * <tr><td valign="top" headers="construct predef"><tt>\s</tt></td>
156 * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
157 * <tr><td valign="top" headers="construct predef"><tt>\S</tt></td>
158 * <td headers="matches">A non-whitespace character: <tt>[^\s]</tt></td></tr>
159 * <tr><td valign="top" headers="construct predef"><tt>\w</tt></td>
160 * <td headers="matches">A word character: <tt>[a-zA-Z_0-9]</tt></td></tr>
161 * <tr><td valign="top" headers="construct predef"><tt>\W</tt></td>
162 * <td headers="matches">A non-word character: <tt>[^\w]</tt></td></tr>
164 * <tr><th> </th></tr>
165 * <tr align="left"><th colspan="2" id="posix">POSIX character classes</b> (US-ASCII only)<b></th></tr>
167 * <tr><td valign="top" headers="construct posix"><tt>\p{Lower}</tt></td>
168 * <td headers="matches">A lower-case alphabetic character: <tt>[a-z]</tt></td></tr>
169 * <tr><td valign="top" headers="construct posix"><tt>\p{Upper}</tt></td>
170 * <td headers="matches">An upper-case alphabetic character:<tt>[A-Z]</tt></td></tr>
171 * <tr><td valign="top" headers="construct posix"><tt>\p{ASCII}</tt></td>
172 * <td headers="matches">All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
173 * <tr><td valign="top" headers="construct posix"><tt>\p{Alpha}</tt></td>
174 * <td headers="matches">An alphabetic character:<tt>[\p{Lower}\p{Upper}]</tt></td></tr>
175 * <tr><td valign="top" headers="construct posix"><tt>\p{Digit}</tt></td>
176 * <td headers="matches">A decimal digit: <tt>[0-9]</tt></td></tr>
177 * <tr><td valign="top" headers="construct posix"><tt>\p{Alnum}</tt></td>
178 * <td headers="matches">An alphanumeric character:<tt>[\p{Alpha}\p{Digit}]</tt></td></tr>
179 * <tr><td valign="top" headers="construct posix"><tt>\p{Punct}</tt></td>
180 * <td headers="matches">Punctuation: One of <tt>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~</tt></td></tr>
181 * <!-- <tt>[\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]</tt>
182 * <tt>[\X21-\X2F\X31-\X40\X5B-\X60\X7B-\X7E]</tt> -->
183 * <tr><td valign="top" headers="construct posix"><tt>\p{Graph}</tt></td>
184 * <td headers="matches">A visible character: <tt>[\p{Alnum}\p{Punct}]</tt></td></tr>
185 * <tr><td valign="top" headers="construct posix"><tt>\p{Print}</tt></td>
186 * <td headers="matches">A printable character: <tt>[\p{Graph}\x20]</tt></td></tr>
187 * <tr><td valign="top" headers="construct posix"><tt>\p{Blank}</tt></td>
188 * <td headers="matches">A space or a tab: <tt>[ \t]</tt></td></tr>
189 * <tr><td valign="top" headers="construct posix"><tt>\p{Cntrl}</tt></td>
190 * <td headers="matches">A control character: <tt>[\x00-\x1F\x7F]</tt></td></tr>
191 * <tr><td valign="top" headers="construct posix"><tt>\p{XDigit}</tt></td>
192 * <td headers="matches">A hexadecimal digit: <tt>[0-9a-fA-F]</tt></td></tr>
193 * <tr><td valign="top" headers="construct posix"><tt>\p{Space}</tt></td>
194 * <td headers="matches">A whitespace character: <tt>[ \t\n\x0B\f\r]</tt></td></tr>
196 * <tr><th> </th></tr>
197 * <tr align="left"><th colspan="2">java.lang.Character classes (simple <a href="#jcc">java character type</a>)</th></tr>
199 * <tr><td valign="top"><tt>\p{javaLowerCase}</tt></td>
200 * <td>Equivalent to java.lang.Character.isLowerCase()</td></tr>
201 * <tr><td valign="top"><tt>\p{javaUpperCase}</tt></td>
202 * <td>Equivalent to java.lang.Character.isUpperCase()</td></tr>
203 * <tr><td valign="top"><tt>\p{javaWhitespace}</tt></td>
204 * <td>Equivalent to java.lang.Character.isWhitespace()</td></tr>
205 * <tr><td valign="top"><tt>\p{javaMirrored}</tt></td>
206 * <td>Equivalent to java.lang.Character.isMirrored()</td></tr>
208 * <tr><th> </th></tr>
209 * <tr align="left"><th colspan="2" id="unicode">Classes for Unicode scripts, blocks, categories and binary properties</th></tr>
210 * * <tr><td valign="top" headers="construct unicode"><tt>\p{IsLatin}</tt></td>
211 * <td headers="matches">A Latin script character (<a href="#usc">script</a>)</td></tr>
212 * <tr><td valign="top" headers="construct unicode"><tt>\p{InGreek}</tt></td>
213 * <td headers="matches">A character in the Greek block (<a href="#ubc">block</a>)</td></tr>
214 * <tr><td valign="top" headers="construct unicode"><tt>\p{Lu}</tt></td>
215 * <td headers="matches">An uppercase letter (<a href="#ucc">category</a>)</td></tr>
216 * <tr><td valign="top" headers="construct unicode"><tt>\p{IsAlphabetic}</tt></td>
217 * <td headers="matches">An alphabetic character (<a href="#ubpc">binary property</a>)</td></tr>
218 * <tr><td valign="top" headers="construct unicode"><tt>\p{Sc}</tt></td>
219 * <td headers="matches">A currency symbol</td></tr>
220 * <tr><td valign="top" headers="construct unicode"><tt>\P{InGreek}</tt></td>
221 * <td headers="matches">Any character except one in the Greek block (negation)</td></tr>
222 * <tr><td valign="top" headers="construct unicode"><tt>[\p{L}&&[^\p{Lu}]] </tt></td>
223 * <td headers="matches">Any letter except an uppercase letter (subtraction)</td></tr>
225 * <tr><th> </th></tr>
226 * <tr align="left"><th colspan="2" id="bounds">Boundary matchers</th></tr>
228 * <tr><td valign="top" headers="construct bounds"><tt>^</tt></td>
229 * <td headers="matches">The beginning of a line</td></tr>
230 * <tr><td valign="top" headers="construct bounds"><tt>$</tt></td>
231 * <td headers="matches">The end of a line</td></tr>
232 * <tr><td valign="top" headers="construct bounds"><tt>\b</tt></td>
233 * <td headers="matches">A word boundary</td></tr>
234 * <tr><td valign="top" headers="construct bounds"><tt>\B</tt></td>
235 * <td headers="matches">A non-word boundary</td></tr>
236 * <tr><td valign="top" headers="construct bounds"><tt>\A</tt></td>
237 * <td headers="matches">The beginning of the input</td></tr>
238 * <tr><td valign="top" headers="construct bounds"><tt>\G</tt></td>
239 * <td headers="matches">The end of the previous match</td></tr>
240 * <tr><td valign="top" headers="construct bounds"><tt>\Z</tt></td>
241 * <td headers="matches">The end of the input but for the final
242 * <a href="#lt">terminator</a>, if any</td></tr>
243 * <tr><td valign="top" headers="construct bounds"><tt>\z</tt></td>
244 * <td headers="matches">The end of the input</td></tr>
246 * <tr><th> </th></tr>
247 * <tr align="left"><th colspan="2" id="greedy">Greedy quantifiers</th></tr>
249 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>?</tt></td>
250 * <td headers="matches"><i>X</i>, once or not at all</td></tr>
251 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>*</tt></td>
252 * <td headers="matches"><i>X</i>, zero or more times</td></tr>
253 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>+</tt></td>
254 * <td headers="matches"><i>X</i>, one or more times</td></tr>
255 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>}</tt></td>
256 * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
257 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,}</tt></td>
258 * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
259 * <tr><td valign="top" headers="construct greedy"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}</tt></td>
260 * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
262 * <tr><th> </th></tr>
263 * <tr align="left"><th colspan="2" id="reluc">Reluctant quantifiers</th></tr>
265 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>??</tt></td>
266 * <td headers="matches"><i>X</i>, once or not at all</td></tr>
267 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>*?</tt></td>
268 * <td headers="matches"><i>X</i>, zero or more times</td></tr>
269 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>+?</tt></td>
270 * <td headers="matches"><i>X</i>, one or more times</td></tr>
271 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>}?</tt></td>
272 * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
273 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,}?</tt></td>
274 * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
275 * <tr><td valign="top" headers="construct reluc"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}?</tt></td>
276 * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
278 * <tr><th> </th></tr>
279 * <tr align="left"><th colspan="2" id="poss">Possessive quantifiers</th></tr>
281 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>?+</tt></td>
282 * <td headers="matches"><i>X</i>, once or not at all</td></tr>
283 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>*+</tt></td>
284 * <td headers="matches"><i>X</i>, zero or more times</td></tr>
285 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>++</tt></td>
286 * <td headers="matches"><i>X</i>, one or more times</td></tr>
287 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>}+</tt></td>
288 * <td headers="matches"><i>X</i>, exactly <i>n</i> times</td></tr>
289 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,}+</tt></td>
290 * <td headers="matches"><i>X</i>, at least <i>n</i> times</td></tr>
291 * <tr><td valign="top" headers="construct poss"><i>X</i><tt>{</tt><i>n</i><tt>,</tt><i>m</i><tt>}+</tt></td>
292 * <td headers="matches"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr>
294 * <tr><th> </th></tr>
295 * <tr align="left"><th colspan="2" id="logical">Logical operators</th></tr>
297 * <tr><td valign="top" headers="construct logical"><i>XY</i></td>
298 * <td headers="matches"><i>X</i> followed by <i>Y</i></td></tr>
299 * <tr><td valign="top" headers="construct logical"><i>X</i><tt>|</tt><i>Y</i></td>
300 * <td headers="matches">Either <i>X</i> or <i>Y</i></td></tr>
301 * <tr><td valign="top" headers="construct logical"><tt>(</tt><i>X</i><tt>)</tt></td>
302 * <td headers="matches">X, as a <a href="#cg">capturing group</a></td></tr>
304 * <tr><th> </th></tr>
305 * <tr align="left"><th colspan="2" id="backref">Back references</th></tr>
307 * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>n</i></td>
308 * <td valign="bottom" headers="matches">Whatever the <i>n</i><sup>th</sup>
309 * <a href="#cg">capturing group</a> matched</td></tr>
311 * <tr><td valign="bottom" headers="construct backref"><tt>\</tt><i>k</i><<i>name</i>></td>
312 * <td valign="bottom" headers="matches">Whatever the
313 * <a href="#groupname">named-capturing group</a> "name" matched</td></tr>
315 * <tr><th> </th></tr>
316 * <tr align="left"><th colspan="2" id="quot">Quotation</th></tr>
318 * <tr><td valign="top" headers="construct quot"><tt>\</tt></td>
319 * <td headers="matches">Nothing, but quotes the following character</td></tr>
320 * <tr><td valign="top" headers="construct quot"><tt>\Q</tt></td>
321 * <td headers="matches">Nothing, but quotes all characters until <tt>\E</tt></td></tr>
322 * <tr><td valign="top" headers="construct quot"><tt>\E</tt></td>
323 * <td headers="matches">Nothing, but ends quoting started by <tt>\Q</tt></td></tr>
324 * <!-- Metachars: !$()*+.<>?[\]^{|} -->
326 * <tr><th> </th></tr>
327 * <tr align="left"><th colspan="2" id="special">Special constructs (named-capturing and non-capturing)</th></tr>
329 * <tr><td valign="top" headers="construct special"><tt>(?<<a href="#groupname">name</a>></tt><i>X</i><tt>)</tt></td>
330 * <td headers="matches"><i>X</i>, as a named-capturing group</td></tr>
331 * <tr><td valign="top" headers="construct special"><tt>(?:</tt><i>X</i><tt>)</tt></td>
332 * <td headers="matches"><i>X</i>, as a non-capturing group</td></tr>
333 * <tr><td valign="top" headers="construct special"><tt>(?idmsuxU-idmsuxU) </tt></td>
334 * <td headers="matches">Nothing, but turns match flags <a href="#CASE_INSENSITIVE">i</a>
335 * <a href="#UNIX_LINES">d</a> <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a>
336 * <a href="#UNICODE_CASE">u</a> <a href="#COMMENTS">x</a> <a href="#UNICODE_CHARACTER_CLASS">U</a>
338 * <tr><td valign="top" headers="construct special"><tt>(?idmsux-idmsux:</tt><i>X</i><tt>)</tt> </td>
339 * <td headers="matches"><i>X</i>, as a <a href="#cg">non-capturing group</a> with the
340 * given flags <a href="#CASE_INSENSITIVE">i</a> <a href="#UNIX_LINES">d</a>
341 * <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> <a href="#UNICODE_CASE">u</a >
342 * <a href="#COMMENTS">x</a> on - off</td></tr>
343 * <tr><td valign="top" headers="construct special"><tt>(?=</tt><i>X</i><tt>)</tt></td>
344 * <td headers="matches"><i>X</i>, via zero-width positive lookahead</td></tr>
345 * <tr><td valign="top" headers="construct special"><tt>(?!</tt><i>X</i><tt>)</tt></td>
346 * <td headers="matches"><i>X</i>, via zero-width negative lookahead</td></tr>
347 * <tr><td valign="top" headers="construct special"><tt>(?<=</tt><i>X</i><tt>)</tt></td>
348 * <td headers="matches"><i>X</i>, via zero-width positive lookbehind</td></tr>
349 * <tr><td valign="top" headers="construct special"><tt>(?<!</tt><i>X</i><tt>)</tt></td>
350 * <td headers="matches"><i>X</i>, via zero-width negative lookbehind</td></tr>
351 * <tr><td valign="top" headers="construct special"><tt>(?></tt><i>X</i><tt>)</tt></td>
352 * <td headers="matches"><i>X</i>, as an independent, non-capturing group</td></tr>
360 * <h4> Backslashes, escapes, and quoting </h4>
362 * <p> The backslash character (<tt>'\'</tt>) serves to introduce escaped
363 * constructs, as defined in the table above, as well as to quote characters
364 * that otherwise would be interpreted as unescaped constructs. Thus the
365 * expression <tt>\\</tt> matches a single backslash and <tt>\{</tt> matches a
368 * <p> It is an error to use a backslash prior to any alphabetic character that
369 * does not denote an escaped construct; these are reserved for future
370 * extensions to the regular-expression language. A backslash may be used
371 * prior to a non-alphabetic character regardless of whether that character is
372 * part of an unescaped construct.
374 * <p> Backslashes within string literals in Java source code are interpreted
376 * <cite>The Java™ Language Specification</cite>
377 * as either Unicode escapes (section 3.3) or other character escapes (section 3.10.6)
378 * It is therefore necessary to double backslashes in string
379 * literals that represent regular expressions to protect them from
380 * interpretation by the Java bytecode compiler. The string literal
381 * <tt>"\b"</tt>, for example, matches a single backspace character when
382 * interpreted as a regular expression, while <tt>"\\b"</tt> matches a
383 * word boundary. The string literal <tt>"\(hello\)"</tt> is illegal
384 * and leads to a compile-time error; in order to match the string
385 * <tt>(hello)</tt> the string literal <tt>"\\(hello\\)"</tt>
389 * <h4> Character Classes </h4>
391 * <p> Character classes may appear within other character classes, and
392 * may be composed by the union operator (implicit) and the intersection
393 * operator (<tt>&&</tt>).
394 * The union operator denotes a class that contains every character that is
395 * in at least one of its operand classes. The intersection operator
396 * denotes a class that contains every character that is in both of its
399 * <p> The precedence of character-class operators is as follows, from
402 * <blockquote><table border="0" cellpadding="1" cellspacing="0"
403 * summary="Precedence of character class operators.">
404 * <tr><th>1 </th>
405 * <td>Literal escape </td>
406 * <td><tt>\x</tt></td></tr>
407 * <tr><th>2 </th>
409 * <td><tt>[...]</tt></td></tr>
410 * <tr><th>3 </th>
412 * <td><tt>a-z</tt></td></tr>
413 * <tr><th>4 </th>
415 * <td><tt>[a-e][i-u]</tt></td></tr>
416 * <tr><th>5 </th>
417 * <td>Intersection</td>
418 * <td><tt>[a-z&&[aeiou]]</tt></td></tr>
419 * </table></blockquote>
421 * <p> Note that a different set of metacharacters are in effect inside
422 * a character class than outside a character class. For instance, the
423 * regular expression <tt>.</tt> loses its special meaning inside a
424 * character class, while the expression <tt>-</tt> becomes a range
425 * forming metacharacter.
428 * <h4> Line terminators </h4>
430 * <p> A <i>line terminator</i> is a one- or two-character sequence that marks
431 * the end of a line of the input character sequence. The following are
432 * recognized as line terminators:
436 * <li> A newline (line feed) character (<tt>'\n'</tt>),
438 * <li> A carriage-return character followed immediately by a newline
439 * character (<tt>"\r\n"</tt>),
441 * <li> A standalone carriage-return character (<tt>'\r'</tt>),
443 * <li> A next-line character (<tt>'\u0085'</tt>),
445 * <li> A line-separator character (<tt>'\u2028'</tt>), or
447 * <li> A paragraph-separator character (<tt>'\u2029</tt>).
450 * <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators
451 * recognized are newline characters.
453 * <p> The regular expression <tt>.</tt> matches any character except a line
454 * terminator unless the {@link #DOTALL} flag is specified.
456 * <p> By default, the regular expressions <tt>^</tt> and <tt>$</tt> ignore
457 * line terminators and only match at the beginning and the end, respectively,
458 * of the entire input sequence. If {@link #MULTILINE} mode is activated then
459 * <tt>^</tt> matches at the beginning of input and after any line terminator
460 * except at the end of input. When in {@link #MULTILINE} mode <tt>$</tt>
461 * matches just before a line terminator or the end of the input sequence.
464 * <h4> Groups and capturing </h4>
467 * <h5> Group number </h5>
468 * <p> Capturing groups are numbered by counting their opening parentheses from
469 * left to right. In the expression <tt>((A)(B(C)))</tt>, for example, there
470 * are four such groups: </p>
472 * <blockquote><table cellpadding=1 cellspacing=0 summary="Capturing group numberings">
473 * <tr><th>1 </th>
474 * <td><tt>((A)(B(C)))</tt></td></tr>
475 * <tr><th>2 </th>
476 * <td><tt>(A)</tt></td></tr>
477 * <tr><th>3 </th>
478 * <td><tt>(B(C))</tt></td></tr>
479 * <tr><th>4 </th>
480 * <td><tt>(C)</tt></td></tr>
481 * </table></blockquote>
483 * <p> Group zero always stands for the entire expression.
485 * <p> Capturing groups are so named because, during a match, each subsequence
486 * of the input sequence that matches such a group is saved. The captured
487 * subsequence may be used later in the expression, via a back reference, and
488 * may also be retrieved from the matcher once the match operation is complete.
490 * <a name="groupname">
491 * <h5> Group name </h5>
492 * <p>A capturing group can also be assigned a "name", a <tt>named-capturing group</tt>,
493 * and then be back-referenced later by the "name". Group names are composed of
494 * the following characters. The first character must be a <tt>letter</tt>.
497 * <li> The uppercase letters <tt>'A'</tt> through <tt>'Z'</tt>
498 * (<tt>'\u0041'</tt> through <tt>'\u005a'</tt>),
499 * <li> The lowercase letters <tt>'a'</tt> through <tt>'z'</tt>
500 * (<tt>'\u0061'</tt> through <tt>'\u007a'</tt>),
501 * <li> The digits <tt>'0'</tt> through <tt>'9'</tt>
502 * (<tt>'\u0030'</tt> through <tt>'\u0039'</tt>),
505 * <p> A <tt>named-capturing group</tt> is still numbered as described in
506 * <a href="#gnumber">Group number</a>.
508 * <p> The captured input associated with a group is always the subsequence
509 * that the group most recently matched. If a group is evaluated a second time
510 * because of quantification then its previously-captured value, if any, will
511 * be retained if the second evaluation fails. Matching the string
512 * <tt>"aba"</tt> against the expression <tt>(a(b)?)+</tt>, for example, leaves
513 * group two set to <tt>"b"</tt>. All captured input is discarded at the
514 * beginning of each match.
516 * <p> Groups beginning with <tt>(?</tt> are either pure, <i>non-capturing</i> groups
517 * that do not capture text and do not count towards the group total, or
518 * <i>named-capturing</i> group.
520 * <h4> Unicode support </h4>
522 * <p> This class is in conformance with Level 1 of <a
523 * href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
524 * Standard #18: Unicode Regular Expression</i></a>, plus RL2.1
525 * Canonical Equivalents.
527 * <b>Unicode escape sequences</b> such as <tt>\u2014</tt> in Java source code
528 * are processed as described in section 3.3 of
529 * <cite>The Java™ Language Specification</cite>.
530 * Such escape sequences are also implemented directly by the regular-expression
531 * parser so that Unicode escapes can be used in expressions that are read from
532 * files or from the keyboard. Thus the strings <tt>"\u2014"</tt> and
533 * <tt>"\\u2014"</tt>, while not equal, compile into the same pattern, which
534 * matches the character with hexadecimal value <tt>0x2014</tt>.
536 * A Unicode character can also be represented in a regular-expression by
537 * using its <b>Hex notation</b>(hexadecimal code point value) directly as described in construct
538 * <tt>\x{...}</tt>, for example a supplementary character U+2011F
539 * can be specified as <tt>\x{2011F}</tt>, instead of two consecutive
540 * Unicode escape sequences of the surrogate pair
541 * <tt>\uD840</tt><tt>\uDD1F</tt>.
543 * Unicode scripts, blocks, categories and binary properties are written with
544 * the <tt>\p</tt> and <tt>\P</tt> constructs as in Perl.
545 * <tt>\p{</tt><i>prop</i><tt>}</tt> matches if
546 * the input has the property <i>prop</i>, while <tt>\P{</tt><i>prop</i><tt>}</tt>
547 * does not match if the input has that property.
549 * Scripts, blocks, categories and binary properties can be used both inside
550 * and outside of a character class.
553 * <b>Scripts</b> are specified either with the prefix {@code Is}, as in
554 * {@code IsHiragana}, or by using the {@code script} keyword (or its short
555 * form {@code sc})as in {@code script=Hiragana} or {@code sc=Hiragana}.
557 * The script names supported by <code>Pattern</code> are the valid script names
558 * accepted and defined by
559 * {@link java.lang.Character.UnicodeScript#forName(String) UnicodeScript.forName}.
562 * <b>Blocks</b> are specified with the prefix {@code In}, as in
563 * {@code InMongolian}, or by using the keyword {@code block} (or its short
564 * form {@code blk}) as in {@code block=Mongolian} or {@code blk=Mongolian}.
566 * The block names supported by <code>Pattern</code> are the valid block names
567 * accepted and defined by
568 * {@link java.lang.Character.UnicodeBlock#forName(String) UnicodeBlock.forName}.
571 * <b>Categories</b> may be specified with the optional prefix {@code Is}:
572 * Both {@code \p{L}} and {@code \p{IsL}} denote the category of Unicode
573 * letters. Same as scripts and blocks, categories can also be specified
574 * by using the keyword {@code general_category} (or its short form
575 * {@code gc}) as in {@code general_category=Lu} or {@code gc=Lu}.
577 * The supported categories are those of
578 * <a href="http://www.unicode.org/unicode/standard/standard.html">
579 * <i>The Unicode Standard</i></a> in the version specified by the
580 * {@link java.lang.Character Character} class. The category names are those
581 * defined in the Standard, both normative and informative.
584 * <b>Binary properties</b> are specified with the prefix {@code Is}, as in
585 * {@code IsAlphabetic}. The supported binary properties by <code>Pattern</code>
599 * <li> Noncharacter_Code_Point
605 * <b>Predefined Character classes</b> and <b>POSIX character classes</b> are in
606 * conformance with the recommendation of <i>Annex C: Compatibility Properties</i>
607 * of <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Regular Expression
608 * </i></a>, when {@link #UNICODE_CHARACTER_CLASS} flag is specified.
610 * <table border="0" cellpadding="1" cellspacing="0"
611 * summary="predefined and posix character classes in Unicode mode">
613 * <th bgcolor="#CCCCFF" align="left" id="classes">Classes</th>
614 * <th bgcolor="#CCCCFF" align="left" id="matches">Matches</th>
616 * <tr><td><tt>\p{Lower}</tt></td>
617 * <td>A lowercase character:<tt>\p{IsLowercase}</tt></td></tr>
618 * <tr><td><tt>\p{Upper}</tt></td>
619 * <td>An uppercase character:<tt>\p{IsUppercase}</tt></td></tr>
620 * <tr><td><tt>\p{ASCII}</tt></td>
621 * <td>All ASCII:<tt>[\x00-\x7F]</tt></td></tr>
622 * <tr><td><tt>\p{Alpha}</tt></td>
623 * <td>An alphabetic character:<tt>\p{IsAlphabetic}</tt></td></tr>
624 * <tr><td><tt>\p{Digit}</tt></td>
625 * <td>A decimal digit character:<tt>p{IsDigit}</tt></td></tr>
626 * <tr><td><tt>\p{Alnum}</tt></td>
627 * <td>An alphanumeric character:<tt>[\p{IsAlphabetic}\p{IsDigit}]</tt></td></tr>
628 * <tr><td><tt>\p{Punct}</tt></td>
629 * <td>A punctuation character:<tt>p{IsPunctuation}</tt></td></tr>
630 * <tr><td><tt>\p{Graph}</tt></td>
631 * <td>A visible character: <tt>[^\p{IsWhite_Space}\p{gc=Cc}\p{gc=Cs}\p{gc=Cn}]</tt></td></tr>
632 * <tr><td><tt>\p{Print}</tt></td>
633 * <td>A printable character: <tt>[\p{Graph}\p{Blank}&&[^\p{Cntrl}]]</tt></td></tr>
634 * <tr><td><tt>\p{Blank}</tt></td>
635 * <td>A space or a tab: <tt>[\p{IsWhite_Space}&&[^\p{gc=Zl}\p{gc=Zp}\x0a\x0b\x0c\x0d\x85]]</tt></td></tr>
636 * <tr><td><tt>\p{Cntrl}</tt></td>
637 * <td>A control character: <tt>\p{gc=Cc}</tt></td></tr>
638 * <tr><td><tt>\p{XDigit}</tt></td>
639 * <td>A hexadecimal digit: <tt>[\p{gc=Nd}\p{IsHex_Digit}]</tt></td></tr>
640 * <tr><td><tt>\p{Space}</tt></td>
641 * <td>A whitespace character:<tt>\p{IsWhite_Space}</tt></td></tr>
642 * <tr><td><tt>\d</tt></td>
643 * <td>A digit: <tt>\p{IsDigit}</tt></td></tr>
644 * <tr><td><tt>\D</tt></td>
645 * <td>A non-digit: <tt>[^\d]</tt></td></tr>
646 * <tr><td><tt>\s</tt></td>
647 * <td>A whitespace character: <tt>\p{IsWhite_Space}</tt></td></tr>
648 * <tr><td><tt>\S</tt></td>
649 * <td>A non-whitespace character: <tt>[^\s]</tt></td></tr>
650 * <tr><td><tt>\w</tt></td>
651 * <td>A word character: <tt>[\p{Alpha}\p{gc=Mn}\p{gc=Me}\p{gc=Mc}\p{Digit}\p{gc=Pc}]</tt></td></tr>
652 * <tr><td><tt>\W</tt></td>
653 * <td>A non-word character: <tt>[^\w]</tt></td></tr>
657 * Categories that behave like the java.lang.Character
658 * boolean is<i>methodname</i> methods (except for the deprecated ones) are
659 * available through the same <tt>\p{</tt><i>prop</i><tt>}</tt> syntax where
660 * the specified property has the name <tt>java<i>methodname</i></tt>.
662 * <h4> Comparison to Perl 5 </h4>
664 * <p>The <code>Pattern</code> engine performs traditional NFA-based matching
665 * with ordered alternation as occurs in Perl 5.
667 * <p> Perl constructs not supported by this class: </p>
670 * <li><p> Predefined character classes (Unicode character)
671 * <p><tt>\h </tt>A horizontal whitespace
672 * <p><tt>\H </tt>A non horizontal whitespace
673 * <p><tt>\v </tt>A vertical whitespace
674 * <p><tt>\V </tt>A non vertical whitespace
675 * <p><tt>\R </tt>Any Unicode linebreak sequence
676 * <tt>\u005cu000D\u005cu000A|[\u005cu000A\u005cu000B\u005cu000C\u005cu000D\u005cu0085\u005cu2028\u005cu2029]</tt>
677 * <p><tt>\X </tt>Match Unicode
678 * <a href="http://www.unicode.org/reports/tr18/#Default_Grapheme_Clusters">
679 * <i>extended grapheme cluster</i></a>
682 * <li><p> The backreference constructs, <tt>\g{</tt><i>n</i><tt>}</tt> for
683 * the <i>n</i><sup>th</sup><a href="#cg">capturing group</a> and
684 * <tt>\g{</tt><i>name</i><tt>}</tt> for
685 * <a href="#groupname">named-capturing group</a>.
688 * <li><p> The named character construct, <tt>\N{</tt><i>name</i><tt>}</tt>
689 * for a Unicode character by its name.
692 * <li><p> The conditional constructs
693 * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>)</tt> and
694 * <tt>(?(</tt><i>condition</i><tt>)</tt><i>X</i><tt>|</tt><i>Y</i><tt>)</tt>,
697 * <li><p> The embedded code constructs <tt>(?{</tt><i>code</i><tt>})</tt>
698 * and <tt>(??{</tt><i>code</i><tt>})</tt>,</p></li>
700 * <li><p> The embedded comment syntax <tt>(?#comment)</tt>, and </p></li>
702 * <li><p> The preprocessing operations <tt>\l</tt> <tt>\u</tt>,
703 * <tt>\L</tt>, and <tt>\U</tt>. </p></li>
707 * <p> Constructs supported by this class but not by Perl: </p>
711 * <li><p> Character-class union and intersection as described
712 * <a href="#cc">above</a>.</p></li>
716 * <p> Notable differences from Perl: </p>
720 * <li><p> In Perl, <tt>\1</tt> through <tt>\9</tt> are always interpreted
721 * as back references; a backslash-escaped number greater than <tt>9</tt> is
722 * treated as a back reference if at least that many subexpressions exist,
723 * otherwise it is interpreted, if possible, as an octal escape. In this
724 * class octal escapes must always begin with a zero. In this class,
725 * <tt>\1</tt> through <tt>\9</tt> are always interpreted as back
726 * references, and a larger number is accepted as a back reference if at
727 * least that many subexpressions exist at that point in the regular
728 * expression, otherwise the parser will drop digits until the number is
729 * smaller or equal to the existing number of groups or it is one digit.
732 * <li><p> Perl uses the <tt>g</tt> flag to request a match that resumes
733 * where the last match left off. This functionality is provided implicitly
734 * by the {@link Matcher} class: Repeated invocations of the {@link
735 * Matcher#find find} method will resume where the last match left off,
736 * unless the matcher is reset. </p></li>
738 * <li><p> In Perl, embedded flags at the top level of an expression affect
739 * the whole expression. In this class, embedded flags always take effect
740 * at the point at which they appear, whether they are at the top level or
741 * within a group; in the latter case, flags are restored at the end of the
742 * group just as in Perl. </p></li>
747 * <p> For a more precise description of the behavior of regular expression
748 * constructs, please see <a href="http://www.oreilly.com/catalog/regex3/">
749 * <i>Mastering Regular Expressions, 3nd Edition</i>, Jeffrey E. F. Friedl,
750 * O'Reilly and Associates, 2006.</a>
753 * @see java.lang.String#split(String, int)
754 * @see java.lang.String#split(String)
756 * @author Mike McCloskey
757 * @author Mark Reinhold
758 * @author JSR-51 Expert Group
763 public final class Pattern
764 implements java.io.Serializable
768 * Regular expression modifier values. Instead of being passed as
769 * arguments, they can also be passed as inline modifiers.
770 * For example, the following statements have the same effect.
772 * RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M);
773 * RegExp r2 = RegExp.compile("(?im)abc", 0);
776 * The flags are duplicated so that the familiar Perl match flag
777 * names are available.
781 * Enables Unix lines mode.
783 * <p> In this mode, only the <tt>'\n'</tt> line terminator is recognized
784 * in the behavior of <tt>.</tt>, <tt>^</tt>, and <tt>$</tt>.
786 * <p> Unix lines mode can also be enabled via the embedded flag
787 * expression <tt>(?d)</tt>.
789 public static final int UNIX_LINES = 0x01;
792 * Enables case-insensitive matching.
794 * <p> By default, case-insensitive matching assumes that only characters
795 * in the US-ASCII charset are being matched. Unicode-aware
796 * case-insensitive matching can be enabled by specifying the {@link
797 * #UNICODE_CASE} flag in conjunction with this flag.
799 * <p> Case-insensitive matching can also be enabled via the embedded flag
800 * expression <tt>(?i)</tt>.
802 * <p> Specifying this flag may impose a slight performance penalty. </p>
804 public static final int CASE_INSENSITIVE = 0x02;
807 * Permits whitespace and comments in pattern.
809 * <p> In this mode, whitespace is ignored, and embedded comments starting
810 * with <tt>#</tt> are ignored until the end of a line.
812 * <p> Comments mode can also be enabled via the embedded flag
813 * expression <tt>(?x)</tt>.
815 public static final int COMMENTS = 0x04;
818 * Enables multiline mode.
820 * <p> In multiline mode the expressions <tt>^</tt> and <tt>$</tt> match
821 * just after or just before, respectively, a line terminator or the end of
822 * the input sequence. By default these expressions only match at the
823 * beginning and the end of the entire input sequence.
825 * <p> Multiline mode can also be enabled via the embedded flag
826 * expression <tt>(?m)</tt>. </p>
828 public static final int MULTILINE = 0x08;
831 * Enables literal parsing of the pattern.
833 * <p> When this flag is specified then the input string that specifies
834 * the pattern is treated as a sequence of literal characters.
835 * Metacharacters or escape sequences in the input sequence will be
836 * given no special meaning.
838 * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on
839 * matching when used in conjunction with this flag. The other flags
840 * become superfluous.
842 * <p> There is no embedded flag character for enabling literal parsing.
845 public static final int LITERAL = 0x10;
848 * Enables dotall mode.
850 * <p> In dotall mode, the expression <tt>.</tt> matches any character,
851 * including a line terminator. By default this expression does not match
854 * <p> Dotall mode can also be enabled via the embedded flag
855 * expression <tt>(?s)</tt>. (The <tt>s</tt> is a mnemonic for
856 * "single-line" mode, which is what this is called in Perl.) </p>
858 public static final int DOTALL = 0x20;
861 * Enables Unicode-aware case folding.
863 * <p> When this flag is specified then case-insensitive matching, when
864 * enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner
865 * consistent with the Unicode Standard. By default, case-insensitive
866 * matching assumes that only characters in the US-ASCII charset are being
869 * <p> Unicode-aware case folding can also be enabled via the embedded flag
870 * expression <tt>(?u)</tt>.
872 * <p> Specifying this flag may impose a performance penalty. </p>
874 public static final int UNICODE_CASE = 0x40;
877 * Enables canonical equivalence.
879 * <p> When this flag is specified then two characters will be considered
880 * to match if, and only if, their full canonical decompositions match.
881 * The expression <tt>"a\u030A"</tt>, for example, will match the
882 * string <tt>"\u00E5"</tt> when this flag is specified. By default,
883 * matching does not take canonical equivalence into account.
885 * <p> There is no embedded flag character for enabling canonical
888 * <p> Specifying this flag may impose a performance penalty. </p>
890 public static final int CANON_EQ = 0x80;
893 * Enables the Unicode version of <i>Predefined character classes</i> and
894 * <i>POSIX character classes</i>.
896 * <p> When this flag is specified then the (US-ASCII only)
897 * <i>Predefined character classes</i> and <i>POSIX character classes</i>
898 * are in conformance with
899 * <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical
900 * Standard #18: Unicode Regular Expression</i></a>
901 * <i>Annex C: Compatibility Properties</i>.
903 * The UNICODE_CHARACTER_CLASS mode can also be enabled via the embedded
904 * flag expression <tt>(?U)</tt>.
906 * The flag implies UNICODE_CASE, that is, it enables Unicode-aware case
909 * Specifying this flag may impose a performance penalty. </p>
912 public static final int UNICODE_CHARACTER_CLASS = 0x100;
914 /* Pattern has only two serialized components: The pattern string
915 * and the flags, which are all that is needed to recompile the pattern
916 * when it is deserialized.
919 /** use serialVersionUID from Merlin b59 for interoperability */
920 private static final long serialVersionUID = 5073258162644648461L;
923 * The original regular-expression pattern string.
927 private String pattern;
930 * The original pattern flags.
937 * Boolean indicating this Pattern is compiled; this is necessary in order
938 * to lazily compile deserialized Patterns.
940 private transient volatile boolean compiled = false;
943 * The normalized pattern string.
945 private transient String normalizedPattern;
948 * The starting point of state machine for the find operation. This allows
949 * a match to start anywhere in the input.
954 * The root of object tree for a match operation. The pattern is matched
955 * at the beginning. This may include a find that uses BnM or a First
958 transient Node matchRoot;
961 * Temporary storage used by parsing pattern slice.
963 transient int[] buffer;
966 * Map the "name" of the "named capturing group" to its group id
969 transient volatile Map<String, Integer> namedGroups;
972 * Temporary storage used while parsing group references.
974 transient GroupHead[] groupNodes;
977 * Temporary null terminated code point array used by pattern compiling.
979 private transient int[] temp;
982 * The number of capturing groups in this Pattern. Used by matchers to
983 * allocate storage needed to perform a match.
985 transient int capturingGroupCount;
988 * The local variable count used by parsing tree. Used by matchers to
989 * allocate storage needed to perform a match.
991 transient int localCount;
994 * Index into the pattern string that keeps track of how much has been
997 private transient int cursor;
1000 * Holds the length of the pattern string.
1002 private transient int patternLength;
1005 * If the Start node might possibly match supplementary characters.
1006 * It is set to true during compiling if
1007 * (1) There is supplementary char in pattern, or
1008 * (2) There is complement node of Category or Block
1010 private transient boolean hasSupplementary;
1013 * Compiles the given regular expression into a pattern. </p>
1016 * The expression to be compiled
1018 * @throws PatternSyntaxException
1019 * If the expression's syntax is invalid
1021 public static Pattern compile(String regex) {
1022 return new Pattern(regex, 0);
1026 * Compiles the given regular expression into a pattern with the given
1030 * The expression to be compiled
1033 * Match flags, a bit mask that may include
1034 * {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL},
1035 * {@link #UNICODE_CASE}, {@link #CANON_EQ}, {@link #UNIX_LINES},
1036 * {@link #LITERAL}, {@link #UNICODE_CHARACTER_CLASS}
1037 * and {@link #COMMENTS}
1039 * @throws IllegalArgumentException
1040 * If bit values other than those corresponding to the defined
1041 * match flags are set in <tt>flags</tt>
1043 * @throws PatternSyntaxException
1044 * If the expression's syntax is invalid
1046 public static Pattern compile(String regex, int flags) {
1047 return new Pattern(regex, flags);
1051 * Returns the regular expression from which this pattern was compiled.
1054 * @return The source of this pattern
1056 public String pattern() {
1061 * <p>Returns the string representation of this pattern. This
1062 * is the regular expression from which this pattern was
1065 * @return The string representation of this pattern
1068 public String toString() {
1073 * Creates a matcher that will match the given input against this pattern.
1077 * The character sequence to be matched
1079 * @return A new matcher for this pattern
1081 public Matcher matcher(CharSequence input) {
1083 synchronized(this) {
1088 Matcher m = new Matcher(this, input);
1093 * Returns this pattern's match flags. </p>
1095 * @return The match flags specified when this pattern was compiled
1097 public int flags() {
1102 * Compiles the given regular expression and attempts to match the given
1105 * <p> An invocation of this convenience method of the form
1108 * Pattern.matches(regex, input);</pre></blockquote>
1110 * behaves in exactly the same way as the expression
1113 * Pattern.compile(regex).matcher(input).matches()</pre></blockquote>
1115 * <p> If a pattern is to be used multiple times, compiling it once and reusing
1116 * it will be more efficient than invoking this method each time. </p>
1119 * The expression to be compiled
1122 * The character sequence to be matched
1124 * @throws PatternSyntaxException
1125 * If the expression's syntax is invalid
1127 public static boolean matches(String regex, CharSequence input) {
1128 Pattern p = Pattern.compile(regex);
1129 Matcher m = p.matcher(input);
1134 * Splits the given input sequence around matches of this pattern.
1136 * <p> The array returned by this method contains each substring of the
1137 * input sequence that is terminated by another subsequence that matches
1138 * this pattern or is terminated by the end of the input sequence. The
1139 * substrings in the array are in the order in which they occur in the
1140 * input. If this pattern does not match any subsequence of the input then
1141 * the resulting array has just one element, namely the input sequence in
1144 * <p> The <tt>limit</tt> parameter controls the number of times the
1145 * pattern is applied and therefore affects the length of the resulting
1146 * array. If the limit <i>n</i> is greater than zero then the pattern
1147 * will be applied at most <i>n</i> - 1 times, the array's
1148 * length will be no greater than <i>n</i>, and the array's last entry
1149 * will contain all input beyond the last matched delimiter. If <i>n</i>
1150 * is non-positive then the pattern will be applied as many times as
1151 * possible and the array can have any length. If <i>n</i> is zero then
1152 * the pattern will be applied as many times as possible, the array can
1153 * have any length, and trailing empty strings will be discarded.
1155 * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
1156 * results with these parameters:
1158 * <blockquote><table cellpadding=1 cellspacing=0
1159 * summary="Split examples showing regex, limit, and result">
1160 * <tr><th><P align="left"><i>Regex </i></th>
1161 * <th><P align="left"><i>Limit </i></th>
1162 * <th><P align="left"><i>Result </i></th></tr>
1163 * <tr><td align=center>:</td>
1164 * <td align=center>2</td>
1165 * <td><tt>{ "boo", "and:foo" }</tt></td></tr>
1166 * <tr><td align=center>:</td>
1167 * <td align=center>5</td>
1168 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
1169 * <tr><td align=center>:</td>
1170 * <td align=center>-2</td>
1171 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
1172 * <tr><td align=center>o</td>
1173 * <td align=center>5</td>
1174 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
1175 * <tr><td align=center>o</td>
1176 * <td align=center>-2</td>
1177 * <td><tt>{ "b", "", ":and:f", "", "" }</tt></td></tr>
1178 * <tr><td align=center>o</td>
1179 * <td align=center>0</td>
1180 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
1181 * </table></blockquote>
1185 * The character sequence to be split
1188 * The result threshold, as described above
1190 * @return The array of strings computed by splitting the input
1191 * around matches of this pattern
1193 public String[] split(CharSequence input, int limit) {
1195 boolean matchLimited = limit > 0;
1196 ArrayList<String> matchList = new ArrayList<>();
1197 Matcher m = matcher(input);
1199 // Add segments before each match found
1201 if (!matchLimited || matchList.size() < limit - 1) {
1202 String match = input.subSequence(index, m.start()).toString();
1203 matchList.add(match);
1205 } else if (matchList.size() == limit - 1) { // last one
1206 String match = input.subSequence(index,
1207 input.length()).toString();
1208 matchList.add(match);
1213 // If no match was found, return this
1215 return new String[] {input.toString()};
1217 // Add remaining segment
1218 if (!matchLimited || matchList.size() < limit)
1219 matchList.add(input.subSequence(index, input.length()).toString());
1222 int resultSize = matchList.size();
1224 while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
1226 String[] result = new String[resultSize];
1227 return matchList.subList(0, resultSize).toArray(result);
1231 * Splits the given input sequence around matches of this pattern.
1233 * <p> This method works as if by invoking the two-argument {@link
1234 * #split(java.lang.CharSequence, int) split} method with the given input
1235 * sequence and a limit argument of zero. Trailing empty strings are
1236 * therefore not included in the resulting array. </p>
1238 * <p> The input <tt>"boo:and:foo"</tt>, for example, yields the following
1239 * results with these expressions:
1241 * <blockquote><table cellpadding=1 cellspacing=0
1242 * summary="Split examples showing regex and result">
1243 * <tr><th><P align="left"><i>Regex </i></th>
1244 * <th><P align="left"><i>Result</i></th></tr>
1245 * <tr><td align=center>:</td>
1246 * <td><tt>{ "boo", "and", "foo" }</tt></td></tr>
1247 * <tr><td align=center>o</td>
1248 * <td><tt>{ "b", "", ":and:f" }</tt></td></tr>
1249 * </table></blockquote>
1253 * The character sequence to be split
1255 * @return The array of strings computed by splitting the input
1256 * around matches of this pattern
1258 public String[] split(CharSequence input) {
1259 return split(input, 0);
1263 * Returns a literal pattern <code>String</code> for the specified
1264 * <code>String</code>.
1266 * <p>This method produces a <code>String</code> that can be used to
1267 * create a <code>Pattern</code> that would match the string
1268 * <code>s</code> as if it were a literal pattern.</p> Metacharacters
1269 * or escape sequences in the input sequence will be given no special
1272 * @param s The string to be literalized
1273 * @return A literal string replacement
1276 public static String quote(String s) {
1277 int slashEIndex = s.indexOf("\\E");
1278 if (slashEIndex == -1)
1279 return "\\Q" + s + "\\E";
1281 StringBuilder sb = new StringBuilder(s.length() * 2);
1285 while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
1286 sb.append(s.substring(current, slashEIndex));
1287 current = slashEIndex + 2;
1288 sb.append("\\E\\\\E\\Q");
1290 sb.append(s.substring(current, s.length()));
1292 return sb.toString();
1296 * Recompile the Pattern instance from a stream. The original pattern
1297 * string is read in and the object tree is recompiled from it.
1299 private void readObject(java.io.ObjectInputStream s)
1300 throws java.io.IOException, ClassNotFoundException {
1302 // Read in all fields
1303 s.defaultReadObject();
1305 // Initialize counts
1306 capturingGroupCount = 1;
1309 // if length > 0, the Pattern is lazily compiled
1311 if (pattern.length() == 0) {
1312 root = new Start(lastAccept);
1313 matchRoot = lastAccept;
1319 * This private constructor is used to create all Patterns. The pattern
1320 * string and match flags are all that is needed to completely describe
1321 * a Pattern. An empty pattern string results in an object tree with
1322 * only a Start node and a LastNode node.
1324 private Pattern(String p, int f) {
1328 // to use UNICODE_CASE if UNICODE_CHARACTER_CLASS present
1329 if ((flags & UNICODE_CHARACTER_CLASS) != 0)
1330 flags |= UNICODE_CASE;
1332 // Reset group index count
1333 capturingGroupCount = 1;
1336 if (pattern.length() > 0) {
1339 root = new Start(lastAccept);
1340 matchRoot = lastAccept;
1345 * The pattern is converted to normalizedD form and then a pure group
1346 * is constructed to match canonical equivalences of the characters.
1348 private void normalize() {
1349 boolean inCharClass = false;
1350 int lastCodePoint = -1;
1352 // Convert pattern into normalizedD form
1353 normalizedPattern = Normalizer.normalize(pattern, Normalizer.Form.NFD);
1354 patternLength = normalizedPattern.length();
1356 // Modify pattern to match canonical equivalences
1357 StringBuilder newPattern = new StringBuilder(patternLength);
1358 for(int i=0; i<patternLength; ) {
1359 int c = normalizedPattern.codePointAt(i);
1360 StringBuilder sequenceBuffer;
1361 if ((Character.getType(c) == Character.NON_SPACING_MARK)
1362 && (lastCodePoint != -1)) {
1363 sequenceBuffer = new StringBuilder();
1364 sequenceBuffer.appendCodePoint(lastCodePoint);
1365 sequenceBuffer.appendCodePoint(c);
1366 while(Character.getType(c) == Character.NON_SPACING_MARK) {
1367 i += Character.charCount(c);
1368 if (i >= patternLength)
1370 c = normalizedPattern.codePointAt(i);
1371 sequenceBuffer.appendCodePoint(c);
1373 String ea = produceEquivalentAlternation(
1374 sequenceBuffer.toString());
1375 newPattern.setLength(newPattern.length()-Character.charCount(lastCodePoint));
1376 newPattern.append("(?:").append(ea).append(")");
1377 } else if (c == '[' && lastCodePoint != '\\') {
1378 i = normalizeCharClass(newPattern, i);
1380 newPattern.appendCodePoint(c);
1383 i += Character.charCount(c);
1385 normalizedPattern = newPattern.toString();
1389 * Complete the character class being parsed and add a set
1390 * of alternations to it that will match the canonical equivalences
1391 * of the characters within the class.
1393 private int normalizeCharClass(StringBuilder newPattern, int i) {
1394 StringBuilder charClass = new StringBuilder();
1395 StringBuilder eq = null;
1396 int lastCodePoint = -1;
1400 charClass.append("[");
1402 int c = normalizedPattern.codePointAt(i);
1403 StringBuilder sequenceBuffer;
1405 if (c == ']' && lastCodePoint != '\\') {
1406 charClass.append((char)c);
1408 } else if (Character.getType(c) == Character.NON_SPACING_MARK) {
1409 sequenceBuffer = new StringBuilder();
1410 sequenceBuffer.appendCodePoint(lastCodePoint);
1411 while(Character.getType(c) == Character.NON_SPACING_MARK) {
1412 sequenceBuffer.appendCodePoint(c);
1413 i += Character.charCount(c);
1414 if (i >= normalizedPattern.length())
1416 c = normalizedPattern.codePointAt(i);
1418 String ea = produceEquivalentAlternation(
1419 sequenceBuffer.toString());
1421 charClass.setLength(charClass.length()-Character.charCount(lastCodePoint));
1423 eq = new StringBuilder();
1427 charClass.appendCodePoint(c);
1430 if (i == normalizedPattern.length())
1431 throw error("Unclosed character class");
1436 result = "(?:"+charClass.toString()+eq.toString()+")";
1438 result = charClass.toString();
1441 newPattern.append(result);
1446 * Given a specific sequence composed of a regular character and
1447 * combining marks that follow it, produce the alternation that will
1448 * match all canonical equivalences of that sequence.
1450 private String produceEquivalentAlternation(String source) {
1451 int len = countChars(source, 0, 1);
1452 if (source.length() == len)
1453 // source has one character.
1456 String base = source.substring(0,len);
1457 String combiningMarks = source.substring(len);
1459 String[] perms = producePermutations(combiningMarks);
1460 StringBuilder result = new StringBuilder(source);
1462 // Add combined permutations
1463 for(int x=0; x<perms.length; x++) {
1464 String next = base + perms[x];
1466 result.append("|"+next);
1467 next = composeOneStep(next);
1469 result.append("|"+produceEquivalentAlternation(next));
1471 return result.toString();
1475 * Returns an array of strings that have all the possible
1476 * permutations of the characters in the input string.
1477 * This is used to get a list of all possible orderings
1478 * of a set of combining marks. Note that some of the permutations
1479 * are invalid because of combining class collisions, and these
1480 * possibilities must be removed because they are not canonically
1483 private String[] producePermutations(String input) {
1484 if (input.length() == countChars(input, 0, 1))
1485 return new String[] {input};
1487 if (input.length() == countChars(input, 0, 2)) {
1488 int c0 = Character.codePointAt(input, 0);
1489 int c1 = Character.codePointAt(input, Character.charCount(c0));
1490 if (getClass(c1) == getClass(c0)) {
1491 return new String[] {input};
1493 String[] result = new String[2];
1495 StringBuilder sb = new StringBuilder(2);
1496 sb.appendCodePoint(c1);
1497 sb.appendCodePoint(c0);
1498 result[1] = sb.toString();
1503 int nCodePoints = countCodePoints(input);
1504 for(int x=1; x<nCodePoints; x++)
1505 length = length * (x+1);
1507 String[] temp = new String[length];
1509 int combClass[] = new int[nCodePoints];
1510 for(int x=0, i=0; x<nCodePoints; x++) {
1511 int c = Character.codePointAt(input, i);
1512 combClass[x] = getClass(c);
1513 i += Character.charCount(c);
1516 // For each char, take it out and add the permutations
1517 // of the remaining chars
1520 // offset maintains the index in code units.
1521 loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
1522 len = countChars(input, offset, 1);
1523 boolean skip = false;
1524 for(int y=x-1; y>=0; y--) {
1525 if (combClass[y] == combClass[x]) {
1529 StringBuilder sb = new StringBuilder(input);
1530 String otherChars = sb.delete(offset, offset+len).toString();
1531 String[] subResult = producePermutations(otherChars);
1533 String prefix = input.substring(offset, offset+len);
1534 for(int y=0; y<subResult.length; y++)
1535 temp[index++] = prefix + subResult[y];
1537 String[] result = new String[index];
1538 for (int x=0; x<index; x++)
1539 result[x] = temp[x];
1543 private int getClass(int c) {
1544 return sun.text.Normalizer.getCombiningClass(c);
1548 * Attempts to compose input by combining the first character
1549 * with the first combining mark following it. Returns a String
1550 * that is the composition of the leading character with its first
1551 * combining mark followed by the remaining combining marks. Returns
1552 * null if the first two characters cannot be further composed.
1554 private String composeOneStep(String input) {
1555 int len = countChars(input, 0, 2);
1556 String firstTwoCharacters = input.substring(0, len);
1557 String result = Normalizer.normalize(firstTwoCharacters, Normalizer.Form.NFC);
1559 if (result.equals(firstTwoCharacters))
1562 String remainder = input.substring(len);
1563 return result + remainder;
1568 * Preprocess any \Q...\E sequences in `temp', meta-quoting them.
1569 * See the description of `quotemeta' in perlfunc(1).
1571 private void RemoveQEQuoting() {
1572 final int pLen = patternLength;
1574 while (i < pLen-1) {
1575 if (temp[i] != '\\')
1577 else if (temp[i + 1] != 'Q')
1582 if (i >= pLen - 1) // No \Q sequence found
1586 int[] newtemp = new int[j + 2*(pLen-i) + 2];
1587 System.arraycopy(temp, 0, newtemp, 0, j);
1589 boolean inQuote = true;
1592 if (! ASCII.isAscii(c) || ASCII.isAlnum(c)) {
1594 } else if (c != '\\') {
1595 if (inQuote) newtemp[j++] = '\\';
1597 } else if (inQuote) {
1598 if (temp[i] == 'E') {
1602 newtemp[j++] = '\\';
1603 newtemp[j++] = '\\';
1606 if (temp[i] == 'Q') {
1612 newtemp[j++] = temp[i++];
1618 temp = Arrays.copyOf(newtemp, j + 2); // double zero termination
1622 * Copies regular expression to an int array and invokes the parsing
1623 * of the expression which will create the object tree.
1625 private void compile() {
1626 // Handle canonical equivalences
1627 if (has(CANON_EQ) && !has(LITERAL)) {
1630 normalizedPattern = pattern;
1632 patternLength = normalizedPattern.length();
1634 // Copy pattern to int array for convenience
1635 // Use double zero to terminate pattern
1636 temp = new int[patternLength + 2];
1638 hasSupplementary = false;
1640 // Convert all chars into code points
1641 for (int x = 0; x < patternLength; x += Character.charCount(c)) {
1642 c = normalizedPattern.codePointAt(x);
1643 if (isSupplementary(c)) {
1644 hasSupplementary = true;
1649 patternLength = count; // patternLength now in code points
1654 // Allocate all temporary objects here.
1655 buffer = new int[32];
1656 groupNodes = new GroupHead[10];
1660 // Literal pattern handling
1661 matchRoot = newSlice(temp, patternLength, hasSupplementary);
1662 matchRoot.next = lastAccept;
1664 // Start recursive descent parsing
1665 matchRoot = expr(lastAccept);
1666 // Check extra pattern characters
1667 if (patternLength != cursor) {
1668 if (peek() == ')') {
1669 throw error("Unmatched closing ')'");
1671 throw error("Unexpected internal error");
1676 // Peephole optimization
1677 if (matchRoot instanceof Slice) {
1678 root = BnM.optimize(matchRoot);
1679 if (root == matchRoot) {
1680 root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
1682 } else if (matchRoot instanceof Begin || matchRoot instanceof First) {
1685 root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
1688 // Release temporary storage
1696 Map<String, Integer> namedGroups() {
1697 if (namedGroups == null)
1698 namedGroups = new HashMap<>(2);
1703 * Used to print out a subtree of the Pattern to help with debugging.
1705 private static void printObjectTree(Node node) {
1706 while(node != null) {
1707 if (node instanceof Prolog) {
1708 System.out.println(node);
1709 printObjectTree(((Prolog)node).loop);
1710 System.out.println("**** end contents prolog loop");
1711 } else if (node instanceof Loop) {
1712 System.out.println(node);
1713 printObjectTree(((Loop)node).body);
1714 System.out.println("**** end contents Loop body");
1715 } else if (node instanceof Curly) {
1716 System.out.println(node);
1717 printObjectTree(((Curly)node).atom);
1718 System.out.println("**** end contents Curly body");
1719 } else if (node instanceof GroupCurly) {
1720 System.out.println(node);
1721 printObjectTree(((GroupCurly)node).atom);
1722 System.out.println("**** end contents GroupCurly body");
1723 } else if (node instanceof GroupTail) {
1724 System.out.println(node);
1725 System.out.println("Tail next is "+node.next);
1728 System.out.println(node);
1732 System.out.println("->next:");
1733 if (node == Pattern.accept) {
1734 System.out.println("Accept Node");
1741 * Used to accumulate information about a subtree of the object graph
1742 * so that optimizations can be applied to the subtree.
1744 static final class TreeInfo {
1748 boolean deterministic;
1757 deterministic = true;
1762 * The following private methods are mainly used to improve the
1763 * readability of the code. In order to let the Java compiler easily
1764 * inline them, we should not put many assertions or error checks in them.
1768 * Indicates whether a particular flag is set or not.
1770 private boolean has(int f) {
1771 return (flags & f) != 0;
1775 * Match next character, signal error if failed.
1777 private void accept(int ch, String s) {
1778 int testChar = temp[cursor++];
1780 testChar = parsePastWhitespace(testChar);
1781 if (ch != testChar) {
1787 * Mark the end of pattern with a specific character.
1789 private void mark(int c) {
1790 temp[patternLength] = c;
1794 * Peek the next character, and do not advance the cursor.
1796 private int peek() {
1797 int ch = temp[cursor];
1799 ch = peekPastWhitespace(ch);
1804 * Read the next character, and advance the cursor by one.
1806 private int read() {
1807 int ch = temp[cursor++];
1809 ch = parsePastWhitespace(ch);
1814 * Read the next character, and advance the cursor by one,
1815 * ignoring the COMMENTS setting
1817 private int readEscaped() {
1818 int ch = temp[cursor++];
1823 * Advance the cursor by one, and peek the next character.
1825 private int next() {
1826 int ch = temp[++cursor];
1828 ch = peekPastWhitespace(ch);
1833 * Advance the cursor by one, and peek the next character,
1834 * ignoring the COMMENTS setting
1836 private int nextEscaped() {
1837 int ch = temp[++cursor];
1842 * If in xmode peek past whitespace and comments.
1844 private int peekPastWhitespace(int ch) {
1845 while (ASCII.isSpace(ch) || ch == '#') {
1846 while (ASCII.isSpace(ch))
1847 ch = temp[++cursor];
1849 ch = peekPastLine();
1856 * If in xmode parse past whitespace and comments.
1858 private int parsePastWhitespace(int ch) {
1859 while (ASCII.isSpace(ch) || ch == '#') {
1860 while (ASCII.isSpace(ch))
1861 ch = temp[cursor++];
1863 ch = parsePastLine();
1869 * xmode parse past comment to end of line.
1871 private int parsePastLine() {
1872 int ch = temp[cursor++];
1873 while (ch != 0 && !isLineSeparator(ch))
1874 ch = temp[cursor++];
1879 * xmode peek past comment to end of line.
1881 private int peekPastLine() {
1882 int ch = temp[++cursor];
1883 while (ch != 0 && !isLineSeparator(ch))
1884 ch = temp[++cursor];
1889 * Determines if character is a line separator in the current mode
1891 private boolean isLineSeparator(int ch) {
1892 if (has(UNIX_LINES)) {
1895 return (ch == '\n' ||
1897 (ch|1) == '\u2029' ||
1903 * Read the character after the next one, and advance the cursor by two.
1905 private int skip() {
1913 * Unread one next character, and retreat cursor by one.
1915 private void unread() {
1920 * Internal method used for handling all syntax errors. The pattern is
1921 * displayed with a pointer to aid in locating the syntax error.
1923 private PatternSyntaxException error(String s) {
1924 return new PatternSyntaxException(s, normalizedPattern, cursor - 1);
1928 * Determines if there is any supplementary character or unpaired
1929 * surrogate in the specified range.
1931 private boolean findSupplementary(int start, int end) {
1932 for (int i = start; i < end; i++) {
1933 if (isSupplementary(temp[i]))
1940 * Determines if the specified code point is a supplementary
1941 * character or unpaired surrogate.
1943 private static final boolean isSupplementary(int ch) {
1944 return ch >= Character.MIN_SUPPLEMENTARY_CODE_POINT ||
1945 Character.isSurrogate((char)ch);
1949 * The following methods handle the main parsing. They are sorted
1950 * according to their precedence order, the lowest one first.
1954 * The expression is parsed with branch nodes added for alternations.
1955 * This may be called recursively to parse sub expressions that may
1956 * contain alternations.
1958 private Node expr(Node end) {
1960 Node firstTail = null;
1961 Node branchConn = null;
1964 Node node = sequence(end);
1965 Node nodeTail = root; //double return
1968 firstTail = nodeTail;
1971 if (branchConn == null) {
1972 branchConn = new BranchConn();
1973 branchConn.next = end;
1976 // if the node returned from sequence() is "end"
1977 // we have an empty expr, set a null atom into
1978 // the branch to indicate to go "next" directly.
1981 // the "tail.next" of each atom goes to branchConn
1982 nodeTail.next = branchConn;
1984 if (prev instanceof Branch) {
1985 ((Branch)prev).add(node);
1990 // replace the "end" with "branchConn" at its tail.next
1991 // when put the "prev" into the branch as the first atom.
1992 firstTail.next = branchConn;
1994 prev = new Branch(prev, node, branchConn);
1997 if (peek() != '|') {
2005 * Parsing of sequences between alternations.
2007 private Node sequence(Node end) {
2016 // Because group handles its own closure,
2017 // we need to treat it differently
2019 // Check for comment or flag group
2026 // Double return: Tail was returned in root
2034 if (ch == 'p' || ch == 'P') {
2035 boolean oneLetter = true;
2036 boolean comp = (ch == 'P');
2037 ch = next(); // Consume { if present
2043 node = family(oneLetter, comp);
2051 if (has(MULTILINE)) {
2052 if (has(UNIX_LINES))
2053 node = new UnixCaret();
2062 if (has(UNIX_LINES))
2063 node = new UnixDollar(has(MULTILINE));
2065 node = new Dollar(has(MULTILINE));
2072 if (has(UNIX_LINES))
2073 node = new UnixDot();
2082 case ']': // Now interpreting dangling ] and } as literals
2090 throw error("Dangling meta character '" + ((char)ch) + "'");
2092 if (cursor >= patternLength) {
2101 node = closure(node);
2114 root = tail; //double return
2119 * Parse and add a new Single or Slice.
2121 private Node atom() {
2124 boolean hasSupplementary = false;
2133 cursor = prev; // Unwind one character
2147 if (ch == 'p' || ch == 'P') { // Property
2148 if (first > 0) { // Slice is waiting; handle it first
2151 } else { // No slice; just return the family node
2152 boolean comp = (ch == 'P');
2153 boolean oneLetter = true;
2154 ch = next(); // Consume { if present
2159 return family(oneLetter, comp);
2164 ch = escape(false, first == 0);
2168 if (isSupplementary(ch)) {
2169 hasSupplementary = true;
2173 } else if (first == 0) {
2176 // Unwind meta escape sequence
2180 if (cursor >= patternLength) {
2188 if (isSupplementary(ch)) {
2189 hasSupplementary = true;
2197 return newSingle(buffer[0]);
2199 return newSlice(buffer, first, hasSupplementary);
2203 private void append(int ch, int len) {
2204 if (len >= buffer.length) {
2205 int[] tmp = new int[len+len];
2206 System.arraycopy(buffer, 0, tmp, 0, len);
2213 * Parses a backref greedily, taking as many numbers as it
2214 * can. The first digit is always treated as a backref, but
2215 * multi digit numbers are only treated as a backref if at
2216 * least that many backrefs exist at this point in the regex.
2218 private Node ref(int refNum) {
2219 boolean done = false;
2233 int newRefNum = (refNum * 10) + (ch - '0');
2234 // Add another number if it doesn't make a group
2235 // that doesn't exist
2236 if (capturingGroupCount - 1 < newRefNum) {
2248 if (has(CASE_INSENSITIVE))
2249 return new CIBackRef(refNum, has(UNICODE_CASE));
2251 return new BackRef(refNum);
2255 * Parses an escape sequence to determine the actual value that needs
2257 * If -1 is returned and create was true a new object was added to the tree
2258 * to handle the escape sequence.
2259 * If the returned value is greater than zero, it is the value that
2260 * matches the escape sequence.
2262 private int escape(boolean inclass, boolean create) {
2278 root = ref((ch - '0'));
2283 if (create) root = new Begin();
2287 if (create) root = new Bound(Bound.NONE, has(UNICODE_CHARACTER_CLASS));
2292 if (create) root = has(UNICODE_CHARACTER_CLASS)
2293 ? new Utype(UnicodeProp.DIGIT).complement()
2294 : new Ctype(ASCII.DIGIT).complement();
2301 if (create) root = new LastMatch();
2316 if (create) root = has(UNICODE_CHARACTER_CLASS)
2317 ? new Utype(UnicodeProp.WHITE_SPACE).complement()
2318 : new Ctype(ASCII.SPACE).complement();
2325 if (create) root = has(UNICODE_CHARACTER_CLASS)
2326 ? new Utype(UnicodeProp.WORD).complement()
2327 : new Ctype(ASCII.WORD).complement();
2335 if (has(UNIX_LINES))
2336 root = new UnixDollar(false);
2338 root = new Dollar(false);
2345 if (create) root = new Bound(Bound.BOTH, has(UNICODE_CHARACTER_CLASS));
2350 if (create) root = has(UNICODE_CHARACTER_CLASS)
2351 ? new Utype(UnicodeProp.DIGIT)
2352 : new Ctype(ASCII.DIGIT);
2367 throw error("\\k is not followed by '<' for named capturing group");
2368 String name = groupname(read());
2369 if (!namedGroups().containsKey(name))
2370 throw error("(named capturing group <"+ name+"> does not exit");
2372 if (has(CASE_INSENSITIVE))
2373 root = new CIBackRef(namedGroups().get(name), has(UNICODE_CASE));
2375 root = new BackRef(namedGroups().get(name));
2390 if (create) root = has(UNICODE_CHARACTER_CLASS)
2391 ? new Utype(UnicodeProp.WHITE_SPACE)
2392 : new Ctype(ASCII.SPACE);
2401 if (create) root = has(UNICODE_CHARACTER_CLASS)
2402 ? new Utype(UnicodeProp.WORD)
2403 : new Ctype(ASCII.WORD);
2411 if (create) root = new End();
2416 throw error("Illegal/unsupported escape sequence");
2420 * Parse a character class, and return the node that matches it.
2422 * Consumes a ] on the way out if consume is true. Usually consume
2423 * is true except for the case of [abc&&def] where def is a separate
2424 * right hand node with "understood" brackets.
2426 private CharProperty clazz(boolean consume) {
2427 CharProperty prev = null;
2428 CharProperty node = null;
2429 BitClass bits = new BitClass();
2430 boolean include = true;
2431 boolean firstInClass = true;
2436 // Negates if first char in a class, otherwise literal
2438 if (temp[cursor-1] != '[')
2444 // ^ not first in class, treat as literal
2448 firstInClass = false;
2453 prev = union(prev, node);
2457 firstInClass = false;
2461 CharProperty rightNode = null;
2462 while (ch != ']' && ch != '&') {
2464 if (rightNode == null)
2465 rightNode = clazz(true);
2467 rightNode = union(rightNode, clazz(true));
2468 } else { // abc&&def
2470 rightNode = clazz(false);
2474 if (rightNode != null)
2477 if (rightNode == null)
2478 throw error("Bad class syntax");
2482 prev = intersection(prev, node);
2485 // treat as a literal &
2491 firstInClass = false;
2492 if (cursor >= patternLength)
2493 throw error("Unclosed character class");
2496 firstInClass = false;
2504 firstInClass = false;
2513 prev = union(prev, node);
2517 prev = node.complement();
2520 prev = setDifference(prev, node);
2527 private CharProperty bitsOrSingle(BitClass bits, int ch) {
2528 /* Bits can only handle codepoints in [u+0000-u+00ff] range.
2529 Use "single" node instead of bits when dealing with unicode
2530 case folding for codepoints listed below.
2531 (1)Uppercase out of range: u+00ff, u+00b5
2532 toUpperCase(u+00ff) -> u+0178
2533 toUpperCase(u+00b5) -> u+039c
2534 (2)LatinSmallLetterLongS u+17f
2535 toUpperCase(u+017f) -> u+0053
2536 (3)LatinSmallLetterDotlessI u+131
2537 toUpperCase(u+0131) -> u+0049
2538 (4)LatinCapitalLetterIWithDotAbove u+0130
2539 toLowerCase(u+0130) -> u+0069
2540 (5)KelvinSign u+212a
2541 toLowerCase(u+212a) ==> u+006B
2542 (6)AngstromSign u+212b
2543 toLowerCase(u+212b) ==> u+00e5
2547 !(has(CASE_INSENSITIVE) && has(UNICODE_CASE) &&
2548 (ch == 0xff || ch == 0xb5 ||
2549 ch == 0x49 || ch == 0x69 || //I and i
2550 ch == 0x53 || ch == 0x73 || //S and s
2551 ch == 0x4b || ch == 0x6b || //K and k
2552 ch == 0xc5 || ch == 0xe5))) //A+ring
2553 return bits.add(ch, flags());
2554 return newSingle(ch);
2558 * Parse a single character or a character range in a character class
2559 * and return its representative node.
2561 private CharProperty range(BitClass bits) {
2565 if (ch == 'p' || ch == 'P') { // A property
2566 boolean comp = (ch == 'P');
2567 boolean oneLetter = true;
2568 // Consume { if present
2574 return family(oneLetter, comp);
2575 } else { // ordinary escape
2577 ch = escape(true, true);
2579 return (CharProperty) root;
2585 if (peek() == '-') {
2586 int endRange = temp[cursor+1];
2587 if (endRange == '[') {
2588 return bitsOrSingle(bits, ch);
2590 if (endRange != ']') {
2594 throw error("Illegal character range");
2595 if (has(CASE_INSENSITIVE))
2596 return caseInsensitiveRangeFor(ch, m);
2598 return rangeFor(ch, m);
2601 return bitsOrSingle(bits, ch);
2603 throw error("Unexpected character '"+((char)ch)+"'");
2606 private int single() {
2610 return escape(true, false);
2618 * Parses a Unicode character family and returns its representative node.
2620 private CharProperty family(boolean singleLetter,
2621 boolean maybeComplement)
2625 CharProperty node = null;
2628 int c = temp[cursor];
2629 if (!Character.isSupplementaryCodePoint(c)) {
2630 name = String.valueOf((char)c);
2632 name = new String(temp, cursor, 1);
2638 while(read() != '}') {
2642 if (j > patternLength)
2643 throw error("Unclosed character family");
2645 throw error("Empty character family");
2646 name = new String(temp, i, j-i-1);
2649 int i = name.indexOf('=');
2651 // property construct \p{name=value}
2652 String value = name.substring(i + 1);
2653 name = name.substring(0, i).toLowerCase(Locale.ENGLISH);
2654 if ("sc".equals(name) || "script".equals(name)) {
2655 node = unicodeScriptPropertyFor(value);
2656 } else if ("blk".equals(name) || "block".equals(name)) {
2657 node = unicodeBlockPropertyFor(value);
2658 } else if ("gc".equals(name) || "general_category".equals(name)) {
2659 node = charPropertyNodeFor(value);
2661 throw error("Unknown Unicode property {name=<" + name + ">, "
2662 + "value=<" + value + ">}");
2665 if (name.startsWith("In")) {
2667 node = unicodeBlockPropertyFor(name.substring(2));
2668 } else if (name.startsWith("Is")) {
2669 // \p{isGeneralCategory} and \p{isScriptName}
2670 name = name.substring(2);
2671 UnicodeProp uprop = UnicodeProp.forName(name);
2673 node = new Utype(uprop);
2675 node = CharPropertyNames.charPropertyFor(name);
2677 node = unicodeScriptPropertyFor(name);
2679 if (has(UNICODE_CHARACTER_CLASS)) {
2680 UnicodeProp uprop = UnicodeProp.forPOSIXName(name);
2682 node = new Utype(uprop);
2685 node = charPropertyNodeFor(name);
2688 if (maybeComplement) {
2689 if (node instanceof Category || node instanceof Block)
2690 hasSupplementary = true;
2691 node = node.complement();
2698 * Returns a CharProperty matching all characters belong to
2701 private CharProperty unicodeScriptPropertyFor(String name) {
2702 final Character.UnicodeScript script;
2704 script = Character.UnicodeScript.forName(name);
2705 } catch (IllegalArgumentException iae) {
2706 throw error("Unknown character script name {" + name + "}");
2708 return new Script(script);
2712 * Returns a CharProperty matching all characters in a UnicodeBlock.
2714 private CharProperty unicodeBlockPropertyFor(String name) {
2715 final Character.UnicodeBlock block;
2717 block = Character.UnicodeBlock.forName(name);
2718 } catch (IllegalArgumentException iae) {
2719 throw error("Unknown character block name {" + name + "}");
2721 return new Block(block);
2725 * Returns a CharProperty matching all characters in a named property.
2727 private CharProperty charPropertyNodeFor(String name) {
2728 CharProperty p = CharPropertyNames.charPropertyFor(name);
2730 throw error("Unknown character property name {" + name + "}");
2735 * Parses and returns the name of a "named capturing group", the trailing
2736 * ">" is consumed after parsing.
2738 private String groupname(int ch) {
2739 StringBuilder sb = new StringBuilder();
2740 sb.append(Character.toChars(ch));
2741 while (ASCII.isLower(ch=read()) || ASCII.isUpper(ch) ||
2742 ASCII.isDigit(ch)) {
2743 sb.append(Character.toChars(ch));
2745 if (sb.length() == 0)
2746 throw error("named capturing group has 0 length name");
2748 throw error("named capturing group is missing trailing '>'");
2749 return sb.toString();
2753 * Parses a group and returns the head node of a set of nodes that process
2754 * the group. Sometimes a double return system is used where the tail is
2757 private Node group0() {
2758 boolean capturingGroup = false;
2767 case ':': // (?:xxx) pure group
2768 head = createGroup(true);
2770 head.next = expr(tail);
2772 case '=': // (?=xxx) and (?!xxx) lookahead
2774 head = createGroup(true);
2776 head.next = expr(tail);
2778 head = tail = new Pos(head);
2780 head = tail = new Neg(head);
2783 case '>': // (?>xxx) independent group
2784 head = createGroup(true);
2786 head.next = expr(tail);
2787 head = tail = new Ques(head, INDEPENDENT);
2789 case '<': // (?<xxx) look behind
2791 if (ASCII.isLower(ch) || ASCII.isUpper(ch)) {
2792 // named captured group
2793 String name = groupname(ch);
2794 if (namedGroups().containsKey(name))
2795 throw error("Named capturing group <" + name
2796 + "> is already defined");
2797 capturingGroup = true;
2798 head = createGroup(false);
2800 namedGroups().put(name, capturingGroupCount-1);
2801 head.next = expr(tail);
2805 head = createGroup(true);
2807 head.next = expr(tail);
2808 tail.next = lookbehindEnd;
2809 TreeInfo info = new TreeInfo();
2811 if (info.maxValid == false) {
2812 throw error("Look-behind group does not have "
2813 + "an obvious maximum length");
2815 boolean hasSupplementary = findSupplementary(start, patternLength);
2817 head = tail = (hasSupplementary ?
2818 new BehindS(head, info.maxLength,
2820 new Behind(head, info.maxLength,
2822 } else if (ch == '!') {
2823 head = tail = (hasSupplementary ?
2824 new NotBehindS(head, info.maxLength,
2826 new NotBehind(head, info.maxLength,
2829 throw error("Unknown look-behind group");
2834 throw error("Unknown group type");
2835 default: // (?xxx:) inlined match flags
2840 return null; // Inline modifier only
2843 throw error("Unknown inline modifier");
2845 head = createGroup(true);
2847 head.next = expr(tail);
2850 } else { // (xxx) a regular group
2851 capturingGroup = true;
2852 head = createGroup(false);
2854 head.next = expr(tail);
2857 accept(')', "Unclosed group");
2860 // Check for quantifiers
2861 Node node = closure(head);
2862 if (node == head) { // No closure
2864 return node; // Dual return
2866 if (head == tail) { // Zero length assertion
2868 return node; // Dual return
2871 if (node instanceof Ques) {
2872 Ques ques = (Ques) node;
2873 if (ques.type == POSSESSIVE) {
2877 tail.next = new BranchConn();
2879 if (ques.type == GREEDY) {
2880 head = new Branch(head, null, tail);
2881 } else { // Reluctant quantifier
2882 head = new Branch(null, head, tail);
2886 } else if (node instanceof Curly) {
2887 Curly curly = (Curly) node;
2888 if (curly.type == POSSESSIVE) {
2892 // Discover if the group is deterministic
2893 TreeInfo info = new TreeInfo();
2894 if (head.study(info)) { // Deterministic
2895 GroupTail temp = (GroupTail) tail;
2896 head = root = new GroupCurly(head.next, curly.cmin,
2897 curly.cmax, curly.type,
2898 ((GroupTail)tail).localIndex,
2899 ((GroupTail)tail).groupIndex,
2902 } else { // Non-deterministic
2903 int temp = ((GroupHead) head).localIndex;
2905 if (curly.type == GREEDY)
2906 loop = new Loop(this.localCount, temp);
2907 else // Reluctant Curly
2908 loop = new LazyLoop(this.localCount, temp);
2909 Prolog prolog = new Prolog(loop);
2910 this.localCount += 1;
2911 loop.cmin = curly.cmin;
2912 loop.cmax = curly.cmax;
2916 return prolog; // Dual return
2919 throw error("Internal logic error");
2923 * Create group head and tail nodes using double return. If the group is
2924 * created with anonymous true then it is a pure group and should not
2925 * affect group counting.
2927 private Node createGroup(boolean anonymous) {
2928 int localIndex = localCount++;
2931 groupIndex = capturingGroupCount++;
2932 GroupHead head = new GroupHead(localIndex);
2933 root = new GroupTail(localIndex, groupIndex);
2934 if (!anonymous && groupIndex < 10)
2935 groupNodes[groupIndex] = head;
2940 * Parses inlined match flags and set them appropriately.
2942 private void addFlag() {
2947 flags |= CASE_INSENSITIVE;
2956 flags |= UNIX_LINES;
2959 flags |= UNICODE_CASE;
2968 flags |= (UNICODE_CHARACTER_CLASS | UNICODE_CASE);
2970 case '-': // subFlag then fall through
2981 * Parses the second part of inlined match flags and turns off
2982 * flags appropriately.
2984 private void subFlag() {
2989 flags &= ~CASE_INSENSITIVE;
2992 flags &= ~MULTILINE;
2998 flags &= ~UNIX_LINES;
3001 flags &= ~UNICODE_CASE;
3010 flags &= ~(UNICODE_CHARACTER_CLASS | UNICODE_CASE);
3018 static final int MAX_REPS = 0x7FFFFFFF;
3020 static final int GREEDY = 0;
3022 static final int LAZY = 1;
3024 static final int POSSESSIVE = 2;
3026 static final int INDEPENDENT = 3;
3029 * Processes repetition. If the next character peeked is a quantifier
3030 * then new nodes must be appended to handle the repetition.
3031 * Prev could be a single or a group, so it could be a chain of nodes.
3033 private Node closure(Node prev) {
3041 return new Ques(prev, LAZY);
3042 } else if (ch == '+') {
3044 return new Ques(prev, POSSESSIVE);
3046 return new Ques(prev, GREEDY);
3051 return new Curly(prev, 0, MAX_REPS, LAZY);
3052 } else if (ch == '+') {
3054 return new Curly(prev, 0, MAX_REPS, POSSESSIVE);
3056 return new Curly(prev, 0, MAX_REPS, GREEDY);
3061 return new Curly(prev, 1, MAX_REPS, LAZY);
3062 } else if (ch == '+') {
3064 return new Curly(prev, 1, MAX_REPS, POSSESSIVE);
3066 return new Curly(prev, 1, MAX_REPS, GREEDY);
3068 ch = temp[cursor+1];
3069 if (ASCII.isDigit(ch)) {
3073 cmin = cmin * 10 + (ch - '0');
3074 } while (ASCII.isDigit(ch = read()));
3081 while (ASCII.isDigit(ch)) {
3082 cmax = cmax * 10 + (ch - '0');
3088 throw error("Unclosed counted closure");
3089 if (((cmin) | (cmax) | (cmax - cmin)) < 0)
3090 throw error("Illegal repetition range");
3095 curly = new Curly(prev, cmin, cmax, LAZY);
3096 } else if (ch == '+') {
3098 curly = new Curly(prev, cmin, cmax, POSSESSIVE);
3100 curly = new Curly(prev, cmin, cmax, GREEDY);
3104 throw error("Illegal repetition");
3112 * Utility method for parsing control escape sequences.
3115 if (cursor < patternLength) {
3118 throw error("Illegal control escape sequence");
3122 * Utility method for parsing octal escape sequences.
3126 if (((n-'0')|('7'-n)) >= 0) {
3128 if (((m-'0')|('7'-m)) >= 0) {
3130 if ((((o-'0')|('7'-o)) >= 0) && (((n-'0')|('3'-n)) >= 0)) {
3131 return (n - '0') * 64 + (m - '0') * 8 + (o - '0');
3134 return (n - '0') * 8 + (m - '0');
3139 throw error("Illegal octal escape sequence");
3143 * Utility method for parsing hexadecimal escape sequences.
3147 if (ASCII.isHexDigit(n)) {
3149 if (ASCII.isHexDigit(m)) {
3150 return ASCII.toDigit(n) * 16 + ASCII.toDigit(m);
3152 } else if (n == '{' && ASCII.isHexDigit(peek())) {
3154 while (ASCII.isHexDigit(n = read())) {
3155 ch = (ch << 4) + ASCII.toDigit(n);
3156 if (ch > Character.MAX_CODE_POINT)
3157 throw error("Hexadecimal codepoint is too big");
3160 throw error("Unclosed hexadecimal escape sequence");
3163 throw error("Illegal hexadecimal escape sequence");
3167 * Utility method for parsing unicode escape sequences.
3169 private int cursor() {
3173 private void setcursor(int pos) {
3177 private int uxxxx() {
3179 for (int i = 0; i < 4; i++) {
3181 if (!ASCII.isHexDigit(ch)) {
3182 throw error("Illegal Unicode escape sequence");
3184 n = n * 16 + ASCII.toDigit(ch);
3191 if (Character.isHighSurrogate((char)n)) {
3193 if (read() == '\\' && read() == 'u') {
3195 if (Character.isLowSurrogate((char)n2))
3196 return Character.toCodePoint((char)n, (char)n2);
3204 // Utility methods for code point support
3207 private static final int countChars(CharSequence seq, int index,
3208 int lengthInCodePoints) {
3210 if (lengthInCodePoints == 1 && !Character.isHighSurrogate(seq.charAt(index))) {
3211 assert (index >= 0 && index < seq.length());
3214 int length = seq.length();
3216 if (lengthInCodePoints >= 0) {
3217 assert (index >= 0 && index < length);
3218 for (int i = 0; x < length && i < lengthInCodePoints; i++) {
3219 if (Character.isHighSurrogate(seq.charAt(x++))) {
3220 if (x < length && Character.isLowSurrogate(seq.charAt(x))) {
3228 assert (index >= 0 && index <= length);
3232 int len = -lengthInCodePoints;
3233 for (int i = 0; x > 0 && i < len; i++) {
3234 if (Character.isLowSurrogate(seq.charAt(--x))) {
3235 if (x > 0 && Character.isHighSurrogate(seq.charAt(x-1))) {
3243 private static final int countCodePoints(CharSequence seq) {
3244 int length = seq.length();
3246 for (int i = 0; i < length; ) {
3248 if (Character.isHighSurrogate(seq.charAt(i++))) {
3249 if (i < length && Character.isLowSurrogate(seq.charAt(i))) {
3258 * Creates a bit vector for matching Latin-1 values. A normal BitClass
3259 * never matches values above Latin-1, and a complemented BitClass always
3260 * matches values above Latin-1.
3262 private static final class BitClass extends BmpCharProperty {
3263 final boolean[] bits;
3264 BitClass() { bits = new boolean[256]; }
3265 private BitClass(boolean[] bits) { this.bits = bits; }
3266 BitClass add(int c, int flags) {
3267 assert c >= 0 && c <= 255;
3268 if ((flags & CASE_INSENSITIVE) != 0) {
3269 if (ASCII.isAscii(c)) {
3270 bits[ASCII.toUpper(c)] = true;
3271 bits[ASCII.toLower(c)] = true;
3272 } else if ((flags & UNICODE_CASE) != 0) {
3273 bits[Character.toLowerCase(c)] = true;
3274 bits[Character.toUpperCase(c)] = true;
3280 boolean isSatisfiedBy(int ch) {
3281 return ch < 256 && bits[ch];
3286 * Returns a suitably optimized, single character matcher.
3288 private CharProperty newSingle(final int ch) {
3289 if (has(CASE_INSENSITIVE)) {
3291 if (has(UNICODE_CASE)) {
3292 upper = Character.toUpperCase(ch);
3293 lower = Character.toLowerCase(upper);
3295 return new SingleU(lower);
3296 } else if (ASCII.isAscii(ch)) {
3297 lower = ASCII.toLower(ch);
3298 upper = ASCII.toUpper(ch);
3300 return new SingleI(lower, upper);
3303 if (isSupplementary(ch))
3304 return new SingleS(ch); // Match a given Unicode character
3305 return new Single(ch); // Match a given BMP character
3309 * Utility method for creating a string slice matcher.
3311 private Node newSlice(int[] buf, int count, boolean hasSupplementary) {
3312 int[] tmp = new int[count];
3313 if (has(CASE_INSENSITIVE)) {
3314 if (has(UNICODE_CASE)) {
3315 for (int i = 0; i < count; i++) {
3316 tmp[i] = Character.toLowerCase(
3317 Character.toUpperCase(buf[i]));
3319 return hasSupplementary? new SliceUS(tmp) : new SliceU(tmp);
3321 for (int i = 0; i < count; i++) {
3322 tmp[i] = ASCII.toLower(buf[i]);
3324 return hasSupplementary? new SliceIS(tmp) : new SliceI(tmp);
3326 for (int i = 0; i < count; i++) {
3329 return hasSupplementary ? new SliceS(tmp) : new Slice(tmp);
3333 * The following classes are the building components of the object
3334 * tree that represents a compiled regular expression. The object tree
3335 * is made of individual elements that handle constructs in the Pattern.
3336 * Each type of object knows how to match its equivalent construct with
3337 * the match() method.
3341 * Base class for all node classes. Subclasses should override the match()
3342 * method as appropriate. This class is an accepting node, so its match()
3343 * always returns true.
3345 static class Node extends Object {
3348 next = Pattern.accept;
3351 * This method implements the classic accept node.
3353 boolean match(Matcher matcher, int i, CharSequence seq) {
3355 matcher.groups[0] = matcher.first;
3356 matcher.groups[1] = matcher.last;
3360 * This method is good for all zero length assertions.
3362 boolean study(TreeInfo info) {
3364 return next.study(info);
3366 return info.deterministic;
3371 static class LastNode extends Node {
3373 * This method implements the classic accept node with
3374 * the addition of a check to see if the match occurred
3375 * using all of the input.
3377 boolean match(Matcher matcher, int i, CharSequence seq) {
3378 if (matcher.acceptMode == Matcher.ENDANCHOR && i != matcher.to)
3381 matcher.groups[0] = matcher.first;
3382 matcher.groups[1] = matcher.last;
3388 * Used for REs that can start anywhere within the input string.
3389 * This basically tries to match repeatedly at each spot in the
3390 * input string, moving forward after each try. An anchored search
3391 * or a BnM will bypass this node completely.
3393 static class Start extends Node {
3397 TreeInfo info = new TreeInfo();
3399 minLength = info.minLength;
3401 boolean match(Matcher matcher, int i, CharSequence seq) {
3402 if (i > matcher.to - minLength) {
3403 matcher.hitEnd = true;
3406 int guard = matcher.to - minLength;
3407 for (; i <= guard; i++) {
3408 if (next.match(matcher, i, seq)) {
3410 matcher.groups[0] = matcher.first;
3411 matcher.groups[1] = matcher.last;
3415 matcher.hitEnd = true;
3418 boolean study(TreeInfo info) {
3420 info.maxValid = false;
3421 info.deterministic = false;
3427 * StartS supports supplementary characters, including unpaired surrogates.
3429 static final class StartS extends Start {
3433 boolean match(Matcher matcher, int i, CharSequence seq) {
3434 if (i > matcher.to - minLength) {
3435 matcher.hitEnd = true;
3438 int guard = matcher.to - minLength;
3439 while (i <= guard) {
3440 //if ((ret = next.match(matcher, i, seq)) || i == guard)
3441 if (next.match(matcher, i, seq)) {
3443 matcher.groups[0] = matcher.first;
3444 matcher.groups[1] = matcher.last;
3449 // Optimization to move to the next character. This is
3450 // faster than countChars(seq, i, 1).
3451 if (Character.isHighSurrogate(seq.charAt(i++))) {
3452 if (i < seq.length() &&
3453 Character.isLowSurrogate(seq.charAt(i))) {
3458 matcher.hitEnd = true;
3464 * Node to anchor at the beginning of input. This object implements the
3465 * match for a \A sequence, and the caret anchor will use this if not in
3468 static final class Begin extends Node {
3469 boolean match(Matcher matcher, int i, CharSequence seq) {
3470 int fromIndex = (matcher.anchoringBounds) ?
3472 if (i == fromIndex && next.match(matcher, i, seq)) {
3474 matcher.groups[0] = i;
3475 matcher.groups[1] = matcher.last;
3484 * Node to anchor at the end of input. This is the absolute end, so this
3485 * should not match at the last newline before the end as $ will.
3487 static final class End extends Node {
3488 boolean match(Matcher matcher, int i, CharSequence seq) {
3489 int endIndex = (matcher.anchoringBounds) ?
3490 matcher.to : matcher.getTextLength();
3491 if (i == endIndex) {
3492 matcher.hitEnd = true;
3493 return next.match(matcher, i, seq);
3500 * Node to anchor at the beginning of a line. This is essentially the
3501 * object to match for the multiline ^.
3503 static final class Caret extends Node {
3504 boolean match(Matcher matcher, int i, CharSequence seq) {
3505 int startIndex = matcher.from;
3506 int endIndex = matcher.to;
3507 if (!matcher.anchoringBounds) {
3509 endIndex = matcher.getTextLength();
3511 // Perl does not match ^ at end of input even after newline
3512 if (i == endIndex) {
3513 matcher.hitEnd = true;
3516 if (i > startIndex) {
3517 char ch = seq.charAt(i-1);
3518 if (ch != '\n' && ch != '\r'
3519 && (ch|1) != '\u2029'
3520 && ch != '\u0085' ) {
3523 // Should treat /r/n as one newline
3524 if (ch == '\r' && seq.charAt(i) == '\n')
3527 return next.match(matcher, i, seq);
3532 * Node to anchor at the beginning of a line when in unixdot mode.
3534 static final class UnixCaret extends Node {
3535 boolean match(Matcher matcher, int i, CharSequence seq) {
3536 int startIndex = matcher.from;
3537 int endIndex = matcher.to;
3538 if (!matcher.anchoringBounds) {
3540 endIndex = matcher.getTextLength();
3542 // Perl does not match ^ at end of input even after newline
3543 if (i == endIndex) {
3544 matcher.hitEnd = true;
3547 if (i > startIndex) {
3548 char ch = seq.charAt(i-1);
3553 return next.match(matcher, i, seq);
3558 * Node to match the location where the last match ended.
3559 * This is used for the \G construct.
3561 static final class LastMatch extends Node {
3562 boolean match(Matcher matcher, int i, CharSequence seq) {
3563 if (i != matcher.oldLast)
3565 return next.match(matcher, i, seq);
3570 * Node to anchor at the end of a line or the end of input based on the
3573 * When not in multiline mode, the $ can only match at the very end
3574 * of the input, unless the input ends in a line terminator in which
3575 * it matches right before the last line terminator.
3577 * Note that \r\n is considered an atomic line terminator.
3579 * Like ^ the $ operator matches at a position, it does not match the
3580 * line terminators themselves.
3582 static final class Dollar extends Node {
3584 Dollar(boolean mul) {
3587 boolean match(Matcher matcher, int i, CharSequence seq) {
3588 int endIndex = (matcher.anchoringBounds) ?
3589 matcher.to : matcher.getTextLength();
3591 if (i < endIndex - 2)
3593 if (i == endIndex - 2) {
3594 char ch = seq.charAt(i);
3597 ch = seq.charAt(i + 1);
3602 // Matches before any line terminator; also matches at the
3604 // Before line terminator:
3605 // If multiline, we match here no matter what
3606 // If not multiline, fall through so that the end
3607 // is marked as hit; this must be a /r/n or a /n
3608 // at the very end so the end was hit; more input
3609 // could make this not match here
3611 char ch = seq.charAt(i);
3613 // No match between \r\n
3614 if (i > 0 && seq.charAt(i-1) == '\r')
3617 return next.match(matcher, i, seq);
3618 } else if (ch == '\r' || ch == '\u0085' ||
3619 (ch|1) == '\u2029') {
3621 return next.match(matcher, i, seq);
3622 } else { // No line terminator, no match
3626 // Matched at current end so hit end
3627 matcher.hitEnd = true;
3628 // If a $ matches because of end of input, then more input
3629 // could cause it to fail!
3630 matcher.requireEnd = true;
3631 return next.match(matcher, i, seq);
3633 boolean study(TreeInfo info) {
3635 return info.deterministic;
3640 * Node to anchor at the end of a line or the end of input based on the
3641 * multiline mode when in unix lines mode.
3643 static final class UnixDollar extends Node {
3645 UnixDollar(boolean mul) {
3648 boolean match(Matcher matcher, int i, CharSequence seq) {
3649 int endIndex = (matcher.anchoringBounds) ?
3650 matcher.to : matcher.getTextLength();
3652 char ch = seq.charAt(i);
3654 // If not multiline, then only possible to
3655 // match at very end or one before end
3656 if (multiline == false && i != endIndex - 1)
3658 // If multiline return next.match without setting
3661 return next.match(matcher, i, seq);
3666 // Matching because at the end or 1 before the end;
3667 // more input could change this so set hitEnd
3668 matcher.hitEnd = true;
3669 // If a $ matches because of end of input, then more input
3670 // could cause it to fail!
3671 matcher.requireEnd = true;
3672 return next.match(matcher, i, seq);
3674 boolean study(TreeInfo info) {
3676 return info.deterministic;
3681 * Abstract node class to match one character satisfying some
3684 private static abstract class CharProperty extends Node {
3685 abstract boolean isSatisfiedBy(int ch);
3686 CharProperty complement() {
3687 return new CharProperty() {
3688 boolean isSatisfiedBy(int ch) {
3689 return ! CharProperty.this.isSatisfiedBy(ch);}};
3691 boolean match(Matcher matcher, int i, CharSequence seq) {
3692 if (i < matcher.to) {
3693 int ch = Character.codePointAt(seq, i);
3694 return isSatisfiedBy(ch)
3695 && next.match(matcher, i+Character.charCount(ch), seq);
3697 matcher.hitEnd = true;
3701 boolean study(TreeInfo info) {
3704 return next.study(info);
3709 * Optimized version of CharProperty that works only for
3710 * properties never satisfied by Supplementary characters.
3712 private static abstract class BmpCharProperty extends CharProperty {
3713 boolean match(Matcher matcher, int i, CharSequence seq) {
3714 if (i < matcher.to) {
3715 return isSatisfiedBy(seq.charAt(i))
3716 && next.match(matcher, i+1, seq);
3718 matcher.hitEnd = true;
3725 * Node class that matches a Supplementary Unicode character
3727 static final class SingleS extends CharProperty {
3729 SingleS(int c) { this.c = c; }
3730 boolean isSatisfiedBy(int ch) {
3736 * Optimization -- matches a given BMP character
3738 static final class Single extends BmpCharProperty {
3740 Single(int c) { this.c = c; }
3741 boolean isSatisfiedBy(int ch) {
3747 * Case insensitive matches a given BMP character
3749 static final class SingleI extends BmpCharProperty {
3752 SingleI(int lower, int upper) {
3756 boolean isSatisfiedBy(int ch) {
3757 return ch == lower || ch == upper;
3762 * Unicode case insensitive matches a given Unicode character
3764 static final class SingleU extends CharProperty {
3766 SingleU(int lower) {
3769 boolean isSatisfiedBy(int ch) {
3770 return lower == ch ||
3771 lower == Character.toLowerCase(Character.toUpperCase(ch));
3777 * Node class that matches a Unicode block.
3779 static final class Block extends CharProperty {
3780 final Character.UnicodeBlock block;
3781 Block(Character.UnicodeBlock block) {
3784 boolean isSatisfiedBy(int ch) {
3785 return block == Character.UnicodeBlock.of(ch);
3790 * Node class that matches a Unicode script
3792 static final class Script extends CharProperty {
3793 final Character.UnicodeScript script;
3794 Script(Character.UnicodeScript script) {
3795 this.script = script;
3797 boolean isSatisfiedBy(int ch) {
3798 return script == Character.UnicodeScript.of(ch);
3803 * Node class that matches a Unicode category.
3805 static final class Category extends CharProperty {
3807 Category(int typeMask) { this.typeMask = typeMask; }
3808 boolean isSatisfiedBy(int ch) {
3809 return (typeMask & (1 << Character.getType(ch))) != 0;
3814 * Node class that matches a Unicode "type"
3816 static final class Utype extends CharProperty {
3817 final UnicodeProp uprop;
3818 Utype(UnicodeProp uprop) { this.uprop = uprop; }
3819 boolean isSatisfiedBy(int ch) {
3820 return uprop.is(ch);
3826 * Node class that matches a POSIX type.
3828 static final class Ctype extends BmpCharProperty {
3830 Ctype(int ctype) { this.ctype = ctype; }
3831 boolean isSatisfiedBy(int ch) {
3832 return ch < 128 && ASCII.isType(ch, ctype);
3837 * Base class for all Slice nodes
3839 static class SliceNode extends Node {
3841 SliceNode(int[] buf) {
3844 boolean study(TreeInfo info) {
3845 info.minLength += buffer.length;
3846 info.maxLength += buffer.length;
3847 return next.study(info);
3852 * Node class for a case sensitive/BMP-only sequence of literal
3855 static final class Slice extends SliceNode {
3859 boolean match(Matcher matcher, int i, CharSequence seq) {
3861 int len = buf.length;
3862 for (int j=0; j<len; j++) {
3863 if ((i+j) >= matcher.to) {
3864 matcher.hitEnd = true;
3867 if (buf[j] != seq.charAt(i+j))
3870 return next.match(matcher, i+len, seq);
3875 * Node class for a case_insensitive/BMP-only sequence of literal
3878 static class SliceI extends SliceNode {
3882 boolean match(Matcher matcher, int i, CharSequence seq) {
3884 int len = buf.length;
3885 for (int j=0; j<len; j++) {
3886 if ((i+j) >= matcher.to) {
3887 matcher.hitEnd = true;
3890 int c = seq.charAt(i+j);
3892 buf[j] != ASCII.toLower(c))
3895 return next.match(matcher, i+len, seq);
3900 * Node class for a unicode_case_insensitive/BMP-only sequence of
3901 * literal characters. Uses unicode case folding.
3903 static final class SliceU extends SliceNode {
3907 boolean match(Matcher matcher, int i, CharSequence seq) {
3909 int len = buf.length;
3910 for (int j=0; j<len; j++) {
3911 if ((i+j) >= matcher.to) {
3912 matcher.hitEnd = true;
3915 int c = seq.charAt(i+j);
3917 buf[j] != Character.toLowerCase(Character.toUpperCase(c)))
3920 return next.match(matcher, i+len, seq);
3925 * Node class for a case sensitive sequence of literal characters
3926 * including supplementary characters.
3928 static final class SliceS extends SliceNode {
3932 boolean match(Matcher matcher, int i, CharSequence seq) {
3935 for (int j = 0; j < buf.length; j++) {
3936 if (x >= matcher.to) {
3937 matcher.hitEnd = true;
3940 int c = Character.codePointAt(seq, x);
3943 x += Character.charCount(c);
3944 if (x > matcher.to) {
3945 matcher.hitEnd = true;
3949 return next.match(matcher, x, seq);
3954 * Node class for a case insensitive sequence of literal characters
3955 * including supplementary characters.
3957 static class SliceIS extends SliceNode {
3958 SliceIS(int[] buf) {
3961 int toLower(int c) {
3962 return ASCII.toLower(c);
3964 boolean match(Matcher matcher, int i, CharSequence seq) {
3967 for (int j = 0; j < buf.length; j++) {
3968 if (x >= matcher.to) {
3969 matcher.hitEnd = true;
3972 int c = Character.codePointAt(seq, x);
3973 if (buf[j] != c && buf[j] != toLower(c))
3975 x += Character.charCount(c);
3976 if (x > matcher.to) {
3977 matcher.hitEnd = true;
3981 return next.match(matcher, x, seq);
3986 * Node class for a case insensitive sequence of literal characters.
3987 * Uses unicode case folding.
3989 static final class SliceUS extends SliceIS {
3990 SliceUS(int[] buf) {
3993 int toLower(int c) {
3994 return Character.toLowerCase(Character.toUpperCase(c));
3998 private static boolean inRange(int lower, int ch, int upper) {
3999 return lower <= ch && ch <= upper;
4003 * Returns node for matching characters within an explicit value range.
4005 private static CharProperty rangeFor(final int lower,
4007 return new CharProperty() {
4008 boolean isSatisfiedBy(int ch) {
4009 return inRange(lower, ch, upper);}};
4013 * Returns node for matching characters within an explicit value
4014 * range in a case insensitive manner.
4016 private CharProperty caseInsensitiveRangeFor(final int lower,
4018 if (has(UNICODE_CASE))
4019 return new CharProperty() {
4020 boolean isSatisfiedBy(int ch) {
4021 if (inRange(lower, ch, upper))
4023 int up = Character.toUpperCase(ch);
4024 return inRange(lower, up, upper) ||
4025 inRange(lower, Character.toLowerCase(up), upper);}};
4026 return new CharProperty() {
4027 boolean isSatisfiedBy(int ch) {
4028 return inRange(lower, ch, upper) ||
4029 ASCII.isAscii(ch) &&
4030 (inRange(lower, ASCII.toUpper(ch), upper) ||
4031 inRange(lower, ASCII.toLower(ch), upper));
4036 * Implements the Unicode category ALL and the dot metacharacter when
4039 static final class All extends CharProperty {
4040 boolean isSatisfiedBy(int ch) {
4046 * Node class for the dot metacharacter when dotall is not enabled.
4048 static final class Dot extends CharProperty {
4049 boolean isSatisfiedBy(int ch) {
4050 return (ch != '\n' && ch != '\r'
4051 && (ch|1) != '\u2029'
4057 * Node class for the dot metacharacter when dotall is not enabled
4058 * but UNIX_LINES is enabled.
4060 static final class UnixDot extends CharProperty {
4061 boolean isSatisfiedBy(int ch) {
4067 * The 0 or 1 quantifier. This one class implements all three types.
4069 static final class Ques extends Node {
4072 Ques(Node node, int type) {
4076 boolean match(Matcher matcher, int i, CharSequence seq) {
4079 return (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq))
4080 || next.match(matcher, i, seq);
4082 return next.match(matcher, i, seq)
4083 || (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq));
4085 if (atom.match(matcher, i, seq)) i = matcher.last;
4086 return next.match(matcher, i, seq);
4088 return atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq);
4091 boolean study(TreeInfo info) {
4092 if (type != INDEPENDENT) {
4093 int minL = info.minLength;
4095 info.minLength = minL;
4096 info.deterministic = false;
4097 return next.study(info);
4100 return next.study(info);
4106 * Handles the curly-brace style repetition with a specified minimum and
4107 * maximum occurrences. The * quantifier is handled as a special case.
4108 * This class handles the three types.
4110 static final class Curly extends Node {
4116 Curly(Node node, int cmin, int cmax, int type) {
4122 boolean match(Matcher matcher, int i, CharSequence seq) {
4124 for (j = 0; j < cmin; j++) {
4125 if (atom.match(matcher, i, seq)) {
4132 return match0(matcher, i, j, seq);
4133 else if (type == LAZY)
4134 return match1(matcher, i, j, seq);
4136 return match2(matcher, i, j, seq);
4139 // i is the index to start matching at
4140 // j is the number of atoms that have matched
4141 boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
4143 // We have matched the maximum... continue with the rest of
4144 // the regular expression
4145 return next.match(matcher, i, seq);
4148 while (atom.match(matcher, i, seq)) {
4149 // k is the length of this match
4150 int k = matcher.last - i;
4151 if (k == 0) // Zero length match
4153 // Move up index and number matched
4156 // We are greedy so match as many as we can
4158 if (!atom.match(matcher, i, seq))
4160 if (i + k != matcher.last) {
4161 if (match0(matcher, matcher.last, j+1, seq))
4168 // Handle backing off if match fails
4169 while (j >= backLimit) {
4170 if (next.match(matcher, i, seq))
4177 return next.match(matcher, i, seq);
4179 // Reluctant match. At this point, the minimum has been satisfied.
4180 // i is the index to start matching at
4181 // j is the number of atoms that have matched
4182 boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
4184 // Try finishing match without consuming any more
4185 if (next.match(matcher, i, seq))
4187 // At the maximum, no match found
4190 // Okay, must try one more atom
4191 if (!atom.match(matcher, i, seq))
4193 // If we haven't moved forward then must break out
4194 if (i == matcher.last)
4196 // Move up index and number matched
4201 boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
4202 for (; j < cmax; j++) {
4203 if (!atom.match(matcher, i, seq))
4205 if (i == matcher.last)
4209 return next.match(matcher, i, seq);
4211 boolean study(TreeInfo info) {
4212 // Save original info
4213 int minL = info.minLength;
4214 int maxL = info.maxLength;
4215 boolean maxV = info.maxValid;
4216 boolean detm = info.deterministic;
4221 int temp = info.minLength * cmin + minL;
4223 temp = 0xFFFFFFF; // arbitrary large number
4225 info.minLength = temp;
4227 if (maxV & info.maxValid) {
4228 temp = info.maxLength * cmax + maxL;
4229 info.maxLength = temp;
4231 info.maxValid = false;
4234 info.maxValid = false;
4237 if (info.deterministic && cmin == cmax)
4238 info.deterministic = detm;
4240 info.deterministic = false;
4242 return next.study(info);
4247 * Handles the curly-brace style repetition with a specified minimum and
4248 * maximum occurrences in deterministic cases. This is an iterative
4249 * optimization over the Prolog and Loop system which would handle this
4250 * in a recursive way. The * quantifier is handled as a special case.
4251 * If capture is true then this class saves group settings and ensures
4252 * that groups are unset when backing off of a group match.
4254 static final class GroupCurly extends Node {
4263 GroupCurly(Node node, int cmin, int cmax, int type, int local,
4264 int group, boolean capture) {
4269 this.localIndex = local;
4270 this.groupIndex = group;
4271 this.capture = capture;
4273 boolean match(Matcher matcher, int i, CharSequence seq) {
4274 int[] groups = matcher.groups;
4275 int[] locals = matcher.locals;
4276 int save0 = locals[localIndex];
4281 save1 = groups[groupIndex];
4282 save2 = groups[groupIndex+1];
4285 // Notify GroupTail there is no need to setup group info
4286 // because it will be set here
4287 locals[localIndex] = -1;
4290 for (int j = 0; j < cmin; j++) {
4291 if (atom.match(matcher, i, seq)) {
4293 groups[groupIndex] = i;
4294 groups[groupIndex+1] = matcher.last;
4303 if (type == GREEDY) {
4304 ret = match0(matcher, i, cmin, seq);
4305 } else if (type == LAZY) {
4306 ret = match1(matcher, i, cmin, seq);
4308 ret = match2(matcher, i, cmin, seq);
4312 locals[localIndex] = save0;
4314 groups[groupIndex] = save1;
4315 groups[groupIndex+1] = save2;
4320 // Aggressive group match
4321 boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
4322 int[] groups = matcher.groups;
4326 save0 = groups[groupIndex];
4327 save1 = groups[groupIndex+1];
4332 if (!atom.match(matcher, i, seq))
4334 int k = matcher.last - i;
4337 groups[groupIndex] = i;
4338 groups[groupIndex+1] = i + k;
4345 groups[groupIndex] = i;
4346 groups[groupIndex+1] = i + k;
4351 if (!atom.match(matcher, i, seq))
4353 if (i + k != matcher.last) {
4354 if (match0(matcher, i, j, seq))
4360 if (next.match(matcher, i, seq)) {
4362 groups[groupIndex+1] = i;
4363 groups[groupIndex] = i - k;
4370 groups[groupIndex+1] = i;
4371 groups[groupIndex] = i - k;
4379 groups[groupIndex] = save0;
4380 groups[groupIndex+1] = save1;
4382 return next.match(matcher, i, seq);
4384 // Reluctant matching
4385 boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
4387 if (next.match(matcher, i, seq))
4391 if (!atom.match(matcher, i, seq))
4393 if (i == matcher.last)
4396 matcher.groups[groupIndex] = i;
4397 matcher.groups[groupIndex+1] = matcher.last;
4403 // Possessive matching
4404 boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
4405 for (; j < cmax; j++) {
4406 if (!atom.match(matcher, i, seq)) {
4410 matcher.groups[groupIndex] = i;
4411 matcher.groups[groupIndex+1] = matcher.last;
4413 if (i == matcher.last) {
4418 return next.match(matcher, i, seq);
4420 boolean study(TreeInfo info) {
4421 // Save original info
4422 int minL = info.minLength;
4423 int maxL = info.maxLength;
4424 boolean maxV = info.maxValid;
4425 boolean detm = info.deterministic;
4430 int temp = info.minLength * cmin + minL;
4432 temp = 0xFFFFFFF; // Arbitrary large number
4434 info.minLength = temp;
4436 if (maxV & info.maxValid) {
4437 temp = info.maxLength * cmax + maxL;
4438 info.maxLength = temp;
4440 info.maxValid = false;
4443 info.maxValid = false;
4446 if (info.deterministic && cmin == cmax) {
4447 info.deterministic = detm;
4449 info.deterministic = false;
4452 return next.study(info);
4457 * A Guard node at the end of each atom node in a Branch. It
4458 * serves the purpose of chaining the "match" operation to
4459 * "next" but not the "study", so we can collect the TreeInfo
4460 * of each atom node without including the TreeInfo of the
4463 static final class BranchConn extends Node {
4465 boolean match(Matcher matcher, int i, CharSequence seq) {
4466 return next.match(matcher, i, seq);
4468 boolean study(TreeInfo info) {
4469 return info.deterministic;
4474 * Handles the branching of alternations. Note this is also used for
4475 * the ? quantifier to branch between the case where it matches once
4476 * and where it does not occur.
4478 static final class Branch extends Node {
4479 Node[] atoms = new Node[2];
4482 Branch(Node first, Node second, Node branchConn) {
4488 void add(Node node) {
4489 if (size >= atoms.length) {
4490 Node[] tmp = new Node[atoms.length*2];
4491 System.arraycopy(atoms, 0, tmp, 0, atoms.length);
4494 atoms[size++] = node;
4497 boolean match(Matcher matcher, int i, CharSequence seq) {
4498 for (int n = 0; n < size; n++) {
4499 if (atoms[n] == null) {
4500 if (conn.next.match(matcher, i, seq))
4502 } else if (atoms[n].match(matcher, i, seq)) {
4509 boolean study(TreeInfo info) {
4510 int minL = info.minLength;
4511 int maxL = info.maxLength;
4512 boolean maxV = info.maxValid;
4514 int minL2 = Integer.MAX_VALUE; //arbitrary large enough num
4516 for (int n = 0; n < size; n++) {
4518 if (atoms[n] != null)
4519 atoms[n].study(info);
4520 minL2 = Math.min(minL2, info.minLength);
4521 maxL2 = Math.max(maxL2, info.maxLength);
4522 maxV = (maxV & info.maxValid);
4529 conn.next.study(info);
4531 info.minLength += minL;
4532 info.maxLength += maxL;
4533 info.maxValid &= maxV;
4534 info.deterministic = false;
4540 * The GroupHead saves the location where the group begins in the locals
4541 * and restores them when the match is done.
4543 * The matchRef is used when a reference to this group is accessed later
4544 * in the expression. The locals will have a negative value in them to
4545 * indicate that we do not want to unset the group if the reference
4548 static final class GroupHead extends Node {
4550 GroupHead(int localCount) {
4551 localIndex = localCount;
4553 boolean match(Matcher matcher, int i, CharSequence seq) {
4554 int save = matcher.locals[localIndex];
4555 matcher.locals[localIndex] = i;
4556 boolean ret = next.match(matcher, i, seq);
4557 matcher.locals[localIndex] = save;
4560 boolean matchRef(Matcher matcher, int i, CharSequence seq) {
4561 int save = matcher.locals[localIndex];
4562 matcher.locals[localIndex] = ~i; // HACK
4563 boolean ret = next.match(matcher, i, seq);
4564 matcher.locals[localIndex] = save;
4570 * Recursive reference to a group in the regular expression. It calls
4571 * matchRef because if the reference fails to match we would not unset
4574 static final class GroupRef extends Node {
4576 GroupRef(GroupHead head) {
4579 boolean match(Matcher matcher, int i, CharSequence seq) {
4580 return head.matchRef(matcher, i, seq)
4581 && next.match(matcher, matcher.last, seq);
4583 boolean study(TreeInfo info) {
4584 info.maxValid = false;
4585 info.deterministic = false;
4586 return next.study(info);
4591 * The GroupTail handles the setting of group beginning and ending
4592 * locations when groups are successfully matched. It must also be able to
4593 * unset groups that have to be backed off of.
4595 * The GroupTail node is also used when a previous group is referenced,
4596 * and in that case no group information needs to be set.
4598 static final class GroupTail extends Node {
4601 GroupTail(int localCount, int groupCount) {
4602 localIndex = localCount;
4603 groupIndex = groupCount + groupCount;
4605 boolean match(Matcher matcher, int i, CharSequence seq) {
4606 int tmp = matcher.locals[localIndex];
4607 if (tmp >= 0) { // This is the normal group case.
4608 // Save the group so we can unset it if it
4609 // backs off of a match.
4610 int groupStart = matcher.groups[groupIndex];
4611 int groupEnd = matcher.groups[groupIndex+1];
4613 matcher.groups[groupIndex] = tmp;
4614 matcher.groups[groupIndex+1] = i;
4615 if (next.match(matcher, i, seq)) {
4618 matcher.groups[groupIndex] = groupStart;
4619 matcher.groups[groupIndex+1] = groupEnd;
4622 // This is a group reference case. We don't need to save any
4623 // group info because it isn't really a group.
4631 * This sets up a loop to handle a recursive quantifier structure.
4633 static final class Prolog extends Node {
4638 boolean match(Matcher matcher, int i, CharSequence seq) {
4639 return loop.matchInit(matcher, i, seq);
4641 boolean study(TreeInfo info) {
4642 return loop.study(info);
4647 * Handles the repetition count for a greedy Curly. The matchInit
4648 * is called from the Prolog to save the index of where the group
4649 * beginning is stored. A zero length group check occurs in the
4650 * normal match but is skipped in the matchInit.
4652 static class Loop extends Node {
4654 int countIndex; // local count index in matcher locals
4655 int beginIndex; // group beginning index
4657 Loop(int countIndex, int beginIndex) {
4658 this.countIndex = countIndex;
4659 this.beginIndex = beginIndex;
4661 boolean match(Matcher matcher, int i, CharSequence seq) {
4662 // Avoid infinite loop in zero-length case.
4663 if (i > matcher.locals[beginIndex]) {
4664 int count = matcher.locals[countIndex];
4666 // This block is for before we reach the minimum
4667 // iterations required for the loop to match
4669 matcher.locals[countIndex] = count + 1;
4670 boolean b = body.match(matcher, i, seq);
4671 // If match failed we must backtrack, so
4672 // the loop count should NOT be incremented
4674 matcher.locals[countIndex] = count;
4675 // Return success or failure since we are under
4679 // This block is for after we have the minimum
4680 // iterations required for the loop to match
4682 matcher.locals[countIndex] = count + 1;
4683 boolean b = body.match(matcher, i, seq);
4684 // If match failed we must backtrack, so
4685 // the loop count should NOT be incremented
4687 matcher.locals[countIndex] = count;
4692 return next.match(matcher, i, seq);
4694 boolean matchInit(Matcher matcher, int i, CharSequence seq) {
4695 int save = matcher.locals[countIndex];
4696 boolean ret = false;
4698 matcher.locals[countIndex] = 1;
4699 ret = body.match(matcher, i, seq);
4700 } else if (0 < cmax) {
4701 matcher.locals[countIndex] = 1;
4702 ret = body.match(matcher, i, seq);
4704 ret = next.match(matcher, i, seq);
4706 ret = next.match(matcher, i, seq);
4708 matcher.locals[countIndex] = save;
4711 boolean study(TreeInfo info) {
4712 info.maxValid = false;
4713 info.deterministic = false;
4719 * Handles the repetition count for a reluctant Curly. The matchInit
4720 * is called from the Prolog to save the index of where the group
4721 * beginning is stored. A zero length group check occurs in the
4722 * normal match but is skipped in the matchInit.
4724 static final class LazyLoop extends Loop {
4725 LazyLoop(int countIndex, int beginIndex) {
4726 super(countIndex, beginIndex);
4728 boolean match(Matcher matcher, int i, CharSequence seq) {
4729 // Check for zero length group
4730 if (i > matcher.locals[beginIndex]) {
4731 int count = matcher.locals[countIndex];
4733 matcher.locals[countIndex] = count + 1;
4734 boolean result = body.match(matcher, i, seq);
4735 // If match failed we must backtrack, so
4736 // the loop count should NOT be incremented
4738 matcher.locals[countIndex] = count;
4741 if (next.match(matcher, i, seq))
4744 matcher.locals[countIndex] = count + 1;
4745 boolean result = body.match(matcher, i, seq);
4746 // If match failed we must backtrack, so
4747 // the loop count should NOT be incremented
4749 matcher.locals[countIndex] = count;
4754 return next.match(matcher, i, seq);
4756 boolean matchInit(Matcher matcher, int i, CharSequence seq) {
4757 int save = matcher.locals[countIndex];
4758 boolean ret = false;
4760 matcher.locals[countIndex] = 1;
4761 ret = body.match(matcher, i, seq);
4762 } else if (next.match(matcher, i, seq)) {
4764 } else if (0 < cmax) {
4765 matcher.locals[countIndex] = 1;
4766 ret = body.match(matcher, i, seq);
4768 matcher.locals[countIndex] = save;
4771 boolean study(TreeInfo info) {
4772 info.maxValid = false;
4773 info.deterministic = false;
4779 * Refers to a group in the regular expression. Attempts to match
4780 * whatever the group referred to last matched.
4782 static class BackRef extends Node {
4784 BackRef(int groupCount) {
4786 groupIndex = groupCount + groupCount;
4788 boolean match(Matcher matcher, int i, CharSequence seq) {
4789 int j = matcher.groups[groupIndex];
4790 int k = matcher.groups[groupIndex+1];
4792 int groupSize = k - j;
4794 // If the referenced group didn't match, neither can this
4798 // If there isn't enough input left no match
4799 if (i + groupSize > matcher.to) {
4800 matcher.hitEnd = true;
4804 // Check each new char to make sure it matches what the group
4805 // referenced matched last time around
4806 for (int index=0; index<groupSize; index++)
4807 if (seq.charAt(i+index) != seq.charAt(j+index))
4810 return next.match(matcher, i+groupSize, seq);
4812 boolean study(TreeInfo info) {
4813 info.maxValid = false;
4814 return next.study(info);
4818 static class CIBackRef extends Node {
4820 boolean doUnicodeCase;
4821 CIBackRef(int groupCount, boolean doUnicodeCase) {
4823 groupIndex = groupCount + groupCount;
4824 this.doUnicodeCase = doUnicodeCase;
4826 boolean match(Matcher matcher, int i, CharSequence seq) {
4827 int j = matcher.groups[groupIndex];
4828 int k = matcher.groups[groupIndex+1];
4830 int groupSize = k - j;
4832 // If the referenced group didn't match, neither can this
4836 // If there isn't enough input left no match
4837 if (i + groupSize > matcher.to) {
4838 matcher.hitEnd = true;
4842 // Check each new char to make sure it matches what the group
4843 // referenced matched last time around
4845 for (int index=0; index<groupSize; index++) {
4846 int c1 = Character.codePointAt(seq, x);
4847 int c2 = Character.codePointAt(seq, j);
4849 if (doUnicodeCase) {
4850 int cc1 = Character.toUpperCase(c1);
4851 int cc2 = Character.toUpperCase(c2);
4853 Character.toLowerCase(cc1) !=
4854 Character.toLowerCase(cc2))
4857 if (ASCII.toLower(c1) != ASCII.toLower(c2))
4861 x += Character.charCount(c1);
4862 j += Character.charCount(c2);
4865 return next.match(matcher, i+groupSize, seq);
4867 boolean study(TreeInfo info) {
4868 info.maxValid = false;
4869 return next.study(info);
4874 * Searches until the next instance of its atom. This is useful for
4875 * finding the atom efficiently without passing an instance of it
4876 * (greedy problem) and without a lot of wasted search time (reluctant
4879 static final class First extends Node {
4882 this.atom = BnM.optimize(node);
4884 boolean match(Matcher matcher, int i, CharSequence seq) {
4885 if (atom instanceof BnM) {
4886 return atom.match(matcher, i, seq)
4887 && next.match(matcher, matcher.last, seq);
4890 if (i > matcher.to) {
4891 matcher.hitEnd = true;
4894 if (atom.match(matcher, i, seq)) {
4895 return next.match(matcher, matcher.last, seq);
4897 i += countChars(seq, i, 1);
4901 boolean study(TreeInfo info) {
4903 info.maxValid = false;
4904 info.deterministic = false;
4905 return next.study(info);
4909 static final class Conditional extends Node {
4910 Node cond, yes, not;
4911 Conditional(Node cond, Node yes, Node not) {
4916 boolean match(Matcher matcher, int i, CharSequence seq) {
4917 if (cond.match(matcher, i, seq)) {
4918 return yes.match(matcher, i, seq);
4920 return not.match(matcher, i, seq);
4923 boolean study(TreeInfo info) {
4924 int minL = info.minLength;
4925 int maxL = info.maxLength;
4926 boolean maxV = info.maxValid;
4930 int minL2 = info.minLength;
4931 int maxL2 = info.maxLength;
4932 boolean maxV2 = info.maxValid;
4936 info.minLength = minL + Math.min(minL2, info.minLength);
4937 info.maxLength = maxL + Math.max(maxL2, info.maxLength);
4938 info.maxValid = (maxV & maxV2 & info.maxValid);
4939 info.deterministic = false;
4940 return next.study(info);
4945 * Zero width positive lookahead.
4947 static final class Pos extends Node {
4952 boolean match(Matcher matcher, int i, CharSequence seq) {
4953 int savedTo = matcher.to;
4954 boolean conditionMatched = false;
4956 // Relax transparent region boundaries for lookahead
4957 if (matcher.transparentBounds)
4958 matcher.to = matcher.getTextLength();
4960 conditionMatched = cond.match(matcher, i, seq);
4962 // Reinstate region boundaries
4963 matcher.to = savedTo;
4965 return conditionMatched && next.match(matcher, i, seq);
4970 * Zero width negative lookahead.
4972 static final class Neg extends Node {
4977 boolean match(Matcher matcher, int i, CharSequence seq) {
4978 int savedTo = matcher.to;
4979 boolean conditionMatched = false;
4981 // Relax transparent region boundaries for lookahead
4982 if (matcher.transparentBounds)
4983 matcher.to = matcher.getTextLength();
4985 if (i < matcher.to) {
4986 conditionMatched = !cond.match(matcher, i, seq);
4988 // If a negative lookahead succeeds then more input
4989 // could cause it to fail!
4990 matcher.requireEnd = true;
4991 conditionMatched = !cond.match(matcher, i, seq);
4994 // Reinstate region boundaries
4995 matcher.to = savedTo;
4997 return conditionMatched && next.match(matcher, i, seq);
5002 * For use with lookbehinds; matches the position where the lookbehind
5005 static Node lookbehindEnd = new Node() {
5006 boolean match(Matcher matcher, int i, CharSequence seq) {
5007 return i == matcher.lookbehindTo;
5012 * Zero width positive lookbehind.
5014 static class Behind extends Node {
5017 Behind(Node cond, int rmax, int rmin) {
5023 boolean match(Matcher matcher, int i, CharSequence seq) {
5024 int savedFrom = matcher.from;
5025 boolean conditionMatched = false;
5026 int startIndex = (!matcher.transparentBounds) ?
5028 int from = Math.max(i - rmax, startIndex);
5030 int savedLBT = matcher.lookbehindTo;
5031 matcher.lookbehindTo = i;
5032 // Relax transparent region boundaries for lookbehind
5033 if (matcher.transparentBounds)
5035 for (int j = i - rmin; !conditionMatched && j >= from; j--) {
5036 conditionMatched = cond.match(matcher, j, seq);
5038 matcher.from = savedFrom;
5039 matcher.lookbehindTo = savedLBT;
5040 return conditionMatched && next.match(matcher, i, seq);
5045 * Zero width positive lookbehind, including supplementary
5046 * characters or unpaired surrogates.
5048 static final class BehindS extends Behind {
5049 BehindS(Node cond, int rmax, int rmin) {
5050 super(cond, rmax, rmin);
5052 boolean match(Matcher matcher, int i, CharSequence seq) {
5053 int rmaxChars = countChars(seq, i, -rmax);
5054 int rminChars = countChars(seq, i, -rmin);
5055 int savedFrom = matcher.from;
5056 int startIndex = (!matcher.transparentBounds) ?
5058 boolean conditionMatched = false;
5059 int from = Math.max(i - rmaxChars, startIndex);
5061 int savedLBT = matcher.lookbehindTo;
5062 matcher.lookbehindTo = i;
5063 // Relax transparent region boundaries for lookbehind
5064 if (matcher.transparentBounds)
5067 for (int j = i - rminChars;
5068 !conditionMatched && j >= from;
5069 j -= j>from ? countChars(seq, j, -1) : 1) {
5070 conditionMatched = cond.match(matcher, j, seq);
5072 matcher.from = savedFrom;
5073 matcher.lookbehindTo = savedLBT;
5074 return conditionMatched && next.match(matcher, i, seq);
5079 * Zero width negative lookbehind.
5081 static class NotBehind extends Node {
5084 NotBehind(Node cond, int rmax, int rmin) {
5090 boolean match(Matcher matcher, int i, CharSequence seq) {
5091 int savedLBT = matcher.lookbehindTo;
5092 int savedFrom = matcher.from;
5093 boolean conditionMatched = false;
5094 int startIndex = (!matcher.transparentBounds) ?
5096 int from = Math.max(i - rmax, startIndex);
5097 matcher.lookbehindTo = i;
5098 // Relax transparent region boundaries for lookbehind
5099 if (matcher.transparentBounds)
5101 for (int j = i - rmin; !conditionMatched && j >= from; j--) {
5102 conditionMatched = cond.match(matcher, j, seq);
5104 // Reinstate region boundaries
5105 matcher.from = savedFrom;
5106 matcher.lookbehindTo = savedLBT;
5107 return !conditionMatched && next.match(matcher, i, seq);
5112 * Zero width negative lookbehind, including supplementary
5113 * characters or unpaired surrogates.
5115 static final class NotBehindS extends NotBehind {
5116 NotBehindS(Node cond, int rmax, int rmin) {
5117 super(cond, rmax, rmin);
5119 boolean match(Matcher matcher, int i, CharSequence seq) {
5120 int rmaxChars = countChars(seq, i, -rmax);
5121 int rminChars = countChars(seq, i, -rmin);
5122 int savedFrom = matcher.from;
5123 int savedLBT = matcher.lookbehindTo;
5124 boolean conditionMatched = false;
5125 int startIndex = (!matcher.transparentBounds) ?
5127 int from = Math.max(i - rmaxChars, startIndex);
5128 matcher.lookbehindTo = i;
5129 // Relax transparent region boundaries for lookbehind
5130 if (matcher.transparentBounds)
5132 for (int j = i - rminChars;
5133 !conditionMatched && j >= from;
5134 j -= j>from ? countChars(seq, j, -1) : 1) {
5135 conditionMatched = cond.match(matcher, j, seq);
5137 //Reinstate region boundaries
5138 matcher.from = savedFrom;
5139 matcher.lookbehindTo = savedLBT;
5140 return !conditionMatched && next.match(matcher, i, seq);
5145 * Returns the set union of two CharProperty nodes.
5147 private static CharProperty union(final CharProperty lhs,
5148 final CharProperty rhs) {
5149 return new CharProperty() {
5150 boolean isSatisfiedBy(int ch) {
5151 return lhs.isSatisfiedBy(ch) || rhs.isSatisfiedBy(ch);}};
5155 * Returns the set intersection of two CharProperty nodes.
5157 private static CharProperty intersection(final CharProperty lhs,
5158 final CharProperty rhs) {
5159 return new CharProperty() {
5160 boolean isSatisfiedBy(int ch) {
5161 return lhs.isSatisfiedBy(ch) && rhs.isSatisfiedBy(ch);}};
5165 * Returns the set difference of two CharProperty nodes.
5167 private static CharProperty setDifference(final CharProperty lhs,
5168 final CharProperty rhs) {
5169 return new CharProperty() {
5170 boolean isSatisfiedBy(int ch) {
5171 return ! rhs.isSatisfiedBy(ch) && lhs.isSatisfiedBy(ch);}};
5175 * Handles word boundaries. Includes a field to allow this one class to
5176 * deal with the different types of word boundaries we can match. The word
5177 * characters include underscores, letters, and digits. Non spacing marks
5178 * can are also part of a word if they have a base character, otherwise
5179 * they are ignored for purposes of finding word boundaries.
5181 static final class Bound extends Node {
5182 static int LEFT = 0x1;
5183 static int RIGHT= 0x2;
5184 static int BOTH = 0x3;
5185 static int NONE = 0x4;
5188 Bound(int n, boolean useUWORD) {
5190 this.useUWORD = useUWORD;
5193 boolean isWord(int ch) {
5194 return useUWORD ? UnicodeProp.WORD.is(ch)
5195 : (ch == '_' || Character.isLetterOrDigit(ch));
5198 int check(Matcher matcher, int i, CharSequence seq) {
5200 boolean left = false;
5201 int startIndex = matcher.from;
5202 int endIndex = matcher.to;
5203 if (matcher.transparentBounds) {
5205 endIndex = matcher.getTextLength();
5207 if (i > startIndex) {
5208 ch = Character.codePointBefore(seq, i);
5209 left = (isWord(ch) ||
5210 ((Character.getType(ch) == Character.NON_SPACING_MARK)
5211 && hasBaseCharacter(matcher, i-1, seq)));
5213 boolean right = false;
5215 ch = Character.codePointAt(seq, i);
5216 right = (isWord(ch) ||
5217 ((Character.getType(ch) == Character.NON_SPACING_MARK)
5218 && hasBaseCharacter(matcher, i, seq)));
5220 // Tried to access char past the end
5221 matcher.hitEnd = true;
5222 // The addition of another char could wreck a boundary
5223 matcher.requireEnd = true;
5225 return ((left ^ right) ? (right ? LEFT : RIGHT) : NONE);
5227 boolean match(Matcher matcher, int i, CharSequence seq) {
5228 return (check(matcher, i, seq) & type) > 0
5229 && next.match(matcher, i, seq);
5234 * Non spacing marks only count as word characters in bounds calculations
5235 * if they have a base character.
5237 private static boolean hasBaseCharacter(Matcher matcher, int i,
5240 int start = (!matcher.transparentBounds) ?
5242 for (int x=i; x >= start; x--) {
5243 int ch = Character.codePointAt(seq, x);
5244 if (Character.isLetterOrDigit(ch))
5246 if (Character.getType(ch) == Character.NON_SPACING_MARK)
5254 * Attempts to match a slice in the input using the Boyer-Moore string
5255 * matching algorithm. The algorithm is based on the idea that the
5256 * pattern can be shifted farther ahead in the search text if it is
5257 * matched right to left.
5259 * The pattern is compared to the input one character at a time, from
5260 * the rightmost character in the pattern to the left. If the characters
5261 * all match the pattern has been found. If a character does not match,
5262 * the pattern is shifted right a distance that is the maximum of two
5263 * functions, the bad character shift and the good suffix shift. This
5264 * shift moves the attempted match position through the input more
5265 * quickly than a naive one position at a time check.
5267 * The bad character shift is based on the character from the text that
5268 * did not match. If the character does not appear in the pattern, the
5269 * pattern can be shifted completely beyond the bad character. If the
5270 * character does occur in the pattern, the pattern can be shifted to
5271 * line the pattern up with the next occurrence of that character.
5273 * The good suffix shift is based on the idea that some subset on the right
5274 * side of the pattern has matched. When a bad character is found, the
5275 * pattern can be shifted right by the pattern length if the subset does
5276 * not occur again in pattern, or by the amount of distance to the
5277 * next occurrence of the subset in the pattern.
5279 * Boyer-Moore search methods adapted from code by Amy Yu.
5281 static class BnM extends Node {
5287 * Pre calculates arrays needed to generate the bad character
5288 * shift and the good suffix shift. Only the last seven bits
5289 * are used to see if chars match; This keeps the tables small
5290 * and covers the heavily used ASCII range, but occasionally
5291 * results in an aliased match for the bad character shift.
5293 static Node optimize(Node node) {
5294 if (!(node instanceof Slice)) {
5298 int[] src = ((Slice) node).buffer;
5299 int patternLength = src.length;
5300 // The BM algorithm requires a bit of overhead;
5301 // If the pattern is short don't use it, since
5302 // a shift larger than the pattern length cannot
5304 if (patternLength < 4) {
5308 int[] lastOcc = new int[128];
5309 int[] optoSft = new int[patternLength];
5310 // Precalculate part of the bad character shift
5311 // It is a table for where in the pattern each
5312 // lower 7-bit value occurs
5313 for (i = 0; i < patternLength; i++) {
5314 lastOcc[src[i]&0x7F] = i + 1;
5316 // Precalculate the good suffix shift
5317 // i is the shift amount being considered
5318 NEXT: for (i = patternLength; i > 0; i--) {
5319 // j is the beginning index of suffix being considered
5320 for (j = patternLength - 1; j >= i; j--) {
5321 // Testing for good suffix
5322 if (src[j] == src[j-i]) {
5323 // src[j..len] is a good suffix
5326 // No match. The array has already been
5327 // filled up with correct values before.
5331 // This fills up the remaining of optoSft
5332 // any suffix can not have larger shift amount
5333 // then its sub-suffix. Why???
5338 // Set the guard value because of unicode compression
5339 optoSft[patternLength-1] = 1;
5340 if (node instanceof SliceS)
5341 return new BnMS(src, lastOcc, optoSft, node.next);
5342 return new BnM(src, lastOcc, optoSft, node.next);
5344 BnM(int[] src, int[] lastOcc, int[] optoSft, Node next) {
5346 this.lastOcc = lastOcc;
5347 this.optoSft = optoSft;
5350 boolean match(Matcher matcher, int i, CharSequence seq) {
5352 int patternLength = src.length;
5353 int last = matcher.to - patternLength;
5355 // Loop over all possible match positions in text
5356 NEXT: while (i <= last) {
5357 // Loop over pattern from right to left
5358 for (int j = patternLength - 1; j >= 0; j--) {
5359 int ch = seq.charAt(i+j);
5361 // Shift search to the right by the maximum of the
5362 // bad character shift and the good suffix shift
5363 i += Math.max(j + 1 - lastOcc[ch&0x7F], optoSft[j]);
5367 // Entire pattern matched starting at i
5369 boolean ret = next.match(matcher, i + patternLength, seq);
5372 matcher.groups[0] = matcher.first;
5373 matcher.groups[1] = matcher.last;
5378 // BnM is only used as the leading node in the unanchored case,
5379 // and it replaced its Start() which always searches to the end
5380 // if it doesn't find what it's looking for, so hitEnd is true.
5381 matcher.hitEnd = true;
5384 boolean study(TreeInfo info) {
5385 info.minLength += buffer.length;
5386 info.maxValid = false;
5387 return next.study(info);
5392 * Supplementary support version of BnM(). Unpaired surrogates are
5393 * also handled by this class.
5395 static final class BnMS extends BnM {
5398 BnMS(int[] src, int[] lastOcc, int[] optoSft, Node next) {
5399 super(src, lastOcc, optoSft, next);
5400 for (int x = 0; x < buffer.length; x++) {
5401 lengthInChars += Character.charCount(buffer[x]);
5404 boolean match(Matcher matcher, int i, CharSequence seq) {
5406 int patternLength = src.length;
5407 int last = matcher.to - lengthInChars;
5409 // Loop over all possible match positions in text
5410 NEXT: while (i <= last) {
5411 // Loop over pattern from right to left
5413 for (int j = countChars(seq, i, patternLength), x = patternLength - 1;
5414 j > 0; j -= Character.charCount(ch), x--) {
5415 ch = Character.codePointBefore(seq, i+j);
5417 // Shift search to the right by the maximum of the
5418 // bad character shift and the good suffix shift
5419 int n = Math.max(x + 1 - lastOcc[ch&0x7F], optoSft[x]);
5420 i += countChars(seq, i, n);
5424 // Entire pattern matched starting at i
5426 boolean ret = next.match(matcher, i + lengthInChars, seq);
5429 matcher.groups[0] = matcher.first;
5430 matcher.groups[1] = matcher.last;
5433 i += countChars(seq, i, 1);
5435 matcher.hitEnd = true;
5440 ///////////////////////////////////////////////////////////////////////////////
5441 ///////////////////////////////////////////////////////////////////////////////
5444 * This must be the very first initializer.
5446 static Node accept = new Node();
5448 static Node lastAccept = new LastNode();
5450 private static class CharPropertyNames {
5452 static CharProperty charPropertyFor(String name) {
5453 CharPropertyFactory m = map.get(name);
5454 return m == null ? null : m.make();
5457 private static abstract class CharPropertyFactory {
5458 abstract CharProperty make();
5461 private static void defCategory(String name,
5462 final int typeMask) {
5463 map.put(name, new CharPropertyFactory() {
5464 CharProperty make() { return new Category(typeMask);}});
5467 private static void defRange(String name,
5468 final int lower, final int upper) {
5469 map.put(name, new CharPropertyFactory() {
5470 CharProperty make() { return rangeFor(lower, upper);}});
5473 private static void defCtype(String name,
5475 map.put(name, new CharPropertyFactory() {
5476 CharProperty make() { return new Ctype(ctype);}});
5479 private static abstract class CloneableProperty
5480 extends CharProperty implements Cloneable
5482 public CloneableProperty clone() {
5484 return (CloneableProperty) super.clone();
5485 } catch (CloneNotSupportedException e) {
5486 throw new AssertionError(e);
5491 private static void defClone(String name,
5492 final CloneableProperty p) {
5493 map.put(name, new CharPropertyFactory() {
5494 CharProperty make() { return p.clone();}});
5497 private static final HashMap<String, CharPropertyFactory> map
5501 // Unicode character property aliases, defined in
5502 // http://www.unicode.org/Public/UNIDATA/PropertyValueAliases.txt
5503 defCategory("Cn", 1<<Character.UNASSIGNED);
5504 defCategory("Lu", 1<<Character.UPPERCASE_LETTER);
5505 defCategory("Ll", 1<<Character.LOWERCASE_LETTER);
5506 defCategory("Lt", 1<<Character.TITLECASE_LETTER);
5507 defCategory("Lm", 1<<Character.MODIFIER_LETTER);
5508 defCategory("Lo", 1<<Character.OTHER_LETTER);
5509 defCategory("Mn", 1<<Character.NON_SPACING_MARK);
5510 defCategory("Me", 1<<Character.ENCLOSING_MARK);
5511 defCategory("Mc", 1<<Character.COMBINING_SPACING_MARK);
5512 defCategory("Nd", 1<<Character.DECIMAL_DIGIT_NUMBER);
5513 defCategory("Nl", 1<<Character.LETTER_NUMBER);
5514 defCategory("No", 1<<Character.OTHER_NUMBER);
5515 defCategory("Zs", 1<<Character.SPACE_SEPARATOR);
5516 defCategory("Zl", 1<<Character.LINE_SEPARATOR);
5517 defCategory("Zp", 1<<Character.PARAGRAPH_SEPARATOR);
5518 defCategory("Cc", 1<<Character.CONTROL);
5519 defCategory("Cf", 1<<Character.FORMAT);
5520 defCategory("Co", 1<<Character.PRIVATE_USE);
5521 defCategory("Cs", 1<<Character.SURROGATE);
5522 defCategory("Pd", 1<<Character.DASH_PUNCTUATION);
5523 defCategory("Ps", 1<<Character.START_PUNCTUATION);
5524 defCategory("Pe", 1<<Character.END_PUNCTUATION);
5525 defCategory("Pc", 1<<Character.CONNECTOR_PUNCTUATION);
5526 defCategory("Po", 1<<Character.OTHER_PUNCTUATION);
5527 defCategory("Sm", 1<<Character.MATH_SYMBOL);
5528 defCategory("Sc", 1<<Character.CURRENCY_SYMBOL);
5529 defCategory("Sk", 1<<Character.MODIFIER_SYMBOL);
5530 defCategory("So", 1<<Character.OTHER_SYMBOL);
5531 defCategory("Pi", 1<<Character.INITIAL_QUOTE_PUNCTUATION);
5532 defCategory("Pf", 1<<Character.FINAL_QUOTE_PUNCTUATION);
5533 defCategory("L", ((1<<Character.UPPERCASE_LETTER) |
5534 (1<<Character.LOWERCASE_LETTER) |
5535 (1<<Character.TITLECASE_LETTER) |
5536 (1<<Character.MODIFIER_LETTER) |
5537 (1<<Character.OTHER_LETTER)));
5538 defCategory("M", ((1<<Character.NON_SPACING_MARK) |
5539 (1<<Character.ENCLOSING_MARK) |
5540 (1<<Character.COMBINING_SPACING_MARK)));
5541 defCategory("N", ((1<<Character.DECIMAL_DIGIT_NUMBER) |
5542 (1<<Character.LETTER_NUMBER) |
5543 (1<<Character.OTHER_NUMBER)));
5544 defCategory("Z", ((1<<Character.SPACE_SEPARATOR) |
5545 (1<<Character.LINE_SEPARATOR) |
5546 (1<<Character.PARAGRAPH_SEPARATOR)));
5547 defCategory("C", ((1<<Character.CONTROL) |
5548 (1<<Character.FORMAT) |
5549 (1<<Character.PRIVATE_USE) |
5550 (1<<Character.SURROGATE))); // Other
5551 defCategory("P", ((1<<Character.DASH_PUNCTUATION) |
5552 (1<<Character.START_PUNCTUATION) |
5553 (1<<Character.END_PUNCTUATION) |
5554 (1<<Character.CONNECTOR_PUNCTUATION) |
5555 (1<<Character.OTHER_PUNCTUATION) |
5556 (1<<Character.INITIAL_QUOTE_PUNCTUATION) |
5557 (1<<Character.FINAL_QUOTE_PUNCTUATION)));
5558 defCategory("S", ((1<<Character.MATH_SYMBOL) |
5559 (1<<Character.CURRENCY_SYMBOL) |
5560 (1<<Character.MODIFIER_SYMBOL) |
5561 (1<<Character.OTHER_SYMBOL)));
5562 defCategory("LC", ((1<<Character.UPPERCASE_LETTER) |
5563 (1<<Character.LOWERCASE_LETTER) |
5564 (1<<Character.TITLECASE_LETTER)));
5565 defCategory("LD", ((1<<Character.UPPERCASE_LETTER) |
5566 (1<<Character.LOWERCASE_LETTER) |
5567 (1<<Character.TITLECASE_LETTER) |
5568 (1<<Character.MODIFIER_LETTER) |
5569 (1<<Character.OTHER_LETTER) |
5570 (1<<Character.DECIMAL_DIGIT_NUMBER)));
5571 defRange("L1", 0x00, 0xFF); // Latin-1
5572 map.put("all", new CharPropertyFactory() {
5573 CharProperty make() { return new All(); }});
5575 // Posix regular expression character classes, defined in
5576 // http://www.unix.org/onlinepubs/009695399/basedefs/xbd_chap09.html
5577 defRange("ASCII", 0x00, 0x7F); // ASCII
5578 defCtype("Alnum", ASCII.ALNUM); // Alphanumeric characters
5579 defCtype("Alpha", ASCII.ALPHA); // Alphabetic characters
5580 defCtype("Blank", ASCII.BLANK); // Space and tab characters
5581 defCtype("Cntrl", ASCII.CNTRL); // Control characters
5582 defRange("Digit", '0', '9'); // Numeric characters
5583 defCtype("Graph", ASCII.GRAPH); // printable and visible
5584 defRange("Lower", 'a', 'z'); // Lower-case alphabetic
5585 defRange("Print", 0x20, 0x7E); // Printable characters
5586 defCtype("Punct", ASCII.PUNCT); // Punctuation characters
5587 defCtype("Space", ASCII.SPACE); // Space characters
5588 defRange("Upper", 'A', 'Z'); // Upper-case alphabetic
5589 defCtype("XDigit",ASCII.XDIGIT); // hexadecimal digits
5591 // Java character properties, defined by methods in Character.java
5592 defClone("javaLowerCase", new CloneableProperty() {
5593 boolean isSatisfiedBy(int ch) {
5594 return Character.isLowerCase(ch);}});
5595 defClone("javaUpperCase", new CloneableProperty() {
5596 boolean isSatisfiedBy(int ch) {
5597 return Character.isUpperCase(ch);}});
5598 defClone("javaAlphabetic", new CloneableProperty() {
5599 boolean isSatisfiedBy(int ch) {
5600 return Character.isAlphabetic(ch);}});
5601 defClone("javaIdeographic", new CloneableProperty() {
5602 boolean isSatisfiedBy(int ch) {
5603 return Character.isIdeographic(ch);}});
5604 defClone("javaTitleCase", new CloneableProperty() {
5605 boolean isSatisfiedBy(int ch) {
5606 return Character.isTitleCase(ch);}});
5607 defClone("javaDigit", new CloneableProperty() {
5608 boolean isSatisfiedBy(int ch) {
5609 return Character.isDigit(ch);}});
5610 defClone("javaDefined", new CloneableProperty() {
5611 boolean isSatisfiedBy(int ch) {
5612 return Character.isDefined(ch);}});
5613 defClone("javaLetter", new CloneableProperty() {
5614 boolean isSatisfiedBy(int ch) {
5615 return Character.isLetter(ch);}});
5616 defClone("javaLetterOrDigit", new CloneableProperty() {
5617 boolean isSatisfiedBy(int ch) {
5618 return Character.isLetterOrDigit(ch);}});
5619 defClone("javaJavaIdentifierStart", new CloneableProperty() {
5620 boolean isSatisfiedBy(int ch) {
5621 return Character.isJavaIdentifierStart(ch);}});
5622 defClone("javaJavaIdentifierPart", new CloneableProperty() {
5623 boolean isSatisfiedBy(int ch) {
5624 return Character.isJavaIdentifierPart(ch);}});
5625 defClone("javaUnicodeIdentifierStart", new CloneableProperty() {
5626 boolean isSatisfiedBy(int ch) {
5627 return Character.isUnicodeIdentifierStart(ch);}});
5628 defClone("javaUnicodeIdentifierPart", new CloneableProperty() {
5629 boolean isSatisfiedBy(int ch) {
5630 return Character.isUnicodeIdentifierPart(ch);}});
5631 defClone("javaIdentifierIgnorable", new CloneableProperty() {
5632 boolean isSatisfiedBy(int ch) {
5633 return Character.isIdentifierIgnorable(ch);}});
5634 defClone("javaSpaceChar", new CloneableProperty() {
5635 boolean isSatisfiedBy(int ch) {
5636 return Character.isSpaceChar(ch);}});
5637 defClone("javaWhitespace", new CloneableProperty() {
5638 boolean isSatisfiedBy(int ch) {
5639 return Character.isWhitespace(ch);}});
5640 defClone("javaISOControl", new CloneableProperty() {
5641 boolean isSatisfiedBy(int ch) {
5642 return Character.isISOControl(ch);}});
5643 defClone("javaMirrored", new CloneableProperty() {
5644 boolean isSatisfiedBy(int ch) {
5645 return Character.isMirrored(ch);}});