Bugfix wildcardmatcher

pull/148/head
Huxing Zhang 7 years ago committed by GitHub
commit e3ce4e7e51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,6 +8,12 @@ public class WildcardMatcher implements Matcher<String> {
private final String pattern; private final String pattern;
private static final Character ASTERISK = '*';
private static final Character QUESTION_MARK = '?';
private static final Character ESCAPE = '\\';
public WildcardMatcher(String pattern) { public WildcardMatcher(String pattern) {
this.pattern = pattern; this.pattern = pattern;
} }
@ -21,51 +27,61 @@ public class WildcardMatcher implements Matcher<String> {
/** /**
* Internal matching recursive function. * Internal matching recursive function.
*/ */
private boolean match(String string, String pattern, int stringStartNdx, int patternStartNdx) { private boolean match(String target, String pattern, int stringStartNdx, int patternStartNdx) {
//#135
if(target==null || pattern==null){
return false;
}
int pNdx = patternStartNdx; int pNdx = patternStartNdx;
int sNdx = stringStartNdx; int sNdx = stringStartNdx;
int pLen = pattern.length(); int pLen = pattern.length();
if (pLen == 1) { if (pLen == 1) {
if (pattern.charAt(0) == '*') { // speed-up // speed-up
if (pattern.charAt(0) == ASTERISK) {
return true; return true;
} }
} }
int sLen = string.length(); int sLen = target.length();
boolean nextIsNotWildcard = false; boolean nextIsNotWildcard = false;
while (true) { while (true) {
// check if end of string and/or pattern occurred // check if end of string and/or pattern occurred
if ((sNdx >= sLen)) { // end of string still may have pending '*' callback pattern if ((sNdx >= sLen)) {
while ((pNdx < pLen) && (pattern.charAt(pNdx) == '*')) { // end of string still may have pending '*' callback pattern
while ((pNdx < pLen) && (pattern.charAt(pNdx) == ASTERISK)) {
pNdx++; pNdx++;
} }
return pNdx >= pLen; return pNdx >= pLen;
} }
if (pNdx >= pLen) { // end of pattern, but not end of the string // end of pattern, but not end of the string
if (pNdx >= pLen) {
return false; return false;
} }
char p = pattern.charAt(pNdx); // pattern char // pattern char
char p = pattern.charAt(pNdx);
// perform logic // perform logic
if (!nextIsNotWildcard) { if (!nextIsNotWildcard) {
if (p == '\\') { if (p == ESCAPE) {
pNdx++; pNdx++;
nextIsNotWildcard = true; nextIsNotWildcard = true;
continue; continue;
} }
if (p == '?') { if (p == QUESTION_MARK) {
sNdx++; sNdx++;
pNdx++; pNdx++;
continue; continue;
} }
if (p == '*') { if (p == ASTERISK) {
char pnext = 0; // next pattern char // next pattern char
char pnext = 0;
if (pNdx + 1 < pLen) { if (pNdx + 1 < pLen) {
pnext = pattern.charAt(pNdx + 1); pnext = pattern.charAt(pNdx + 1);
} }
if (pnext == '*') { // double '*' have the same effect as one '*' // double '*' have the same effect as one '*'
if (pnext == ASTERISK) {
pNdx++; pNdx++;
continue; continue;
} }
@ -74,8 +90,8 @@ public class WildcardMatcher implements Matcher<String> {
// find recursively if there is any substring from the end of the // find recursively if there is any substring from the end of the
// line that matches the rest of the pattern !!! // line that matches the rest of the pattern !!!
for (i = string.length(); i >= sNdx; i--) { for (i = target.length(); i >= sNdx; i--) {
if (match(string, pattern, i, pNdx)) { if (match(target, pattern, i, pNdx)) {
return true; return true;
} }
} }
@ -86,7 +102,7 @@ public class WildcardMatcher implements Matcher<String> {
} }
// check if pattern char and string char are equals // check if pattern char and string char are equals
if (p != string.charAt(sNdx)) { if (p != target.charAt(sNdx)) {
return false; return false;
} }

@ -0,0 +1,36 @@
package com.taobao.arthas.core.util.matcher;
import org.junit.Assert;
import org.junit.Test;
/**
* @author earayu
*/
public class WildcardMatcherTest {
@Test
public void testMatching(){
Assert.assertFalse(new WildcardMatcher(null).matching(null));
Assert.assertFalse(new WildcardMatcher(null).matching("foo"));
Assert.assertFalse(new WildcardMatcher("foo").matching(null));
Assert.assertTrue(new WildcardMatcher("foo").matching("foo"));
Assert.assertFalse(new WildcardMatcher("foo").matching("bar"));
Assert.assertTrue(new WildcardMatcher("foo*").matching("foo"));
Assert.assertTrue(new WildcardMatcher("foo*").matching("fooooooobar"));
Assert.assertTrue(new WildcardMatcher("f*r").matching("fooooooobar"));
Assert.assertFalse(new WildcardMatcher("foo*").matching("fo"));
Assert.assertFalse(new WildcardMatcher("foo*").matching("bar"));
Assert.assertFalse(new WildcardMatcher("foo?").matching("foo"));
Assert.assertTrue(new WildcardMatcher("foo?").matching("foob"));
Assert.assertTrue(new WildcardMatcher("foo\\*").matching("foo*"));
Assert.assertFalse(new WildcardMatcher("foo\\*").matching("foooooo"));
Assert.assertTrue(new WildcardMatcher("foo\\?").matching("foo?"));
Assert.assertFalse(new WildcardMatcher("foo\\?").matching("foob"));
}
}
Loading…
Cancel
Save