jdk/src/share/classes/java/util/regex/Matcher.java
author naoto
Wed, 21 Jan 2009 13:58:46 -0800
changeset 1848 6ecbe9158c6e
parent 715 f16baef3a20e
child 2070 6e9972fbd965
permissions -rw-r--r--
6627549: ISO 3166 code addition: Saint Barthelemy and Saint Martin 6786276: Locale.getISOCountries() still contains country code "CS" Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util.regex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * An engine that performs match operations on a {@link java.lang.CharSequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * </code>character sequence<code>} by interpreting a {@link Pattern}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p> A matcher is created from a pattern by invoking the pattern's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Pattern#matcher matcher} method.  Once created, a matcher can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * perform three different kinds of match operations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *   <li><p> The {@link #matches matches} method attempts to match the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *   input sequence against the pattern.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *   <li><p> The {@link #lookingAt lookingAt} method attempts to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *   input sequence, starting at the beginning, against the pattern.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   <li><p> The {@link #find find} method scans the input sequence looking for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   the next subsequence that matches the pattern.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p> Each of these methods returns a boolean indicating success or failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * More information about a successful match can be obtained by querying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * state of the matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p> A matcher finds matches in a subset of its input called the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <i>region</i>. By default, the region contains all of the matcher's input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * The region can be modified via the{@link #region region} method and queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * via the {@link #regionStart regionStart} and {@link #regionEnd regionEnd}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * methods. The way that the region boundaries interact with some pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * constructs can be changed. See {@link #useAnchoringBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * useAnchoringBounds} and {@link #useTransparentBounds useTransparentBounds}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * for more details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p> This class also defines methods for replacing matched subsequences with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * new strings whose contents can, if desired, be computed from the match
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * result.  The {@link #appendReplacement appendReplacement} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * #appendTail appendTail} methods can be used in tandem in order to collect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * the result into an existing string buffer, or the more convenient {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * #replaceAll replaceAll} method can be used to create a string in which every
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * matching subsequence in the input sequence is replaced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * <p> The explicit state of a matcher includes the start and end indices of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * the most recent successful match.  It also includes the start and end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * indices of the input subsequence captured by each <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * href="Pattern.html#cg">capturing group</a> in the pattern as well as a total
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * count of such subsequences.  As a convenience, methods are also provided for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * returning these captured subsequences in string form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p> The explicit state of a matcher is initially undefined; attempting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * query any part of it before a successful match will cause an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * IllegalStateException} to be thrown.  The explicit state of a matcher is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * recomputed by every match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p> The implicit state of a matcher includes the input character sequence as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * well as the <i>append position</i>, which is initially zero and is updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * by the {@link #appendReplacement appendReplacement} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p> A matcher may be reset explicitly by invoking its {@link #reset()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * method or, if a new input sequence is desired, its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * #reset(java.lang.CharSequence) reset(CharSequence)} method.  Resetting a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * matcher discards its explicit state information and sets the append position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p> Instances of this class are not safe for use by multiple concurrent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * threads. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @author      Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @author      JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @since       1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * @spec        JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
public final class Matcher implements MatchResult {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The Pattern object that created this Matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    Pattern parentPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The storage used by groups. They may contain invalid values if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * a group was skipped during the matching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    int[] groups;
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 range within the sequence that is to be matched. Anchors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * will match at these "hard" boundaries. Changing the region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * changes these values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    int from, to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Lookbehind uses this value to ensure that the subexpression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * match ends at the point where the lookbehind was encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    int lookbehindTo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * The original string being matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    CharSequence text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Matcher state used by the last node. NOANCHOR is used when a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * match does not have to consume all of the input. ENDANCHOR is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * the mode used for matching all the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    static final int ENDANCHOR = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    static final int NOANCHOR = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    int acceptMode = NOANCHOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * The range of string that last matched the pattern. If the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * match failed then first is -1; last initially holds 0 then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * holds the index of the end of the last match (which is where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * next search starts).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    int first = -1, last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * The end index of what matched in the last match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    int oldLast = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * The index of the last position appended in a substitution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    int lastAppendPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Storage used by nodes to tell what repetition they are on in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * a pattern, and where groups begin. The nodes themselves are stateless,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * so they rely on this field to hold state during a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    int[] locals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Boolean indicating whether or not more input could change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * the results of the last match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * If hitEnd is true, and a match was found, then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * might cause a different match to be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * If hitEnd is true and a match was not found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * input could cause a match to be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * If hitEnd is false and a match was found, then more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * will not change the match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * If hitEnd is false and a match was not found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * input will not cause a match to be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    boolean hitEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Boolean indicating whether or not more input could change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * a positive match into a negative one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * If requireEnd is true, and a match was found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * input could cause the match to be lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * If requireEnd is false and a match was found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * input might change the match but the match won't be lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * If a match was not found, then requireEnd has no meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    boolean requireEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * If transparentBounds is true then the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * matcher's region are transparent to lookahead, lookbehind,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * and boundary matching constructs that try to see beyond them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    boolean transparentBounds = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * If anchoringBounds is true then the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * matcher's region match anchors such as ^ and $.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    boolean anchoringBounds = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * No default constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    Matcher() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * All matchers have the state used by Pattern during a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    Matcher(Pattern parent, CharSequence text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        this.parentPattern = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this.text = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // Allocate state storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int parentGroupCount = Math.max(parent.capturingGroupCount, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        groups = new int[parentGroupCount * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        locals = new int[parent.localCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        // Put fields into initial states
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        reset();
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
     * Returns the pattern that is interpreted by this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @return  The pattern for which this matcher was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public Pattern pattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return parentPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Returns the match state of this matcher as a {@link MatchResult}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * The result is unaffected by subsequent operations performed upon this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @return  a <code>MatchResult</code> with the state of this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public MatchResult toMatchResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        Matcher result = new Matcher(this.parentPattern, text.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        result.first = this.first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        result.last = this.last;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   252
        result.groups = this.groups.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return result;
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
      * Changes the <tt>Pattern</tt> that this <tt>Matcher</tt> uses to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
      * find matches with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
      * <p> This method causes this matcher to lose information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
      * about the groups of the last match that occurred. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
      * matcher's position in the input is maintained and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
      * last append position is unaffected.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
      * @param  newPattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
      *         The new pattern used by this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
      * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
      * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
      *          If newPattern is <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
      * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public Matcher usePattern(Pattern newPattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (newPattern == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            throw new IllegalArgumentException("Pattern cannot be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        parentPattern = newPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        // Reallocate state storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        groups = new int[parentGroupCount * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        locals = new int[newPattern.localCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        for (int i = 0; i < locals.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            locals[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Resets this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p> Resetting a matcher discards all of its explicit state information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * and sets its append position to zero. The matcher's region is set to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * default region, which is its entire character sequence. The anchoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * and transparency of this matcher's region boundaries are unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    public Matcher reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        first = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        last = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        oldLast = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        for(int i=0; i<groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        for(int i=0; i<locals.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            locals[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        lastAppendPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        from = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        to = getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Resets this matcher with a new input sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * <p> Resetting a matcher discards all of its explicit state information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * and sets its append position to zero.  The matcher's region is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * the default region, which is its entire character sequence.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * anchoring and transparency of this matcher's region boundaries are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *         The new input character sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public Matcher reset(CharSequence input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        text = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        return reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Returns the start index of the previous match.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @return  The index of the first character matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public int start() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            throw new IllegalStateException("No match available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Returns the start index of the subsequence captured by the given group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * during the previous match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * to right, starting at one.  Group zero denotes the entire pattern, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * the expression <i>m.</i><tt>start(0)</tt> is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <i>m.</i><tt>start()</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param  group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *         The index of a capturing group in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return  The index of the first character captured by the group,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *          or <tt>-1</tt> if the match was successful but the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *          itself did not match anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *          If there is no capturing group in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *          with the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public int start(int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            throw new IllegalStateException("No match available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (group > groupCount())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            throw new IndexOutOfBoundsException("No group " + group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return groups[group * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Returns the offset after the last character matched.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @return  The offset after the last character matched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public int end() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            throw new IllegalStateException("No match available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * Returns the offset after the last character of the subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * captured by the given group during the previous match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * to right, starting at one.  Group zero denotes the entire pattern, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * the expression <i>m.</i><tt>end(0)</tt> is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * <i>m.</i><tt>end()</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @param  group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *         The index of a capturing group in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @return  The offset after the last character captured by the group,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *          or <tt>-1</tt> if the match was successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *          but the group itself did not match anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *          If there is no capturing group in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *          with the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public int end(int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            throw new IllegalStateException("No match available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (group > groupCount())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            throw new IndexOutOfBoundsException("No group " + group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        return groups[group * 2 + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * Returns the input subsequence matched by the previous match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * <p> For a matcher <i>m</i> with input sequence <i>s</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * the expressions <i>m.</i><tt>group()</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * <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
   431
     * are equivalent.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <p> Note that some patterns, for example <tt>a*</tt>, match the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * string.  This method will return the empty string when the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * successfully matches the empty string in the input.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @return The (possibly empty) subsequence matched by the previous match,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *         in string form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public String group() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return group(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Returns the input subsequence captured by the given group during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * previous match operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * <p> For a matcher <i>m</i>, input sequence <i>s</i>, and group index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * <i>g</i>, the expressions <i>m.</i><tt>group(</tt><i>g</i><tt>)</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * <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
   455
     * are equivalent.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * <p> <a href="Pattern.html#cg">Capturing groups</a> are indexed from left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * to right, starting at one.  Group zero denotes the entire pattern, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * the expression <tt>m.group(0)</tt> is equivalent to <tt>m.group()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * <p> If the match was successful but the group specified failed to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * any part of the input sequence, then <tt>null</tt> is returned. Note
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * that some groups, for example <tt>(a*)</tt>, match the empty string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * This method will return the empty string when such a group successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * matches the empty string in the input.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @param  group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *         The index of a capturing group in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @return  The (possibly empty) subsequence captured by the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *          during the previous match, or <tt>null</tt> if the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *          failed to match part of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *          If there is no capturing group in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *          with the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public String group(int group) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            throw new IllegalStateException("No match found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (group < 0 || group > groupCount())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            throw new IndexOutOfBoundsException("No group " + group);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Returns the number of capturing groups in this matcher's pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * <p> Group zero denotes the entire pattern by convention. It is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * included in this count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * <p> Any non-negative integer smaller than or equal to the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * returned by this method is guaranteed to be a valid group index for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * this matcher.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @return The number of capturing groups in this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public int groupCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return parentPattern.capturingGroupCount - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * Attempts to match the entire region against the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * <p> If the match succeeds then more information can be obtained via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @return  <tt>true</tt> if, and only if, the entire region sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *          matches this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    public boolean matches() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return match(from, ENDANCHOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Attempts to find the next subsequence of the input sequence that matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * <p> This method starts at the beginning of this matcher's region, or, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * a previous invocation of the method was successful and the matcher has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * not since been reset, at the first character not matched by the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * <p> If the match succeeds then more information can be obtained via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @return  <tt>true</tt> if, and only if, a subsequence of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *          sequence matches this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public boolean find() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        int nextSearchIndex = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        if (nextSearchIndex == first)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            nextSearchIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        // If next search starts before region, start it at region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (nextSearchIndex < from)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            nextSearchIndex = from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // If next search starts beyond region then it fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (nextSearchIndex > to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return search(nextSearchIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Resets this matcher and then attempts to find the next subsequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * the input sequence that matches the pattern, starting at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * <p> If the match succeeds then more information can be obtained via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods, and subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * invocations of the {@link #find()} method will start at the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * character not matched by this match.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *          If start is less than zero or if start is greater than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *          length of the input sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @return  <tt>true</tt> if, and only if, a subsequence of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *          sequence starting at the given index matches this matcher's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *          pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    public boolean find(int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        int limit = getTextLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if ((start < 0) || (start > limit))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            throw new IndexOutOfBoundsException("Illegal start index");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        return search(start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * Attempts to match the input sequence, starting at the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * region, against the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <p> Like the {@link #matches matches} method, this method always starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * at the beginning of the region; unlike that method, it does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * require that the entire region be matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * <p> If the match succeeds then more information can be obtained via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <tt>start</tt>, <tt>end</tt>, and <tt>group</tt> methods.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @return  <tt>true</tt> if, and only if, a prefix of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *          sequence matches this matcher's pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public boolean lookingAt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        return match(from, NOANCHOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Returns a literal replacement <code>String</code> for the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * This method produces a <code>String</code> that will work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * as a literal replacement <code>s</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * <code>appendReplacement</code> method of the {@link Matcher} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * The <code>String</code> produced will match the sequence of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * in <code>s</code> treated as a literal sequence. Slashes ('\') and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * dollar signs ('$') will be given no special meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @param  s The string to be literalized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @return  A literal string replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    public static String quoteReplacement(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if ((s.indexOf('\\') == -1) && (s.indexOf('$') == -1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        for (int i=0; i<s.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            if (c == '\\' || c == '$') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                sb.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            sb.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Implements a non-terminal append-and-replace step.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <p> This method performs the following actions: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *   <li><p> It reads characters from the input sequence, starting at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *   append position, and appends them to the given string buffer.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *   stops after reading the last character preceding the previous match,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *   that is, the character at index {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *   #start()}&nbsp;<tt>-</tt>&nbsp;<tt>1</tt>.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *   <li><p> It appends the given replacement string to the string buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *   <li><p> It sets the append position of this matcher to the index of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *   the last character matched, plus one, that is, to {@link #end()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * <p> The replacement string may contain references to subsequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * captured during the previous match: Each occurrence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * <tt>$</tt><i>g</i><tt></tt> will be replaced by the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * evaluating {@link #group(int) group}<tt>(</tt><i>g</i><tt>)</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * The first number after the <tt>$</tt> is always treated as part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * the group reference. Subsequent numbers are incorporated into g if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * they would form a legal group reference. Only the numerals '0'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * through '9' are considered as potential components of the group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * reference. If the second group matched the string <tt>"foo"</tt>, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * example, then passing the replacement string <tt>"$2bar"</tt> would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * cause <tt>"foobar"</tt> to be appended to the string buffer. A dollar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * sign (<tt>$</tt>) may be included as a literal in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * string by preceding it with a backslash (<tt>\$</tt>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * the replacement string may cause the results to be different than if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * were being treated as a literal replacement string. Dollar signs may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * treated as references to captured subsequences as described above, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * backslashes are used to escape literal characters in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * <p> This method is intended to be used in a loop together with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * {@link #appendTail appendTail} and {@link #find find} methods.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * following code, for example, writes <tt>one dog two dogs in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * yard</tt> to the standard-output stream: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * Pattern p = Pattern.compile("cat");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Matcher m = p.matcher("one cat two cats in the yard");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * while (m.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *     m.appendReplacement(sb, "dog");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * m.appendTail(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * System.out.println(sb.toString());</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * @param  sb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     *         The target string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @param  replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *         The replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @return  This matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @throws  IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *          If no match has yet been attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *          or if the previous match operation failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *          If the replacement string refers to a capturing group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *          that does not exist in the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    public Matcher appendReplacement(StringBuffer sb, String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        // If no match, return error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if (first < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            throw new IllegalStateException("No match available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        // Process substitution string to replace group references with groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        int cursor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        StringBuilder result = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        while (cursor < replacement.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            char nextChar = replacement.charAt(cursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            if (nextChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                cursor++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                nextChar = replacement.charAt(cursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                result.append(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                cursor++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            } else if (nextChar == '$') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                // Skip past $
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                cursor++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                // The first number is always a group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                int refNum = (int)replacement.charAt(cursor) - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                if ((refNum < 0)||(refNum > 9))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        "Illegal group reference");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                cursor++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                // Capture the largest legal group string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    if (cursor >= replacement.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    int nextDigit = replacement.charAt(cursor) - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                    if ((nextDigit < 0)||(nextDigit > 9)) { // not a number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    int newRefNum = (refNum * 10) + nextDigit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                    if (groupCount() < newRefNum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                        refNum = newRefNum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                        cursor++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                // Append group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                if (start(refNum) != -1 && end(refNum) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                    result.append(text, start(refNum), end(refNum));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                result.append(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                cursor++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // Append the intervening text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        sb.append(text, lastAppendPosition, first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        // Append the match substitution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        sb.append(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        lastAppendPosition = last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * Implements a terminal append-and-replace step.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * <p> This method reads characters from the input sequence, starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * the append position, and appends them to the given string buffer.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * intended to be invoked after one or more invocations of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * #appendReplacement appendReplacement} method in order to copy the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * remainder of the input sequence.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @param  sb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *         The target string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * @return  The target string buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public StringBuffer appendTail(StringBuffer sb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        sb.append(text, lastAppendPosition, getTextLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        return sb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * Replaces every subsequence of the input sequence that matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * pattern with the given replacement string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * <p> This method first resets this matcher.  It then scans the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * sequence looking for matches of the pattern.  Characters that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * part of any match are appended directly to the result string; each match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * is replaced in the result by the replacement string.  The replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * string may contain references to captured subsequences as in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * #appendReplacement appendReplacement} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * <p> Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * the replacement string may cause the results to be different than if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * were being treated as a literal replacement string. Dollar signs may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * treated as references to captured subsequences as described above, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * backslashes are used to escape literal characters in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * <p> Given the regular expression <tt>a*b</tt>, the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * <tt>"aabfooaabfooabfoob"</tt>, and the replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * <tt>"-"</tt>, an invocation of this method on a matcher for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * expression would yield the string <tt>"-foo-foo-foo-"</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * <p> Invoking this method changes this matcher's state.  If the matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * is to be used in further matching operations then it should first be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * reset.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @param  replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *         The replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @return  The string constructed by replacing each matching subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *          by the replacement string, substituting captured subsequences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *          as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    public String replaceAll(String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        boolean result = find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                appendReplacement(sb, replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                result = find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            } while (result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            appendTail(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        return text.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * Replaces the first subsequence of the input sequence that matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * pattern with the given replacement string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * <p> This method first resets this matcher.  It then scans the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * sequence looking for a match of the pattern.  Characters that are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * part of the match are appended directly to the result string; the match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * is replaced in the result by the replacement string.  The replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * string may contain references to captured subsequences as in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * #appendReplacement appendReplacement} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * <p>Note that backslashes (<tt>\</tt>) and dollar signs (<tt>$</tt>) in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * the replacement string may cause the results to be different than if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * were being treated as a literal replacement string. Dollar signs may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * treated as references to captured subsequences as described above, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * backslashes are used to escape literal characters in the replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * <p> Given the regular expression <tt>dog</tt>, the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * <tt>"zzzdogzzzdogzzz"</tt>, and the replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * <tt>"cat"</tt>, an invocation of this method on a matcher for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * expression would yield the string <tt>"zzzcatzzzdogzzz"</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * <p> Invoking this method changes this matcher's state.  If the matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * is to be used in further matching operations then it should first be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * reset.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @param  replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *         The replacement string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @return  The string constructed by replacing the first matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *          subsequence by the replacement string, substituting captured
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *          subsequences as needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    public String replaceFirst(String replacement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        if (replacement == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            throw new NullPointerException("replacement");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        if (!find())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            return text.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        appendReplacement(sb, replacement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        appendTail(sb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * Sets the limits of this matcher's region. The region is the part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * input sequence that will be searched to find a match. Invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * method resets the matcher, and then sets the region to start at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * index specified by the <code>start</code> parameter and end at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * index specified by the <code>end</code> parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * <p>Depending on the transparency and anchoring being used (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * {@link #useTransparentBounds useTransparentBounds} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * {@link #useAnchoringBounds useAnchoringBounds}), certain constructs such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * as anchors may behave differently at or around the boundaries of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @param  start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *         The index to start searching at (inclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @param  end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *         The index to end searching at (exclusive)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *          If start or end is less than zero, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *          start is greater than the length of the input sequence, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *          end is greater than the length of the input sequence, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *          start is greater than end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @return  this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    public Matcher region(int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        if ((start < 0) || (start > getTextLength()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            throw new IndexOutOfBoundsException("start");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        if ((end < 0) || (end > getTextLength()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            throw new IndexOutOfBoundsException("end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if (start > end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            throw new IndexOutOfBoundsException("start > end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        from = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        to = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * Reports the start index of this matcher's region. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * searches this matcher conducts are limited to finding matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * within {@link #regionStart regionStart} (inclusive) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * {@link #regionEnd regionEnd} (exclusive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @return  The starting point of this matcher's region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    public int regionStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        return from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Reports the end index (exclusive) of this matcher's region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * The searches this matcher conducts are limited to finding matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * within {@link #regionStart regionStart} (inclusive) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * {@link #regionEnd regionEnd} (exclusive).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * @return  the ending point of this matcher's region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    public int regionEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        return to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * Queries the transparency of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * <p> This method returns <tt>true</tt> if this matcher uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * <i>transparent</i> bounds, <tt>false</tt> if it uses <i>opaque</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * <p> See {@link #useTransparentBounds useTransparentBounds} for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * description of transparent and opaque bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * <p> By default, a matcher uses opaque region boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * @return <tt>true</tt> iff this matcher is using transparent bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *         <tt>false</tt> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @see java.util.regex.Matcher#useTransparentBounds(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    public boolean hasTransparentBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        return transparentBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * Sets the transparency of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * <p> Invoking this method with an argument of <tt>true</tt> will set this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * matcher to use <i>transparent</i> bounds. If the boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * argument is <tt>false</tt>, then <i>opaque</i> bounds will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * <p> Using transparent bounds, the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * matcher's region are transparent to lookahead, lookbehind,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * and boundary matching constructs. Those constructs can see beyond the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * boundaries of the region to see if a match is appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * <p> Using opaque bounds, the boundaries of this matcher's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * region are opaque to lookahead, lookbehind, and boundary matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * constructs that may try to see beyond them. Those constructs cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * look past the boundaries so they will fail to match anything outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * of the region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * <p> By default, a matcher uses opaque bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @param  b a boolean indicating whether to use opaque or transparent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *         regions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * @return this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * @see java.util.regex.Matcher#hasTransparentBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    public Matcher useTransparentBounds(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        transparentBounds = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * Queries the anchoring of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * <p> This method returns <tt>true</tt> if this matcher uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * <i>anchoring</i> bounds, <tt>false</tt> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * <p> See {@link #useAnchoringBounds useAnchoringBounds} for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * description of anchoring bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * <p> By default, a matcher uses anchoring region boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @return <tt>true</tt> iff this matcher is using anchoring bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *         <tt>false</tt> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @see java.util.regex.Matcher#useAnchoringBounds(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    public boolean hasAnchoringBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        return anchoringBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * Sets the anchoring of region bounds for this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * <p> Invoking this method with an argument of <tt>true</tt> will set this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * matcher to use <i>anchoring</i> bounds. If the boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * argument is <tt>false</tt>, then <i>non-anchoring</i> bounds will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * <p> Using anchoring bounds, the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * matcher's region match anchors such as ^ and $.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * <p> Without anchoring bounds, the boundaries of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * matcher's region will not match anchors such as ^ and $.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * <p> By default, a matcher uses anchoring region boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @param  b a boolean indicating whether or not to use anchoring bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * @return this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * @see java.util.regex.Matcher#hasAnchoringBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    public Matcher useAnchoringBounds(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        anchoringBounds = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * <p>Returns the string representation of this matcher. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * string representation of a <code>Matcher</code> contains information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * that may be useful for debugging. The exact format is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @return  The string representation of this matcher
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        sb.append("java.util.regex.Matcher");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        sb.append("[pattern=" + pattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        sb.append(" region=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        sb.append(regionStart() + "," + regionEnd());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        sb.append(" lastmatch=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        if ((first >= 0) && (group() != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            sb.append(group());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        sb.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * <p>Returns true if the end of input was hit by the search engine in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * the last match operation performed by this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * <p>When this method returns true, then it is possible that more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * would have changed the result of the last search.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @return  true iff the end of input was hit in the last match; false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *          otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    public boolean hitEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        return hitEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * <p>Returns true if more input could change a positive match into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * negative one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * <p>If this method returns true, and a match was found, then more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * input could cause the match to be lost. If this method returns false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * and a match was found, then more input might change the match but the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * match won't be lost. If a match was not found, then requireEnd has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * meaning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * @return  true iff more input could change a positive match into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     *          negative one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    public boolean requireEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        return requireEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * Initiates a search to find a Pattern within the given bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * The groups are filled with default values and the match of the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * of the state machine is called. The state machine will hold the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * of the match as it proceeds in this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * Matcher.from is not set here, because it is the "hard" boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * of the start of the search which anchors will set to. The from param
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * is the "soft" boundary of the start of the search, meaning that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * regex tries to match at that index but ^ won't match there. Subsequent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * calls to the search methods start at a new "soft" boundary which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * the end of the previous match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    boolean search(int from) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        this.hitEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        this.requireEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        from        = from < 0 ? 0 : from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        this.first  = from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        this.oldLast = oldLast < 0 ? from : oldLast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        acceptMode = NOANCHOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        boolean result = parentPattern.root.match(this, from, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        if (!result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            this.first = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        this.oldLast = this.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * Initiates a search for an anchored match to a Pattern within the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * bounds. The groups are filled with default values and the match of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * root of the state machine is called. The state machine will hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * state of the match as it proceeds in this matcher.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    boolean match(int from, int anchor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        this.hitEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        this.requireEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        from        = from < 0 ? 0 : from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        this.first  = from;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        this.oldLast = oldLast < 0 ? from : oldLast;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        for (int i = 0; i < groups.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            groups[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        acceptMode = anchor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        boolean result = parentPattern.matchRoot.match(this, from, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        if (!result)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            this.first = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        this.oldLast = this.last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * Returns the end index of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * @return the index after the last character in the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    int getTextLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        return text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * Generates a String from this Matcher's input in the specified range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * @param  beginIndex   the beginning index, inclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * @param  endIndex     the ending index, exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * @return A String generated from this Matcher's input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    CharSequence getSubSequence(int beginIndex, int endIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        return text.subSequence(beginIndex, endIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * Returns this Matcher's input character at index i.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @return A char from the specified index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    char charAt(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        return text.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
}