Browse Source

Insert empty strings instead of "null" for groups that didn't match.

PR: 14619


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274344 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
f7d07d585c
4 changed files with 32 additions and 7 deletions
  1. +7
    -2
      src/main/org/apache/tools/ant/util/regexp/JakartaOroMatcher.java
  2. +7
    -2
      src/main/org/apache/tools/ant/util/regexp/JakartaRegexpMatcher.java
  3. +7
    -2
      src/main/org/apache/tools/ant/util/regexp/Jdk14RegexpMatcher.java
  4. +11
    -1
      src/testcases/org/apache/tools/ant/util/regexp/RegexpMatcherTest.java

+ 7
- 2
src/main/org/apache/tools/ant/util/regexp/JakartaOroMatcher.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -144,7 +144,12 @@ public class JakartaOroMatcher implements RegexpMatcher {
MatchResult mr = matcher.getMatch(); MatchResult mr = matcher.getMatch();
int cnt = mr.groups(); int cnt = mr.groups();
for (int i = 0; i < cnt; i++) { for (int i = 0; i < cnt; i++) {
v.addElement(mr.group(i));
String match = mr.group(i);
// treat non-matching groups as empty matches
if (match == null) {
match = "";
}
v.addElement(match);
} }
return v; return v;
} }


+ 7
- 2
src/main/org/apache/tools/ant/util/regexp/JakartaRegexpMatcher.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -133,7 +133,12 @@ public class JakartaRegexpMatcher implements RegexpMatcher {
Vector v = new Vector(); Vector v = new Vector();
int cnt = reg.getParenCount(); int cnt = reg.getParenCount();
for (int i = 0; i < cnt; i++) { for (int i = 0; i < cnt; i++) {
v.addElement(reg.getParen(i));
String match = reg.getParen(i);
// treat non-matching groups as empty matches
if (match == null) {
match = "";
}
v.addElement(match);
} }
return v; return v;
} }


+ 7
- 2
src/main/org/apache/tools/ant/util/regexp/Jdk14RegexpMatcher.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -144,7 +144,12 @@ public class Jdk14RegexpMatcher implements RegexpMatcher {
Vector v = new Vector(); Vector v = new Vector();
int cnt = matcher.groupCount(); int cnt = matcher.groupCount();
for (int i = 0; i <= cnt; i++) { for (int i = 0; i <= cnt; i++) {
v.addElement(matcher.group(i));
String match = matcher.group(i);
// treat non-matching groups as empty matches
if (match == null) {
match = "";
}
v.addElement(match);
} }
return v; return v;
} }


+ 11
- 1
src/testcases/org/apache/tools/ant/util/regexp/RegexpMatcherTest.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -133,6 +133,16 @@ public abstract class RegexpMatcherTest extends TestCase {
assertEquals("b", (String) v.elementAt(2)); assertEquals("b", (String) v.elementAt(2));
} }


public void testBugzillaReport14619() {
reg.setPattern("^(.*)/src/((.*/)*)([a-zA-Z0-9_\\.]+)\\.java$");
Vector v = reg.getGroups("de/tom/src/Google.java");
assertEquals(5, v.size());
assertEquals("de/tom", v.elementAt(1));
assertEquals("", v.elementAt(2));
assertEquals("", v.elementAt(3));
assertEquals("Google", v.elementAt(4));
}

public void testCaseInsensitiveMatch() { public void testCaseInsensitiveMatch() {
reg.setPattern("aaaa"); reg.setPattern("aaaa");
assertTrue("aaaa doesn't match AAaa", !reg.matches("AAaa")); assertTrue("aaaa doesn't match AAaa", !reg.matches("AAaa"));


Loading…
Cancel
Save