[kaffe] CVS kaffe (jim): Fix Pattern.split() bug + convert CRLF to LF.

Kaffe CVS cvs-commits at kaffe.org
Sun Oct 12 20:13:16 PDT 2003


PatchSet 4109 
Date: 2003/10/13 03:10:02
Author: jim
Branch: HEAD
Tag: (none) 
Log:
Fix Pattern.split() bug + convert CRLF to LF.

This should probably be merged upstream.  Here's some sample
code to illustrate the bug:

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class Tst {
  public static void main(String[] args)
  {
          String test1 = "test1/test2";
          String test2 = "test3";

          String [] test1parts = test1.split("/");
          for (int i=0; i < test1parts.length; i++)
                  System.out.println("test1parts[" + i + "] = " + test1parts[i]);

          String [] test2parts = test2.split("/");
          for (int i=0; i < test2parts.length; i++)
                  System.out.println("test2parts[" + i + "] = " + test2parts[i]);

          String [] test3parts = Pattern.compile("/").split(test2, 0);
          for (int i=0; i < test3parts.length; i++)
                  System.out.println("test3parts[" + i + "] = " + test3parts[i]);
  }
}

With this, I can run "Open DP-500", to stream DiVX and audio
to my new KiSS DVD player w/Ethernet.  :-)

  http://www.sourceforge.net/projects/open-dp500/

Cheers,

 - Jim

Members: 
	ChangeLog:1.1703->1.1704 
	libraries/javalib/java/util/regex/Matcher.java:1.2->1.3 
	libraries/javalib/java/util/regex/Pattern.java:1.2->1.3 
	libraries/javalib/java/util/regex/PatternSyntaxException.java:1.1->1.2 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.1703 kaffe/ChangeLog:1.1704
--- kaffe/ChangeLog:1.1703	Sat Oct 11 20:45:46 2003
+++ kaffe/ChangeLog	Mon Oct 13 03:10:02 2003
@@ -1,3 +1,11 @@
+2003-10-12  Jim Pick  <jim at kaffe.org>
+
+	* libraries/javalib/java/util/regex/Matcher.java,
+	libraries/javalib/java/util/regex/Pattern.java,
+	libraries/javalib/java/util/regex/PatternSyntaxException.java:
+	Return original string when Pattern.split(regex) does
+	not match.  Convert from DOS to Unix line endings.
+
 2003-10-11  Timothy S. Stack <stack at cs.utah.edu>
 
 	* config/i386/jit3-i386.def:
