in src/main/java/org/apache/commons/io/FilenameUtils.java [1574:1650]
public static boolean wildcardMatch(final String fileName, final String wildcardMatcher, IOCase ioCase) {
if (fileName == null && wildcardMatcher == null) {
return true;
}
if (fileName == null || wildcardMatcher == null) {
return false;
}
ioCase = IOCase.value(ioCase, IOCase.SENSITIVE);
final String[] wcs = splitOnTokens(wildcardMatcher);
boolean anyChars = false;
int textIdx = 0;
int wcsIdx = 0;
final Deque<int[]> backtrack = new ArrayDeque<>(wcs.length);
// loop around a backtrack stack, to handle complex * matching
do {
if (!backtrack.isEmpty()) {
final int[] array = backtrack.pop();
wcsIdx = array[0];
textIdx = array[1];
anyChars = true;
}
// loop whilst tokens and text left to process
while (wcsIdx < wcs.length) {
if (wcs[wcsIdx].equals("?")) {
// ? so move to next text char
textIdx++;
if (textIdx > fileName.length()) {
break;
}
anyChars = false;
} else if (wcs[wcsIdx].equals("*")) {
// set any chars status
anyChars = true;
if (wcsIdx == wcs.length - 1) {
textIdx = fileName.length();
}
} else {
// matching text token
if (anyChars) {
// any chars then try to locate text token
textIdx = ioCase.checkIndexOf(fileName, textIdx, wcs[wcsIdx]);
if (textIdx == NOT_FOUND) {
// token not found
break;
}
final int repeat = ioCase.checkIndexOf(fileName, textIdx + 1, wcs[wcsIdx]);
if (repeat >= 0) {
backtrack.push(new int[] {wcsIdx, repeat});
}
} else if (!ioCase.checkRegionMatches(fileName, textIdx, wcs[wcsIdx])) {
// matching from current position
// couldn't match token
break;
}
// matched text token, move text index to end of matched token
textIdx += wcs[wcsIdx].length();
anyChars = false;
}
wcsIdx++;
}
// full match
if (wcsIdx == wcs.length && textIdx == fileName.length()) {
return true;
}
} while (!backtrack.isEmpty());
return false;
}