public static boolean wildcardMatch()

in aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SearchPattern.java [45:104]


    public static boolean wildcardMatch(@Nullable String pattern, @Nullable String text, boolean caseInsensitive) {
        if (pattern == null || text == null) {
            return false;
        }

        int patternLength = pattern.length();
        int textLength = text.length();
        if (patternLength == 0) {
            return textLength == 0;
        }

        // Check the special case of a single * pattern, as it's common
        if (isWildcardGlob(pattern)) {
            return true;
        }

        if (caseInsensitive) {
            pattern = pattern.toLowerCase();
            text = text.toLowerCase();
        }

        // Infix globs are relatively rare, and the below search is expensive especially when
        // Balsa is used a lot. Check for infix globs and, in their absence, do the simple thing
        int indexOfGlob = pattern.indexOf('*');
        if (indexOfGlob == -1 || indexOfGlob == patternLength - 1) {
            return simpleWildcardMatch(pattern, text);
        }

        /*
         * The res[i] is used to record if there is a match
         * between the first i chars in text and the first j chars in pattern.
         * So will return res[textLength+1] in the end
         * Loop from the beginning of the pattern
         * case not '*': if text[i]==pattern[j] or pattern[j] is '?', and res[i] is true,
         *   set res[i+1] to true, otherwise false
         * case '*': since '*' can match any globing, as long as there is a true in res before i
         *   all the res[i+1], res[i+2],...,res[textLength] could be true
        */
        boolean[] res = new boolean[textLength + 1];
        res[0] = true;
        for (int j = 0; j < patternLength; j++) {
            char p = pattern.charAt(j);
            if (p != '*') {
                for (int i = textLength - 1; i >= 0; i--) {
                    char t = text.charAt(i);
                    res[i + 1] = res[i] && (p == '?' || (p == t));
                }
            } else {
                int i = 0;
                while (i <= textLength && !res[i]) {
                    i++;
                }
                for (; i <= textLength; i++) {
                    res[i] = true;
                }
            }
            res[0] = res[0] && p == '*';
        }
        return res[textLength];
    }