Bug 22873

Summary: java.util.regex.Matcher.group(int) differs from Sun's API
Product: classpath Reporter: from-classpath
Component: classpathAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: bug-classpath
Priority: P3    
Version: unspecified   
Target Milestone: 0.90   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:

Description from-classpath 2005-02-13 14:40:02 UTC
According to Sun's API documentation, java.util.regex.Matcher.group(int) returns null if the specified group didn't match. This isn't mentioned in the documentation of the respective method in Classpath 0.13. Further, the implementation is to return gnu.regexp.REMatch.toString(group), which is an empty string in this situation.

There is also a related internal inconsistency in Classpath resulting from this implementation, because REMatch.toString(int) doesn't throw the exception that Match.group(int) promises in case the group doesn't exist in the pattern.

Attached is a test program that prints two OKs on Sun's JDK
1.5.0 and two shoulds on Kaffe 1.1.4 and Sablevm 1.1.9.
Comment 1 from-classpath 2005-05-10 22:22:40 UTC
java.util.regex.Matcher returns IllegalStateException for itself, see assertMatchOp at the end of Matcher.java; so gnu.regexp.REMatch doesn't have to throw it.

/gnu/regexp/REMatch.java, however, does incorrectly return empty strings instead of null values for groups that are outside the range of the regex's result.

Attached is a simple patch that corrects the issue in REMatch.java.
Comment 2 from-classpath 2005-05-10 23:02:06 UTC
My patch was not done with following the patch-from-project-root-directory convention, I have attached a corrected patch.  I have also renamed it.

Comment 3 Ito Kazumitsu 2006-01-17 22:24:40 UTC
The proposed patch seems to have been lost when this bug was carried
over from the old bug tracking system.

Does anyone have the proposed patch?

Comment 4 Mark Wielaard 2006-01-29 21:01:53 UTC
Subject: Re:  java.util.regex.Matcher.group(int)
	differs from Sun's API

On Tue, 2006-01-17 at 22:24 +0000, kaz at maczuka dot gcd dot org wrote:
> ------- Comment #3 from kaz at maczuka dot gcd dot org  2006-01-17 22:24 -------
> The proposed patch seems to have been lost when this bug was carried
> over from the old bug tracking system.
> 
> Does anyone have the proposed patch?

Unfortunately it seems to have been lost.
It was submitted by the savannah user Joe Gallo <tellerpenn>:
http://savannah.gnu.org/users/tellerpenn

Comment 5 cvs-commit@developer.classpath.org 2006-01-31 16:27:01 UTC
Subject: Bug 22873

CVSROOT:	/cvsroot/classpath
Module name:	classpath
Branch: 	
Changes by:	Ito Kazumitsu <itokaz@savannah.gnu.org>	06/01/31 14:57:43

Modified files:
	.              : ChangeLog 
	gnu/regexp     : REMatch.java 

Log message:
	2006-01-31  Ito Kazumitsu  <kaz@maczuka.gcd.org>
	
	Fixes bug #22873
	* gnu/regexp/REMatch(toString(int)): Throw IndexOutOfBoundsException
	for an invalid index and return null for a skipped group.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/classpath/classpath/ChangeLog.diff?tr1=1.6231&tr2=1.6232&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/classpath/classpath/gnu/regexp/REMatch.java.diff?tr1=1.4&tr2=1.5&r1=text&r2=text



Comment 6 Ito Kazumitsu 2006-01-31 16:31:49 UTC
Fixed.