public static boolean like(String source, String exp) {
if (source == null || exp == null) {
return false;
}
int sourceLength = source.length();
int expLength = exp.length();
if (sourceLength == 0 || expLength == 0) {
return false;
}
boolean fuzzy = false;
char lastCharOfExp = 0;
int positionOfSource = 0;
for (int i = 0; i < expLength; i++) {
char ch = exp.charAt(i);
// ????
boolean escape = false;
if (lastCharOfExp == '\\') {
if (ch == '%' || ch == '_') {
escape = true;
// System.out.println("escape " + ch);
}
}
if (!escape && ch == '%') {
fuzzy = true;
} else if (!escape && ch == '_') {
if (positionOfSource >= sourceLength) {
return false;
}
positionOfSource++;// <<<----- ???1
} else if (ch != '\\') {// ????,?????????
if (positionOfSource >= sourceLength) {// ?????source????
return false;
}
if (lastCharOfExp == '%') { // ??????%,?????
int tp = source.indexOf(ch);
// System.out.println("?????=%,?????=" + ch + ",position=" + position + ",tp=" + tp);
if (tp == -1) { // ????????,????
return false;
}
if (tp >= positionOfSource) {
positionOfSource = tp + 1;// <<<----- ????
if (i == expLength - 1 && positionOfSource < sourceLength) { // exp??????????,????source?????????
return false;
}
} else {
return false;
}
} else if (source.charAt(positionOfSource) == ch) {// ????????ch??
positionOfSource++;
} else {
return false;
}
}
lastCharOfExp = ch;// <<<----- ??
// System.out.println("?????=" + ch + ",position=" + position);
}
// expr???????,???????,??source???????????source???
if (!fuzzy && positionOfSource < sourceLength) {
// System.out.println("?????=" + lastChar + ",position=" + position );
return false;
}
return true;// ????true
}
Assert.assertEquals(true, like("abc_d", "abc\\_d"));
Assert.assertEquals(true, like("abc%d", "abc\\%%d"));
Assert.assertEquals(false, like("abcd", "abc\\_d"));
String source = "1abcd";
Assert.assertEquals(true, like(source, "_%d"));
Assert.assertEquals(false, like(source, "%%a"));
Assert.assertEquals(false, like(source, "1"));
Assert.assertEquals(true, like(source, "%d"));
Assert.assertEquals(true, like(source, "%%%%"));
Assert.assertEquals(true, like(source, "1%_"));
Assert.assertEquals(false, like(source, "1%_2"));
Assert.assertEquals(false, like(source, "1abcdef"));
Assert.assertEquals(true, like(source, "1abcd"));
Assert.assertEquals(false, like(source, "1abcde"));
// ????case?????
Assert.assertEquals(true, like(source, "_%_"));
Assert.assertEquals(true, like(source, "_%____"));
Assert.assertEquals(true, like(source, "_____"));// 5?
Assert.assertEquals(false, like(source, "___"));// 3?
Assert.assertEquals(false, like(source, "__%____"));// 6?
Assert.assertEquals(false, like(source, "1"));
Assert.assertEquals(false, like(source, "a_%b"));
Assert.assertEquals(true, like(source, "1%"));
Assert.assertEquals(false, like(source, "d%"));
Assert.assertEquals(true, like(source, "_%"));
Assert.assertEquals(true, like(source, "_abc%"));
Assert.assertEquals(true, like(source, "%d"));
Assert.assertEquals(true, like(source, "%abc%"));
Assert.assertEquals(false, like(source, "ab_%"));
Assert.assertEquals(true, like(source, "1ab__"));
Assert.assertEquals(true, like(source, "1ab__%"));
Assert.assertEquals(false, like(source, "1ab___"));
Assert.assertEquals(true, like(source, "%"));
Assert.assertEquals(false, like(null, "1ab___"));
Assert.assertEquals(false, like(source, null));
Assert.assertEquals(false, like(source, ""));