mirror of
https://github.com/alibaba/arthas.git
synced 2026-11-30 15:33:59 +00:00
1. change the param's name, from 'string' to 'target' 2. get rid of the magic value of: * ? \ 3. bugfix: return false if the pattern or target is NULL
This commit is contained in:
1 parent
537be5ba71
commit
9237e9b32a
1 file changed
+31
-15
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user