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.
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.
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.
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?
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>:
Subject: Bug 22873
Module name: classpath
Changes by: Ito Kazumitsu <email@example.com> 06/01/31 14:57:43
. : ChangeLog
gnu/regexp : REMatch.java
2006-01-31 Ito Kazumitsu <firstname.lastname@example.org>
Fixes bug #22873
* gnu/regexp/REMatch(toString(int)): Throw IndexOutOfBoundsException
for an invalid index and return null for a skipped group.