001: /*
002: * gnu/regexp/REMatch.java
003: * Copyright (C) 1998 Wes Biggs
004: *
005: * This library is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU Library General Public License as published
007: * by the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Library General Public License for more details.
014: *
015: * You should have received a copy of the GNU Library General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018: */
019:
020: package gnu.regexp;
021:
022: /**
023: * An instance of this class represents a match
024: * completed by a gnu.regexp matching function. It can be used
025: * to obtain relevant information about the location of a match
026: * or submatch.
027: *
028: * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
029: */
030: public class REMatch {
031: private String m_match;
032: int offset, anchor;
033: int[] start; // package scope for
034: int[] end; // quick access internally
035: int[] count; // runtime count of times through each subexpression
036:
037: REMatch(int f_subs, int f_index) {
038: start = new int[f_subs + 1];
039: end = new int[f_subs + 1];
040: count = new int[f_subs + 1];
041: anchor = f_index;
042: clear(f_index);
043: }
044:
045: void finish(CharIndexed text) {
046: start[0] = 0;
047: StringBuffer sb = new StringBuffer();
048: int i;
049: for (i = 0; i < end[0]; i++)
050: sb.append(text.charAt(i));
051: m_match = sb.toString();
052: for (i = 0; i < start.length; i++) {
053: if (start[i] == -1)
054: end[i] = -1;
055: }
056: }
057:
058: void reset(int f_subIndex) {
059: for (int i = f_subIndex; i < start.length; i++) {
060: start[i] = end[i] = -1;
061: count[i] = 0;
062: }
063: }
064:
065: void clear(int f_index) {
066: offset = f_index;
067: for (int i = 0; i < start.length; i++) {
068: start[i] = end[i] = -1;
069: count[i] = 0;
070: }
071: }
072:
073: /**
074: * Returns the string matching the pattern. This makes it convenient
075: * to write code like the following:
076: * <P>
077: * <code> REMatch myMatch = myExpression.getMatch(myString);<br>
078: * if (myMatch != null) System.out.println("Regexp found: "+myMatch);</code>
079: */
080: public String toString() {
081: return m_match;
082: }
083:
084: /**
085: * Returns the index within the input text where the match in its entirety
086: * began.
087: */
088: public int getStartIndex() {
089: return offset + start[0];
090: }
091:
092: /**
093: * Returns the index within the input string where the match in its entirety
094: * ends. The return value is the next position after the end of the string;
095: * therefore, a match created by the following call:
096: * <P>
097: * <code>REMatch myMatch = myExpression.getMatch(myString);</code>
098: * <P>
099: * can be viewed (given that myMatch is not null) by creating
100: * <P>
101: * <code>String theMatch = myString.substring(myMatch.getStartIndex(),
102: * myMatch.getEndIndex());</code>
103: * <P>
104: * But you can save yourself that work, since the <code>toString()</code>
105: * method (above) does exactly that for you.
106: */
107: public int getEndIndex() {
108: return offset + end[0];
109: }
110:
111: /**
112: * Returns the string matching the given subexpression.
113: *
114: * @param sub Index of the subexpression.
115: */
116: public String toString(int sub) {
117: if ((sub >= start.length) || (start[sub] == -1))
118: return "";
119: return (m_match.substring(start[sub], end[sub]));
120: }
121:
122: /**
123: * Returns the index within the input string used to generate this match
124: * where subexpression number <i>sub</i> begins, or <code>-1</code> if
125: * the subexpression does not exist.
126: *
127: * @param sub Subexpression index
128: */
129: public int getSubStartIndex(int sub) {
130: if (sub >= start.length)
131: return -1;
132: int x = start[sub];
133: return (x == -1) ? x : offset + x;
134: }
135:
136: /**
137: * Returns the index within the input string used to generate this match
138: * where subexpression number <i>sub</i> ends, or <code>-1</code> if
139: * the subexpression does not exist.
140: *
141: * @param sub Subexpression index
142: */
143: public int getSubEndIndex(int sub) {
144: if (sub >= start.length)
145: return -1;
146: int x = end[sub];
147: return (x == -1) ? x : offset + x;
148: }
149:
150: /**
151: * Substitute the results of this match to create a new string.
152: * This is patterned after PERL, so the tokens to watch out for are
153: * <code>$0</code> through <code>$9</code>. <code>$0</code> matches
154: * the full substring matched; <code>$<i>n</i></code> matches
155: * subexpression number <i>n</i>.
156: *
157: * @param input A string consisting of literals and <code>$<i>n</i></code> tokens.
158: */
159: public String substituteInto(String input) {
160: // a la Perl, $0 is whole thing, $1 - $9 are subexpressions
161: StringBuffer output = new StringBuffer();
162: int pos;
163: for (pos = 0; pos < input.length() - 1; pos++) {
164: if ((input.charAt(pos) == '$')
165: && (Character.isDigit(input.charAt(pos + 1)))) {
166: int val = Character.digit(input.charAt(++pos), 10);
167: if (val < start.length) {
168: output.append(toString(val));
169: }
170: } else
171: output.append(input.charAt(pos));
172: }
173: if (pos < input.length())
174: output.append(input.charAt(pos));
175: return output.toString();
176: }
177: }
|