[kaffe] CVS kaffe (dalibor): Fixes for regex wrappers
Kaffe CVS
cvs-commits at kaffe.org
Sat Jan 10 11:54:03 PST 2004
PatchSet 4311
Date: 2004/01/10 19:38:13
Author: dalibor
Branch: HEAD
Tag: (none)
Log:
Fixes for regex wrappers
2004-01-10 Mark Wielaard <mark at klomp.org>
* 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.
Members:
ChangeLog:1.1898->1.1899
libraries/javalib/java/util/regex/Matcher.java:1.3->1.4
libraries/javalib/java/util/regex/Pattern.java:1.3->1.4
Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.1898 kaffe/ChangeLog:1.1899
--- kaffe/ChangeLog:1.1898 Sat Jan 10 19:06:30 2004
+++ kaffe/ChangeLog Sat Jan 10 19:38:13 2004
@@ -1,3 +1,12 @@
+2004-01-10 Mark Wielaard <mark at klomp.org>
+
+ * 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.
+
2004-01-10 Dalibor Topic <robilad at kaffe.org>
Resynced with GNU Classpath.
Index: kaffe/libraries/javalib/java/util/regex/Matcher.java
diff -u kaffe/libraries/javalib/java/util/regex/Matcher.java:1.3 kaffe/libraries/javalib/java/util/regex/Matcher.java:1.4
--- kaffe/libraries/javalib/java/util/regex/Matcher.java:1.3 Mon Oct 13 03:10:04 2003
+++ kaffe/libraries/javalib/java/util/regex/Matcher.java Sat Jan 10 19:38:14 2004
@@ -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: kaffe/libraries/javalib/java/util/regex/Pattern.java
diff -u kaffe/libraries/javalib/java/util/regex/Pattern.java:1.3 kaffe/libraries/javalib/java/util/regex/Pattern.java:1.4
--- kaffe/libraries/javalib/java/util/regex/Pattern.java:1.3 Mon Oct 13 03:10:04 2003
+++ kaffe/libraries/javalib/java/util/regex/Pattern.java Sat Jan 10 19:38:14 2004
@@ -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;
}
}
More information about the kaffe
mailing list