|
What this is
Other links
The source code/* * gnu/regexp/REMatch.java * Copyright (C) 1998-2001 Wes Biggs * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ package gnu.regexp; import java.io.Serializable; /** * An instance of this class represents a match * completed by a gnu.regexp matching function. It can be used * to obtain relevant information about the location of a match * or submatch. * * @author Wes Biggs */ public final class REMatch implements Serializable, Cloneable { private String matchedText; // These variables are package scope for fast access within the engine int eflags; // execution flags this match was made using // Offset in source text where match was tried. This is zero-based; // the actual position in the source text is given by (offset + anchor). int offset; // Anchor position refers to the index into the source input // at which the matching operation began. // This is also useful for the ANCHORINDEX option. int anchor; // Package scope; used by RE. int index; // used while matching to mark current match position in input int[] start; // start positions (relative to offset) for each (sub)exp. int[] end; // end positions for the same REMatch next; // other possibility (to avoid having to use arrays) public Object clone() { try { REMatch copy = (REMatch) super.clone(); copy.next = null; copy.start = (int[]) start.clone(); copy.end = (int[]) end.clone(); return copy; } catch (CloneNotSupportedException e) { throw new Error(); // doesn't happen } } void assignFrom(REMatch other) { start = other.start; end = other.end; index = other.index; // need to deep clone? next = other.next; } REMatch(int subs, int anchor, int eflags) { start = new int[subs+1]; end = new int[subs+1]; this.anchor = anchor; this.eflags = eflags; clear(anchor); } void finish(CharIndexed text) { start[0] = 0; StringBuffer sb = new StringBuffer(); int i; for (i = 0; i < end[0]; i++) sb.append(text.charAt(i)); matchedText = sb.toString(); for (i = 0; i < start.length; i++) { // If any subexpressions didn't terminate, they don't count // TODO check if this code ever gets hit if ((start[i] == -1) ^ (end[i] == -1)) { start[i] = -1; end[i] = -1; } } next = null; // cut off alternates } /** Clears the current match and moves the offset to the new index. */ void clear(int index) { offset = index; this.index = 0; for (int i = 0; i < start.length; i++) { start[i] = end[i] = -1; } next = null; // cut off alternates } /** * Returns the string matching the pattern. This makes it convenient * to write code like the following: * |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.