Hi, I am preparing for inclusion of java.util.regex and gnu.regexp in GNU Classpath. I just added some test cases to Mauve. I need the following patches to make them work correctly with kaffe. Hope they make sense.
2004-01-08 Mark Wielaard <[EMAIL PROTECTED]>
* java/util/regex/Matcher.java (find): Check whether or not we are
stuck at the same position after a successful match and bump position
of possible.
* java/util/regex/Pattern.java (split(CharSequence, int)): Use
ArrayList, not Vector. Make sure we match at most limit -1 times, when
limit > 0. Check whether or not to add emtpty strings.
They are only against the java.util.regex wrappers, I haven't looked at
gnu.regexp proper yet. I have some more Mauve tests that I will check in
later, but gnu.regexp doesn't seem to support octal, hex and unicode
escapes yet so most of them fail.
Cheers,
Mark
Index: libraries/javalib/java/util/regex/Matcher.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/util/regex/Matcher.java,v
retrieving revision 1.3
diff -u -r1.3 Matcher.java
--- libraries/javalib/java/util/regex/Matcher.java 13 Oct 2003 03:10:04 -0000 1.3
+++ libraries/javalib/java/util/regex/Matcher.java 8 Jan 2004 11:17:50 -0000
@@ -34,7 +34,25 @@
}
public boolean find() {
- return find(position);
+ 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;
}
public boolean find(int start) {
Index: libraries/javalib/java/util/regex/Pattern.java
===================================================================
RCS file: /cvs/kaffe/kaffe/libraries/javalib/java/util/regex/Pattern.java,v
retrieving revision 1.3
diff -u -r1.3 Pattern.java
--- libraries/javalib/java/util/regex/Pattern.java 13 Oct 2003 03:10:04 -0000 1.3
+++ libraries/javalib/java/util/regex/Pattern.java 8 Jan 2004 11:17:50 -0000
@@ -3,7 +3,7 @@
import gnu.regexp.RE;
import gnu.regexp.RESyntax;
import gnu.regexp.REException;
-import java.util.Vector;
+import java.util.ArrayList;
public final class Pattern implements Serializable {
@@ -85,36 +85,58 @@
public String[] split(CharSequence input, int limit) {
Matcher matcher = new Matcher(this, input);
- Vector list = new Vector();
+ ArrayList list = new ArrayList();
+ int empties = 0;
int count = 0;
int start = 0;
int end;
- while (matcher.find()) {
+ boolean matched;
+ while (matched = matcher.find() && (limit <= 0 || count < limit - 1)) {
++count;
end = matcher.start();
if (start == end) {
- if (limit != 0) {
- list.addElement("");
- }
+ empties++;
} else {
+ while (empties-- > 0) {
+ list.add("");
+ }
String text = input.subSequence(start, end).toString();
- list.addElement(text);
+ list.add(text);
}
start = matcher.end();
- if (count == limit) break;
}
- // last token at end
- if (count != limit) {
- String text = input.subSequence(start, input.length()).toString();
- if (!("".equals(text) && (limit == 0))) {
- list.addElement(text);
+
+ // We matched nothing.
+ if (!matched && count == 0) {
+ return new String[] { input.toString() };
+ }
+
+ // Is the last token empty?
+ boolean emptyLast = (start == input.length());
+
+ // Can/Must we add empties or an extra last token at the end?
+ if (list.size() < limit || limit < 0 || (limit == 0 && !emptyLast)) {
+ if (limit > list.size()) {
+ int max = limit - list.size();
+ empties = (empties > max) ? max : empties;
+ }
+ while (empties-- > 0) {
+ list.add("");
}
}
- if (list.size() == 0) {
- list.addElement(input.toString());
+
+ // last token at end
+ if (limit != 0 || (limit == 0 && !emptyLast)) {
+ String t = input.subSequence(start, input.length()).toString();
+ if ("".equals(t) && limit == 0) {
+ // Don't add.
+ } else {
+ list.add(t);
+ }
}
+
String[] output = new String [list.size()];
- list.copyInto(output);
+ list.toArray(output);
return output;
}
}
signature.asc
Description: This is a digitally signed message part
