Re: Subgroup wrong when matching (.)(?=(.)) against "XY"?

"Daniel F. Savarese" <[email protected]> Fri, 31 May 2002 12:36:24 -0400
Newsgroups gmane.comp.jakarta.oro.user
Message-ID <[email protected]>
In message <[email protected]>, "Daniel F. Savarese" 
writes:
>groups that match outside of the full match to be saved?  It's
>actually quite tricky to implement this without either maintaining
>a reference to a copy of the entire original input (undesirable) or
>screwing up a lot of other cases.  I'd like to mull this one over

Here's a patch that fixes the case you reported.  I don't think it
breaks anything else, but I can't be sure.  Any volunteers for those
unit tests we keep talking about?  At any rate, I don't want to apply
the patch until it has been well-tested.

daniel

--
To unsubscribe, e-mail:   <mailto:[email protected]>
For additional commands, e-mail: <mailto:[email protected]>
LookaheadAssertionGroup.patch.txt (text/plain, 2.4 KB)
Index: src/java/org/apache/oro/text/regex/Perl5MatchResult.java
===================================================================
RCS file: /home/cvs/jakarta-oro/src/java/org/apache/oro/text/regex/Perl5MatchResult.java,v
retrieving revision 1.4
diff -u -r1.4 Perl5MatchResult.java
--- src/java/org/apache/oro/text/regex/Perl5MatchResult.java	17 May 2001 21:59:33 -0000	1.4
+++ src/java/org/apache/oro/text/regex/Perl5MatchResult.java	31 May 2002 16:21:15 -0000
@@ -125,7 +125,11 @@
    * @return The length of the match.
    */
   public int length(){
-    return _match.length();
+    int length;
+
+    length = (_endGroupOffset[0] - _beginGroupOffset[0]);
+
+    return (length > 0 ? length : 0);
   }
 
 
Index: src/java/org/apache/oro/text/regex/Perl5Matcher.java
===================================================================
RCS file: /home/cvs/jakarta-oro/src/java/org/apache/oro/text/regex/Perl5Matcher.java,v
retrieving revision 1.18
diff -u -r1.18 Perl5Matcher.java
--- src/java/org/apache/oro/text/regex/Perl5Matcher.java	25 Jan 2002 09:32:28 -0000	1.18
+++ src/java/org/apache/oro/text/regex/Perl5Matcher.java	31 May 2002 16:21:16 -0000
@@ -241,7 +241,7 @@
   // Set the match result information.  Only call this if we successfully
   // matched.
   private void __setLastMatchResult() {
-    int offs;
+    int offs, maxEndOffs = 0;
 
     //endOffset+=dontTry;
 
@@ -251,10 +251,6 @@
     if(__endMatchOffsets[0] > __originalInput.length)
       throw new ArrayIndexOutOfBoundsException();
 
-    __lastMatchResult._match =
-      new String(__originalInput, __beginMatchOffsets[0],
-		 __endMatchOffsets[0] - __beginMatchOffsets[0]);
-
     __lastMatchResult._matchBeginOffset = __beginMatchOffsets[0];
 
     while(__numParentheses >= 0) {
@@ -269,15 +265,21 @@
 
       offs = __endMatchOffsets[__numParentheses];
 
-      if(offs >= 0)
+      if(offs >= 0) {
 	__lastMatchResult._endGroupOffset[__numParentheses] =
 	  offs - __lastMatchResult._matchBeginOffset;
-      else
+	if(offs > maxEndOffs && offs <= __originalInput.length)
+	  maxEndOffs = offs;
+      } else
 	__lastMatchResult._endGroupOffset[__numParentheses] =
 	  OpCode._NULL_OFFSET;
 
       --__numParentheses;
     }
+
+    __lastMatchResult._match =
+      new String(__originalInput, __beginMatchOffsets[0],
+		 maxEndOffs - __beginMatchOffsets[0]);
 
     // Free up for garbage collection
     __originalInput = null;