-/* Matcher.java --
- Copyright (C) 2002 Free Software Foundation, Inc.
+/* Matcher.java -- Instance of a regular expression applied to a char sequence.
+ Copyright (C) 2002, 2004 Free Software Foundation, Inc.
This file is part of GNU Classpath.
package java.util.regex;
+import gnu.regexp.RE;
+import gnu.regexp.REMatch;
+
/**
- * @author Michael Koch
+ * Instance of a regular expression applied to a char sequence.
+ *
* @since 1.4
*/
public class Matcher
{
private Pattern pattern;
+ private CharSequence input;
+ private int position;
+ private int appendPosition;
+ private REMatch match;
+
+ Matcher(Pattern pattern, CharSequence input)
+ {
+ this.pattern = pattern;
+ this.input = input;
+ }
/**
* @param sb The target string buffer
public Matcher appendReplacement (StringBuffer sb, String replacement)
throws IllegalStateException
{
- throw new Error("Not implemented");
+ assertMatchOp();
+ sb.append(input.subSequence(appendPosition,
+ match.getStartIndex()).toString());
+ sb.append(match.substituteInto(replacement));
+ appendPosition = match.getEndIndex();
+ return this;
}
/**
*/
public StringBuffer appendTail (StringBuffer sb)
{
- throw new Error("Not implemented");
+ sb.append(input.subSequence(appendPosition, input.length()).toString());
+ return sb;
}
/**
public int end ()
throws IllegalStateException
{
- throw new Error ("Not implemented");
+ assertMatchOp();
+ return match.getEndIndex();
}
/**
public int end (int group)
throws IllegalStateException
{
- throw new Error ("Not implemented");
+ assertMatchOp();
+ return match.getEndIndex(group);
}
public boolean find ()
{
- throw new Error ("Not implemented");
- }
-
+ boolean first = (match == null);
+ match = pattern.getRE().getMatch(input, position);
+ if (match != null)
+ {
+ int endIndex = match.getEndIndex();
+ // Are we stuck at the same position?
+ if (!first && endIndex == position)
+ {
+ match = null;
+ // Not at the end of the input yet?
+ if (position < input.length() - 1)
+ {
+ position++;
+ return find(position);
+ }
+ else
+ return false;
+ }
+ position = endIndex;
+ return true;
+ }
+ return false;
+ }
+
/**
* @param start The index to start the new pattern matching
*
*/
public boolean find (int start)
{
- throw new Error ("Not implemented");
+ match = pattern.getRE().getMatch(input, start);
+ if (match != null)
+ {
+ position = match.getEndIndex();
+ return true;
+ }
+ return false;
}
/**
*/
public String group ()
{
- throw new Error ("Not implemented");
+ assertMatchOp();
+ return match.toString();
}
/**
public String group (int group)
throws IllegalStateException
{
- throw new Error ("Not implemented");
+ assertMatchOp();
+ return match.toString(group);
}
/**
*/
public String replaceFirst (String replacement)
{
- throw new Error ("Not implemented");
+ reset();
+ // Semantics might not quite match
+ return pattern.getRE().substitute(input, replacement, position);
}
/**
*/
public String replaceAll (String replacement)
{
- throw new Error ("Not implemented");
+ reset();
+ return pattern.getRE().substituteAll(input, replacement, position);
}
public int groupCount ()
{
- throw new Error("Not implemented");
+ return pattern.getRE().getNumSubs();
}
public boolean lookingAt ()
{
- throw new Error("Not implemented");
+ match = pattern.getRE().getMatch(input, 0);
+ if (match != null)
+ {
+ if (match.getStartIndex() == 0)
+ return true;
+ match = null;
+ }
+ return false;
}
/**
*/
public boolean matches ()
{
- throw new Error("Not implemented");
+ return find(0);
}
/**
public Matcher reset ()
{
- throw new Error ("Not implemented");
+ position = 0;
+ match = null;
+ return this;
}
/**
*/
public Matcher reset (CharSequence input)
{
- throw new Error ("Not implemented");
+ this.input = input;
+ return reset();
}
/**
public int start ()
throws IllegalStateException
{
- throw new Error("Not implemented");
+ assertMatchOp();
+ return match.getStartIndex();
}
/**
public int start (int group)
throws IllegalStateException
{
- throw new Error("Not implemented");
+ assertMatchOp();
+ return match.getStartIndex(group);
+ }
+
+ private void assertMatchOp()
+ {
+ if (match == null) throw new IllegalStateException();
}
}