corda/test/regex/RegexMatcher.java
Johannes Schindelin 2073d4bffb Prepare the Matcher class for multiple groups
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2013-12-03 12:28:10 -06:00

77 lines
1.7 KiB
Java

/* Copyright (c) 2008-2013, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package regex;
/**
* A minimal implementation of a regular expression matcher.
*
* @author Johannes Schindelin
*/
public class RegexMatcher extends Matcher {
private final PikeVM vm;
private char[] array;
int[] groupStart, groupEnd;
RegexMatcher(PikeVM vm, CharSequence string) {
super(string);
this.vm = vm;
}
private final PikeVM.Result adapter = new PikeVM.Result() {
public void set(int[] start, int[] end) {
RegexMatcher.this.start = start[0];
RegexMatcher.this.end = end[0];
RegexMatcher.this.groupStart = start;
RegexMatcher.this.groupEnd = end;
}
};
public Matcher reset() {
start = end = -1;
return this;
}
public Matcher reset(CharSequence input) {
this.input = input;
array = input.toString().toCharArray();
return reset();
}
public boolean matches() {
return vm.matches(array, 0, array.length, true, true, adapter);
}
public boolean find(int offset) {
throw new UnsupportedOperationException("TODO");
}
public int start(int group) {
return groupStart[group];
}
public int end(int group) {
return groupEnd[group];
}
public String group(int group) {
int offset = start(group);
if (offset < 0) {
return null;
}
int length = end(group) - offset;
return new String(array, offset, length);
}
public int groupCount() {
return groupStart.length - 1;
}
}