src/java.base/share/classes/java/util/regex/Pattern.java
author igerasim
Tue, 21 May 2019 18:40:29 -0700
changeset 54971 4285b4d13471
parent 54714 37630ad8fa67
child 55013 8dae495a59e7
permissions -rw-r--r--
8223593: Refactor code for reallocating storage Reviewed-by: prappo, plevart, rriggs, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
     2
 * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4820
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4820
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4820
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4820
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4820
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.regex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Arrays;
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
    39
import java.util.NoSuchElementException;
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
    40
import java.util.Spliterator;
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
    41
import java.util.Spliterators;
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
    42
import java.util.function.Predicate;
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
    43
import java.util.stream.Stream;
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
    44
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 54714
diff changeset
    46
import jdk.internal.util.ArraysSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * A compiled representation of a regular expression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p> A regular expression, specified as a string, must first be compiled into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * an instance of this class.  The resulting pattern can then be used to create
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
    53
 * a {@link Matcher} object that can match arbitrary {@linkplain
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
    54
 * java.lang.CharSequence character sequences} against the regular
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * expression.  All of the state involved in performing a match resides in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * matcher, so many matchers can share the same pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p> A typical invocation sequence is thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Pattern p = Pattern.{@link #compile compile}("a*b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Matcher m = p.{@link #matcher matcher}("aaaaab");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * boolean b = m.{@link Matcher#matches matches}();</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p> A {@link #matches matches} method is defined by this class as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * convenience for when a regular expression is used just once.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * compiles an expression and matches an input sequence against it in a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * invocation.  The statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * boolean b = Pattern.matches("a*b", "aaaaab");</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * is equivalent to the three statements above, though for repeated matches it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * is less efficient since it does not allow the compiled pattern to be reused.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p> Instances of this class are immutable and are safe for use by multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * concurrent threads.  Instances of the {@link Matcher} class are not safe for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * such use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
    81
 * <h2><a id="sum">Summary of regular-expression constructs</a></h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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&nbsp;{@code <=}&nbsp;<i>n</i>&nbsp;{@code <=}&nbsp;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&nbsp;{@code <=}&nbsp;<i>n</i>&nbsp;{@code <=}&nbsp;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&nbsp;{@code <=}&nbsp;<i>m</i>&nbsp;{@code <=}&nbsp;3,
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   108
 *         0&nbsp;{@code <=}&nbsp;<i>n</i>&nbsp;{@code <=}&nbsp;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>&#92;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&nbsp;value&nbsp;{@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>&#92;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
 *         &nbsp;&lt;=&nbsp;{@code 0x}<i>h...h</i>&nbsp;&lt;=&nbsp;
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>&#92;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>'&#92;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>'&#92;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>'&#92;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>'&#92;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>'&#92;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>'&#92;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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   154
 *          and not {@code m} through {@code p}: {@code [a-lq-z]}(subtraction)</td></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&#92;u1680&#92;u180e&#92;u2000-&#92;u200a&#92;u202f&#92;u205f&#92;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&#92;u2028&#92;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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   199
 *     <!-- {@code [\!"#\$%&'\(\)\*\+,\-\./:;\<=\>\?@\[\\\]\^_`\{\|\}~]}
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&nbsp;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&nbsp;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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *         <a href="#lt">terminator</a>, if&nbsp;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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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>&#92;u000D&#92;u000A|[&#92;u000A&#92;u000B&#92;u000C&#92;u000D&#92;u0085&#92;u2028&#92;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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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>&lt;<i>name</i>&gt;</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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 *     <!-- Metachars: !$()*+.<>?[\]^{|} -->
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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>(?&lt;<a href="#groupname">name</a>&gt;</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)&nbsp;</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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
   360
 * <tr><th style="vertical-align:top; font-weight:normal" id="non_capture_group_flags"><code>(?idmsux-idmsux:</code><i>X</i>{@code )}&nbsp;&nbsp;</th>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
 *         given flags <a href="#CASE_INSENSITIVE">i</a> <a href="#UNIX_LINES">d</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
 * <a href="#MULTILINE">m</a> <a href="#DOTALL">s</a> <a href="#UNICODE_CASE">u</a >
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
 * <a href="#COMMENTS">x</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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
 *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44846
diff changeset
   376
 * </tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
 * <hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   382
 * <h2><a id="bs">Backslashes, escapes, and quoting</a></h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
 * constructs, as defined in the table above, as well as to quote characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
 * left brace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
 * <p> It is an error to use a backslash prior to any alphabetic character that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
 * does not denote an escaped construct; these are reserved for future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
 * extensions to the regular-expression language.  A backslash may be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
 * prior to a non-alphabetic character regardless of whether that character is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
 * part of an unescaped construct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&trade; 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
 * literals that represent regular expressions to protect them from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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>"&#92;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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
 * must be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   410
 * <h2><a id="cc">Character Classes</a></h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
 *    <p> Character classes may appear within other character classes, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
 *    The union operator denotes a class that contains every character that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
 *    in at least one of its operand classes.  The intersection operator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
 *    denotes a class that contains every character that is in both of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
 *    operand classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
 *    <p> The precedence of character-class operators is as follows, from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
 *    highest to lowest:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
 *        <td>Literal escape&nbsp;&nbsp;&nbsp;&nbsp;</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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 *        <td>Intersection</td>
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 *    <p> Note that a different set of metacharacters are in effect inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
 *    forming metacharacter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   453
 * <h2><a id="lt">Line terminators</a></h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
 * <p> A <i>line terminator</i> is a one- or two-character sequence that marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
 * the end of a line of the input character sequence.  The following are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
 * recognized as line terminators:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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>'&#92;u0085'</code>),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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>'&#92;u2028'</code>), or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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>'&#92;u2029'</code>).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
 * <p>If {@link #UNIX_LINES} mode is activated, then the only line terminators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
 * recognized are newline characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
 * terminator unless the {@link #DOTALL} flag is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
 * line terminators and only match at the beginning and the end, respectively,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
 * matches just before a line terminator or the end of the input sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   488
 * <h2><a id="cg">Groups and capturing</a></h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   490
 * <h3><a id="gnumber">Group number</a></h3>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
 * are four such groups: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
 * <p> Group zero always stands for the entire expression.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
 * <p> Capturing groups are so named because, during a match, each subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
 * of the input sequence that matches such a group is saved.  The captured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
 * subsequence may be used later in the expression, via a back reference, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
 * may also be retrieved from the matcher once the match operation is complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   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>'&#92;u0041'</code>&nbsp;through&nbsp;<code>'&#92;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>'&#92;u0061'</code>&nbsp;through&nbsp;<code>'&#92;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>'&#92;u0030'</code>&nbsp;through&nbsp;<code>'&#92;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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
 * <p> The captured input associated with a group is always the subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
 * that the group most recently matched.  If a group is evaluated a second time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
 * because of quantification then its previously-captured value, if any, will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
 * beginning of each match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   538
 * <h2> Unicode support </h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
 * <p> This class is in conformance with Level 1 of <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
 * Canonical Equivalents.
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>&#92;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&trade; 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>"&#92;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>&#92;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>&#92;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>&#92;uD840</code><code>&#92;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>&#92;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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
   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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
 * <a href="http://www.unicode.org/unicode/standard/standard.html">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
 * <i>The Unicode Standard</i></a> in the version specified by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
 * {@link java.lang.Character Character} class. The category names are those
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 20190
diff changeset
   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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   692
 * <h2> Comparison to Perl 5 </h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
 * with ordered alternation as occurs in Perl 5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
 * <p> Perl constructs not supported by this class: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   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
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   703
 *    <a href="#groupname">named-capturing group</a>.
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   704
 *    </p></li>
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   705
 *
3d4bcfaa8627 7036522: j.u.r.Pattern documentation errors
sherman
parents: 9536
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
 *    </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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>&#92;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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
 * <p> Constructs supported by this class but not by Perl: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
 *    <li><p> Character-class union and intersection as described
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
 *    <a href="#cc">above</a>.</p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
 * <p> Notable differences from Perl: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
 *    treated as a back reference if at least that many subexpressions exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
 *    otherwise it is interpreted, if possible, as an octal escape.  In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
 *    references, and a larger number is accepted as a back reference if at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
 *    least that many subexpressions exist at that point in the regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
 *    expression, otherwise the parser will drop digits until the number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
 *    smaller or equal to the existing number of groups or it is one digit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
 *    </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
 *    where the last match left off.  This functionality is provided implicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
 *    by the {@link Matcher} class: Repeated invocations of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
 *    Matcher#find find} method will resume where the last match left off,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
 *    unless the matcher is reset.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
 *    <li><p> In Perl, embedded flags at the top level of an expression affect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
 *    the whole expression.  In this class, embedded flags always take effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
 *    at the point at which they appear, whether they are at the top level or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
 *    within a group; in the latter case, flags are restored at the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
 *    group just as in Perl.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
 * <p> For a more precise description of the behavior of regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
 * constructs, please see <a href="http://www.oreilly.com/catalog/regex3/">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
 * <i>Mastering Regular Expressions, 3nd Edition</i>, Jeffrey E. F. Friedl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
 * O'Reilly and Associates, 2006.</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
 * @see java.lang.String#split(String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
 * @see java.lang.String#split(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
 * @author      Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
 * @author      JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
 * @spec        JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
public final class Pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * Regular expression modifier values.  Instead of being passed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * arguments, they can also be passed as inline modifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * For example, the following statements have the same effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * Enables Unix lines mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&nbsp;{@code (?d)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    public static final int UNIX_LINES = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * Enables case-insensitive matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * <p> By default, case-insensitive matching assumes that only characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * in the US-ASCII charset are being matched.  Unicode-aware
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * case-insensitive matching can be enabled by specifying the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * #UNICODE_CASE} flag in conjunction with this flag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&nbsp;{@code (?i)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * <p> Specifying this flag may impose a slight performance penalty.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public static final int CASE_INSENSITIVE = 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Permits whitespace and comments in pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&nbsp;{@code (?x)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    public static final int COMMENTS = 0x04;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * Enables multiline mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * just after or just before, respectively, a line terminator or the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * the input sequence.  By default these expressions only match at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * beginning and the end of the entire input sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&nbsp;{@code (?m)}.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
    public static final int MULTILINE = 0x08;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * Enables literal parsing of the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * <p> When this flag is specified then the input string that specifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * the pattern is treated as a sequence of literal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * Metacharacters or escape sequences in the input sequence will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * given no special meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * <p>The flags CASE_INSENSITIVE and UNICODE_CASE retain their impact on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * matching when used in conjunction with this flag. The other flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * become superfluous.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * <p> There is no embedded flag character for enabling literal parsing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    public static final int LITERAL = 0x10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * Enables dotall mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * including a line terminator.  By default this expression does not match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * line terminators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&nbsp;{@code (?s)}.  (The {@code s} is a mnemonic for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * "single-line" mode, which is what this is called in Perl.)  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    public static final int DOTALL = 0x20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * Enables Unicode-aware case folding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * <p> When this flag is specified then case-insensitive matching, when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * enabled by the {@link #CASE_INSENSITIVE} flag, is done in a manner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * consistent with the Unicode Standard.  By default, case-insensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * matching assumes that only characters in the US-ASCII charset are being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&nbsp;{@code (?u)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * <p> Specifying this flag may impose a performance penalty.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    public static final int UNICODE_CASE = 0x40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * Enables canonical equivalence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * <p> When this flag is specified then two characters will be considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&#92;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>"&#92;u00E5"</code> when this flag is specified.  By default,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * matching does not take canonical equivalence into account.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * <p> There is no embedded flag character for enabling canonical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * equivalence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * <p> Specifying this flag may impose a performance penalty.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    public static final int CANON_EQ = 0x80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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&nbsp;{@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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    /* Pattern has only two serialized components: The pattern string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * and the flags, which are all that is needed to recompile the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * when it is deserialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    /** use serialVersionUID from Merlin b59 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    private static final long serialVersionUID = 5073258162644648461L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * The original regular-expression pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    private String pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * The original pattern flags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
    private int flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    /**
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
   955
     * 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
   956
     * on and off by embedded flag.
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
   957
     */
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
   958
    private transient int flags0;
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
   959
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
   960
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Boolean indicating this Pattern is compiled; this is necessary in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * to lazily compile deserialized Patterns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     */
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34684
diff changeset
   964
    private transient volatile boolean compiled;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * The normalized pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    private transient String normalizedPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * The starting point of state machine for the find operation.  This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * a match to start anywhere in the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    transient Node root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * The root of object tree for a match operation.  The pattern is matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * at the beginning.  This may include a find that uses BnM or a First
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    transient Node matchRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * Temporary storage used by parsing pattern slice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    transient int[] buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    /**
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   990
     * 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
   991
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   992
    transient CharPredicate predicate;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   993
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
   994
    /**
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   995
     * 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
   996
     * node.
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   997
     */
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   998
    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
   999
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1000
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * Temporary storage used while parsing group references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    transient GroupHead[] groupNodes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    /**
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1006
     * 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
  1007
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1008
    transient List<Node> topClosureNodes;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1009
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
     * 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
  1012
     * 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
  1013
     * 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
  1014
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1015
    transient int localTCNCount;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1016
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
     * 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
  1019
     * 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
  1020
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1021
    transient boolean hasGroupRef;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1022
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1023
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * Temporary null terminated code point array used by pattern compiling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    private transient int[] temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * The number of capturing groups in this Pattern. Used by matchers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * allocate storage needed to perform a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    transient int capturingGroupCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * The local variable count used by parsing tree. Used by matchers to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * allocate storage needed to perform a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    transient int localCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * Index into the pattern string that keeps track of how much has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    private transient int cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * Holds the length of the pattern string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    private transient int patternLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    /**
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1052
     * 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
  1053
     * It is set to true during compiling if
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1054
     * (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
  1055
     * (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
  1056
     */
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1057
    private transient boolean hasSupplementary;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1058
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1059
    /**
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
  1060
     * Compiles the given regular expression into a pattern.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @param  regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     *         The expression to be compiled
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
  1064
     * @return the given regular expression compiled into a pattern
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *          If the expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    public static Pattern compile(String regex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        return new Pattern(regex, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * Compiles the given regular expression into a pattern with the given
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
  1074
     * flags.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @param  regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *         The expression to be compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     * @param  flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *         Match flags, a bit mask that may include
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *         {@link #CASE_INSENSITIVE}, {@link #MULTILINE}, {@link #DOTALL},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     *         {@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
  1083
     *         {@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
  1084
     *         and {@link #COMMENTS}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
  1086
     * @return the given regular expression compiled into a pattern with the given flags
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     *          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
  1089
     *          match flags are set in {@code flags}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     *          If the expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    public static Pattern compile(String regex, int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        return new Pattern(regex, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * Returns the regular expression from which this pattern was compiled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * @return  The source of this pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    public String pattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        return pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * <p>Returns the string representation of this pattern. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * is the regular expression from which this pattern was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * compiled.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @return  The string representation of this pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        return pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * Creates a matcher that will match the given input against this pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *         The character sequence to be matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @return  A new matcher for this pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    public Matcher matcher(CharSequence input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        if (!compiled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                if (!compiled)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                    compile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        Matcher m = new Matcher(this, input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    /**
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
  1139
     * Returns this pattern's match flags.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * @return  The match flags specified when this pattern was compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    public int flags() {
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  1144
        return flags0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * Compiles the given regular expression and attempts to match the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * input against it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * <p> An invocation of this convenience method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * Pattern.matches(regex, input);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * Pattern.compile(regex).matcher(input).matches()</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * <p> If a pattern is to be used multiple times, compiling it once and reusing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * it will be more efficient than invoking this method each time.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * @param  regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *         The expression to be compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *         The character sequence to be matched
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 18156
diff changeset
  1169
     * @return whether or not the regular expression matches on the input
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @throws  PatternSyntaxException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *          If the expression's syntax is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    public static boolean matches(String regex, CharSequence input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        Pattern p = Pattern.compile(regex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        Matcher m = p.matcher(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        return m.matches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * Splits the given input sequence around matches of this pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * <p> The array returned by this method contains each substring of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     * input sequence that is terminated by another subsequence that matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * this pattern or is terminated by the end of the input sequence.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * substrings in the array are in the order in which they occur in the
21668
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1186
     * input. If this pattern does not match any subsequence of the input then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * 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
  1188
     * string form.
21668
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1189
     *
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1190
     * <p> When there is a positive-width match at the beginning of the input
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1191
     * sequence then an empty leading substring is included at the beginning
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1192
     * of the resulting array. A zero-width match at the beginning however
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1193
     * never produces such empty leading substring.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1195
     * <p> The {@code limit} parameter controls the number of times the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * 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
  1197
     * array.
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 49763
diff changeset
  1198
     * <ul>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 49763
diff changeset
  1199
     *    <li><p>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 49763
diff changeset
  1200
     *    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
  1201
     *    at most <i>limit</i>&nbsp;-&nbsp;1 times, the array's length will be
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 49763
diff changeset
  1202
     *    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
  1203
     *    all input beyond the last matched delimiter.</p></li>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 49763
diff changeset
  1204
     *
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 49763
diff changeset
  1205
     *    <li><p>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 49763
diff changeset
  1206
     *    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
  1207
     *    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
  1208
     *    empty strings will be discarded.</p></li>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 49763
diff changeset
  1209
     *
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 49763
diff changeset
  1210
     *    <li><p>
ec52b4d094c0 8200172: String.split non-positive term incorrect use
sherman
parents: 49763
diff changeset
  1211
     *    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
  1212
     *    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
  1213
     * </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1215
     * <p> The input {@code "boo:and:foo"}, for example, yields the following
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * results with these parameters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1218
     * <table class="plain" style="margin-left:2em;">
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1219
     * <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
  1220
     * <thead>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1221
     * <tr>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1222
     *     <th scope="col">Regex</th>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1223
     *     <th scope="col">Limit</th>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1224
     *     <th scope="col">Result</th>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1225
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44846
diff changeset
  1226
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44846
diff changeset
  1227
     * <tbody>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1228
     * <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
  1229
     *     <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
  1230
     *     <td>{@code { "boo", "and:foo" }}</td></tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1231
     * <tr><!-- : -->
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1232
     *     <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
  1233
     *     <td>{@code { "boo", "and", "foo" }}</td></tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1234
     * <tr><!-- : -->
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1235
     *     <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
  1236
     *     <td>{@code { "boo", "and", "foo" }}</td></tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1237
     * <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
  1238
     *     <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
  1239
     *     <td>{@code { "b", "", ":and:f", "", "" }}</td></tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1240
     * <tr><!-- o -->
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1241
     *     <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
  1242
     *     <td>{@code { "b", "", ":and:f", "", "" }}</td></tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1243
     * <tr><!-- o -->
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1244
     *     <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
  1245
     *     <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
  1246
     * </tbody>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1247
     * </table>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     *         The character sequence to be split
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * @param  limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *         The result threshold, as described above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * @return  The array of strings computed by splitting the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     *          around matches of this pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    public String[] split(CharSequence input, int limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        boolean matchLimited = limit > 0;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5627
diff changeset
  1261
        ArrayList<String> matchList = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        Matcher m = matcher(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        // Add segments before each match found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        while(m.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            if (!matchLimited || matchList.size() < limit - 1) {
21668
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1267
                if (index == 0 && index == m.start() && m.start() == m.end()) {
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1268
                    // no empty leading substring included for zero-width match
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1269
                    // at the beginning of the input char sequence.
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1270
                    continue;
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  1271
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                String match = input.subSequence(index, m.start()).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                matchList.add(match);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                index = m.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            } else if (matchList.size() == limit - 1) { // last one
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                String match = input.subSequence(index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                                                 input.length()).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                matchList.add(match);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                index = m.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        // If no match was found, return this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        if (index == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            return new String[] {input.toString()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        // Add remaining segment
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        if (!matchLimited || matchList.size() < limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            matchList.add(input.subSequence(index, input.length()).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        // Construct result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
        int resultSize = matchList.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        if (limit == 0)
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52080
diff changeset
  1294
            while (resultSize > 0 && matchList.get(resultSize-1).isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                resultSize--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        String[] result = new String[resultSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        return matchList.subList(0, resultSize).toArray(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * Splits the given input sequence around matches of this pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * <p> This method works as if by invoking the two-argument {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * #split(java.lang.CharSequence, int) split} method with the given input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * sequence and a limit argument of zero.  Trailing empty strings are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * therefore not included in the resulting array. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1308
     * <p> The input {@code "boo:and:foo"}, for example, yields the following
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * results with these expressions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     *
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1311
     * <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
  1312
     * <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
  1313
     * <thead>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1314
     * <tr>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1315
     *  <th scope="col">Regex</th>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1316
     *  <th scope="col">Result</th>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1317
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44846
diff changeset
  1318
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44846
diff changeset
  1319
     * <tbody>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1320
     * <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
  1321
     *     <td>{@code { "boo", "and", "foo" }}</td></tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1322
     * <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
  1323
     *     <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
  1324
     * </tbody>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 46058
diff changeset
  1325
     * </table>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *         The character sequence to be split
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * @return  The array of strings computed by splitting the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     *          around matches of this pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    public String[] split(CharSequence input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        return split(input, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1339
     * 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
  1340
     * {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1342
     * <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
  1343
     * 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
  1344
     * {@code s} as if it were a literal pattern.</p> Metacharacters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * or escape sequences in the input sequence will be given no special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * @param  s The string to be literalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * @return  A literal string replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    public static String quote(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        int slashEIndex = s.indexOf("\\E");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        if (slashEIndex == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            return "\\Q" + s + "\\E";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
32012
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1357
        int lenHint = s.length();
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1358
        lenHint = (lenHint < Integer.MAX_VALUE - 8 - lenHint) ?
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1359
                (lenHint << 1) : (Integer.MAX_VALUE - 8);
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1360
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1361
        StringBuilder sb = new StringBuilder(lenHint);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        sb.append("\\Q");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        int current = 0;
32012
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1364
        do {
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1365
            sb.append(s, current, slashEIndex)
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1366
                    .append("\\E\\\\E\\Q");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
            current = slashEIndex + 2;
32012
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1368
        } while ((slashEIndex = s.indexOf("\\E", current)) != -1);
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1369
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1370
        return sb.append(s, current, s.length())
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1371
                .append("\\E")
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 28964
diff changeset
  1372
                .toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * Recompile the Pattern instance from a stream.  The original pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * string is read in and the object tree is recompiled from it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        throws java.io.IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        // Read in all fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  1385
        // reset the flags
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  1386
        flags0 = flags;
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  1387
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        // Initialize counts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        capturingGroupCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        localCount = 0;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1391
        localTCNCount = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        // if length > 0, the Pattern is lazily compiled
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
  1394
        if (pattern.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
            root = new Start(lastAccept);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
            matchRoot = lastAccept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
            compiled = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * This private constructor is used to create all Patterns. The pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * string and match flags are all that is needed to completely describe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * a Pattern. An empty pattern string results in an object tree with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * only a Start node and a LastNode node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    private Pattern(String p, int f) {
25523
c751d1010164 8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents: 22997
diff changeset
  1408
        if ((f & ~ALL_FLAGS) != 0) {
c751d1010164 8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents: 22997
diff changeset
  1409
            throw new IllegalArgumentException("Unknown flag 0x"
c751d1010164 8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents: 22997
diff changeset
  1410
                                               + Integer.toHexString(f));
c751d1010164 8035975: Pattern.compile(String, int) fails to throw IllegalArgumentException
igerasim
parents: 22997
diff changeset
  1411
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        pattern = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        flags = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
9536
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  1415
        // 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
  1416
        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
  1417
            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
  1418
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  1419
        // 'flags' for compiling
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  1420
        flags0 = flags;
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  1421
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        // Reset group index count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        capturingGroupCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        localCount = 0;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1425
        localTCNCount = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52902
diff changeset
  1427
        if (!pattern.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            compile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            root = new Start(lastAccept);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
            matchRoot = lastAccept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    /**
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1436
     * 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
  1437
     * 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
  1438
     * 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
  1439
     * 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
  1440
     * 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
  1441
     * 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
  1442
     * characters.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1444
    private static String normalize(String pattern) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1445
        int plen = pattern.length();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1446
        StringBuilder pbuf = new StringBuilder(plen);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1447
        char last = 0;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1448
        int lastStart = 0;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1449
        char cc = 0;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1450
        for (int i = 0; i < plen;) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1451
            char c = pattern.charAt(i);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1452
            if (cc == 0 &&    // top level
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1453
                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
  1454
                i += 2; last = 0;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1455
                continue;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1456
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1457
            if (c == '[' && last != '\\') {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1458
                if (cc == 0) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1459
                    if (lastStart < i)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1460
                        normalizeSlice(pattern, lastStart, i, pbuf);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1461
                    lastStart = i;
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
                cc++;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1464
            } else if (c == ']' && last != '\\') {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1465
                cc--;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1466
                if (cc == 0) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1467
                    normalizeClazz(pattern, lastStart, i + 1, pbuf);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1468
                    lastStart = i + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
                }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1470
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1471
            last = c;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1472
            i++;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1473
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1474
        assert (cc == 0);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1475
        if (lastStart < plen)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1476
            normalizeSlice(pattern, lastStart, plen, pbuf);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1477
        return pbuf.toString();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1478
    }
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
    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
  1481
                                       StringBuilder dst)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1482
    {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1483
        int len = src.length();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1484
        int off0 = off;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1485
        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
  1486
            off++;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1487
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1488
        if (off == limit) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1489
            dst.append(src, off0, limit);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1490
            return;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1491
        }
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
        if (off < off0)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1494
            off = off0;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1495
        else
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1496
            dst.append(src, off0, off);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1497
        while (off < limit) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1498
            int ch0 = src.codePointAt(off);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1499
            if (".$|()[]{}^?*+\\".indexOf(ch0) != -1) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1500
                dst.append((char)ch0);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1501
                off++;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1502
                continue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1504
            int j = off + Character.charCount(ch0);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1505
            int ch1;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1506
            while (j < limit) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1507
                ch1 = src.codePointAt(j);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1508
                if (Grapheme.isBoundary(ch0, ch1))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1509
                    break;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1510
                ch0 = ch1;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1511
                j += Character.charCount(ch1);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1512
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1513
            String seq = src.substring(off, j);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1514
            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
  1515
            off = j;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1516
            if (nfd.length() > 1) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1517
                ch0 = nfd.codePointAt(0);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1518
                ch1 = nfd.codePointAt(Character.charCount(ch0));
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1519
                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
  1520
                    Set<String> altns = new LinkedHashSet<>();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1521
                    altns.add(seq);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1522
                    produceEquivalentAlternation(nfd, altns);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1523
                    dst.append("(?:");
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  1524
                    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
  1525
                    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
  1526
                    dst.append(")");
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1527
                    continue;
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
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1530
            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
  1531
            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
  1532
                dst.append("(?:" + seq + "|" + nfd  + "|" + nfc + ")");
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1533
            else if (!seq.equals(nfd))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1534
                dst.append("(?:" + seq + "|" + nfd + ")");
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1535
            else
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1536
                dst.append(seq);
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
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1540
    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
  1541
                                       StringBuilder dst)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1542
    {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1543
        dst.append(Normalizer.normalize(src.substring(off, limit), Form.NFC));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * Given a specific sequence composed of a regular character and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * combining marks that follow it, produce the alternation that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * match all canonical equivalences of that sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1551
    private static void produceEquivalentAlternation(String src,
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1552
                                                     Set<String> dst)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1553
    {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1554
        int len = countChars(src, 0, 1);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1555
        if (src.length() == len) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1556
            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
  1557
            return;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1558
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1559
        String base = src.substring(0,len);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1560
        String combiningMarks = src.substring(len);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        String[] perms = producePermutations(combiningMarks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        // Add combined permutations
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1563
        for(int x = 0; x < perms.length; x++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
            String next = base + perms[x];
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1565
            dst.add(next);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            next = composeOneStep(next);
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1567
            if (next != null) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1568
                produceEquivalentAlternation(next, dst);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1569
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1570
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * Returns an array of strings that have all the possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * permutations of the characters in the input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * This is used to get a list of all possible orderings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * of a set of combining marks. Note that some of the permutations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * are invalid because of combining class collisions, and these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * possibilities must be removed because they are not canonically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * equivalent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1582
    private static String[] producePermutations(String input) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        if (input.length() == countChars(input, 0, 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
            return new String[] {input};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        if (input.length() == countChars(input, 0, 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            int c0 = Character.codePointAt(input, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
            int c1 = Character.codePointAt(input, Character.charCount(c0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            if (getClass(c1) == getClass(c0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
                return new String[] {input};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
            String[] result = new String[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            result[0] = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
            StringBuilder sb = new StringBuilder(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
            sb.appendCodePoint(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            sb.appendCodePoint(c0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
            result[1] = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        int length = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        int nCodePoints = countCodePoints(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        for(int x=1; x<nCodePoints; x++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            length = length * (x+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        String[] temp = new String[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        int combClass[] = new int[nCodePoints];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
        for(int x=0, i=0; x<nCodePoints; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
            int c = Character.codePointAt(input, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            combClass[x] = getClass(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
            i +=  Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        // For each char, take it out and add the permutations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        // of the remaining chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        // offset maintains the index in code units.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
loop:   for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            len = countChars(input, offset, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
            for(int y=x-1; y>=0; y--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
                if (combClass[y] == combClass[x]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
                    continue loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            StringBuilder sb = new StringBuilder(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
            String otherChars = sb.delete(offset, offset+len).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            String[] subResult = producePermutations(otherChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            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
  1632
            for (String sre : subResult)
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  1633
                temp[index++] = prefix + sre;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        String[] result = new String[index];
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34684
diff changeset
  1636
        System.arraycopy(temp, 0, result, 0, index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1640
    private static int getClass(int c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        return sun.text.Normalizer.getCombiningClass(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
     * Attempts to compose input by combining the first character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * with the first combining mark following it. Returns a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * that is the composition of the leading character with its first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * combining mark followed by the remaining combining marks. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * null if the first two characters cannot be further composed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1651
    private static String composeOneStep(String input) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        int len = countChars(input, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        String firstTwoCharacters = input.substring(0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        String result = Normalizer.normalize(firstTwoCharacters, Normalizer.Form.NFC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        if (result.equals(firstTwoCharacters))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            String remainder = input.substring(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            return result + remainder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * Preprocess any \Q...\E sequences in `temp', meta-quoting them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * See the description of `quotemeta' in perlfunc(1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    private void RemoveQEQuoting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        final int pLen = patternLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        while (i < pLen-1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            if (temp[i] != '\\')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                i += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            else if (temp[i + 1] != 'Q')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                i += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        if (i >= pLen - 1)    // No \Q sequence found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        int j = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        i += 2;
54714
37630ad8fa67 8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents: 54206
diff changeset
  1682
        int newTempLen;
37630ad8fa67 8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents: 54206
diff changeset
  1683
        try {
37630ad8fa67 8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents: 54206
diff changeset
  1684
            newTempLen = Math.addExact(j + 2, Math.multiplyExact(3, pLen - i));
37630ad8fa67 8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents: 54206
diff changeset
  1685
        } catch (ArithmeticException ae) {
37630ad8fa67 8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents: 54206
diff changeset
  1686
            throw new OutOfMemoryError();
37630ad8fa67 8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents: 54206
diff changeset
  1687
        }
37630ad8fa67 8223174: Pattern.compile() can throw confusing NegativeArraySizeException
igerasim
parents: 54206
diff changeset
  1688
        int[] newtemp = new int[newTempLen];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
        System.arraycopy(temp, 0, newtemp, 0, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        boolean inQuote = true;
11287
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1692
        boolean beginQuote = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        while (i < pLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            int c = temp[i++];
11287
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1695
            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
  1696
                newtemp[j++] = c;
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1697
            } else if (ASCII.isDigit(c)) {
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1698
                if (beginQuote) {
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1699
                    /*
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1700
                     * 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
  1701
                     * 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
  1702
                     * part of the escape.
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1703
                     */
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1704
                    newtemp[j++] = '\\';
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1705
                    newtemp[j++] = 'x';
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1706
                    newtemp[j++] = '3';
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1707
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
                newtemp[j++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            } else if (c != '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
                if (inQuote) newtemp[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                newtemp[j++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            } else if (inQuote) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
                if (temp[i] == 'E') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
                    inQuote = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
                    newtemp[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                    newtemp[j++] = '\\';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                if (temp[i] == 'Q') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                    inQuote = true;
11287
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1724
                    beginQuote = true;
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1725
                    continue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
                    newtemp[j++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                    if (i != pLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
                        newtemp[j++] = temp[i++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            }
11287
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1732
3db172a5433c 6990617: Regular expression doesn't match if unicode character next to a digit.
sherman
parents: 9694
diff changeset
  1733
            beginQuote = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
        patternLength = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        temp = Arrays.copyOf(newtemp, j + 2); // double zero termination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * Copies regular expression to an int array and invokes the parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * of the expression which will create the object tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    private void compile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        // Handle canonical equivalences
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        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
  1747
            normalizedPattern = normalize(pattern);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
            normalizedPattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        patternLength = normalizedPattern.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        // Copy pattern to int array for convenience
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
        // Use double zero to terminate pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        temp = new int[patternLength + 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  1757
        hasSupplementary = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
        int c, count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        // Convert all chars into code points
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        for (int x = 0; x < patternLength; x += Character.charCount(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            c = normalizedPattern.codePointAt(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
            if (isSupplementary(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                hasSupplementary = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
            temp[count++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        patternLength = count;   // patternLength now in code points
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        if (! has(LITERAL))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            RemoveQEQuoting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        // Allocate all temporary objects here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        buffer = new int[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        groupNodes = new GroupHead[10];
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1776
        namedGroups = null;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1777
        topClosureNodes = new ArrayList<>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        if (has(LITERAL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            // Literal pattern handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
            matchRoot = newSlice(temp, patternLength, hasSupplementary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            matchRoot.next = lastAccept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
            // Start recursive descent parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
            matchRoot = expr(lastAccept);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            // Check extra pattern characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            if (patternLength != cursor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                if (peek() == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                    throw error("Unmatched closing ')'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                    throw error("Unexpected internal error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        // Peephole optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        if (matchRoot instanceof Slice) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            root = BnM.optimize(matchRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
            if (root == matchRoot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        } else if (matchRoot instanceof Begin || matchRoot instanceof First) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
            root = matchRoot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            root = hasSupplementary ? new StartS(matchRoot) : new Start(matchRoot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1808
        // 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
  1809
        // 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
  1810
        // 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
  1811
        // 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
  1812
        if (!hasGroupRef) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1813
            for (Node node : topClosureNodes) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1814
                if (node instanceof Loop) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1815
                    // non-deterministic-greedy-group
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1816
                    ((Loop)node).posIndex = localTCNCount++;
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
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1820
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        // Release temporary storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        temp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        buffer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        groupNodes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        patternLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        compiled = true;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  1827
        topClosureNodes = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1830
    Map<String, Integer> namedGroups() {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34684
diff changeset
  1831
        Map<String, Integer> groups = namedGroups;
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34684
diff changeset
  1832
        if (groups == null) {
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34684
diff changeset
  1833
            namedGroups = groups = new HashMap<>(2);
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34684
diff changeset
  1834
        }
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34684
diff changeset
  1835
        return groups;
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1836
    }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1837
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * Used to accumulate information about a subtree of the object graph
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * so that optimizations can be applied to the subtree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    static final class TreeInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        int minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        int maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        boolean maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        boolean deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        TreeInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            minLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            maxLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            maxValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            deterministic = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * The following private methods are mainly used to improve the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * readability of the code. In order to let the Java compiler easily
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * inline them, we should not put many assertions or error checks in them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     * Indicates whether a particular flag is set or not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    private boolean has(int f) {
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  1869
        return (flags0 & f) != 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * Match next character, signal error if failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    private void accept(int ch, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        int testChar = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        if (has(COMMENTS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            testChar = parsePastWhitespace(testChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        if (ch != testChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            throw error(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * Mark the end of pattern with a specific character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    private void mark(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        temp[patternLength] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * Peek the next character, and do not advance the cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    private int peek() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        int ch = temp[cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        if (has(COMMENTS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            ch = peekPastWhitespace(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * Read the next character, and advance the cursor by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    private int read() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        int ch = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        if (has(COMMENTS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
            ch = parsePastWhitespace(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * Read the next character, and advance the cursor by one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * ignoring the COMMENTS setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
    private int readEscaped() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        int ch = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     * Advance the cursor by one, and peek the next character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    private int next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        int ch = temp[++cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        if (has(COMMENTS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            ch = peekPastWhitespace(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * Advance the cursor by one, and peek the next character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * ignoring the COMMENTS setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    private int nextEscaped() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        int ch = temp[++cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     * If in xmode peek past whitespace and comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    private int peekPastWhitespace(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        while (ASCII.isSpace(ch) || ch == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
            while (ASCII.isSpace(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                ch = temp[++cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            if (ch == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                ch = peekPastLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     * If in xmode parse past whitespace and comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    private int parsePastWhitespace(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        while (ASCII.isSpace(ch) || ch == '#') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            while (ASCII.isSpace(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
                ch = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            if (ch == '#')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                ch = parsePastLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * xmode parse past comment to end of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
    private int parsePastLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        int ch = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        while (ch != 0 && !isLineSeparator(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            ch = temp[cursor++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * xmode peek past comment to end of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    private int peekPastLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        int ch = temp[++cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        while (ch != 0 && !isLineSeparator(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            ch = temp[++cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * Determines if character is a line separator in the current mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    private boolean isLineSeparator(int ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        if (has(UNIX_LINES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            return ch == '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            return (ch == '\n' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                    ch == '\r' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                    (ch|1) == '\u2029' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                    ch == '\u0085');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * Read the character after the next one, and advance the cursor by two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    private int skip() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        int ch = temp[i+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        cursor = i + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * Unread one next character, and retreat cursor by one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
    private void unread() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        cursor--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     * Internal method used for handling all syntax errors. The pattern is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * displayed with a pointer to aid in locating the syntax error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
    private PatternSyntaxException error(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        return new PatternSyntaxException(s, normalizedPattern,  cursor - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     * Determines if there is any supplementary character or unpaired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * surrogate in the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    private boolean findSupplementary(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        for (int i = start; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            if (isSupplementary(temp[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * Determines if the specified code point is a supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * character or unpaired surrogate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
    private static final boolean isSupplementary(int ch) {
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2042
        return ch >= Character.MIN_SUPPLEMENTARY_CODE_POINT ||
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2043
               Character.isSurrogate((char)ch);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     *  The following methods handle the main parsing. They are sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     *  according to their precedence order, the lowest one first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * The expression is parsed with branch nodes added for alternations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * This may be called recursively to parse sub expressions that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * contain alternations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
    private Node expr(Node end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
        Node prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        Node firstTail = null;
13554
ad737436c54a 7189363: Regex Pattern compilation buggy for special sequences
sherman
parents: 13158
diff changeset
  2059
        Branch branch = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        Node branchConn = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
            Node node = sequence(end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
            Node nodeTail = root;      //double return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            if (prev == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                prev = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                firstTail = nodeTail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                // Branch
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                if (branchConn == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                    branchConn = new BranchConn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                    branchConn.next = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                if (node == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                    // if the node returned from sequence() is "end"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                    // we have an empty expr, set a null atom into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                    // the branch to indicate to go "next" directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                    node = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                    // the "tail.next" of each atom goes to branchConn
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                    nodeTail.next = branchConn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                }
13554
ad737436c54a 7189363: Regex Pattern compilation buggy for special sequences
sherman
parents: 13158
diff changeset
  2083
                if (prev == branch) {
ad737436c54a 7189363: Regex Pattern compilation buggy for special sequences
sherman
parents: 13158
diff changeset
  2084
                    branch.add(node);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
                    if (prev == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                        prev = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                        // replace the "end" with "branchConn" at its tail.next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                        // when put the "prev" into the branch as the first atom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                        firstTail.next = branchConn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                    }
13554
ad737436c54a 7189363: Regex Pattern compilation buggy for special sequences
sherman
parents: 13158
diff changeset
  2093
                    prev = branch = new Branch(prev, node, branchConn);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
            if (peek() != '|') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                return prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
13158
f628cbfcbfa0 7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents: 12675
diff changeset
  2103
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     * Parsing of sequences between alternations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
    private Node sequence(Node end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        Node head = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        Node tail = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        Node node = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
    LOOP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            case '(':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                // Because group handles its own closure,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                // we need to treat it differently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                node = group0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                // Check for comment or flag group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                if (node == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                if (head == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                    head = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                    tail.next = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                // Double return: Tail was returned in root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            case '[':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2130
                if (has(CANON_EQ) && !has(LITERAL))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2131
                    node = new NFCCharProperty(clazz(true));
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2132
                else
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2133
                    node = newCharProperty(clazz(true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                ch = nextEscaped();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                if (ch == 'p' || ch == 'P') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                    boolean oneLetter = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                    boolean comp = (ch == 'P');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                    ch = next(); // Consume { if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                    if (ch != '{') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                        unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                        oneLetter = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                    }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2146
                    // node = newCharProperty(family(oneLetter, comp));
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2147
                    if (has(CANON_EQ) && !has(LITERAL))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2148
                        node = new NFCCharProperty(family(oneLetter, comp));
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2149
                    else
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2150
                        node = newCharProperty(family(oneLetter, comp));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                    unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                    node = atom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
            case '^':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                if (has(MULTILINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                    if (has(UNIX_LINES))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                        node = new UnixCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                        node = new Caret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                    node = new Begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
            case '$':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                if (has(UNIX_LINES))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                    node = new UnixDollar(has(MULTILINE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                    node = new Dollar(has(MULTILINE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
            case '.':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                if (has(DOTALL)) {
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2177
                    node = new CharProperty(ALL());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                } else {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2179
                    if (has(UNIX_LINES)) {
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2180
                        node = new CharProperty(UNIXDOT());
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2181
                    } else {
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2182
                        node = new CharProperty(DOT());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
            case '|':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
            case ')':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                break LOOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
            case ']': // Now interpreting dangling ] and } as literals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
            case '}':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                node = atom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            case '?':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                throw error("Dangling meta character '" + ((char)ch) + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                if (cursor >= patternLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                    break LOOP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                // Fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                node = atom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
            node = closure(node);
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2209
            /* 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
  2210
            if (node instanceof GreedyCharProperty &&
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2211
                ((GreedyCharProperty)node).cp instanceof Dot) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2212
                topClosureNodes.add(node);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2213
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2214
            */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
            if (head == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                head = tail = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                tail.next = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                tail = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
        if (head == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
            return end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        tail.next = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        root = tail;      //double return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        return head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
13158
f628cbfcbfa0 7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents: 12675
diff changeset
  2230
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * Parse and add a new Single or Slice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
    private Node atom() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
        int first = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
        int prev = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
        boolean hasSupplementary = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
            case '*':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
            case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
            case '?':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
            case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                if (first > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                    cursor = prev;    // Unwind one character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                    first--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
            case '$':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
            case '.':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
            case '^':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
            case '(':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
            case '[':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
            case '|':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
            case ')':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            case '\\':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                ch = nextEscaped();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                if (ch == 'p' || ch == 'P') { // Property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                    if (first > 0) { // Slice is waiting; handle it first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                        unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                    } else { // No slice; just return the family node
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                        boolean comp = (ch == 'P');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                        boolean oneLetter = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                        ch = next(); // Consume { if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                        if (ch != '{')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                            unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                            oneLetter = false;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2272
                        if (has(CANON_EQ) && !has(LITERAL))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2273
                            return new NFCCharProperty(family(oneLetter, comp));
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2274
                        else
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2275
                            return newCharProperty(family(oneLetter, comp));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                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
  2280
                ch = escape(false, first == 0, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                if (ch >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                    append(ch, first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                    first++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                    if (isSupplementary(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                        hasSupplementary = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                    ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                } else if (first == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                    return root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                // Unwind meta escape sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                cursor = prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                if (cursor >= patternLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                // Fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                prev = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                append(ch, first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
                first++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                if (isSupplementary(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                    hasSupplementary = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
        if (first == 1) {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2313
            return newCharProperty(single(buffer[0]));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
            return newSlice(buffer, first, hasSupplementary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 54714
diff changeset
  2319
    private void append(int ch, int index) {
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 54714
diff changeset
  2320
        int len = buffer.length;
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 54714
diff changeset
  2321
        if (index - len >= 0) {
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 54714
diff changeset
  2322
            len = ArraysSupport.newLength(len,
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 54714
diff changeset
  2323
                    1 + index - len, /* minimum growth */
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 54714
diff changeset
  2324
                    len              /* preferred growth */);
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 54714
diff changeset
  2325
            buffer = Arrays.copyOf(buffer, len);
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 54714
diff changeset
  2326
        }
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 54714
diff changeset
  2327
        buffer[index] = ch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * Parses a backref greedily, taking as many numbers as it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * can. The first digit is always treated as a backref, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * multi digit numbers are only treated as a backref if at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     * least that many backrefs exist at this point in the regex.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
    private Node ref(int refNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
        boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        while(!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
            int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
            switch(ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
            case '0':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            case '1':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
            case '2':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            case '3':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
            case '4':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            case '5':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            case '6':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
            case '7':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
            case '8':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
            case '9':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                int newRefNum = (refNum * 10) + (ch - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                // Add another number if it doesn't make a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                // that doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                if (capturingGroupCount - 1 < newRefNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                    done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                refNum = newRefNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2366
        hasGroupRef = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
        if (has(CASE_INSENSITIVE))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            return new CIBackRef(refNum, has(UNICODE_CASE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
            return new BackRef(refNum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     * Parses an escape sequence to determine the actual value that needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     * to be matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     * If -1 is returned and create was true a new object was added to the tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     * to handle the escape sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
     * If the returned value is greater than zero, it is the value that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
     * matches the escape sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
     */
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
  2381
    private int escape(boolean inclass, boolean create, boolean isrange) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
        int ch = skip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        case '0':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            return o();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        case '1':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
        case '2':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
        case '3':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
        case '4':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
        case '5':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
        case '6':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
        case '7':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
        case '8':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
        case '9':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
            if (inclass) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
            if (create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                root = ref((ch - '0'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
        case 'A':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
            if (inclass) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
            if (create) root = new Begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
        case 'B':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            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
  2406
            if (create) root = new Bound(Bound.NONE, has(UNICODE_CHARACTER_CLASS));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
        case 'C':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        case 'D':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2411
            if (create) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2412
                predicate = has(UNICODE_CHARACTER_CLASS) ?
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2413
                            CharPredicates.DIGIT() : CharPredicates.ASCII_DIGIT();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2414
                predicate = predicate.negate();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2415
                if (!inclass)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2416
                    root = newCharProperty(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2417
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        case 'E':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
        case 'F':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        case 'G':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
            if (inclass) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
            if (create) root = new LastMatch();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        case 'H':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2427
            if (create) {
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2428
                predicate = HorizWS().negate();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2429
                if (!inclass)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2430
                    root = newCharProperty(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2431
            }
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
  2432
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        case 'I':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        case 'J':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
        case 'K':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        case 'L':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        case 'M':
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2438
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        case 'N':
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2440
            return N();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
        case 'O':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        case 'P':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        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
  2444
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        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
  2446
            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
  2447
            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
  2448
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        case 'S':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2450
            if (create) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2451
                predicate = has(UNICODE_CHARACTER_CLASS) ?
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2452
                            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
  2453
                predicate = predicate.negate();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2454
                if (!inclass)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2455
                    root = newCharProperty(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2456
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        case 'T':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
        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
  2460
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        case 'V':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2462
            if (create) {
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2463
                predicate = VertWS().negate();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2464
                if (!inclass)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2465
                    root = newCharProperty(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2466
            }
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
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
        case 'W':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2469
            if (create) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2470
                predicate = has(UNICODE_CHARACTER_CLASS) ?
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2471
                            CharPredicates.WORD() : CharPredicates.ASCII_WORD();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2472
                predicate = predicate.negate();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2473
                if (!inclass)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2474
                    root = newCharProperty(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2475
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
        case 'X':
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2478
            if (inclass) break;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2479
            if (create) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2480
                root = new XGrapheme();
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2481
            }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2482
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
        case 'Y':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
        case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
            if (inclass) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
            if (create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                if (has(UNIX_LINES))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                    root = new UnixDollar(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                    root = new Dollar(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
        case 'a':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
            return '\007';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
        case 'b':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            if (inclass) break;
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2498
            if (create) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2499
                if (peek() == '{') {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2500
                    if (skip() == 'g') {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2501
                        if (read() == '}') {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2502
                            root = new GraphemeBound();
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2503
                            return -1;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2504
                        }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2505
                        break;  // error missing trailing }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2506
                    }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2507
                    unread(); unread();
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2508
                }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2509
                root = new Bound(Bound.BOTH, has(UNICODE_CHARACTER_CLASS));
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  2510
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
        case 'c':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
            return c();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
        case 'd':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2515
            if (create) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2516
                predicate = has(UNICODE_CHARACTER_CLASS) ?
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2517
                            CharPredicates.DIGIT() : CharPredicates.ASCII_DIGIT();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2518
                if (!inclass)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2519
                    root = newCharProperty(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2520
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
        case 'e':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
            return '\033';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
        case 'f':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
            return '\f';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
        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
  2527
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
        case 'h':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2529
            if (create) {
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2530
                predicate = HorizWS();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2531
                if (!inclass)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2532
                    root = newCharProperty(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2533
            }
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
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
        case 'i':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
        case 'j':
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2537
            break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        case 'k':
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2539
            if (inclass)
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2540
                break;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2541
            if (read() != '<')
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2542
                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
  2543
            String name = groupname(read());
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2544
            if (!namedGroups().containsKey(name))
48853
84b4ffbba8b0 8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents: 48491
diff changeset
  2545
                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
  2546
            if (create) {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2547
                hasGroupRef = true;
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2548
                if (has(CASE_INSENSITIVE))
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2549
                    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
  2550
                else
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2551
                    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
  2552
            }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2553
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        case 'l':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        case 'm':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
        case 'n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
            return '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
        case 'o':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
        case 'p':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
        case 'q':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        case 'r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
            return '\r';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
        case 's':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2566
            if (create) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2567
                predicate = has(UNICODE_CHARACTER_CLASS) ?
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2568
                            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
  2569
                if (!inclass)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2570
                    root = newCharProperty(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2571
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
        case 't':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
            return '\t';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
        case 'u':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
            return u();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
        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
  2578
            // '\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
  2579
            // 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
  2580
            // 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
  2581
            // 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
  2582
            // 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
  2583
            // 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
  2584
            // which a single definite value (0x0B) is expected. For
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20190
diff changeset
  2585
            // 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
  2586
            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
  2587
                return '\013';
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2588
            if (create) {
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2589
                predicate = VertWS();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2590
                if (!inclass)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2591
                    root = newCharProperty(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2592
            }
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
            return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
        case 'w':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2595
            if (create) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2596
                predicate = has(UNICODE_CHARACTER_CLASS) ?
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  2597
                            CharPredicates.WORD() : CharPredicates.ASCII_WORD();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2598
                if (!inclass)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2599
                    root = newCharProperty(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2600
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        case 'x':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
            return x();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        case 'y':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        case 'z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
            if (inclass) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            if (create) root = new End();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
        throw error("Illegal/unsupported escape sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
     * Parse a character class, and return the node that matches it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
     * Consumes a ] on the way out if consume is true. Usually consume
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
     * is true except for the case of [abc&&def] where def is a separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
     * right hand node with "understood" brackets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2623
    private CharPredicate clazz(boolean consume) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2624
        CharPredicate prev = null;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2625
        CharPredicate curr = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        BitClass bits = new BitClass();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2627
        BmpCharPredicate bitsP = ch -> ch < 256 && bits.bits[ch];
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2628
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2629
        boolean isNeg = false;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2630
        boolean hasBits = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
        int ch = next();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2632
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2633
        // 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
  2634
        if (ch == '^' && temp[cursor-1] == '[') {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2635
            ch = next();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2636
            isNeg = true;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2637
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                case '[':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2641
                    curr = clazz(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                    if (prev == null)
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2643
                        prev = curr;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                    else
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2645
                        prev = prev.union(curr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                    ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
                case '&':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                    ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
                    if (ch == '&') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
                        ch = next();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2652
                        CharPredicate right = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                        while (ch != ']' && ch != '&') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                            if (ch == '[') {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2655
                                if (right == null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2656
                                    right = clazz(true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                                else
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2658
                                    right = right.union(clazz(true));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                            } else { // abc&&def
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
                                unread();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2661
                                right = clazz(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                            ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
                        }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2665
                        if (hasBits) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2666
                            // bits used, union has high precedence
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2667
                            if (prev == null) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2668
                                prev = curr = bitsP;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2669
                            } else {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2670
                                prev = prev.union(bitsP);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2671
                            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2672
                            hasBits = false;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2673
                        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2674
                        if (right != null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2675
                            curr = right;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
                        if (prev == null) {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2677
                            if (right == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
                                throw error("Bad class syntax");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
                            else
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2680
                                prev = right;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
                        } else {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2682
                            prev = prev.and(curr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
                        // treat as a literal &
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
                        unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
                    if (cursor >= patternLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                        throw error("Unclosed character class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                case ']':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2695
                    if (prev != null || hasBits) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                        if (consume)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                            next();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2698
                        if (prev == null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2699
                            prev = bitsP;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2700
                        else if (hasBits)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2701
                            prev = prev.union(bitsP);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2702
                        if (isNeg)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2703
                            return prev.negate();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
                        return prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
            }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2710
            curr = range(bits);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2711
            if (curr == null) {    // the bits used
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2712
                hasBits = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
            } else {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2714
                if (prev == null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2715
                    prev = curr;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2716
                else if (prev != curr)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2717
                    prev = prev.union(curr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
            ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2723
    private CharPredicate bitsOrSingle(BitClass bits, int ch) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
        /* Bits can only handle codepoints in [u+0000-u+00ff] range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
           Use "single" node instead of bits when dealing with unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
           case folding for codepoints listed below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
           (1)Uppercase out of range: u+00ff, u+00b5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
              toUpperCase(u+00ff) -> u+0178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
              toUpperCase(u+00b5) -> u+039c
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
           (2)LatinSmallLetterLongS u+17f
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
              toUpperCase(u+017f) -> u+0053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
           (3)LatinSmallLetterDotlessI u+131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
              toUpperCase(u+0131) -> u+0049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
           (4)LatinCapitalLetterIWithDotAbove u+0130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
              toLowerCase(u+0130) -> u+0069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
           (5)KelvinSign u+212a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
              toLowerCase(u+212a) ==> u+006B
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
           (6)AngstromSign u+212b
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
              toLowerCase(u+212b) ==> u+00e5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
        if (ch < 256 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
            !(has(CASE_INSENSITIVE) && has(UNICODE_CASE) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
              (ch == 0xff || ch == 0xb5 ||
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2744
               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
  2745
               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
  2746
               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
  2747
               ch == 0xc5 || ch == 0xe5))) {  //A+ring
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  2748
            bits.add(ch, flags0);
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2749
            return null;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2750
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2751
        return single(ch);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2752
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2753
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2754
    /**
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2755
     *  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
  2756
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2757
    private CharPredicate single(final int ch) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2758
        if (has(CASE_INSENSITIVE)) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2759
            int lower, upper;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2760
            if (has(UNICODE_CASE)) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2761
                upper = Character.toUpperCase(ch);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2762
                lower = Character.toLowerCase(upper);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2763
                // Unicode case insensitive matches
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2764
                if (upper != lower)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2765
                    return SingleU(lower);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2766
            } else if (ASCII.isAscii(ch)) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2767
                lower = ASCII.toLower(ch);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2768
                upper = ASCII.toUpper(ch);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2769
                // 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
  2770
                if (lower != upper)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2771
                    return SingleI(lower, upper);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2772
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2773
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2774
        if (isSupplementary(ch))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2775
            return SingleS(ch);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2776
        return Single(ch);  // Match a given BMP character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     * Parse a single character or a character range in a character class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * and return its representative node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2783
    private CharPredicate range(BitClass bits) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
        if (ch == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
            ch = nextEscaped();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
            if (ch == 'p' || ch == 'P') { // A property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
                boolean comp = (ch == 'P');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
                boolean oneLetter = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
                // Consume { if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
                ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                if (ch != '{')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                    unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                    oneLetter = false;
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  2796
                return family(oneLetter, comp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
            } 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
  2798
                boolean isrange = temp[cursor+1] == '-';
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
                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
  2800
                ch = escape(true, true, isrange);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
                if (ch == -1)
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2802
                    return predicate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
        } 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
  2805
            next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
        if (ch >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
            if (peek() == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
                int endRange = temp[cursor+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
                if (endRange == '[') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
                    return bitsOrSingle(bits, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                if (endRange != ']') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                    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
  2815
                    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
  2816
                    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
  2817
                        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
  2818
                    } 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
  2819
                        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
  2820
                    }
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
                    if (m < ch) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
                        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
  2823
                    }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2824
                    if (has(CASE_INSENSITIVE)) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2825
                        if (has(UNICODE_CASE))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2826
                            return CIRangeU(ch, m);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2827
                        return CIRange(ch, m);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2828
                    } else {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2829
                        return Range(ch, m);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2830
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
            return bitsOrSingle(bits, ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
        throw error("Unexpected character '"+((char)ch)+"'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
     * Parses a Unicode character family and returns its representative node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2841
    private CharPredicate family(boolean singleLetter, boolean isComplement) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
        String name;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2844
        CharPredicate p = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
        if (singleLetter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
            int c = temp[cursor];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
            if (!Character.isSupplementaryCodePoint(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
                name = String.valueOf((char)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
                name = new String(temp, cursor, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
            read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
            int i = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
            mark('}');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
            while(read() != '}') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
            mark('\000');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
            int j = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
            if (j > patternLength)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
                throw error("Unclosed character family");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
            if (i + 1 >= j)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                throw error("Empty character family");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
            name = new String(temp, i, j-i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2868
        int i = name.indexOf('=');
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2869
        if (i != -1) {
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2870
            // property construct \p{name=value}
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2871
            String value = name.substring(i + 1);
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2872
            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
  2873
            switch (name) {
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  2874
                case "sc":
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  2875
                case "script":
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2876
                    p = CharPredicates.forUnicodeScript(value);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  2877
                    break;
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  2878
                case "blk":
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  2879
                case "block":
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2880
                    p = CharPredicates.forUnicodeBlock(value);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  2881
                    break;
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  2882
                case "gc":
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  2883
                case "general_category":
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2884
                    p = CharPredicates.forProperty(value);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  2885
                    break;
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  2886
                default:
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2887
                    break;
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2888
            }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2889
            if (p == null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2890
                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
  2891
                             + "value=<" + value + ">}");
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2892
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
        } else {
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2894
            if (name.startsWith("In")) {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2895
                // \p{InBlockName}
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2896
                p = CharPredicates.forUnicodeBlock(name.substring(2));
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2897
            } else if (name.startsWith("Is")) {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2898
                // \p{IsGeneralCategory} and \p{IsScriptName}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
                name = name.substring(2);
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2900
                p = CharPredicates.forUnicodeProperty(name);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2901
                if (p == null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2902
                    p = CharPredicates.forProperty(name);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2903
                if (p == null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2904
                    p = CharPredicates.forUnicodeScript(name);
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2905
            } 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
  2906
                if (has(UNICODE_CHARACTER_CLASS)) {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2907
                    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
  2908
                }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2909
                if (p == null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2910
                    p = CharPredicates.forProperty(name);
5610
fd2427610c7f 6945564: Unicode script support in Character class
sherman
parents: 4820
diff changeset
  2911
            }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2912
            if (p == null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2913
                throw error("Unknown character property name {In/Is" + name + "}");
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2914
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2915
        if (isComplement) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2916
            // 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
  2917
            // 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
  2918
            // go with StartS.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2919
            hasSupplementary = true;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2920
            p = p.negate();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2921
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2922
        return p;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2925
    private CharProperty newCharProperty(CharPredicate p) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
        if (p == null)
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2927
            return null;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2928
        if (p instanceof BmpCharPredicate)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2929
            return new BmpCharProperty((BmpCharPredicate)p);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2930
        else
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2931
            return new CharProperty(p);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
    /**
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2935
     * 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
  2936
     * ">" is consumed after parsing.
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2937
     */
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2938
    private String groupname(int ch) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2939
        StringBuilder sb = new StringBuilder();
48853
84b4ffbba8b0 8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents: 48491
diff changeset
  2940
        if (!ASCII.isAlpha(ch))
84b4ffbba8b0 8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents: 48491
diff changeset
  2941
            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
  2942
        do {
84b4ffbba8b0 8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents: 48491
diff changeset
  2943
            sb.append((char) ch);
84b4ffbba8b0 8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents: 48491
diff changeset
  2944
        } 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
  2945
        if (ch != '>')
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2946
            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
  2947
        return sb.toString();
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2948
    }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2949
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2950
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
     * Parses a group and returns the head node of a set of nodes that process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
     * the group. Sometimes a double return system is used where the tail is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
     * returned in root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
    private Node group0() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
        boolean capturingGroup = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
        Node head = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
        Node tail = null;
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  2959
        int save = flags0;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2960
        int saveTCNCount = topClosureNodes.size();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
        root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
        int ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
        if (ch == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
            ch = skip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
            case ':':   //  (?:xxx) pure group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
                head = createGroup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
                head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
            case '=':   // (?=xxx) and (?!xxx) lookahead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
            case '!':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                head = createGroup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                if (ch == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                    head = tail = new Pos(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
                    head = tail = new Neg(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
            case '>':   // (?>xxx)  independent group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                head = createGroup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
                head.next = expr(tail);
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  2986
                head = tail = new Ques(head, Qtype.INDEPENDENT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
            case '<':   // (?<xxx)  look behind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
                ch = read();
48853
84b4ffbba8b0 8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents: 48491
diff changeset
  2990
                if (ch != '=' && ch != '!') {
2290
3a3bde061968 6817475: named-capturing group name started with digit causes PSE exception
sherman
parents: 2070
diff changeset
  2991
                    // named captured group
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2992
                    String name = groupname(ch);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2993
                    if (namedGroups().containsKey(name))
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2994
                        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
  2995
                                    + "> is already defined");
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2996
                    capturingGroup = true;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2997
                    head = createGroup(false);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2998
                    tail = root;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  2999
                    namedGroups().put(name, capturingGroupCount-1);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  3000
                    head.next = expr(tail);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  3001
                    break;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  3002
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
                int start = cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
                head = createGroup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
                head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                tail.next = lookbehindEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
                TreeInfo info = new TreeInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
                head.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                if (info.maxValid == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
                    throw error("Look-behind group does not have "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
                                + "an obvious maximum length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
                boolean hasSupplementary = findSupplementary(start, patternLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
                if (ch == '=') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
                    head = tail = (hasSupplementary ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
                                   new BehindS(head, info.maxLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
                                               info.minLength) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
                                   new Behind(head, info.maxLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
                                              info.minLength));
48853
84b4ffbba8b0 8197462: Inconsistent exception messages for invalid capturing group names
igerasim
parents: 48491
diff changeset
  3021
                } else { // if (ch == '!')
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
                    head = tail = (hasSupplementary ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
                                   new NotBehindS(head, info.maxLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
                                                  info.minLength) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                                   new NotBehind(head, info.maxLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
                                                 info.minLength));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
                }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3028
                // 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
  3029
                if (saveTCNCount < topClosureNodes.size())
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3030
                    topClosureNodes.subList(saveTCNCount, topClosureNodes.size()).clear();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
            case '$':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
            case '@':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
                throw error("Unknown group type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
            default:    // (?xxx:) inlined match flags
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
                unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
                addFlag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
                ch = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
                if (ch == ')') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
                    return null;    // Inline modifier only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
                if (ch != ':') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
                    throw error("Unknown inline modifier");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
                head = createGroup(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
                tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
                head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
        } else { // (xxx) a regular group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
            capturingGroup = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
            head = createGroup(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
            tail = root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
            head.next = expr(tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
        accept(')', "Unclosed group");
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3058
        flags0 = save;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
        // Check for quantifiers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
        Node node = closure(head);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
        if (node == head) { // No closure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
            root = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
            return node;    // Dual return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
        if (head == tail) { // Zero length assertion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
            root = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
            return node;    // Dual return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3071
        // 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
  3072
        // 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
  3073
        if (saveTCNCount < topClosureNodes.size())
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3074
            topClosureNodes.subList(saveTCNCount, topClosureNodes.size()).clear();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3075
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
        if (node instanceof Ques) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
            Ques ques = (Ques) node;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3078
            if (ques.type == Qtype.POSSESSIVE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
                root = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
            tail.next = new BranchConn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
            tail = tail.next;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3084
            if (ques.type == Qtype.GREEDY) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
                head = new Branch(head, null, tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
            } else { // Reluctant quantifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
                head = new Branch(null, head, tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
            root = tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
            return head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
        } else if (node instanceof Curly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
            Curly curly = (Curly) node;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3093
            if (curly.type == Qtype.POSSESSIVE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                root = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
            // Discover if the group is deterministic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
            TreeInfo info = new TreeInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
            if (head.study(info)) { // Deterministic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
                GroupTail temp = (GroupTail) tail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                head = root = new GroupCurly(head.next, curly.cmin,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
                                   curly.cmax, curly.type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
                                   ((GroupTail)tail).localIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
                                   ((GroupTail)tail).groupIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
                                             capturingGroup);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
                return head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
            } else { // Non-deterministic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
                int temp = ((GroupHead) head).localIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
                Loop loop;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3110
                if (curly.type == Qtype.GREEDY) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
                    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
  3112
                    // 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
  3113
                    if (curly.cmax == MAX_REPS)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3114
                        topClosureNodes.add(loop);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3115
                } else {  // Reluctant Curly
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
                    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
  3117
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
                Prolog prolog = new Prolog(loop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
                this.localCount += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
                loop.cmin = curly.cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
                loop.cmax = curly.cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
                loop.body = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
                tail.next = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
                root = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
                return prolog; // Dual return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
        throw error("Internal logic error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
     * Create group head and tail nodes using double return. If the group is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
     * created with anonymous true then it is a pure group and should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
     * affect group counting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
    private Node createGroup(boolean anonymous) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
        int localIndex = localCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
        int groupIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
        if (!anonymous)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
            groupIndex = capturingGroupCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
        GroupHead head = new GroupHead(localIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
        root = new GroupTail(localIndex, groupIndex);
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3143
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3144
        // 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
  3145
        head.tail = (GroupTail)root;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3146
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
        if (!anonymous && groupIndex < 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
            groupNodes[groupIndex] = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
        return head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
13158
f628cbfcbfa0 7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents: 12675
diff changeset
  3152
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
     * Parses inlined match flags and set them appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
    private void addFlag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
            case 'i':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3161
                flags0 |= CASE_INSENSITIVE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
            case 'm':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3164
                flags0 |= MULTILINE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
            case 's':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3167
                flags0 |= DOTALL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
            case 'd':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3170
                flags0 |= UNIX_LINES;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
            case 'u':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3173
                flags0 |= UNICODE_CASE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
            case 'c':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3176
                flags0 |= CANON_EQ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
            case 'x':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3179
                flags0 |= COMMENTS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
                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
  3181
            case 'U':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3182
                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
  3183
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
            case '-': // subFlag then fall through
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
                ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
                subFlag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
            ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
13158
f628cbfcbfa0 7176907: additional warnings cleanup in java.util, java.util.regexp, java.util.zip
smarks
parents: 12675
diff changeset
  3194
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
     * Parses the second part of inlined match flags and turns off
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
     * flags appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
    private void subFlag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
            case 'i':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3204
                flags0 &= ~CASE_INSENSITIVE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
            case 'm':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3207
                flags0 &= ~MULTILINE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
            case 's':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3210
                flags0 &= ~DOTALL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
            case 'd':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3213
                flags0 &= ~UNIX_LINES;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
            case 'u':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3216
                flags0 &= ~UNICODE_CASE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
            case 'c':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3219
                flags0 &= ~CANON_EQ;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
            case 'x':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3222
                flags0 &= ~COMMENTS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
                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
  3224
            case 'U':
48491
7f11a1699ef6 8194667: Regex: Serialization doesn't work with match flags
sherman
parents: 47216
diff changeset
  3225
                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
  3226
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
            ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
    static final int MAX_REPS   = 0x7FFFFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3236
    static enum Qtype {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3237
        GREEDY, LAZY, POSSESSIVE, INDEPENDENT
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3238
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3239
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3240
    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
  3241
        int ch = next();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3242
        if (ch == '?') {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3243
            next();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3244
            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
  3245
        } else if (ch == '+') {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3246
            next();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3247
            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
  3248
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3249
        if (prev instanceof BmpCharProperty) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3250
            return new BmpCharPropertyGreedy((BmpCharProperty)prev, cmin);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3251
        } else if (prev instanceof CharProperty) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3252
            return new CharPropertyGreedy((CharProperty)prev, cmin);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3253
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3254
        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
  3255
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
     * Processes repetition. If the next character peeked is a quantifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
     * then new nodes must be appended to handle the repetition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
     * Prev could be a single or a group, so it could be a chain of nodes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
    private Node closure(Node prev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
        Node atom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
        int ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
        switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
        case '?':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
            ch = next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
            if (ch == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
                next();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3270
                return new Ques(prev, Qtype.LAZY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
            } else if (ch == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
                next();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3273
                return new Ques(prev, Qtype.POSSESSIVE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
            }
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.GREEDY);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3276
        case '*':
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3277
            return curly(prev, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
        case '+':
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3279
            return curly(prev, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
        case '{':
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
            ch = temp[cursor+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
            if (ASCII.isDigit(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
                skip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
                int cmin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
                    cmin = cmin * 10 + (ch - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
                } while (ASCII.isDigit(ch = read()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
                int cmax = cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
                if (ch == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
                    ch = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
                    cmax = MAX_REPS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
                    if (ch != '}') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
                        cmax = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
                        while (ASCII.isDigit(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
                            cmax = cmax * 10 + (ch - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
                            ch = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
                if (ch != '}')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
                    throw error("Unclosed counted closure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
                if (((cmin) | (cmax) | (cmax - cmin)) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
                    throw error("Illegal repetition range");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
                Curly curly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
                ch = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
                if (ch == '?') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
                    next();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3308
                    curly = new Curly(prev, cmin, cmax, Qtype.LAZY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
                } else if (ch == '+') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
                    next();
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3311
                    curly = new Curly(prev, cmin, cmax, Qtype.POSSESSIVE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
                } else {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3313
                    curly = new Curly(prev, cmin, cmax, Qtype.GREEDY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
                return curly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                throw error("Illegal repetition");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
            return prev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
     *  Utility method for parsing control escape sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
    private int c() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
        if (cursor < patternLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
            return read() ^ 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
        throw error("Illegal control escape sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
     *  Utility method for parsing octal escape sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
    private int o() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
        int n = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
        if (((n-'0')|('7'-n)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
            int m = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
            if (((m-'0')|('7'-m)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
                int o = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
                if ((((o-'0')|('7'-o)) >= 0) && (((n-'0')|('3'-n)) >= 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                    return (n - '0') * 64 + (m - '0') * 8 + (o - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
                unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
                return (n - '0') * 8 + (m - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
            unread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
            return (n - '0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
        throw error("Illegal octal escape sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
     *  Utility method for parsing hexadecimal escape sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
    private int x() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
        int n = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
        if (ASCII.isHexDigit(n)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
            int m = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
            if (ASCII.isHexDigit(m)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
                return ASCII.toDigit(n) * 16 + ASCII.toDigit(m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
            }
8173
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3365
        } else if (n == '{' && ASCII.isHexDigit(peek())) {
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3366
            int ch = 0;
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3367
            while (ASCII.isHexDigit(n = read())) {
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3368
                ch = (ch << 4) + ASCII.toDigit(n);
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3369
                if (ch > Character.MAX_CODE_POINT)
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3370
                    throw error("Hexadecimal codepoint is too big");
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3371
            }
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3372
            if (n != '}')
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3373
                throw error("Unclosed hexadecimal escape sequence");
a3a39b98e05a 7014645: Support perl style Unicode hex notation \x{...}
sherman
parents: 7816
diff changeset
  3374
            return ch;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
        throw error("Illegal hexadecimal escape sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
     *  Utility method for parsing unicode escape sequences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
     */
404
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3382
    private int cursor() {
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3383
        return cursor;
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3384
    }
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3385
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3386
    private void setcursor(int pos) {
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3387
        cursor = pos;
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3388
    }
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3389
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3390
    private int uxxxx() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
        for (int i = 0; i < 4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
            int ch = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
            if (!ASCII.isHexDigit(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
                throw error("Illegal Unicode escape sequence");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
            n = n * 16 + ASCII.toDigit(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
404
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3402
    private int u() {
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3403
        int n = uxxxx();
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3404
        if (Character.isHighSurrogate((char)n)) {
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3405
            int cur = cursor();
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3406
            if (read() == '\\' && read() == 'u') {
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3407
                int n2 = uxxxx();
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3408
                if (Character.isLowSurrogate((char)n2))
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3409
                    return Character.toCodePoint((char)n, (char)n2);
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3410
            }
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3411
            setcursor(cur);
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3412
        }
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3413
        return n;
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3414
    }
0f6ea24796a4 6635133: Exception thrown when using a Unicode escape
sherman
parents: 2
diff changeset
  3415
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3416
    private int N() {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3417
        if (read() == '{') {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3418
            int i = cursor;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3419
            while (cursor < patternLength && read() != '}') {}
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3420
            if (cursor > patternLength)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3421
                throw error("Unclosed character name escape sequence");
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3422
            String name = new String(temp, i, cursor - i - 1);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3423
            try {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3424
                return Character.codePointOf(name);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3425
            } catch (IllegalArgumentException x) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3426
                throw error("Unknown character name [" + name + "]");
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3427
            }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3428
        }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3429
        throw error("Illegal character name escape sequence");
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3430
    }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  3431
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
    // Utility methods for code point support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
    private static final int countChars(CharSequence seq, int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
                                        int lengthInCodePoints) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
        // optimization
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
        if (lengthInCodePoints == 1 && !Character.isHighSurrogate(seq.charAt(index))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
            assert (index >= 0 && index < seq.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
        int length = seq.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
        int x = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
        if (lengthInCodePoints >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
            assert (index >= 0 && index < length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
            for (int i = 0; x < length && i < lengthInCodePoints; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
                if (Character.isHighSurrogate(seq.charAt(x++))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
                    if (x < length && Character.isLowSurrogate(seq.charAt(x))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
                        x++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
            return x - index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
        assert (index >= 0 && index <= length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
        if (index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
        int len = -lengthInCodePoints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
        for (int i = 0; x > 0 && i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
            if (Character.isLowSurrogate(seq.charAt(--x))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
                if (x > 0 && Character.isHighSurrogate(seq.charAt(x-1))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
                    x--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
        return index - x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
    private static final int countCodePoints(CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
        int length = seq.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
        for (int i = 0; i < length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
            n++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
            if (Character.isHighSurrogate(seq.charAt(i++))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
                if (i < length && Character.isLowSurrogate(seq.charAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
                    i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
     *  Creates a bit vector for matching Latin-1 values. A normal BitClass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
     *  never matches values above Latin-1, and a complemented BitClass always
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
     *  matches values above Latin-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3490
    static final class BitClass extends BmpCharProperty {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
        final boolean[] bits;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3492
        BitClass() {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3493
            this(new boolean[256]);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3494
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3495
        private BitClass(boolean[] bits) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3496
            super( ch -> ch < 256 && bits[ch]);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3497
            this.bits = bits;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3498
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
        BitClass add(int c, int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
            assert c >= 0 && c <= 255;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
            if ((flags & CASE_INSENSITIVE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
                if (ASCII.isAscii(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
                    bits[ASCII.toUpper(c)] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
                    bits[ASCII.toLower(c)] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
                } else if ((flags & UNICODE_CASE) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
                    bits[Character.toLowerCase(c)] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
                    bits[Character.toUpperCase(c)] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
            bits[c] = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
     *  Utility method for creating a string slice matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
    private Node newSlice(int[] buf, int count, boolean hasSupplementary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
        int[] tmp = new int[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
        if (has(CASE_INSENSITIVE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
            if (has(UNICODE_CASE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
                for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
                    tmp[i] = Character.toLowerCase(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
                                 Character.toUpperCase(buf[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
                return hasSupplementary? new SliceUS(tmp) : new SliceU(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
                tmp[i] = ASCII.toLower(buf[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
            return hasSupplementary? new SliceIS(tmp) : new SliceI(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
            tmp[i] = buf[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
        return hasSupplementary ? new SliceS(tmp) : new Slice(tmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
     * The following classes are the building components of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
     * tree that represents a compiled regular expression. The object tree
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
     * is made of individual elements that handle constructs in the Pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
     * Each type of object knows how to match its equivalent construct with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
     * the match() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
     * Base class for all node classes. Subclasses should override the match()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
     * method as appropriate. This class is an accepting node, so its match()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
     * always returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
    static class Node extends Object {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
        Node next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
        Node() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            next = Pattern.accept;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
         * This method implements the classic accept node.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
            matcher.last = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
            matcher.groups[0] = matcher.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
            matcher.groups[1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
         * This method is good for all zero length assertions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
            if (next != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
                return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
                return info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
    static class LastNode extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
         * This method implements the classic accept node with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
         * the addition of a check to see if the match occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
         * using all of the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
            if (matcher.acceptMode == Matcher.ENDANCHOR && i != matcher.to)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
            matcher.last = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
            matcher.groups[0] = matcher.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
            matcher.groups[1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
     * Used for REs that can start anywhere within the input string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
     * This basically tries to match repeatedly at each spot in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
     * input string, moving forward after each try. An anchored search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
     * or a BnM will bypass this node completely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
    static class Start extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
        int minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
        Start(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
            this.next = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
            TreeInfo info = new TreeInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
            next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
            minLength = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
            if (i > matcher.to - minLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
            int guard = matcher.to - minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
            for (; i <= guard; i++) {
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3615
                if (next.match(matcher, i, seq)) {
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3616
                    matcher.first = i;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3617
                    matcher.groups[0] = matcher.first;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3618
                    matcher.groups[1] = matcher.last;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3619
                    return true;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3620
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
            }
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3622
            matcher.hitEnd = true;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3623
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
            next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
     * StartS supports supplementary characters, including unpaired surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
    static final class StartS extends Start {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
        StartS(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
            super(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
            if (i > matcher.to - minLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
            int guard = matcher.to - minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
            while (i <= guard) {
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3647
                //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
  3648
                if (next.match(matcher, i, seq)) {
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3649
                    matcher.first = i;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3650
                    matcher.groups[0] = matcher.first;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3651
                    matcher.groups[1] = matcher.last;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3652
                    return true;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3653
                }
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3654
                if (i == guard)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
                // Optimization to move to the next character. This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
                // faster than countChars(seq, i, 1).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
                if (Character.isHighSurrogate(seq.charAt(i++))) {
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3659
                    if (i < seq.length() &&
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3660
                        Character.isLowSurrogate(seq.charAt(i))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
                        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
            }
4820
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3665
            matcher.hitEnd = true;
e32f5d2ba062 6919132: Regex \P{Lu} selects half of a surrogate pari
sherman
parents: 4161
diff changeset
  3666
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
     * Node to anchor at the beginning of input. This object implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
     * match for a \A sequence, and the caret anchor will use this if not in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
     * multiline mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
    static final class Begin extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
            int fromIndex = (matcher.anchoringBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
                matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
            if (i == fromIndex && next.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
                matcher.first = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
                matcher.groups[0] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
                matcher.groups[1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
     * Node to anchor at the end of input. This is the absolute end, so this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
     * should not match at the last newline before the end as $ will.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
    static final class End extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
            int endIndex = (matcher.anchoringBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
                matcher.to : matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
            if (i == endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
                return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
     * Node to anchor at the beginning of a line. This is essentially the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
     * object to match for the multiline ^.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
    static final class Caret extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
            int startIndex = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
            int endIndex = matcher.to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
            if (!matcher.anchoringBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
                endIndex = matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
            // Perl does not match ^ at end of input even after newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
            if (i == endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
            if (i > startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
                char ch = seq.charAt(i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
                if (ch != '\n' && ch != '\r'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
                    && (ch|1) != '\u2029'
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
                    && ch != '\u0085' ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
                // Should treat /r/n as one newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
                if (ch == '\r' && seq.charAt(i) == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
     * Node to anchor at the beginning of a line when in unixdot mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
    static final class UnixCaret extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
            int startIndex = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
            int endIndex = matcher.to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
            if (!matcher.anchoringBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
                endIndex = matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
            // Perl does not match ^ at end of input even after newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
            if (i == endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
            if (i > startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
                char ch = seq.charAt(i-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
                if (ch != '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
     * Node to match the location where the last match ended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
     * This is used for the \G construct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
    static final class LastMatch extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
            if (i != matcher.oldLast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
     * Node to anchor at the end of a line or the end of input based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
     * multiline mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
     * When not in multiline mode, the $ can only match at the very end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
     * of the input, unless the input ends in a line terminator in which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
     * it matches right before the last line terminator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
     * Note that \r\n is considered an atomic line terminator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
     * Like ^ the $ operator matches at a position, it does not match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
     * line terminators themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
    static final class Dollar extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
        boolean multiline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
        Dollar(boolean mul) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
            multiline = mul;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
            int endIndex = (matcher.anchoringBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
                matcher.to : matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
            if (!multiline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
                if (i < endIndex - 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
                if (i == endIndex - 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
                    char ch = seq.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
                    if (ch != '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
                    ch = seq.charAt(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
                    if (ch != '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
            // Matches before any line terminator; also matches at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
            // end of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
            // Before line terminator:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
            // If multiline, we match here no matter what
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
            // If not multiline, fall through so that the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
            // is marked as hit; this must be a /r/n or a /n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
            // at the very end so the end was hit; more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
            // could make this not match here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
            if (i < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
                char ch = seq.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
                 if (ch == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
                     // No match between \r\n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
                     if (i > 0 && seq.charAt(i-1) == '\r')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
                         return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
                     if (multiline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
                         return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
                 } else if (ch == '\r' || ch == '\u0085' ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
                            (ch|1) == '\u2029') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
                     if (multiline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
                         return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
                 } else { // No line terminator, no match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
                     return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
            // Matched at current end so hit end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
            matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
            // If a $ matches because of end of input, then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
            // could cause it to fail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
            matcher.requireEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
            next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
            return info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
     * Node to anchor at the end of a line or the end of input based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
     * multiline mode when in unix lines mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
    static final class UnixDollar extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
        boolean multiline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
        UnixDollar(boolean mul) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
            multiline = mul;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
            int endIndex = (matcher.anchoringBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
                matcher.to : matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
            if (i < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
                char ch = seq.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
                if (ch == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
                    // If not multiline, then only possible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
                    // match at very end or one before end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
                    if (multiline == false && i != endIndex - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
                    // If multiline return next.match without setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
                    // matcher.hitEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
                    if (multiline)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
                        return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
            // Matching because at the end or 1 before the end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
            // more input could change this so set hitEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
            matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
            // If a $ matches because of end of input, then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
            // could cause it to fail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
            matcher.requireEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
            next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
            return info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
    /**
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
  3888
     * 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
  3889
     */
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
  3890
    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
  3891
        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
  3892
            // (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
  3893
            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
  3894
                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
  3895
                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
  3896
                    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
  3897
                    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
  3898
                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
  3899
                    i++;
45888
ae448cf92e00 8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents: 45124
diff changeset
  3900
                    if (i < matcher.to) {
ae448cf92e00 8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents: 45124
diff changeset
  3901
                        if (seq.charAt(i) == 0x0A &&
ae448cf92e00 8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents: 45124
diff changeset
  3902
                            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
  3903
                            return true;
ae448cf92e00 8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents: 45124
diff changeset
  3904
                        }
ae448cf92e00 8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents: 45124
diff changeset
  3905
                    } else {
ae448cf92e00 8184706: Matcher doesn't indicate hitEnd after matching \u0D with \R at EOL
igerasim
parents: 45124
diff changeset
  3906
                        matcher.hitEnd = true;
44122
65a14579a2ae 8176029: Linebreak matcher is not equivalent to the pattern as stated in javadoc
sherman
parents: 43502
diff changeset
  3907
                    }
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
  3908
                    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
  3909
                }
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
            } 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
  3911
                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
  3912
            }
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
  3913
            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
  3914
        }
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
  3915
        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
  3916
            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
  3917
            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
  3918
            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
  3919
        }
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
    }
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
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
     * Abstract node class to match one character satisfying some
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
     * boolean property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3926
    static class CharProperty extends Node {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3927
        CharPredicate predicate;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3928
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3929
        CharProperty (CharPredicate predicate) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3930
            this.predicate = predicate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
            if (i < matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
                int ch = Character.codePointAt(seq, i);
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3935
                return predicate.is(ch) &&
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3936
                       next.match(matcher, i + Character.charCount(ch), seq);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
            info.minLength++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
            info.maxLength++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
     * Optimized version of CharProperty that works only for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
     * properties never satisfied by Supplementary characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3953
    private static class BmpCharProperty extends CharProperty {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3954
        BmpCharProperty (BmpCharPredicate predicate) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3955
            super(predicate);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3956
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
            if (i < matcher.to) {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3959
                return predicate.is(seq.charAt(i)) &&
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3960
                       next.match(matcher, i + 1, seq);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3968
    private static class NFCCharProperty extends Node {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3969
        CharPredicate predicate;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3970
        NFCCharProperty (CharPredicate predicate) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3971
            this.predicate = predicate;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3972
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3973
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3974
        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
  3975
            if (i < matcher.to) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3976
                int ch0 = Character.codePointAt(seq, i);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3977
                int n = Character.charCount(ch0);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3978
                int j = i + n;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3979
                while (j < matcher.to) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3980
                    int ch1 = Character.codePointAt(seq, j);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3981
                    if (Grapheme.isBoundary(ch0, ch1))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3982
                        break;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3983
                    ch0 = ch1;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3984
                    j += Character.charCount(ch1);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3985
                }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3986
                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
  3987
                    if (predicate.is(ch0))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3988
                        return next.match(matcher, j, seq);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3989
                } else {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3990
                    while (i + n < j) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3991
                        String nfc = Normalizer.normalize(
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3992
                            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
  3993
                        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
  3994
                            if (predicate.is(nfc.codePointAt(0)) &&
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3995
                                next.match(matcher, j, seq)) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3996
                                return true;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3997
                            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3998
                        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  3999
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4000
                        ch0 = Character.codePointBefore(seq, j);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4001
                        j -= Character.charCount(ch0);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4002
                    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4003
                }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4004
                if (j < matcher.to)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4005
                    return false;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4006
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4007
            matcher.hitEnd = true;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4008
            return false;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4009
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4010
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4011
        boolean study(TreeInfo info) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4012
            info.minLength++;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4013
            info.deterministic = false;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4014
            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
  4015
        }
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
  4016
    }
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
  4017
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
  4018
    /**
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4019
     * Node class that matches an unicode extended grapheme cluster
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4020
     */
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4021
    static class XGrapheme extends Node {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4022
        boolean match(Matcher matcher, int i, CharSequence seq) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4023
            if (i < matcher.to) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4024
                int ch0 = Character.codePointAt(seq, i);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4025
                    i += Character.charCount(ch0);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4026
                while (i < matcher.to) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4027
                    int ch1 = Character.codePointAt(seq, i);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4028
                    if (Grapheme.isBoundary(ch0, ch1))
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4029
                        break;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4030
                    ch0 = ch1;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4031
                    i += Character.charCount(ch1);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4032
                }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4033
                return next.match(matcher, i, seq);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4034
            }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4035
            matcher.hitEnd = true;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4036
            return false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4037
        }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4038
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4039
        boolean study(TreeInfo info) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4040
            info.minLength++;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4041
            info.deterministic = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4042
            return next.study(info);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4043
        }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4044
    }
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
     * Node class that handles grapheme boundaries
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4048
     */
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4049
    static class GraphemeBound extends Node {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4050
        boolean match(Matcher matcher, int i, CharSequence seq) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4051
            int startIndex = matcher.from;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4052
            int endIndex = matcher.to;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4053
            if (matcher.transparentBounds) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4054
                startIndex = 0;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4055
                endIndex = matcher.getTextLength();
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
            if (i == startIndex) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4058
                return next.match(matcher, i, seq);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4059
            }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4060
            if (i < endIndex) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4061
                if (Character.isSurrogatePair(seq.charAt(i-1), seq.charAt(i)) ||
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4062
                    !Grapheme.isBoundary(Character.codePointBefore(seq, i),
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4063
                                         Character.codePointAt(seq, i))) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4064
                    return false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4065
                }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4066
            } else {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4067
                matcher.hitEnd = true;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4068
                matcher.requireEnd = true;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4069
            }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4070
            return next.match(matcher, i, seq);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4071
        }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4072
    }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4073
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents: 34774
diff changeset
  4074
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4075
     * Base class for all Slice nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4076
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4077
    static class SliceNode extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4078
        int[] buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4079
        SliceNode(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4080
            buffer = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4082
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4083
            info.minLength += buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4084
            info.maxLength += buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4085
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4086
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4089
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4090
     * Node class for a case sensitive/BMP-only sequence of literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4091
     * characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4092
     */
22997
7d2259be8e79 8035076: Pattern$BnMS never used due to bug in Pattern$BnM.optimize
sherman
parents: 22078
diff changeset
  4093
    static class Slice extends SliceNode {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4094
        Slice(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4095
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4097
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4098
            int[] buf = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4099
            int len = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4100
            for (int j=0; j<len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4101
                if ((i+j) >= matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4102
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4103
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4105
                if (buf[j] != seq.charAt(i+j))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4106
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4108
            return next.match(matcher, i+len, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4113
     * Node class for a case_insensitive/BMP-only sequence of literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4114
     * characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4116
    static class SliceI extends SliceNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4117
        SliceI(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4118
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4120
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4121
            int[] buf = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4122
            int len = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4123
            for (int j=0; j<len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4124
                if ((i+j) >= matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4125
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4126
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4128
                int c = seq.charAt(i+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4129
                if (buf[j] != c &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4130
                    buf[j] != ASCII.toLower(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4131
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4133
            return next.match(matcher, i+len, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4138
     * Node class for a unicode_case_insensitive/BMP-only sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4139
     * literal characters. Uses unicode case folding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4141
    static final class SliceU extends SliceNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4142
        SliceU(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4143
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4145
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4146
            int[] buf = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4147
            int len = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4148
            for (int j=0; j<len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4149
                if ((i+j) >= matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4150
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4151
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4153
                int c = seq.charAt(i+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4154
                if (buf[j] != c &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4155
                    buf[j] != Character.toLowerCase(Character.toUpperCase(c)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4156
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4158
            return next.match(matcher, i+len, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4163
     * Node class for a case sensitive sequence of literal characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4164
     * including supplementary characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4165
     */
22997
7d2259be8e79 8035076: Pattern$BnMS never used due to bug in Pattern$BnM.optimize
sherman
parents: 22078
diff changeset
  4166
    static final class SliceS extends Slice {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4167
        SliceS(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4168
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4170
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4171
            int[] buf = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4172
            int x = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4173
            for (int j = 0; j < buf.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4174
                if (x >= matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4175
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4176
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4178
                int c = Character.codePointAt(seq, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4179
                if (buf[j] != c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4180
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4181
                x += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4182
                if (x > matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4183
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4184
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4187
            return next.match(matcher, x, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4192
     * Node class for a case insensitive sequence of literal characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4193
     * including supplementary characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4195
    static class SliceIS extends SliceNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4196
        SliceIS(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4197
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4199
        int toLower(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4200
            return ASCII.toLower(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4202
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4203
            int[] buf = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4204
            int x = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4205
            for (int j = 0; j < buf.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4206
                if (x >= matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4207
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4208
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4210
                int c = Character.codePointAt(seq, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4211
                if (buf[j] != c && buf[j] != toLower(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4212
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4213
                x += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4214
                if (x > matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4215
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4216
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4219
            return next.match(matcher, x, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4222
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4224
     * Node class for a case insensitive sequence of literal characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4225
     * Uses unicode case folding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4227
    static final class SliceUS extends SliceIS {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4228
        SliceUS(int[] buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4229
            super(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4231
        int toLower(int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4232
            return Character.toLowerCase(Character.toUpperCase(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4237
     * The 0 or 1 quantifier. This one class implements all three types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4239
    static final class Ques extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4240
        Node atom;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4241
        Qtype type;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4242
        Ques(Node node, Qtype type) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4243
            this.atom = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4244
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4246
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4247
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4248
            case GREEDY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4249
                return (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4250
                    || next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4251
            case LAZY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4252
                return next.match(matcher, i, seq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4253
                    || (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4254
            case POSSESSIVE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4255
                if (atom.match(matcher, i, seq)) i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4256
                return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4257
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4258
                return atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4261
        boolean study(TreeInfo info) {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4262
            if (type != Qtype.INDEPENDENT) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4263
                int minL = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4264
                atom.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4265
                info.minLength = minL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4266
                info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4267
                return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4268
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4269
                atom.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4270
                return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4275
    /**
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4276
     * Handles the greedy style repetition with the minimum either be
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4277
     * 0 or 1 and the maximum be MAX_REPS, for * and + quantifier.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4278
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4279
    static class CharPropertyGreedy extends Node {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4280
        final CharPredicate predicate;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4281
        final int cmin;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4282
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4283
        CharPropertyGreedy(CharProperty cp, int cmin) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4284
            this.predicate = cp.predicate;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4285
            this.cmin = cmin;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4286
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4287
        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
  4288
            int n = 0;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4289
            int to = matcher.to;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4290
            // greedy, all the way down
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4291
            while (i < to) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4292
                int ch = Character.codePointAt(seq, i);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4293
                if (!predicate.is(ch))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4294
                   break;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4295
                i += Character.charCount(ch);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4296
                n++;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4297
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4298
            if (i >= to) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4299
                matcher.hitEnd = true;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4300
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4301
            while (n >= cmin) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4302
                if (next.match(matcher, i, seq))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4303
                    return true;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4304
                if (n == cmin)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4305
                    return false;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4306
                 // backing off if match fails
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4307
                int ch = Character.codePointBefore(seq, i);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4308
                i -= Character.charCount(ch);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4309
                n--;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4310
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4311
            return false;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4312
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4313
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4314
        boolean study(TreeInfo info) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4315
            info.minLength += cmin;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4316
            if (info.maxValid) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4317
                info.maxLength += MAX_REPS;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4318
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4319
            info.deterministic = false;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4320
            return next.study(info);
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
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4324
    static final class BmpCharPropertyGreedy extends CharPropertyGreedy {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4325
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4326
        BmpCharPropertyGreedy(BmpCharProperty bcp, int cmin) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4327
            super(bcp, cmin);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4328
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4329
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4330
        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
  4331
            int n = 0;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4332
            int to = matcher.to;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4333
            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
  4334
                i++; n++;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4335
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4336
            if (i >= to) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4337
                matcher.hitEnd = true;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4338
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4339
            while (n >= cmin) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4340
                if (next.match(matcher, i, seq))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4341
                    return true;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4342
                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
  4343
            }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4344
            return false;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4345
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4346
    }
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
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4349
     * Handles the curly-brace style repetition with a specified minimum and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4350
     * maximum occurrences. The * quantifier is handled as a special case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4351
     * This class handles the three types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4353
    static final class Curly extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4354
        Node atom;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4355
        Qtype type;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4356
        int cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4357
        int cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4358
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4359
        Curly(Node node, int cmin, int cmax, Qtype type) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4360
            this.atom = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4361
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4362
            this.cmin = cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4363
            this.cmax = cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4365
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4366
            int j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4367
            for (j = 0; j < cmin; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4368
                if (atom.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4369
                    i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4370
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4372
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4373
            }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4374
            if (type == Qtype.GREEDY)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4375
                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
  4376
            else if (type == Qtype.LAZY)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4377
                return match1(matcher, i, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4378
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4379
                return match2(matcher, i, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4381
        // Greedy match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4382
        // i is the index to start matching at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4383
        // j is the number of atoms that have matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4384
        boolean match0(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4385
            if (j >= cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4386
                // We have matched the maximum... continue with the rest of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4387
                // the regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4388
                return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4390
            int backLimit = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4391
            while (atom.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4392
                // k is the length of this match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4393
                int k = matcher.last - i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4394
                if (k == 0) // Zero length match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4395
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4396
                // Move up index and number matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4397
                i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4398
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4399
                // We are greedy so match as many as we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4400
                while (j < cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4401
                    if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4402
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4403
                    if (i + k != matcher.last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4404
                        if (match0(matcher, matcher.last, j+1, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4405
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4406
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4407
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4408
                    i += k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4409
                    j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4410
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4411
                // Handle backing off if match fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4412
                while (j >= backLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4413
                   if (next.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4414
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4415
                    i -= k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4416
                    j--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4417
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4418
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4420
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4422
        // Reluctant match. At this point, the minimum has been satisfied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4423
        // i is the index to start matching at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4424
        // j is the number of atoms that have matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4425
        boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4426
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4427
                // Try finishing match without consuming any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4428
                if (next.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4429
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4430
                // At the maximum, no match found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4431
                if (j >= cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4432
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4433
                // Okay, must try one more atom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4434
                if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4435
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4436
                // If we haven't moved forward then must break out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4437
                if (i == matcher.last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4438
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4439
                // Move up index and number matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4440
                i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4441
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4443
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4444
        boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4445
            for (; j < cmax; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4446
                if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4447
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4448
                if (i == matcher.last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4449
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4450
                i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4452
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4454
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4455
            // Save original info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4456
            int minL = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4457
            int maxL = info.maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4458
            boolean maxV = info.maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4459
            boolean detm = info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4460
            info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4462
            atom.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4464
            int temp = info.minLength * cmin + minL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4465
            if (temp < minL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4466
                temp = 0xFFFFFFF; // arbitrary large number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4468
            info.minLength = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4470
            if (maxV & info.maxValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4471
                temp = info.maxLength * cmax + maxL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4472
                info.maxLength = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4473
                if (temp < maxL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4474
                    info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4475
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4476
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4477
                info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4480
            if (info.deterministic && cmin == cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4481
                info.deterministic = detm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4482
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4483
                info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4484
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4487
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4489
     * Handles the curly-brace style repetition with a specified minimum and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4490
     * maximum occurrences in deterministic cases. This is an iterative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4491
     * optimization over the Prolog and Loop system which would handle this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4492
     * in a recursive way. The * quantifier is handled as a special case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4493
     * If capture is true then this class saves group settings and ensures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4494
     * that groups are unset when backing off of a group match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4496
    static final class GroupCurly extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4497
        Node atom;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4498
        Qtype type;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4499
        int cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4500
        int cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4501
        int localIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4502
        int groupIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4503
        boolean capture;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4504
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4505
        GroupCurly(Node node, int cmin, int cmax, Qtype type, int local,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4506
                   int group, boolean capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4507
            this.atom = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4508
            this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4509
            this.cmin = cmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4510
            this.cmax = cmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4511
            this.localIndex = local;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4512
            this.groupIndex = group;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4513
            this.capture = capture;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4515
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4516
            int[] groups = matcher.groups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4517
            int[] locals = matcher.locals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4518
            int save0 = locals[localIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4519
            int save1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4520
            int save2 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4522
            if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4523
                save1 = groups[groupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4524
                save2 = groups[groupIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4527
            // Notify GroupTail there is no need to setup group info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4528
            // because it will be set here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4529
            locals[localIndex] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4531
            boolean ret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4532
            for (int j = 0; j < cmin; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4533
                if (atom.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4534
                    if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4535
                        groups[groupIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4536
                        groups[groupIndex+1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4537
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4538
                    i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4539
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4540
                    ret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4541
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4542
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4544
            if (ret) {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4545
                if (type == Qtype.GREEDY) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4546
                    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
  4547
                } else if (type == Qtype.LAZY) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4548
                    ret = match1(matcher, i, cmin, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4549
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4550
                    ret = match2(matcher, i, cmin, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4551
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4553
            if (!ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4554
                locals[localIndex] = save0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4555
                if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4556
                    groups[groupIndex] = save1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4557
                    groups[groupIndex+1] = save2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4559
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4560
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4562
        // Aggressive group match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4563
        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
  4564
            // 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
  4565
            int min = j;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4566
            int[] groups = matcher.groups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4567
            int save0 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4568
            int save1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4569
            if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4570
                save0 = groups[groupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4571
                save1 = groups[groupIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4573
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4574
                if (j >= cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4575
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4576
                if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4577
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4578
                int k = matcher.last - i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4579
                if (k <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4580
                    if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4581
                        groups[groupIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4582
                        groups[groupIndex+1] = i + k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4583
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4584
                    i = i + k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4585
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4586
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4587
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4588
                    if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4589
                        groups[groupIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4590
                        groups[groupIndex+1] = i + k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4591
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4592
                    i = i + k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4593
                    if (++j >= cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4594
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4595
                    if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4596
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4597
                    if (i + k != matcher.last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4598
                        if (match0(matcher, i, j, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4599
                            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4600
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4601
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4602
                }
17183
1e21e0bc10e7 8007395: StringIndexOutofBoundsException in Match.find() when input String contains surrogate UTF-16 characters
sherman
parents: 14342
diff changeset
  4603
                while (j > min) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4604
                    if (next.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4605
                        if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4606
                            groups[groupIndex+1] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4607
                            groups[groupIndex] = i - k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4608
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4609
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4610
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4611
                    // backing off
19604
f96e3aef2081 8023647: "abc1c".matches("(\\w)+1\\1")) returns false
sherman
parents: 19577
diff changeset
  4612
                    i = i - k;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4613
                    if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4614
                        groups[groupIndex+1] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4615
                        groups[groupIndex] = i - k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4616
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4617
                    j--;
19604
f96e3aef2081 8023647: "abc1c".matches("(\\w)+1\\1")) returns false
sherman
parents: 19577
diff changeset
  4618
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4619
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4620
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4622
            if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4623
                groups[groupIndex] = save0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4624
                groups[groupIndex+1] = save1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4625
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4626
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4628
        // Reluctant matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4629
        boolean match1(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4630
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4631
                if (next.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4632
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4633
                if (j >= cmax)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4634
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4635
                if (!atom.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4636
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4637
                if (i == matcher.last)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4638
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4639
                if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4640
                    matcher.groups[groupIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4641
                    matcher.groups[groupIndex+1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4642
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4643
                i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4644
                j++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4647
        // Possessive matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4648
        boolean match2(Matcher matcher, int i, int j, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4649
            for (; j < cmax; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4650
                if (!atom.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4651
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4653
                if (capture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4654
                    matcher.groups[groupIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4655
                    matcher.groups[groupIndex+1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4656
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4657
                if (i == matcher.last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4658
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4659
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4660
                i = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4661
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4662
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4664
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4665
            // Save original info
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4666
            int minL = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4667
            int maxL = info.maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4668
            boolean maxV = info.maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4669
            boolean detm = info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4670
            info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4672
            atom.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4674
            int temp = info.minLength * cmin + minL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4675
            if (temp < minL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4676
                temp = 0xFFFFFFF; // Arbitrary large number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4678
            info.minLength = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4680
            if (maxV & info.maxValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4681
                temp = info.maxLength * cmax + maxL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4682
                info.maxLength = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4683
                if (temp < maxL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4684
                    info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4685
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4686
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4687
                info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4690
            if (info.deterministic && cmin == cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4691
                info.deterministic = detm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4692
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4693
                info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4695
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4699
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4700
     * A Guard node at the end of each atom node in a Branch. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4701
     * serves the purpose of chaining the "match" operation to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4702
     * "next" but not the "study", so we can collect the TreeInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4703
     * of each atom node without including the TreeInfo of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4704
     * "next".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4706
    static final class BranchConn extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4707
        BranchConn() {};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4708
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4709
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4711
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4712
            return info.deterministic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4715
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4717
     * Handles the branching of alternations. Note this is also used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4718
     * the ? quantifier to branch between the case where it matches once
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4719
     * and where it does not occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4721
    static final class Branch extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4722
        Node[] atoms = new Node[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4723
        int size = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4724
        Node conn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4725
        Branch(Node first, Node second, Node branchConn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4726
            conn = branchConn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4727
            atoms[0] = first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4728
            atoms[1] = second;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4731
        void add(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4732
            if (size >= atoms.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4733
                Node[] tmp = new Node[atoms.length*2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4734
                System.arraycopy(atoms, 0, tmp, 0, atoms.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4735
                atoms = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4737
            atoms[size++] = node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4740
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4741
            for (int n = 0; n < size; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4742
                if (atoms[n] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4743
                    if (conn.next.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4744
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4745
                } else if (atoms[n].match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4746
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4747
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4748
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4749
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4752
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4753
            int minL = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4754
            int maxL = info.maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4755
            boolean maxV = info.maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4756
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4757
            int minL2 = Integer.MAX_VALUE; //arbitrary large enough num
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4758
            int maxL2 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4759
            for (int n = 0; n < size; n++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4760
                info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4761
                if (atoms[n] != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4762
                    atoms[n].study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4763
                minL2 = Math.min(minL2, info.minLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4764
                maxL2 = Math.max(maxL2, info.maxLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4765
                maxV = (maxV & info.maxValid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4766
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4768
            minL += minL2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4769
            maxL += maxL2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4771
            info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4772
            conn.next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4773
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4774
            info.minLength += minL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4775
            info.maxLength += maxL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4776
            info.maxValid &= maxV;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4777
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4778
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4782
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4783
     * The GroupHead saves the location where the group begins in the locals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4784
     * and restores them when the match is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4786
     * The matchRef is used when a reference to this group is accessed later
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4787
     * in the expression. The locals will have a negative value in them to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4788
     * indicate that we do not want to unset the group if the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4789
     * doesn't match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4791
    static final class GroupHead extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4792
        int localIndex;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4793
        GroupTail tail;    // for debug/print only, match does not need to know
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4794
        GroupHead(int localCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4795
            localIndex = localCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4797
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4798
            int save = matcher.locals[localIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4799
            matcher.locals[localIndex] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4800
            boolean ret = next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4801
            matcher.locals[localIndex] = save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4802
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4804
        boolean matchRef(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4805
            int save = matcher.locals[localIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4806
            matcher.locals[localIndex] = ~i; // HACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4807
            boolean ret = next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4808
            matcher.locals[localIndex] = save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4809
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4814
     * Recursive reference to a group in the regular expression. It calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4815
     * matchRef because if the reference fails to match we would not unset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4816
     * the group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4817
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4818
    static final class GroupRef extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4819
        GroupHead head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4820
        GroupRef(GroupHead head) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4821
            this.head = head;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4823
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4824
            return head.matchRef(matcher, i, seq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4825
                && next.match(matcher, matcher.last, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4827
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4828
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4829
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4830
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4833
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4834
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4835
     * The GroupTail handles the setting of group beginning and ending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4836
     * locations when groups are successfully matched. It must also be able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4837
     * unset groups that have to be backed off of.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4839
     * The GroupTail node is also used when a previous group is referenced,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4840
     * and in that case no group information needs to be set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4842
    static final class GroupTail extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4843
        int localIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4844
        int groupIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4845
        GroupTail(int localCount, int groupCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4846
            localIndex = localCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4847
            groupIndex = groupCount + groupCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4849
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4850
            int tmp = matcher.locals[localIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4851
            if (tmp >= 0) { // This is the normal group case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4852
                // Save the group so we can unset it if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4853
                // backs off of a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4854
                int groupStart = matcher.groups[groupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4855
                int groupEnd = matcher.groups[groupIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4857
                matcher.groups[groupIndex] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4858
                matcher.groups[groupIndex+1] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4859
                if (next.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4860
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4862
                matcher.groups[groupIndex] = groupStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4863
                matcher.groups[groupIndex+1] = groupEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4864
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4865
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4866
                // This is a group reference case. We don't need to save any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4867
                // group info because it isn't really a group.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4868
                matcher.last = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4869
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4875
     * This sets up a loop to handle a recursive quantifier structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4877
    static final class Prolog extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4878
        Loop loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4879
        Prolog(Loop loop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4880
            this.loop = loop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4882
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4883
            return loop.matchInit(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4884
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4885
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4886
            return loop.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4888
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4891
     * Handles the repetition count for a greedy Curly. The matchInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4892
     * is called from the Prolog to save the index of where the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4893
     * beginning is stored. A zero length group check occurs in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4894
     * normal match but is skipped in the matchInit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4895
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4896
    static class Loop extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4897
        Node body;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4898
        int countIndex; // local count index in matcher locals
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4899
        int beginIndex; // group beginning index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4900
        int cmin, cmax;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4901
        int posIndex;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4902
        Loop(int countIndex, int beginIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4903
            this.countIndex = countIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4904
            this.beginIndex = beginIndex;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4905
            this.posIndex = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4907
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4908
            // Avoid infinite loop in zero-length case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4909
            if (i > matcher.locals[beginIndex]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4910
                int count = matcher.locals[countIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4912
                // This block is for before we reach the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4913
                // iterations required for the loop to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4914
                if (count < cmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4915
                    matcher.locals[countIndex] = count + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4916
                    boolean b = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4917
                    // If match failed we must backtrack, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4918
                    // the loop count should NOT be incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4919
                    if (!b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4920
                        matcher.locals[countIndex] = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4921
                    // Return success or failure since we are under
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4922
                    // minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4923
                    return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4924
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4925
                // This block is for after we have the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4926
                // iterations required for the loop to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4927
                if (count < cmax) {
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4928
                    // 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
  4929
                    // 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
  4930
                    // 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
  4931
                    // again, to stop the exponential backtracking.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4932
                    if (posIndex != -1 &&
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4933
                        matcher.localsPos[posIndex].contains(i)) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4934
                        return next.match(matcher, i, seq);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4935
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4936
                    matcher.locals[countIndex] = count + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4937
                    boolean b = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4938
                    // If match failed we must backtrack, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4939
                    // 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
  4940
                    if (b)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4941
                        return true;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4942
                    matcher.locals[countIndex] = count;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4943
                    // save the failed position
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4944
                    if (posIndex != -1) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4945
                        matcher.localsPos[posIndex].add(i);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4946
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4947
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4949
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4951
        boolean matchInit(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4952
            int save = matcher.locals[countIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4953
            boolean ret = false;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4954
            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
  4955
                matcher.localsPos[posIndex] = new IntHashSet();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  4956
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4957
            if (0 < cmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4958
                matcher.locals[countIndex] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4959
                ret = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4960
            } else if (0 < cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4961
                matcher.locals[countIndex] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4962
                ret = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4963
                if (ret == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4964
                    ret = next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4965
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4966
                ret = next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4967
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4968
            matcher.locals[countIndex] = save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4969
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4970
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4971
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4972
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4973
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4974
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4978
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4979
     * Handles the repetition count for a reluctant Curly. The matchInit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4980
     * is called from the Prolog to save the index of where the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4981
     * beginning is stored. A zero length group check occurs in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4982
     * normal match but is skipped in the matchInit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4983
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4984
    static final class LazyLoop extends Loop {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4985
        LazyLoop(int countIndex, int beginIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4986
            super(countIndex, beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4988
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4989
            // Check for zero length group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4990
            if (i > matcher.locals[beginIndex]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4991
                int count = matcher.locals[countIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4992
                if (count < cmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4993
                    matcher.locals[countIndex] = count + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4994
                    boolean result = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4995
                    // If match failed we must backtrack, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4996
                    // the loop count should NOT be incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4997
                    if (!result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4998
                        matcher.locals[countIndex] = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  4999
                    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5000
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5001
                if (next.match(matcher, i, seq))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5002
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5003
                if (count < cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5004
                    matcher.locals[countIndex] = count + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5005
                    boolean result = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5006
                    // If match failed we must backtrack, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5007
                    // the loop count should NOT be incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5008
                    if (!result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5009
                        matcher.locals[countIndex] = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5010
                    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5011
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5012
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5013
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5014
            return next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5016
        boolean matchInit(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5017
            int save = matcher.locals[countIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5018
            boolean ret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5019
            if (0 < cmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5020
                matcher.locals[countIndex] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5021
                ret = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5022
            } else if (next.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5023
                ret = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5024
            } else if (0 < cmax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5025
                matcher.locals[countIndex] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5026
                ret = body.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5027
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5028
            matcher.locals[countIndex] = save;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5029
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5030
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5031
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5032
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5033
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5034
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5036
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5038
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5039
     * Refers to a group in the regular expression. Attempts to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5040
     * whatever the group referred to last matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5042
    static class BackRef extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5043
        int groupIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5044
        BackRef(int groupCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5045
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5046
            groupIndex = groupCount + groupCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5048
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5049
            int j = matcher.groups[groupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5050
            int k = matcher.groups[groupIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5052
            int groupSize = k - j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5053
            // If the referenced group didn't match, neither can this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5054
            if (j < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5055
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5057
            // If there isn't enough input left no match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5058
            if (i + groupSize > matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5059
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5060
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5061
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5062
            // Check each new char to make sure it matches what the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5063
            // referenced matched last time around
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5064
            for (int index=0; index<groupSize; index++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5065
                if (seq.charAt(i+index) != seq.charAt(j+index))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5066
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5068
            return next.match(matcher, i+groupSize, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5070
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5071
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5072
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5076
    static class CIBackRef extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5077
        int groupIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5078
        boolean doUnicodeCase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5079
        CIBackRef(int groupCount, boolean doUnicodeCase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5080
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5081
            groupIndex = groupCount + groupCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5082
            this.doUnicodeCase = doUnicodeCase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5084
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5085
            int j = matcher.groups[groupIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5086
            int k = matcher.groups[groupIndex+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5088
            int groupSize = k - j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5090
            // If the referenced group didn't match, neither can this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5091
            if (j < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5092
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5094
            // If there isn't enough input left no match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5095
            if (i + groupSize > matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5096
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5097
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5098
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5100
            // Check each new char to make sure it matches what the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5101
            // referenced matched last time around
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5102
            int x = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5103
            for (int index=0; index<groupSize; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5104
                int c1 = Character.codePointAt(seq, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5105
                int c2 = Character.codePointAt(seq, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5106
                if (c1 != c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5107
                    if (doUnicodeCase) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5108
                        int cc1 = Character.toUpperCase(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5109
                        int cc2 = Character.toUpperCase(c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5110
                        if (cc1 != cc2 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5111
                            Character.toLowerCase(cc1) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5112
                            Character.toLowerCase(cc2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5113
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5114
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5115
                        if (ASCII.toLower(c1) != ASCII.toLower(c2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5116
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5117
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5119
                x += Character.charCount(c1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5120
                j += Character.charCount(c2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5123
            return next.match(matcher, i+groupSize, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5125
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5126
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5127
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5132
     * Searches until the next instance of its atom. This is useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5133
     * finding the atom efficiently without passing an instance of it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5134
     * (greedy problem) and without a lot of wasted search time (reluctant
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5135
     * problem).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5137
    static final class First extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5138
        Node atom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5139
        First(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5140
            this.atom = BnM.optimize(node);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5142
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5143
            if (atom instanceof BnM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5144
                return atom.match(matcher, i, seq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5145
                    && next.match(matcher, matcher.last, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5147
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5148
                if (i > matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5149
                    matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5150
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5152
                if (atom.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5153
                    return next.match(matcher, matcher.last, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5155
                i += countChars(seq, i, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5156
                matcher.first++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5159
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5160
            atom.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5161
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5162
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5163
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5167
    static final class Conditional extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5168
        Node cond, yes, not;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5169
        Conditional(Node cond, Node yes, Node not) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5170
            this.cond = cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5171
            this.yes = yes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5172
            this.not = not;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5174
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5175
            if (cond.match(matcher, i, seq)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5176
                return yes.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5177
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5178
                return not.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5181
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5182
            int minL = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5183
            int maxL = info.maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5184
            boolean maxV = info.maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5185
            info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5186
            yes.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5188
            int minL2 = info.minLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5189
            int maxL2 = info.maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5190
            boolean maxV2 = info.maxValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5191
            info.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5192
            not.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5194
            info.minLength = minL + Math.min(minL2, info.minLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5195
            info.maxLength = maxL + Math.max(maxL2, info.maxLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5196
            info.maxValid = (maxV & maxV2 & info.maxValid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5197
            info.deterministic = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5198
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5203
     * Zero width positive lookahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5205
    static final class Pos extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5206
        Node cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5207
        Pos(Node cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5208
            this.cond = cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5210
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5211
            int savedTo = matcher.to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5212
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5214
            // Relax transparent region boundaries for lookahead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5215
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5216
                matcher.to = matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5217
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5218
                conditionMatched = cond.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5219
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5220
                // Reinstate region boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5221
                matcher.to = savedTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5223
            return conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5228
     * Zero width negative lookahead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5230
    static final class Neg extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5231
        Node cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5232
        Neg(Node cond) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5233
            this.cond = cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5235
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5236
            int savedTo = matcher.to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5237
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5239
            // Relax transparent region boundaries for lookahead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5240
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5241
                matcher.to = matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5242
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5243
                if (i < matcher.to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5244
                    conditionMatched = !cond.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5245
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5246
                    // If a negative lookahead succeeds then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5247
                    // could cause it to fail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5248
                    matcher.requireEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5249
                    conditionMatched = !cond.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5251
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5252
                // Reinstate region boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5253
                matcher.to = savedTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5255
            return conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5260
     * For use with lookbehinds; matches the position where the lookbehind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5261
     * was encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5263
    static Node lookbehindEnd = new Node() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5264
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5265
            return i == matcher.lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5267
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5270
     * Zero width positive lookbehind.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5272
    static class Behind extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5273
        Node cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5274
        int rmax, rmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5275
        Behind(Node cond, int rmax, int rmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5276
            this.cond = cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5277
            this.rmax = rmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5278
            this.rmin = rmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5281
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5282
            int savedFrom = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5283
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5284
            int startIndex = (!matcher.transparentBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5285
                             matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5286
            int from = Math.max(i - rmax, startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5287
            // Set end boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5288
            int savedLBT = matcher.lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5289
            matcher.lookbehindTo = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5290
            // Relax transparent region boundaries for lookbehind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5291
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5292
                matcher.from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5293
            for (int j = i - rmin; !conditionMatched && j >= from; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5294
                conditionMatched = cond.match(matcher, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5296
            matcher.from = savedFrom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5297
            matcher.lookbehindTo = savedLBT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5298
            return conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5301
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5303
     * Zero width positive lookbehind, including supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5304
     * characters or unpaired surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5306
    static final class BehindS extends Behind {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5307
        BehindS(Node cond, int rmax, int rmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5308
            super(cond, rmax, rmin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5310
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5311
            int rmaxChars = countChars(seq, i, -rmax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5312
            int rminChars = countChars(seq, i, -rmin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5313
            int savedFrom = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5314
            int startIndex = (!matcher.transparentBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5315
                             matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5316
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5317
            int from = Math.max(i - rmaxChars, startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5318
            // Set end boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5319
            int savedLBT = matcher.lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5320
            matcher.lookbehindTo = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5321
            // Relax transparent region boundaries for lookbehind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5322
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5323
                matcher.from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5325
            for (int j = i - rminChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5326
                 !conditionMatched && j >= from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5327
                 j -= j>from ? countChars(seq, j, -1) : 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5328
                conditionMatched = cond.match(matcher, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5330
            matcher.from = savedFrom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5331
            matcher.lookbehindTo = savedLBT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5332
            return conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5337
     * Zero width negative lookbehind.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5339
    static class NotBehind extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5340
        Node cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5341
        int rmax, rmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5342
        NotBehind(Node cond, int rmax, int rmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5343
            this.cond = cond;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5344
            this.rmax = rmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5345
            this.rmin = rmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5347
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5348
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5349
            int savedLBT = matcher.lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5350
            int savedFrom = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5351
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5352
            int startIndex = (!matcher.transparentBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5353
                             matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5354
            int from = Math.max(i - rmax, startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5355
            matcher.lookbehindTo = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5356
            // Relax transparent region boundaries for lookbehind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5357
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5358
                matcher.from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5359
            for (int j = i - rmin; !conditionMatched && j >= from; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5360
                conditionMatched = cond.match(matcher, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5362
            // Reinstate region boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5363
            matcher.from = savedFrom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5364
            matcher.lookbehindTo = savedLBT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5365
            return !conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5370
     * Zero width negative lookbehind, including supplementary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5371
     * characters or unpaired surrogates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5373
    static final class NotBehindS extends NotBehind {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5374
        NotBehindS(Node cond, int rmax, int rmin) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5375
            super(cond, rmax, rmin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5377
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5378
            int rmaxChars = countChars(seq, i, -rmax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5379
            int rminChars = countChars(seq, i, -rmin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5380
            int savedFrom = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5381
            int savedLBT = matcher.lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5382
            boolean conditionMatched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5383
            int startIndex = (!matcher.transparentBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5384
                             matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5385
            int from = Math.max(i - rmaxChars, startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5386
            matcher.lookbehindTo = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5387
            // Relax transparent region boundaries for lookbehind
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5388
            if (matcher.transparentBounds)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5389
                matcher.from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5390
            for (int j = i - rminChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5391
                 !conditionMatched && j >= from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5392
                 j -= j>from ? countChars(seq, j, -1) : 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5393
                conditionMatched = cond.match(matcher, j, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5395
            //Reinstate region boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5396
            matcher.from = savedFrom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5397
            matcher.lookbehindTo = savedLBT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5398
            return !conditionMatched && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5403
     * Handles word boundaries. Includes a field to allow this one class to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5404
     * deal with the different types of word boundaries we can match. The word
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5405
     * characters include underscores, letters, and digits. Non spacing marks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5406
     * can are also part of a word if they have a base character, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5407
     * they are ignored for purposes of finding word boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5409
    static final class Bound extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5410
        static int LEFT = 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5411
        static int RIGHT= 0x2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5412
        static int BOTH = 0x3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5413
        static int NONE = 0x4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5414
        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
  5415
        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
  5416
        Bound(int n, boolean useUWORD) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5417
            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
  5418
            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
  5419
        }
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5420
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5421
        boolean isWord(int ch) {
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5422
            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
  5423
                            : (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
  5424
        }
648c9add2a74 7039066: j.u.rgex does not match TR18 RL1.4 Simple Word Boundaries and RL1.2 Properties
sherman
parents: 9275
diff changeset
  5425
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5426
        int check(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5427
            int ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5428
            boolean left = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5429
            int startIndex = matcher.from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5430
            int endIndex = matcher.to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5431
            if (matcher.transparentBounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5432
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5433
                endIndex = matcher.getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5435
            if (i > startIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5436
                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
  5437
                left = (isWord(ch) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5438
                    ((Character.getType(ch) == Character.NON_SPACING_MARK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5439
                     && hasBaseCharacter(matcher, i-1, seq)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5441
            boolean right = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5442
            if (i < endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5443
                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
  5444
                right = (isWord(ch) ||
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5445
                    ((Character.getType(ch) == Character.NON_SPACING_MARK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5446
                     && hasBaseCharacter(matcher, i, seq)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5447
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5448
                // Tried to access char past the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5449
                matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5450
                // The addition of another char could wreck a boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5451
                matcher.requireEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5452
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5453
            return ((left ^ right) ? (right ? LEFT : RIGHT) : NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5455
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5456
            return (check(matcher, i, seq) & type) > 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5457
                && next.match(matcher, i, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5458
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5462
     * Non spacing marks only count as word characters in bounds calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5463
     * if they have a base character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5464
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5465
    private static boolean hasBaseCharacter(Matcher matcher, int i,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5466
                                            CharSequence seq)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5467
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5468
        int start = (!matcher.transparentBounds) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5469
            matcher.from : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5470
        for (int x=i; x >= start; x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5471
            int ch = Character.codePointAt(seq, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5472
            if (Character.isLetterOrDigit(ch))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5473
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5474
            if (Character.getType(ch) == Character.NON_SPACING_MARK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5475
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5476
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5478
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5480
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5482
     * Attempts to match a slice in the input using the Boyer-Moore string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5483
     * matching algorithm. The algorithm is based on the idea that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5484
     * pattern can be shifted farther ahead in the search text if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5485
     * matched right to left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5486
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5487
     * The pattern is compared to the input one character at a time, from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5488
     * the rightmost character in the pattern to the left. If the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5489
     * all match the pattern has been found. If a character does not match,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5490
     * the pattern is shifted right a distance that is the maximum of two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5491
     * functions, the bad character shift and the good suffix shift. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5492
     * shift moves the attempted match position through the input more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5493
     * quickly than a naive one position at a time check.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5494
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5495
     * The bad character shift is based on the character from the text that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5496
     * did not match. If the character does not appear in the pattern, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5497
     * pattern can be shifted completely beyond the bad character. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5498
     * character does occur in the pattern, the pattern can be shifted to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5499
     * line the pattern up with the next occurrence of that character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5500
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5501
     * The good suffix shift is based on the idea that some subset on the right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5502
     * side of the pattern has matched. When a bad character is found, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5503
     * pattern can be shifted right by the pattern length if the subset does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5504
     * not occur again in pattern, or by the amount of distance to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5505
     * next occurrence of the subset in the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5507
     * Boyer-Moore search methods adapted from code by Amy Yu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5509
    static class BnM extends Node {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5510
        int[] buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5511
        int[] lastOcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5512
        int[] optoSft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5514
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5515
         * Pre calculates arrays needed to generate the bad character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5516
         * shift and the good suffix shift. Only the last seven bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5517
         * are used to see if chars match; This keeps the tables small
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5518
         * and covers the heavily used ASCII range, but occasionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5519
         * results in an aliased match for the bad character shift.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5520
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5521
        static Node optimize(Node node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5522
            if (!(node instanceof Slice)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5523
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5524
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5526
            int[] src = ((Slice) node).buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5527
            int patternLength = src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5528
            // The BM algorithm requires a bit of overhead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5529
            // If the pattern is short don't use it, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5530
            // a shift larger than the pattern length cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5531
            // be used anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5532
            if (patternLength < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5533
                return node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5535
            int i, j, k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5536
            int[] lastOcc = new int[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5537
            int[] optoSft = new int[patternLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5538
            // Precalculate part of the bad character shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5539
            // It is a table for where in the pattern each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5540
            // lower 7-bit value occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5541
            for (i = 0; i < patternLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5542
                lastOcc[src[i]&0x7F] = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5544
            // Precalculate the good suffix shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5545
            // i is the shift amount being considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5546
NEXT:       for (i = patternLength; i > 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5547
                // j is the beginning index of suffix being considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5548
                for (j = patternLength - 1; j >= i; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5549
                    // Testing for good suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5550
                    if (src[j] == src[j-i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5551
                        // src[j..len] is a good suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5552
                        optoSft[j-1] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5553
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5554
                        // No match. The array has already been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5555
                        // filled up with correct values before.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5556
                        continue NEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5557
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5558
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5559
                // This fills up the remaining of optoSft
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5560
                // any suffix can not have larger shift amount
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5561
                // then its sub-suffix. Why???
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5562
                while (j > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5563
                    optoSft[--j] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5564
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5566
            // Set the guard value because of unicode compression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5567
            optoSft[patternLength-1] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5568
            if (node instanceof SliceS)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5569
                return new BnMS(src, lastOcc, optoSft, node.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5570
            return new BnM(src, lastOcc, optoSft, node.next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5572
        BnM(int[] src, int[] lastOcc, int[] optoSft, Node next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5573
            this.buffer = src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5574
            this.lastOcc = lastOcc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5575
            this.optoSft = optoSft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5576
            this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5577
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5578
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5579
            int[] src = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5580
            int patternLength = src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5581
            int last = matcher.to - patternLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5583
            // Loop over all possible match positions in text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5584
NEXT:       while (i <= last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5585
                // Loop over pattern from right to left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5586
                for (int j = patternLength - 1; j >= 0; j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5587
                    int ch = seq.charAt(i+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5588
                    if (ch != src[j]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5589
                        // Shift search to the right by the maximum of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5590
                        // bad character shift and the good suffix shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5591
                        i += Math.max(j + 1 - lastOcc[ch&0x7F], optoSft[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5592
                        continue NEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5593
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5594
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5595
                // Entire pattern matched starting at i
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5596
                matcher.first = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5597
                boolean ret = next.match(matcher, i + patternLength, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5598
                if (ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5599
                    matcher.first = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5600
                    matcher.groups[0] = matcher.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5601
                    matcher.groups[1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5602
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5603
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5604
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5606
            // BnM is only used as the leading node in the unanchored case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5607
            // and it replaced its Start() which always searches to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5608
            // if it doesn't find what it's looking for, so hitEnd is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5609
            matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5610
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5612
        boolean study(TreeInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5613
            info.minLength += buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5614
            info.maxValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5615
            return next.study(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5620
     * Supplementary support version of BnM(). Unpaired surrogates are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5621
     * also handled by this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5623
    static final class BnMS extends BnM {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5624
        int lengthInChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5625
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5626
        BnMS(int[] src, int[] lastOcc, int[] optoSft, Node next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5627
            super(src, lastOcc, optoSft, next);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  5628
            for (int cp : buffer) {
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21670
diff changeset
  5629
                lengthInChars += Character.charCount(cp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5632
        boolean match(Matcher matcher, int i, CharSequence seq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5633
            int[] src = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5634
            int patternLength = src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5635
            int last = matcher.to - lengthInChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5637
            // Loop over all possible match positions in text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5638
NEXT:       while (i <= last) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5639
                // Loop over pattern from right to left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5640
                int ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5641
                for (int j = countChars(seq, i, patternLength), x = patternLength - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5642
                     j > 0; j -= Character.charCount(ch), x--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5643
                    ch = Character.codePointBefore(seq, i+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5644
                    if (ch != src[x]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5645
                        // Shift search to the right by the maximum of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5646
                        // bad character shift and the good suffix shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5647
                        int n = Math.max(x + 1 - lastOcc[ch&0x7F], optoSft[x]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5648
                        i += countChars(seq, i, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5649
                        continue NEXT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5650
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5651
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5652
                // Entire pattern matched starting at i
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5653
                matcher.first = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5654
                boolean ret = next.match(matcher, i + lengthInChars, seq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5655
                if (ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5656
                    matcher.first = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5657
                    matcher.groups[0] = matcher.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5658
                    matcher.groups[1] = matcher.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5659
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5660
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5661
                i += countChars(seq, i, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5663
            matcher.hitEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5664
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5667
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5668
    @FunctionalInterface
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5669
    static interface CharPredicate {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5670
        boolean is(int ch);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5671
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5672
        default CharPredicate and(CharPredicate p) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5673
            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
  5674
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5675
        default CharPredicate union(CharPredicate p) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5676
            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
  5677
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5678
        default CharPredicate union(CharPredicate p1,
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5679
                                    CharPredicate p2 ) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5680
            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
  5681
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5682
        default CharPredicate negate() {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5683
            return ch -> !is(ch);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5684
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5685
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5686
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5687
    static interface BmpCharPredicate extends CharPredicate {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5688
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5689
        default CharPredicate and(CharPredicate p) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5690
            if(p instanceof BmpCharPredicate)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5691
                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
  5692
            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
  5693
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5694
        default CharPredicate union(CharPredicate p) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5695
            if (p instanceof BmpCharPredicate)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5696
                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
  5697
            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
  5698
        }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5699
        static CharPredicate union(CharPredicate... predicates) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5700
            CharPredicate cp = ch -> {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5701
                for (CharPredicate p : predicates) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5702
                    if (!p.is(ch))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5703
                        return false;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5704
                }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5705
                return true;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5706
            };
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5707
            for (CharPredicate p : predicates) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5708
                if (! (p instanceof BmpCharPredicate))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5709
                    return cp;
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
            return (BmpCharPredicate)cp;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5712
        }
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
     * matches a Perl vertical whitespace
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5717
     */
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5718
    static BmpCharPredicate VertWS() {
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5719
        return cp -> (cp >= 0x0A && cp <= 0x0D) ||
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5720
            cp == 0x85 || cp == 0x2028 || cp == 0x2029;
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5721
    }
37882
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
    /**
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5724
     * matches a Perl horizontal whitespace
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5725
     */
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5726
    static BmpCharPredicate HorizWS() {
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5727
        return cp ->
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5728
            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
  5729
            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
  5730
            cp == 0x205f || cp == 0x3000;
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5731
    }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5732
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5733
    /**
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5734
     *  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
  5735
     *  in dotall mode.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5736
     */
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5737
    static CharPredicate ALL() {
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5738
        return ch -> true;
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5739
    }
37882
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
     * 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
  5743
     */
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5744
    static CharPredicate DOT() {
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5745
        return ch ->
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5746
            (ch != '\n' && ch != '\r'
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5747
            && (ch|1) != '\u2029'
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5748
            && ch != '\u0085');
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5749
    }
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5750
37882
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
     *  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
  5753
     */
43502
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5754
    static CharPredicate UNIXDOT() {
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5755
        return ch ->  ch != '\n';
aec39566b45e 8160302: Reduce number of lambdas created when loading java.util.regex.Pattern
redestad
parents: 38777
diff changeset
  5756
    }
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5757
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5758
    /**
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5759
     * 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
  5760
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5761
    static CharPredicate SingleS(int c) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5762
        return ch -> ch == c;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5763
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5764
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5765
    /**
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5766
     * A bmp/optimized predicate of single
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5767
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5768
    static BmpCharPredicate Single(int c) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5769
        return ch -> ch == c;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5770
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5771
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5772
    /**
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5773
     * 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
  5774
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5775
    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
  5776
        return ch -> ch == lower || ch == upper;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5777
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5778
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5779
    /**
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5780
     * 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
  5781
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5782
    static CharPredicate SingleU(int lower) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5783
        return ch -> lower == ch ||
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5784
                     lower == Character.toLowerCase(Character.toUpperCase(ch));
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5785
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5786
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5787
    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
  5788
        return lower <= ch && ch <= upper;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5789
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5790
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5791
    /**
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5792
     * Charactrs within a explicit value range
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5793
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5794
    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
  5795
        if (upper < Character.MIN_HIGH_SURROGATE ||
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5796
            lower > Character.MAX_HIGH_SURROGATE &&
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5797
            upper < Character.MIN_SUPPLEMENTARY_CODE_POINT)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5798
            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
  5799
        return ch -> inRange(lower, ch, upper);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5800
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5801
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5802
   /**
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5803
    * 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
  5804
    */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5805
    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
  5806
        return ch -> inRange(lower, ch, upper) ||
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5807
                     ASCII.isAscii(ch) &&
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5808
                     (inRange(lower, ASCII.toUpper(ch), upper) ||
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5809
                      inRange(lower, ASCII.toLower(ch), upper));
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5810
    }
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5811
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5812
    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
  5813
        return ch -> {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5814
            if (inRange(lower, ch, upper))
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5815
                return true;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5816
            int up = Character.toUpperCase(ch);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5817
            return inRange(lower, up, upper) ||
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5818
                   inRange(lower, Character.toLowerCase(up), upper);
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5819
        };
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5820
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5822
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5823
     *  This must be the very first initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5824
     */
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5825
    static final Node accept = new Node();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5826
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 37880
diff changeset
  5827
    static final Node lastAccept = new LastNode();
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5828
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5829
    /**
49554
f088ec60bed5 8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents: 48853
diff changeset
  5830
     * 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
  5831
     * string.
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5832
     *
49554
f088ec60bed5 8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents: 48853
diff changeset
  5833
     * @apiNote
f088ec60bed5 8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents: 48853
diff changeset
  5834
     * 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
  5835
     * from the input sequence and then calls {@code find}, for example a
f088ec60bed5 8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents: 48853
diff changeset
  5836
     * predicate of the form:
f088ec60bed5 8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents: 48853
diff changeset
  5837
     * <pre>{@code
f088ec60bed5 8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents: 48853
diff changeset
  5838
     *   s -> matcher(s).find();
f088ec60bed5 8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents: 48853
diff changeset
  5839
     * }</pre>
f088ec60bed5 8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents: 48853
diff changeset
  5840
     *
f088ec60bed5 8164781: Pattern.asPredicate specification is incomplete
vtheeyarath
parents: 48853
diff changeset
  5841
     * @return  The predicate which can be used for finding a match on a
49763
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5842
     *          subsequence of a string
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5843
     * @since   1.8
49763
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5844
     * @see     Matcher#find
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5845
     */
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5846
    public Predicate<String> asPredicate() {
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5847
        return s -> matcher(s).find();
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5848
    }
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5849
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5850
    /**
49763
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5851
     * Creates a predicate that tests if this pattern matches a given input string.
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5852
     *
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5853
     * @apiNote
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5854
     * This method creates a predicate that behaves as if it creates a matcher
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5855
     * from the input sequence and then calls {@code matches}, for example a
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5856
     * predicate of the form:
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5857
     * <pre>{@code
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5858
     *   s -> matcher(s).matches();
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5859
     * }</pre>
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5860
     *
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5861
     * @return  The predicate which can be used for matching an input string
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5862
     *          against this pattern.
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5863
     * @since   11
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5864
     * @see     Matcher#matches
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5865
     */
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5866
    public Predicate<String> asMatchPredicate() {
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5867
        return s -> matcher(s).matches();
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5868
    }
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5869
1ccf5fae9664 8184692: add Pattern.asMatchPredicate
vtheeyarath
parents: 49554
diff changeset
  5870
    /**
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5871
     * Creates a stream from the given input sequence around matches of this
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5872
     * pattern.
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5873
     *
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5874
     * <p> The stream returned by this method contains each substring of the
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5875
     * input sequence that is terminated by another subsequence that matches
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5876
     * this pattern or is terminated by the end of the input sequence.  The
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5877
     * 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
  5878
     * 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
  5879
     * the stream.
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5880
     *
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5881
     * <p> If this pattern does not match any subsequence of the input then
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5882
     * the resulting stream has just one element, namely the input sequence in
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5883
     * string form.
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5884
     *
21668
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  5885
     * <p> When there is a positive-width match at the beginning of the input
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  5886
     * sequence then an empty leading substring is included at the beginning
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  5887
     * of the stream. A zero-width match at the beginning however never produces
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  5888
     * such empty leading substring.
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  5889
     *
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5890
     * <p> If the input sequence is mutable, it must remain constant during the
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5891
     * execution of the terminal stream operation.  Otherwise, the result of the
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5892
     * terminal stream operation is undefined.
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5893
     *
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5894
     * @param   input
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5895
     *          The character sequence to be split
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5896
     *
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5897
     * @return  The stream of strings computed by splitting the input
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5898
     *          around matches of this pattern
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5899
     * @see     #split(CharSequence)
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5900
     * @since   1.8
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5901
     */
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5902
    public Stream<String> splitAsStream(final CharSequence input) {
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5903
        class MatcherIterator implements Iterator<String> {
34684
b721350c05c0 8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents: 32649
diff changeset
  5904
            private Matcher matcher;
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5905
            // The start position of the next sub-sequence of input
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5906
            // when current == input.length there are no more elements
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5907
            private int current;
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5908
            // null if the next element, if any, needs to obtained
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5909
            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
  5910
            // > 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
  5911
            private int emptyElementCount;
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5912
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5913
            public String next() {
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5914
                if (!hasNext())
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5915
                    throw new NoSuchElementException();
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5916
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
  5917
                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
  5918
                    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
  5919
                    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
  5920
                    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
  5921
                } 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
  5922
                    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
  5923
                    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
  5924
                }
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5925
            }
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5926
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5927
            public boolean hasNext() {
34684
b721350c05c0 8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents: 32649
diff changeset
  5928
                if (matcher == null) {
b721350c05c0 8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents: 32649
diff changeset
  5929
                    matcher = matcher(input);
b721350c05c0 8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents: 32649
diff changeset
  5930
                    // 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
  5931
                    // 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
  5932
                    // element count to 1
b721350c05c0 8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents: 32649
diff changeset
  5933
                    emptyElementCount = input.length() == 0 ? 1 : 0;
b721350c05c0 8145007: Pattern splitAsStream is not late binding as required by the specification
psandoz
parents: 32649
diff changeset
  5934
                }
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
  5935
                if (nextElement != null || emptyElementCount > 0)
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5936
                    return true;
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5937
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5938
                if (current == input.length())
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5939
                    return false;
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5940
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
  5941
                // 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
  5942
                // 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
  5943
                while (matcher.find()) {
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5944
                    nextElement = input.subSequence(current, matcher.start()).toString();
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5945
                    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
  5946
                    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
  5947
                        return true;
21668
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  5948
                    } else if (current > 0) { // no empty leading substring for zero-width
b62ce4a9635f 8027645: Pattern.split() with positive lookahead
sherman
parents: 21428
diff changeset
  5949
                                              // 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
  5950
                        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
  5951
                    }
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
  5952
                }
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
  5953
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
  5954
                // 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
  5955
                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
  5956
                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
  5957
                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
  5958
                    return true;
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5959
                } 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
  5960
                    // 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
  5961
                    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
  5962
                    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
  5963
                    return false;
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5964
                }
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5965
            }
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5966
        }
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5967
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18787
diff changeset
  5968
                new MatcherIterator(), Spliterator.ORDERED | Spliterator.NONNULL), false);
17447
3031deba3fbe 8012646: Pattern.splitAsStream
psandoz
parents: 17434
diff changeset
  5969
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  5970
}