Index: kaffe/libraries/javalib/java/util/regex/Matcher.java
diff -u kaffe/libraries/javalib/java/util/regex/Matcher.java:1.2 kaffe/libraries/javalib/java/util/regex/Matcher.java:1.3
--- kaffe/libraries/javalib/java/util/regex/Matcher.java:1.2	Mon Dec  2 14:35:53 2002
+++ kaffe/libraries/javalib/java/util/regex/Matcher.java	Mon Oct 13 03:10:04 2003
@@ -1,123 +1,123 @@
-package java.util.regex;
-import gnu.regexp.RE;
-import gnu.regexp.REMatch;
-
-public final class Matcher {
-    private Pattern pattern;
-    private CharSequence input;
-    private int position;
-    private int appendPosition;
-    private REMatch match;
-
-    public Pattern pattern() {
-	return pattern;
-    }
-
-    Matcher(Pattern pattern, CharSequence input) {
-	this.pattern = pattern;
-	this.input = input;
-    }
-
-    public boolean matches() {
-	return find(0);
-    }
-
-    public boolean lookingAt() {
-	match = pattern.getRE().getMatch(input, 0);
-	if (match != null) {
-	    if (match.getStartIndex() == 0) {
-		return true;
-	    }
-	    match = null;
-	}
-	return false;
-    }
-
-    public boolean find() {
-	return find(position);
-    }
-
-    public boolean find(int start) {
-	match = pattern.getRE().getMatch(input, start);
-	if (match != null) {
-	    position = match.getEndIndex();
-	    return true;
-	}
-	return false;
-    }
-
-    private void assertMatchOp() {
-	if (match == null) throw new IllegalStateException();
-    }
-
-    public int start() {
-	assertMatchOp();
-	return match.getStartIndex();
-    }
-
-    public int start(int group) {
-	assertMatchOp();
-	return match.getStartIndex(group);
-    }
-
-    public int end() {
-	assertMatchOp();
-	return match.getEndIndex();
-    }
-
-    public int end(int group) {
-	assertMatchOp();
-	return match.getEndIndex(group);
-    }
-    
-    public String group() {
-	assertMatchOp();
-	return match.toString();
-    }
-
-    public String group(int group) {
-	assertMatchOp();
-	return match.toString(group);
-    }
-
-    public int groupCount() {
-	return pattern.getRE().getNumSubs();
-    }
-
-    public Matcher appendReplacement(StringBuffer sb, String replacement) {
-	assertMatchOp();
-	sb.append(input.subSequence(appendPosition, match.getStartIndex()).toString());
-	sb.append(match.substituteInto(replacement));
-	appendPosition = match.getEndIndex();
-
-	return this;
-    }
-
-    public StringBuffer appendTail(StringBuffer sb) {
-	sb.append(input.subSequence(appendPosition, input.length()).toString());
-	return sb;
-    }
-
-    public String replaceAll(String replacement) {
-	reset();
-	return pattern.getRE().substituteAll(input, replacement, position);
-    }
-
-    public String replaceFirst(String replacement) {
-	reset();
-	// Semantics might not quite match
-	return pattern.getRE().substitute(input, replacement, position);
-    }
-
-    public Matcher reset() {
-	position = 0;
-	match = null;
-	return this;
-    }
-
-    public Matcher reset(CharSequence input) {
-	this.input = input;
-	reset();
-	return this;
-    }
-}
+package java.util.regex;
+import gnu.regexp.RE;
+import gnu.regexp.REMatch;
+
+public final class Matcher {
+    private Pattern pattern;
+    private CharSequence input;
+    private int position;
+    private int appendPosition;
+    private REMatch match;
+
+    public Pattern pattern() {
+	return pattern;
+    }
+
+    Matcher(Pattern pattern, CharSequence input) {
+	this.pattern = pattern;
+	this.input = input;
+    }
+
+    public boolean matches() {
+	return find(0);
+    }
+
+    public boolean lookingAt() {
+	match = pattern.getRE().getMatch(input, 0);
+	if (match != null) {
+	    if (match.getStartIndex() == 0) {
+		return true;
+	    }
+	    match = null;
+	}
+	return false;
+    }
+
+    public boolean find() {
+	return find(position);
+    }
+
+    public boolean find(int start) {
+	match = pattern.getRE().getMatch(input, start);
+	if (match != null) {
+	    position = match.getEndIndex();
+	    return true;
+	}
+	return false;
+    }
+
+    private void assertMatchOp() {
+	if (match == null) throw new IllegalStateException();
+    }
+
+    public int start() {
+	assertMatchOp();
+	return match.getStartIndex();
+    }
+
+    public int start(int group) {
+	assertMatchOp();
+	return match.getStartIndex(group);
+    }
+
+    public int end() {
+	assertMatchOp();
+	return match.getEndIndex();
+    }
+
+    public int end(int group) {
+	assertMatchOp();
+	return match.getEndIndex(group);
+    }
+    
+    public String group() {
+	assertMatchOp();
+	return match.toString();
+    }
+
+    public String group(int group) {
+	assertMatchOp();
+	return match.toString(group);
+    }
+
+    public int groupCount() {
+	return pattern.getRE().getNumSubs();
+    }
+
+    public Matcher appendReplacement(StringBuffer sb, String replacement) {
+	assertMatchOp();
+	sb.append(input.subSequence(appendPosition, match.getStartIndex()).toString());
+	sb.append(match.substituteInto(replacement));
+	appendPosition = match.getEndIndex();
+
+	return this;
+    }
+
+    public StringBuffer appendTail(StringBuffer sb) {
+	sb.append(input.subSequence(appendPosition, input.length()).toString());
+	return sb;
+    }
+
+    public String replaceAll(String replacement) {
+	reset();
+	return pattern.getRE().substituteAll(input, replacement, position);
+    }
+
+    public String replaceFirst(String replacement) {
+	reset();
+	// Semantics might not quite match
+	return pattern.getRE().substitute(input, replacement, position);
+    }
+
+    public Matcher reset() {
+	position = 0;
+	match = null;
+	return this;
+    }
+
+    public Matcher reset(CharSequence input) {
+	this.input = input;
+	reset();
+	return this;
+    }
+}
Index: kaffe/libraries/javalib/java/util/regex/Pattern.java
diff -u kaffe/libraries/javalib/java/util/regex/Pattern.java:1.2 kaffe/libraries/javalib/java/util/regex/Pattern.java:1.3
--- kaffe/libraries/javalib/java/util/regex/Pattern.java:1.2	Mon Dec  2 14:35:53 2002
+++ kaffe/libraries/javalib/java/util/regex/Pattern.java	Mon Oct 13 03:10:04 2003
@@ -1,117 +1,120 @@
-package java.util.regex;
-import java.io.Serializable;
-import gnu.regexp.RE;
-import gnu.regexp.RESyntax;
-import gnu.regexp.REException;
-import java.util.Vector;
-
-public final class Pattern implements Serializable {
-
-    public static final int UNIX_LINES = 1;
-    public static final int CASE_INSENSITIVE = 2;
-    public static final int COMMENTS = 4;
-    public static final int MULTILINE = 8;
-    public static final int DOTALL = 32;
-    public static final int UNICODE_CASE = 64;
-    public static final int CANON_EQ = 128;
-
-    private String pattern;
-    private int flags;
-    
-    private RE re;
-    RE getRE() { return re; }
-
-    private Pattern(String pattern, int flags) throws PatternSyntaxException {
-	this.pattern = pattern;
-	this.flags = flags;
-
-	int gnuFlags = 0;
-	if ((flags & CASE_INSENSITIVE) != 0) gnuFlags |= RE.REG_ICASE;
-	if ((flags & MULTILINE) != 0) gnuFlags |= RE.REG_MULTILINE;
-	if ((flags & DOTALL) != 0) gnuFlags |= RE.REG_DOT_NEWLINE;
-	// not yet supported:
-	// if ((flags & UNICODE_CASE) != 0) gnuFlags =
-	// if ((flags & CANON_EQ) != 0) gnuFlags =
-
-	// Eventually there will be such a thing as JDK 1_4 syntax
-	RESyntax syntax = RESyntax.RE_SYNTAX_PERL5;
-	if ((flags & UNIX_LINES) != 0) {
-	    // Use a syntax set with \n for linefeeds?
-	    syntax = new RESyntax(syntax);
-	    syntax.setLineSeparator("\n");
-	}
-
-	if ((flags & COMMENTS) != 0) {
-	    // Use a syntax with support for comments?
-	}
-
-	try {
-	    this.re = new RE(pattern, gnuFlags, syntax);
-	} catch (REException e) {
-	    throw new PatternSyntaxException(e.getMessage(),
-					     pattern, e.getPosition());
-	}
-    }
-    
-    public static Pattern compile(String regex) throws PatternSyntaxException {
-	return compile(regex, 0);
-    }
-
-    public static Pattern compile(String regex, int flags) throws PatternSyntaxException {
-	return new Pattern(regex, flags);
-    }
-
-    public static boolean matches(String regex, CharSequence input) throws PatternSyntaxException {
-	return compile(regex).matcher(input).matches();
-    }
-
-    public String pattern() {
-	return pattern;
-    }
-
-    public int flags() {
-	return flags;
-    }
-
-    public Matcher matcher(CharSequence input) {
-	return new Matcher(this, input);
-    }
-
-    public String[] split(CharSequence input) {
-	return split(input, 0);
-    }
-
-    private static final String[] modelArray = new String [0];
-
-    public String[] split(CharSequence input, int limit) {
-	Matcher matcher = new Matcher(this, input);
-	Vector list = new Vector();
-	int count = 0;
-	int start = 0;
-	int end;
-	while (matcher.find()) {
-	    ++count;
-	    end = matcher.start();
-	    if (start == end) {
-		if (limit != 0) {
-		    list.addElement("");
-		}
-	    } else {
-		String text = input.subSequence(start, end).toString();
-		list.addElement(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);
-	    }
-	}
-	String[] output = new String [list.size()];
-	list.copyInto(output);
-	return output;
-    }
-}
+package java.util.regex;
+import java.io.Serializable;
+import gnu.regexp.RE;
+import gnu.regexp.RESyntax;
+import gnu.regexp.REException;
+import java.util.Vector;
+
+public final class Pattern implements Serializable {
+
+    public static final int UNIX_LINES = 1;
+    public static final int CASE_INSENSITIVE = 2;
+    public static final int COMMENTS = 4;
+    public static final int MULTILINE = 8;
+    public static final int DOTALL = 32;
+    public static final int UNICODE_CASE = 64;
+    public static final int CANON_EQ = 128;
+
+    private String pattern;
+    private int flags;
+    
+    private RE re;
+    RE getRE() { return re; }
+
+    private Pattern(String pattern, int flags) throws PatternSyntaxException {
+	this.pattern = pattern;
+	this.flags = flags;
+
+	int gnuFlags = 0;
+	if ((flags & CASE_INSENSITIVE) != 0) gnuFlags |= RE.REG_ICASE;
+	if ((flags & MULTILINE) != 0) gnuFlags |= RE.REG_MULTILINE;
+	if ((flags & DOTALL) != 0) gnuFlags |= RE.REG_DOT_NEWLINE;
+	// not yet supported:
+	// if ((flags & UNICODE_CASE) != 0) gnuFlags =
+	// if ((flags & CANON_EQ) != 0) gnuFlags =
+
+	// Eventually there will be such a thing as JDK 1_4 syntax
+	RESyntax syntax = RESyntax.RE_SYNTAX_PERL5;
+	if ((flags & UNIX_LINES) != 0) {
+	    // Use a syntax set with \n for linefeeds?
+	    syntax = new RESyntax(syntax);
+	    syntax.setLineSeparator("\n");
+	}
+
+	if ((flags & COMMENTS) != 0) {
+	    // Use a syntax with support for comments?
+	}
+
+	try {
+	    this.re = new RE(pattern, gnuFlags, syntax);
+	} catch (REException e) {
+	    throw new PatternSyntaxException(e.getMessage(),
+					     pattern, e.getPosition());
+	}
+    }
+    
+    public static Pattern compile(String regex) throws PatternSyntaxException {
+	return compile(regex, 0);
+    }
+
+    public static Pattern compile(String regex, int flags) throws PatternSyntaxException {
+	return new Pattern(regex, flags);
+    }
+
+    public static boolean matches(String regex, CharSequence input) throws PatternSyntaxException {
+	return compile(regex).matcher(input).matches();
+    }
+
+    public String pattern() {
+	return pattern;
+    }
+
+    public int flags() {
+	return flags;
+    }
+
+    public Matcher matcher(CharSequence input) {
+	return new Matcher(this, input);
+    }
+
+    public String[] split(CharSequence input) {
+	return split(input, 0);
+    }
+
+    private static final String[] modelArray = new String [0];
+
+    public String[] split(CharSequence input, int limit) {
+	Matcher matcher = new Matcher(this, input);
+	Vector list = new Vector();
+	int count = 0;
+	int start = 0;
+	int end;
+	while (matcher.find()) {
+	    ++count;
+	    end = matcher.start();
+	    if (start == end) {
+		if (limit != 0) {
+		    list.addElement("");
+		}
+	    } else {
+		String text = input.subSequence(start, end).toString();
+		list.addElement(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);
+	    }
+	}
+	if (list.size() == 0) {
+		list.addElement(input.toString());
+	}
+	String[] output = new String [list.size()];
+	list.copyInto(output);
+	return output;
+    }
+}
Index: kaffe/libraries/javalib/java/util/regex/PatternSyntaxException.java
diff -u kaffe/libraries/javalib/java/util/regex/PatternSyntaxException.java:1.1 kaffe/libraries/javalib/java/util/regex/PatternSyntaxException.java:1.2
--- kaffe/libraries/javalib/java/util/regex/PatternSyntaxException.java:1.1	Thu Nov 28 10:23:18 2002
+++ kaffe/libraries/javalib/java/util/regex/PatternSyntaxException.java	Mon Oct 13 03:10:04 2003
@@ -1,29 +1,29 @@
-package java.util.regex;
-
-public class PatternSyntaxException extends IllegalArgumentException {
-    protected String description;
-    protected String pattern;
-    protected int index;
-
-    public PatternSyntaxException(String description, String pattern, int index) {
-	this.description = description;
-	this.pattern = pattern;
-	this.index = index;
-    }
-
-    public String getDescription() {
-	return description;
-    }
-
-    public String getPattern() {
-	return pattern;
-    }
-
-    public int getIndex() {
-	return index;
-    }
-
-    public String getMessage() {
-	return description; // XXX
-    }
-}
+package java.util.regex;
+
+public class PatternSyntaxException extends IllegalArgumentException {
+    protected String description;
+    protected String pattern;
+    protected int index;
+
+    public PatternSyntaxException(String description, String pattern, int index) {
+	this.description = description;
+	this.pattern = pattern;
+	this.index = index;
+    }
+
+    public String getDescription() {
+	return description;
+    }
+
+    public String getPattern() {
+	return pattern;
+    }
+
+    public int getIndex() {
+	return index;
+    }
+
+    public String getMessage() {
+	return description; // XXX
+    }
+}




More information about the kaffe mailing list