src/java.base/share/classes/java/util/regex/Matcher.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 50340 d52bba1f19aa
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 50340
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: 4161
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: 4161
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: 4161
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4161
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4161
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
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
    28
import java.util.ConcurrentModificationException;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
    29
import java.util.Iterator;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
    30
import java.util.NoSuchElementException;
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
    31
import java.util.Objects;
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
    32
import java.util.Spliterator;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
    33
import java.util.Spliterators;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
    34
import java.util.function.Consumer;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
    35
import java.util.function.Function;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
    36
import java.util.stream.Stream;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
    37
import java.util.stream.StreamSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
    40
 * An engine that performs match operations on a {@linkplain
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
    41
 * java.lang.CharSequence character sequence} by interpreting a {@link Pattern}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> A matcher is created from a pattern by invoking the pattern's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Pattern#matcher matcher} method.  Once created, a matcher can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * perform three different kinds of match operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   <li><p> The {@link #matches matches} method attempts to match the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *   input sequence against the pattern.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   <li><p> The {@link #lookingAt lookingAt} method attempts to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   input sequence, starting at the beginning, against the pattern.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
    55
 *   <li><p> The {@link #find find} method scans the input sequence looking
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
    56
 *   for the next subsequence that matches the pattern.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p> Each of these methods returns a boolean indicating success or failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * More information about a successful match can be obtained by querying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * state of the matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <p> A matcher finds matches in a subset of its input called the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <i>region</i>. By default, the region contains all of the matcher's input.
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
    66
 * The region can be modified via the {@link #region(int, int) region} method
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
    67
 * and queried via the {@link #regionStart() regionStart} and {@link
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
    68
 * #regionEnd() regionEnd} methods. The way that the region boundaries interact
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
    69
 * with some pattern constructs can be changed. See {@link
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
    70
 * #useAnchoringBounds(boolean) useAnchoringBounds} and {@link
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
    71
 * #useTransparentBounds(boolean) useTransparentBounds} for more details.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <p> This class also defines methods for replacing matched subsequences with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * new strings whose contents can, if desired, be computed from the match
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * result.  The {@link #appendReplacement appendReplacement} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * #appendTail appendTail} methods can be used in tandem in order to collect
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
    77
 * the result into an existing string buffer or string builder. Alternatively,
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
    78
 * the more convenient {@link #replaceAll replaceAll} method can be used to
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
    79
 * create a string in which every matching subsequence in the input sequence
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
    80
 * is replaced.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p> The explicit state of a matcher includes the start and end indices of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * the most recent successful match.  It also includes the start and end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * indices of the input subsequence captured by each <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * href="Pattern.html#cg">capturing group</a> in the pattern as well as a total
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * count of such subsequences.  As a convenience, methods are also provided for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * returning these captured subsequences in string form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p> The explicit state of a matcher is initially undefined; attempting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * query any part of it before a successful match will cause an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * IllegalStateException} to be thrown.  The explicit state of a matcher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * recomputed by every match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p> The implicit state of a matcher includes the input character sequence as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * well as the <i>append position</i>, which is initially zero and is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * by the {@link #appendReplacement appendReplacement} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <p> A matcher may be reset explicitly by invoking its {@link #reset()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * method or, if a new input sequence is desired, its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * #reset(java.lang.CharSequence) reset(CharSequence)} method.  Resetting a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * matcher discards its explicit state information and sets the append position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <p> Instances of this class are not safe for use by multiple concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * threads. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * @author      Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * @author      JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * @spec        JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
public final class Matcher implements MatchResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * The Pattern object that created this Matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    Pattern parentPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The storage used by groups. They may contain invalid values if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * a group was skipped during the matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    int[] groups;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * The range within the sequence that is to be matched. Anchors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * will match at these "hard" boundaries. Changing the region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * changes these values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    int from, to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Lookbehind uses this value to ensure that the subexpression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * match ends at the point where the lookbehind was encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    int lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The original string being matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    CharSequence text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Matcher state used by the last node. NOANCHOR is used when a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * match does not have to consume all of the input. ENDANCHOR is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * the mode used for matching all the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static final int ENDANCHOR = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    static final int NOANCHOR = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    int acceptMode = NOANCHOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * The range of string that last matched the pattern. If the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * match failed then first is -1; last initially holds 0 then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * holds the index of the end of the last match (which is where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * next search starts).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    int first = -1, last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * The end index of what matched in the last match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    int oldLast = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * The index of the last position appended in a substitution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    int lastAppendPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Storage used by nodes to tell what repetition they are on in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * a pattern, and where groups begin. The nodes themselves are stateless,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * so they rely on this field to hold state during a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    int[] locals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   181
     * Storage used by top greedy Loop node to store a specific hash set to
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   182
     * keep the beginning index of the failed repetition match. The nodes
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   183
     * themselves are stateless, so they rely on this field to hold state
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   184
     * during a match.
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   185
     */
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   186
    IntHashSet[] localsPos;
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   187
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   188
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Boolean indicating whether or not more input could change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * the results of the last match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * If hitEnd is true, and a match was found, then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * might cause a different match to be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * If hitEnd is true and a match was not found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * input could cause a match to be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * If hitEnd is false and a match was found, then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * will not change the match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * If hitEnd is false and a match was not found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * input will not cause a match to be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    boolean hitEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Boolean indicating whether or not more input could change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * a positive match into a negative one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * If requireEnd is true, and a match was found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * input could cause the match to be lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * If requireEnd is false and a match was found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * input might change the match but the match won't be lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * If a match was not found, then requireEnd has no meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    boolean requireEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * If transparentBounds is true then the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * matcher's region are transparent to lookahead, lookbehind,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * and boundary matching constructs that try to see beyond them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    boolean transparentBounds = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * If anchoringBounds is true then the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * matcher's region match anchors such as ^ and $.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    boolean anchoringBounds = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   229
     * Number of times this matcher's state has been modified
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   230
     */
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   231
    int modCount;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   232
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   233
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * No default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    Matcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * All matchers have the state used by Pattern during a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    Matcher(Pattern parent, CharSequence text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        this.parentPattern = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // Allocate state storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        int parentGroupCount = Math.max(parent.capturingGroupCount, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        groups = new int[parentGroupCount * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        locals = new int[parent.localCount];
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   250
        localsPos = new IntHashSet[parent.localTCNCount];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // Put fields into initial states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Returns the pattern that is interpreted by this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @return  The pattern for which this matcher was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public Pattern pattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return parentPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Returns the match state of this matcher as a {@link MatchResult}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * The result is unaffected by subsequent operations performed upon this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   270
     * @return  a {@code MatchResult} with the state of this matcher
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public MatchResult toMatchResult() {
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   274
        return toMatchResult(text.toString());
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   275
    }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   276
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   277
    private MatchResult toMatchResult(String text) {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   278
        return new ImmutableMatchResult(this.first,
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   279
                                        this.last,
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   280
                                        groupCount(),
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   281
                                        this.groups.clone(),
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   282
                                        text);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   283
    }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   284
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   285
    private static class ImmutableMatchResult implements MatchResult {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   286
        private final int first;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   287
        private final int last;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   288
        private final int[] groups;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   289
        private final int groupCount;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   290
        private final String text;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   291
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   292
        ImmutableMatchResult(int first, int last, int groupCount,
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   293
                             int groups[], String text)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   294
        {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   295
            this.first = first;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   296
            this.last = last;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   297
            this.groupCount = groupCount;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   298
            this.groups = groups;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   299
            this.text = text;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   300
        }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   301
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   302
        @Override
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   303
        public int start() {
29503
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   304
            checkMatch();
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   305
            return first;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   306
        }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   307
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   308
        @Override
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   309
        public int start(int group) {
29503
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   310
            checkMatch();
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   311
            if (group < 0 || group > groupCount)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   312
                throw new IndexOutOfBoundsException("No group " + group);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   313
            return groups[group * 2];
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   314
        }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   315
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   316
        @Override
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   317
        public int end() {
29503
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   318
            checkMatch();
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   319
            return last;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   320
        }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   321
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   322
        @Override
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   323
        public int end(int group) {
29503
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   324
            checkMatch();
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   325
            if (group < 0 || group > groupCount)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   326
                throw new IndexOutOfBoundsException("No group " + group);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   327
            return groups[group * 2 + 1];
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   328
        }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   329
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   330
        @Override
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   331
        public int groupCount() {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   332
            return groupCount;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   333
        }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   334
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   335
        @Override
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   336
        public String group() {
29503
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   337
            checkMatch();
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   338
            return group(0);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   339
        }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   340
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   341
        @Override
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   342
        public String group(int group) {
29503
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   343
            checkMatch();
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   344
            if (group < 0 || group > groupCount)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   345
                throw new IndexOutOfBoundsException("No group " + group);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   346
            if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   347
                return null;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   348
            return text.subSequence(groups[group * 2], groups[group * 2 + 1]).toString();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   349
        }
29503
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   350
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   351
        private void checkMatch() {
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   352
            if (first < 0)
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   353
                throw new IllegalStateException("No match found");
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   354
95318f222149 8074678: JCK test java_util/regex/MatchResult/index.html starts failing after JDK-8071479
sherman
parents: 29380
diff changeset
   355
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   359
      * Changes the {@code Pattern} that this {@code Matcher} uses to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
      * find matches with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
      * <p> This method causes this matcher to lose information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
      * about the groups of the last match that occurred. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
      * matcher's position in the input is maintained and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
      * last append position is unaffected.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
      * @param  newPattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
      *         The new pattern used by this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
      * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
      * @throws  IllegalArgumentException
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   371
      *          If newPattern is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
      * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    public Matcher usePattern(Pattern newPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (newPattern == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            throw new IllegalArgumentException("Pattern cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        parentPattern = newPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        // Reallocate state storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        groups = new int[parentGroupCount * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        locals = new int[newPattern.localCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        for (int i = 0; i < locals.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            locals[i] = -1;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   387
        localsPos = new IntHashSet[parentPattern.localTCNCount];
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   388
        modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Resets this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * <p> Resetting a matcher discards all of its explicit state information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * and sets its append position to zero. The matcher's region is set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * default region, which is its entire character sequence. The anchoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * and transparency of this matcher's region boundaries are unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public Matcher reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        first = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        oldLast = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        for(int i=0; i<groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        for(int i=0; i<locals.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            locals[i] = -1;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   410
        for (int i = 0; i < localsPos.length; i++) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   411
            if (localsPos[i] != null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   412
                localsPos[i].clear();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
   413
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        lastAppendPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        to = getTextLength();
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   417
        modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Resets this matcher with a new input sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <p> Resetting a matcher discards all of its explicit state information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * and sets its append position to zero.  The matcher's region is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * the default region, which is its entire character sequence.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * anchoring and transparency of this matcher's region boundaries are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *         The new input character sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public Matcher reset(CharSequence input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        text = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
   441
     * Returns the start index of the previous match.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return  The index of the first character matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public int start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            throw new IllegalStateException("No match available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        return first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Returns the start index of the subsequence captured by the given group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * during the previous match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * to right, starting at one.  Group zero denotes the entire pattern, so
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   461
     * the expression <i>m.</i>{@code start(0)} is equivalent to
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   462
     * <i>m.</i>{@code start()}.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @param  group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *         The index of a capturing group in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @return  The index of the first character captured by the group,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   468
     *          or {@code -1} if the match was successful but the group
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *          itself did not match anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *          If there is no capturing group in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *          with the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public int start(int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            throw new IllegalStateException("No match available");
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   482
        if (group < 0 || group > groupCount())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            throw new IndexOutOfBoundsException("No group " + group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return groups[group * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   488
     * Returns the start index of the subsequence captured by the given
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   489
     * <a href="Pattern.html#groupname">named-capturing group</a> during the
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   490
     * previous match operation.
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   491
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   492
     * @param  name
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   493
     *         The name of a named-capturing group in this matcher's pattern
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   494
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   495
     * @return  The index of the first character captured by the group,
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   496
     *          or {@code -1} if the match was successful but the group
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   497
     *          itself did not match anything
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   498
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   499
     * @throws  IllegalStateException
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   500
     *          If no match has yet been attempted,
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   501
     *          or if the previous match operation failed
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   502
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   503
     * @throws  IllegalArgumentException
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   504
     *          If there is no capturing group in the pattern
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   505
     *          with the given name
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   506
     * @since 1.8
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   507
     */
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   508
    public int start(String name) {
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   509
        return groups[getMatchedGroupIndex(name) * 2];
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   510
    }
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   511
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   512
    /**
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
   513
     * Returns the offset after the last character matched.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @return  The offset after the last character matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public int end() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            throw new IllegalStateException("No match available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Returns the offset after the last character of the subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * captured by the given group during the previous match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * to right, starting at one.  Group zero denotes the entire pattern, so
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   533
     * the expression <i>m.</i>{@code end(0)} is equivalent to
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   534
     * <i>m.</i>{@code end()}.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param  group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *         The index of a capturing group in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @return  The offset after the last character captured by the group,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   540
     *          or {@code -1} if the match was successful
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *          but the group itself did not match anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *          If there is no capturing group in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *          with the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    public int end(int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            throw new IllegalStateException("No match available");
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   554
        if (group < 0 || group > groupCount())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            throw new IndexOutOfBoundsException("No group " + group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        return groups[group * 2 + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   560
     * Returns the offset after the last character of the subsequence
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   561
     * captured by the given <a href="Pattern.html#groupname">named-capturing
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   562
     * group</a> during the previous match operation.
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   563
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   564
     * @param  name
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   565
     *         The name of a named-capturing group in this matcher's pattern
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   566
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   567
     * @return  The offset after the last character captured by the group,
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   568
     *          or {@code -1} if the match was successful
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   569
     *          but the group itself did not match anything
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   570
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   571
     * @throws  IllegalStateException
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   572
     *          If no match has yet been attempted,
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   573
     *          or if the previous match operation failed
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   574
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   575
     * @throws  IllegalArgumentException
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   576
     *          If there is no capturing group in the pattern
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   577
     *          with the given name
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   578
     * @since 1.8
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   579
     */
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   580
    public int end(String name) {
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   581
        return groups[getMatchedGroupIndex(name) * 2 + 1];
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   582
    }
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   583
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   584
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * Returns the input subsequence matched by the previous match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <p> For a matcher <i>m</i> with input sequence <i>s</i>,
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   588
     * the expressions <i>m.</i>{@code group()} and
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
   589
     * <i>s.</i>{@code substring(}<i>m.</i>{@code start(),}&nbsp;<i>m.</i>
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
   590
     * {@code end())} are equivalent.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   592
     * <p> Note that some patterns, for example {@code a*}, match the empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * string.  This method will return the empty string when the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * successfully matches the empty string in the input.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @return The (possibly empty) subsequence matched by the previous match,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *         in string form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    public String group() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        return group(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Returns the input subsequence captured by the given group during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * previous match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * <p> For a matcher <i>m</i>, input sequence <i>s</i>, and group index
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   612
     * <i>g</i>, the expressions <i>m.</i>{@code group(}<i>g</i>{@code )} and
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   613
     * <i>s.</i>{@code substring(}<i>m.</i>{@code start(}<i>g</i>{@code
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   614
     * ),}&nbsp;<i>m.</i>{@code end(}<i>g</i>{@code ))}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * are equivalent.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * to right, starting at one.  Group zero denotes the entire pattern, so
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   619
     * the expression {@code m.group(0)} is equivalent to {@code m.group()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * <p> If the match was successful but the group specified failed to match
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   623
     * any part of the input sequence, then {@code null} is returned. Note
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   624
     * that some groups, for example {@code (a*)}, match the empty string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * This method will return the empty string when such a group successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * matches the empty string in the input.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @param  group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *         The index of a capturing group in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @return  The (possibly empty) subsequence captured by the group
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   632
     *          during the previous match, or {@code null} if the group
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *          failed to match part of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *          If there is no capturing group in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *          with the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public String group(int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            throw new IllegalStateException("No match found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (group < 0 || group > groupCount())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            throw new IndexOutOfBoundsException("No group " + group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   654
     * Returns the input subsequence captured by the given
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
   655
     * <a href="Pattern.html#groupname">named-capturing group</a> during the
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
   656
     * previous match operation.
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   657
     *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   658
     * <p> If the match was successful but the group specified failed to match
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   659
     * any part of the input sequence, then {@code null} is returned. Note
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   660
     * that some groups, for example {@code (a*)}, match the empty string.
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   661
     * This method will return the empty string when such a group successfully
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   662
     * matches the empty string in the input.  </p>
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   663
     *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   664
     * @param  name
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   665
     *         The name of a named-capturing group in this matcher's pattern
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   666
     *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   667
     * @return  The (possibly empty) subsequence captured by the named group
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   668
     *          during the previous match, or {@code null} if the group
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   669
     *          failed to match part of the input
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   670
     *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   671
     * @throws  IllegalStateException
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   672
     *          If no match has yet been attempted,
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   673
     *          or if the previous match operation failed
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   674
     *
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   675
     * @throws  IllegalArgumentException
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   676
     *          If there is no capturing group in the pattern
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   677
     *          with the given name
10345
b4aebbfc5b3a 7066490: @since 1.7 tag is missing for java.util.regex.Matcher.group(java.lang.String)
sherman
parents: 5506
diff changeset
   678
     * @since 1.7
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   679
     */
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   680
    public String group(String name) {
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
   681
        int group = getMatchedGroupIndex(name);
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   682
        if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   683
            return null;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   684
        return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   685
    }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   686
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   687
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Returns the number of capturing groups in this matcher's pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <p> Group zero denotes the entire pattern by convention. It is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * included in this count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * <p> Any non-negative integer smaller than or equal to the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * returned by this method is guaranteed to be a valid group index for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * this matcher.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @return The number of capturing groups in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    public int groupCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        return parentPattern.capturingGroupCount - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Attempts to match the entire region against the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <p> If the match succeeds then more information can be obtained via the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   707
     * {@code start}, {@code end}, and {@code group} methods.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   709
     * @return  {@code true} if, and only if, the entire region sequence
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *          matches this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    public boolean matches() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        return match(from, ENDANCHOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Attempts to find the next subsequence of the input sequence that matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <p> This method starts at the beginning of this matcher's region, or, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * a previous invocation of the method was successful and the matcher has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * not since been reset, at the first character not matched by the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * <p> If the match succeeds then more information can be obtained via the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   726
     * {@code start}, {@code end}, and {@code group} methods.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   728
     * @return  {@code true} if, and only if, a subsequence of the input
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *          sequence matches this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    public boolean find() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        int nextSearchIndex = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        if (nextSearchIndex == first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            nextSearchIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        // If next search starts before region, start it at region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (nextSearchIndex < from)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            nextSearchIndex = from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        // If next search starts beyond region then it fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (nextSearchIndex > to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        return search(nextSearchIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * Resets this matcher and then attempts to find the next subsequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * the input sequence that matches the pattern, starting at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * <p> If the match succeeds then more information can be obtained via the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   755
     * {@code start}, {@code end}, and {@code group} methods, and subsequent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * invocations of the {@link #find()} method will start at the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * character not matched by this match.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
   759
     * @param start the index to start searching for a match
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *          If start is less than zero or if start is greater than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *          length of the input sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   764
     * @return  {@code true} if, and only if, a subsequence of the input
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *          sequence starting at the given index matches this matcher's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *          pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    public boolean find(int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        int limit = getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if ((start < 0) || (start > limit))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            throw new IndexOutOfBoundsException("Illegal start index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        return search(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Attempts to match the input sequence, starting at the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * region, against the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * <p> Like the {@link #matches matches} method, this method always starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * at the beginning of the region; unlike that method, it does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * require that the entire region be matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * <p> If the match succeeds then more information can be obtained via the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   785
     * {@code start}, {@code end}, and {@code group} methods.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   787
     * @return  {@code true} if, and only if, a prefix of the input
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *          sequence matches this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    public boolean lookingAt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        return match(from, NOANCHOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /**
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   795
     * Returns a literal replacement {@code String} for the specified
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   796
     * {@code String}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   798
     * This method produces a {@code String} that will work
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   799
     * as a literal replacement {@code s} in the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   800
     * {@code appendReplacement} method of the {@link Matcher} class.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   801
     * The {@code String} produced will match the sequence of characters
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   802
     * in {@code s} treated as a literal sequence. Slashes ('\') and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * dollar signs ('$') will be given no special meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * @param  s The string to be literalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @return  A literal string replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    public static String quoteReplacement(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        for (int i=0; i<s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            if (c == '\\' || c == '$') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                sb.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * Implements a non-terminal append-and-replace step.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * <p> This method performs the following actions: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     *   <li><p> It reads characters from the input sequence, starting at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *   append position, and appends them to the given string buffer.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *   stops after reading the last character preceding the previous match,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *   that is, the character at index {@link
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   834
     *   #start()}&nbsp;{@code -}&nbsp;{@code 1}.  </p></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *   <li><p> It appends the given replacement string to the string buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *   <li><p> It sets the append position of this matcher to the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     *   the last character matched, plus one, that is, to {@link #end()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * <p> The replacement string may contain references to subsequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * captured during the previous match: Each occurrence of
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   847
     * <code>${</code><i>name</i><code>}</code> or {@code $}<i>g</i>
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   848
     * will be replaced by the result of evaluating the corresponding
18787
8a57ff107238 8020095: Fix doclint warnings in java.util.regex
darcy
parents: 17434
diff changeset
   849
     * {@link #group(String) group(name)} or {@link #group(int) group(g)}
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   850
     * respectively. For {@code $}<i>g</i>,
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   851
     * the first number after the {@code $} is always treated as part of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * the group reference. Subsequent numbers are incorporated into g if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * they would form a legal group reference. Only the numerals '0'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * through '9' are considered as potential components of the group
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   855
     * reference. If the second group matched the string {@code "foo"}, for
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   856
     * example, then passing the replacement string {@code "$2bar"} would
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   857
     * cause {@code "foobar"} to be appended to the string buffer. A dollar
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   858
     * sign ({@code $}) may be included as a literal in the replacement
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   859
     * string by preceding it with a backslash ({@code \$}).
2
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> Note that backslashes ({@code \}) and dollar signs ({@code $}) in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * the replacement string may cause the results to be different than if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * were being treated as a literal replacement string. Dollar signs may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * treated as references to captured subsequences as described above, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * backslashes are used to escape literal characters in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * <p> This method is intended to be used in a loop together with the
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
   869
     * {@link #appendTail(StringBuffer) appendTail} and {@link #find() find}
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
   870
     * methods.  The following code, for example, writes {@code one dog two dogs
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
   871
     * in the yard} to the standard-output stream: </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Pattern p = Pattern.compile("cat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * Matcher m = p.matcher("one cat two cats in the yard");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * while (m.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *     m.appendReplacement(sb, "dog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * m.appendTail(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * System.out.println(sb.toString());</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @param  sb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *         The target string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @param  replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *         The replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   895
     * @throws  IllegalArgumentException
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   896
     *          If the replacement string refers to a named-capturing
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   897
     *          group that does not exist in the pattern
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
   898
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *          If the replacement string refers to a capturing group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     *          that does not exist in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    public Matcher appendReplacement(StringBuffer sb, String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        // If no match, return error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            throw new IllegalStateException("No match available");
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   907
        StringBuilder result = new StringBuilder();
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   908
        appendExpandedReplacement(replacement, result);
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   909
        // Append the intervening text
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   910
        sb.append(text, lastAppendPosition, first);
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   911
        // Append the match substitution
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   912
        sb.append(result);
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   913
        lastAppendPosition = last;
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
   914
        modCount++;
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   915
        return this;
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   916
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   918
    /**
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   919
     * Implements a non-terminal append-and-replace step.
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   920
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   921
     * <p> This method performs the following actions: </p>
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   922
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   923
     * <ol>
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   924
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   925
     *   <li><p> It reads characters from the input sequence, starting at the
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   926
     *   append position, and appends them to the given string builder.  It
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   927
     *   stops after reading the last character preceding the previous match,
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   928
     *   that is, the character at index {@link
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   929
     *   #start()}&nbsp;{@code -}&nbsp;{@code 1}.  </p></li>
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   930
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   931
     *   <li><p> It appends the given replacement string to the string builder.
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   932
     *   </p></li>
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   933
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   934
     *   <li><p> It sets the append position of this matcher to the index of
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   935
     *   the last character matched, plus one, that is, to {@link #end()}.
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   936
     *   </p></li>
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   937
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   938
     * </ol>
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   939
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   940
     * <p> The replacement string may contain references to subsequences
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   941
     * captured during the previous match: Each occurrence of
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   942
     * {@code $}<i>g</i> will be replaced by the result of
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   943
     * evaluating {@link #group(int) group}{@code (}<i>g</i>{@code )}.
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   944
     * The first number after the {@code $} is always treated as part of
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   945
     * the group reference. Subsequent numbers are incorporated into g if
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   946
     * they would form a legal group reference. Only the numerals '0'
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   947
     * through '9' are considered as potential components of the group
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   948
     * reference. If the second group matched the string {@code "foo"}, for
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   949
     * example, then passing the replacement string {@code "$2bar"} would
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   950
     * cause {@code "foobar"} to be appended to the string builder. A dollar
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   951
     * sign ({@code $}) may be included as a literal in the replacement
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   952
     * string by preceding it with a backslash ({@code \$}).
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   953
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
   954
     * <p> Note that backslashes ({@code \}) and dollar signs ({@code $}) in
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   955
     * the replacement string may cause the results to be different than if it
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   956
     * were being treated as a literal replacement string. Dollar signs may be
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   957
     * treated as references to captured subsequences as described above, and
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   958
     * backslashes are used to escape literal characters in the replacement
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   959
     * string.
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   960
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   961
     * <p> This method is intended to be used in a loop together with the
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
   962
     * {@link #appendTail(StringBuilder) appendTail} and
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
   963
     * {@link #find() find} methods. The following code, for example, writes
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
   964
     * {@code one dog two dogs in the yard} to the standard-output stream: </p>
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   965
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   966
     * <blockquote><pre>
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   967
     * Pattern p = Pattern.compile("cat");
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   968
     * Matcher m = p.matcher("one cat two cats in the yard");
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   969
     * StringBuilder sb = new StringBuilder();
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   970
     * while (m.find()) {
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   971
     *     m.appendReplacement(sb, "dog");
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   972
     * }
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   973
     * m.appendTail(sb);
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   974
     * System.out.println(sb.toString());</pre></blockquote>
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   975
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   976
     * @param  sb
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   977
     *         The target string builder
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   978
     * @param  replacement
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   979
     *         The replacement string
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   980
     * @return  This matcher
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   981
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   982
     * @throws  IllegalStateException
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   983
     *          If no match has yet been attempted,
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   984
     *          or if the previous match operation failed
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   985
     * @throws  IllegalArgumentException
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   986
     *          If the replacement string refers to a named-capturing
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   987
     *          group that does not exist in the pattern
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   988
     * @throws  IndexOutOfBoundsException
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   989
     *          If the replacement string refers to a capturing group
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   990
     *          that does not exist in the pattern
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32108
diff changeset
   991
     * @since 9
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   992
     */
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   993
    public Matcher appendReplacement(StringBuilder sb, String replacement) {
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   994
        // If no match, return error
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   995
        if (first < 0)
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   996
            throw new IllegalStateException("No match available");
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   997
        StringBuilder result = new StringBuilder();
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   998
        appendExpandedReplacement(replacement, result);
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
   999
        // Append the intervening text
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1000
        sb.append(text, lastAppendPosition, first);
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1001
        // Append the match substitution
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1002
        sb.append(result);
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1003
        lastAppendPosition = last;
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1004
        modCount++;
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1005
        return this;
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1006
    }
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1007
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1008
    /**
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1009
     * Processes replacement string to replace group references with
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1010
     * groups.
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1011
     */
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1012
    private StringBuilder appendExpandedReplacement(
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1013
        String replacement, StringBuilder result) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        int cursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        while (cursor < replacement.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            char nextChar = replacement.charAt(cursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            if (nextChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                cursor++;
12432
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
  1019
                if (cursor == replacement.length())
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
  1020
                    throw new IllegalArgumentException(
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
  1021
                        "character to be escaped is missing");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                nextChar = replacement.charAt(cursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                result.append(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                cursor++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            } else if (nextChar == '$') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                // Skip past $
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                cursor++;
12432
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
  1028
                // Throw IAE if this "$" is the last character in replacement
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
  1029
                if (cursor == replacement.length())
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
  1030
                   throw new IllegalArgumentException(
748fcaa0e0c1 7067045: replaceAll("\u20ac", "$"); causses java.lang.StringIndexOutOfBoundsExceptio
sherman
parents: 10345
diff changeset
  1031
                        "Illegal group reference: group index is missing");
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1032
                nextChar = replacement.charAt(cursor);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1033
                int refNum = -1;
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
  1034
                if (nextChar == '{') {
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1035
                    cursor++;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1036
                    StringBuilder gsb = new StringBuilder();
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1037
                    while (cursor < replacement.length()) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1038
                        nextChar = replacement.charAt(cursor);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1039
                        if (ASCII.isLower(nextChar) ||
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1040
                            ASCII.isUpper(nextChar) ||
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1041
                            ASCII.isDigit(nextChar)) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1042
                            gsb.append(nextChar);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1043
                            cursor++;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1044
                        } else {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1045
                            break;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1046
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    }
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1048
                    if (gsb.length() == 0)
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1049
                        throw new IllegalArgumentException(
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1050
                            "named capturing group has 0 length name");
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
  1051
                    if (nextChar != '}')
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1052
                        throw new IllegalArgumentException(
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
  1053
                            "named capturing group is missing trailing '}'");
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1054
                    String gname = gsb.toString();
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
  1055
                    if (ASCII.isDigit(gname.charAt(0)))
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
  1056
                        throw new IllegalArgumentException(
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
  1057
                            "capturing group name {" + gname +
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
  1058
                            "} starts with digit character");
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1059
                    if (!parentPattern.namedGroups().containsKey(gname))
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1060
                        throw new IllegalArgumentException(
4161
679d00486dc6 6878475: Better syntax for the named capture group in regex
sherman
parents: 2163
diff changeset
  1061
                            "No group with name {" + gname + "}");
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1062
                    refNum = parentPattern.namedGroups().get(gname);
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1063
                    cursor++;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1064
                } else {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1065
                    // The first number is always a group
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1066
                    refNum = nextChar - '0';
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1067
                    if ((refNum < 0) || (refNum > 9))
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1068
                        throw new IllegalArgumentException(
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1069
                            "Illegal group reference");
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1070
                    cursor++;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1071
                    // Capture the largest legal group string
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1072
                    boolean done = false;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1073
                    while (!done) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1074
                        if (cursor >= replacement.length()) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1075
                            break;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1076
                        }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1077
                        int nextDigit = replacement.charAt(cursor) - '0';
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1078
                        if ((nextDigit < 0) || (nextDigit > 9)) { // not a number
2070
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1079
                            break;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1080
                        }
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1081
                        int newRefNum = (refNum * 10) + nextDigit;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1082
                        if (groupCount() < newRefNum) {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1083
                            done = true;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1084
                        } else {
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1085
                            refNum = newRefNum;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1086
                            cursor++;
6e9972fbd965 6350801: Add support for named (instead of numbered) capture groups in regular expression
sherman
parents: 715
diff changeset
  1087
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                // Append group
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                if (start(refNum) != -1 && end(refNum) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    result.append(text, start(refNum), end(refNum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                result.append(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                cursor++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        }
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1098
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * Implements a terminal append-and-replace step.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * <p> This method reads characters from the input sequence, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * the append position, and appends them to the given string buffer.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * intended to be invoked after one or more invocations of the {@link
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1107
     * #appendReplacement(StringBuffer, String) appendReplacement} method in
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1108
     * order to copy the remainder of the input sequence.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * @param  sb
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *         The target string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @return  The target string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    public StringBuffer appendTail(StringBuffer sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        sb.append(text, lastAppendPosition, getTextLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        return sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    /**
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1121
     * Implements a terminal append-and-replace step.
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1122
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1123
     * <p> This method reads characters from the input sequence, starting at
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1124
     * the append position, and appends them to the given string builder.  It is
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1125
     * intended to be invoked after one or more invocations of the {@link
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1126
     * #appendReplacement(StringBuilder, String)
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1127
     * appendReplacement} method in order to copy the remainder of the input
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1128
     * sequence.  </p>
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1129
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1130
     * @param  sb
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1131
     *         The target string builder
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1132
     *
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1133
     * @return  The target string builder
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1134
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32108
diff changeset
  1135
     * @since 9
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1136
     */
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1137
    public StringBuilder appendTail(StringBuilder sb) {
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1138
        sb.append(text, lastAppendPosition, getTextLength());
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1139
        return sb;
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1140
    }
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1141
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1142
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * Replaces every subsequence of the input sequence that matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * pattern with the given replacement string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * <p> This method first resets this matcher.  It then scans the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * sequence looking for matches of the pattern.  Characters that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * part of any match are appended directly to the result string; each match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * is replaced in the result by the replacement string.  The replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * string may contain references to captured subsequences as in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * #appendReplacement appendReplacement} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1153
     * <p> Note that backslashes ({@code \}) and dollar signs ({@code $}) in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * the replacement string may cause the results to be different than if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * were being treated as a literal replacement string. Dollar signs may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * treated as references to captured subsequences as described above, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * backslashes are used to escape literal characters in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1160
     * <p> Given the regular expression {@code a*b}, the input
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1161
     * {@code "aabfooaabfooabfoob"}, and the replacement string
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1162
     * {@code "-"}, an invocation of this method on a matcher for that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1163
     * expression would yield the string {@code "-foo-foo-foo-"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * <p> Invoking this method changes this matcher's state.  If the matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * is to be used in further matching operations then it should first be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * reset.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * @param  replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     *         The replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @return  The string constructed by replacing each matching subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *          by the replacement string, substituting captured subsequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *          as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    public String replaceAll(String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        boolean result = find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (result) {
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1180
            StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                appendReplacement(sb, replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
                result = find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            } while (result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            appendTail(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        return text.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
    /**
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1192
     * Replaces every subsequence of the input sequence that matches the
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1193
     * pattern with the result of applying the given replacer function to the
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1194
     * match result of this matcher corresponding to that subsequence.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1195
     * Exceptions thrown by the function are relayed to the caller.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1196
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1197
     * <p> This method first resets this matcher.  It then scans the input
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1198
     * sequence looking for matches of the pattern.  Characters that are not
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1199
     * part of any match are appended directly to the result string; each match
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1200
     * is replaced in the result by the applying the replacer function that
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1201
     * returns a replacement string.  Each replacement string may contain
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1202
     * references to captured subsequences as in the {@link #appendReplacement
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1203
     * appendReplacement} method.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1204
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1205
     * <p> Note that backslashes ({@code \}) and dollar signs ({@code $}) in
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1206
     * a replacement string may cause the results to be different than if it
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1207
     * were being treated as a literal replacement string. Dollar signs may be
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1208
     * treated as references to captured subsequences as described above, and
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1209
     * backslashes are used to escape literal characters in the replacement
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1210
     * string.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1211
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1212
     * <p> Given the regular expression {@code dog}, the input
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1213
     * {@code "zzzdogzzzdogzzz"}, and the function
29380
c18777f9b6b9 8074674: Doclint regression in java/util/regex/Matcher.java
amlu
parents: 29243
diff changeset
  1214
     * {@code mr -> mr.group().toUpperCase()}, an invocation of this method on
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1215
     * a matcher for that expression would yield the string
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1216
     * {@code "zzzDOGzzzDOGzzz"}.
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1217
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1218
     * <p> Invoking this method changes this matcher's state.  If the matcher
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1219
     * is to be used in further matching operations then it should first be
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1220
     * reset.  </p>
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1221
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1222
     * <p> The replacer function should not modify this matcher's state during
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1223
     * replacement.  This method will, on a best-effort basis, throw a
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1224
     * {@link java.util.ConcurrentModificationException} if such modification is
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1225
     * detected.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1226
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1227
     * <p> The state of each match result passed to the replacer function is
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1228
     * guaranteed to be constant only for the duration of the replacer function
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1229
     * call and only if the replacer function does not modify this matcher's
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1230
     * state.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1231
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1232
     * @implNote
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1233
     * This implementation applies the replacer function to this matcher, which
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1234
     * is an instance of {@code MatchResult}.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1235
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1236
     * @param  replacer
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1237
     *         The function to be applied to the match result of this matcher
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1238
     *         that returns a replacement string.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1239
     * @return  The string constructed by replacing each matching subsequence
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1240
     *          with the result of applying the replacer function to that
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1241
     *          matched subsequence, substituting captured subsequences as
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1242
     *          needed.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1243
     * @throws NullPointerException if the replacer function is null
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1244
     * @throws ConcurrentModificationException if it is detected, on a
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1245
     *         best-effort basis, that the replacer function modified this
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1246
     *         matcher's state
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32108
diff changeset
  1247
     * @since 9
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1248
     */
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1249
    public String replaceAll(Function<MatchResult, String> replacer) {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1250
        Objects.requireNonNull(replacer);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1251
        reset();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1252
        boolean result = find();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1253
        if (result) {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1254
            StringBuilder sb = new StringBuilder();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1255
            do {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1256
                int ec = modCount;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1257
                String replacement =  replacer.apply(this);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1258
                if (ec != modCount)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1259
                    throw new ConcurrentModificationException();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1260
                appendReplacement(sb, replacement);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1261
                result = find();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1262
            } while (result);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1263
            appendTail(sb);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1264
            return sb.toString();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1265
        }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1266
        return text.toString();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1267
    }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1268
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1269
    /**
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1270
     * Returns a stream of match results for each subsequence of the input
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1271
     * sequence that matches the pattern.  The match results occur in the
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1272
     * same order as the matching subsequences in the input sequence.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1273
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1274
     * <p> Each match result is produced as if by {@link #toMatchResult()}.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1275
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1276
     * <p> This method does not reset this matcher.  Matching starts on
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1277
     * initiation of the terminal stream operation either at the beginning of
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1278
     * this matcher's region, or, if the matcher has not since been reset, at
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1279
     * the first character not matched by a previous match.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1280
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1281
     * <p> If the matcher is to be used for further matching operations after
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1282
     * the terminal stream operation completes then it should be first reset.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1283
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1284
     * <p> This matcher's state should not be modified during execution of the
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1285
     * returned stream's pipeline.  The returned stream's source
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1286
     * {@code Spliterator} is <em>fail-fast</em> and will, on a best-effort
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1287
     * basis, throw a {@link java.util.ConcurrentModificationException} if such
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1288
     * modification is detected.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1289
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1290
     * @return a sequential stream of match results.
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32108
diff changeset
  1291
     * @since 9
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1292
     */
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1293
    public Stream<MatchResult> results() {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1294
        class MatchResultIterator implements Iterator<MatchResult> {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1295
            // -ve for call to find, 0 for not found, 1 for found
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1296
            int state = -1;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1297
            // State for concurrent modification checking
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1298
            // -1 for uninitialized
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1299
            int expectedCount = -1;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1300
            // The input sequence as a string, set once only after first find
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1301
            // Avoids repeated conversion from CharSequence for each match
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1302
            String textAsString;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1303
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1304
            @Override
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1305
            public MatchResult next() {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1306
                if (expectedCount >= 0 && expectedCount != modCount)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1307
                    throw new ConcurrentModificationException();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1308
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1309
                if (!hasNext())
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1310
                    throw new NoSuchElementException();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1311
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1312
                state = -1;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1313
                return toMatchResult(textAsString);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1314
            }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1315
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1316
            @Override
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1317
            public boolean hasNext() {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1318
                if (state >= 0)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1319
                    return state == 1;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1320
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1321
                // Defer throwing ConcurrentModificationException to when next
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1322
                // or forEachRemaining is called.  The is consistent with other
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1323
                // fail-fast implementations.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1324
                if (expectedCount >= 0 && expectedCount != modCount)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1325
                    return true;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1326
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1327
                boolean found = find();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1328
                // Capture the input sequence as a string on first find
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1329
                if (found && state < 0)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1330
                    textAsString = text.toString();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1331
                state = found ? 1 : 0;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1332
                expectedCount = modCount;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1333
                return found;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1334
            }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1335
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1336
            @Override
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1337
            public void forEachRemaining(Consumer<? super MatchResult> action) {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1338
                if (expectedCount >= 0 && expectedCount != modCount)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1339
                    throw new ConcurrentModificationException();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1340
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1341
                int s = state;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1342
                if (s == 0)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1343
                    return;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1344
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1345
                // Set state to report no more elements on further operations
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1346
                state = 0;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1347
                expectedCount = -1;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1348
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1349
                // Perform a first find if required
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1350
                if (s < 0 && !find())
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1351
                    return;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1352
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1353
                // Capture the input sequence as a string on first find
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1354
                textAsString = text.toString();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1355
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1356
                do {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1357
                    int ec = modCount;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1358
                    action.accept(toMatchResult(textAsString));
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1359
                    if (ec != modCount)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1360
                        throw new ConcurrentModificationException();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1361
                } while (find());
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1362
            }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1363
        }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1364
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1365
                new MatchResultIterator(), Spliterator.ORDERED | Spliterator.NONNULL), false);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1366
    }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1367
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1368
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * Replaces the first subsequence of the input sequence that matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * pattern with the given replacement string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * <p> This method first resets this matcher.  It then scans the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * sequence looking for a match of the pattern.  Characters that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * part of the match are appended directly to the result string; the match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * is replaced in the result by the replacement string.  The replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * string may contain references to captured subsequences as in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * #appendReplacement appendReplacement} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1379
     * <p>Note that backslashes ({@code \}) and dollar signs ({@code $}) in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * the replacement string may cause the results to be different than if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * were being treated as a literal replacement string. Dollar signs may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * treated as references to captured subsequences as described above, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * backslashes are used to escape literal characters in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1386
     * <p> Given the regular expression {@code dog}, the input
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1387
     * {@code "zzzdogzzzdogzzz"}, and the replacement string
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1388
     * {@code "cat"}, an invocation of this method on a matcher for that
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1389
     * expression would yield the string {@code "zzzcatzzzdogzzz"}.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * <p> Invoking this method changes this matcher's state.  If the matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * is to be used in further matching operations then it should first be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * reset.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * @param  replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *         The replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * @return  The string constructed by replacing the first matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     *          subsequence by the replacement string, substituting captured
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     *          subsequences as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    public String replaceFirst(String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        if (replacement == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            throw new NullPointerException("replacement");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        if (!find())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            return text.toString();
23734
439905b27f94 8039124: j.u.regex.Matcher.appendReplace/Tail() should support StringBuilder variant
sherman
parents: 18787
diff changeset
  1407
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        appendReplacement(sb, replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        appendTail(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    /**
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1414
     * Replaces the first subsequence of the input sequence that matches the
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1415
     * pattern with the result of applying the given replacer function to the
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1416
     * match result of this matcher corresponding to that subsequence.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1417
     * Exceptions thrown by the replace function are relayed to the caller.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1418
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1419
     * <p> This method first resets this matcher.  It then scans the input
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1420
     * sequence looking for a match of the pattern.  Characters that are not
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1421
     * part of the match are appended directly to the result string; the match
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1422
     * is replaced in the result by the applying the replacer function that
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1423
     * returns a replacement string.  The replacement string may contain
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1424
     * references to captured subsequences as in the {@link #appendReplacement
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1425
     * appendReplacement} method.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1426
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1427
     * <p>Note that backslashes ({@code \}) and dollar signs ({@code $}) in
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1428
     * the replacement string may cause the results to be different than if it
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1429
     * were being treated as a literal replacement string. Dollar signs may be
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1430
     * treated as references to captured subsequences as described above, and
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1431
     * backslashes are used to escape literal characters in the replacement
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1432
     * string.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1433
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1434
     * <p> Given the regular expression {@code dog}, the input
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1435
     * {@code "zzzdogzzzdogzzz"}, and the function
29380
c18777f9b6b9 8074674: Doclint regression in java/util/regex/Matcher.java
amlu
parents: 29243
diff changeset
  1436
     * {@code mr -> mr.group().toUpperCase()}, an invocation of this method on
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1437
     * a matcher for that expression would yield the string
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1438
     * {@code "zzzDOGzzzdogzzz"}.
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1439
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1440
     * <p> Invoking this method changes this matcher's state.  If the matcher
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1441
     * is to be used in further matching operations then it should first be
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1442
     * reset.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1443
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1444
     * <p> The replacer function should not modify this matcher's state during
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1445
     * replacement.  This method will, on a best-effort basis, throw a
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1446
     * {@link java.util.ConcurrentModificationException} if such modification is
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1447
     * detected.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1448
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1449
     * <p> The state of the match result passed to the replacer function is
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1450
     * guaranteed to be constant only for the duration of the replacer function
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1451
     * call and only if the replacer function does not modify this matcher's
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1452
     * state.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1453
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1454
     * @implNote
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1455
     * This implementation applies the replacer function to this matcher, which
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1456
     * is an instance of {@code MatchResult}.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1457
     *
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1458
     * @param  replacer
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1459
     *         The function to be applied to the match result of this matcher
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1460
     *         that returns a replacement string.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1461
     * @return  The string constructed by replacing the first matching
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1462
     *          subsequence with the result of applying the replacer function to
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1463
     *          the matched subsequence, substituting captured subsequences as
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1464
     *          needed.
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1465
     * @throws NullPointerException if the replacer function is null
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1466
     * @throws ConcurrentModificationException if it is detected, on a
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1467
     *         best-effort basis, that the replacer function modified this
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1468
     *         matcher's state
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32108
diff changeset
  1469
     * @since 9
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1470
     */
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1471
    public String replaceFirst(Function<MatchResult, String> replacer) {
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1472
        Objects.requireNonNull(replacer);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1473
        reset();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1474
        if (!find())
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1475
            return text.toString();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1476
        StringBuilder sb = new StringBuilder();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1477
        int ec = modCount;
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1478
        String replacement = replacer.apply(this);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1479
        if (ec != modCount)
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1480
            throw new ConcurrentModificationException();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1481
        appendReplacement(sb, replacement);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1482
        appendTail(sb);
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1483
        return sb.toString();
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1484
    }
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1485
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1486
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * Sets the limits of this matcher's region. The region is the part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * input sequence that will be searched to find a match. Invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * method resets the matcher, and then sets the region to start at the
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1490
     * index specified by the {@code start} parameter and end at the
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1491
     * index specified by the {@code end} parameter.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * <p>Depending on the transparency and anchoring being used (see
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1494
     * {@link #useTransparentBounds(boolean) useTransparentBounds} and
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1495
     * {@link #useAnchoringBounds(boolean) useAnchoringBounds}), certain
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1496
     * constructs such as anchors may behave differently at or around the
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1497
     * boundaries of the region.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * @param  start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     *         The index to start searching at (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * @param  end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     *         The index to end searching at (exclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     *          If start or end is less than zero, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     *          start is greater than the length of the input sequence, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     *          end is greater than the length of the input sequence, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     *          start is greater than end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * @return  this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    public Matcher region(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        if ((start < 0) || (start > getTextLength()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
            throw new IndexOutOfBoundsException("start");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        if ((end < 0) || (end > getTextLength()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            throw new IndexOutOfBoundsException("end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        if (start > end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
            throw new IndexOutOfBoundsException("start > end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        from = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        to = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     * Reports the start index of this matcher's region. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
     * searches this matcher conducts are limited to finding matches
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1527
     * within {@link #regionStart() regionStart} (inclusive) and
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1528
     * {@link #regionEnd() regionEnd} (exclusive).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
     * @return  The starting point of this matcher's region
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    public int regionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        return from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * Reports the end index (exclusive) of this matcher's region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * The searches this matcher conducts are limited to finding matches
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1540
     * within {@link #regionStart() regionStart} (inclusive) and
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1541
     * {@link #regionEnd() regionEnd} (exclusive).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     * @return  the ending point of this matcher's region
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    public int regionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        return to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * Queries the transparency of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1553
     * <p> This method returns {@code true} if this matcher uses
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1554
     * <i>transparent</i> bounds, {@code false} if it uses <i>opaque</i>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     *
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1557
     * <p> See {@link #useTransparentBounds(boolean) useTransparentBounds} for a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * description of transparent and opaque bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     * <p> By default, a matcher uses opaque region boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1562
     * @return {@code true} iff this matcher is using transparent bounds,
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1563
     *         {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * @see java.util.regex.Matcher#useTransparentBounds(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    public boolean hasTransparentBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        return transparentBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * Sets the transparency of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1574
     * <p> Invoking this method with an argument of {@code true} will set this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * matcher to use <i>transparent</i> bounds. If the boolean
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1576
     * argument is {@code false}, then <i>opaque</i> bounds will be used.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * <p> Using transparent bounds, the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * matcher's region are transparent to lookahead, lookbehind,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * and boundary matching constructs. Those constructs can see beyond the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * boundaries of the region to see if a match is appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     * <p> Using opaque bounds, the boundaries of this matcher's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     * region are opaque to lookahead, lookbehind, and boundary matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * constructs that may try to see beyond them. Those constructs cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * look past the boundaries so they will fail to match anything outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * of the region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * <p> By default, a matcher uses opaque bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * @param  b a boolean indicating whether to use opaque or transparent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     *         regions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * @return this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * @see java.util.regex.Matcher#hasTransparentBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    public Matcher useTransparentBounds(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        transparentBounds = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * Queries the anchoring of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1605
     * <p> This method returns {@code true} if this matcher uses
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1606
     * <i>anchoring</i> bounds, {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     *
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1608
     * <p> See {@link #useAnchoringBounds(boolean) useAnchoringBounds} for a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * description of anchoring bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * <p> By default, a matcher uses anchoring region boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1613
     * @return {@code true} iff this matcher is using anchoring bounds,
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1614
     *         {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * @see java.util.regex.Matcher#useAnchoringBounds(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
    public boolean hasAnchoringBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        return anchoringBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * Sets the anchoring of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     *
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1625
     * <p> Invoking this method with an argument of {@code true} will set this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * matcher to use <i>anchoring</i> bounds. If the boolean
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1627
     * argument is {@code false}, then <i>non-anchoring</i> bounds will be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * <p> Using anchoring bounds, the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * matcher's region match anchors such as ^ and $.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * <p> Without anchoring bounds, the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * matcher's region will not match anchors such as ^ and $.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * <p> By default, a matcher uses anchoring region boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * @param  b a boolean indicating whether or not to use anchoring bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * @return this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * @see java.util.regex.Matcher#hasAnchoringBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    public Matcher useAnchoringBounds(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        anchoringBounds = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * <p>Returns the string representation of this matcher. The
32108
aa5490a167ee 8133188: docs: replace <tt> tags (obsolete in html5) for java.util
avstepan
parents: 32012
diff changeset
  1650
     * string representation of a {@code Matcher} contains information
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * that may be useful for debugging. The exact format is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * @return  The string representation of this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        StringBuilder sb = new StringBuilder();
32012
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 29503
diff changeset
  1658
        sb.append("java.util.regex.Matcher")
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 29503
diff changeset
  1659
                .append("[pattern=").append(pattern())
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 29503
diff changeset
  1660
                .append(" region=")
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 29503
diff changeset
  1661
                .append(regionStart()).append(',').append(regionEnd())
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 29503
diff changeset
  1662
                .append(" lastmatch=");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
        if ((first >= 0) && (group() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            sb.append(group());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        }
32012
73a05aa621ce 8131034: Cleanup in j.u.regex.Pattern.quote()
igerasim
parents: 29503
diff changeset
  1666
        sb.append(']');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * <p>Returns true if the end of input was hit by the search engine in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * the last match operation performed by this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * <p>When this method returns true, then it is possible that more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * would have changed the result of the last search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * @return  true iff the end of input was hit in the last match; false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     *          otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    public boolean hitEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        return hitEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * <p>Returns true if more input could change a positive match into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * negative one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * <p>If this method returns true, and a match was found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * input could cause the match to be lost. If this method returns false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     * and a match was found, then more input might change the match but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * match won't be lost. If a match was not found, then requireEnd has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * @return  true iff more input could change a positive match into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     *          negative one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    public boolean requireEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        return requireEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * Initiates a search to find a Pattern within the given bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * The groups are filled with default values and the match of the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * of the state machine is called. The state machine will hold the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * of the match as it proceeds in this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * Matcher.from is not set here, because it is the "hard" boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * of the start of the search which anchors will set to. The from param
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * is the "soft" boundary of the start of the search, meaning that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * regex tries to match at that index but ^ won't match there. Subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * calls to the search methods start at a new "soft" boundary which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * the end of the previous match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
    boolean search(int from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        this.hitEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        this.requireEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
        from        = from < 0 ? 0 : from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        this.first  = from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        this.oldLast = oldLast < 0 ? from : oldLast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            groups[i] = -1;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
  1724
        for (int i = 0; i < localsPos.length; i++) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
  1725
            if (localsPos[i] != null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
  1726
                localsPos[i].clear();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
  1727
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        acceptMode = NOANCHOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        boolean result = parentPattern.root.match(this, from, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        if (!result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            this.first = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        this.oldLast = this.last;
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1733
        this.modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * Initiates a search for an anchored match to a Pattern within the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * bounds. The groups are filled with default values and the match of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * root of the state machine is called. The state machine will hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * state of the match as it proceeds in this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    boolean match(int from, int anchor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        this.hitEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        this.requireEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        from        = from < 0 ? 0 : from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        this.first  = from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        this.oldLast = oldLast < 0 ? from : oldLast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
            groups[i] = -1;
37882
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
  1751
        for (int i = 0; i < localsPos.length; i++) {
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
  1752
            if (localsPos[i] != null)
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
  1753
                localsPos[i].clear();
e7f3cf12e739 6328855: String: Matches hangs at short and easy Strings containing \r \n
sherman
parents: 35302
diff changeset
  1754
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
        acceptMode = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        boolean result = parentPattern.matchRoot.match(this, from, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
        if (!result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
            this.first = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        this.oldLast = this.last;
29243
80ea8d3d39d0 8071479: Stream and lambdafication improvements to j.u.regex.Matcher
psandoz
parents: 25859
diff changeset
  1760
        this.modCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * Returns the end index of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * @return the index after the last character in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
    int getTextLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        return text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    /**
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1774
     * Generates a String from this matcher's input in the specified range.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * @param  beginIndex   the beginning index, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * @param  endIndex     the ending index, exclusive
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1778
     * @return A String generated from this matcher's input
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
    CharSequence getSubSequence(int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        return text.subSequence(beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    /**
50340
d52bba1f19aa 8190417: javadoc updates to java.util.regex.Matcher
lancea
parents: 47216
diff changeset
  1785
     * Returns this matcher's input character at index i.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * @return A char from the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
    char charAt(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        return text.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
17434
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1793
    /**
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1794
     * Returns the group index of the matched capturing group.
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1795
     *
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1796
     * @return the index of the named-capturing group
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1797
     */
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1798
    int getMatchedGroupIndex(String name) {
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1799
        Objects.requireNonNull(name, "Group name");
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1800
        if (first < 0)
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1801
            throw new IllegalStateException("No match found");
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1802
        if (!parentPattern.namedGroups().containsKey(name))
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1803
            throw new IllegalArgumentException("No group with name <" + name + ">");
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1804
        return parentPattern.namedGroups().get(name);
4a04d7127e80 8013252: Regex Matcher .start and .end should be accessible by group name
sherman
parents: 14342
diff changeset
  1805
    }
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 50340
diff changeset
  1806
}