001: /*
002: * gnu/regexp/REMatch.java
003: * Copyright (C) 1998-2001 Wes Biggs
004: *
005: * This library is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU Lesser General Public License as published
007: * by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser 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: import java.io.Serializable;
023:
024: /**
025: * An instance of this class represents a match
026: * completed by a gnu.regexp matching function. It can be used
027: * to obtain relevant information about the location of a match
028: * or submatch.
029: *
030: * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
031: */
032: public final class REMatch implements Serializable, Cloneable {
033: private String matchedText;
034:
035: // These variables are package scope for fast access within the engine
036: int eflags; // execution flags this match was made using
037:
038: // Offset in source text where match was tried. This is zero-based;
039: // the actual position in the source text is given by (offset + anchor).
040: int offset;
041:
042: // Anchor position refers to the index into the source input
043: // at which the matching operation began.
044: // This is also useful for the ANCHORINDEX option.
045: int anchor;
046:
047: // Package scope; used by RE.
048: int index; // used while matching to mark current match position in input
049: int[] start; // start positions (relative to offset) for each (sub)exp.
050: int[] end; // end positions for the same
051: REMatch next; // other possibility (to avoid having to use arrays)
052:
053: public Object clone() {
054: try {
055: REMatch copy = (REMatch) super .clone();
056: copy.next = null;
057:
058: copy.start = (int[]) start.clone();
059: copy.end = (int[]) end.clone();
060:
061: return copy;
062: } catch (CloneNotSupportedException e) {
063: throw new Error(); // doesn't happen
064: }
065: }
066:
067: void assignFrom(REMatch other) {
068: start = other.start;
069: end = other.end;
070: index = other.index;
071: // need to deep clone?
072: next = other.next;
073: }
074:
075: REMatch(int subs, int anchor, int eflags) {
076: start = new int[subs + 1];
077: end = new int[subs + 1];
078: this .anchor = anchor;
079: this .eflags = eflags;
080: clear(anchor);
081: }
082:
083: void finish(CharIndexed text) {
084: start[0] = 0;
085: StringBuffer sb = new StringBuffer();
086: int i;
087: for (i = 0; i < end[0]; i++)
088: sb.append(text.charAt(i));
089: matchedText = sb.toString();
090: for (i = 0; i < start.length; i++) {
091: // If any subexpressions didn't terminate, they don't count
092: // TODO check if this code ever gets hit
093: if ((start[i] == -1) ^ (end[i] == -1)) {
094: start[i] = -1;
095: end[i] = -1;
096: }
097: }
098: next = null; // cut off alternates
099: }
100:
101: /** Clears the current match and moves the offset to the new index. */
102: void clear(int index) {
103: offset = index;
104: this .index = 0;
105: for (int i = 0; i < start.length; i++) {
106: start[i] = end[i] = -1;
107: }
108: next = null; // cut off alternates
109: }
110:
111: /**
112: * Returns the string matching the pattern. This makes it convenient
113: * to write code like the following:
114: * <P>
115: * <code>
116: * REMatch myMatch = myExpression.getMatch(myString);<br>
117: * if (myMatch != null) System.out.println("Regexp found: "+myMatch);
118: * </code>
119: */
120: public String toString() {
121: return matchedText;
122: }
123:
124: /**
125: * Returns the index within the input text where the match in its entirety
126: * began.
127: */
128: public int getStartIndex() {
129: return offset + start[0];
130: }
131:
132: /**
133: * Returns the index within the input string where the match in
134: * its entirety ends. The return value is the next position after
135: * the end of the string; therefore, a match created by the
136: * following call:
137: *
138: * <P>
139: * <code>REMatch myMatch = myExpression.getMatch(myString);</code>
140: * <P>
141: * can be viewed (given that myMatch is not null) by creating
142: * <P>
143: * <code>String theMatch = myString.substring(myMatch.getStartIndex(),
144: * myMatch.getEndIndex());</code>
145: * <P>
146: * But you can save yourself that work, since the <code>toString()</code>
147: * method (above) does exactly that for you.
148: */
149: public int getEndIndex() {
150: return offset + end[0];
151: }
152:
153: /**
154: * Returns the string matching the given subexpression. The subexpressions
155: * are indexed starting with one, not zero. That is, the subexpression
156: * identified by the first set of parentheses in a regular expression
157: * could be retrieved from an REMatch by calling match.toString(1).
158: *
159: * @param sub Index of the subexpression.
160: */
161: public String toString(int sub) {
162: if ((sub >= start.length) || (start[sub] == -1))
163: return "";
164: return (matchedText.substring(start[sub], end[sub]));
165: }
166:
167: /**
168: * Returns the index within the input string used to generate this match
169: * where subexpression number <i>sub</i> begins, or <code>-1</code> if
170: * the subexpression does not exist. The initial position is zero.
171: *
172: * @param sub Subexpression index
173: * @deprecated Use getStartIndex(int) instead.
174: */
175: public int getSubStartIndex(int sub) {
176: if (sub >= start.length)
177: return -1;
178: int x = start[sub];
179: return (x == -1) ? x : offset + x;
180: }
181:
182: /**
183: * Returns the index within the input string used to generate this match
184: * where subexpression number <i>sub</i> begins, or <code>-1</code> if
185: * the subexpression does not exist. The initial position is zero.
186: *
187: * @param sub Subexpression index
188: * @since gnu.regexp 1.1.0
189: */
190: public int getStartIndex(int sub) {
191: if (sub >= start.length)
192: return -1;
193: int x = start[sub];
194: return (x == -1) ? x : offset + x;
195: }
196:
197: /**
198: * Returns the index within the input string used to generate this match
199: * where subexpression number <i>sub</i> ends, or <code>-1</code> if
200: * the subexpression does not exist. The initial position is zero.
201: *
202: * @param sub Subexpression index
203: * @deprecated Use getEndIndex(int) instead
204: */
205: public int getSubEndIndex(int sub) {
206: if (sub >= start.length)
207: return -1;
208: int x = end[sub];
209: return (x == -1) ? x : offset + x;
210: }
211:
212: /**
213: * Returns the index within the input string used to generate this match
214: * where subexpression number <i>sub</i> ends, or <code>-1</code> if
215: * the subexpression does not exist. The initial position is zero.
216: *
217: * @param sub Subexpression index
218: */
219: public int getEndIndex(int sub) {
220: if (sub >= start.length)
221: return -1;
222: int x = end[sub];
223: return (x == -1) ? x : offset + x;
224: }
225:
226: /**
227: * Substitute the results of this match to create a new string.
228: * This is patterned after PERL, so the tokens to watch out for are
229: * <code>$0</code> through <code>$9</code>. <code>$0</code> matches
230: * the full substring matched; <code>$<i>n</i></code> matches
231: * subexpression number <i>n</i>.
232: *
233: * @param input A string consisting of literals and <code>$<i>n</i></code> tokens.
234: */
235: public String substituteInto(String input) {
236: // a la Perl, $0 is whole thing, $1 - $9 are subexpressions
237: StringBuffer output = new StringBuffer();
238: int pos;
239: for (pos = 0; pos < input.length() - 1; pos++) {
240: if ((input.charAt(pos) == '$')
241: && (Character.isDigit(input.charAt(pos + 1)))) {
242: int val = Character.digit(input.charAt(++pos), 10);
243: if (val < start.length) {
244: output.append(toString(val));
245: }
246: } else
247: output.append(input.charAt(pos));
248: }
249: if (pos < input.length())
250: output.append(input.charAt(pos));
251: return output.toString();
252: }
253: }
|