summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIto Kazumitsu <kaz@maczuka.gcd.org>2006-01-23 13:18:09 +0000
committerIto Kazumitsu <kaz@maczuka.gcd.org>2006-01-23 13:18:09 +0000
commit50a87641769e0211199f0635010c2eaaa56e4f05 (patch)
tree6a99eb3080d63457bbeb861722ab5a5bff13ef41
parenta6e0eac079580547464cdf6538b42dcb3aa82027 (diff)
downloadclasspath-50a87641769e0211199f0635010c2eaaa56e4f05.tar.gz
2006-01-23 Ito Kazumitsu <kaz@maczuka.gcd.org>
* gnu/regexp/REToken.java(empty): Made Cloneable. * gnu/regexp/RETokenOneOf.java(match): RE.java(match): Use separate methods matchN and matchP depending on the boolean negative. (matchN): New method used when negative. Done as before. (matchP): New method used when not negative. Each token is tried not by itself but by a clone of it.
-rw-r--r--ChangeLog10
-rw-r--r--gnu/regexp/REToken.java11
-rw-r--r--gnu/regexp/RETokenOneOf.java70
3 files changed, 60 insertions, 31 deletions
diff --git a/ChangeLog b/ChangeLog
index c3171671c..9ef5e00d3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2006-01-23 Ito Kazumitsu <kaz@maczuka.gcd.org>
+
+ * gnu/regexp/REToken.java(empty): Made Cloneable.
+ * gnu/regexp/RETokenOneOf.java(match): RE.java(match):
+ Use separate methods matchN and matchP depending on the
+ boolean negative.
+ (matchN): New method used when negative. Done as before.
+ (matchP): New method used when not negative. Each token is
+ tried not by itself but by a clone of it.
+
2006-01-23 Chris Burdess <dog@gnu.org>
Fixes bug #25906
diff --git a/gnu/regexp/REToken.java b/gnu/regexp/REToken.java
index 4eae9ec47..f96213075 100644
--- a/gnu/regexp/REToken.java
+++ b/gnu/regexp/REToken.java
@@ -38,12 +38,21 @@ exception statement from your version. */
package gnu.regexp;
import java.io.Serializable;
-abstract class REToken implements Serializable {
+abstract class REToken implements Serializable, Cloneable {
protected REToken next = null;
protected REToken uncle = null;
protected int subIndex;
+ public Object clone() {
+ try {
+ REToken copy = (REToken) super.clone();
+ return copy;
+ } catch (CloneNotSupportedException e) {
+ throw new Error(); // doesn't happen
+ }
+ }
+
protected REToken(int subIndex) {
this.subIndex = subIndex;
}
diff --git a/gnu/regexp/RETokenOneOf.java b/gnu/regexp/RETokenOneOf.java
index 3f6e89e21..426330443 100644
--- a/gnu/regexp/RETokenOneOf.java
+++ b/gnu/regexp/RETokenOneOf.java
@@ -71,52 +71,62 @@ final class RETokenOneOf extends REToken {
}
boolean match(CharIndexed input, REMatch mymatch) {
- if (negative && (input.charAt(mymatch.index) == CharIndexed.OUT_OF_BOUNDS))
+ return negative ? matchN(input, mymatch) : matchP(input, mymatch);
+ }
+
+ private boolean matchN(CharIndexed input, REMatch mymatch) {
+ if (input.charAt(mymatch.index) == CharIndexed.OUT_OF_BOUNDS)
return false;
REMatch newMatch = null;
REMatch last = null;
REToken tk;
- boolean isMatch;
for (int i=0; i < options.size(); i++) {
tk = (REToken) options.elementAt(i);
REMatch tryMatch = (REMatch) mymatch.clone();
if (tk.match(input, tryMatch)) { // match was successful
- if (negative) return false;
-
- if (next(input, tryMatch)) {
- // Add tryMatch to list of possibilities.
- if (last == null) {
- newMatch = tryMatch;
- last = tryMatch;
- } else {
- last.next = tryMatch;
- last = tryMatch;
- }
- } // next succeeds
+ return false;
+ } // is a match
+ } // try next option
+
+ ++mymatch.index;
+ return next(input, mymatch);
+ }
+
+ private boolean matchP(CharIndexed input, REMatch mymatch) {
+ REMatch newMatch = null;
+ REMatch last = null;
+ REToken tk;
+ for (int i=0; i < options.size(); i++) {
+ // In ordaer that the backtracking can work,
+ // each option must be chained to the next token.
+ // But the chain method has some side effect, so
+ // we use clones.
+ tk = (REToken)((REToken) options.elementAt(i)).clone();
+ tk.chain(this.next);
+ tk.setUncle(this.uncle);
+ tk.subIndex = this.subIndex;
+ REMatch tryMatch = (REMatch) mymatch.clone();
+ if (tk.match(input, tryMatch)) { // match was successful
+ if (last == null) {
+ newMatch = tryMatch;
+ last = tryMatch;
+ } else {
+ last.next = tryMatch;
+ last = tryMatch;
+ }
} // is a match
} // try next option
if (newMatch != null) {
- if (negative) {
- return false;
- } else {
- // set contents of mymatch equal to newMatch
+ // set contents of mymatch equal to newMatch
- // try each one that matched
- mymatch.assignFrom(newMatch);
- return true;
- }
+ // try each one that matched
+ mymatch.assignFrom(newMatch);
+ return true;
} else {
- if (negative) {
- ++mymatch.index;
- return next(input, mymatch);
- } else {
- return false;
- }
+ return false;
}
-
- // index+1 works for [^abc] lists, not for generic lookahead (--> index)
}
void dump(StringBuffer os) {