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