author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58623 | 2ffbbd3c662c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54206 | 2 |
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
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). |
|
16 |
* |
|
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. |
|
20 |
* |
|
5506 | 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 |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.regex; |
|
27 |
||
28 |
import java.text.Normalizer; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
29 |
import java.text.Normalizer.Form; |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
30 |
import java.util.Locale; |
17447 | 31 |
import java.util.Iterator; |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
32 |
import java.util.Map; |
2 | 33 |
import java.util.ArrayList; |
34 |
import java.util.HashMap; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
35 |
import java.util.LinkedHashSet; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
36 |
import java.util.List; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
37 |
import java.util.Set; |
2 | 38 |
import java.util.Arrays; |
17447 | 39 |
import java.util.NoSuchElementException; |
40 |
import java.util.Spliterator; |
|
41 |
import java.util.Spliterators; |
|
42 |
import java.util.function.Predicate; |
|
43 |
import java.util.stream.Stream; |
|
44 |
import java.util.stream.StreamSupport; |
|
2 | 45 |
|
54971 | 46 |
import jdk.internal.util.ArraysSupport; |
2 | 47 |
|
48 |
/** |
|
49 |
* A compiled representation of a regular expression. |
|
50 |
* |
|
51 |
* <p> A regular expression, specified as a string, must first be compiled into |
|
52 |
* an instance of this class. The resulting pattern can then be used to create |
|
18787 | 53 |
* a {@link Matcher} object that can match arbitrary {@linkplain |
54 |
* java.lang.CharSequence character sequences} against the regular |
|
2 | 55 |
* expression. All of the state involved in performing a match resides in the |
56 |
* matcher, so many matchers can share the same pattern. |
|
57 |
* |
|
58 |
* <p> A typical invocation sequence is thus |
|
59 |
* |
|
60 |
* <blockquote><pre> |
|
61 |
* Pattern p = Pattern.{@link #compile compile}("a*b"); |
|
62 |
* Matcher m = p.{@link #matcher matcher}("aaaaab"); |
|
63 |
* boolean b = m.{@link Matcher#matches matches}();</pre></blockquote> |
|
64 |
* |
|
65 |
* <p> A {@link #matches matches} method is defined by this class as a |
|
66 |
* convenience for when a regular expression is used just once. This method |
|
67 |
* compiles an expression and matches an input sequence against it in a single |
|
68 |
* invocation. The statement |
|
69 |
* |
|
70 |
* <blockquote><pre> |
|
71 |
* boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote> |
|
72 |
* |
|
73 |
* is equivalent to the three statements above, though for repeated matches it |
|
74 |
* is less efficient since it does not allow the compiled pattern to be reused. |
|
75 |
* |
|
76 |
* <p> Instances of this class are immutable and are safe for use by multiple |
|
77 |
* concurrent threads. Instances of the {@link Matcher} class are not safe for |
|
78 |
* such use. |
|
79 |
* |
|
80 |
* |
|
54206 | 81 |
* <h2><a id="sum">Summary of regular-expression constructs</a></h2> |
2 | 82 |
* |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
83 |
* <table class="borderless"> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
84 |
* <caption style="display:none">Regular expression constructs, and what they match</caption> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
85 |
* <thead style="text-align:left"> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
86 |
* <tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
87 |
* <th id="construct">Construct</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
88 |
* <th id="matches">Matches</th> |
2 | 89 |
* </tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
90 |
* </thead> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
91 |
* <tbody style="text-align:left"> |
2 | 92 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
93 |
* <tr><th colspan="2" style="padding-top:20px" id="characters">Characters</th></tr> |
2 | 94 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
95 |
* <tr><th style="vertical-align:top; font-weight: normal" id="x"><i>x</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
96 |
* <td headers="matches characters x">The character <i>x</i></td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
97 |
* <tr><th style="vertical-align:top; font-weight: normal" id="backslash">{@code \\}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
98 |
* <td headers="matches characters backslash">The backslash character</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
99 |
* <tr><th style="vertical-align:top; font-weight: normal" id="octal_n">{@code \0}<i>n</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
100 |
* <td headers="matches characters octal_n">The character with octal value {@code 0}<i>n</i> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
101 |
* (0 {@code <=} <i>n</i> {@code <=} 7)</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
102 |
* <tr><th style="vertical-align:top; font-weight: normal" id="octal_nn">{@code \0}<i>nn</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
103 |
* <td headers="matches characters octal_nn">The character with octal value {@code 0}<i>nn</i> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
104 |
* (0 {@code <=} <i>n</i> {@code <=} 7)</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
105 |
* <tr><th style="vertical-align:top; font-weight: normal" id="octal_nnn">{@code \0}<i>mnn</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
106 |
* <td headers="matches characters octal_nnn">The character with octal value {@code 0}<i>mnn</i> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
107 |
* (0 {@code <=} <i>m</i> {@code <=} 3, |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
108 |
* 0 {@code <=} <i>n</i> {@code <=} 7)</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
109 |
* <tr><th style="vertical-align:top; font-weight: normal" id="hex_hh">{@code \x}<i>hh</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
110 |
* <td headers="matches characters hex_hh">The character with hexadecimal value {@code 0x}<i>hh</i></td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
111 |
* <tr><th style="vertical-align:top; font-weight: normal" id="hex_hhhh"><code>\u</code><i>hhhh</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
112 |
* <td headers="matches characters hex_hhhh">The character with hexadecimal value {@code 0x}<i>hhhh</i></td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
113 |
* <tr><th style="vertical-align:top; font-weight: normal" id="hex_h_h"><code>\x</code><i>{h...h}</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
114 |
* <td headers="matches characters hex_h_h">The character with hexadecimal value {@code 0x}<i>h...h</i> |
8173
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
115 |
* ({@link java.lang.Character#MIN_CODE_POINT Character.MIN_CODE_POINT} |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
116 |
* <= {@code 0x}<i>h...h</i> <= |
8173
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
117 |
* {@link java.lang.Character#MAX_CODE_POINT Character.MAX_CODE_POINT})</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
118 |
* <tr><th style="vertical-align:top; font-weight: normal" id="unicode_name"><code>\N{</code><i>name</i><code>}</code></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
119 |
* <td headers="matches characters unicode_name">The character with Unicode character name <i>'name'</i></td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
120 |
* <tr><th style="vertical-align:top; font-weight:normal" id="tab">{@code \t}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
121 |
* <td headers="matches characters tab">The tab character (<code>'\u0009'</code>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
122 |
* <tr><th style="vertical-align:top; font-weight:normal" id="newline">{@code \n}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
123 |
* <td headers="matches characters newline">The newline (line feed) character (<code>'\u000A'</code>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
124 |
* <tr><th style="vertical-align:top; font-weight:normal" id="return">{@code \r}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
125 |
* <td headers="matches characters return">The carriage-return character (<code>'\u000D'</code>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
126 |
* <tr><th style="vertical-align:top; font-weight:normal" id="form_feed">{@code \f}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
127 |
* <td headers="matches characters form_feed">The form-feed character (<code>'\u000C'</code>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
128 |
* <tr><th style="vertical-align:top; font-weight:normal" id="bell">{@code \a}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
129 |
* <td headers="matches characters bell">The alert (bell) character (<code>'\u0007'</code>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
130 |
* <tr><th style="vertical-align:top; font-weight:normal" id="escape">{@code \e}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
131 |
* <td headers="matches characters escape">The escape character (<code>'\u001B'</code>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
132 |
* <tr><th style="vertical-align:top; font-weight:normal" id="ctrl_x">{@code \c}<i>x</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
133 |
* <td headers="matches characters ctrl_x">The control character corresponding to <i>x</i></td></tr> |
2 | 134 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
135 |
* <tr><th colspan="2" style="padding-top:20px" id="classes">Character classes</th></tr> |
2 | 136 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
137 |
* <tr><th style="vertical-align:top; font-weight:normal" id="simple">{@code [abc]}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
138 |
* <td headers="matches classes simple">{@code a}, {@code b}, or {@code c} (simple class)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
139 |
* <tr><th style="vertical-align:top; font-weight:normal" id="negation">{@code [^abc]}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
140 |
* <td headers="matches classes negation">Any character except {@code a}, {@code b}, or {@code c} (negation)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
141 |
* <tr><th style="vertical-align:top; font-weight:normal" id="range">{@code [a-zA-Z]}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
142 |
* <td headers="matches classes range">{@code a} through {@code z} |
18787 | 143 |
* or {@code A} through {@code Z}, inclusive (range)</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
144 |
* <tr><th style="vertical-align:top; font-weight:normal" id="union">{@code [a-d[m-p]]}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
145 |
* <td headers="matches classes union">{@code a} through {@code d}, |
18787 | 146 |
* or {@code m} through {@code p}: {@code [a-dm-p]} (union)</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
147 |
* <tr><th style="vertical-align:top; font-weight:normal" id="intersection">{@code [a-z&&[def]]}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
148 |
* <td headers="matches classes intersection">{@code d}, {@code e}, or {@code f} (intersection)</tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
149 |
* <tr><th style="vertical-align:top; font-weight:normal" id="subtraction1">{@code [a-z&&[^bc]]}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
150 |
* <td headers="matches classes subtraction1">{@code a} through {@code z}, |
18787 | 151 |
* except for {@code b} and {@code c}: {@code [ad-z]} (subtraction)</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
152 |
* <tr><th style="vertical-align:top; font-weight:normal" id="subtraction2">{@code [a-z&&[^m-p]]}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
153 |
* <td headers="matches classes subtraction2">{@code a} through {@code z}, |
18787 | 154 |
* and not {@code m} through {@code p}: {@code [a-lq-z]}(subtraction)</td></tr> |
2 | 155 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
156 |
* <tr><th colspan="2" style="padding-top:20px" id="predef">Predefined character classes</th></tr> |
2 | 157 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
158 |
* <tr><th style="vertical-align:top; font-weight:normal" id="any">{@code .}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
159 |
* <td headers="matches predef any">Any character (may or may not match <a href="#lt">line terminators</a>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
160 |
* <tr><th style="vertical-align:top; font-weight:normal" id="digit">{@code \d}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
161 |
* <td headers="matches predef digit">A digit: {@code [0-9]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
162 |
* <tr><th style="vertical-align:top; font-weight:normal" id="non_digit">{@code \D}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
163 |
* <td headers="matches predef non_digit">A non-digit: {@code [^0-9]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
164 |
* <tr><th style="vertical-align:top; font-weight:normal" id="horiz_white">{@code \h}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
165 |
* <td headers="matches predef horiz_white">A horizontal whitespace character: |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
166 |
* <code>[ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]</code></td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
167 |
* <tr><th style="vertical-align:top; font-weight:normal" id="non_horiz_white">{@code \H}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
168 |
* <td headers="matches predef non_horiz_white">A non-horizontal whitespace character: {@code [^\h]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
169 |
* <tr><th style="vertical-align:top; font-weight:normal" id="white">{@code \s}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
170 |
* <td headers="matches predef white">A whitespace character: {@code [ \t\n\x0B\f\r]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
171 |
* <tr><th style="vertical-align:top; font-weight:normal" id="non_white">{@code \S}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
172 |
* <td headers="matches predef non_white">A non-whitespace character: {@code [^\s]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
173 |
* <tr><th style="vertical-align:top; font-weight:normal" id="vert_white">{@code \v}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
174 |
* <td headers="matches predef vert_white">A vertical whitespace character: <code>[\n\x0B\f\r\x85\u2028\u2029]</code> |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
175 |
* </td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
176 |
* <tr><th style="vertical-align:top; font-weight:normal" id="non_vert_white">{@code \V}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
177 |
* <td headers="matches predef non_vert_white">A non-vertical whitespace character: {@code [^\v]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
178 |
* <tr><th style="vertical-align:top; font-weight:normal" id="word">{@code \w}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
179 |
* <td headers="matches predef word">A word character: {@code [a-zA-Z_0-9]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
180 |
* <tr><th style="vertical-align:top; font-weight:normal" id="non_word">{@code \W}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
181 |
* <td headers="matches predef non_word">A non-word character: {@code [^\w]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
182 |
* |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
183 |
* <tr><th colspan="2" style="padding-top:20px" id="posix"><b>POSIX character classes (US-ASCII only)</b></th></tr> |
2 | 184 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
185 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Lower">{@code \p{Lower}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
186 |
* <td headers="matches posix Lower">A lower-case alphabetic character: {@code [a-z]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
187 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Upper">{@code \p{Upper}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
188 |
* <td headers="matches posix Upper">An upper-case alphabetic character:{@code [A-Z]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
189 |
* <tr><th style="vertical-align:top; font-weight:normal" id="ASCII">{@code \p{ASCII}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
190 |
* <td headers="matches posix ASCII">All ASCII:{@code [\x00-\x7F]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
191 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Alpha">{@code \p{Alpha}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
192 |
* <td headers="matches posix Alpha">An alphabetic character:{@code [\p{Lower}\p{Upper}]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
193 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Digit">{@code \p{Digit}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
194 |
* <td headers="matches posix Digit">A decimal digit: {@code [0-9]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
195 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Alnum">{@code \p{Alnum}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
196 |
* <td headers="matches posix Alnum">An alphanumeric character:{@code [\p{Alpha}\p{Digit}]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
197 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Punct">{@code \p{Punct}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
198 |
* <td headers="matches posix Punct">Punctuation: One of {@code !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~}</td></tr> |
18787 | 199 |
* <!-- {@code [\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]} |
200 |
* {@code [\X21-\X2F\X31-\X40\X5B-\X60\X7B-\X7E]} --> |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
201 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Graph">{@code \p{Graph}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
202 |
* <td headers="matches posix Graph">A visible character: {@code [\p{Alnum}\p{Punct}]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
203 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Print">{@code \p{Print}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
204 |
* <td headers="matches posix Print">A printable character: {@code [\p{Graph}\x20]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
205 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Blank">{@code \p{Blank}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
206 |
* <td headers="matches posix Blank">A space or a tab: {@code [ \t]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
207 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Cntrl">{@code \p{Cntrl}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
208 |
* <td headers="matches posix Cntrl">A control character: {@code [\x00-\x1F\x7F]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
209 |
* <tr><th style="vertical-align:top; font-weight:normal" id="XDigit">{@code \p{XDigit}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
210 |
* <td headers="matches posix XDigit">A hexadecimal digit: {@code [0-9a-fA-F]}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
211 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Space">{@code \p{Space}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
212 |
* <td headers="matches posix Space">A whitespace character: {@code [ \t\n\x0B\f\r]}</td></tr> |
2 | 213 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
214 |
* <tr><th colspan="2" style="padding-top:20px" id="java">java.lang.Character classes (simple <a href="#jcc">java character type</a>)</th></tr> |
2 | 215 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
216 |
* <tr><th style="vertical-align:top; font-weight:normal" id="javaLowerCase">{@code \p{javaLowerCase}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
217 |
* <td headers="matches java javaLowerCase">Equivalent to java.lang.Character.isLowerCase()</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
218 |
* <tr><th style="vertical-align:top; font-weight:normal" id="javaUpperCase">{@code \p{javaUpperCase}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
219 |
* <td headers="matches java javaUpperCase">Equivalent to java.lang.Character.isUpperCase()</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
220 |
* <tr><th style="vertical-align:top; font-weight:normal" id="javaWhitespace">{@code \p{javaWhitespace}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
221 |
* <td headers="matches java javaWhitespace">Equivalent to java.lang.Character.isWhitespace()</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
222 |
* <tr><th style="vertical-align:top; font-weight:normal" id="javaMirrored">{@code \p{javaMirrored}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
223 |
* <td headers="matches java javaMirrored">Equivalent to java.lang.Character.isMirrored()</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
224 |
* |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
225 |
* <tr><th colspan="2" style="padding-top:20px" id="unicode">Classes for Unicode scripts, blocks, categories and binary properties</th></tr> |
2 | 226 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
227 |
* <tr><th style="vertical-align:top; font-weight:normal" id="IsLatin">{@code \p{IsLatin}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
228 |
* <td headers="matches unicode IsLatin">A Latin script character (<a href="#usc">script</a>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
229 |
* <tr><th style="vertical-align:top; font-weight:normal" id="InGreek">{@code \p{InGreek}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
230 |
* <td headers="matches unicode InGreek">A character in the Greek block (<a href="#ubc">block</a>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
231 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Lu">{@code \p{Lu}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
232 |
* <td headers="matches unicode Lu">An uppercase letter (<a href="#ucc">category</a>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
233 |
* <tr><th style="vertical-align:top; font-weight:normal" id="IsAlphabetic">{@code \p{IsAlphabetic}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
234 |
* <td headers="matches unicode IsAlphabetic">An alphabetic character (<a href="#ubpc">binary property</a>)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
235 |
* <tr><th style="vertical-align:top; font-weight:normal" id="Sc">{@code \p{Sc}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
236 |
* <td headers="matches unicode Sc">A currency symbol</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
237 |
* <tr><th style="vertical-align:top; font-weight:normal" id="not_InGreek">{@code \P{InGreek}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
238 |
* <td headers="matches unicode not_InGreek">Any character except one in the Greek block (negation)</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
239 |
* <tr><th style="vertical-align:top; font-weight:normal" id="not_uppercase">{@code [\p{L}&&[^\p{Lu}]]}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
240 |
* <td headers="matches unicode not_uppercase">Any letter except an uppercase letter (subtraction)</td></tr> |
2 | 241 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
242 |
* <tr><th colspan="2" style="padding-top:20px" id="bounds">Boundary matchers</th></tr> |
2 | 243 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
244 |
* <tr><th style="vertical-align:top; font-weight:normal" id="begin_line">{@code ^}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
245 |
* <td headers="matches bounds begin_line">The beginning of a line</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
246 |
* <tr><th style="vertical-align:top; font-weight:normal" id="end_line">{@code $}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
247 |
* <td headers="matches bounds end_line">The end of a line</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
248 |
* <tr><th style="vertical-align:top; font-weight:normal" id="word_boundary">{@code \b}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
249 |
* <td headers="matches bounds word_boundary">A word boundary</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
250 |
* <tr><th style="vertical-align:top; font-weight:normal" id="grapheme_cluster_boundary">{@code \b{g}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
251 |
* <td headers="matches bounds grapheme_cluster_boundary">A Unicode extended grapheme cluster boundary</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
252 |
* <tr><th style="vertical-align:top; font-weight:normal" id="non_word_boundary">{@code \B}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
253 |
* <td headers="matches bounds non_word_boundary">A non-word boundary</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
254 |
* <tr><th style="vertical-align:top; font-weight:normal" id="begin_input">{@code \A}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
255 |
* <td headers="matches bounds begin_input">The beginning of the input</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
256 |
* <tr><th style="vertical-align:top; font-weight:normal" id="end_prev_match">{@code \G}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
257 |
* <td headers="matches bounds end_prev_match">The end of the previous match</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
258 |
* <tr><th style="vertical-align:top; font-weight:normal" id="end_input_except_term">{@code \Z}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
259 |
* <td headers="matches bounds end_input_except_term">The end of the input but for the final |
2 | 260 |
* <a href="#lt">terminator</a>, if any</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
261 |
* <tr><th style="vertical-align:top; font-weight:normal" id="end_input">{@code \z}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
262 |
* <td headers="matches bounds end_input">The end of the input</td></tr> |
2 | 263 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
264 |
* <tr><th colspan="2" style="padding-top:20px" id="linebreak">Linebreak matcher</th></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
265 |
* |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
266 |
* <tr><th style="vertical-align:top; font-weight:normal" id="any_unicode_linebreak">{@code \R}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
267 |
* <td headers="matches linebreak any_unicode_linebreak">Any Unicode linebreak sequence, is equivalent to |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
268 |
* <code>\u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029] |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
269 |
* </code></td></tr> |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
270 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
271 |
* <tr><th colspan="2" style="padding-top:20px" id="grapheme">Unicode Extended Grapheme matcher</th></tr> |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
272 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
273 |
* <tr><th style="vertical-align:top; font-weight:normal" id="grapheme_any">{@code \X}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
274 |
* <td headers="matches grapheme grapheme_any">Any Unicode extended grapheme cluster</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
275 |
* |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
276 |
* <tr><th colspan="2" style="padding-top:20px" id="greedy">Greedy quantifiers</th></tr> |
2 | 277 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
278 |
* <tr><th style="vertical-align:top; font-weight:normal" id="greedy_once_or_not"><i>X</i>{@code ?}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
279 |
* <td headers="matches greedy greedy_once_or_not"><i>X</i>, once or not at all</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
280 |
* <tr><th style="vertical-align:top; font-weight:normal" id="greedy_zero_or_more"><i>X</i>{@code *}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
281 |
* <td headers="matches greedy greedy_zero_or_more"><i>X</i>, zero or more times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
282 |
* <tr><th style="vertical-align:top; font-weight:normal" id="greedy_one_or_more"><i>X</i>{@code +}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
283 |
* <td headers="matches greedy greedy_one_or_more"><i>X</i>, one or more times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
284 |
* <tr><th style="vertical-align:top; font-weight:normal" id="greedy_exactly"><i>X</i><code>{</code><i>n</i><code>}</code></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
285 |
* <td headers="matches greedy greedy_exactly"><i>X</i>, exactly <i>n</i> times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
286 |
* <tr><th style="vertical-align:top; font-weight:normal" id="greedy_at_least"><i>X</i><code>{</code><i>n</i>{@code ,}}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
287 |
* <td headers="matches greedy greedy_at_least"><i>X</i>, at least <i>n</i> times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
288 |
* <tr><th style="vertical-align:top; font-weight:normal" id="greedy_at_least_up_to"><i>X</i><code>{</code><i>n</i>{@code ,}<i>m</i><code>}</code></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
289 |
* <td headers="matches greedy greedy_at_least_up_to"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr> |
2 | 290 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
291 |
* <tr><th colspan="2" style="padding-top:20px" id="reluc">Reluctant quantifiers</th></tr> |
2 | 292 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
293 |
* <tr><th style="vertical-align:top; font-weight:normal" id="reluc_once_or_not"><i>X</i>{@code ??}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
294 |
* <td headers="matches reluc reluc_once_or_not"><i>X</i>, once or not at all</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
295 |
* <tr><th style="vertical-align:top; font-weight:normal" id="reluc_zero_or_more"><i>X</i>{@code *?}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
296 |
* <td headers="matches reluc reluc_zero_or_more"><i>X</i>, zero or more times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
297 |
* <tr><th style="vertical-align:top; font-weight:normal" id="reluc_one_or_more"><i>X</i>{@code +?}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
298 |
* <td headers="matches reluc reluc_one_or_more"><i>X</i>, one or more times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
299 |
* <tr><th style="vertical-align:top; font-weight:normal" id="reluc_exactly"><i>X</i><code>{</code><i>n</i><code>}?</code></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
300 |
* <td headers="matches reluc reluc_exactly"><i>X</i>, exactly <i>n</i> times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
301 |
* <tr><th style="vertical-align:top; font-weight:normal" id="reluc_at_least"><i>X</i><code>{</code><i>n</i><code>,}?</code></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
302 |
* <td headers="matches reluc reluc_at_least"><i>X</i>, at least <i>n</i> times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
303 |
* <tr><th style="vertical-align:top; font-weight:normal" id="reluc_at_least_up_to"><i>X</i><code>{</code><i>n</i>{@code ,}<i>m</i><code>}?</code></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
304 |
* <td headers="matches reluc reluc_at_least_up_to"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr> |
2 | 305 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
306 |
* <tr><th colspan="2" style="padding-top:20px" id="poss">Possessive quantifiers</th></tr> |
2 | 307 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
308 |
* <tr><th style="vertical-align:top; font-weight:normal" id="poss_once_or_not"><i>X</i>{@code ?+}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
309 |
* <td headers="matches poss poss_once_or_not"><i>X</i>, once or not at all</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
310 |
* <tr><th style="vertical-align:top; font-weight:normal" id="poss_zero_or_more"><i>X</i>{@code *+}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
311 |
* <td headers="matches poss poss_zero_or_more"><i>X</i>, zero or more times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
312 |
* <tr><th style="vertical-align:top; font-weight:normal" id="poss_one_or_more"><i>X</i>{@code ++}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
313 |
* <td headers="matches poss poss_one_or_more"><i>X</i>, one or more times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
314 |
* <tr><th style="vertical-align:top; font-weight:normal" id="poss_exactly"><i>X</i><code>{</code><i>n</i><code>}+</code></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
315 |
* <td headers="matches poss poss_exactly"><i>X</i>, exactly <i>n</i> times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
316 |
* <tr><th style="vertical-align:top; font-weight:normal" id="poss_at_least"><i>X</i><code>{</code><i>n</i><code>,}+</code></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
317 |
* <td headers="matches poss poss_at_least"><i>X</i>, at least <i>n</i> times</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
318 |
* <tr><th style="vertical-align:top; font-weight:normal" id="poss_at_least_up_to"><i>X</i><code>{</code><i>n</i>{@code ,}<i>m</i><code>}+</code></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
319 |
* <td headers="matches poss poss_at_least_up_to"><i>X</i>, at least <i>n</i> but not more than <i>m</i> times</td></tr> |
2 | 320 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
321 |
* <tr><th colspan="2" style="padding-top:20px" id="logical">Logical operators</th></tr> |
2 | 322 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
323 |
* <tr><th style="vertical-align:top; font-weight:normal" id="concat"><i>XY</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
324 |
* <td headers="matches logical concat"><i>X</i> followed by <i>Y</i></td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
325 |
* <tr><th style="vertical-align:top; font-weight:normal" id="alternate"><i>X</i>{@code |}<i>Y</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
326 |
* <td headers="matches logical alternate">Either <i>X</i> or <i>Y</i></td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
327 |
* <tr><th style="vertical-align:top; font-weight:normal" id="group">{@code (}<i>X</i>{@code )}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
328 |
* <td headers="matches logical group">X, as a <a href="#cg">capturing group</a></td></tr> |
2 | 329 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
330 |
* <tr><th colspan="2" style="padding-top:20px" id="backref">Back references</th></tr> |
2 | 331 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
332 |
* <tr><th style="vertical-align:top; font-weight:normal" id="back_nth">{@code \}<i>n</i></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
333 |
* <td headers="matches backref back_nth">Whatever the <i>n</i><sup>th</sup> |
2 | 334 |
* <a href="#cg">capturing group</a> matched</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
335 |
* <tr><th style="vertical-align:top; font-weight:normal" id="back_named">{@code \}<i>k</i><<i>name</i>></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
336 |
* <td headers="matches backref back_named">Whatever the |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
337 |
* <a href="#groupname">named-capturing group</a> "name" matched</td></tr> |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
338 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
339 |
* <tr><th colspan="2" style="padding-top:20px" id="quote">Quotation</th></tr> |
2 | 340 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
341 |
* <tr><th style="vertical-align:top; font-weight:normal" id="quote_follow">{@code \}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
342 |
* <td headers="matches quote quote_follow">Nothing, but quotes the following character</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
343 |
* <tr><th style="vertical-align:top; font-weight:normal" id="quote_begin">{@code \Q}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
344 |
* <td headers="matches quote quote_begin">Nothing, but quotes all characters until {@code \E}</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
345 |
* <tr><th style="vertical-align:top; font-weight:normal" id="quote_end">{@code \E}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
346 |
* <td headers="matches quote quote_end">Nothing, but ends quoting started by {@code \Q}</td></tr> |
2 | 347 |
* <!-- Metachars: !$()*+.<>?[\]^{|} --> |
348 |
* |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
349 |
* <tr><th colspan="2" style="padding-top:20px" id="special">Special constructs (named-capturing and non-capturing)</th></tr> |
2 | 350 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
351 |
* <tr><th style="vertical-align:top; font-weight:normal" id="named_group"><code>(?<<a href="#groupname">name</a>></code><i>X</i>{@code )}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
352 |
* <td headers="matches special named_group"><i>X</i>, as a named-capturing group</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
353 |
* <tr><th style="vertical-align:top; font-weight:normal" id="non_capture_group">{@code (?:}<i>X</i>{@code )}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
354 |
* <td headers="matches special non_capture_group"><i>X</i>, as a non-capturing group</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
355 |
* <tr><th style="vertical-align:top; font-weight:normal" id="flags"><code>(?idmsuxU-idmsuxU) </code></th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
356 |
* <td headers="matches special flags">Nothing, but turns match flags <a href="#CASE_INSENSITIVE">i</a> |
2 | 357 |
* <a href="#UNIX_LINES">d</a> <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
358 |
* <a href="#UNICODE_CASE">u</a> <a href="#COMMENTS">x</a> <a href="#UNICODE_CHARACTER_CLASS">U</a> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
359 |
* on - off</td></tr> |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58111
diff
changeset
|
360 |
* <tr><th style="vertical-align:top; font-weight:normal" id="non_capture_group_flags">{@code (?idmsuxU-idmsuxU:}<i>X</i>{@code )} </th> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
361 |
* <td headers="matches special non_capture_group_flags"><i>X</i>, as a <a href="#cg">non-capturing group</a> with the |
2 | 362 |
* given flags <a href="#CASE_INSENSITIVE">i</a> <a href="#UNIX_LINES">d</a> |
363 |
* <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> <a href="#UNICODE_CASE">u</a > |
|
55494
1e63cdb2b2ba
8224849: Flag (?U:...) is allowed for non-capturing groups
igerasim
parents:
55220
diff
changeset
|
364 |
* <a href="#COMMENTS">x</a> <a href="#UNICODE_CHARACTER_CLASS">U</a> on - off</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
365 |
* <tr><th style="vertical-align:top; font-weight:normal" id="pos_lookahead">{@code (?=}<i>X</i>{@code )}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
366 |
* <td headers="matches special pos_lookahead"><i>X</i>, via zero-width positive lookahead</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
367 |
* <tr><th style="vertical-align:top; font-weight:normal" id="neg_lookahead">{@code (?!}<i>X</i>{@code )}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
368 |
* <td headers="matches special neg_lookahead"><i>X</i>, via zero-width negative lookahead</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
369 |
* <tr><th style="vertical-align:top; font-weight:normal" id="pos_lookbehind">{@code (?<=}<i>X</i>{@code )}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
370 |
* <td headers="matches special pos_lookbehind"><i>X</i>, via zero-width positive lookbehind</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
371 |
* <tr><th style="vertical-align:top; font-weight:normal" id="neg_lookbehind">{@code (?<!}<i>X</i>{@code )}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
372 |
* <td headers="matches special neg_lookbehind"><i>X</i>, via zero-width negative lookbehind</td></tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
373 |
* <tr><th style="vertical-align:top; font-weight:normal" id="indep_non_capture_group">{@code (?>}<i>X</i>{@code )}</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
374 |
* <td headers="matches special indep_non_capture_group"><i>X</i>, as an independent, non-capturing group</td></tr> |
2 | 375 |
* |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
376 |
* </tbody> |
2 | 377 |
* </table> |
378 |
* |
|
379 |
* <hr> |
|
380 |
* |
|
381 |
* |
|
54206 | 382 |
* <h2><a id="bs">Backslashes, escapes, and quoting</a></h2> |
2 | 383 |
* |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
384 |
* <p> The backslash character ({@code '\'}) serves to introduce escaped |
2 | 385 |
* constructs, as defined in the table above, as well as to quote characters |
386 |
* that otherwise would be interpreted as unescaped constructs. Thus the |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
387 |
* expression {@code \\} matches a single backslash and <code>\{</code> matches a |
2 | 388 |
* left brace. |
389 |
* |
|
390 |
* <p> It is an error to use a backslash prior to any alphabetic character that |
|
391 |
* does not denote an escaped construct; these are reserved for future |
|
392 |
* extensions to the regular-expression language. A backslash may be used |
|
393 |
* prior to a non-alphabetic character regardless of whether that character is |
|
394 |
* part of an unescaped construct. |
|
395 |
* |
|
396 |
* <p> Backslashes within string literals in Java source code are interpreted |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8173
diff
changeset
|
397 |
* as required by |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8173
diff
changeset
|
398 |
* <cite>The Java™ Language Specification</cite> |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8173
diff
changeset
|
399 |
* as either Unicode escapes (section 3.3) or other character escapes (section 3.10.6) |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8173
diff
changeset
|
400 |
* It is therefore necessary to double backslashes in string |
2 | 401 |
* literals that represent regular expressions to protect them from |
402 |
* interpretation by the Java bytecode compiler. The string literal |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
403 |
* <code>"\b"</code>, for example, matches a single backspace character when |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
404 |
* interpreted as a regular expression, while {@code "\\b"} matches a |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
405 |
* word boundary. The string literal {@code "\(hello\)"} is illegal |
2 | 406 |
* and leads to a compile-time error; in order to match the string |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
407 |
* {@code (hello)} the string literal {@code "\\(hello\\)"} |
2 | 408 |
* must be used. |
409 |
* |
|
54206 | 410 |
* <h2><a id="cc">Character Classes</a></h2> |
2 | 411 |
* |
412 |
* <p> Character classes may appear within other character classes, and |
|
413 |
* may be composed by the union operator (implicit) and the intersection |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
414 |
* operator ({@code &&}). |
2 | 415 |
* The union operator denotes a class that contains every character that is |
416 |
* in at least one of its operand classes. The intersection operator |
|
417 |
* denotes a class that contains every character that is in both of its |
|
418 |
* operand classes. |
|
419 |
* |
|
420 |
* <p> The precedence of character-class operators is as follows, from |
|
421 |
* highest to lowest: |
|
422 |
* |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
423 |
* <table class="striped" style="margin-left: 2em;"> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
424 |
* <caption style="display:none">Precedence of character class operators.</caption> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
425 |
* <thead> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
426 |
* <tr><th scope="col">Precedence<th scope="col">Name<th scope="col">Example |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
427 |
* </thead> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
428 |
* <tbody> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
429 |
* <tr><th scope="row">1</th> |
2 | 430 |
* <td>Literal escape </td> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
431 |
* <td>{@code \x}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
432 |
* <tr><th scope="row">2</th> |
2 | 433 |
* <td>Grouping</td> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
434 |
* <td>{@code [...]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
435 |
* <tr><th scope="row">3</th> |
2 | 436 |
* <td>Range</td> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
437 |
* <td>{@code a-z}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
438 |
* <tr><th scope="row">4</th> |
2 | 439 |
* <td>Union</td> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
440 |
* <td>{@code [a-e][i-u]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
441 |
* <tr><th scope="row">5</th> |
2 | 442 |
* <td>Intersection</td> |
18787 | 443 |
* <td>{@code [a-z&&[aeiou]]}</td></tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
444 |
* </tbody> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
445 |
* </table> |
2 | 446 |
* |
447 |
* <p> Note that a different set of metacharacters are in effect inside |
|
448 |
* a character class than outside a character class. For instance, the |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
449 |
* regular expression {@code .} loses its special meaning inside a |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
450 |
* character class, while the expression {@code -} becomes a range |
2 | 451 |
* forming metacharacter. |
452 |
* |
|
54206 | 453 |
* <h2><a id="lt">Line terminators</a></h2> |
2 | 454 |
* |
455 |
* <p> A <i>line terminator</i> is a one- or two-character sequence that marks |
|
456 |
* the end of a line of the input character sequence. The following are |
|
457 |
* recognized as line terminators: |
|
458 |
* |
|
459 |
* <ul> |
|
460 |
* |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
461 |
* <li> A newline (line feed) character ({@code '\n'}), |
2 | 462 |
* |
463 |
* <li> A carriage-return character followed immediately by a newline |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
464 |
* character ({@code "\r\n"}), |
2 | 465 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
466 |
* <li> A standalone carriage-return character ({@code '\r'}), |
2 | 467 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
468 |
* <li> A next-line character (<code>'\u0085'</code>), |
2 | 469 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
470 |
* <li> A line-separator character (<code>'\u2028'</code>), or |
2 | 471 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
472 |
* <li> A paragraph-separator character (<code>'\u2029'</code>). |
2 | 473 |
* |
474 |
* </ul> |
|
475 |
* <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators |
|
476 |
* recognized are newline characters. |
|
477 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
478 |
* <p> The regular expression {@code .} matches any character except a line |
2 | 479 |
* terminator unless the {@link #DOTALL} flag is specified. |
480 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
481 |
* <p> By default, the regular expressions {@code ^} and {@code $} ignore |
2 | 482 |
* line terminators and only match at the beginning and the end, respectively, |
483 |
* of the entire input sequence. If {@link #MULTILINE} mode is activated then |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
484 |
* {@code ^} matches at the beginning of input and after any line terminator |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
485 |
* except at the end of input. When in {@link #MULTILINE} mode {@code $} |
2 | 486 |
* matches just before a line terminator or the end of the input sequence. |
487 |
* |
|
54206 | 488 |
* <h2><a id="cg">Groups and capturing</a></h2> |
2 | 489 |
* |
54206 | 490 |
* <h3><a id="gnumber">Group number</a></h3> |
2 | 491 |
* <p> Capturing groups are numbered by counting their opening parentheses from |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
492 |
* left to right. In the expression {@code ((A)(B(C)))}, for example, there |
2 | 493 |
* are four such groups: </p> |
494 |
* |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
495 |
* <ol style="margin-left:2em;"> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
496 |
* <li> {@code ((A)(B(C)))} |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
497 |
* <li> {@code (A)} |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
498 |
* <li> {@code (B(C))} |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
499 |
* <li> {@code (C)} |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
500 |
* </ol> |
2 | 501 |
* |
502 |
* <p> Group zero always stands for the entire expression. |
|
503 |
* |
|
504 |
* <p> Capturing groups are so named because, during a match, each subsequence |
|
505 |
* of the input sequence that matches such a group is saved. The captured |
|
506 |
* subsequence may be used later in the expression, via a back reference, and |
|
507 |
* may also be retrieved from the matcher once the match operation is complete. |
|
508 |
* |
|
54206 | 509 |
* <h3><a id="groupname">Group name</a></h3> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
510 |
* <p>A capturing group can also be assigned a "name", a {@code named-capturing group}, |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
511 |
* and then be back-referenced later by the "name". Group names are composed of |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
512 |
* the following characters. The first character must be a {@code letter}. |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
513 |
* |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
514 |
* <ul> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
515 |
* <li> The uppercase letters {@code 'A'} through {@code 'Z'} |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
516 |
* (<code>'\u0041'</code> through <code>'\u005a'</code>), |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
517 |
* <li> The lowercase letters {@code 'a'} through {@code 'z'} |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
518 |
* (<code>'\u0061'</code> through <code>'\u007a'</code>), |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
519 |
* <li> The digits {@code '0'} through {@code '9'} |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
520 |
* (<code>'\u0030'</code> through <code>'\u0039'</code>), |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
521 |
* </ul> |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
522 |
* |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
523 |
* <p> A {@code named-capturing group} is still numbered as described in |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
524 |
* <a href="#gnumber">Group number</a>. |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
525 |
* |
2 | 526 |
* <p> The captured input associated with a group is always the subsequence |
527 |
* that the group most recently matched. If a group is evaluated a second time |
|
528 |
* because of quantification then its previously-captured value, if any, will |
|
529 |
* be retained if the second evaluation fails. Matching the string |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
530 |
* {@code "aba"} against the expression {@code (a(b)?)+}, for example, leaves |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
531 |
* group two set to {@code "b"}. All captured input is discarded at the |
2 | 532 |
* beginning of each match. |
533 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
534 |
* <p> Groups beginning with {@code (?} are either pure, <i>non-capturing</i> groups |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
535 |
* that do not capture text and do not count towards the group total, or |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
536 |
* <i>named-capturing</i> group. |
2 | 537 |
* |
54206 | 538 |
* <h2> Unicode support </h2> |
2 | 539 |
* |
540 |
* <p> This class is in conformance with Level 1 of <a |
|
541 |
* href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical |
|
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
542 |
* Standard #18: Unicode Regular Expression</i></a>, plus RL2.1 |
55013 | 543 |
* Canonical Equivalents and RL2.2 Extended Grapheme Clusters. |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
544 |
* <p> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
545 |
* <b>Unicode escape sequences</b> such as <code>\u2014</code> in Java source code |
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8173
diff
changeset
|
546 |
* are processed as described in section 3.3 of |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
8173
diff
changeset
|
547 |
* <cite>The Java™ Language Specification</cite>. |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
548 |
* Such escape sequences are also implemented directly by the regular-expression |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
549 |
* parser so that Unicode escapes can be used in expressions that are read from |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
550 |
* files or from the keyboard. Thus the strings <code>"\u2014"</code> and |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
551 |
* {@code "\\u2014"}, while not equal, compile into the same pattern, which |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
552 |
* matches the character with hexadecimal value {@code 0x2014}. |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
553 |
* <p> |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
554 |
* A Unicode character can also be represented by using its <b>Hex notation</b> |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
555 |
* (hexadecimal code point value) directly as described in construct |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
556 |
* <code>\x{...}</code>, for example a supplementary character U+2011F can be |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
557 |
* specified as <code>\x{2011F}</code>, instead of two consecutive Unicode escape |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
558 |
* sequences of the surrogate pair <code>\uD840</code><code>\uDD1F</code>. |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
559 |
* <p> |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
560 |
* <b>Unicode character names</b> are supported by the named character construct |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
561 |
* <code>\N{</code>...<code>}</code>, for example, <code>\N{WHITE SMILING FACE}</code> |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
562 |
* specifies character <code>\u263A</code>. The character names supported |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
563 |
* by this class are the valid Unicode character names matched by |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
564 |
* {@link java.lang.Character#codePointOf(String) Character.codePointOf(name)}. |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
565 |
* <p> |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
566 |
* <a href="http://www.unicode.org/reports/tr18/#Default_Grapheme_Clusters"> |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
567 |
* <b>Unicode extended grapheme clusters</b></a> are supported by the grapheme |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
568 |
* cluster matcher {@code \X} and the corresponding boundary matcher {@code \b{g}}. |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
569 |
* <p> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
570 |
* Unicode scripts, blocks, categories and binary properties are written with |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
571 |
* the {@code \p} and {@code \P} constructs as in Perl. |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
572 |
* <code>\p{</code><i>prop</i><code>}</code> matches if |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
573 |
* the input has the property <i>prop</i>, while <code>\P{</code><i>prop</i><code>}</code> |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
574 |
* does not match if the input has that property. |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
575 |
* <p> |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
576 |
* Scripts, blocks, categories and binary properties can be used both inside |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
577 |
* and outside of a character class. |
18787 | 578 |
* |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
579 |
* <p> |
44844
b2b4d98404ba
8179364: update "<a name=" in java.base module to use id attribute
jjg
parents:
44122
diff
changeset
|
580 |
* <b><a id="usc">Scripts</a></b> are specified either with the prefix {@code Is}, as in |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
581 |
* {@code IsHiragana}, or by using the {@code script} keyword (or its short |
32012 | 582 |
* form {@code sc}) as in {@code script=Hiragana} or {@code sc=Hiragana}. |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
583 |
* <p> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
584 |
* The script names supported by {@code Pattern} are the valid script names |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
585 |
* accepted and defined by |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
586 |
* {@link java.lang.Character.UnicodeScript#forName(String) UnicodeScript.forName}. |
18787 | 587 |
* |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
588 |
* <p> |
44844
b2b4d98404ba
8179364: update "<a name=" in java.base module to use id attribute
jjg
parents:
44122
diff
changeset
|
589 |
* <b><a id="ubc">Blocks</a></b> are specified with the prefix {@code In}, as in |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
590 |
* {@code InMongolian}, or by using the keyword {@code block} (or its short |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
591 |
* form {@code blk}) as in {@code block=Mongolian} or {@code blk=Mongolian}. |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
592 |
* <p> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
593 |
* The block names supported by {@code Pattern} are the valid block names |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
594 |
* accepted and defined by |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
595 |
* {@link java.lang.Character.UnicodeBlock#forName(String) UnicodeBlock.forName}. |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
596 |
* <p> |
18787 | 597 |
* |
44844
b2b4d98404ba
8179364: update "<a name=" in java.base module to use id attribute
jjg
parents:
44122
diff
changeset
|
598 |
* <b><a id="ucc">Categories</a></b> may be specified with the optional prefix {@code Is}: |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
599 |
* Both {@code \p{L}} and {@code \p{IsL}} denote the category of Unicode |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
600 |
* letters. Same as scripts and blocks, categories can also be specified |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
601 |
* by using the keyword {@code general_category} (or its short form |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
602 |
* {@code gc}) as in {@code general_category=Lu} or {@code gc=Lu}. |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
603 |
* <p> |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
604 |
* The supported categories are those of |
2 | 605 |
* <a href="http://www.unicode.org/unicode/standard/standard.html"> |
606 |
* <i>The Unicode Standard</i></a> in the version specified by the |
|
607 |
* {@link java.lang.Character Character} class. The category names are those |
|
608 |
* defined in the Standard, both normative and informative. |
|
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
609 |
* <p> |
18787 | 610 |
* |
44844
b2b4d98404ba
8179364: update "<a name=" in java.base module to use id attribute
jjg
parents:
44122
diff
changeset
|
611 |
* <b><a id="ubpc">Binary properties</a></b> are specified with the prefix {@code Is}, as in |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
612 |
* {@code IsAlphabetic}. The supported binary properties by {@code Pattern} |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
613 |
* are |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
614 |
* <ul> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
615 |
* <li> Alphabetic |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
616 |
* <li> Ideographic |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
617 |
* <li> Letter |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
618 |
* <li> Lowercase |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
619 |
* <li> Uppercase |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
620 |
* <li> Titlecase |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
621 |
* <li> Punctuation |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
622 |
* <Li> Control |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
623 |
* <li> White_Space |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
624 |
* <li> Digit |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
625 |
* <li> Hex_Digit |
17434
4a04d7127e80
8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents:
17183
diff
changeset
|
626 |
* <li> Join_Control |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
627 |
* <li> Noncharacter_Code_Point |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
628 |
* <li> Assigned |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
629 |
* </ul> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
630 |
* <p> |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
631 |
* The following <b>Predefined Character classes</b> and <b>POSIX character classes</b> |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
632 |
* are in conformance with the recommendation of <i>Annex C: Compatibility Properties</i> |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
633 |
* of <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Regular Expression |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
634 |
* </i></a>, when {@link #UNICODE_CHARACTER_CLASS} flag is specified. |
21334 | 635 |
* |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
636 |
* <table class="striped"> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
637 |
* <caption style="display:none">predefined and posix character classes in Unicode mode</caption> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
638 |
* <thead> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
639 |
* <tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
640 |
* <th scope="col" id="predef_classes">Classes</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
641 |
* <th scope="col" id="predef_matches">Matches</th> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
642 |
* </tr> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
643 |
* </thead> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
644 |
* <tbody> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
645 |
* <tr><th scope="row">{@code \p{Lower}}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
646 |
* <td>A lowercase character:{@code \p{IsLowercase}}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
647 |
* <tr><th scope="row">{@code \p{Upper}}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
648 |
* <td>An uppercase character:{@code \p{IsUppercase}}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
649 |
* <tr><th scope="row">{@code \p{ASCII}}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
650 |
* <td>All ASCII:{@code [\x00-\x7F]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
651 |
* <tr><th scope="row">{@code \p{Alpha}}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
652 |
* <td>An alphabetic character:{@code \p{IsAlphabetic}}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
653 |
* <tr><th scope="row">{@code \p{Digit}}</th> |
46058
d7cc0adf7ce1
8185754: Typo in java.util.regex.Pattern javadoc: no slash in \p class
igerasim
parents:
45888
diff
changeset
|
654 |
* <td>A decimal digit character:{@code \p{IsDigit}}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
655 |
* <tr><th scope="row">{@code \p{Alnum}}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
656 |
* <td>An alphanumeric character:{@code [\p{IsAlphabetic}\p{IsDigit}]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
657 |
* <tr><th scope="row">{@code \p{Punct}}</th> |
46058
d7cc0adf7ce1
8185754: Typo in java.util.regex.Pattern javadoc: no slash in \p class
igerasim
parents:
45888
diff
changeset
|
658 |
* <td>A punctuation character:{@code \p{IsPunctuation}}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
659 |
* <tr><th scope="row">{@code \p{Graph}}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
660 |
* <td>A visible character: {@code [^\p{IsWhite_Space}\p{gc=Cc}\p{gc=Cs}\p{gc=Cn}]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
661 |
* <tr><th scope="row">{@code \p{Print}}</th> |
18787 | 662 |
* <td>A printable character: {@code [\p{Graph}\p{Blank}&&[^\p{Cntrl}]]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
663 |
* <tr><th scope="row">{@code \p{Blank}}</th> |
18787 | 664 |
* <td>A space or a tab: {@code [\p{IsWhite_Space}&&[^\p{gc=Zl}\p{gc=Zp}\x0a\x0b\x0c\x0d\x85]]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
665 |
* <tr><th scope="row">{@code \p{Cntrl}}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
666 |
* <td>A control character: {@code \p{gc=Cc}}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
667 |
* <tr><th scope="row">{@code \p{XDigit}}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
668 |
* <td>A hexadecimal digit: {@code [\p{gc=Nd}\p{IsHex_Digit}]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
669 |
* <tr><th scope="row">{@code \p{Space}}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
670 |
* <td>A whitespace character:{@code \p{IsWhite_Space}}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
671 |
* <tr><th scope="row">{@code \d}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
672 |
* <td>A digit: {@code \p{IsDigit}}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
673 |
* <tr><th scope="row">{@code \D}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
674 |
* <td>A non-digit: {@code [^\d]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
675 |
* <tr><th scope="row">{@code \s}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
676 |
* <td>A whitespace character: {@code \p{IsWhite_Space}}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
677 |
* <tr><th scope="row">{@code \S}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
678 |
* <td>A non-whitespace character: {@code [^\s]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
679 |
* <tr><th scope="row">{@code \w}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
680 |
* <td>A word character: {@code [\p{Alpha}\p{gc=Mn}\p{gc=Me}\p{gc=Mc}\p{Digit}\p{gc=Pc}\p{IsJoin_Control}]}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
681 |
* <tr><th scope="row">{@code \W}</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
682 |
* <td>A non-word character: {@code [^\w]}</td></tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
683 |
* </tbody> |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
684 |
* </table> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
685 |
* <p> |
44844
b2b4d98404ba
8179364: update "<a name=" in java.base module to use id attribute
jjg
parents:
44122
diff
changeset
|
686 |
* <a id="jcc"> |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
687 |
* Categories that behave like the java.lang.Character |
2 | 688 |
* boolean is<i>methodname</i> methods (except for the deprecated ones) are |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
689 |
* available through the same <code>\p{</code><i>prop</i><code>}</code> syntax where |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
690 |
* the specified property has the name <code>java<i>methodname</i></code></a>. |
2 | 691 |
* |
54206 | 692 |
* <h2> Comparison to Perl 5 </h2> |
2 | 693 |
* |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
694 |
* <p>The {@code Pattern} engine performs traditional NFA-based matching |
2 | 695 |
* with ordered alternation as occurs in Perl 5. |
696 |
* |
|
697 |
* <p> Perl constructs not supported by this class: </p> |
|
698 |
* |
|
699 |
* <ul> |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
700 |
* <li><p> The backreference constructs, <code>\g{</code><i>n</i><code>}</code> for |
9543 | 701 |
* the <i>n</i><sup>th</sup><a href="#cg">capturing group</a> and |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
702 |
* <code>\g{</code><i>name</i><code>}</code> for |
9543 | 703 |
* <a href="#groupname">named-capturing group</a>. |
704 |
* </p></li> |
|
705 |
* |
|
706 |
* <li><p> The conditional constructs |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
707 |
* {@code (?(}<i>condition</i>{@code )}<i>X</i>{@code )} and |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
708 |
* {@code (?(}<i>condition</i>{@code )}<i>X</i>{@code |}<i>Y</i>{@code )}, |
2 | 709 |
* </p></li> |
710 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
711 |
* <li><p> The embedded code constructs <code>(?{</code><i>code</i><code>})</code> |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
712 |
* and <code>(??{</code><i>code</i><code>})</code>,</p></li> |
2 | 713 |
* |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
714 |
* <li><p> The embedded comment syntax {@code (?#comment)}, and </p></li> |
2 | 715 |
* |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
716 |
* <li><p> The preprocessing operations {@code \l} <code>\u</code>, |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
717 |
* {@code \L}, and {@code \U}. </p></li> |
2 | 718 |
* |
719 |
* </ul> |
|
720 |
* |
|
721 |
* <p> Constructs supported by this class but not by Perl: </p> |
|
722 |
* |
|
723 |
* <ul> |
|
724 |
* |
|
725 |
* <li><p> Character-class union and intersection as described |
|
726 |
* <a href="#cc">above</a>.</p></li> |
|
727 |
* |
|
728 |
* </ul> |
|
729 |
* |
|
730 |
* <p> Notable differences from Perl: </p> |
|
731 |
* |
|
732 |
* <ul> |
|
733 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
734 |
* <li><p> In Perl, {@code \1} through {@code \9} are always interpreted |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
735 |
* as back references; a backslash-escaped number greater than {@code 9} is |
2 | 736 |
* treated as a back reference if at least that many subexpressions exist, |
737 |
* otherwise it is interpreted, if possible, as an octal escape. In this |
|
738 |
* class octal escapes must always begin with a zero. In this class, |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
739 |
* {@code \1} through {@code \9} are always interpreted as back |
2 | 740 |
* references, and a larger number is accepted as a back reference if at |
741 |
* least that many subexpressions exist at that point in the regular |
|
742 |
* expression, otherwise the parser will drop digits until the number is |
|
743 |
* smaller or equal to the existing number of groups or it is one digit. |
|
744 |
* </p></li> |
|
745 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
746 |
* <li><p> Perl uses the {@code g} flag to request a match that resumes |
2 | 747 |
* where the last match left off. This functionality is provided implicitly |
748 |
* by the {@link Matcher} class: Repeated invocations of the {@link |
|
749 |
* Matcher#find find} method will resume where the last match left off, |
|
750 |
* unless the matcher is reset. </p></li> |
|
751 |
* |
|
752 |
* <li><p> In Perl, embedded flags at the top level of an expression affect |
|
753 |
* the whole expression. In this class, embedded flags always take effect |
|
754 |
* at the point at which they appear, whether they are at the top level or |
|
755 |
* within a group; in the latter case, flags are restored at the end of the |
|
756 |
* group just as in Perl. </p></li> |
|
757 |
* |
|
758 |
* </ul> |
|
759 |
* |
|
760 |
* |
|
761 |
* <p> For a more precise description of the behavior of regular expression |
|
762 |
* constructs, please see <a href="http://www.oreilly.com/catalog/regex3/"> |
|
763 |
* <i>Mastering Regular Expressions, 3nd Edition</i>, Jeffrey E. F. Friedl, |
|
764 |
* O'Reilly and Associates, 2006.</a> |
|
765 |
* </p> |
|
766 |
* |
|
767 |
* @see java.lang.String#split(String, int) |
|
768 |
* @see java.lang.String#split(String) |
|
769 |
* |
|
770 |
* @author Mike McCloskey |
|
771 |
* @author Mark Reinhold |
|
772 |
* @author JSR-51 Expert Group |
|
773 |
* @since 1.4 |
|
774 |
* @spec JSR-51 |
|
775 |
*/ |
|
776 |
||
777 |
public final class Pattern |
|
778 |
implements java.io.Serializable |
|
779 |
{ |
|
780 |
||
781 |
/** |
|
782 |
* Regular expression modifier values. Instead of being passed as |
|
783 |
* arguments, they can also be passed as inline modifiers. |
|
784 |
* For example, the following statements have the same effect. |
|
785 |
* <pre> |
|
48853
84b4ffbba8b0
8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents:
48491
diff
changeset
|
786 |
* Pattern p1 = Pattern.compile("abc", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE); |
84b4ffbba8b0
8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents:
48491
diff
changeset
|
787 |
* Pattern p2 = Pattern.compile("(?im)abc", 0); |
2 | 788 |
* </pre> |
789 |
*/ |
|
790 |
||
791 |
/** |
|
792 |
* Enables Unix lines mode. |
|
793 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
794 |
* <p> In this mode, only the {@code '\n'} line terminator is recognized |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
795 |
* in the behavior of {@code .}, {@code ^}, and {@code $}. |
2 | 796 |
* |
797 |
* <p> Unix lines mode can also be enabled via the embedded flag |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
798 |
* expression {@code (?d)}. |
2 | 799 |
*/ |
800 |
public static final int UNIX_LINES = 0x01; |
|
801 |
||
802 |
/** |
|
803 |
* Enables case-insensitive matching. |
|
804 |
* |
|
805 |
* <p> By default, case-insensitive matching assumes that only characters |
|
806 |
* in the US-ASCII charset are being matched. Unicode-aware |
|
807 |
* case-insensitive matching can be enabled by specifying the {@link |
|
808 |
* #UNICODE_CASE} flag in conjunction with this flag. |
|
809 |
* |
|
810 |
* <p> Case-insensitive matching can also be enabled via the embedded flag |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
811 |
* expression {@code (?i)}. |
2 | 812 |
* |
813 |
* <p> Specifying this flag may impose a slight performance penalty. </p> |
|
814 |
*/ |
|
815 |
public static final int CASE_INSENSITIVE = 0x02; |
|
816 |
||
817 |
/** |
|
818 |
* Permits whitespace and comments in pattern. |
|
819 |
* |
|
820 |
* <p> In this mode, whitespace is ignored, and embedded comments starting |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
821 |
* with {@code #} are ignored until the end of a line. |
2 | 822 |
* |
823 |
* <p> Comments mode can also be enabled via the embedded flag |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
824 |
* expression {@code (?x)}. |
2 | 825 |
*/ |
826 |
public static final int COMMENTS = 0x04; |
|
827 |
||
828 |
/** |
|
829 |
* Enables multiline mode. |
|
830 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
831 |
* <p> In multiline mode the expressions {@code ^} and {@code $} match |
2 | 832 |
* just after or just before, respectively, a line terminator or the end of |
833 |
* the input sequence. By default these expressions only match at the |
|
834 |
* beginning and the end of the entire input sequence. |
|
835 |
* |
|
836 |
* <p> Multiline mode can also be enabled via the embedded flag |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
837 |
* expression {@code (?m)}. </p> |
2 | 838 |
*/ |
839 |
public static final int MULTILINE = 0x08; |
|
840 |
||
841 |
/** |
|
842 |
* Enables literal parsing of the pattern. |
|
843 |
* |
|
844 |
* <p> When this flag is specified then the input string that specifies |
|
845 |
* the pattern is treated as a sequence of literal characters. |
|
846 |
* Metacharacters or escape sequences in the input sequence will be |
|
847 |
* given no special meaning. |
|
848 |
* |
|
849 |
* <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on |
|
850 |
* matching when used in conjunction with this flag. The other flags |
|
851 |
* become superfluous. |
|
852 |
* |
|
853 |
* <p> There is no embedded flag character for enabling literal parsing. |
|
854 |
* @since 1.5 |
|
855 |
*/ |
|
856 |
public static final int LITERAL = 0x10; |
|
857 |
||
858 |
/** |
|
859 |
* Enables dotall mode. |
|
860 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
861 |
* <p> In dotall mode, the expression {@code .} matches any character, |
2 | 862 |
* including a line terminator. By default this expression does not match |
863 |
* line terminators. |
|
864 |
* |
|
865 |
* <p> Dotall mode can also be enabled via the embedded flag |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
866 |
* expression {@code (?s)}. (The {@code s} is a mnemonic for |
2 | 867 |
* "single-line" mode, which is what this is called in Perl.) </p> |
868 |
*/ |
|
869 |
public static final int DOTALL = 0x20; |
|
870 |
||
871 |
/** |
|
872 |
* Enables Unicode-aware case folding. |
|
873 |
* |
|
874 |
* <p> When this flag is specified then case-insensitive matching, when |
|
875 |
* enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner |
|
876 |
* consistent with the Unicode Standard. By default, case-insensitive |
|
877 |
* matching assumes that only characters in the US-ASCII charset are being |
|
878 |
* matched. |
|
879 |
* |
|
880 |
* <p> Unicode-aware case folding can also be enabled via the embedded flag |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
881 |
* expression {@code (?u)}. |
2 | 882 |
* |
883 |
* <p> Specifying this flag may impose a performance penalty. </p> |
|
884 |
*/ |
|
885 |
public static final int UNICODE_CASE = 0x40; |
|
886 |
||
887 |
/** |
|
888 |
* Enables canonical equivalence. |
|
889 |
* |
|
890 |
* <p> When this flag is specified then two characters will be considered |
|
891 |
* to match if, and only if, their full canonical decompositions match. |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
892 |
* The expression <code>"a\u030A"</code>, for example, will match the |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
893 |
* string <code>"\u00E5"</code> when this flag is specified. By default, |
2 | 894 |
* matching does not take canonical equivalence into account. |
895 |
* |
|
896 |
* <p> There is no embedded flag character for enabling canonical |
|
897 |
* equivalence. |
|
898 |
* |
|
899 |
* <p> Specifying this flag may impose a performance penalty. </p> |
|
900 |
*/ |
|
901 |
public static final int CANON_EQ = 0x80; |
|
902 |
||
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
903 |
/** |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
904 |
* Enables the Unicode version of <i>Predefined character classes</i> and |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
905 |
* <i>POSIX character classes</i>. |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
906 |
* |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
907 |
* <p> When this flag is specified then the (US-ASCII only) |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
908 |
* <i>Predefined character classes</i> and <i>POSIX character classes</i> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
909 |
* are in conformance with |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
910 |
* <a href="http://www.unicode.org/reports/tr18/"><i>Unicode Technical |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
911 |
* Standard #18: Unicode Regular Expression</i></a> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
912 |
* <i>Annex C: Compatibility Properties</i>. |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
913 |
* <p> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
914 |
* The UNICODE_CHARACTER_CLASS mode can also be enabled via the embedded |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
915 |
* flag expression {@code (?U)}. |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
916 |
* <p> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
917 |
* The flag implies UNICODE_CASE, that is, it enables Unicode-aware case |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
918 |
* folding. |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
919 |
* <p> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
920 |
* Specifying this flag may impose a performance penalty. </p> |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
921 |
* @since 1.7 |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
922 |
*/ |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
923 |
public static final int UNICODE_CHARACTER_CLASS = 0x100; |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
924 |
|
25523
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
925 |
/** |
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
926 |
* Contains all possible flags for compile(regex, flags). |
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
927 |
*/ |
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
928 |
private static final int ALL_FLAGS = CASE_INSENSITIVE | MULTILINE | |
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
929 |
DOTALL | UNICODE_CASE | CANON_EQ | UNIX_LINES | LITERAL | |
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
930 |
UNICODE_CHARACTER_CLASS | COMMENTS; |
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
931 |
|
2 | 932 |
/* Pattern has only two serialized components: The pattern string |
933 |
* and the flags, which are all that is needed to recompile the pattern |
|
934 |
* when it is deserialized. |
|
935 |
*/ |
|
936 |
||
937 |
/** use serialVersionUID from Merlin b59 for interoperability */ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
57941
diff
changeset
|
938 |
@java.io.Serial |
2 | 939 |
private static final long serialVersionUID = 5073258162644648461L; |
940 |
||
941 |
/** |
|
942 |
* The original regular-expression pattern string. |
|
943 |
* |
|
944 |
* @serial |
|
945 |
*/ |
|
946 |
private String pattern; |
|
947 |
||
948 |
/** |
|
949 |
* The original pattern flags. |
|
950 |
* |
|
951 |
* @serial |
|
952 |
*/ |
|
953 |
private int flags; |
|
954 |
||
955 |
/** |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
956 |
* The temporary pattern flags used during compiling. The flags might be turn |
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
957 |
* on and off by embedded flag. |
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
958 |
*/ |
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
959 |
private transient int flags0; |
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
960 |
|
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
961 |
/** |
2 | 962 |
* Boolean indicating this Pattern is compiled; this is necessary in order |
963 |
* to lazily compile deserialized Patterns. |
|
964 |
*/ |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
34684
diff
changeset
|
965 |
private transient volatile boolean compiled; |
2 | 966 |
|
967 |
/** |
|
968 |
* The normalized pattern string. |
|
969 |
*/ |
|
970 |
private transient String normalizedPattern; |
|
971 |
||
972 |
/** |
|
973 |
* The starting point of state machine for the find operation. This allows |
|
974 |
* a match to start anywhere in the input. |
|
975 |
*/ |
|
976 |
transient Node root; |
|
977 |
||
978 |
/** |
|
979 |
* The root of object tree for a match operation. The pattern is matched |
|
980 |
* at the beginning. This may include a find that uses BnM or a First |
|
981 |
* node. |
|
982 |
*/ |
|
983 |
transient Node matchRoot; |
|
984 |
||
985 |
/** |
|
986 |
* Temporary storage used by parsing pattern slice. |
|
987 |
*/ |
|
988 |
transient int[] buffer; |
|
989 |
||
990 |
/** |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
991 |
* A temporary storage used for predicate for double return. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
992 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
993 |
transient CharPredicate predicate; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
994 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
995 |
/** |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
996 |
* Map the "name" of the "named capturing group" to its group id |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
997 |
* node. |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
998 |
*/ |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
999 |
transient volatile Map<String, Integer> namedGroups; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1000 |
|
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1001 |
/** |
2 | 1002 |
* Temporary storage used while parsing group references. |
1003 |
*/ |
|
1004 |
transient GroupHead[] groupNodes; |
|
1005 |
||
1006 |
/** |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1007 |
* Temporary storage used to store the top level closure nodes. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1008 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1009 |
transient List<Node> topClosureNodes; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1010 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1011 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1012 |
* The number of top greedy closure nodes in this Pattern. Used by |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1013 |
* matchers to allocate storage needed for a IntHashSet to keep the |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1014 |
* beginning pos {@code i} of all failed match. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1015 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1016 |
transient int localTCNCount; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1017 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1018 |
/* |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1019 |
* Turn off the stop-exponential-backtracking optimization if there |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1020 |
* is a group ref in the pattern. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1021 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1022 |
transient boolean hasGroupRef; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1023 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1024 |
/** |
2 | 1025 |
* Temporary null terminated code point array used by pattern compiling. |
1026 |
*/ |
|
1027 |
private transient int[] temp; |
|
1028 |
||
1029 |
/** |
|
1030 |
* The number of capturing groups in this Pattern. Used by matchers to |
|
1031 |
* allocate storage needed to perform a match. |
|
1032 |
*/ |
|
1033 |
transient int capturingGroupCount; |
|
1034 |
||
1035 |
/** |
|
1036 |
* The local variable count used by parsing tree. Used by matchers to |
|
1037 |
* allocate storage needed to perform a match. |
|
1038 |
*/ |
|
1039 |
transient int localCount; |
|
1040 |
||
1041 |
/** |
|
1042 |
* Index into the pattern string that keeps track of how much has been |
|
1043 |
* parsed. |
|
1044 |
*/ |
|
1045 |
private transient int cursor; |
|
1046 |
||
1047 |
/** |
|
1048 |
* Holds the length of the pattern string. |
|
1049 |
*/ |
|
1050 |
private transient int patternLength; |
|
1051 |
||
1052 |
/** |
|
4820
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
1053 |
* If the Start node might possibly match supplementary characters. |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
1054 |
* It is set to true during compiling if |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
1055 |
* (1) There is supplementary char in pattern, or |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1056 |
* (2) There is complement node of a "family" CharProperty |
4820
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
1057 |
*/ |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
1058 |
private transient boolean hasSupplementary; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
1059 |
|
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
1060 |
/** |
18787 | 1061 |
* Compiles the given regular expression into a pattern. |
2 | 1062 |
* |
1063 |
* @param regex |
|
1064 |
* The expression to be compiled |
|
18787 | 1065 |
* @return the given regular expression compiled into a pattern |
2 | 1066 |
* @throws PatternSyntaxException |
1067 |
* If the expression's syntax is invalid |
|
1068 |
*/ |
|
1069 |
public static Pattern compile(String regex) { |
|
1070 |
return new Pattern(regex, 0); |
|
1071 |
} |
|
1072 |
||
1073 |
/** |
|
1074 |
* Compiles the given regular expression into a pattern with the given |
|
18787 | 1075 |
* flags. |
2 | 1076 |
* |
1077 |
* @param regex |
|
1078 |
* The expression to be compiled |
|
1079 |
* |
|
1080 |
* @param flags |
|
1081 |
* Match flags, a bit mask that may include |
|
1082 |
* {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL}, |
|
1083 |
* {@link #UNICODE_CASE}, {@link #CANON_EQ}, {@link #UNIX_LINES}, |
|
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
1084 |
* {@link #LITERAL}, {@link #UNICODE_CHARACTER_CLASS} |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
1085 |
* and {@link #COMMENTS} |
2 | 1086 |
* |
18787 | 1087 |
* @return the given regular expression compiled into a pattern with the given flags |
2 | 1088 |
* @throws IllegalArgumentException |
1089 |
* If bit values other than those corresponding to the defined |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1090 |
* match flags are set in {@code flags} |
2 | 1091 |
* |
1092 |
* @throws PatternSyntaxException |
|
1093 |
* If the expression's syntax is invalid |
|
1094 |
*/ |
|
1095 |
public static Pattern compile(String regex, int flags) { |
|
1096 |
return new Pattern(regex, flags); |
|
1097 |
} |
|
1098 |
||
1099 |
/** |
|
1100 |
* Returns the regular expression from which this pattern was compiled. |
|
1101 |
* |
|
1102 |
* @return The source of this pattern |
|
1103 |
*/ |
|
1104 |
public String pattern() { |
|
1105 |
return pattern; |
|
1106 |
} |
|
1107 |
||
1108 |
/** |
|
1109 |
* <p>Returns the string representation of this pattern. This |
|
1110 |
* is the regular expression from which this pattern was |
|
1111 |
* compiled.</p> |
|
1112 |
* |
|
1113 |
* @return The string representation of this pattern |
|
1114 |
* @since 1.5 |
|
1115 |
*/ |
|
1116 |
public String toString() { |
|
1117 |
return pattern; |
|
1118 |
} |
|
1119 |
||
1120 |
/** |
|
1121 |
* Creates a matcher that will match the given input against this pattern. |
|
1122 |
* |
|
1123 |
* @param input |
|
1124 |
* The character sequence to be matched |
|
1125 |
* |
|
1126 |
* @return A new matcher for this pattern |
|
1127 |
*/ |
|
1128 |
public Matcher matcher(CharSequence input) { |
|
1129 |
if (!compiled) { |
|
1130 |
synchronized(this) { |
|
1131 |
if (!compiled) |
|
1132 |
compile(); |
|
1133 |
} |
|
1134 |
} |
|
1135 |
Matcher m = new Matcher(this, input); |
|
1136 |
return m; |
|
1137 |
} |
|
1138 |
||
1139 |
/** |
|
18787 | 1140 |
* Returns this pattern's match flags. |
2 | 1141 |
* |
1142 |
* @return The match flags specified when this pattern was compiled |
|
1143 |
*/ |
|
1144 |
public int flags() { |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
1145 |
return flags0; |
2 | 1146 |
} |
1147 |
||
1148 |
/** |
|
1149 |
* Compiles the given regular expression and attempts to match the given |
|
1150 |
* input against it. |
|
1151 |
* |
|
1152 |
* <p> An invocation of this convenience method of the form |
|
1153 |
* |
|
1154 |
* <blockquote><pre> |
|
1155 |
* Pattern.matches(regex, input);</pre></blockquote> |
|
1156 |
* |
|
1157 |
* behaves in exactly the same way as the expression |
|
1158 |
* |
|
1159 |
* <blockquote><pre> |
|
1160 |
* Pattern.compile(regex).matcher(input).matches()</pre></blockquote> |
|
1161 |
* |
|
1162 |
* <p> If a pattern is to be used multiple times, compiling it once and reusing |
|
1163 |
* it will be more efficient than invoking this method each time. </p> |
|
1164 |
* |
|
1165 |
* @param regex |
|
1166 |
* The expression to be compiled |
|
1167 |
* |
|
1168 |
* @param input |
|
1169 |
* The character sequence to be matched |
|
18787 | 1170 |
* @return whether or not the regular expression matches on the input |
2 | 1171 |
* @throws PatternSyntaxException |
1172 |
* If the expression's syntax is invalid |
|
1173 |
*/ |
|
1174 |
public static boolean matches(String regex, CharSequence input) { |
|
1175 |
Pattern p = Pattern.compile(regex); |
|
1176 |
Matcher m = p.matcher(input); |
|
1177 |
return m.matches(); |
|
1178 |
} |
|
1179 |
||
1180 |
/** |
|
1181 |
* Splits the given input sequence around matches of this pattern. |
|
1182 |
* |
|
1183 |
* <p> The array returned by this method contains each substring of the |
|
1184 |
* input sequence that is terminated by another subsequence that matches |
|
1185 |
* this pattern or is terminated by the end of the input sequence. The |
|
1186 |
* substrings in the array are in the order in which they occur in the |
|
21668 | 1187 |
* input. If this pattern does not match any subsequence of the input then |
2 | 1188 |
* the resulting array has just one element, namely the input sequence in |
21670
ca3553133ede
8028321: Fix for String.split() empty input sequence/JDK-6559590 triggers regression
sherman
parents:
21668
diff
changeset
|
1189 |
* string form. |
21668 | 1190 |
* |
1191 |
* <p> When there is a positive-width match at the beginning of the input |
|
1192 |
* sequence then an empty leading substring is included at the beginning |
|
1193 |
* of the resulting array. A zero-width match at the beginning however |
|
1194 |
* never produces such empty leading substring. |
|
2 | 1195 |
* |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1196 |
* <p> The {@code limit} parameter controls the number of times the |
2 | 1197 |
* pattern is applied and therefore affects the length of the resulting |
50237
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1198 |
* array. |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1199 |
* <ul> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1200 |
* <li><p> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1201 |
* If the <i>limit</i> is positive then the pattern will be applied |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1202 |
* at most <i>limit</i> - 1 times, the array's length will be |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1203 |
* no greater than <i>limit</i>, and the array's last entry will contain |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1204 |
* all input beyond the last matched delimiter.</p></li> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1205 |
* |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1206 |
* <li><p> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1207 |
* If the <i>limit</i> is zero then the pattern will be applied as |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1208 |
* many times as possible, the array can have any length, and trailing |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1209 |
* empty strings will be discarded.</p></li> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1210 |
* |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1211 |
* <li><p> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1212 |
* If the <i>limit</i> is negative then the pattern will be applied |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1213 |
* as many times as possible and the array can have any length.</p></li> |
ec52b4d094c0
8200172: String.split non-positive term incorrect use
sherman
parents:
49763
diff
changeset
|
1214 |
* </ul> |
2 | 1215 |
* |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1216 |
* <p> The input {@code "boo:and:foo"}, for example, yields the following |
2 | 1217 |
* results with these parameters: |
1218 |
* |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1219 |
* <table class="plain" style="margin-left:2em;"> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1220 |
* <caption style="display:none">Split example showing regex, limit, and result</caption> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
1221 |
* <thead> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1222 |
* <tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1223 |
* <th scope="col">Regex</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1224 |
* <th scope="col">Limit</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1225 |
* <th scope="col">Result</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1226 |
* </tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
1227 |
* </thead> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
1228 |
* <tbody> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1229 |
* <tr><th scope="row" rowspan="3" style="font-weight:normal">:</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1230 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">2</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1231 |
* <td>{@code { "boo", "and:foo" }}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1232 |
* <tr><!-- : --> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1233 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">5</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1234 |
* <td>{@code { "boo", "and", "foo" }}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1235 |
* <tr><!-- : --> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1236 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">-2</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1237 |
* <td>{@code { "boo", "and", "foo" }}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1238 |
* <tr><th scope="row" rowspan="3" style="font-weight:normal">o</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1239 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">5</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1240 |
* <td>{@code { "b", "", ":and:f", "", "" }}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1241 |
* <tr><!-- o --> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1242 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">-2</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1243 |
* <td>{@code { "b", "", ":and:f", "", "" }}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1244 |
* <tr><!-- o --> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1245 |
* <th scope="row" style="font-weight:normal; text-align:right; padding-right:1em">0</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1246 |
* <td>{@code { "b", "", ":and:f" }}</td></tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
1247 |
* </tbody> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1248 |
* </table> |
2 | 1249 |
* |
1250 |
* @param input |
|
1251 |
* The character sequence to be split |
|
1252 |
* |
|
1253 |
* @param limit |
|
1254 |
* The result threshold, as described above |
|
1255 |
* |
|
1256 |
* @return The array of strings computed by splitting the input |
|
1257 |
* around matches of this pattern |
|
1258 |
*/ |
|
1259 |
public String[] split(CharSequence input, int limit) { |
|
1260 |
int index = 0; |
|
1261 |
boolean matchLimited = limit > 0; |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5627
diff
changeset
|
1262 |
ArrayList<String> matchList = new ArrayList<>(); |
2 | 1263 |
Matcher m = matcher(input); |
1264 |
||
1265 |
// Add segments before each match found |
|
1266 |
while(m.find()) { |
|
1267 |
if (!matchLimited || matchList.size() < limit - 1) { |
|
21668 | 1268 |
if (index == 0 && index == m.start() && m.start() == m.end()) { |
1269 |
// no empty leading substring included for zero-width match |
|
1270 |
// at the beginning of the input char sequence. |
|
1271 |
continue; |
|
1272 |
} |
|
2 | 1273 |
String match = input.subSequence(index, m.start()).toString(); |
1274 |
matchList.add(match); |
|
1275 |
index = m.end(); |
|
1276 |
} else if (matchList.size() == limit - 1) { // last one |
|
1277 |
String match = input.subSequence(index, |
|
1278 |
input.length()).toString(); |
|
1279 |
matchList.add(match); |
|
1280 |
index = m.end(); |
|
1281 |
} |
|
1282 |
} |
|
1283 |
||
1284 |
// If no match was found, return this |
|
1285 |
if (index == 0) |
|
1286 |
return new String[] {input.toString()}; |
|
1287 |
||
1288 |
// Add remaining segment |
|
1289 |
if (!matchLimited || matchList.size() < limit) |
|
1290 |
matchList.add(input.subSequence(index, input.length()).toString()); |
|
1291 |
||
1292 |
// Construct result |
|
1293 |
int resultSize = matchList.size(); |
|
1294 |
if (limit == 0) |
|
52902
e3398b2e1ab0
8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents:
52080
diff
changeset
|
1295 |
while (resultSize > 0 && matchList.get(resultSize-1).isEmpty()) |
2 | 1296 |
resultSize--; |
1297 |
String[] result = new String[resultSize]; |
|
1298 |
return matchList.subList(0, resultSize).toArray(result); |
|
1299 |
} |
|
1300 |
||
1301 |
/** |
|
1302 |
* Splits the given input sequence around matches of this pattern. |
|
1303 |
* |
|
1304 |
* <p> This method works as if by invoking the two-argument {@link |
|
1305 |
* #split(java.lang.CharSequence, int) split} method with the given input |
|
1306 |
* sequence and a limit argument of zero. Trailing empty strings are |
|
1307 |
* therefore not included in the resulting array. </p> |
|
1308 |
* |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1309 |
* <p> The input {@code "boo:and:foo"}, for example, yields the following |
2 | 1310 |
* results with these expressions: |
1311 |
* |
|
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1312 |
* <table class="plain" style="margin-left:2em"> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
1313 |
* <caption style="display:none">Split examples showing regex and result</caption> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
1314 |
* <thead> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1315 |
* <tr> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1316 |
* <th scope="col">Regex</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1317 |
* <th scope="col">Result</th> |
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1318 |
* </tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
1319 |
* </thead> |
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
1320 |
* <tbody> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1321 |
* <tr><th scope="row" style="text-weight:normal">:</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1322 |
* <td>{@code { "boo", "and", "foo" }}</td></tr> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1323 |
* <tr><th scope="row" style="text-weight:normal">o</th> |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1324 |
* <td>{@code { "b", "", ":and:f" }}</td></tr> |
45124
144479e89cdb
8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents:
44846
diff
changeset
|
1325 |
* </tbody> |
46900
e92e67ed12b4
8186466: Fix accessibility and other minor issues in java.base
jjg
parents:
46058
diff
changeset
|
1326 |
* </table> |
2 | 1327 |
* |
1328 |
* |
|
1329 |
* @param input |
|
1330 |
* The character sequence to be split |
|
1331 |
* |
|
1332 |
* @return The array of strings computed by splitting the input |
|
1333 |
* around matches of this pattern |
|
1334 |
*/ |
|
1335 |
public String[] split(CharSequence input) { |
|
1336 |
return split(input, 0); |
|
1337 |
} |
|
1338 |
||
1339 |
/** |
|
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1340 |
* Returns a literal pattern {@code String} for the specified |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1341 |
* {@code String}. |
2 | 1342 |
* |
32108
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1343 |
* <p>This method produces a {@code String} that can be used to |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1344 |
* create a {@code Pattern} that would match the string |
aa5490a167ee
8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents:
32012
diff
changeset
|
1345 |
* {@code s} as if it were a literal pattern.</p> Metacharacters |
2 | 1346 |
* or escape sequences in the input sequence will be given no special |
1347 |
* meaning. |
|
1348 |
* |
|
1349 |
* @param s The string to be literalized |
|
1350 |
* @return A literal string replacement |
|
1351 |
* @since 1.5 |
|
1352 |
*/ |
|
1353 |
public static String quote(String s) { |
|
1354 |
int slashEIndex = s.indexOf("\\E"); |
|
1355 |
if (slashEIndex == -1) |
|
1356 |
return "\\Q" + s + "\\E"; |
|
1357 |
||
32012 | 1358 |
int lenHint = s.length(); |
1359 |
lenHint = (lenHint < Integer.MAX_VALUE - 8 - lenHint) ? |
|
1360 |
(lenHint << 1) : (Integer.MAX_VALUE - 8); |
|
1361 |
||
1362 |
StringBuilder sb = new StringBuilder(lenHint); |
|
2 | 1363 |
sb.append("\\Q"); |
1364 |
int current = 0; |
|
32012 | 1365 |
do { |
1366 |
sb.append(s, current, slashEIndex) |
|
1367 |
.append("\\E\\\\E\\Q"); |
|
2 | 1368 |
current = slashEIndex + 2; |
32012 | 1369 |
} while ((slashEIndex = s.indexOf("\\E", current)) != -1); |
1370 |
||
1371 |
return sb.append(s, current, s.length()) |
|
1372 |
.append("\\E") |
|
1373 |
.toString(); |
|
2 | 1374 |
} |
1375 |
||
1376 |
/** |
|
1377 |
* Recompile the Pattern instance from a stream. The original pattern |
|
1378 |
* string is read in and the object tree is recompiled from it. |
|
1379 |
*/ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
57941
diff
changeset
|
1380 |
@java.io.Serial |
2 | 1381 |
private void readObject(java.io.ObjectInputStream s) |
1382 |
throws java.io.IOException, ClassNotFoundException { |
|
1383 |
||
1384 |
// Read in all fields |
|
1385 |
s.defaultReadObject(); |
|
1386 |
||
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
1387 |
// reset the flags |
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
1388 |
flags0 = flags; |
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
1389 |
|
2 | 1390 |
// Initialize counts |
1391 |
capturingGroupCount = 1; |
|
1392 |
localCount = 0; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1393 |
localTCNCount = 0; |
2 | 1394 |
|
1395 |
// if length > 0, the Pattern is lazily compiled |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52902
diff
changeset
|
1396 |
if (pattern.isEmpty()) { |
2 | 1397 |
root = new Start(lastAccept); |
1398 |
matchRoot = lastAccept; |
|
1399 |
compiled = true; |
|
1400 |
} |
|
1401 |
} |
|
1402 |
||
1403 |
/** |
|
1404 |
* This private constructor is used to create all Patterns. The pattern |
|
1405 |
* string and match flags are all that is needed to completely describe |
|
1406 |
* a Pattern. An empty pattern string results in an object tree with |
|
1407 |
* only a Start node and a LastNode node. |
|
1408 |
*/ |
|
1409 |
private Pattern(String p, int f) { |
|
25523
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
1410 |
if ((f & ~ALL_FLAGS) != 0) { |
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
1411 |
throw new IllegalArgumentException("Unknown flag 0x" |
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
1412 |
+ Integer.toHexString(f)); |
c751d1010164
8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents:
22997
diff
changeset
|
1413 |
} |
2 | 1414 |
pattern = p; |
1415 |
flags = f; |
|
1416 |
||
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
1417 |
// to use UNICODE_CASE if UNICODE_CHARACTER_CLASS present |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
1418 |
if ((flags & UNICODE_CHARACTER_CLASS) != 0) |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
1419 |
flags |= UNICODE_CASE; |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
1420 |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
1421 |
// 'flags' for compiling |
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
1422 |
flags0 = flags; |
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
1423 |
|
2 | 1424 |
// Reset group index count |
1425 |
capturingGroupCount = 1; |
|
1426 |
localCount = 0; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1427 |
localTCNCount = 0; |
2 | 1428 |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52902
diff
changeset
|
1429 |
if (!pattern.isEmpty()) { |
58623 | 1430 |
try { |
1431 |
compile(); |
|
1432 |
} catch (StackOverflowError soe) { |
|
1433 |
throw error("Stack overflow during pattern compilation"); |
|
1434 |
} |
|
2 | 1435 |
} else { |
1436 |
root = new Start(lastAccept); |
|
1437 |
matchRoot = lastAccept; |
|
1438 |
} |
|
1439 |
} |
|
1440 |
||
1441 |
/** |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1442 |
* The pattern is converted to normalized form ({@link |
52080
a2c72b476c9f
8211396: Broken link in javadoc for private java.util.regex.Pattern#normalize()
igerasim
parents:
50237
diff
changeset
|
1443 |
* java.text.Normalizer.Form#NFC NFC}, canonical decomposition, |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1444 |
* followed by canonical composition for the character class |
52080
a2c72b476c9f
8211396: Broken link in javadoc for private java.util.regex.Pattern#normalize()
igerasim
parents:
50237
diff
changeset
|
1445 |
* part, and {@link java.text.Normalizer.Form#NFD NFD}, |
a2c72b476c9f
8211396: Broken link in javadoc for private java.util.regex.Pattern#normalize()
igerasim
parents:
50237
diff
changeset
|
1446 |
* canonical decomposition for the rest), and then a pure |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1447 |
* group is constructed to match canonical equivalences of the |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1448 |
* characters. |
2 | 1449 |
*/ |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1450 |
private static String normalize(String pattern) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1451 |
int plen = pattern.length(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1452 |
StringBuilder pbuf = new StringBuilder(plen); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1453 |
char last = 0; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1454 |
int lastStart = 0; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1455 |
char cc = 0; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1456 |
for (int i = 0; i < plen;) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1457 |
char c = pattern.charAt(i); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1458 |
if (cc == 0 && // top level |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1459 |
c == '\\' && i + 1 < plen && pattern.charAt(i + 1) == '\\') { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1460 |
i += 2; last = 0; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1461 |
continue; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1462 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1463 |
if (c == '[' && last != '\\') { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1464 |
if (cc == 0) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1465 |
if (lastStart < i) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1466 |
normalizeSlice(pattern, lastStart, i, pbuf); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1467 |
lastStart = i; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1468 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1469 |
cc++; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1470 |
} else if (c == ']' && last != '\\') { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1471 |
cc--; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1472 |
if (cc == 0) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1473 |
normalizeClazz(pattern, lastStart, i + 1, pbuf); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1474 |
lastStart = i + 1; |
2 | 1475 |
} |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1476 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1477 |
last = c; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1478 |
i++; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1479 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1480 |
assert (cc == 0); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1481 |
if (lastStart < plen) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1482 |
normalizeSlice(pattern, lastStart, plen, pbuf); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1483 |
return pbuf.toString(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1484 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1485 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1486 |
private static void normalizeSlice(String src, int off, int limit, |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1487 |
StringBuilder dst) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1488 |
{ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1489 |
int len = src.length(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1490 |
int off0 = off; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1491 |
while (off < limit && ASCII.isAscii(src.charAt(off))) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1492 |
off++; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1493 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1494 |
if (off == limit) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1495 |
dst.append(src, off0, limit); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1496 |
return; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1497 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1498 |
off--; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1499 |
if (off < off0) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1500 |
off = off0; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1501 |
else |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1502 |
dst.append(src, off0, off); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1503 |
while (off < limit) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1504 |
int ch0 = src.codePointAt(off); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1505 |
if (".$|()[]{}^?*+\\".indexOf(ch0) != -1) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1506 |
dst.append((char)ch0); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1507 |
off++; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1508 |
continue; |
2 | 1509 |
} |
55013 | 1510 |
int j = Grapheme.nextBoundary(src, off, limit); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1511 |
int ch1; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1512 |
String seq = src.substring(off, j); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1513 |
String nfd = Normalizer.normalize(seq, Normalizer.Form.NFD); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1514 |
off = j; |
57624
67e58672c503
8228352: CANON_EQ breaks when pattern contains supplementary codepoint
igerasim
parents:
55494
diff
changeset
|
1515 |
if (nfd.codePointCount(0, nfd.length()) > 1) { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1516 |
ch0 = nfd.codePointAt(0); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1517 |
ch1 = nfd.codePointAt(Character.charCount(ch0)); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1518 |
if (Character.getType(ch1) == Character.NON_SPACING_MARK) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1519 |
Set<String> altns = new LinkedHashSet<>(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1520 |
altns.add(seq); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1521 |
produceEquivalentAlternation(nfd, altns); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1522 |
dst.append("(?:"); |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
1523 |
altns.forEach( s -> dst.append(s).append('|')); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1524 |
dst.delete(dst.length() - 1, dst.length()); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1525 |
dst.append(")"); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1526 |
continue; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1527 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1528 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1529 |
String nfc = Normalizer.normalize(seq, Normalizer.Form.NFC); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1530 |
if (!seq.equals(nfc) && !nfd.equals(nfc)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1531 |
dst.append("(?:" + seq + "|" + nfd + "|" + nfc + ")"); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1532 |
else if (!seq.equals(nfd)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1533 |
dst.append("(?:" + seq + "|" + nfd + ")"); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1534 |
else |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1535 |
dst.append(seq); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1536 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1537 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1538 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1539 |
private static void normalizeClazz(String src, int off, int limit, |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1540 |
StringBuilder dst) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1541 |
{ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1542 |
dst.append(Normalizer.normalize(src.substring(off, limit), Form.NFC)); |
2 | 1543 |
} |
1544 |
||
1545 |
/** |
|
1546 |
* Given a specific sequence composed of a regular character and |
|
1547 |
* combining marks that follow it, produce the alternation that will |
|
1548 |
* match all canonical equivalences of that sequence. |
|
1549 |
*/ |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1550 |
private static void produceEquivalentAlternation(String src, |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1551 |
Set<String> dst) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1552 |
{ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1553 |
int len = countChars(src, 0, 1); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1554 |
if (src.length() == len) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1555 |
dst.add(src); // source has one character. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1556 |
return; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1557 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1558 |
String base = src.substring(0,len); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1559 |
String combiningMarks = src.substring(len); |
2 | 1560 |
String[] perms = producePermutations(combiningMarks); |
1561 |
// Add combined permutations |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1562 |
for(int x = 0; x < perms.length; x++) { |
2 | 1563 |
String next = base + perms[x]; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1564 |
dst.add(next); |
2 | 1565 |
next = composeOneStep(next); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1566 |
if (next != null) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1567 |
produceEquivalentAlternation(next, dst); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1568 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1569 |
} |
2 | 1570 |
} |
1571 |
||
1572 |
/** |
|
1573 |
* Returns an array of strings that have all the possible |
|
1574 |
* permutations of the characters in the input string. |
|
1575 |
* This is used to get a list of all possible orderings |
|
1576 |
* of a set of combining marks. Note that some of the permutations |
|
1577 |
* are invalid because of combining class collisions, and these |
|
1578 |
* possibilities must be removed because they are not canonically |
|
1579 |
* equivalent. |
|
1580 |
*/ |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1581 |
private static String[] producePermutations(String input) { |
2 | 1582 |
if (input.length() == countChars(input, 0, 1)) |
1583 |
return new String[] {input}; |
|
1584 |
||
1585 |
if (input.length() == countChars(input, 0, 2)) { |
|
1586 |
int c0 = Character.codePointAt(input, 0); |
|
1587 |
int c1 = Character.codePointAt(input, Character.charCount(c0)); |
|
1588 |
if (getClass(c1) == getClass(c0)) { |
|
1589 |
return new String[] {input}; |
|
1590 |
} |
|
1591 |
String[] result = new String[2]; |
|
1592 |
result[0] = input; |
|
1593 |
StringBuilder sb = new StringBuilder(2); |
|
1594 |
sb.appendCodePoint(c1); |
|
1595 |
sb.appendCodePoint(c0); |
|
1596 |
result[1] = sb.toString(); |
|
1597 |
return result; |
|
1598 |
} |
|
1599 |
||
1600 |
int length = 1; |
|
1601 |
int nCodePoints = countCodePoints(input); |
|
1602 |
for(int x=1; x<nCodePoints; x++) |
|
1603 |
length = length * (x+1); |
|
1604 |
||
1605 |
String[] temp = new String[length]; |
|
1606 |
||
1607 |
int combClass[] = new int[nCodePoints]; |
|
1608 |
for(int x=0, i=0; x<nCodePoints; x++) { |
|
1609 |
int c = Character.codePointAt(input, i); |
|
1610 |
combClass[x] = getClass(c); |
|
1611 |
i += Character.charCount(c); |
|
1612 |
} |
|
1613 |
||
1614 |
// For each char, take it out and add the permutations |
|
1615 |
// of the remaining chars |
|
1616 |
int index = 0; |
|
1617 |
int len; |
|
1618 |
// offset maintains the index in code units. |
|
1619 |
loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) { |
|
1620 |
len = countChars(input, offset, 1); |
|
1621 |
for(int y=x-1; y>=0; y--) { |
|
1622 |
if (combClass[y] == combClass[x]) { |
|
1623 |
continue loop; |
|
1624 |
} |
|
1625 |
} |
|
1626 |
StringBuilder sb = new StringBuilder(input); |
|
1627 |
String otherChars = sb.delete(offset, offset+len).toString(); |
|
1628 |
String[] subResult = producePermutations(otherChars); |
|
1629 |
||
1630 |
String prefix = input.substring(offset, offset+len); |
|
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
1631 |
for (String sre : subResult) |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
1632 |
temp[index++] = prefix + sre; |
2 | 1633 |
} |
1634 |
String[] result = new String[index]; |
|
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
34684
diff
changeset
|
1635 |
System.arraycopy(temp, 0, result, 0, index); |
2 | 1636 |
return result; |
1637 |
} |
|
1638 |
||
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1639 |
private static int getClass(int c) { |
2 | 1640 |
return sun.text.Normalizer.getCombiningClass(c); |
1641 |
} |
|
1642 |
||
1643 |
/** |
|
1644 |
* Attempts to compose input by combining the first character |
|
1645 |
* with the first combining mark following it. Returns a String |
|
1646 |
* that is the composition of the leading character with its first |
|
1647 |
* combining mark followed by the remaining combining marks. Returns |
|
1648 |
* null if the first two characters cannot be further composed. |
|
1649 |
*/ |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1650 |
private static String composeOneStep(String input) { |
2 | 1651 |
int len = countChars(input, 0, 2); |
1652 |
String firstTwoCharacters = input.substring(0, len); |
|
1653 |
String result = Normalizer.normalize(firstTwoCharacters, Normalizer.Form.NFC); |
|
1654 |
if (result.equals(firstTwoCharacters)) |
|
1655 |
return null; |
|
1656 |
else { |
|
1657 |
String remainder = input.substring(len); |
|
1658 |
return result + remainder; |
|
1659 |
} |
|
1660 |
} |
|
1661 |
||
1662 |
/** |
|
1663 |
* Preprocess any \Q...\E sequences in `temp', meta-quoting them. |
|
1664 |
* See the description of `quotemeta' in perlfunc(1). |
|
1665 |
*/ |
|
1666 |
private void RemoveQEQuoting() { |
|
1667 |
final int pLen = patternLength; |
|
1668 |
int i = 0; |
|
1669 |
while (i < pLen-1) { |
|
1670 |
if (temp[i] != '\\') |
|
1671 |
i += 1; |
|
1672 |
else if (temp[i + 1] != 'Q') |
|
1673 |
i += 2; |
|
1674 |
else |
|
1675 |
break; |
|
1676 |
} |
|
1677 |
if (i >= pLen - 1) // No \Q sequence found |
|
1678 |
return; |
|
1679 |
int j = i; |
|
1680 |
i += 2; |
|
54714
37630ad8fa67
8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents:
54206
diff
changeset
|
1681 |
int newTempLen; |
37630ad8fa67
8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents:
54206
diff
changeset
|
1682 |
try { |
37630ad8fa67
8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents:
54206
diff
changeset
|
1683 |
newTempLen = Math.addExact(j + 2, Math.multiplyExact(3, pLen - i)); |
37630ad8fa67
8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents:
54206
diff
changeset
|
1684 |
} catch (ArithmeticException ae) { |
37630ad8fa67
8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents:
54206
diff
changeset
|
1685 |
throw new OutOfMemoryError(); |
37630ad8fa67
8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents:
54206
diff
changeset
|
1686 |
} |
37630ad8fa67
8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents:
54206
diff
changeset
|
1687 |
int[] newtemp = new int[newTempLen]; |
2 | 1688 |
System.arraycopy(temp, 0, newtemp, 0, j); |
1689 |
||
1690 |
boolean inQuote = true; |
|
11287
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1691 |
boolean beginQuote = true; |
2 | 1692 |
while (i < pLen) { |
1693 |
int c = temp[i++]; |
|
11287
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1694 |
if (!ASCII.isAscii(c) || ASCII.isAlpha(c)) { |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1695 |
newtemp[j++] = c; |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1696 |
} else if (ASCII.isDigit(c)) { |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1697 |
if (beginQuote) { |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1698 |
/* |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1699 |
* A unicode escape \[0xu] could be before this quote, |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1700 |
* and we don't want this numeric char to processed as |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1701 |
* part of the escape. |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1702 |
*/ |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1703 |
newtemp[j++] = '\\'; |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1704 |
newtemp[j++] = 'x'; |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1705 |
newtemp[j++] = '3'; |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1706 |
} |
2 | 1707 |
newtemp[j++] = c; |
1708 |
} else if (c != '\\') { |
|
1709 |
if (inQuote) newtemp[j++] = '\\'; |
|
1710 |
newtemp[j++] = c; |
|
1711 |
} else if (inQuote) { |
|
1712 |
if (temp[i] == 'E') { |
|
1713 |
i++; |
|
1714 |
inQuote = false; |
|
1715 |
} else { |
|
1716 |
newtemp[j++] = '\\'; |
|
1717 |
newtemp[j++] = '\\'; |
|
1718 |
} |
|
1719 |
} else { |
|
1720 |
if (temp[i] == 'Q') { |
|
1721 |
i++; |
|
1722 |
inQuote = true; |
|
11287
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1723 |
beginQuote = true; |
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1724 |
continue; |
2 | 1725 |
} else { |
1726 |
newtemp[j++] = c; |
|
1727 |
if (i != pLen) |
|
1728 |
newtemp[j++] = temp[i++]; |
|
1729 |
} |
|
1730 |
} |
|
11287
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1731 |
|
3db172a5433c
6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents:
9694
diff
changeset
|
1732 |
beginQuote = false; |
2 | 1733 |
} |
1734 |
||
1735 |
patternLength = j; |
|
1736 |
temp = Arrays.copyOf(newtemp, j + 2); // double zero termination |
|
1737 |
} |
|
1738 |
||
1739 |
/** |
|
1740 |
* Copies regular expression to an int array and invokes the parsing |
|
1741 |
* of the expression which will create the object tree. |
|
1742 |
*/ |
|
1743 |
private void compile() { |
|
1744 |
// Handle canonical equivalences |
|
1745 |
if (has(CANON_EQ) && !has(LITERAL)) { |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1746 |
normalizedPattern = normalize(pattern); |
2 | 1747 |
} else { |
1748 |
normalizedPattern = pattern; |
|
1749 |
} |
|
1750 |
patternLength = normalizedPattern.length(); |
|
1751 |
||
1752 |
// Copy pattern to int array for convenience |
|
1753 |
// Use double zero to terminate pattern |
|
1754 |
temp = new int[patternLength + 2]; |
|
1755 |
||
4820
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
1756 |
hasSupplementary = false; |
2 | 1757 |
int c, count = 0; |
1758 |
// Convert all chars into code points |
|
1759 |
for (int x = 0; x < patternLength; x += Character.charCount(c)) { |
|
1760 |
c = normalizedPattern.codePointAt(x); |
|
1761 |
if (isSupplementary(c)) { |
|
1762 |
hasSupplementary = true; |
|
1763 |
} |
|
1764 |
temp[count++] = c; |
|
1765 |
} |
|
1766 |
||
1767 |
patternLength = count; // patternLength now in code points |
|
1768 |
||
1769 |
if (! has(LITERAL)) |
|
1770 |
RemoveQEQuoting(); |
|
1771 |
||
1772 |
// Allocate all temporary objects here. |
|
1773 |
buffer = new int[32]; |
|
1774 |
groupNodes = new GroupHead[10]; |
|
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1775 |
namedGroups = null; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1776 |
topClosureNodes = new ArrayList<>(10); |
2 | 1777 |
|
1778 |
if (has(LITERAL)) { |
|
1779 |
// Literal pattern handling |
|
1780 |
matchRoot = newSlice(temp, patternLength, hasSupplementary); |
|
1781 |
matchRoot.next = lastAccept; |
|
1782 |
} else { |
|
1783 |
// Start recursive descent parsing |
|
1784 |
matchRoot = expr(lastAccept); |
|
1785 |
// Check extra pattern characters |
|
1786 |
if (patternLength != cursor) { |
|
1787 |
if (peek() == ')') { |
|
1788 |
throw error("Unmatched closing ')'"); |
|
1789 |
} else { |
|
1790 |
throw error("Unexpected internal error"); |
|
1791 |
} |
|
1792 |
} |
|
1793 |
} |
|
1794 |
||
1795 |
// Peephole optimization |
|
1796 |
if (matchRoot instanceof Slice) { |
|
1797 |
root = BnM.optimize(matchRoot); |
|
1798 |
if (root == matchRoot) { |
|
1799 |
root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot); |
|
1800 |
} |
|
1801 |
} else if (matchRoot instanceof Begin || matchRoot instanceof First) { |
|
1802 |
root = matchRoot; |
|
1803 |
} else { |
|
1804 |
root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot); |
|
1805 |
} |
|
1806 |
||
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1807 |
// Optimize the greedy Loop to prevent exponential backtracking, IF there |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1808 |
// is no group ref in this pattern. With a non-negative localTCNCount value, |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1809 |
// the greedy type Loop, Curly will skip the backtracking for any starting |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1810 |
// position "i" that failed in the past. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1811 |
if (!hasGroupRef) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1812 |
for (Node node : topClosureNodes) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1813 |
if (node instanceof Loop) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1814 |
// non-deterministic-greedy-group |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1815 |
((Loop)node).posIndex = localTCNCount++; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1816 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1817 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1818 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1819 |
|
2 | 1820 |
// Release temporary storage |
1821 |
temp = null; |
|
1822 |
buffer = null; |
|
1823 |
groupNodes = null; |
|
1824 |
patternLength = 0; |
|
1825 |
compiled = true; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
1826 |
topClosureNodes = null; |
2 | 1827 |
} |
1828 |
||
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1829 |
Map<String, Integer> namedGroups() { |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
34684
diff
changeset
|
1830 |
Map<String, Integer> groups = namedGroups; |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
34684
diff
changeset
|
1831 |
if (groups == null) { |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
34684
diff
changeset
|
1832 |
namedGroups = groups = new HashMap<>(2); |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
34684
diff
changeset
|
1833 |
} |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
34684
diff
changeset
|
1834 |
return groups; |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1835 |
} |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
1836 |
|
2 | 1837 |
/** |
1838 |
* Used to accumulate information about a subtree of the object graph |
|
1839 |
* so that optimizations can be applied to the subtree. |
|
1840 |
*/ |
|
1841 |
static final class TreeInfo { |
|
1842 |
int minLength; |
|
1843 |
int maxLength; |
|
1844 |
boolean maxValid; |
|
1845 |
boolean deterministic; |
|
1846 |
||
1847 |
TreeInfo() { |
|
1848 |
reset(); |
|
1849 |
} |
|
1850 |
void reset() { |
|
1851 |
minLength = 0; |
|
1852 |
maxLength = 0; |
|
1853 |
maxValid = true; |
|
1854 |
deterministic = true; |
|
1855 |
} |
|
1856 |
} |
|
1857 |
||
1858 |
/* |
|
1859 |
* The following private methods are mainly used to improve the |
|
1860 |
* readability of the code. In order to let the Java compiler easily |
|
1861 |
* inline them, we should not put many assertions or error checks in them. |
|
1862 |
*/ |
|
1863 |
||
1864 |
/** |
|
1865 |
* Indicates whether a particular flag is set or not. |
|
1866 |
*/ |
|
1867 |
private boolean has(int f) { |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
1868 |
return (flags0 & f) != 0; |
2 | 1869 |
} |
1870 |
||
1871 |
/** |
|
1872 |
* Match next character, signal error if failed. |
|
1873 |
*/ |
|
1874 |
private void accept(int ch, String s) { |
|
1875 |
int testChar = temp[cursor++]; |
|
1876 |
if (has(COMMENTS)) |
|
1877 |
testChar = parsePastWhitespace(testChar); |
|
1878 |
if (ch != testChar) { |
|
1879 |
throw error(s); |
|
1880 |
} |
|
1881 |
} |
|
1882 |
||
1883 |
/** |
|
1884 |
* Mark the end of pattern with a specific character. |
|
1885 |
*/ |
|
1886 |
private void mark(int c) { |
|
1887 |
temp[patternLength] = c; |
|
1888 |
} |
|
1889 |
||
1890 |
/** |
|
1891 |
* Peek the next character, and do not advance the cursor. |
|
1892 |
*/ |
|
1893 |
private int peek() { |
|
1894 |
int ch = temp[cursor]; |
|
1895 |
if (has(COMMENTS)) |
|
1896 |
ch = peekPastWhitespace(ch); |
|
1897 |
return ch; |
|
1898 |
} |
|
1899 |
||
1900 |
/** |
|
1901 |
* Read the next character, and advance the cursor by one. |
|
1902 |
*/ |
|
1903 |
private int read() { |
|
1904 |
int ch = temp[cursor++]; |
|
1905 |
if (has(COMMENTS)) |
|
1906 |
ch = parsePastWhitespace(ch); |
|
1907 |
return ch; |
|
1908 |
} |
|
1909 |
||
1910 |
/** |
|
1911 |
* Read the next character, and advance the cursor by one, |
|
1912 |
* ignoring the COMMENTS setting |
|
1913 |
*/ |
|
1914 |
private int readEscaped() { |
|
1915 |
int ch = temp[cursor++]; |
|
1916 |
return ch; |
|
1917 |
} |
|
1918 |
||
1919 |
/** |
|
1920 |
* Advance the cursor by one, and peek the next character. |
|
1921 |
*/ |
|
1922 |
private int next() { |
|
1923 |
int ch = temp[++cursor]; |
|
1924 |
if (has(COMMENTS)) |
|
1925 |
ch = peekPastWhitespace(ch); |
|
1926 |
return ch; |
|
1927 |
} |
|
1928 |
||
1929 |
/** |
|
1930 |
* Advance the cursor by one, and peek the next character, |
|
1931 |
* ignoring the COMMENTS setting |
|
1932 |
*/ |
|
1933 |
private int nextEscaped() { |
|
1934 |
int ch = temp[++cursor]; |
|
1935 |
return ch; |
|
1936 |
} |
|
1937 |
||
1938 |
/** |
|
1939 |
* If in xmode peek past whitespace and comments. |
|
1940 |
*/ |
|
1941 |
private int peekPastWhitespace(int ch) { |
|
1942 |
while (ASCII.isSpace(ch) || ch == '#') { |
|
1943 |
while (ASCII.isSpace(ch)) |
|
1944 |
ch = temp[++cursor]; |
|
1945 |
if (ch == '#') { |
|
1946 |
ch = peekPastLine(); |
|
1947 |
} |
|
1948 |
} |
|
1949 |
return ch; |
|
1950 |
} |
|
1951 |
||
1952 |
/** |
|
1953 |
* If in xmode parse past whitespace and comments. |
|
1954 |
*/ |
|
1955 |
private int parsePastWhitespace(int ch) { |
|
1956 |
while (ASCII.isSpace(ch) || ch == '#') { |
|
1957 |
while (ASCII.isSpace(ch)) |
|
1958 |
ch = temp[cursor++]; |
|
1959 |
if (ch == '#') |
|
1960 |
ch = parsePastLine(); |
|
1961 |
} |
|
1962 |
return ch; |
|
1963 |
} |
|
1964 |
||
1965 |
/** |
|
1966 |
* xmode parse past comment to end of line. |
|
1967 |
*/ |
|
1968 |
private int parsePastLine() { |
|
1969 |
int ch = temp[cursor++]; |
|
1970 |
while (ch != 0 && !isLineSeparator(ch)) |
|
1971 |
ch = temp[cursor++]; |
|
58623 | 1972 |
if (ch == 0 && cursor > patternLength) { |
1973 |
cursor = patternLength; |
|
1974 |
ch = temp[cursor++]; |
|
1975 |
} |
|
2 | 1976 |
return ch; |
1977 |
} |
|
1978 |
||
1979 |
/** |
|
1980 |
* xmode peek past comment to end of line. |
|
1981 |
*/ |
|
1982 |
private int peekPastLine() { |
|
1983 |
int ch = temp[++cursor]; |
|
1984 |
while (ch != 0 && !isLineSeparator(ch)) |
|
1985 |
ch = temp[++cursor]; |
|
58623 | 1986 |
if (ch == 0 && cursor > patternLength) { |
1987 |
cursor = patternLength; |
|
1988 |
ch = temp[cursor]; |
|
1989 |
} |
|
2 | 1990 |
return ch; |
1991 |
} |
|
1992 |
||
1993 |
/** |
|
1994 |
* Determines if character is a line separator in the current mode |
|
1995 |
*/ |
|
1996 |
private boolean isLineSeparator(int ch) { |
|
1997 |
if (has(UNIX_LINES)) { |
|
1998 |
return ch == '\n'; |
|
1999 |
} else { |
|
2000 |
return (ch == '\n' || |
|
2001 |
ch == '\r' || |
|
2002 |
(ch|1) == '\u2029' || |
|
2003 |
ch == '\u0085'); |
|
2004 |
} |
|
2005 |
} |
|
2006 |
||
2007 |
/** |
|
2008 |
* Read the character after the next one, and advance the cursor by two. |
|
2009 |
*/ |
|
2010 |
private int skip() { |
|
2011 |
int i = cursor; |
|
2012 |
int ch = temp[i+1]; |
|
2013 |
cursor = i + 2; |
|
2014 |
return ch; |
|
2015 |
} |
|
2016 |
||
2017 |
/** |
|
2018 |
* Unread one next character, and retreat cursor by one. |
|
2019 |
*/ |
|
2020 |
private void unread() { |
|
2021 |
cursor--; |
|
2022 |
} |
|
2023 |
||
2024 |
/** |
|
2025 |
* Internal method used for handling all syntax errors. The pattern is |
|
2026 |
* displayed with a pointer to aid in locating the syntax error. |
|
2027 |
*/ |
|
2028 |
private PatternSyntaxException error(String s) { |
|
2029 |
return new PatternSyntaxException(s, normalizedPattern, cursor - 1); |
|
2030 |
} |
|
2031 |
||
2032 |
/** |
|
2033 |
* Determines if there is any supplementary character or unpaired |
|
2034 |
* surrogate in the specified range. |
|
2035 |
*/ |
|
2036 |
private boolean findSupplementary(int start, int end) { |
|
2037 |
for (int i = start; i < end; i++) { |
|
2038 |
if (isSupplementary(temp[i])) |
|
2039 |
return true; |
|
2040 |
} |
|
2041 |
return false; |
|
2042 |
} |
|
2043 |
||
2044 |
/** |
|
2045 |
* Determines if the specified code point is a supplementary |
|
2046 |
* character or unpaired surrogate. |
|
2047 |
*/ |
|
2048 |
private static final boolean isSupplementary(int ch) { |
|
4820
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
2049 |
return ch >= Character.MIN_SUPPLEMENTARY_CODE_POINT || |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
2050 |
Character.isSurrogate((char)ch); |
2 | 2051 |
} |
2052 |
||
2053 |
/** |
|
2054 |
* The following methods handle the main parsing. They are sorted |
|
2055 |
* according to their precedence order, the lowest one first. |
|
2056 |
*/ |
|
2057 |
||
2058 |
/** |
|
2059 |
* The expression is parsed with branch nodes added for alternations. |
|
2060 |
* This may be called recursively to parse sub expressions that may |
|
2061 |
* contain alternations. |
|
2062 |
*/ |
|
2063 |
private Node expr(Node end) { |
|
2064 |
Node prev = null; |
|
2065 |
Node firstTail = null; |
|
13554
ad737436c54a
7189363: Regex Pattern compilation buggy for special sequences
sherman
parents:
13158
diff
changeset
|
2066 |
Branch branch = null; |
2 | 2067 |
Node branchConn = null; |
2068 |
||
2069 |
for (;;) { |
|
2070 |
Node node = sequence(end); |
|
2071 |
Node nodeTail = root; //double return |
|
2072 |
if (prev == null) { |
|
2073 |
prev = node; |
|
2074 |
firstTail = nodeTail; |
|
2075 |
} else { |
|
2076 |
// Branch |
|
2077 |
if (branchConn == null) { |
|
2078 |
branchConn = new BranchConn(); |
|
2079 |
branchConn.next = end; |
|
2080 |
} |
|
2081 |
if (node == end) { |
|
2082 |
// if the node returned from sequence() is "end" |
|
2083 |
// we have an empty expr, set a null atom into |
|
2084 |
// the branch to indicate to go "next" directly. |
|
2085 |
node = null; |
|
2086 |
} else { |
|
2087 |
// the "tail.next" of each atom goes to branchConn |
|
2088 |
nodeTail.next = branchConn; |
|
2089 |
} |
|
13554
ad737436c54a
7189363: Regex Pattern compilation buggy for special sequences
sherman
parents:
13158
diff
changeset
|
2090 |
if (prev == branch) { |
ad737436c54a
7189363: Regex Pattern compilation buggy for special sequences
sherman
parents:
13158
diff
changeset
|
2091 |
branch.add(node); |
2 | 2092 |
} else { |
2093 |
if (prev == end) { |
|
2094 |
prev = null; |
|
2095 |
} else { |
|
2096 |
// replace the "end" with "branchConn" at its tail.next |
|
2097 |
// when put the "prev" into the branch as the first atom. |
|
2098 |
firstTail.next = branchConn; |
|
2099 |
} |
|
13554
ad737436c54a
7189363: Regex Pattern compilation buggy for special sequences
sherman
parents:
13158
diff
changeset
|
2100 |
prev = branch = new Branch(prev, node, branchConn); |
2 | 2101 |
} |
2102 |
} |
|
2103 |
if (peek() != '|') { |
|
2104 |
return prev; |
|
2105 |
} |
|
2106 |
next(); |
|
2107 |
} |
|
2108 |
} |
|
2109 |
||
13158
f628cbfcbfa0
7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents:
12675
diff
changeset
|
2110 |
@SuppressWarnings("fallthrough") |
2 | 2111 |
/** |
2112 |
* Parsing of sequences between alternations. |
|
2113 |
*/ |
|
2114 |
private Node sequence(Node end) { |
|
2115 |
Node head = null; |
|
2116 |
Node tail = null; |
|
55220 | 2117 |
Node node; |
2 | 2118 |
LOOP: |
2119 |
for (;;) { |
|
2120 |
int ch = peek(); |
|
2121 |
switch (ch) { |
|
2122 |
case '(': |
|
2123 |
// Because group handles its own closure, |
|
2124 |
// we need to treat it differently |
|
2125 |
node = group0(); |
|
2126 |
// Check for comment or flag group |
|
2127 |
if (node == null) |
|
2128 |
continue; |
|
2129 |
if (head == null) |
|
2130 |
head = node; |
|
2131 |
else |
|
2132 |
tail.next = node; |
|
2133 |
// Double return: Tail was returned in root |
|
2134 |
tail = root; |
|
2135 |
continue; |
|
2136 |
case '[': |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2137 |
if (has(CANON_EQ) && !has(LITERAL)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2138 |
node = new NFCCharProperty(clazz(true)); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2139 |
else |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2140 |
node = newCharProperty(clazz(true)); |
2 | 2141 |
break; |
2142 |
case '\\': |
|
2143 |
ch = nextEscaped(); |
|
2144 |
if (ch == 'p' || ch == 'P') { |
|
2145 |
boolean oneLetter = true; |
|
2146 |
boolean comp = (ch == 'P'); |
|
2147 |
ch = next(); // Consume { if present |
|
2148 |
if (ch != '{') { |
|
2149 |
unread(); |
|
2150 |
} else { |
|
2151 |
oneLetter = false; |
|
2152 |
} |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2153 |
// node = newCharProperty(family(oneLetter, comp)); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2154 |
if (has(CANON_EQ) && !has(LITERAL)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2155 |
node = new NFCCharProperty(family(oneLetter, comp)); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2156 |
else |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2157 |
node = newCharProperty(family(oneLetter, comp)); |
2 | 2158 |
} else { |
2159 |
unread(); |
|
2160 |
node = atom(); |
|
2161 |
} |
|
2162 |
break; |
|
2163 |
case '^': |
|
2164 |
next(); |
|
2165 |
if (has(MULTILINE)) { |
|
2166 |
if (has(UNIX_LINES)) |
|
2167 |
node = new UnixCaret(); |
|
2168 |
else |
|
2169 |
node = new Caret(); |
|
2170 |
} else { |
|
2171 |
node = new Begin(); |
|
2172 |
} |
|
2173 |
break; |
|
2174 |
case '$': |
|
2175 |
next(); |
|
2176 |
if (has(UNIX_LINES)) |
|
2177 |
node = new UnixDollar(has(MULTILINE)); |
|
2178 |
else |
|
2179 |
node = new Dollar(has(MULTILINE)); |
|
2180 |
break; |
|
2181 |
case '.': |
|
2182 |
next(); |
|
2183 |
if (has(DOTALL)) { |
|
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2184 |
node = new CharProperty(ALL()); |
2 | 2185 |
} else { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2186 |
if (has(UNIX_LINES)) { |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2187 |
node = new CharProperty(UNIXDOT()); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2188 |
} else { |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2189 |
node = new CharProperty(DOT()); |
2 | 2190 |
} |
2191 |
} |
|
2192 |
break; |
|
2193 |
case '|': |
|
2194 |
case ')': |
|
2195 |
break LOOP; |
|
2196 |
case ']': // Now interpreting dangling ] and } as literals |
|
2197 |
case '}': |
|
2198 |
node = atom(); |
|
2199 |
break; |
|
2200 |
case '?': |
|
2201 |
case '*': |
|
2202 |
case '+': |
|
2203 |
next(); |
|
2204 |
throw error("Dangling meta character '" + ((char)ch) + "'"); |
|
2205 |
case 0: |
|
2206 |
if (cursor >= patternLength) { |
|
2207 |
break LOOP; |
|
2208 |
} |
|
2209 |
// Fall through |
|
2210 |
default: |
|
2211 |
node = atom(); |
|
2212 |
break; |
|
2213 |
} |
|
2214 |
||
2215 |
node = closure(node); |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2216 |
/* save the top dot-greedy nodes (.*, .+) as well |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2217 |
if (node instanceof GreedyCharProperty && |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2218 |
((GreedyCharProperty)node).cp instanceof Dot) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2219 |
topClosureNodes.add(node); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2220 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2221 |
*/ |
2 | 2222 |
if (head == null) { |
2223 |
head = tail = node; |
|
2224 |
} else { |
|
2225 |
tail.next = node; |
|
2226 |
tail = node; |
|
2227 |
} |
|
2228 |
} |
|
2229 |
if (head == null) { |
|
2230 |
return end; |
|
2231 |
} |
|
2232 |
tail.next = end; |
|
2233 |
root = tail; //double return |
|
2234 |
return head; |
|
2235 |
} |
|
2236 |
||
13158
f628cbfcbfa0
7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents:
12675
diff
changeset
|
2237 |
@SuppressWarnings("fallthrough") |
2 | 2238 |
/** |
2239 |
* Parse and add a new Single or Slice. |
|
2240 |
*/ |
|
2241 |
private Node atom() { |
|
2242 |
int first = 0; |
|
2243 |
int prev = -1; |
|
2244 |
boolean hasSupplementary = false; |
|
2245 |
int ch = peek(); |
|
2246 |
for (;;) { |
|
2247 |
switch (ch) { |
|
2248 |
case '*': |
|
2249 |
case '+': |
|
2250 |
case '?': |
|
2251 |
case '{': |
|
2252 |
if (first > 1) { |
|
2253 |
cursor = prev; // Unwind one character |
|
2254 |
first--; |
|
2255 |
} |
|
2256 |
break; |
|
2257 |
case '$': |
|
2258 |
case '.': |
|
2259 |
case '^': |
|
2260 |
case '(': |
|
2261 |
case '[': |
|
2262 |
case '|': |
|
2263 |
case ')': |
|
2264 |
break; |
|
2265 |
case '\\': |
|
2266 |
ch = nextEscaped(); |
|
2267 |
if (ch == 'p' || ch == 'P') { // Property |
|
2268 |
if (first > 0) { // Slice is waiting; handle it first |
|
2269 |
unread(); |
|
2270 |
break; |
|
2271 |
} else { // No slice; just return the family node |
|
2272 |
boolean comp = (ch == 'P'); |
|
2273 |
boolean oneLetter = true; |
|
2274 |
ch = next(); // Consume { if present |
|
2275 |
if (ch != '{') |
|
2276 |
unread(); |
|
2277 |
else |
|
2278 |
oneLetter = false; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2279 |
if (has(CANON_EQ) && !has(LITERAL)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2280 |
return new NFCCharProperty(family(oneLetter, comp)); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2281 |
else |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2282 |
return newCharProperty(family(oneLetter, comp)); |
2 | 2283 |
} |
2284 |
} |
|
2285 |
unread(); |
|
2286 |
prev = cursor; |
|
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2287 |
ch = escape(false, first == 0, false); |
2 | 2288 |
if (ch >= 0) { |
2289 |
append(ch, first); |
|
2290 |
first++; |
|
2291 |
if (isSupplementary(ch)) { |
|
2292 |
hasSupplementary = true; |
|
2293 |
} |
|
2294 |
ch = peek(); |
|
2295 |
continue; |
|
2296 |
} else if (first == 0) { |
|
2297 |
return root; |
|
2298 |
} |
|
2299 |
// Unwind meta escape sequence |
|
2300 |
cursor = prev; |
|
2301 |
break; |
|
2302 |
case 0: |
|
2303 |
if (cursor >= patternLength) { |
|
2304 |
break; |
|
2305 |
} |
|
2306 |
// Fall through |
|
2307 |
default: |
|
2308 |
prev = cursor; |
|
2309 |
append(ch, first); |
|
2310 |
first++; |
|
2311 |
if (isSupplementary(ch)) { |
|
2312 |
hasSupplementary = true; |
|
2313 |
} |
|
2314 |
ch = next(); |
|
2315 |
continue; |
|
2316 |
} |
|
2317 |
break; |
|
2318 |
} |
|
2319 |
if (first == 1) { |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2320 |
return newCharProperty(single(buffer[0])); |
2 | 2321 |
} else { |
2322 |
return newSlice(buffer, first, hasSupplementary); |
|
2323 |
} |
|
2324 |
} |
|
2325 |
||
54971 | 2326 |
private void append(int ch, int index) { |
2327 |
int len = buffer.length; |
|
2328 |
if (index - len >= 0) { |
|
2329 |
len = ArraysSupport.newLength(len, |
|
2330 |
1 + index - len, /* minimum growth */ |
|
2331 |
len /* preferred growth */); |
|
2332 |
buffer = Arrays.copyOf(buffer, len); |
|
2333 |
} |
|
2334 |
buffer[index] = ch; |
|
2 | 2335 |
} |
2336 |
||
2337 |
/** |
|
2338 |
* Parses a backref greedily, taking as many numbers as it |
|
2339 |
* can. The first digit is always treated as a backref, but |
|
2340 |
* multi digit numbers are only treated as a backref if at |
|
2341 |
* least that many backrefs exist at this point in the regex. |
|
2342 |
*/ |
|
2343 |
private Node ref(int refNum) { |
|
2344 |
boolean done = false; |
|
2345 |
while(!done) { |
|
2346 |
int ch = peek(); |
|
2347 |
switch(ch) { |
|
2348 |
case '0': |
|
2349 |
case '1': |
|
2350 |
case '2': |
|
2351 |
case '3': |
|
2352 |
case '4': |
|
2353 |
case '5': |
|
2354 |
case '6': |
|
2355 |
case '7': |
|
2356 |
case '8': |
|
2357 |
case '9': |
|
2358 |
int newRefNum = (refNum * 10) + (ch - '0'); |
|
2359 |
// Add another number if it doesn't make a group |
|
2360 |
// that doesn't exist |
|
2361 |
if (capturingGroupCount - 1 < newRefNum) { |
|
2362 |
done = true; |
|
2363 |
break; |
|
2364 |
} |
|
2365 |
refNum = newRefNum; |
|
2366 |
read(); |
|
2367 |
break; |
|
2368 |
default: |
|
2369 |
done = true; |
|
2370 |
break; |
|
2371 |
} |
|
2372 |
} |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2373 |
hasGroupRef = true; |
2 | 2374 |
if (has(CASE_INSENSITIVE)) |
2375 |
return new CIBackRef(refNum, has(UNICODE_CASE)); |
|
2376 |
else |
|
2377 |
return new BackRef(refNum); |
|
2378 |
} |
|
2379 |
||
2380 |
/** |
|
2381 |
* Parses an escape sequence to determine the actual value that needs |
|
2382 |
* to be matched. |
|
2383 |
* If -1 is returned and create was true a new object was added to the tree |
|
2384 |
* to handle the escape sequence. |
|
2385 |
* If the returned value is greater than zero, it is the value that |
|
2386 |
* matches the escape sequence. |
|
2387 |
*/ |
|
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2388 |
private int escape(boolean inclass, boolean create, boolean isrange) { |
2 | 2389 |
int ch = skip(); |
2390 |
switch (ch) { |
|
2391 |
case '0': |
|
2392 |
return o(); |
|
2393 |
case '1': |
|
2394 |
case '2': |
|
2395 |
case '3': |
|
2396 |
case '4': |
|
2397 |
case '5': |
|
2398 |
case '6': |
|
2399 |
case '7': |
|
2400 |
case '8': |
|
2401 |
case '9': |
|
2402 |
if (inclass) break; |
|
2403 |
if (create) { |
|
2404 |
root = ref((ch - '0')); |
|
2405 |
} |
|
2406 |
return -1; |
|
2407 |
case 'A': |
|
2408 |
if (inclass) break; |
|
2409 |
if (create) root = new Begin(); |
|
2410 |
return -1; |
|
2411 |
case 'B': |
|
2412 |
if (inclass) break; |
|
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
2413 |
if (create) root = new Bound(Bound.NONE, has(UNICODE_CHARACTER_CLASS)); |
2 | 2414 |
return -1; |
2415 |
case 'C': |
|
2416 |
break; |
|
2417 |
case 'D': |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2418 |
if (create) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2419 |
predicate = has(UNICODE_CHARACTER_CLASS) ? |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2420 |
CharPredicates.DIGIT() : CharPredicates.ASCII_DIGIT(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2421 |
predicate = predicate.negate(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2422 |
if (!inclass) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2423 |
root = newCharProperty(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2424 |
} |
2 | 2425 |
return -1; |
2426 |
case 'E': |
|
2427 |
case 'F': |
|
2428 |
break; |
|
2429 |
case 'G': |
|
2430 |
if (inclass) break; |
|
2431 |
if (create) root = new LastMatch(); |
|
2432 |
return -1; |
|
2433 |
case 'H': |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2434 |
if (create) { |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2435 |
predicate = HorizWS().negate(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2436 |
if (!inclass) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2437 |
root = newCharProperty(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2438 |
} |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2439 |
return -1; |
2 | 2440 |
case 'I': |
2441 |
case 'J': |
|
2442 |
case 'K': |
|
2443 |
case 'L': |
|
2444 |
case 'M': |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2445 |
break; |
2 | 2446 |
case 'N': |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2447 |
return N(); |
2 | 2448 |
case 'O': |
2449 |
case 'P': |
|
2450 |
case 'Q': |
|
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2451 |
break; |
2 | 2452 |
case 'R': |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2453 |
if (inclass) break; |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2454 |
if (create) root = new LineEnding(); |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2455 |
return -1; |
2 | 2456 |
case 'S': |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2457 |
if (create) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2458 |
predicate = has(UNICODE_CHARACTER_CLASS) ? |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2459 |
CharPredicates.WHITE_SPACE() : CharPredicates.ASCII_SPACE(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2460 |
predicate = predicate.negate(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2461 |
if (!inclass) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2462 |
root = newCharProperty(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2463 |
} |
2 | 2464 |
return -1; |
2465 |
case 'T': |
|
2466 |
case 'U': |
|
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2467 |
break; |
2 | 2468 |
case 'V': |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2469 |
if (create) { |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2470 |
predicate = VertWS().negate(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2471 |
if (!inclass) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2472 |
root = newCharProperty(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2473 |
} |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2474 |
return -1; |
2 | 2475 |
case 'W': |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2476 |
if (create) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2477 |
predicate = has(UNICODE_CHARACTER_CLASS) ? |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2478 |
CharPredicates.WORD() : CharPredicates.ASCII_WORD(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2479 |
predicate = predicate.negate(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2480 |
if (!inclass) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2481 |
root = newCharProperty(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2482 |
} |
2 | 2483 |
return -1; |
2484 |
case 'X': |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2485 |
if (inclass) break; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2486 |
if (create) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2487 |
root = new XGrapheme(); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2488 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2489 |
return -1; |
2 | 2490 |
case 'Y': |
2491 |
break; |
|
2492 |
case 'Z': |
|
2493 |
if (inclass) break; |
|
2494 |
if (create) { |
|
2495 |
if (has(UNIX_LINES)) |
|
2496 |
root = new UnixDollar(false); |
|
2497 |
else |
|
2498 |
root = new Dollar(false); |
|
2499 |
} |
|
2500 |
return -1; |
|
2501 |
case 'a': |
|
2502 |
return '\007'; |
|
2503 |
case 'b': |
|
2504 |
if (inclass) break; |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2505 |
if (create) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2506 |
if (peek() == '{') { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2507 |
if (skip() == 'g') { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2508 |
if (read() == '}') { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2509 |
root = new GraphemeBound(); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2510 |
return -1; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2511 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2512 |
break; // error missing trailing } |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2513 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2514 |
unread(); unread(); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2515 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2516 |
root = new Bound(Bound.BOTH, has(UNICODE_CHARACTER_CLASS)); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
2517 |
} |
2 | 2518 |
return -1; |
2519 |
case 'c': |
|
2520 |
return c(); |
|
2521 |
case 'd': |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2522 |
if (create) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2523 |
predicate = has(UNICODE_CHARACTER_CLASS) ? |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2524 |
CharPredicates.DIGIT() : CharPredicates.ASCII_DIGIT(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2525 |
if (!inclass) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2526 |
root = newCharProperty(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2527 |
} |
2 | 2528 |
return -1; |
2529 |
case 'e': |
|
2530 |
return '\033'; |
|
2531 |
case 'f': |
|
2532 |
return '\f'; |
|
2533 |
case 'g': |
|
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2534 |
break; |
2 | 2535 |
case 'h': |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2536 |
if (create) { |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2537 |
predicate = HorizWS(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2538 |
if (!inclass) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2539 |
root = newCharProperty(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2540 |
} |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2541 |
return -1; |
2 | 2542 |
case 'i': |
2543 |
case 'j': |
|
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2544 |
break; |
2 | 2545 |
case 'k': |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2546 |
if (inclass) |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2547 |
break; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2548 |
if (read() != '<') |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2549 |
throw error("\\k is not followed by '<' for named capturing group"); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2550 |
String name = groupname(read()); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2551 |
if (!namedGroups().containsKey(name)) |
48853
84b4ffbba8b0
8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents:
48491
diff
changeset
|
2552 |
throw error("named capturing group <" + name + "> does not exist"); |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2553 |
if (create) { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2554 |
hasGroupRef = true; |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2555 |
if (has(CASE_INSENSITIVE)) |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2556 |
root = new CIBackRef(namedGroups().get(name), has(UNICODE_CASE)); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2557 |
else |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2558 |
root = new BackRef(namedGroups().get(name)); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2559 |
} |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2560 |
return -1; |
2 | 2561 |
case 'l': |
2562 |
case 'm': |
|
2563 |
break; |
|
2564 |
case 'n': |
|
2565 |
return '\n'; |
|
2566 |
case 'o': |
|
2567 |
case 'p': |
|
2568 |
case 'q': |
|
2569 |
break; |
|
2570 |
case 'r': |
|
2571 |
return '\r'; |
|
2572 |
case 's': |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2573 |
if (create) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2574 |
predicate = has(UNICODE_CHARACTER_CLASS) ? |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2575 |
CharPredicates.WHITE_SPACE() : CharPredicates.ASCII_SPACE(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2576 |
if (!inclass) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2577 |
root = newCharProperty(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2578 |
} |
2 | 2579 |
return -1; |
2580 |
case 't': |
|
2581 |
return '\t'; |
|
2582 |
case 'u': |
|
2583 |
return u(); |
|
2584 |
case 'v': |
|
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2585 |
// '\v' was implemented as VT/0x0B in releases < 1.8 (though |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2586 |
// undocumented). In JDK8 '\v' is specified as a predefined |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2587 |
// character class for all vertical whitespace characters. |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2588 |
// So [-1, root=VertWS node] pair is returned (instead of a |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2589 |
// single 0x0B). This breaks the range if '\v' is used as |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2590 |
// the start or end value, such as [\v-...] or [...-\v], in |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2591 |
// which a single definite value (0x0B) is expected. For |
21278 | 2592 |
// compatibility concern '\013'/0x0B is returned if isrange. |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2593 |
if (isrange) |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2594 |
return '\013'; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2595 |
if (create) { |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2596 |
predicate = VertWS(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2597 |
if (!inclass) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2598 |
root = newCharProperty(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2599 |
} |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2600 |
return -1; |
2 | 2601 |
case 'w': |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2602 |
if (create) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2603 |
predicate = has(UNICODE_CHARACTER_CLASS) ? |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
2604 |
CharPredicates.WORD() : CharPredicates.ASCII_WORD(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2605 |
if (!inclass) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2606 |
root = newCharProperty(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2607 |
} |
2 | 2608 |
return -1; |
2609 |
case 'x': |
|
2610 |
return x(); |
|
2611 |
case 'y': |
|
2612 |
break; |
|
2613 |
case 'z': |
|
2614 |
if (inclass) break; |
|
2615 |
if (create) root = new End(); |
|
2616 |
return -1; |
|
2617 |
default: |
|
2618 |
return ch; |
|
2619 |
} |
|
2620 |
throw error("Illegal/unsupported escape sequence"); |
|
2621 |
} |
|
2622 |
||
2623 |
/** |
|
2624 |
* Parse a character class, and return the node that matches it. |
|
2625 |
* |
|
2626 |
* Consumes a ] on the way out if consume is true. Usually consume |
|
2627 |
* is true except for the case of [abc&&def] where def is a separate |
|
2628 |
* right hand node with "understood" brackets. |
|
2629 |
*/ |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2630 |
private CharPredicate clazz(boolean consume) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2631 |
CharPredicate prev = null; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2632 |
CharPredicate curr = null; |
2 | 2633 |
BitClass bits = new BitClass(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2634 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2635 |
boolean isNeg = false; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2636 |
boolean hasBits = false; |
2 | 2637 |
int ch = next(); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2638 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2639 |
// Negates if first char in a class, otherwise literal |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2640 |
if (ch == '^' && temp[cursor-1] == '[') { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2641 |
ch = next(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2642 |
isNeg = true; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2643 |
} |
2 | 2644 |
for (;;) { |
2645 |
switch (ch) { |
|
2646 |
case '[': |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2647 |
curr = clazz(true); |
2 | 2648 |
if (prev == null) |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2649 |
prev = curr; |
2 | 2650 |
else |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2651 |
prev = prev.union(curr); |
2 | 2652 |
ch = peek(); |
2653 |
continue; |
|
2654 |
case '&': |
|
2655 |
ch = next(); |
|
2656 |
if (ch == '&') { |
|
2657 |
ch = next(); |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2658 |
CharPredicate right = null; |
2 | 2659 |
while (ch != ']' && ch != '&') { |
2660 |
if (ch == '[') { |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2661 |
if (right == null) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2662 |
right = clazz(true); |
2 | 2663 |
else |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2664 |
right = right.union(clazz(true)); |
2 | 2665 |
} else { // abc&&def |
2666 |
unread(); |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2667 |
right = clazz(false); |
2 | 2668 |
} |
2669 |
ch = peek(); |
|
2670 |
} |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2671 |
if (hasBits) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2672 |
// bits used, union has high precedence |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2673 |
if (prev == null) { |
55220 | 2674 |
prev = curr = bits; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2675 |
} else { |
55220 | 2676 |
prev = prev.union(bits); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2677 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2678 |
hasBits = false; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2679 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2680 |
if (right != null) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2681 |
curr = right; |
2 | 2682 |
if (prev == null) { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2683 |
if (right == null) |
2 | 2684 |
throw error("Bad class syntax"); |
2685 |
else |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2686 |
prev = right; |
2 | 2687 |
} else { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2688 |
prev = prev.and(curr); |
2 | 2689 |
} |
2690 |
} else { |
|
2691 |
// treat as a literal & |
|
2692 |
unread(); |
|
2693 |
break; |
|
2694 |
} |
|
2695 |
continue; |
|
2696 |
case 0: |
|
2697 |
if (cursor >= patternLength) |
|
2698 |
throw error("Unclosed character class"); |
|
2699 |
break; |
|
2700 |
case ']': |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2701 |
if (prev != null || hasBits) { |
2 | 2702 |
if (consume) |
2703 |
next(); |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2704 |
if (prev == null) |
55220 | 2705 |
prev = bits; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2706 |
else if (hasBits) |
55220 | 2707 |
prev = prev.union(bits); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2708 |
if (isNeg) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2709 |
return prev.negate(); |
2 | 2710 |
return prev; |
2711 |
} |
|
2712 |
break; |
|
2713 |
default: |
|
2714 |
break; |
|
2715 |
} |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2716 |
curr = range(bits); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2717 |
if (curr == null) { // the bits used |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2718 |
hasBits = true; |
2 | 2719 |
} else { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2720 |
if (prev == null) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2721 |
prev = curr; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2722 |
else if (prev != curr) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2723 |
prev = prev.union(curr); |
2 | 2724 |
} |
2725 |
ch = peek(); |
|
2726 |
} |
|
2727 |
} |
|
2728 |
||
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2729 |
private CharPredicate bitsOrSingle(BitClass bits, int ch) { |
2 | 2730 |
/* Bits can only handle codepoints in [u+0000-u+00ff] range. |
2731 |
Use "single" node instead of bits when dealing with unicode |
|
2732 |
case folding for codepoints listed below. |
|
2733 |
(1)Uppercase out of range: u+00ff, u+00b5 |
|
2734 |
toUpperCase(u+00ff) -> u+0178 |
|
2735 |
toUpperCase(u+00b5) -> u+039c |
|
2736 |
(2)LatinSmallLetterLongS u+17f |
|
2737 |
toUpperCase(u+017f) -> u+0053 |
|
2738 |
(3)LatinSmallLetterDotlessI u+131 |
|
2739 |
toUpperCase(u+0131) -> u+0049 |
|
2740 |
(4)LatinCapitalLetterIWithDotAbove u+0130 |
|
2741 |
toLowerCase(u+0130) -> u+0069 |
|
2742 |
(5)KelvinSign u+212a |
|
2743 |
toLowerCase(u+212a) ==> u+006B |
|
2744 |
(6)AngstromSign u+212b |
|
2745 |
toLowerCase(u+212b) ==> u+00e5 |
|
2746 |
*/ |
|
2747 |
if (ch < 256 && |
|
2748 |
!(has(CASE_INSENSITIVE) && has(UNICODE_CASE) && |
|
2749 |
(ch == 0xff || ch == 0xb5 || |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2750 |
ch == 0x49 || ch == 0x69 || //I and i |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2751 |
ch == 0x53 || ch == 0x73 || //S and s |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2752 |
ch == 0x4b || ch == 0x6b || //K and k |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2753 |
ch == 0xc5 || ch == 0xe5))) { //A+ring |
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
2754 |
bits.add(ch, flags0); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2755 |
return null; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2756 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2757 |
return single(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2758 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2759 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2760 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2761 |
* Returns a suitably optimized, single character predicate |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2762 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2763 |
private CharPredicate single(final int ch) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2764 |
if (has(CASE_INSENSITIVE)) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2765 |
int lower, upper; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2766 |
if (has(UNICODE_CASE)) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2767 |
upper = Character.toUpperCase(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2768 |
lower = Character.toLowerCase(upper); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2769 |
// Unicode case insensitive matches |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2770 |
if (upper != lower) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2771 |
return SingleU(lower); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2772 |
} else if (ASCII.isAscii(ch)) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2773 |
lower = ASCII.toLower(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2774 |
upper = ASCII.toUpper(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2775 |
// Case insensitive matches a given BMP character |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2776 |
if (lower != upper) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2777 |
return SingleI(lower, upper); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2778 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2779 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2780 |
if (isSupplementary(ch)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2781 |
return SingleS(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2782 |
return Single(ch); // Match a given BMP character |
2 | 2783 |
} |
2784 |
||
2785 |
/** |
|
2786 |
* Parse a single character or a character range in a character class |
|
2787 |
* and return its representative node. |
|
2788 |
*/ |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2789 |
private CharPredicate range(BitClass bits) { |
2 | 2790 |
int ch = peek(); |
2791 |
if (ch == '\\') { |
|
2792 |
ch = nextEscaped(); |
|
2793 |
if (ch == 'p' || ch == 'P') { // A property |
|
2794 |
boolean comp = (ch == 'P'); |
|
2795 |
boolean oneLetter = true; |
|
2796 |
// Consume { if present |
|
2797 |
ch = next(); |
|
2798 |
if (ch != '{') |
|
2799 |
unread(); |
|
2800 |
else |
|
2801 |
oneLetter = false; |
|
4820
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
2802 |
return family(oneLetter, comp); |
2 | 2803 |
} else { // ordinary escape |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2804 |
boolean isrange = temp[cursor+1] == '-'; |
2 | 2805 |
unread(); |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2806 |
ch = escape(true, true, isrange); |
2 | 2807 |
if (ch == -1) |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2808 |
return predicate; |
2 | 2809 |
} |
2810 |
} else { |
|
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2811 |
next(); |
2 | 2812 |
} |
2813 |
if (ch >= 0) { |
|
2814 |
if (peek() == '-') { |
|
2815 |
int endRange = temp[cursor+1]; |
|
2816 |
if (endRange == '[') { |
|
2817 |
return bitsOrSingle(bits, ch); |
|
2818 |
} |
|
2819 |
if (endRange != ']') { |
|
2820 |
next(); |
|
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2821 |
int m = peek(); |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2822 |
if (m == '\\') { |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2823 |
m = escape(true, false, true); |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2824 |
} else { |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2825 |
next(); |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2826 |
} |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2827 |
if (m < ch) { |
2 | 2828 |
throw error("Illegal character range"); |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
2829 |
} |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2830 |
if (has(CASE_INSENSITIVE)) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2831 |
if (has(UNICODE_CASE)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2832 |
return CIRangeU(ch, m); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2833 |
return CIRange(ch, m); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2834 |
} else { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2835 |
return Range(ch, m); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2836 |
} |
2 | 2837 |
} |
2838 |
} |
|
2839 |
return bitsOrSingle(bits, ch); |
|
2840 |
} |
|
2841 |
throw error("Unexpected character '"+((char)ch)+"'"); |
|
2842 |
} |
|
2843 |
||
2844 |
/** |
|
2845 |
* Parses a Unicode character family and returns its representative node. |
|
2846 |
*/ |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2847 |
private CharPredicate family(boolean singleLetter, boolean isComplement) { |
2 | 2848 |
next(); |
2849 |
String name; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2850 |
CharPredicate p = null; |
2 | 2851 |
|
2852 |
if (singleLetter) { |
|
2853 |
int c = temp[cursor]; |
|
2854 |
if (!Character.isSupplementaryCodePoint(c)) { |
|
2855 |
name = String.valueOf((char)c); |
|
2856 |
} else { |
|
2857 |
name = new String(temp, cursor, 1); |
|
2858 |
} |
|
2859 |
read(); |
|
2860 |
} else { |
|
2861 |
int i = cursor; |
|
2862 |
mark('}'); |
|
2863 |
while(read() != '}') { |
|
2864 |
} |
|
2865 |
mark('\000'); |
|
2866 |
int j = cursor; |
|
2867 |
if (j > patternLength) |
|
2868 |
throw error("Unclosed character family"); |
|
2869 |
if (i + 1 >= j) |
|
2870 |
throw error("Empty character family"); |
|
2871 |
name = new String(temp, i, j-i-1); |
|
2872 |
} |
|
2873 |
||
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
2874 |
int i = name.indexOf('='); |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
2875 |
if (i != -1) { |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
2876 |
// property construct \p{name=value} |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
2877 |
String value = name.substring(i + 1); |
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
2878 |
name = name.substring(0, i).toLowerCase(Locale.ENGLISH); |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2879 |
switch (name) { |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2880 |
case "sc": |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2881 |
case "script": |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2882 |
p = CharPredicates.forUnicodeScript(value); |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2883 |
break; |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2884 |
case "blk": |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2885 |
case "block": |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2886 |
p = CharPredicates.forUnicodeBlock(value); |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2887 |
break; |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2888 |
case "gc": |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2889 |
case "general_category": |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2890 |
p = CharPredicates.forProperty(value); |
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2891 |
break; |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
2892 |
default: |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2893 |
break; |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
2894 |
} |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2895 |
if (p == null) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2896 |
throw error("Unknown Unicode property {name=<" + name + ">, " |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2897 |
+ "value=<" + value + ">}"); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2898 |
|
2 | 2899 |
} else { |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
2900 |
if (name.startsWith("In")) { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2901 |
// \p{InBlockName} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2902 |
p = CharPredicates.forUnicodeBlock(name.substring(2)); |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
2903 |
} else if (name.startsWith("Is")) { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2904 |
// \p{IsGeneralCategory} and \p{IsScriptName} |
57941
a1a8f8fae7d9
8230338: Accurate error message about bad Unicode block name
igerasim
parents:
57624
diff
changeset
|
2905 |
String shortName = name.substring(2); |
a1a8f8fae7d9
8230338: Accurate error message about bad Unicode block name
igerasim
parents:
57624
diff
changeset
|
2906 |
p = CharPredicates.forUnicodeProperty(shortName); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2907 |
if (p == null) |
57941
a1a8f8fae7d9
8230338: Accurate error message about bad Unicode block name
igerasim
parents:
57624
diff
changeset
|
2908 |
p = CharPredicates.forProperty(shortName); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2909 |
if (p == null) |
57941
a1a8f8fae7d9
8230338: Accurate error message about bad Unicode block name
igerasim
parents:
57624
diff
changeset
|
2910 |
p = CharPredicates.forUnicodeScript(shortName); |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
2911 |
} else { |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
2912 |
if (has(UNICODE_CHARACTER_CLASS)) { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2913 |
p = CharPredicates.forPOSIXName(name); |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
2914 |
} |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2915 |
if (p == null) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2916 |
p = CharPredicates.forProperty(name); |
5610
fd2427610c7f
6945564: Unicode script support in Character class
sherman
parents:
4820
diff
changeset
|
2917 |
} |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2918 |
if (p == null) |
57941
a1a8f8fae7d9
8230338: Accurate error message about bad Unicode block name
igerasim
parents:
57624
diff
changeset
|
2919 |
throw error("Unknown character property name {" + name + "}"); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2920 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2921 |
if (isComplement) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2922 |
// it might be too expensive to detect if a complement of |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2923 |
// CharProperty can match "certain" supplementary. So just |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2924 |
// go with StartS. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2925 |
hasSupplementary = true; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2926 |
p = p.negate(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2927 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2928 |
return p; |
2 | 2929 |
} |
2930 |
||
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2931 |
private CharProperty newCharProperty(CharPredicate p) { |
2 | 2932 |
if (p == null) |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2933 |
return null; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2934 |
if (p instanceof BmpCharPredicate) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2935 |
return new BmpCharProperty((BmpCharPredicate)p); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2936 |
else |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2937 |
return new CharProperty(p); |
2 | 2938 |
} |
2939 |
||
2940 |
/** |
|
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2941 |
* Parses and returns the name of a "named capturing group", the trailing |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2942 |
* ">" is consumed after parsing. |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2943 |
*/ |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2944 |
private String groupname(int ch) { |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2945 |
StringBuilder sb = new StringBuilder(); |
48853
84b4ffbba8b0
8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents:
48491
diff
changeset
|
2946 |
if (!ASCII.isAlpha(ch)) |
84b4ffbba8b0
8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents:
48491
diff
changeset
|
2947 |
throw error("capturing group name does not start with a Latin letter"); |
84b4ffbba8b0
8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents:
48491
diff
changeset
|
2948 |
do { |
84b4ffbba8b0
8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents:
48491
diff
changeset
|
2949 |
sb.append((char) ch); |
84b4ffbba8b0
8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents:
48491
diff
changeset
|
2950 |
} while (ASCII.isAlnum(ch=read())); |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2951 |
if (ch != '>') |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2952 |
throw error("named capturing group is missing trailing '>'"); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2953 |
return sb.toString(); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2954 |
} |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2955 |
|
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2956 |
/** |
2 | 2957 |
* Parses a group and returns the head node of a set of nodes that process |
2958 |
* the group. Sometimes a double return system is used where the tail is |
|
2959 |
* returned in root. |
|
2960 |
*/ |
|
2961 |
private Node group0() { |
|
2962 |
boolean capturingGroup = false; |
|
55220 | 2963 |
Node head; |
2964 |
Node tail; |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
2965 |
int save = flags0; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2966 |
int saveTCNCount = topClosureNodes.size(); |
2 | 2967 |
root = null; |
2968 |
int ch = next(); |
|
2969 |
if (ch == '?') { |
|
2970 |
ch = skip(); |
|
2971 |
switch (ch) { |
|
2972 |
case ':': // (?:xxx) pure group |
|
2973 |
head = createGroup(true); |
|
2974 |
tail = root; |
|
2975 |
head.next = expr(tail); |
|
2976 |
break; |
|
2977 |
case '=': // (?=xxx) and (?!xxx) lookahead |
|
2978 |
case '!': |
|
2979 |
head = createGroup(true); |
|
2980 |
tail = root; |
|
2981 |
head.next = expr(tail); |
|
2982 |
if (ch == '=') { |
|
2983 |
head = tail = new Pos(head); |
|
2984 |
} else { |
|
2985 |
head = tail = new Neg(head); |
|
2986 |
} |
|
2987 |
break; |
|
2988 |
case '>': // (?>xxx) independent group |
|
2989 |
head = createGroup(true); |
|
2990 |
tail = root; |
|
2991 |
head.next = expr(tail); |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
2992 |
head = tail = new Ques(head, Qtype.INDEPENDENT); |
2 | 2993 |
break; |
2994 |
case '<': // (?<xxx) look behind |
|
2995 |
ch = read(); |
|
48853
84b4ffbba8b0
8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents:
48491
diff
changeset
|
2996 |
if (ch != '=' && ch != '!') { |
2290
3a3bde061968
6817475: named-capturing group name started with digit causes PSE exception
sherman
parents:
2070
diff
changeset
|
2997 |
// named captured group |
2070
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2998 |
String name = groupname(ch); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
2999 |
if (namedGroups().containsKey(name)) |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
3000 |
throw error("Named capturing group <" + name |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
3001 |
+ "> is already defined"); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
3002 |
capturingGroup = true; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
3003 |
head = createGroup(false); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
3004 |
tail = root; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
3005 |
namedGroups().put(name, capturingGroupCount-1); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
3006 |
head.next = expr(tail); |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
3007 |
break; |
6e9972fbd965
6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents:
715
diff
changeset
|
3008 |
} |
2 | 3009 |
int start = cursor; |
3010 |
head = createGroup(true); |
|
3011 |
tail = root; |
|
3012 |
head.next = expr(tail); |
|
55220 | 3013 |
tail.next = LookBehindEndNode.INSTANCE; |
2 | 3014 |
TreeInfo info = new TreeInfo(); |
3015 |
head.study(info); |
|
3016 |
if (info.maxValid == false) { |
|
3017 |
throw error("Look-behind group does not have " |
|
3018 |
+ "an obvious maximum length"); |
|
3019 |
} |
|
3020 |
boolean hasSupplementary = findSupplementary(start, patternLength); |
|
3021 |
if (ch == '=') { |
|
3022 |
head = tail = (hasSupplementary ? |
|
3023 |
new BehindS(head, info.maxLength, |
|
3024 |
info.minLength) : |
|
3025 |
new Behind(head, info.maxLength, |
|
3026 |
info.minLength)); |
|
48853
84b4ffbba8b0
8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents:
48491
diff
changeset
|
3027 |
} else { // if (ch == '!') |
2 | 3028 |
head = tail = (hasSupplementary ? |
3029 |
new NotBehindS(head, info.maxLength, |
|
3030 |
info.minLength) : |
|
3031 |
new NotBehind(head, info.maxLength, |
|
3032 |
info.minLength)); |
|
3033 |
} |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3034 |
// clear all top-closure-nodes inside lookbehind |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3035 |
if (saveTCNCount < topClosureNodes.size()) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3036 |
topClosureNodes.subList(saveTCNCount, topClosureNodes.size()).clear(); |
2 | 3037 |
break; |
3038 |
case '$': |
|
3039 |
case '@': |
|
3040 |
throw error("Unknown group type"); |
|
3041 |
default: // (?xxx:) inlined match flags |
|
3042 |
unread(); |
|
3043 |
addFlag(); |
|
3044 |
ch = read(); |
|
3045 |
if (ch == ')') { |
|
3046 |
return null; // Inline modifier only |
|
3047 |
} |
|
3048 |
if (ch != ':') { |
|
3049 |
throw error("Unknown inline modifier"); |
|
3050 |
} |
|
3051 |
head = createGroup(true); |
|
3052 |
tail = root; |
|
3053 |
head.next = expr(tail); |
|
3054 |
break; |
|
3055 |
} |
|
3056 |
} else { // (xxx) a regular group |
|
3057 |
capturingGroup = true; |
|
3058 |
head = createGroup(false); |
|
3059 |
tail = root; |
|
3060 |
head.next = expr(tail); |
|
3061 |
} |
|
3062 |
||
3063 |
accept(')', "Unclosed group"); |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3064 |
flags0 = save; |
2 | 3065 |
|
3066 |
// Check for quantifiers |
|
3067 |
Node node = closure(head); |
|
3068 |
if (node == head) { // No closure |
|
3069 |
root = tail; |
|
3070 |
return node; // Dual return |
|
3071 |
} |
|
3072 |
if (head == tail) { // Zero length assertion |
|
3073 |
root = node; |
|
3074 |
return node; // Dual return |
|
3075 |
} |
|
3076 |
||
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3077 |
// have group closure, clear all inner closure nodes from the |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3078 |
// top list (no backtracking stopper optimization for inner |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3079 |
if (saveTCNCount < topClosureNodes.size()) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3080 |
topClosureNodes.subList(saveTCNCount, topClosureNodes.size()).clear(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3081 |
|
2 | 3082 |
if (node instanceof Ques) { |
3083 |
Ques ques = (Ques) node; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3084 |
if (ques.type == Qtype.POSSESSIVE) { |
2 | 3085 |
root = node; |
3086 |
return node; |
|
3087 |
} |
|
3088 |
tail.next = new BranchConn(); |
|
3089 |
tail = tail.next; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3090 |
if (ques.type == Qtype.GREEDY) { |
2 | 3091 |
head = new Branch(head, null, tail); |
3092 |
} else { // Reluctant quantifier |
|
3093 |
head = new Branch(null, head, tail); |
|
3094 |
} |
|
3095 |
root = tail; |
|
3096 |
return head; |
|
3097 |
} else if (node instanceof Curly) { |
|
3098 |
Curly curly = (Curly) node; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3099 |
if (curly.type == Qtype.POSSESSIVE) { |
2 | 3100 |
root = node; |
3101 |
return node; |
|
3102 |
} |
|
3103 |
// Discover if the group is deterministic |
|
3104 |
TreeInfo info = new TreeInfo(); |
|
3105 |
if (head.study(info)) { // Deterministic |
|
3106 |
GroupTail temp = (GroupTail) tail; |
|
3107 |
head = root = new GroupCurly(head.next, curly.cmin, |
|
3108 |
curly.cmax, curly.type, |
|
3109 |
((GroupTail)tail).localIndex, |
|
3110 |
((GroupTail)tail).groupIndex, |
|
3111 |
capturingGroup); |
|
3112 |
return head; |
|
3113 |
} else { // Non-deterministic |
|
3114 |
int temp = ((GroupHead) head).localIndex; |
|
3115 |
Loop loop; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3116 |
if (curly.type == Qtype.GREEDY) { |
2 | 3117 |
loop = new Loop(this.localCount, temp); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3118 |
// add the max_reps greedy to the top-closure-node list |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3119 |
if (curly.cmax == MAX_REPS) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3120 |
topClosureNodes.add(loop); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3121 |
} else { // Reluctant Curly |
2 | 3122 |
loop = new LazyLoop(this.localCount, temp); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3123 |
} |
2 | 3124 |
Prolog prolog = new Prolog(loop); |
3125 |
this.localCount += 1; |
|
3126 |
loop.cmin = curly.cmin; |
|
3127 |
loop.cmax = curly.cmax; |
|
3128 |
loop.body = head; |
|
3129 |
tail.next = loop; |
|
3130 |
root = loop; |
|
3131 |
return prolog; // Dual return |
|
3132 |
} |
|
3133 |
} |
|
3134 |
throw error("Internal logic error"); |
|
3135 |
} |
|
3136 |
||
3137 |
/** |
|
3138 |
* Create group head and tail nodes using double return. If the group is |
|
3139 |
* created with anonymous true then it is a pure group and should not |
|
3140 |
* affect group counting. |
|
3141 |
*/ |
|
3142 |
private Node createGroup(boolean anonymous) { |
|
3143 |
int localIndex = localCount++; |
|
3144 |
int groupIndex = 0; |
|
3145 |
if (!anonymous) |
|
3146 |
groupIndex = capturingGroupCount++; |
|
3147 |
GroupHead head = new GroupHead(localIndex); |
|
3148 |
root = new GroupTail(localIndex, groupIndex); |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3149 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3150 |
// for debug/print only, head.match does NOT need the "tail" info |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3151 |
head.tail = (GroupTail)root; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3152 |
|
2 | 3153 |
if (!anonymous && groupIndex < 10) |
3154 |
groupNodes[groupIndex] = head; |
|
3155 |
return head; |
|
3156 |
} |
|
3157 |
||
13158
f628cbfcbfa0
7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents:
12675
diff
changeset
|
3158 |
@SuppressWarnings("fallthrough") |
2 | 3159 |
/** |
3160 |
* Parses inlined match flags and set them appropriately. |
|
3161 |
*/ |
|
3162 |
private void addFlag() { |
|
3163 |
int ch = peek(); |
|
3164 |
for (;;) { |
|
3165 |
switch (ch) { |
|
3166 |
case 'i': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3167 |
flags0 |= CASE_INSENSITIVE; |
2 | 3168 |
break; |
3169 |
case 'm': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3170 |
flags0 |= MULTILINE; |
2 | 3171 |
break; |
3172 |
case 's': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3173 |
flags0 |= DOTALL; |
2 | 3174 |
break; |
3175 |
case 'd': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3176 |
flags0 |= UNIX_LINES; |
2 | 3177 |
break; |
3178 |
case 'u': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3179 |
flags0 |= UNICODE_CASE; |
2 | 3180 |
break; |
3181 |
case 'c': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3182 |
flags0 |= CANON_EQ; |
2 | 3183 |
break; |
3184 |
case 'x': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3185 |
flags0 |= COMMENTS; |
2 | 3186 |
break; |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
3187 |
case 'U': |
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3188 |
flags0 |= (UNICODE_CHARACTER_CLASS | UNICODE_CASE); |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
3189 |
break; |
2 | 3190 |
case '-': // subFlag then fall through |
3191 |
ch = next(); |
|
3192 |
subFlag(); |
|
3193 |
default: |
|
3194 |
return; |
|
3195 |
} |
|
3196 |
ch = next(); |
|
3197 |
} |
|
3198 |
} |
|
3199 |
||
13158
f628cbfcbfa0
7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents:
12675
diff
changeset
|
3200 |
@SuppressWarnings("fallthrough") |
2 | 3201 |
/** |
3202 |
* Parses the second part of inlined match flags and turns off |
|
3203 |
* flags appropriately. |
|
3204 |
*/ |
|
3205 |
private void subFlag() { |
|
3206 |
int ch = peek(); |
|
3207 |
for (;;) { |
|
3208 |
switch (ch) { |
|
3209 |
case 'i': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3210 |
flags0 &= ~CASE_INSENSITIVE; |
2 | 3211 |
break; |
3212 |
case 'm': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3213 |
flags0 &= ~MULTILINE; |
2 | 3214 |
break; |
3215 |
case 's': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3216 |
flags0 &= ~DOTALL; |
2 | 3217 |
break; |
3218 |
case 'd': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3219 |
flags0 &= ~UNIX_LINES; |
2 | 3220 |
break; |
3221 |
case 'u': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3222 |
flags0 &= ~UNICODE_CASE; |
2 | 3223 |
break; |
3224 |
case 'c': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3225 |
flags0 &= ~CANON_EQ; |
2 | 3226 |
break; |
3227 |
case 'x': |
|
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3228 |
flags0 &= ~COMMENTS; |
2 | 3229 |
break; |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
3230 |
case 'U': |
48491
7f11a1699ef6
8194667: Regex: Serialization doesn't work with match flags
sherman
parents:
47216
diff
changeset
|
3231 |
flags0 &= ~(UNICODE_CHARACTER_CLASS | UNICODE_CASE); |
38777
826eb7091523
8158482: regex UNICODE_CHARACTER_CLASS flag cannot be disabled with an embedded flag expression
sherman
parents:
37882
diff
changeset
|
3232 |
break; |
2 | 3233 |
default: |
3234 |
return; |
|
3235 |
} |
|
3236 |
ch = next(); |
|
3237 |
} |
|
3238 |
} |
|
3239 |
||
3240 |
static final int MAX_REPS = 0x7FFFFFFF; |
|
3241 |
||
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3242 |
static enum Qtype { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3243 |
GREEDY, LAZY, POSSESSIVE, INDEPENDENT |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3244 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3245 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3246 |
private Node curly(Node prev, int cmin) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3247 |
int ch = next(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3248 |
if (ch == '?') { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3249 |
next(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3250 |
return new Curly(prev, cmin, MAX_REPS, Qtype.LAZY); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3251 |
} else if (ch == '+') { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3252 |
next(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3253 |
return new Curly(prev, cmin, MAX_REPS, Qtype.POSSESSIVE); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3254 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3255 |
if (prev instanceof BmpCharProperty) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3256 |
return new BmpCharPropertyGreedy((BmpCharProperty)prev, cmin); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3257 |
} else if (prev instanceof CharProperty) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3258 |
return new CharPropertyGreedy((CharProperty)prev, cmin); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3259 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3260 |
return new Curly(prev, cmin, MAX_REPS, Qtype.GREEDY); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3261 |
} |
2 | 3262 |
|
3263 |
/** |
|
3264 |
* Processes repetition. If the next character peeked is a quantifier |
|
3265 |
* then new nodes must be appended to handle the repetition. |
|
3266 |
* Prev could be a single or a group, so it could be a chain of nodes. |
|
3267 |
*/ |
|
3268 |
private Node closure(Node prev) { |
|
3269 |
int ch = peek(); |
|
3270 |
switch (ch) { |
|
3271 |
case '?': |
|
3272 |
ch = next(); |
|
3273 |
if (ch == '?') { |
|
3274 |
next(); |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3275 |
return new Ques(prev, Qtype.LAZY); |
2 | 3276 |
} else if (ch == '+') { |
3277 |
next(); |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3278 |
return new Ques(prev, Qtype.POSSESSIVE); |
2 | 3279 |
} |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3280 |
return new Ques(prev, Qtype.GREEDY); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3281 |
case '*': |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3282 |
return curly(prev, 0); |
2 | 3283 |
case '+': |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3284 |
return curly(prev, 1); |
2 | 3285 |
case '{': |
55096
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3286 |
ch = skip(); |
2 | 3287 |
if (ASCII.isDigit(ch)) { |
55096
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3288 |
int cmin = 0, cmax; |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3289 |
try { |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3290 |
do { |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3291 |
cmin = Math.addExact(Math.multiplyExact(cmin, 10), |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3292 |
ch - '0'); |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3293 |
} while (ASCII.isDigit(ch = read())); |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3294 |
if (ch == ',') { |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3295 |
ch = read(); |
55214
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
3296 |
if (ch == '}') { |
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
3297 |
unread(); |
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
3298 |
return curly(prev, cmin); |
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
3299 |
} else { |
55096
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3300 |
cmax = 0; |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3301 |
while (ASCII.isDigit(ch)) { |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3302 |
cmax = Math.addExact(Math.multiplyExact(cmax, 10), |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3303 |
ch - '0'); |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3304 |
ch = read(); |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3305 |
} |
2 | 3306 |
} |
55214
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
3307 |
} else { |
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
3308 |
cmax = cmin; |
2 | 3309 |
} |
55096
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3310 |
} catch (ArithmeticException ae) { |
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3311 |
throw error("Illegal repetition range"); |
2 | 3312 |
} |
3313 |
if (ch != '}') |
|
3314 |
throw error("Unclosed counted closure"); |
|
55096
234673929e0a
8224789: Parsing repetition count in regex does not detect numeric overflow
igerasim
parents:
55013
diff
changeset
|
3315 |
if (cmax < cmin) |
2 | 3316 |
throw error("Illegal repetition range"); |
3317 |
ch = peek(); |
|
3318 |
if (ch == '?') { |
|
3319 |
next(); |
|
55214
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
3320 |
return new Curly(prev, cmin, cmax, Qtype.LAZY); |
2 | 3321 |
} else if (ch == '+') { |
3322 |
next(); |
|
55214
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
3323 |
return new Curly(prev, cmin, cmax, Qtype.POSSESSIVE); |
2 | 3324 |
} else { |
55214
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
3325 |
return new Curly(prev, cmin, cmax, Qtype.GREEDY); |
2 | 3326 |
} |
3327 |
} else { |
|
3328 |
throw error("Illegal repetition"); |
|
3329 |
} |
|
3330 |
default: |
|
3331 |
return prev; |
|
3332 |
} |
|
3333 |
} |
|
3334 |
||
3335 |
/** |
|
3336 |
* Utility method for parsing control escape sequences. |
|
3337 |
*/ |
|
3338 |
private int c() { |
|
3339 |
if (cursor < patternLength) { |
|
3340 |
return read() ^ 64; |
|
3341 |
} |
|
3342 |
throw error("Illegal control escape sequence"); |
|
3343 |
} |
|
3344 |
||
3345 |
/** |
|
3346 |
* Utility method for parsing octal escape sequences. |
|
3347 |
*/ |
|
3348 |
private int o() { |
|
3349 |
int n = read(); |
|
3350 |
if (((n-'0')|('7'-n)) >= 0) { |
|
3351 |
int m = read(); |
|
3352 |
if (((m-'0')|('7'-m)) >= 0) { |
|
3353 |
int o = read(); |
|
3354 |
if ((((o-'0')|('7'-o)) >= 0) && (((n-'0')|('3'-n)) >= 0)) { |
|
3355 |
return (n - '0') * 64 + (m - '0') * 8 + (o - '0'); |
|
3356 |
} |
|
3357 |
unread(); |
|
3358 |
return (n - '0') * 8 + (m - '0'); |
|
3359 |
} |
|
3360 |
unread(); |
|
3361 |
return (n - '0'); |
|
3362 |
} |
|
3363 |
throw error("Illegal octal escape sequence"); |
|
3364 |
} |
|
3365 |
||
3366 |
/** |
|
3367 |
* Utility method for parsing hexadecimal escape sequences. |
|
3368 |
*/ |
|
3369 |
private int x() { |
|
3370 |
int n = read(); |
|
3371 |
if (ASCII.isHexDigit(n)) { |
|
3372 |
int m = read(); |
|
3373 |
if (ASCII.isHexDigit(m)) { |
|
3374 |
return ASCII.toDigit(n) * 16 + ASCII.toDigit(m); |
|
3375 |
} |
|
8173
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
3376 |
} else if (n == '{' && ASCII.isHexDigit(peek())) { |
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
3377 |
int ch = 0; |
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
3378 |
while (ASCII.isHexDigit(n = read())) { |
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
3379 |
ch = (ch << 4) + ASCII.toDigit(n); |
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
3380 |
if (ch > Character.MAX_CODE_POINT) |
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
3381 |
throw error("Hexadecimal codepoint is too big"); |
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
3382 |
} |
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
3383 |
if (n != '}') |
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
3384 |
throw error("Unclosed hexadecimal escape sequence"); |
a3a39b98e05a
7014645: Support perl style Unicode hex notation \x{...}
sherman
parents:
7816
diff
changeset
|
3385 |
return ch; |
2 | 3386 |
} |
3387 |
throw error("Illegal hexadecimal escape sequence"); |
|
3388 |
} |
|
3389 |
||
3390 |
/** |
|
3391 |
* Utility method for parsing unicode escape sequences. |
|
3392 |
*/ |
|
404
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3393 |
private int cursor() { |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3394 |
return cursor; |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3395 |
} |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3396 |
|
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3397 |
private void setcursor(int pos) { |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3398 |
cursor = pos; |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3399 |
} |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3400 |
|
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3401 |
private int uxxxx() { |
2 | 3402 |
int n = 0; |
3403 |
for (int i = 0; i < 4; i++) { |
|
3404 |
int ch = read(); |
|
3405 |
if (!ASCII.isHexDigit(ch)) { |
|
3406 |
throw error("Illegal Unicode escape sequence"); |
|
3407 |
} |
|
3408 |
n = n * 16 + ASCII.toDigit(ch); |
|
3409 |
} |
|
3410 |
return n; |
|
3411 |
} |
|
3412 |
||
404
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3413 |
private int u() { |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3414 |
int n = uxxxx(); |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3415 |
if (Character.isHighSurrogate((char)n)) { |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3416 |
int cur = cursor(); |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3417 |
if (read() == '\\' && read() == 'u') { |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3418 |
int n2 = uxxxx(); |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3419 |
if (Character.isLowSurrogate((char)n2)) |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3420 |
return Character.toCodePoint((char)n, (char)n2); |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3421 |
} |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3422 |
setcursor(cur); |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3423 |
} |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3424 |
return n; |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3425 |
} |
0f6ea24796a4
6635133: Exception thrown when using a Unicode escape
sherman
parents:
2
diff
changeset
|
3426 |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3427 |
private int N() { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3428 |
if (read() == '{') { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3429 |
int i = cursor; |
58623 | 3430 |
while (read() != '}') { |
3431 |
if (cursor >= patternLength) |
|
3432 |
throw error("Unclosed character name escape sequence"); |
|
3433 |
} |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3434 |
String name = new String(temp, i, cursor - i - 1); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3435 |
try { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3436 |
return Character.codePointOf(name); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3437 |
} catch (IllegalArgumentException x) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3438 |
throw error("Unknown character name [" + name + "]"); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3439 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3440 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3441 |
throw error("Illegal character name escape sequence"); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3442 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
3443 |
|
2 | 3444 |
// |
3445 |
// Utility methods for code point support |
|
3446 |
// |
|
3447 |
private static final int countChars(CharSequence seq, int index, |
|
3448 |
int lengthInCodePoints) { |
|
3449 |
// optimization |
|
3450 |
if (lengthInCodePoints == 1 && !Character.isHighSurrogate(seq.charAt(index))) { |
|
3451 |
assert (index >= 0 && index < seq.length()); |
|
3452 |
return 1; |
|
3453 |
} |
|
3454 |
int length = seq.length(); |
|
3455 |
int x = index; |
|
3456 |
if (lengthInCodePoints >= 0) { |
|
3457 |
assert (index >= 0 && index < length); |
|
3458 |
for (int i = 0; x < length && i < lengthInCodePoints; i++) { |
|
3459 |
if (Character.isHighSurrogate(seq.charAt(x++))) { |
|
3460 |
if (x < length && Character.isLowSurrogate(seq.charAt(x))) { |
|
3461 |
x++; |
|
3462 |
} |
|
3463 |
} |
|
3464 |
} |
|
3465 |
return x - index; |
|
3466 |
} |
|
3467 |
||
3468 |
assert (index >= 0 && index <= length); |
|
3469 |
if (index == 0) { |
|
3470 |
return 0; |
|
3471 |
} |
|
3472 |
int len = -lengthInCodePoints; |
|
3473 |
for (int i = 0; x > 0 && i < len; i++) { |
|
3474 |
if (Character.isLowSurrogate(seq.charAt(--x))) { |
|
3475 |
if (x > 0 && Character.isHighSurrogate(seq.charAt(x-1))) { |
|
3476 |
x--; |
|
3477 |
} |
|
3478 |
} |
|
3479 |
} |
|
3480 |
return index - x; |
|
3481 |
} |
|
3482 |
||
3483 |
private static final int countCodePoints(CharSequence seq) { |
|
3484 |
int length = seq.length(); |
|
3485 |
int n = 0; |
|
3486 |
for (int i = 0; i < length; ) { |
|
3487 |
n++; |
|
3488 |
if (Character.isHighSurrogate(seq.charAt(i++))) { |
|
3489 |
if (i < length && Character.isLowSurrogate(seq.charAt(i))) { |
|
3490 |
i++; |
|
3491 |
} |
|
3492 |
} |
|
3493 |
} |
|
3494 |
return n; |
|
3495 |
} |
|
3496 |
||
3497 |
/** |
|
3498 |
* Creates a bit vector for matching Latin-1 values. A normal BitClass |
|
3499 |
* never matches values above Latin-1, and a complemented BitClass always |
|
3500 |
* matches values above Latin-1. |
|
3501 |
*/ |
|
55220 | 3502 |
static final class BitClass implements BmpCharPredicate { |
2 | 3503 |
final boolean[] bits; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3504 |
BitClass() { |
55220 | 3505 |
bits = new boolean[256]; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3506 |
} |
2 | 3507 |
BitClass add(int c, int flags) { |
3508 |
assert c >= 0 && c <= 255; |
|
3509 |
if ((flags & CASE_INSENSITIVE) != 0) { |
|
3510 |
if (ASCII.isAscii(c)) { |
|
3511 |
bits[ASCII.toUpper(c)] = true; |
|
3512 |
bits[ASCII.toLower(c)] = true; |
|
3513 |
} else if ((flags & UNICODE_CASE) != 0) { |
|
3514 |
bits[Character.toLowerCase(c)] = true; |
|
3515 |
bits[Character.toUpperCase(c)] = true; |
|
3516 |
} |
|
3517 |
} |
|
3518 |
bits[c] = true; |
|
3519 |
return this; |
|
3520 |
} |
|
55220 | 3521 |
public boolean is(int ch) { |
3522 |
return ch < 256 && bits[ch]; |
|
3523 |
} |
|
2 | 3524 |
} |
3525 |
||
55220 | 3526 |
|
2 | 3527 |
/** |
3528 |
* Utility method for creating a string slice matcher. |
|
3529 |
*/ |
|
3530 |
private Node newSlice(int[] buf, int count, boolean hasSupplementary) { |
|
3531 |
int[] tmp = new int[count]; |
|
3532 |
if (has(CASE_INSENSITIVE)) { |
|
3533 |
if (has(UNICODE_CASE)) { |
|
3534 |
for (int i = 0; i < count; i++) { |
|
3535 |
tmp[i] = Character.toLowerCase( |
|
3536 |
Character.toUpperCase(buf[i])); |
|
3537 |
} |
|
3538 |
return hasSupplementary? new SliceUS(tmp) : new SliceU(tmp); |
|
3539 |
} |
|
3540 |
for (int i = 0; i < count; i++) { |
|
3541 |
tmp[i] = ASCII.toLower(buf[i]); |
|
3542 |
} |
|
3543 |
return hasSupplementary? new SliceIS(tmp) : new SliceI(tmp); |
|
3544 |
} |
|
3545 |
for (int i = 0; i < count; i++) { |
|
3546 |
tmp[i] = buf[i]; |
|
3547 |
} |
|
3548 |
return hasSupplementary ? new SliceS(tmp) : new Slice(tmp); |
|
3549 |
} |
|
3550 |
||
3551 |
/** |
|
3552 |
* The following classes are the building components of the object |
|
3553 |
* tree that represents a compiled regular expression. The object tree |
|
3554 |
* is made of individual elements that handle constructs in the Pattern. |
|
3555 |
* Each type of object knows how to match its equivalent construct with |
|
3556 |
* the match() method. |
|
3557 |
*/ |
|
3558 |
||
3559 |
/** |
|
3560 |
* Base class for all node classes. Subclasses should override the match() |
|
3561 |
* method as appropriate. This class is an accepting node, so its match() |
|
3562 |
* always returns true. |
|
3563 |
*/ |
|
3564 |
static class Node extends Object { |
|
3565 |
Node next; |
|
3566 |
Node() { |
|
3567 |
next = Pattern.accept; |
|
3568 |
} |
|
3569 |
/** |
|
3570 |
* This method implements the classic accept node. |
|
3571 |
*/ |
|
3572 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3573 |
matcher.last = i; |
|
3574 |
matcher.groups[0] = matcher.first; |
|
3575 |
matcher.groups[1] = matcher.last; |
|
3576 |
return true; |
|
3577 |
} |
|
3578 |
/** |
|
3579 |
* This method is good for all zero length assertions. |
|
3580 |
*/ |
|
3581 |
boolean study(TreeInfo info) { |
|
3582 |
if (next != null) { |
|
3583 |
return next.study(info); |
|
3584 |
} else { |
|
3585 |
return info.deterministic; |
|
3586 |
} |
|
3587 |
} |
|
3588 |
} |
|
3589 |
||
3590 |
static class LastNode extends Node { |
|
3591 |
/** |
|
3592 |
* This method implements the classic accept node with |
|
3593 |
* the addition of a check to see if the match occurred |
|
3594 |
* using all of the input. |
|
3595 |
*/ |
|
3596 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3597 |
if (matcher.acceptMode == Matcher.ENDANCHOR && i != matcher.to) |
|
3598 |
return false; |
|
3599 |
matcher.last = i; |
|
3600 |
matcher.groups[0] = matcher.first; |
|
3601 |
matcher.groups[1] = matcher.last; |
|
3602 |
return true; |
|
3603 |
} |
|
3604 |
} |
|
3605 |
||
3606 |
/** |
|
3607 |
* Used for REs that can start anywhere within the input string. |
|
3608 |
* This basically tries to match repeatedly at each spot in the |
|
3609 |
* input string, moving forward after each try. An anchored search |
|
3610 |
* or a BnM will bypass this node completely. |
|
3611 |
*/ |
|
3612 |
static class Start extends Node { |
|
3613 |
int minLength; |
|
3614 |
Start(Node node) { |
|
3615 |
this.next = node; |
|
3616 |
TreeInfo info = new TreeInfo(); |
|
3617 |
next.study(info); |
|
3618 |
minLength = info.minLength; |
|
3619 |
} |
|
3620 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3621 |
if (i > matcher.to - minLength) { |
|
3622 |
matcher.hitEnd = true; |
|
3623 |
return false; |
|
3624 |
} |
|
3625 |
int guard = matcher.to - minLength; |
|
3626 |
for (; i <= guard; i++) { |
|
4820
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3627 |
if (next.match(matcher, i, seq)) { |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3628 |
matcher.first = i; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3629 |
matcher.groups[0] = matcher.first; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3630 |
matcher.groups[1] = matcher.last; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3631 |
return true; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3632 |
} |
2 | 3633 |
} |
4820
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3634 |
matcher.hitEnd = true; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3635 |
return false; |
2 | 3636 |
} |
3637 |
boolean study(TreeInfo info) { |
|
3638 |
next.study(info); |
|
3639 |
info.maxValid = false; |
|
3640 |
info.deterministic = false; |
|
3641 |
return false; |
|
3642 |
} |
|
3643 |
} |
|
3644 |
||
3645 |
/* |
|
3646 |
* StartS supports supplementary characters, including unpaired surrogates. |
|
3647 |
*/ |
|
3648 |
static final class StartS extends Start { |
|
3649 |
StartS(Node node) { |
|
3650 |
super(node); |
|
3651 |
} |
|
3652 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3653 |
if (i > matcher.to - minLength) { |
|
3654 |
matcher.hitEnd = true; |
|
3655 |
return false; |
|
3656 |
} |
|
3657 |
int guard = matcher.to - minLength; |
|
3658 |
while (i <= guard) { |
|
4820
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3659 |
//if ((ret = next.match(matcher, i, seq)) || i == guard) |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3660 |
if (next.match(matcher, i, seq)) { |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3661 |
matcher.first = i; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3662 |
matcher.groups[0] = matcher.first; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3663 |
matcher.groups[1] = matcher.last; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3664 |
return true; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3665 |
} |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3666 |
if (i == guard) |
2 | 3667 |
break; |
3668 |
// Optimization to move to the next character. This is |
|
3669 |
// faster than countChars(seq, i, 1). |
|
3670 |
if (Character.isHighSurrogate(seq.charAt(i++))) { |
|
4820
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3671 |
if (i < seq.length() && |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3672 |
Character.isLowSurrogate(seq.charAt(i))) { |
2 | 3673 |
i++; |
3674 |
} |
|
3675 |
} |
|
3676 |
} |
|
4820
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3677 |
matcher.hitEnd = true; |
e32f5d2ba062
6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents:
4161
diff
changeset
|
3678 |
return false; |
2 | 3679 |
} |
3680 |
} |
|
3681 |
||
3682 |
/** |
|
3683 |
* Node to anchor at the beginning of input. This object implements the |
|
3684 |
* match for a \A sequence, and the caret anchor will use this if not in |
|
3685 |
* multiline mode. |
|
3686 |
*/ |
|
3687 |
static final class Begin extends Node { |
|
3688 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3689 |
int fromIndex = (matcher.anchoringBounds) ? |
|
3690 |
matcher.from : 0; |
|
3691 |
if (i == fromIndex && next.match(matcher, i, seq)) { |
|
3692 |
matcher.first = i; |
|
3693 |
matcher.groups[0] = i; |
|
3694 |
matcher.groups[1] = matcher.last; |
|
3695 |
return true; |
|
3696 |
} else { |
|
3697 |
return false; |
|
3698 |
} |
|
3699 |
} |
|
3700 |
} |
|
3701 |
||
3702 |
/** |
|
3703 |
* Node to anchor at the end of input. This is the absolute end, so this |
|
3704 |
* should not match at the last newline before the end as $ will. |
|
3705 |
*/ |
|
3706 |
static final class End extends Node { |
|
3707 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3708 |
int endIndex = (matcher.anchoringBounds) ? |
|
3709 |
matcher.to : matcher.getTextLength(); |
|
3710 |
if (i == endIndex) { |
|
3711 |
matcher.hitEnd = true; |
|
3712 |
return next.match(matcher, i, seq); |
|
3713 |
} |
|
3714 |
return false; |
|
3715 |
} |
|
3716 |
} |
|
3717 |
||
3718 |
/** |
|
3719 |
* Node to anchor at the beginning of a line. This is essentially the |
|
3720 |
* object to match for the multiline ^. |
|
3721 |
*/ |
|
3722 |
static final class Caret extends Node { |
|
3723 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3724 |
int startIndex = matcher.from; |
|
3725 |
int endIndex = matcher.to; |
|
3726 |
if (!matcher.anchoringBounds) { |
|
3727 |
startIndex = 0; |
|
3728 |
endIndex = matcher.getTextLength(); |
|
3729 |
} |
|
3730 |
// Perl does not match ^ at end of input even after newline |
|
3731 |
if (i == endIndex) { |
|
3732 |
matcher.hitEnd = true; |
|
3733 |
return false; |
|
3734 |
} |
|
3735 |
if (i > startIndex) { |
|
3736 |
char ch = seq.charAt(i-1); |
|
3737 |
if (ch != '\n' && ch != '\r' |
|
3738 |
&& (ch|1) != '\u2029' |
|
3739 |
&& ch != '\u0085' ) { |
|
3740 |
return false; |
|
3741 |
} |
|
3742 |
// Should treat /r/n as one newline |
|
3743 |
if (ch == '\r' && seq.charAt(i) == '\n') |
|
3744 |
return false; |
|
3745 |
} |
|
3746 |
return next.match(matcher, i, seq); |
|
3747 |
} |
|
3748 |
} |
|
3749 |
||
3750 |
/** |
|
3751 |
* Node to anchor at the beginning of a line when in unixdot mode. |
|
3752 |
*/ |
|
3753 |
static final class UnixCaret extends Node { |
|
3754 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3755 |
int startIndex = matcher.from; |
|
3756 |
int endIndex = matcher.to; |
|
3757 |
if (!matcher.anchoringBounds) { |
|
3758 |
startIndex = 0; |
|
3759 |
endIndex = matcher.getTextLength(); |
|
3760 |
} |
|
3761 |
// Perl does not match ^ at end of input even after newline |
|
3762 |
if (i == endIndex) { |
|
3763 |
matcher.hitEnd = true; |
|
3764 |
return false; |
|
3765 |
} |
|
3766 |
if (i > startIndex) { |
|
3767 |
char ch = seq.charAt(i-1); |
|
3768 |
if (ch != '\n') { |
|
3769 |
return false; |
|
3770 |
} |
|
3771 |
} |
|
3772 |
return next.match(matcher, i, seq); |
|
3773 |
} |
|
3774 |
} |
|
3775 |
||
3776 |
/** |
|
3777 |
* Node to match the location where the last match ended. |
|
3778 |
* This is used for the \G construct. |
|
3779 |
*/ |
|
3780 |
static final class LastMatch extends Node { |
|
3781 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3782 |
if (i != matcher.oldLast) |
|
3783 |
return false; |
|
3784 |
return next.match(matcher, i, seq); |
|
3785 |
} |
|
3786 |
} |
|
3787 |
||
3788 |
/** |
|
3789 |
* Node to anchor at the end of a line or the end of input based on the |
|
3790 |
* multiline mode. |
|
3791 |
* |
|
3792 |
* When not in multiline mode, the $ can only match at the very end |
|
3793 |
* of the input, unless the input ends in a line terminator in which |
|
3794 |
* it matches right before the last line terminator. |
|
3795 |
* |
|
3796 |
* Note that \r\n is considered an atomic line terminator. |
|
3797 |
* |
|
3798 |
* Like ^ the $ operator matches at a position, it does not match the |
|
3799 |
* line terminators themselves. |
|
3800 |
*/ |
|
3801 |
static final class Dollar extends Node { |
|
3802 |
boolean multiline; |
|
3803 |
Dollar(boolean mul) { |
|
3804 |
multiline = mul; |
|
3805 |
} |
|
3806 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3807 |
int endIndex = (matcher.anchoringBounds) ? |
|
3808 |
matcher.to : matcher.getTextLength(); |
|
3809 |
if (!multiline) { |
|
3810 |
if (i < endIndex - 2) |
|
3811 |
return false; |
|
3812 |
if (i == endIndex - 2) { |
|
3813 |
char ch = seq.charAt(i); |
|
3814 |
if (ch != '\r') |
|
3815 |
return false; |
|
3816 |
ch = seq.charAt(i + 1); |
|
3817 |
if (ch != '\n') |
|
3818 |
return false; |
|
3819 |
} |
|
3820 |
} |
|
3821 |
// Matches before any line terminator; also matches at the |
|
3822 |
// end of input |
|
3823 |
// Before line terminator: |
|
3824 |
// If multiline, we match here no matter what |
|
3825 |
// If not multiline, fall through so that the end |
|
3826 |
// is marked as hit; this must be a /r/n or a /n |
|
3827 |
// at the very end so the end was hit; more input |
|
3828 |
// could make this not match here |
|
3829 |
if (i < endIndex) { |
|
3830 |
char ch = seq.charAt(i); |
|
3831 |
if (ch == '\n') { |
|
3832 |
// No match between \r\n |
|
3833 |
if (i > 0 && seq.charAt(i-1) == '\r') |
|
3834 |
return false; |
|
3835 |
if (multiline) |
|
3836 |
return next.match(matcher, i, seq); |
|
3837 |
} else if (ch == '\r' || ch == '\u0085' || |
|
3838 |
(ch|1) == '\u2029') { |
|
3839 |
if (multiline) |
|
3840 |
return next.match(matcher, i, seq); |
|
3841 |
} else { // No line terminator, no match |
|
3842 |
return false; |
|
3843 |
} |
|
3844 |
} |
|
3845 |
// Matched at current end so hit end |
|
3846 |
matcher.hitEnd = true; |
|
3847 |
// If a $ matches because of end of input, then more input |
|
3848 |
// could cause it to fail! |
|
3849 |
matcher.requireEnd = true; |
|
3850 |
return next.match(matcher, i, seq); |
|
3851 |
} |
|
3852 |
boolean study(TreeInfo info) { |
|
3853 |
next.study(info); |
|
3854 |
return info.deterministic; |
|
3855 |
} |
|
3856 |
} |
|
3857 |
||
3858 |
/** |
|
3859 |
* Node to anchor at the end of a line or the end of input based on the |
|
3860 |
* multiline mode when in unix lines mode. |
|
3861 |
*/ |
|
3862 |
static final class UnixDollar extends Node { |
|
3863 |
boolean multiline; |
|
3864 |
UnixDollar(boolean mul) { |
|
3865 |
multiline = mul; |
|
3866 |
} |
|
3867 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3868 |
int endIndex = (matcher.anchoringBounds) ? |
|
3869 |
matcher.to : matcher.getTextLength(); |
|
3870 |
if (i < endIndex) { |
|
3871 |
char ch = seq.charAt(i); |
|
3872 |
if (ch == '\n') { |
|
3873 |
// If not multiline, then only possible to |
|
3874 |
// match at very end or one before end |
|
3875 |
if (multiline == false && i != endIndex - 1) |
|
3876 |
return false; |
|
3877 |
// If multiline return next.match without setting |
|
3878 |
// matcher.hitEnd |
|
3879 |
if (multiline) |
|
3880 |
return next.match(matcher, i, seq); |
|
3881 |
} else { |
|
3882 |
return false; |
|
3883 |
} |
|
3884 |
} |
|
3885 |
// Matching because at the end or 1 before the end; |
|
3886 |
// more input could change this so set hitEnd |
|
3887 |
matcher.hitEnd = true; |
|
3888 |
// If a $ matches because of end of input, then more input |
|
3889 |
// could cause it to fail! |
|
3890 |
matcher.requireEnd = true; |
|
3891 |
return next.match(matcher, i, seq); |
|
3892 |
} |
|
3893 |
boolean study(TreeInfo info) { |
|
3894 |
next.study(info); |
|
3895 |
return info.deterministic; |
|
3896 |
} |
|
3897 |
} |
|
3898 |
||
3899 |
/** |
|
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3900 |
* Node class that matches a Unicode line ending '\R' |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3901 |
*/ |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3902 |
static final class LineEnding extends Node { |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3903 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3904 |
// (u+000Du+000A|[u+000Au+000Bu+000Cu+000Du+0085u+2028u+2029]) |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3905 |
if (i < matcher.to) { |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3906 |
int ch = seq.charAt(i); |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3907 |
if (ch == 0x0A || ch == 0x0B || ch == 0x0C || |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3908 |
ch == 0x85 || ch == 0x2028 || ch == 0x2029) |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3909 |
return next.match(matcher, i + 1, seq); |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3910 |
if (ch == 0x0D) { |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3911 |
i++; |
45888
ae448cf92e00
8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents:
45124
diff
changeset
|
3912 |
if (i < matcher.to) { |
ae448cf92e00
8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents:
45124
diff
changeset
|
3913 |
if (seq.charAt(i) == 0x0A && |
ae448cf92e00
8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents:
45124
diff
changeset
|
3914 |
next.match(matcher, i + 1, seq)) { |
ae448cf92e00
8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents:
45124
diff
changeset
|
3915 |
return true; |
ae448cf92e00
8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents:
45124
diff
changeset
|
3916 |
} |
ae448cf92e00
8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents:
45124
diff
changeset
|
3917 |
} else { |
ae448cf92e00
8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents:
45124
diff
changeset
|
3918 |
matcher.hitEnd = true; |
44122
65a14579a2ae
8176029: Linebreak matcher is not equivalent to the pattern as stated in javadoc
sherman
parents:
43502
diff
changeset
|
3919 |
} |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3920 |
return next.match(matcher, i, seq); |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3921 |
} |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3922 |
} else { |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3923 |
matcher.hitEnd = true; |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3924 |
} |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3925 |
return false; |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3926 |
} |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3927 |
boolean study(TreeInfo info) { |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3928 |
info.minLength++; |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3929 |
info.maxLength += 2; |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3930 |
return next.study(info); |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3931 |
} |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3932 |
} |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3933 |
|
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
3934 |
/** |
2 | 3935 |
* Abstract node class to match one character satisfying some |
3936 |
* boolean property. |
|
3937 |
*/ |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3938 |
static class CharProperty extends Node { |
55220 | 3939 |
final CharPredicate predicate; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3940 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3941 |
CharProperty (CharPredicate predicate) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3942 |
this.predicate = predicate; |
2 | 3943 |
} |
3944 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
3945 |
if (i < matcher.to) { |
|
3946 |
int ch = Character.codePointAt(seq, i); |
|
58111
f63f50a4bf43
8230829: Matcher matches a surrogate pair that crosses border of the region
igerasim
parents:
57956
diff
changeset
|
3947 |
i += Character.charCount(ch); |
f63f50a4bf43
8230829: Matcher matches a surrogate pair that crosses border of the region
igerasim
parents:
57956
diff
changeset
|
3948 |
if (i <= matcher.to) { |
f63f50a4bf43
8230829: Matcher matches a surrogate pair that crosses border of the region
igerasim
parents:
57956
diff
changeset
|
3949 |
return predicate.is(ch) && |
f63f50a4bf43
8230829: Matcher matches a surrogate pair that crosses border of the region
igerasim
parents:
57956
diff
changeset
|
3950 |
next.match(matcher, i, seq); |
f63f50a4bf43
8230829: Matcher matches a surrogate pair that crosses border of the region
igerasim
parents:
57956
diff
changeset
|
3951 |
} |
2 | 3952 |
} |
58111
f63f50a4bf43
8230829: Matcher matches a surrogate pair that crosses border of the region
igerasim
parents:
57956
diff
changeset
|
3953 |
matcher.hitEnd = true; |
f63f50a4bf43
8230829: Matcher matches a surrogate pair that crosses border of the region
igerasim
parents:
57956
diff
changeset
|
3954 |
return false; |
2 | 3955 |
} |
3956 |
boolean study(TreeInfo info) { |
|
3957 |
info.minLength++; |
|
3958 |
info.maxLength++; |
|
3959 |
return next.study(info); |
|
3960 |
} |
|
3961 |
} |
|
3962 |
||
3963 |
/** |
|
3964 |
* Optimized version of CharProperty that works only for |
|
3965 |
* properties never satisfied by Supplementary characters. |
|
3966 |
*/ |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3967 |
private static class BmpCharProperty extends CharProperty { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3968 |
BmpCharProperty (BmpCharPredicate predicate) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3969 |
super(predicate); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3970 |
} |
2 | 3971 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
3972 |
if (i < matcher.to) { |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3973 |
return predicate.is(seq.charAt(i)) && |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3974 |
next.match(matcher, i + 1, seq); |
2 | 3975 |
} else { |
3976 |
matcher.hitEnd = true; |
|
3977 |
return false; |
|
3978 |
} |
|
3979 |
} |
|
3980 |
} |
|
3981 |
||
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3982 |
private static class NFCCharProperty extends Node { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3983 |
CharPredicate predicate; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3984 |
NFCCharProperty (CharPredicate predicate) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3985 |
this.predicate = predicate; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3986 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3987 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3988 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3989 |
if (i < matcher.to) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3990 |
int ch0 = Character.codePointAt(seq, i); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
3991 |
int n = Character.charCount(ch0); |
55141 | 3992 |
int j = i + n; |
3993 |
// Fast check if it's necessary to call Normalizer; |
|
3994 |
// testing Grapheme.isBoundary is enough for this case |
|
3995 |
while (j < matcher.to) { |
|
3996 |
int ch1 = Character.codePointAt(seq, j); |
|
3997 |
if (Grapheme.isBoundary(ch0, ch1)) |
|
3998 |
break; |
|
3999 |
ch0 = ch1; |
|
4000 |
j += Character.charCount(ch1); |
|
4001 |
} |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4002 |
if (i + n == j) { // single, assume nfc cp |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4003 |
if (predicate.is(ch0)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4004 |
return next.match(matcher, j, seq); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4005 |
} else { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4006 |
while (i + n < j) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4007 |
String nfc = Normalizer.normalize( |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4008 |
seq.toString().substring(i, j), Normalizer.Form.NFC); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4009 |
if (nfc.codePointCount(0, nfc.length()) == 1) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4010 |
if (predicate.is(nfc.codePointAt(0)) && |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4011 |
next.match(matcher, j, seq)) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4012 |
return true; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4013 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4014 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4015 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4016 |
ch0 = Character.codePointBefore(seq, j); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4017 |
j -= Character.charCount(ch0); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4018 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4019 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4020 |
if (j < matcher.to) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4021 |
return false; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4022 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4023 |
matcher.hitEnd = true; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4024 |
return false; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4025 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4026 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4027 |
boolean study(TreeInfo info) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4028 |
info.minLength++; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4029 |
info.deterministic = false; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4030 |
return next.study(info); |
12675
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
4031 |
} |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
4032 |
} |
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
4033 |
|
3b7d71495649
7014640: To add a metachar \R for line ending and character classes for vertical/horizontal ws \v \V \h \H
sherman
parents:
11287
diff
changeset
|
4034 |
/** |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4035 |
* Node class that matches an unicode extended grapheme cluster |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4036 |
*/ |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4037 |
static class XGrapheme extends Node { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4038 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4039 |
if (i < matcher.to) { |
55013 | 4040 |
i = Grapheme.nextBoundary(seq, i, matcher.to); |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4041 |
return next.match(matcher, i, seq); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4042 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4043 |
matcher.hitEnd = true; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4044 |
return false; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4045 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4046 |
|
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4047 |
boolean study(TreeInfo info) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4048 |
info.minLength++; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4049 |
info.deterministic = false; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4050 |
return next.study(info); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4051 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4052 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4053 |
|
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4054 |
/** |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4055 |
* Node class that handles grapheme boundaries |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4056 |
*/ |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4057 |
static class GraphemeBound extends Node { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4058 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4059 |
int startIndex = matcher.from; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4060 |
int endIndex = matcher.to; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4061 |
if (matcher.transparentBounds) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4062 |
startIndex = 0; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4063 |
endIndex = matcher.getTextLength(); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4064 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4065 |
if (i == startIndex) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4066 |
return next.match(matcher, i, seq); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4067 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4068 |
if (i < endIndex) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4069 |
if (Character.isSurrogatePair(seq.charAt(i-1), seq.charAt(i)) || |
55013 | 4070 |
Grapheme.nextBoundary(seq, |
4071 |
i - Character.charCount(Character.codePointBefore(seq, i)), |
|
4072 |
i + Character.charCount(Character.codePointAt(seq, i))) > i) { |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4073 |
return false; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4074 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4075 |
} else { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4076 |
matcher.hitEnd = true; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4077 |
matcher.requireEnd = true; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4078 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4079 |
return next.match(matcher, i, seq); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4080 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4081 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4082 |
|
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
34774
diff
changeset
|
4083 |
/** |
2 | 4084 |
* Base class for all Slice nodes |
4085 |
*/ |
|
4086 |
static class SliceNode extends Node { |
|
4087 |
int[] buffer; |
|
4088 |
SliceNode(int[] buf) { |
|
4089 |
buffer = buf; |
|
4090 |
} |
|
4091 |
boolean study(TreeInfo info) { |
|
4092 |
info.minLength += buffer.length; |
|
4093 |
info.maxLength += buffer.length; |
|
4094 |
return next.study(info); |
|
4095 |
} |
|
4096 |
} |
|
4097 |
||
4098 |
/** |
|
4099 |
* Node class for a case sensitive/BMP-only sequence of literal |
|
4100 |
* characters. |
|
4101 |
*/ |
|
22997
7d2259be8e79
8035076: Pattern$BnMS never used due to bug in Pattern$BnM.optimize
sherman
parents:
22078
diff
changeset
|
4102 |
static class Slice extends SliceNode { |
2 | 4103 |
Slice(int[] buf) { |
4104 |
super(buf); |
|
4105 |
} |
|
4106 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4107 |
int[] buf = buffer; |
|
4108 |
int len = buf.length; |
|
4109 |
for (int j=0; j<len; j++) { |
|
4110 |
if ((i+j) >= matcher.to) { |
|
4111 |
matcher.hitEnd = true; |
|
4112 |
return false; |
|
4113 |
} |
|
4114 |
if (buf[j] != seq.charAt(i+j)) |
|
4115 |
return false; |
|
4116 |
} |
|
4117 |
return next.match(matcher, i+len, seq); |
|
4118 |
} |
|
4119 |
} |
|
4120 |
||
4121 |
/** |
|
4122 |
* Node class for a case_insensitive/BMP-only sequence of literal |
|
4123 |
* characters. |
|
4124 |
*/ |
|
4125 |
static class SliceI extends SliceNode { |
|
4126 |
SliceI(int[] buf) { |
|
4127 |
super(buf); |
|
4128 |
} |
|
4129 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4130 |
int[] buf = buffer; |
|
4131 |
int len = buf.length; |
|
4132 |
for (int j=0; j<len; j++) { |
|
4133 |
if ((i+j) >= matcher.to) { |
|
4134 |
matcher.hitEnd = true; |
|
4135 |
return false; |
|
4136 |
} |
|
4137 |
int c = seq.charAt(i+j); |
|
4138 |
if (buf[j] != c && |
|
4139 |
buf[j] != ASCII.toLower(c)) |
|
4140 |
return false; |
|
4141 |
} |
|
4142 |
return next.match(matcher, i+len, seq); |
|
4143 |
} |
|
4144 |
} |
|
4145 |
||
4146 |
/** |
|
4147 |
* Node class for a unicode_case_insensitive/BMP-only sequence of |
|
4148 |
* literal characters. Uses unicode case folding. |
|
4149 |
*/ |
|
4150 |
static final class SliceU extends SliceNode { |
|
4151 |
SliceU(int[] buf) { |
|
4152 |
super(buf); |
|
4153 |
} |
|
4154 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4155 |
int[] buf = buffer; |
|
4156 |
int len = buf.length; |
|
4157 |
for (int j=0; j<len; j++) { |
|
4158 |
if ((i+j) >= matcher.to) { |
|
4159 |
matcher.hitEnd = true; |
|
4160 |
return false; |
|
4161 |
} |
|
4162 |
int c = seq.charAt(i+j); |
|
4163 |
if (buf[j] != c && |
|
4164 |
buf[j] != Character.toLowerCase(Character.toUpperCase(c))) |
|
4165 |
return false; |
|
4166 |
} |
|
4167 |
return next.match(matcher, i+len, seq); |
|
4168 |
} |
|
4169 |
} |
|
4170 |
||
4171 |
/** |
|
4172 |
* Node class for a case sensitive sequence of literal characters |
|
4173 |
* including supplementary characters. |
|
4174 |
*/ |
|
22997
7d2259be8e79
8035076: Pattern$BnMS never used due to bug in Pattern$BnM.optimize
sherman
parents:
22078
diff
changeset
|
4175 |
static final class SliceS extends Slice { |
2 | 4176 |
SliceS(int[] buf) { |
4177 |
super(buf); |
|
4178 |
} |
|
4179 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4180 |
int[] buf = buffer; |
|
4181 |
int x = i; |
|
4182 |
for (int j = 0; j < buf.length; j++) { |
|
4183 |
if (x >= matcher.to) { |
|
4184 |
matcher.hitEnd = true; |
|
4185 |
return false; |
|
4186 |
} |
|
4187 |
int c = Character.codePointAt(seq, x); |
|
4188 |
if (buf[j] != c) |
|
4189 |
return false; |
|
4190 |
x += Character.charCount(c); |
|
4191 |
if (x > matcher.to) { |
|
4192 |
matcher.hitEnd = true; |
|
4193 |
return false; |
|
4194 |
} |
|
4195 |
} |
|
4196 |
return next.match(matcher, x, seq); |
|
4197 |
} |
|
4198 |
} |
|
4199 |
||
4200 |
/** |
|
4201 |
* Node class for a case insensitive sequence of literal characters |
|
4202 |
* including supplementary characters. |
|
4203 |
*/ |
|
4204 |
static class SliceIS extends SliceNode { |
|
4205 |
SliceIS(int[] buf) { |
|
4206 |
super(buf); |
|
4207 |
} |
|
4208 |
int toLower(int c) { |
|
4209 |
return ASCII.toLower(c); |
|
4210 |
} |
|
4211 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4212 |
int[] buf = buffer; |
|
4213 |
int x = i; |
|
4214 |
for (int j = 0; j < buf.length; j++) { |
|
4215 |
if (x >= matcher.to) { |
|
4216 |
matcher.hitEnd = true; |
|
4217 |
return false; |
|
4218 |
} |
|
4219 |
int c = Character.codePointAt(seq, x); |
|
4220 |
if (buf[j] != c && buf[j] != toLower(c)) |
|
4221 |
return false; |
|
4222 |
x += Character.charCount(c); |
|
4223 |
if (x > matcher.to) { |
|
4224 |
matcher.hitEnd = true; |
|
4225 |
return false; |
|
4226 |
} |
|
4227 |
} |
|
4228 |
return next.match(matcher, x, seq); |
|
4229 |
} |
|
4230 |
} |
|
4231 |
||
4232 |
/** |
|
4233 |
* Node class for a case insensitive sequence of literal characters. |
|
4234 |
* Uses unicode case folding. |
|
4235 |
*/ |
|
4236 |
static final class SliceUS extends SliceIS { |
|
4237 |
SliceUS(int[] buf) { |
|
4238 |
super(buf); |
|
4239 |
} |
|
4240 |
int toLower(int c) { |
|
4241 |
return Character.toLowerCase(Character.toUpperCase(c)); |
|
4242 |
} |
|
4243 |
} |
|
4244 |
||
4245 |
/** |
|
4246 |
* The 0 or 1 quantifier. This one class implements all three types. |
|
4247 |
*/ |
|
4248 |
static final class Ques extends Node { |
|
4249 |
Node atom; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4250 |
Qtype type; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4251 |
Ques(Node node, Qtype type) { |
2 | 4252 |
this.atom = node; |
4253 |
this.type = type; |
|
4254 |
} |
|
4255 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4256 |
switch (type) { |
|
4257 |
case GREEDY: |
|
4258 |
return (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq)) |
|
4259 |
|| next.match(matcher, i, seq); |
|
4260 |
case LAZY: |
|
4261 |
return next.match(matcher, i, seq) |
|
4262 |
|| (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq)); |
|
4263 |
case POSSESSIVE: |
|
4264 |
if (atom.match(matcher, i, seq)) i = matcher.last; |
|
4265 |
return next.match(matcher, i, seq); |
|
4266 |
default: |
|
4267 |
return atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq); |
|
4268 |
} |
|
4269 |
} |
|
4270 |
boolean study(TreeInfo info) { |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4271 |
if (type != Qtype.INDEPENDENT) { |
2 | 4272 |
int minL = info.minLength; |
4273 |
atom.study(info); |
|
4274 |
info.minLength = minL; |
|
4275 |
info.deterministic = false; |
|
4276 |
return next.study(info); |
|
4277 |
} else { |
|
4278 |
atom.study(info); |
|
4279 |
return next.study(info); |
|
4280 |
} |
|
4281 |
} |
|
4282 |
} |
|
4283 |
||
4284 |
/** |
|
55214
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
4285 |
* Handles the greedy style repetition with the specified minimum |
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
4286 |
* and the maximum equal to MAX_REPS, for *, + and {N,} quantifiers. |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4287 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4288 |
static class CharPropertyGreedy extends Node { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4289 |
final CharPredicate predicate; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4290 |
final int cmin; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4291 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4292 |
CharPropertyGreedy(CharProperty cp, int cmin) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4293 |
this.predicate = cp.predicate; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4294 |
this.cmin = cmin; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4295 |
} |
55214
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
4296 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4297 |
int n = 0; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4298 |
int to = matcher.to; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4299 |
// greedy, all the way down |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4300 |
while (i < to) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4301 |
int ch = Character.codePointAt(seq, i); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4302 |
if (!predicate.is(ch)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4303 |
break; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4304 |
i += Character.charCount(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4305 |
n++; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4306 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4307 |
if (i >= to) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4308 |
matcher.hitEnd = true; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4309 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4310 |
while (n >= cmin) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4311 |
if (next.match(matcher, i, seq)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4312 |
return true; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4313 |
if (n == cmin) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4314 |
return false; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4315 |
// backing off if match fails |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4316 |
int ch = Character.codePointBefore(seq, i); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4317 |
i -= Character.charCount(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4318 |
n--; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4319 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4320 |
return false; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4321 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4322 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4323 |
boolean study(TreeInfo info) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4324 |
info.minLength += cmin; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4325 |
if (info.maxValid) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4326 |
info.maxLength += MAX_REPS; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4327 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4328 |
info.deterministic = false; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4329 |
return next.study(info); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4330 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4331 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4332 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4333 |
static final class BmpCharPropertyGreedy extends CharPropertyGreedy { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4334 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4335 |
BmpCharPropertyGreedy(BmpCharProperty bcp, int cmin) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4336 |
super(bcp, cmin); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4337 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4338 |
|
55214
7a026580fed5
8225198: Optimize regex tree for greedy quantifiers of type {N,}
igerasim
parents:
55141
diff
changeset
|
4339 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4340 |
int n = 0; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4341 |
int to = matcher.to; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4342 |
while (i < to && predicate.is(seq.charAt(i))) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4343 |
i++; n++; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4344 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4345 |
if (i >= to) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4346 |
matcher.hitEnd = true; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4347 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4348 |
while (n >= cmin) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4349 |
if (next.match(matcher, i, seq)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4350 |
return true; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4351 |
i--; n--; // backing off if match fails |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4352 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4353 |
return false; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4354 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4355 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4356 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4357 |
/** |
2 | 4358 |
* Handles the curly-brace style repetition with a specified minimum and |
4359 |
* maximum occurrences. The * quantifier is handled as a special case. |
|
4360 |
* This class handles the three types. |
|
4361 |
*/ |
|
4362 |
static final class Curly extends Node { |
|
4363 |
Node atom; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4364 |
Qtype type; |
2 | 4365 |
int cmin; |
4366 |
int cmax; |
|
4367 |
||
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4368 |
Curly(Node node, int cmin, int cmax, Qtype type) { |
2 | 4369 |
this.atom = node; |
4370 |
this.type = type; |
|
4371 |
this.cmin = cmin; |
|
4372 |
this.cmax = cmax; |
|
4373 |
} |
|
4374 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4375 |
int j; |
|
4376 |
for (j = 0; j < cmin; j++) { |
|
4377 |
if (atom.match(matcher, i, seq)) { |
|
4378 |
i = matcher.last; |
|
4379 |
continue; |
|
4380 |
} |
|
4381 |
return false; |
|
4382 |
} |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4383 |
if (type == Qtype.GREEDY) |
2 | 4384 |
return match0(matcher, i, j, seq); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4385 |
else if (type == Qtype.LAZY) |
2 | 4386 |
return match1(matcher, i, j, seq); |
4387 |
else |
|
4388 |
return match2(matcher, i, j, seq); |
|
4389 |
} |
|
4390 |
// Greedy match. |
|
4391 |
// i is the index to start matching at |
|
4392 |
// j is the number of atoms that have matched |
|
4393 |
boolean match0(Matcher matcher, int i, int j, CharSequence seq) { |
|
4394 |
if (j >= cmax) { |
|
4395 |
// We have matched the maximum... continue with the rest of |
|
4396 |
// the regular expression |
|
4397 |
return next.match(matcher, i, seq); |
|
4398 |
} |
|
4399 |
int backLimit = j; |
|
4400 |
while (atom.match(matcher, i, seq)) { |
|
4401 |
// k is the length of this match |
|
4402 |
int k = matcher.last - i; |
|
4403 |
if (k == 0) // Zero length match |
|
4404 |
break; |
|
4405 |
// Move up index and number matched |
|
4406 |
i = matcher.last; |
|
4407 |
j++; |
|
4408 |
// We are greedy so match as many as we can |
|
4409 |
while (j < cmax) { |
|
4410 |
if (!atom.match(matcher, i, seq)) |
|
4411 |
break; |
|
4412 |
if (i + k != matcher.last) { |
|
4413 |
if (match0(matcher, matcher.last, j+1, seq)) |
|
4414 |
return true; |
|
4415 |
break; |
|
4416 |
} |
|
4417 |
i += k; |
|
4418 |
j++; |
|
4419 |
} |
|
4420 |
// Handle backing off if match fails |
|
4421 |
while (j >= backLimit) { |
|
4422 |
if (next.match(matcher, i, seq)) |
|
4423 |
return true; |
|
4424 |
i -= k; |
|
4425 |
j--; |
|
4426 |
} |
|
4427 |
return false; |
|
4428 |
} |
|
4429 |
return next.match(matcher, i, seq); |
|
4430 |
} |
|
4431 |
// Reluctant match. At this point, the minimum has been satisfied. |
|
4432 |
// i is the index to start matching at |
|
4433 |
// j is the number of atoms that have matched |
|
4434 |
boolean match1(Matcher matcher, int i, int j, CharSequence seq) { |
|
4435 |
for (;;) { |
|
4436 |
// Try finishing match without consuming any more |
|
4437 |
if (next.match(matcher, i, seq)) |
|
4438 |
return true; |
|
4439 |
// At the maximum, no match found |
|
4440 |
if (j >= cmax) |
|
4441 |
return false; |
|
4442 |
// Okay, must try one more atom |
|
4443 |
if (!atom.match(matcher, i, seq)) |
|
4444 |
return false; |
|
4445 |
// If we haven't moved forward then must break out |
|
4446 |
if (i == matcher.last) |
|
4447 |
return false; |
|
4448 |
// Move up index and number matched |
|
4449 |
i = matcher.last; |
|
4450 |
j++; |
|
4451 |
} |
|
4452 |
} |
|
4453 |
boolean match2(Matcher matcher, int i, int j, CharSequence seq) { |
|
4454 |
for (; j < cmax; j++) { |
|
4455 |
if (!atom.match(matcher, i, seq)) |
|
4456 |
break; |
|
4457 |
if (i == matcher.last) |
|
4458 |
break; |
|
4459 |
i = matcher.last; |
|
4460 |
} |
|
4461 |
return next.match(matcher, i, seq); |
|
4462 |
} |
|
4463 |
boolean study(TreeInfo info) { |
|
4464 |
// Save original info |
|
4465 |
int minL = info.minLength; |
|
4466 |
int maxL = info.maxLength; |
|
4467 |
boolean maxV = info.maxValid; |
|
4468 |
boolean detm = info.deterministic; |
|
4469 |
info.reset(); |
|
4470 |
||
4471 |
atom.study(info); |
|
4472 |
||
4473 |
int temp = info.minLength * cmin + minL; |
|
4474 |
if (temp < minL) { |
|
4475 |
temp = 0xFFFFFFF; // arbitrary large number |
|
4476 |
} |
|
4477 |
info.minLength = temp; |
|
4478 |
||
4479 |
if (maxV & info.maxValid) { |
|
4480 |
temp = info.maxLength * cmax + maxL; |
|
4481 |
info.maxLength = temp; |
|
4482 |
if (temp < maxL) { |
|
4483 |
info.maxValid = false; |
|
4484 |
} |
|
4485 |
} else { |
|
4486 |
info.maxValid = false; |
|
4487 |
} |
|
4488 |
||
4489 |
if (info.deterministic && cmin == cmax) |
|
4490 |
info.deterministic = detm; |
|
4491 |
else |
|
4492 |
info.deterministic = false; |
|
4493 |
return next.study(info); |
|
4494 |
} |
|
4495 |
} |
|
4496 |
||
4497 |
/** |
|
4498 |
* Handles the curly-brace style repetition with a specified minimum and |
|
4499 |
* maximum occurrences in deterministic cases. This is an iterative |
|
4500 |
* optimization over the Prolog and Loop system which would handle this |
|
4501 |
* in a recursive way. The * quantifier is handled as a special case. |
|
4502 |
* If capture is true then this class saves group settings and ensures |
|
4503 |
* that groups are unset when backing off of a group match. |
|
4504 |
*/ |
|
4505 |
static final class GroupCurly extends Node { |
|
4506 |
Node atom; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4507 |
Qtype type; |
2 | 4508 |
int cmin; |
4509 |
int cmax; |
|
4510 |
int localIndex; |
|
4511 |
int groupIndex; |
|
4512 |
boolean capture; |
|
4513 |
||
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4514 |
GroupCurly(Node node, int cmin, int cmax, Qtype type, int local, |
2 | 4515 |
int group, boolean capture) { |
4516 |
this.atom = node; |
|
4517 |
this.type = type; |
|
4518 |
this.cmin = cmin; |
|
4519 |
this.cmax = cmax; |
|
4520 |
this.localIndex = local; |
|
4521 |
this.groupIndex = group; |
|
4522 |
this.capture = capture; |
|
4523 |
} |
|
4524 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4525 |
int[] groups = matcher.groups; |
|
4526 |
int[] locals = matcher.locals; |
|
4527 |
int save0 = locals[localIndex]; |
|
4528 |
int save1 = 0; |
|
4529 |
int save2 = 0; |
|
4530 |
||
4531 |
if (capture) { |
|
4532 |
save1 = groups[groupIndex]; |
|
4533 |
save2 = groups[groupIndex+1]; |
|
4534 |
} |
|
4535 |
||
4536 |
// Notify GroupTail there is no need to setup group info |
|
4537 |
// because it will be set here |
|
4538 |
locals[localIndex] = -1; |
|
4539 |
||
4540 |
boolean ret = true; |
|
4541 |
for (int j = 0; j < cmin; j++) { |
|
4542 |
if (atom.match(matcher, i, seq)) { |
|
4543 |
if (capture) { |
|
4544 |
groups[groupIndex] = i; |
|
4545 |
groups[groupIndex+1] = matcher.last; |
|
4546 |
} |
|
4547 |
i = matcher.last; |
|
4548 |
} else { |
|
4549 |
ret = false; |
|
4550 |
break; |
|
4551 |
} |
|
4552 |
} |
|
4553 |
if (ret) { |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4554 |
if (type == Qtype.GREEDY) { |
2 | 4555 |
ret = match0(matcher, i, cmin, seq); |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4556 |
} else if (type == Qtype.LAZY) { |
2 | 4557 |
ret = match1(matcher, i, cmin, seq); |
4558 |
} else { |
|
4559 |
ret = match2(matcher, i, cmin, seq); |
|
4560 |
} |
|
4561 |
} |
|
4562 |
if (!ret) { |
|
4563 |
locals[localIndex] = save0; |
|
4564 |
if (capture) { |
|
4565 |
groups[groupIndex] = save1; |
|
4566 |
groups[groupIndex+1] = save2; |
|
4567 |
} |
|
4568 |
} |
|
4569 |
return ret; |
|
4570 |
} |
|
4571 |
// Aggressive group match |
|
4572 |
boolean match0(Matcher matcher, int i, int j, CharSequence seq) { |
|
17183
1e21e0bc10e7
8007395: StringIndexOutofBoundsException in Match.find() when input String contains surrogate UTF-16 characters
sherman
parents:
14342
diff
changeset
|
4573 |
// don't back off passing the starting "j" |
1e21e0bc10e7
8007395: StringIndexOutofBoundsException in Match.find() when input String contains surrogate UTF-16 characters
sherman
parents:
14342
diff
changeset
|
4574 |
int min = j; |
2 | 4575 |
int[] groups = matcher.groups; |
4576 |
int save0 = 0; |
|
4577 |
int save1 = 0; |
|
4578 |
if (capture) { |
|
4579 |
save0 = groups[groupIndex]; |
|
4580 |
save1 = groups[groupIndex+1]; |
|
4581 |
} |
|
4582 |
for (;;) { |
|
4583 |
if (j >= cmax) |
|
4584 |
break; |
|
4585 |
if (!atom.match(matcher, i, seq)) |
|
4586 |
break; |
|
4587 |
int k = matcher.last - i; |
|
4588 |
if (k <= 0) { |
|
4589 |
if (capture) { |
|
4590 |
groups[groupIndex] = i; |
|
4591 |
groups[groupIndex+1] = i + k; |
|
4592 |
} |
|
4593 |
i = i + k; |
|
4594 |
break; |
|
4595 |
} |
|
4596 |
for (;;) { |
|
4597 |
if (capture) { |
|
4598 |
groups[groupIndex] = i; |
|
4599 |
groups[groupIndex+1] = i + k; |
|
4600 |
} |
|
4601 |
i = i + k; |
|
4602 |
if (++j >= cmax) |
|
4603 |
break; |
|
4604 |
if (!atom.match(matcher, i, seq)) |
|
4605 |
break; |
|
4606 |
if (i + k != matcher.last) { |
|
4607 |
if (match0(matcher, i, j, seq)) |
|
4608 |
return true; |
|
4609 |
break; |
|
4610 |
} |
|
4611 |
} |
|
17183
1e21e0bc10e7
8007395: StringIndexOutofBoundsException in Match.find() when input String contains surrogate UTF-16 characters
sherman
parents:
14342
diff
changeset
|
4612 |
while (j > min) { |
2 | 4613 |
if (next.match(matcher, i, seq)) { |
4614 |
if (capture) { |
|
4615 |
groups[groupIndex+1] = i; |
|
4616 |
groups[groupIndex] = i - k; |
|
4617 |
} |
|
4618 |
return true; |
|
4619 |
} |
|
4620 |
// backing off |
|
19604
f96e3aef2081
8023647: "abc1c".matches("(\\w)+1\\1")) returns false
sherman
parents:
19577
diff
changeset
|
4621 |
i = i - k; |
2 | 4622 |
if (capture) { |
4623 |
groups[groupIndex+1] = i; |
|
4624 |
groups[groupIndex] = i - k; |
|
4625 |
} |
|
4626 |
j--; |
|
19604
f96e3aef2081
8023647: "abc1c".matches("(\\w)+1\\1")) returns false
sherman
parents:
19577
diff
changeset
|
4627 |
|
2 | 4628 |
} |
4629 |
break; |
|
4630 |
} |
|
4631 |
if (capture) { |
|
4632 |
groups[groupIndex] = save0; |
|
4633 |
groups[groupIndex+1] = save1; |
|
4634 |
} |
|
4635 |
return next.match(matcher, i, seq); |
|
4636 |
} |
|
4637 |
// Reluctant matching |
|
4638 |
boolean match1(Matcher matcher, int i, int j, CharSequence seq) { |
|
4639 |
for (;;) { |
|
4640 |
if (next.match(matcher, i, seq)) |
|
4641 |
return true; |
|
4642 |
if (j >= cmax) |
|
4643 |
return false; |
|
4644 |
if (!atom.match(matcher, i, seq)) |
|
4645 |
return false; |
|
4646 |
if (i == matcher.last) |
|
4647 |
return false; |
|
4648 |
if (capture) { |
|
4649 |
matcher.groups[groupIndex] = i; |
|
4650 |
matcher.groups[groupIndex+1] = matcher.last; |
|
4651 |
} |
|
4652 |
i = matcher.last; |
|
4653 |
j++; |
|
4654 |
} |
|
4655 |
} |
|
4656 |
// Possessive matching |
|
4657 |
boolean match2(Matcher matcher, int i, int j, CharSequence seq) { |
|
4658 |
for (; j < cmax; j++) { |
|
4659 |
if (!atom.match(matcher, i, seq)) { |
|
4660 |
break; |
|
4661 |
} |
|
4662 |
if (capture) { |
|
4663 |
matcher.groups[groupIndex] = i; |
|
4664 |
matcher.groups[groupIndex+1] = matcher.last; |
|
4665 |
} |
|
4666 |
if (i == matcher.last) { |
|
4667 |
break; |
|
4668 |
} |
|
4669 |
i = matcher.last; |
|
4670 |
} |
|
4671 |
return next.match(matcher, i, seq); |
|
4672 |
} |
|
4673 |
boolean study(TreeInfo info) { |
|
4674 |
// Save original info |
|
4675 |
int minL = info.minLength; |
|
4676 |
int maxL = info.maxLength; |
|
4677 |
boolean maxV = info.maxValid; |
|
4678 |
boolean detm = info.deterministic; |
|
4679 |
info.reset(); |
|
4680 |
||
4681 |
atom.study(info); |
|
4682 |
||
4683 |
int temp = info.minLength * cmin + minL; |
|
4684 |
if (temp < minL) { |
|
4685 |
temp = 0xFFFFFFF; // Arbitrary large number |
|
4686 |
} |
|
4687 |
info.minLength = temp; |
|
4688 |
||
4689 |
if (maxV & info.maxValid) { |
|
4690 |
temp = info.maxLength * cmax + maxL; |
|
4691 |
info.maxLength = temp; |
|
4692 |
if (temp < maxL) { |
|
4693 |
info.maxValid = false; |
|
4694 |
} |
|
4695 |
} else { |
|
4696 |
info.maxValid = false; |
|
4697 |
} |
|
4698 |
||
4699 |
if (info.deterministic && cmin == cmax) { |
|
4700 |
info.deterministic = detm; |
|
4701 |
} else { |
|
4702 |
info.deterministic = false; |
|
4703 |
} |
|
4704 |
return next.study(info); |
|
4705 |
} |
|
4706 |
} |
|
4707 |
||
4708 |
/** |
|
4709 |
* A Guard node at the end of each atom node in a Branch. It |
|
4710 |
* serves the purpose of chaining the "match" operation to |
|
4711 |
* "next" but not the "study", so we can collect the TreeInfo |
|
4712 |
* of each atom node without including the TreeInfo of the |
|
4713 |
* "next". |
|
4714 |
*/ |
|
4715 |
static final class BranchConn extends Node { |
|
55220 | 4716 |
BranchConn() {} |
2 | 4717 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
4718 |
return next.match(matcher, i, seq); |
|
4719 |
} |
|
4720 |
boolean study(TreeInfo info) { |
|
4721 |
return info.deterministic; |
|
4722 |
} |
|
4723 |
} |
|
4724 |
||
4725 |
/** |
|
4726 |
* Handles the branching of alternations. Note this is also used for |
|
4727 |
* the ? quantifier to branch between the case where it matches once |
|
4728 |
* and where it does not occur. |
|
4729 |
*/ |
|
4730 |
static final class Branch extends Node { |
|
4731 |
Node[] atoms = new Node[2]; |
|
4732 |
int size = 2; |
|
4733 |
Node conn; |
|
4734 |
Branch(Node first, Node second, Node branchConn) { |
|
4735 |
conn = branchConn; |
|
4736 |
atoms[0] = first; |
|
4737 |
atoms[1] = second; |
|
4738 |
} |
|
4739 |
||
4740 |
void add(Node node) { |
|
4741 |
if (size >= atoms.length) { |
|
4742 |
Node[] tmp = new Node[atoms.length*2]; |
|
4743 |
System.arraycopy(atoms, 0, tmp, 0, atoms.length); |
|
4744 |
atoms = tmp; |
|
4745 |
} |
|
4746 |
atoms[size++] = node; |
|
4747 |
} |
|
4748 |
||
4749 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4750 |
for (int n = 0; n < size; n++) { |
|
4751 |
if (atoms[n] == null) { |
|
4752 |
if (conn.next.match(matcher, i, seq)) |
|
4753 |
return true; |
|
4754 |
} else if (atoms[n].match(matcher, i, seq)) { |
|
4755 |
return true; |
|
4756 |
} |
|
4757 |
} |
|
4758 |
return false; |
|
4759 |
} |
|
4760 |
||
4761 |
boolean study(TreeInfo info) { |
|
4762 |
int minL = info.minLength; |
|
4763 |
int maxL = info.maxLength; |
|
4764 |
boolean maxV = info.maxValid; |
|
4765 |
||
4766 |
int minL2 = Integer.MAX_VALUE; //arbitrary large enough num |
|
4767 |
int maxL2 = -1; |
|
4768 |
for (int n = 0; n < size; n++) { |
|
4769 |
info.reset(); |
|
4770 |
if (atoms[n] != null) |
|
4771 |
atoms[n].study(info); |
|
4772 |
minL2 = Math.min(minL2, info.minLength); |
|
4773 |
maxL2 = Math.max(maxL2, info.maxLength); |
|
4774 |
maxV = (maxV & info.maxValid); |
|
4775 |
} |
|
4776 |
||
4777 |
minL += minL2; |
|
4778 |
maxL += maxL2; |
|
4779 |
||
4780 |
info.reset(); |
|
4781 |
conn.next.study(info); |
|
4782 |
||
4783 |
info.minLength += minL; |
|
4784 |
info.maxLength += maxL; |
|
4785 |
info.maxValid &= maxV; |
|
4786 |
info.deterministic = false; |
|
4787 |
return false; |
|
4788 |
} |
|
4789 |
} |
|
4790 |
||
4791 |
/** |
|
4792 |
* The GroupHead saves the location where the group begins in the locals |
|
4793 |
* and restores them when the match is done. |
|
4794 |
* |
|
4795 |
* The matchRef is used when a reference to this group is accessed later |
|
4796 |
* in the expression. The locals will have a negative value in them to |
|
4797 |
* indicate that we do not want to unset the group if the reference |
|
4798 |
* doesn't match. |
|
4799 |
*/ |
|
4800 |
static final class GroupHead extends Node { |
|
4801 |
int localIndex; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4802 |
GroupTail tail; // for debug/print only, match does not need to know |
2 | 4803 |
GroupHead(int localCount) { |
4804 |
localIndex = localCount; |
|
4805 |
} |
|
4806 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4807 |
int save = matcher.locals[localIndex]; |
|
4808 |
matcher.locals[localIndex] = i; |
|
4809 |
boolean ret = next.match(matcher, i, seq); |
|
4810 |
matcher.locals[localIndex] = save; |
|
4811 |
return ret; |
|
4812 |
} |
|
4813 |
} |
|
4814 |
||
4815 |
/** |
|
4816 |
* The GroupTail handles the setting of group beginning and ending |
|
4817 |
* locations when groups are successfully matched. It must also be able to |
|
4818 |
* unset groups that have to be backed off of. |
|
4819 |
* |
|
4820 |
* The GroupTail node is also used when a previous group is referenced, |
|
4821 |
* and in that case no group information needs to be set. |
|
4822 |
*/ |
|
4823 |
static final class GroupTail extends Node { |
|
4824 |
int localIndex; |
|
4825 |
int groupIndex; |
|
4826 |
GroupTail(int localCount, int groupCount) { |
|
4827 |
localIndex = localCount; |
|
4828 |
groupIndex = groupCount + groupCount; |
|
4829 |
} |
|
4830 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4831 |
int tmp = matcher.locals[localIndex]; |
|
4832 |
if (tmp >= 0) { // This is the normal group case. |
|
4833 |
// Save the group so we can unset it if it |
|
4834 |
// backs off of a match. |
|
4835 |
int groupStart = matcher.groups[groupIndex]; |
|
4836 |
int groupEnd = matcher.groups[groupIndex+1]; |
|
4837 |
||
4838 |
matcher.groups[groupIndex] = tmp; |
|
4839 |
matcher.groups[groupIndex+1] = i; |
|
4840 |
if (next.match(matcher, i, seq)) { |
|
4841 |
return true; |
|
4842 |
} |
|
4843 |
matcher.groups[groupIndex] = groupStart; |
|
4844 |
matcher.groups[groupIndex+1] = groupEnd; |
|
4845 |
return false; |
|
4846 |
} else { |
|
4847 |
// This is a group reference case. We don't need to save any |
|
4848 |
// group info because it isn't really a group. |
|
4849 |
matcher.last = i; |
|
4850 |
return true; |
|
4851 |
} |
|
4852 |
} |
|
4853 |
} |
|
4854 |
||
4855 |
/** |
|
4856 |
* This sets up a loop to handle a recursive quantifier structure. |
|
4857 |
*/ |
|
4858 |
static final class Prolog extends Node { |
|
4859 |
Loop loop; |
|
4860 |
Prolog(Loop loop) { |
|
4861 |
this.loop = loop; |
|
4862 |
} |
|
4863 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4864 |
return loop.matchInit(matcher, i, seq); |
|
4865 |
} |
|
4866 |
boolean study(TreeInfo info) { |
|
4867 |
return loop.study(info); |
|
4868 |
} |
|
4869 |
} |
|
4870 |
||
4871 |
/** |
|
4872 |
* Handles the repetition count for a greedy Curly. The matchInit |
|
4873 |
* is called from the Prolog to save the index of where the group |
|
4874 |
* beginning is stored. A zero length group check occurs in the |
|
4875 |
* normal match but is skipped in the matchInit. |
|
4876 |
*/ |
|
4877 |
static class Loop extends Node { |
|
4878 |
Node body; |
|
4879 |
int countIndex; // local count index in matcher locals |
|
4880 |
int beginIndex; // group beginning index |
|
4881 |
int cmin, cmax; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4882 |
int posIndex; |
2 | 4883 |
Loop(int countIndex, int beginIndex) { |
4884 |
this.countIndex = countIndex; |
|
4885 |
this.beginIndex = beginIndex; |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4886 |
this.posIndex = -1; |
2 | 4887 |
} |
4888 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4889 |
// Avoid infinite loop in zero-length case. |
|
4890 |
if (i > matcher.locals[beginIndex]) { |
|
4891 |
int count = matcher.locals[countIndex]; |
|
4892 |
||
4893 |
// This block is for before we reach the minimum |
|
4894 |
// iterations required for the loop to match |
|
4895 |
if (count < cmin) { |
|
4896 |
matcher.locals[countIndex] = count + 1; |
|
4897 |
boolean b = body.match(matcher, i, seq); |
|
4898 |
// If match failed we must backtrack, so |
|
4899 |
// the loop count should NOT be incremented |
|
4900 |
if (!b) |
|
4901 |
matcher.locals[countIndex] = count; |
|
4902 |
// Return success or failure since we are under |
|
4903 |
// minimum |
|
4904 |
return b; |
|
4905 |
} |
|
4906 |
// This block is for after we have the minimum |
|
4907 |
// iterations required for the loop to match |
|
4908 |
if (count < cmax) { |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4909 |
// Let's check if we have already tried and failed |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4910 |
// at this starting position "i" in the past. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4911 |
// If yes, then just return false wihtout trying |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4912 |
// again, to stop the exponential backtracking. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4913 |
if (posIndex != -1 && |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4914 |
matcher.localsPos[posIndex].contains(i)) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4915 |
return next.match(matcher, i, seq); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4916 |
} |
2 | 4917 |
matcher.locals[countIndex] = count + 1; |
4918 |
boolean b = body.match(matcher, i, seq); |
|
4919 |
// If match failed we must backtrack, so |
|
4920 |
// the loop count should NOT be incremented |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4921 |
if (b) |
2 | 4922 |
return true; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4923 |
matcher.locals[countIndex] = count; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4924 |
// save the failed position |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4925 |
if (posIndex != -1) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4926 |
matcher.localsPos[posIndex].add(i); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4927 |
} |
2 | 4928 |
} |
4929 |
} |
|
4930 |
return next.match(matcher, i, seq); |
|
4931 |
} |
|
4932 |
boolean matchInit(Matcher matcher, int i, CharSequence seq) { |
|
4933 |
int save = matcher.locals[countIndex]; |
|
55220 | 4934 |
boolean ret; |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4935 |
if (posIndex != -1 && matcher.localsPos[posIndex] == null) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4936 |
matcher.localsPos[posIndex] = new IntHashSet(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
4937 |
} |
2 | 4938 |
if (0 < cmin) { |
4939 |
matcher.locals[countIndex] = 1; |
|
4940 |
ret = body.match(matcher, i, seq); |
|
4941 |
} else if (0 < cmax) { |
|
4942 |
matcher.locals[countIndex] = 1; |
|
4943 |
ret = body.match(matcher, i, seq); |
|
4944 |
if (ret == false) |
|
4945 |
ret = next.match(matcher, i, seq); |
|
4946 |
} else { |
|
4947 |
ret = next.match(matcher, i, seq); |
|
4948 |
} |
|
4949 |
matcher.locals[countIndex] = save; |
|
4950 |
return ret; |
|
4951 |
} |
|
4952 |
boolean study(TreeInfo info) { |
|
4953 |
info.maxValid = false; |
|
4954 |
info.deterministic = false; |
|
4955 |
return false; |
|
4956 |
} |
|
4957 |
} |
|
4958 |
||
4959 |
/** |
|
4960 |
* Handles the repetition count for a reluctant Curly. The matchInit |
|
4961 |
* is called from the Prolog to save the index of where the group |
|
4962 |
* beginning is stored. A zero length group check occurs in the |
|
4963 |
* normal match but is skipped in the matchInit. |
|
4964 |
*/ |
|
4965 |
static final class LazyLoop extends Loop { |
|
4966 |
LazyLoop(int countIndex, int beginIndex) { |
|
4967 |
super(countIndex, beginIndex); |
|
4968 |
} |
|
4969 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
4970 |
// Check for zero length group |
|
4971 |
if (i > matcher.locals[beginIndex]) { |
|
4972 |
int count = matcher.locals[countIndex]; |
|
4973 |
if (count < cmin) { |
|
4974 |
matcher.locals[countIndex] = count + 1; |
|
4975 |
boolean result = body.match(matcher, i, seq); |
|
4976 |
// If match failed we must backtrack, so |
|
4977 |
// the loop count should NOT be incremented |
|
4978 |
if (!result) |
|
4979 |
matcher.locals[countIndex] = count; |
|
4980 |
return result; |
|
4981 |
} |
|
4982 |
if (next.match(matcher, i, seq)) |
|
4983 |
return true; |
|
4984 |
if (count < cmax) { |
|
4985 |
matcher.locals[countIndex] = count + 1; |
|
4986 |
boolean result = body.match(matcher, i, seq); |
|
4987 |
// If match failed we must backtrack, so |
|
4988 |
// the loop count should NOT be incremented |
|
4989 |
if (!result) |
|
4990 |
matcher.locals[countIndex] = count; |
|
4991 |
return result; |
|
4992 |
} |
|
4993 |
return false; |
|
4994 |
} |
|
4995 |
return next.match(matcher, i, seq); |
|
4996 |
} |
|
4997 |
boolean matchInit(Matcher matcher, int i, CharSequence seq) { |
|
4998 |
int save = matcher.locals[countIndex]; |
|
4999 |
boolean ret = false; |
|
5000 |
if (0 < cmin) { |
|
5001 |
matcher.locals[countIndex] = 1; |
|
5002 |
ret = body.match(matcher, i, seq); |
|
5003 |
} else if (next.match(matcher, i, seq)) { |
|
5004 |
ret = true; |
|
5005 |
} else if (0 < cmax) { |
|
5006 |
matcher.locals[countIndex] = 1; |
|
5007 |
ret = body.match(matcher, i, seq); |
|
5008 |
} |
|
5009 |
matcher.locals[countIndex] = save; |
|
5010 |
return ret; |
|
5011 |
} |
|
5012 |
boolean study(TreeInfo info) { |
|
5013 |
info.maxValid = false; |
|
5014 |
info.deterministic = false; |
|
5015 |
return false; |
|
5016 |
} |
|
5017 |
} |
|
5018 |
||
5019 |
/** |
|
5020 |
* Refers to a group in the regular expression. Attempts to match |
|
5021 |
* whatever the group referred to last matched. |
|
5022 |
*/ |
|
5023 |
static class BackRef extends Node { |
|
5024 |
int groupIndex; |
|
5025 |
BackRef(int groupCount) { |
|
5026 |
super(); |
|
5027 |
groupIndex = groupCount + groupCount; |
|
5028 |
} |
|
5029 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5030 |
int j = matcher.groups[groupIndex]; |
|
5031 |
int k = matcher.groups[groupIndex+1]; |
|
5032 |
||
5033 |
int groupSize = k - j; |
|
5034 |
// If the referenced group didn't match, neither can this |
|
5035 |
if (j < 0) |
|
5036 |
return false; |
|
5037 |
||
5038 |
// If there isn't enough input left no match |
|
5039 |
if (i + groupSize > matcher.to) { |
|
5040 |
matcher.hitEnd = true; |
|
5041 |
return false; |
|
5042 |
} |
|
5043 |
// Check each new char to make sure it matches what the group |
|
5044 |
// referenced matched last time around |
|
5045 |
for (int index=0; index<groupSize; index++) |
|
5046 |
if (seq.charAt(i+index) != seq.charAt(j+index)) |
|
5047 |
return false; |
|
5048 |
||
5049 |
return next.match(matcher, i+groupSize, seq); |
|
5050 |
} |
|
5051 |
boolean study(TreeInfo info) { |
|
5052 |
info.maxValid = false; |
|
5053 |
return next.study(info); |
|
5054 |
} |
|
5055 |
} |
|
5056 |
||
5057 |
static class CIBackRef extends Node { |
|
5058 |
int groupIndex; |
|
5059 |
boolean doUnicodeCase; |
|
5060 |
CIBackRef(int groupCount, boolean doUnicodeCase) { |
|
5061 |
super(); |
|
5062 |
groupIndex = groupCount + groupCount; |
|
5063 |
this.doUnicodeCase = doUnicodeCase; |
|
5064 |
} |
|
5065 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5066 |
int j = matcher.groups[groupIndex]; |
|
5067 |
int k = matcher.groups[groupIndex+1]; |
|
5068 |
||
5069 |
int groupSize = k - j; |
|
5070 |
||
5071 |
// If the referenced group didn't match, neither can this |
|
5072 |
if (j < 0) |
|
5073 |
return false; |
|
5074 |
||
5075 |
// If there isn't enough input left no match |
|
5076 |
if (i + groupSize > matcher.to) { |
|
5077 |
matcher.hitEnd = true; |
|
5078 |
return false; |
|
5079 |
} |
|
5080 |
||
5081 |
// Check each new char to make sure it matches what the group |
|
5082 |
// referenced matched last time around |
|
5083 |
int x = i; |
|
5084 |
for (int index=0; index<groupSize; index++) { |
|
5085 |
int c1 = Character.codePointAt(seq, x); |
|
5086 |
int c2 = Character.codePointAt(seq, j); |
|
5087 |
if (c1 != c2) { |
|
5088 |
if (doUnicodeCase) { |
|
5089 |
int cc1 = Character.toUpperCase(c1); |
|
5090 |
int cc2 = Character.toUpperCase(c2); |
|
5091 |
if (cc1 != cc2 && |
|
5092 |
Character.toLowerCase(cc1) != |
|
5093 |
Character.toLowerCase(cc2)) |
|
5094 |
return false; |
|
5095 |
} else { |
|
5096 |
if (ASCII.toLower(c1) != ASCII.toLower(c2)) |
|
5097 |
return false; |
|
5098 |
} |
|
5099 |
} |
|
5100 |
x += Character.charCount(c1); |
|
5101 |
j += Character.charCount(c2); |
|
5102 |
} |
|
5103 |
||
5104 |
return next.match(matcher, i+groupSize, seq); |
|
5105 |
} |
|
5106 |
boolean study(TreeInfo info) { |
|
5107 |
info.maxValid = false; |
|
5108 |
return next.study(info); |
|
5109 |
} |
|
5110 |
} |
|
5111 |
||
5112 |
/** |
|
5113 |
* Searches until the next instance of its atom. This is useful for |
|
5114 |
* finding the atom efficiently without passing an instance of it |
|
5115 |
* (greedy problem) and without a lot of wasted search time (reluctant |
|
5116 |
* problem). |
|
5117 |
*/ |
|
5118 |
static final class First extends Node { |
|
5119 |
Node atom; |
|
5120 |
First(Node node) { |
|
5121 |
this.atom = BnM.optimize(node); |
|
5122 |
} |
|
5123 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5124 |
if (atom instanceof BnM) { |
|
5125 |
return atom.match(matcher, i, seq) |
|
5126 |
&& next.match(matcher, matcher.last, seq); |
|
5127 |
} |
|
5128 |
for (;;) { |
|
5129 |
if (i > matcher.to) { |
|
5130 |
matcher.hitEnd = true; |
|
5131 |
return false; |
|
5132 |
} |
|
5133 |
if (atom.match(matcher, i, seq)) { |
|
5134 |
return next.match(matcher, matcher.last, seq); |
|
5135 |
} |
|
5136 |
i += countChars(seq, i, 1); |
|
5137 |
matcher.first++; |
|
5138 |
} |
|
5139 |
} |
|
5140 |
boolean study(TreeInfo info) { |
|
5141 |
atom.study(info); |
|
5142 |
info.maxValid = false; |
|
5143 |
info.deterministic = false; |
|
5144 |
return next.study(info); |
|
5145 |
} |
|
5146 |
} |
|
5147 |
||
5148 |
/** |
|
5149 |
* Zero width positive lookahead. |
|
5150 |
*/ |
|
5151 |
static final class Pos extends Node { |
|
5152 |
Node cond; |
|
5153 |
Pos(Node cond) { |
|
5154 |
this.cond = cond; |
|
5155 |
} |
|
5156 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5157 |
int savedTo = matcher.to; |
|
55220 | 5158 |
boolean conditionMatched; |
2 | 5159 |
|
5160 |
// Relax transparent region boundaries for lookahead |
|
5161 |
if (matcher.transparentBounds) |
|
5162 |
matcher.to = matcher.getTextLength(); |
|
5163 |
try { |
|
5164 |
conditionMatched = cond.match(matcher, i, seq); |
|
5165 |
} finally { |
|
5166 |
// Reinstate region boundaries |
|
5167 |
matcher.to = savedTo; |
|
5168 |
} |
|
5169 |
return conditionMatched && next.match(matcher, i, seq); |
|
5170 |
} |
|
5171 |
} |
|
5172 |
||
5173 |
/** |
|
5174 |
* Zero width negative lookahead. |
|
5175 |
*/ |
|
5176 |
static final class Neg extends Node { |
|
5177 |
Node cond; |
|
5178 |
Neg(Node cond) { |
|
5179 |
this.cond = cond; |
|
5180 |
} |
|
5181 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5182 |
int savedTo = matcher.to; |
|
55220 | 5183 |
boolean conditionMatched; |
2 | 5184 |
|
5185 |
// Relax transparent region boundaries for lookahead |
|
5186 |
if (matcher.transparentBounds) |
|
5187 |
matcher.to = matcher.getTextLength(); |
|
5188 |
try { |
|
5189 |
if (i < matcher.to) { |
|
5190 |
conditionMatched = !cond.match(matcher, i, seq); |
|
5191 |
} else { |
|
5192 |
// If a negative lookahead succeeds then more input |
|
5193 |
// could cause it to fail! |
|
5194 |
matcher.requireEnd = true; |
|
5195 |
conditionMatched = !cond.match(matcher, i, seq); |
|
5196 |
} |
|
5197 |
} finally { |
|
5198 |
// Reinstate region boundaries |
|
5199 |
matcher.to = savedTo; |
|
5200 |
} |
|
5201 |
return conditionMatched && next.match(matcher, i, seq); |
|
5202 |
} |
|
5203 |
} |
|
5204 |
||
5205 |
/** |
|
5206 |
* For use with lookbehinds; matches the position where the lookbehind |
|
5207 |
* was encountered. |
|
5208 |
*/ |
|
55220 | 5209 |
static class LookBehindEndNode extends Node { |
5210 |
private LookBehindEndNode() {} // Singleton |
|
5211 |
||
5212 |
static LookBehindEndNode INSTANCE = new LookBehindEndNode(); |
|
5213 |
||
2 | 5214 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
5215 |
return i == matcher.lookbehindTo; |
|
5216 |
} |
|
55220 | 5217 |
} |
2 | 5218 |
|
5219 |
/** |
|
5220 |
* Zero width positive lookbehind. |
|
5221 |
*/ |
|
5222 |
static class Behind extends Node { |
|
5223 |
Node cond; |
|
5224 |
int rmax, rmin; |
|
5225 |
Behind(Node cond, int rmax, int rmin) { |
|
5226 |
this.cond = cond; |
|
5227 |
this.rmax = rmax; |
|
5228 |
this.rmin = rmin; |
|
5229 |
} |
|
5230 |
||
5231 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5232 |
int savedFrom = matcher.from; |
|
5233 |
boolean conditionMatched = false; |
|
5234 |
int startIndex = (!matcher.transparentBounds) ? |
|
5235 |
matcher.from : 0; |
|
5236 |
int from = Math.max(i - rmax, startIndex); |
|
5237 |
// Set end boundary |
|
5238 |
int savedLBT = matcher.lookbehindTo; |
|
5239 |
matcher.lookbehindTo = i; |
|
5240 |
// Relax transparent region boundaries for lookbehind |
|
5241 |
if (matcher.transparentBounds) |
|
5242 |
matcher.from = 0; |
|
5243 |
for (int j = i - rmin; !conditionMatched && j >= from; j--) { |
|
5244 |
conditionMatched = cond.match(matcher, j, seq); |
|
5245 |
} |
|
5246 |
matcher.from = savedFrom; |
|
5247 |
matcher.lookbehindTo = savedLBT; |
|
5248 |
return conditionMatched && next.match(matcher, i, seq); |
|
5249 |
} |
|
5250 |
} |
|
5251 |
||
5252 |
/** |
|
5253 |
* Zero width positive lookbehind, including supplementary |
|
5254 |
* characters or unpaired surrogates. |
|
5255 |
*/ |
|
5256 |
static final class BehindS extends Behind { |
|
5257 |
BehindS(Node cond, int rmax, int rmin) { |
|
5258 |
super(cond, rmax, rmin); |
|
5259 |
} |
|
5260 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5261 |
int rmaxChars = countChars(seq, i, -rmax); |
|
5262 |
int rminChars = countChars(seq, i, -rmin); |
|
5263 |
int savedFrom = matcher.from; |
|
5264 |
int startIndex = (!matcher.transparentBounds) ? |
|
5265 |
matcher.from : 0; |
|
5266 |
boolean conditionMatched = false; |
|
5267 |
int from = Math.max(i - rmaxChars, startIndex); |
|
5268 |
// Set end boundary |
|
5269 |
int savedLBT = matcher.lookbehindTo; |
|
5270 |
matcher.lookbehindTo = i; |
|
5271 |
// Relax transparent region boundaries for lookbehind |
|
5272 |
if (matcher.transparentBounds) |
|
5273 |
matcher.from = 0; |
|
5274 |
||
5275 |
for (int j = i - rminChars; |
|
5276 |
!conditionMatched && j >= from; |
|
5277 |
j -= j>from ? countChars(seq, j, -1) : 1) { |
|
5278 |
conditionMatched = cond.match(matcher, j, seq); |
|
5279 |
} |
|
5280 |
matcher.from = savedFrom; |
|
5281 |
matcher.lookbehindTo = savedLBT; |
|
5282 |
return conditionMatched && next.match(matcher, i, seq); |
|
5283 |
} |
|
5284 |
} |
|
5285 |
||
5286 |
/** |
|
5287 |
* Zero width negative lookbehind. |
|
5288 |
*/ |
|
5289 |
static class NotBehind extends Node { |
|
5290 |
Node cond; |
|
5291 |
int rmax, rmin; |
|
5292 |
NotBehind(Node cond, int rmax, int rmin) { |
|
5293 |
this.cond = cond; |
|
5294 |
this.rmax = rmax; |
|
5295 |
this.rmin = rmin; |
|
5296 |
} |
|
5297 |
||
5298 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5299 |
int savedLBT = matcher.lookbehindTo; |
|
5300 |
int savedFrom = matcher.from; |
|
5301 |
boolean conditionMatched = false; |
|
5302 |
int startIndex = (!matcher.transparentBounds) ? |
|
5303 |
matcher.from : 0; |
|
5304 |
int from = Math.max(i - rmax, startIndex); |
|
5305 |
matcher.lookbehindTo = i; |
|
5306 |
// Relax transparent region boundaries for lookbehind |
|
5307 |
if (matcher.transparentBounds) |
|
5308 |
matcher.from = 0; |
|
5309 |
for (int j = i - rmin; !conditionMatched && j >= from; j--) { |
|
5310 |
conditionMatched = cond.match(matcher, j, seq); |
|
5311 |
} |
|
5312 |
// Reinstate region boundaries |
|
5313 |
matcher.from = savedFrom; |
|
5314 |
matcher.lookbehindTo = savedLBT; |
|
5315 |
return !conditionMatched && next.match(matcher, i, seq); |
|
5316 |
} |
|
5317 |
} |
|
5318 |
||
5319 |
/** |
|
5320 |
* Zero width negative lookbehind, including supplementary |
|
5321 |
* characters or unpaired surrogates. |
|
5322 |
*/ |
|
5323 |
static final class NotBehindS extends NotBehind { |
|
5324 |
NotBehindS(Node cond, int rmax, int rmin) { |
|
5325 |
super(cond, rmax, rmin); |
|
5326 |
} |
|
5327 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5328 |
int rmaxChars = countChars(seq, i, -rmax); |
|
5329 |
int rminChars = countChars(seq, i, -rmin); |
|
5330 |
int savedFrom = matcher.from; |
|
5331 |
int savedLBT = matcher.lookbehindTo; |
|
5332 |
boolean conditionMatched = false; |
|
5333 |
int startIndex = (!matcher.transparentBounds) ? |
|
5334 |
matcher.from : 0; |
|
5335 |
int from = Math.max(i - rmaxChars, startIndex); |
|
5336 |
matcher.lookbehindTo = i; |
|
5337 |
// Relax transparent region boundaries for lookbehind |
|
5338 |
if (matcher.transparentBounds) |
|
5339 |
matcher.from = 0; |
|
5340 |
for (int j = i - rminChars; |
|
5341 |
!conditionMatched && j >= from; |
|
5342 |
j -= j>from ? countChars(seq, j, -1) : 1) { |
|
5343 |
conditionMatched = cond.match(matcher, j, seq); |
|
5344 |
} |
|
5345 |
//Reinstate region boundaries |
|
5346 |
matcher.from = savedFrom; |
|
5347 |
matcher.lookbehindTo = savedLBT; |
|
5348 |
return !conditionMatched && next.match(matcher, i, seq); |
|
5349 |
} |
|
5350 |
} |
|
5351 |
||
5352 |
/** |
|
5353 |
* Handles word boundaries. Includes a field to allow this one class to |
|
5354 |
* deal with the different types of word boundaries we can match. The word |
|
5355 |
* characters include underscores, letters, and digits. Non spacing marks |
|
5356 |
* can are also part of a word if they have a base character, otherwise |
|
5357 |
* they are ignored for purposes of finding word boundaries. |
|
5358 |
*/ |
|
5359 |
static final class Bound extends Node { |
|
5360 |
static int LEFT = 0x1; |
|
5361 |
static int RIGHT= 0x2; |
|
5362 |
static int BOTH = 0x3; |
|
5363 |
static int NONE = 0x4; |
|
5364 |
int type; |
|
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5365 |
boolean useUWORD; |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5366 |
Bound(int n, boolean useUWORD) { |
2 | 5367 |
type = n; |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5368 |
this.useUWORD = useUWORD; |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5369 |
} |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5370 |
|
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5371 |
boolean isWord(int ch) { |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5372 |
return useUWORD ? CharPredicates.WORD().is(ch) |
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5373 |
: (ch == '_' || Character.isLetterOrDigit(ch)); |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5374 |
} |
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5375 |
|
2 | 5376 |
int check(Matcher matcher, int i, CharSequence seq) { |
5377 |
int ch; |
|
5378 |
boolean left = false; |
|
5379 |
int startIndex = matcher.from; |
|
5380 |
int endIndex = matcher.to; |
|
5381 |
if (matcher.transparentBounds) { |
|
5382 |
startIndex = 0; |
|
5383 |
endIndex = matcher.getTextLength(); |
|
5384 |
} |
|
5385 |
if (i > startIndex) { |
|
5386 |
ch = Character.codePointBefore(seq, i); |
|
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5387 |
left = (isWord(ch) || |
2 | 5388 |
((Character.getType(ch) == Character.NON_SPACING_MARK) |
5389 |
&& hasBaseCharacter(matcher, i-1, seq))); |
|
5390 |
} |
|
5391 |
boolean right = false; |
|
5392 |
if (i < endIndex) { |
|
5393 |
ch = Character.codePointAt(seq, i); |
|
9536
648c9add2a74
7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents:
9275
diff
changeset
|
5394 |
right = (isWord(ch) || |
2 | 5395 |
((Character.getType(ch) == Character.NON_SPACING_MARK) |
5396 |
&& hasBaseCharacter(matcher, i, seq))); |
|
5397 |
} else { |
|
5398 |
// Tried to access char past the end |
|
5399 |
matcher.hitEnd = true; |
|
5400 |
// The addition of another char could wreck a boundary |
|
5401 |
matcher.requireEnd = true; |
|
5402 |
} |
|
5403 |
return ((left ^ right) ? (right ? LEFT : RIGHT) : NONE); |
|
5404 |
} |
|
5405 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5406 |
return (check(matcher, i, seq) & type) > 0 |
|
5407 |
&& next.match(matcher, i, seq); |
|
5408 |
} |
|
5409 |
} |
|
5410 |
||
5411 |
/** |
|
5412 |
* Non spacing marks only count as word characters in bounds calculations |
|
5413 |
* if they have a base character. |
|
5414 |
*/ |
|
5415 |
private static boolean hasBaseCharacter(Matcher matcher, int i, |
|
5416 |
CharSequence seq) |
|
5417 |
{ |
|
5418 |
int start = (!matcher.transparentBounds) ? |
|
5419 |
matcher.from : 0; |
|
5420 |
for (int x=i; x >= start; x--) { |
|
5421 |
int ch = Character.codePointAt(seq, x); |
|
5422 |
if (Character.isLetterOrDigit(ch)) |
|
5423 |
return true; |
|
5424 |
if (Character.getType(ch) == Character.NON_SPACING_MARK) |
|
5425 |
continue; |
|
5426 |
return false; |
|
5427 |
} |
|
5428 |
return false; |
|
5429 |
} |
|
5430 |
||
5431 |
/** |
|
5432 |
* Attempts to match a slice in the input using the Boyer-Moore string |
|
5433 |
* matching algorithm. The algorithm is based on the idea that the |
|
5434 |
* pattern can be shifted farther ahead in the search text if it is |
|
5435 |
* matched right to left. |
|
5436 |
* <p> |
|
5437 |
* The pattern is compared to the input one character at a time, from |
|
5438 |
* the rightmost character in the pattern to the left. If the characters |
|
5439 |
* all match the pattern has been found. If a character does not match, |
|
5440 |
* the pattern is shifted right a distance that is the maximum of two |
|
5441 |
* functions, the bad character shift and the good suffix shift. This |
|
5442 |
* shift moves the attempted match position through the input more |
|
5443 |
* quickly than a naive one position at a time check. |
|
5444 |
* <p> |
|
5445 |
* The bad character shift is based on the character from the text that |
|
5446 |
* did not match. If the character does not appear in the pattern, the |
|
5447 |
* pattern can be shifted completely beyond the bad character. If the |
|
5448 |
* character does occur in the pattern, the pattern can be shifted to |
|
5449 |
* line the pattern up with the next occurrence of that character. |
|
5450 |
* <p> |
|
5451 |
* The good suffix shift is based on the idea that some subset on the right |
|
5452 |
* side of the pattern has matched. When a bad character is found, the |
|
5453 |
* pattern can be shifted right by the pattern length if the subset does |
|
5454 |
* not occur again in pattern, or by the amount of distance to the |
|
5455 |
* next occurrence of the subset in the pattern. |
|
5456 |
* |
|
5457 |
* Boyer-Moore search methods adapted from code by Amy Yu. |
|
5458 |
*/ |
|
5459 |
static class BnM extends Node { |
|
5460 |
int[] buffer; |
|
5461 |
int[] lastOcc; |
|
5462 |
int[] optoSft; |
|
5463 |
||
5464 |
/** |
|
5465 |
* Pre calculates arrays needed to generate the bad character |
|
5466 |
* shift and the good suffix shift. Only the last seven bits |
|
5467 |
* are used to see if chars match; This keeps the tables small |
|
5468 |
* and covers the heavily used ASCII range, but occasionally |
|
5469 |
* results in an aliased match for the bad character shift. |
|
5470 |
*/ |
|
5471 |
static Node optimize(Node node) { |
|
5472 |
if (!(node instanceof Slice)) { |
|
5473 |
return node; |
|
5474 |
} |
|
5475 |
||
5476 |
int[] src = ((Slice) node).buffer; |
|
5477 |
int patternLength = src.length; |
|
5478 |
// The BM algorithm requires a bit of overhead; |
|
5479 |
// If the pattern is short don't use it, since |
|
5480 |
// a shift larger than the pattern length cannot |
|
5481 |
// be used anyway. |
|
5482 |
if (patternLength < 4) { |
|
5483 |
return node; |
|
5484 |
} |
|
55220 | 5485 |
int i, j; |
2 | 5486 |
int[] lastOcc = new int[128]; |
5487 |
int[] optoSft = new int[patternLength]; |
|
5488 |
// Precalculate part of the bad character shift |
|
5489 |
// It is a table for where in the pattern each |
|
5490 |
// lower 7-bit value occurs |
|
5491 |
for (i = 0; i < patternLength; i++) { |
|
5492 |
lastOcc[src[i]&0x7F] = i + 1; |
|
5493 |
} |
|
5494 |
// Precalculate the good suffix shift |
|
5495 |
// i is the shift amount being considered |
|
5496 |
NEXT: for (i = patternLength; i > 0; i--) { |
|
5497 |
// j is the beginning index of suffix being considered |
|
5498 |
for (j = patternLength - 1; j >= i; j--) { |
|
5499 |
// Testing for good suffix |
|
5500 |
if (src[j] == src[j-i]) { |
|
5501 |
// src[j..len] is a good suffix |
|
5502 |
optoSft[j-1] = i; |
|
5503 |
} else { |
|
5504 |
// No match. The array has already been |
|
5505 |
// filled up with correct values before. |
|
5506 |
continue NEXT; |
|
5507 |
} |
|
5508 |
} |
|
5509 |
// This fills up the remaining of optoSft |
|
5510 |
// any suffix can not have larger shift amount |
|
5511 |
// then its sub-suffix. Why??? |
|
5512 |
while (j > 0) { |
|
5513 |
optoSft[--j] = i; |
|
5514 |
} |
|
5515 |
} |
|
5516 |
// Set the guard value because of unicode compression |
|
5517 |
optoSft[patternLength-1] = 1; |
|
5518 |
if (node instanceof SliceS) |
|
5519 |
return new BnMS(src, lastOcc, optoSft, node.next); |
|
5520 |
return new BnM(src, lastOcc, optoSft, node.next); |
|
5521 |
} |
|
5522 |
BnM(int[] src, int[] lastOcc, int[] optoSft, Node next) { |
|
5523 |
this.buffer = src; |
|
5524 |
this.lastOcc = lastOcc; |
|
5525 |
this.optoSft = optoSft; |
|
5526 |
this.next = next; |
|
5527 |
} |
|
5528 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5529 |
int[] src = buffer; |
|
5530 |
int patternLength = src.length; |
|
5531 |
int last = matcher.to - patternLength; |
|
5532 |
||
5533 |
// Loop over all possible match positions in text |
|
5534 |
NEXT: while (i <= last) { |
|
5535 |
// Loop over pattern from right to left |
|
5536 |
for (int j = patternLength - 1; j >= 0; j--) { |
|
5537 |
int ch = seq.charAt(i+j); |
|
5538 |
if (ch != src[j]) { |
|
5539 |
// Shift search to the right by the maximum of the |
|
5540 |
// bad character shift and the good suffix shift |
|
5541 |
i += Math.max(j + 1 - lastOcc[ch&0x7F], optoSft[j]); |
|
5542 |
continue NEXT; |
|
5543 |
} |
|
5544 |
} |
|
5545 |
// Entire pattern matched starting at i |
|
5546 |
matcher.first = i; |
|
5547 |
boolean ret = next.match(matcher, i + patternLength, seq); |
|
5548 |
if (ret) { |
|
5549 |
matcher.first = i; |
|
5550 |
matcher.groups[0] = matcher.first; |
|
5551 |
matcher.groups[1] = matcher.last; |
|
5552 |
return true; |
|
5553 |
} |
|
5554 |
i++; |
|
5555 |
} |
|
5556 |
// BnM is only used as the leading node in the unanchored case, |
|
5557 |
// and it replaced its Start() which always searches to the end |
|
5558 |
// if it doesn't find what it's looking for, so hitEnd is true. |
|
5559 |
matcher.hitEnd = true; |
|
5560 |
return false; |
|
5561 |
} |
|
5562 |
boolean study(TreeInfo info) { |
|
5563 |
info.minLength += buffer.length; |
|
5564 |
info.maxValid = false; |
|
5565 |
return next.study(info); |
|
5566 |
} |
|
5567 |
} |
|
5568 |
||
5569 |
/** |
|
5570 |
* Supplementary support version of BnM(). Unpaired surrogates are |
|
5571 |
* also handled by this class. |
|
5572 |
*/ |
|
5573 |
static final class BnMS extends BnM { |
|
5574 |
int lengthInChars; |
|
5575 |
||
5576 |
BnMS(int[] src, int[] lastOcc, int[] optoSft, Node next) { |
|
5577 |
super(src, lastOcc, optoSft, next); |
|
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
5578 |
for (int cp : buffer) { |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
21670
diff
changeset
|
5579 |
lengthInChars += Character.charCount(cp); |
2 | 5580 |
} |
5581 |
} |
|
5582 |
boolean match(Matcher matcher, int i, CharSequence seq) { |
|
5583 |
int[] src = buffer; |
|
5584 |
int patternLength = src.length; |
|
5585 |
int last = matcher.to - lengthInChars; |
|
5586 |
||
5587 |
// Loop over all possible match positions in text |
|
5588 |
NEXT: while (i <= last) { |
|
5589 |
// Loop over pattern from right to left |
|
5590 |
int ch; |
|
5591 |
for (int j = countChars(seq, i, patternLength), x = patternLength - 1; |
|
5592 |
j > 0; j -= Character.charCount(ch), x--) { |
|
5593 |
ch = Character.codePointBefore(seq, i+j); |
|
5594 |
if (ch != src[x]) { |
|
5595 |
// Shift search to the right by the maximum of the |
|
5596 |
// bad character shift and the good suffix shift |
|
5597 |
int n = Math.max(x + 1 - lastOcc[ch&0x7F], optoSft[x]); |
|
5598 |
i += countChars(seq, i, n); |
|
5599 |
continue NEXT; |
|
5600 |
} |
|
5601 |
} |
|
5602 |
// Entire pattern matched starting at i |
|
5603 |
matcher.first = i; |
|
5604 |
boolean ret = next.match(matcher, i + lengthInChars, seq); |
|
5605 |
if (ret) { |
|
5606 |
matcher.first = i; |
|
5607 |
matcher.groups[0] = matcher.first; |
|
5608 |
matcher.groups[1] = matcher.last; |
|
5609 |
return true; |
|
5610 |
} |
|
5611 |
i += countChars(seq, i, 1); |
|
5612 |
} |
|
5613 |
matcher.hitEnd = true; |
|
5614 |
return false; |
|
5615 |
} |
|
5616 |
} |
|
5617 |
||
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5618 |
@FunctionalInterface |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5619 |
static interface CharPredicate { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5620 |
boolean is(int ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5621 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5622 |
default CharPredicate and(CharPredicate p) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5623 |
return ch -> is(ch) && p.is(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5624 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5625 |
default CharPredicate union(CharPredicate p) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5626 |
return ch -> is(ch) || p.is(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5627 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5628 |
default CharPredicate union(CharPredicate p1, |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5629 |
CharPredicate p2 ) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5630 |
return ch -> is(ch) || p1.is(ch) || p2.is(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5631 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5632 |
default CharPredicate negate() { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5633 |
return ch -> !is(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5634 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5635 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5636 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5637 |
static interface BmpCharPredicate extends CharPredicate { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5638 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5639 |
default CharPredicate and(CharPredicate p) { |
55220 | 5640 |
if (p instanceof BmpCharPredicate) |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5641 |
return (BmpCharPredicate)(ch -> is(ch) && p.is(ch)); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5642 |
return ch -> is(ch) && p.is(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5643 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5644 |
default CharPredicate union(CharPredicate p) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5645 |
if (p instanceof BmpCharPredicate) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5646 |
return (BmpCharPredicate)(ch -> is(ch) || p.is(ch)); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5647 |
return ch -> is(ch) || p.is(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5648 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5649 |
static CharPredicate union(CharPredicate... predicates) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5650 |
CharPredicate cp = ch -> { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5651 |
for (CharPredicate p : predicates) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5652 |
if (!p.is(ch)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5653 |
return false; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5654 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5655 |
return true; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5656 |
}; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5657 |
for (CharPredicate p : predicates) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5658 |
if (! (p instanceof BmpCharPredicate)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5659 |
return cp; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5660 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5661 |
return (BmpCharPredicate)cp; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5662 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5663 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5664 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5665 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5666 |
* matches a Perl vertical whitespace |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5667 |
*/ |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5668 |
static BmpCharPredicate VertWS() { |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5669 |
return cp -> (cp >= 0x0A && cp <= 0x0D) || |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5670 |
cp == 0x85 || cp == 0x2028 || cp == 0x2029; |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5671 |
} |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5672 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5673 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5674 |
* matches a Perl horizontal whitespace |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5675 |
*/ |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5676 |
static BmpCharPredicate HorizWS() { |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5677 |
return cp -> |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5678 |
cp == 0x09 || cp == 0x20 || cp == 0xa0 || cp == 0x1680 || |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5679 |
cp == 0x180e || cp >= 0x2000 && cp <= 0x200a || cp == 0x202f || |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5680 |
cp == 0x205f || cp == 0x3000; |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5681 |
} |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5682 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5683 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5684 |
* for the Unicode category ALL and the dot metacharacter when |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5685 |
* in dotall mode. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5686 |
*/ |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5687 |
static CharPredicate ALL() { |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5688 |
return ch -> true; |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5689 |
} |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5690 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5691 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5692 |
* for the dot metacharacter when dotall is not enabled. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5693 |
*/ |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5694 |
static CharPredicate DOT() { |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5695 |
return ch -> |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5696 |
(ch != '\n' && ch != '\r' |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5697 |
&& (ch|1) != '\u2029' |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5698 |
&& ch != '\u0085'); |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5699 |
} |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5700 |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5701 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5702 |
* the dot metacharacter when dotall is not enabled but UNIX_LINES is enabled. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5703 |
*/ |
43502
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5704 |
static CharPredicate UNIXDOT() { |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5705 |
return ch -> ch != '\n'; |
aec39566b45e
8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents:
38777
diff
changeset
|
5706 |
} |
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5707 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5708 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5709 |
* Indicate that matches a Supplementary Unicode character |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5710 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5711 |
static CharPredicate SingleS(int c) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5712 |
return ch -> ch == c; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5713 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5714 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5715 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5716 |
* A bmp/optimized predicate of single |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5717 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5718 |
static BmpCharPredicate Single(int c) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5719 |
return ch -> ch == c; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5720 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5721 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5722 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5723 |
* Case insensitive matches a given BMP character |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5724 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5725 |
static BmpCharPredicate SingleI(int lower, int upper) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5726 |
return ch -> ch == lower || ch == upper; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5727 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5728 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5729 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5730 |
* Unicode case insensitive matches a given Unicode character |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5731 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5732 |
static CharPredicate SingleU(int lower) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5733 |
return ch -> lower == ch || |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5734 |
lower == Character.toLowerCase(Character.toUpperCase(ch)); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5735 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5736 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5737 |
private static boolean inRange(int lower, int ch, int upper) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5738 |
return lower <= ch && ch <= upper; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5739 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5740 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5741 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5742 |
* Charactrs within a explicit value range |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5743 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5744 |
static CharPredicate Range(int lower, int upper) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5745 |
if (upper < Character.MIN_HIGH_SURROGATE || |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5746 |
lower > Character.MAX_HIGH_SURROGATE && |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5747 |
upper < Character.MIN_SUPPLEMENTARY_CODE_POINT) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5748 |
return (BmpCharPredicate)(ch -> inRange(lower, ch, upper)); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5749 |
return ch -> inRange(lower, ch, upper); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5750 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5751 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5752 |
/** |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5753 |
* Charactrs within a explicit value range in a case insensitive manner. |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5754 |
*/ |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5755 |
static CharPredicate CIRange(int lower, int upper) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5756 |
return ch -> inRange(lower, ch, upper) || |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5757 |
ASCII.isAscii(ch) && |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5758 |
(inRange(lower, ASCII.toUpper(ch), upper) || |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5759 |
inRange(lower, ASCII.toLower(ch), upper)); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5760 |
} |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5761 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5762 |
static CharPredicate CIRangeU(int lower, int upper) { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5763 |
return ch -> { |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5764 |
if (inRange(lower, ch, upper)) |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5765 |
return true; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5766 |
int up = Character.toUpperCase(ch); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5767 |
return inRange(lower, up, upper) || |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5768 |
inRange(lower, Character.toLowerCase(up), upper); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5769 |
}; |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5770 |
} |
2 | 5771 |
|
5772 |
/** |
|
5773 |
* This must be the very first initializer. |
|
5774 |
*/ |
|
37882
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5775 |
static final Node accept = new Node(); |
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5776 |
|
e7f3cf12e739
6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents:
37880
diff
changeset
|
5777 |
static final Node lastAccept = new LastNode(); |
17447 | 5778 |
|
5779 |
/** |
|
49554
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5780 |
* Creates a predicate that tests if this pattern is found in a given input |
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5781 |
* string. |
17447 | 5782 |
* |
49554
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5783 |
* @apiNote |
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5784 |
* This method creates a predicate that behaves as if it creates a matcher |
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5785 |
* from the input sequence and then calls {@code find}, for example a |
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5786 |
* predicate of the form: |
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5787 |
* <pre>{@code |
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5788 |
* s -> matcher(s).find(); |
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5789 |
* }</pre> |
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5790 |
* |
f088ec60bed5
8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents:
48853
diff
changeset
|
5791 |
* @return The predicate which can be used for finding a match on a |
49763 | 5792 |
* subsequence of a string |
17447 | 5793 |
* @since 1.8 |
49763 | 5794 |
* @see Matcher#find |
17447 | 5795 |
*/ |
5796 |
public Predicate<String> asPredicate() { |
|
5797 |
return s -> matcher(s).find(); |
|
5798 |
} |
|
5799 |
||
5800 |
/** |
|
49763 | 5801 |
* Creates a predicate that tests if this pattern matches a given input string. |
5802 |
* |
|
5803 |
* @apiNote |
|
5804 |
* This method creates a predicate that behaves as if it creates a matcher |
|
5805 |
* from the input sequence and then calls {@code matches}, for example a |
|
5806 |
* predicate of the form: |
|
5807 |
* <pre>{@code |
|
5808 |
* s -> matcher(s).matches(); |
|
5809 |
* }</pre> |
|
5810 |
* |
|
5811 |
* @return The predicate which can be used for matching an input string |
|
5812 |
* against this pattern. |
|
5813 |
* @since 11 |
|
5814 |
* @see Matcher#matches |
|
5815 |
*/ |
|
5816 |
public Predicate<String> asMatchPredicate() { |
|
5817 |
return s -> matcher(s).matches(); |
|
5818 |
} |
|
5819 |
||
5820 |
/** |
|
17447 | 5821 |
* Creates a stream from the given input sequence around matches of this |
5822 |
* pattern. |
|
5823 |
* |
|
5824 |
* <p> The stream returned by this method contains each substring of the |
|
5825 |
* input sequence that is terminated by another subsequence that matches |
|
5826 |
* this pattern or is terminated by the end of the input sequence. The |
|
5827 |
* substrings in the stream are in the order in which they occur in the |
|
21670
ca3553133ede
8028321: Fix for String.split() empty input sequence/JDK-6559590 triggers regression
sherman
parents:
21668
diff
changeset
|
5828 |
* input. Trailing empty strings will be discarded and not encountered in |
20190
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5829 |
* the stream. |
17447 | 5830 |
* |
5831 |
* <p> If this pattern does not match any subsequence of the input then |
|
5832 |
* the resulting stream has just one element, namely the input sequence in |
|
5833 |
* string form. |
|
5834 |
* |
|
21668 | 5835 |
* <p> When there is a positive-width match at the beginning of the input |
5836 |
* sequence then an empty leading substring is included at the beginning |
|
5837 |
* of the stream. A zero-width match at the beginning however never produces |
|
5838 |
* such empty leading substring. |
|
5839 |
* |
|
17447 | 5840 |
* <p> If the input sequence is mutable, it must remain constant during the |
5841 |
* execution of the terminal stream operation. Otherwise, the result of the |
|
5842 |
* terminal stream operation is undefined. |
|
5843 |
* |
|
5844 |
* @param input |
|
5845 |
* The character sequence to be split |
|
5846 |
* |
|
5847 |
* @return The stream of strings computed by splitting the input |
|
5848 |
* around matches of this pattern |
|
5849 |
* @see #split(CharSequence) |
|
5850 |
* @since 1.8 |
|
5851 |
*/ |
|
5852 |
public Stream<String> splitAsStream(final CharSequence input) { |
|
5853 |
class MatcherIterator implements Iterator<String> { |
|
34684
b721350c05c0
8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents:
32649
diff
changeset
|
5854 |
private Matcher matcher; |
17447 | 5855 |
// The start position of the next sub-sequence of input |
5856 |
// when current == input.length there are no more elements |
|
5857 |
private int current; |
|
5858 |
// null if the next element, if any, needs to obtained |
|
5859 |
private String nextElement; |
|
20190
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5860 |
// > 0 if there are N next empty elements |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5861 |
private int emptyElementCount; |
17447 | 5862 |
|
5863 |
public String next() { |
|
5864 |
if (!hasNext()) |
|
5865 |
throw new NoSuchElementException(); |
|
5866 |
||
20190
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5867 |
if (emptyElementCount == 0) { |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5868 |
String n = nextElement; |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5869 |
nextElement = null; |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5870 |
return n; |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5871 |
} else { |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5872 |
emptyElementCount--; |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5873 |
return ""; |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5874 |
} |
17447 | 5875 |
} |
5876 |
||
5877 |
public boolean hasNext() { |
|
34684
b721350c05c0
8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents:
32649
diff
changeset
|
5878 |
if (matcher == null) { |
b721350c05c0
8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents:
32649
diff
changeset
|
5879 |
matcher = matcher(input); |
b721350c05c0
8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents:
32649
diff
changeset
|
5880 |
// If the input is an empty string then the result can only be a |
b721350c05c0
8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents:
32649
diff
changeset
|
5881 |
// stream of the input. Induce that by setting the empty |
b721350c05c0
8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents:
32649
diff
changeset
|
5882 |
// element count to 1 |
b721350c05c0
8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents:
32649
diff
changeset
|
5883 |
emptyElementCount = input.length() == 0 ? 1 : 0; |
b721350c05c0
8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents:
32649
diff
changeset
|
5884 |
} |
20190
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5885 |
if (nextElement != null || emptyElementCount > 0) |
17447 | 5886 |
return true; |
5887 |
||
5888 |
if (current == input.length()) |
|
5889 |
return false; |
|
5890 |
||
20190
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5891 |
// Consume the next matching element |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5892 |
// Count sequence of matching empty elements |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5893 |
while (matcher.find()) { |
17447 | 5894 |
nextElement = input.subSequence(current, matcher.start()).toString(); |
5895 |
current = matcher.end(); |
|
20190
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5896 |
if (!nextElement.isEmpty()) { |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5897 |
return true; |
21668 | 5898 |
} else if (current > 0) { // no empty leading substring for zero-width |
5899 |
// match at the beginning of the input |
|
20190
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5900 |
emptyElementCount++; |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5901 |
} |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5902 |
} |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5903 |
|
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5904 |
// Consume last matching element |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5905 |
nextElement = input.subSequence(current, input.length()).toString(); |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5906 |
current = input.length(); |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5907 |
if (!nextElement.isEmpty()) { |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5908 |
return true; |
17447 | 5909 |
} else { |
20190
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5910 |
// Ignore a terminal sequence of matching empty elements |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5911 |
emptyElementCount = 0; |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5912 |
nextElement = null; |
15c72885e3fd
8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
psandoz
parents:
19604
diff
changeset
|
5913 |
return false; |
17447 | 5914 |
} |
5915 |
} |
|
5916 |
} |
|
5917 |
return StreamSupport.stream(Spliterators.spliteratorUnknownSize( |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18787
diff
changeset
|
5918 |
new MatcherIterator(), Spliterator.ORDERED | Spliterator.NONNULL), false); |
17447 | 5919 |
} |
2 | 5920 |
} |