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