Author: vgritsenko Date: Tue Mar 13 18:41:38 2007 New Revision: 517970 URL: http://svn.apache.org/viewvc?view=rev&rev=517970 Log: fix bug 36405
Modified: jakarta/regexp/trunk/docs/changes.html jakarta/regexp/trunk/src/java/org/apache/regexp/RE.java jakarta/regexp/trunk/src/java/org/apache/regexp/RETest.java jakarta/regexp/trunk/xdocs/changes.xml Modified: jakarta/regexp/trunk/docs/changes.html URL: http://svn.apache.org/viewvc/jakarta/regexp/trunk/docs/changes.html?view=diff&rev=517970&r1=517969&r2=517970 ============================================================================== --- jakarta/regexp/trunk/docs/changes.html (original) +++ jakarta/regexp/trunk/docs/changes.html Tue Mar 13 18:41:38 2007 @@ -91,6 +91,9 @@ <h3>Version 1.5-dev</h3> <ul> +<li>Fixed Bug + <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=36405">36405</a>: + Referencing an optional backreference returns null (VG)</li> <li>Implemented optimization: RE compiler omits branch instruction if only one branch is present (VG)</li> <li>Fixed Bug Modified: jakarta/regexp/trunk/src/java/org/apache/regexp/RE.java URL: http://svn.apache.org/viewvc/jakarta/regexp/trunk/src/java/org/apache/regexp/RE.java?view=diff&rev=517970&r1=517969&r2=517970 ============================================================================== --- jakarta/regexp/trunk/src/java/org/apache/regexp/RE.java (original) +++ jakarta/regexp/trunk/src/java/org/apache/regexp/RE.java Tue Mar 13 18:41:38 2007 @@ -1674,33 +1674,23 @@ int lCurrentPosition = 0; int lLastPosition = -2; int lLength = substitution.length(); - boolean bAddedPrefix = false; while ((lCurrentPosition = substitution.indexOf("$", lCurrentPosition)) >= 0) { if ((lCurrentPosition == 0 || substitution.charAt(lCurrentPosition - 1) != '\\') - && lCurrentPosition+1 < lLength) + && lCurrentPosition + 1 < lLength) { char c = substitution.charAt(lCurrentPosition + 1); if (c >= '0' && c <= '9') { - if (!bAddedPrefix) - { - // Append everything between the beginning of the - // substitution string and the current $ sign - ret.append(substitution.substring(0, lCurrentPosition)); - bAddedPrefix = true; - } - else - { - // Append everything between the last and the current $ sign - ret.append(substitution.substring(lLastPosition + 2, lCurrentPosition)); - } + // Append everything between the last and the current $ sign + ret.append(substitution.substring(lLastPosition + 2, lCurrentPosition)); - // Append the parenthesized expression - // Note: if a parenthesized expression of the requested - // index is not available "null" is added to the string - ret.append(getParen(c - '0')); + // Append the parenthesized expression, if present + String val = getParen(c - '0'); + if (val != null) { + ret.append(val); + } lLastPosition = lCurrentPosition; } } Modified: jakarta/regexp/trunk/src/java/org/apache/regexp/RETest.java URL: http://svn.apache.org/viewvc/jakarta/regexp/trunk/src/java/org/apache/regexp/RETest.java?view=diff&rev=517970&r1=517969&r2=517970 ============================================================================== --- jakarta/regexp/trunk/src/java/org/apache/regexp/RETest.java (original) +++ jakarta/regexp/trunk/src/java/org/apache/regexp/RETest.java Tue Mar 13 18:41:38 2007 @@ -535,8 +535,25 @@ // Test for Bug #36106 r = new RE("fo(o)"); - actual = r.subst("foo", "$1", RE.REPLACE_BACKREFERENCES); + actual = r.subst("foo", + "$1", RE.REPLACE_BACKREFERENCES); assertEquals("Wrong subst() result", "o", actual); + + // Test for Bug #36405 + r = new RE("^(.*?)(x)?$"); + actual = r.subst("abc", + "$1-$2", RE.REPLACE_BACKREFERENCES); + assertEquals("Wrong subst() result", "abc-", actual); + + r = new RE("^(.*?)(x)?$"); + actual = r.subst("abcx", + "$1-$2", RE.REPLACE_BACKREFERENCES); + assertEquals("Wrong subst() result", "abc-x", actual); + + r = new RE("([a-b]+?)([c-d]+)"); + actual = r.subst("zzabcdzz", + "$1-$2", RE.REPLACE_BACKREFERENCES); + assertEquals("Wrong subst() result", "zzab-cdzz", actual); } public void assertEquals(String message, String expected, String actual) Modified: jakarta/regexp/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewvc/jakarta/regexp/trunk/xdocs/changes.xml?view=diff&rev=517970&r1=517969&r2=517970 ============================================================================== --- jakarta/regexp/trunk/xdocs/changes.xml (original) +++ jakarta/regexp/trunk/xdocs/changes.xml Tue Mar 13 18:41:38 2007 @@ -34,6 +34,9 @@ <h3>Version 1.5-dev</h3> <ul> +<li>Fixed Bug + <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=36405">36405</a>: + Referencing an optional backreference returns null (VG)</li> <li>Implemented optimization: RE compiler omits branch instruction if only one branch is present (VG)</li> <li>Fixed Bug --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]