wildcardMatch: function wildcardMatch()

in packages/core/lib/utils.js [55:141]


  wildcardMatch: function wildcardMatch(pattern, text) {
    if (pattern === undefined || text === undefined) {
      return false;
    }

    if (pattern.length === 1 && pattern.charAt(0) === '*') {
      return true;
    }

    var patternLength = pattern.length;
    var textLength = text.length;
    var indexOfGlob = pattern.indexOf('*');

    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
    if (indexOfGlob === -1 || indexOfGlob === (patternLength - 1)) {
      var match = function simpleWildcardMatch() {
        var j = 0;

        for (var i = 0; i < patternLength; i++) {
          var patternChar = pattern.charAt(i);
          if (patternChar === '*') {
            // Presumption for this method is that globs only occur at end
            return true;
          } else if (patternChar === '?') {
            if (j === textLength) {
              return false;
            } // No character to match

            j++;
          } else {
            if (j >= textLength || patternChar != text.charAt(j)) {
              return false;
            }

            j++;
          }
        }
        // Ate up all the pattern and didn't end at a glob, so a match will have consumed all
        // the text
        return j === textLength;
      };

      return match();
    }

    /*
     * The matchArray[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 matchArray[textLength+1] in the end
     * Loop from the beginning of the pattern
     * case not '*': if text[i]==pattern[j] or pattern[j] is '?', and matchArray[i] is true,
     *   set matchArray[i+1] to true, otherwise false
     * case '*': since '*' can match any globing, as long as there is a true in matchArray before i
     *   all the matchArray[i+1], matchArray[i+2],...,matchArray[textLength] could be true
    */

    var matchArray = [];
    matchArray[0] = true;

    for (var j = 0; j < patternLength; j++) {
      var i;
      var patternChar = pattern.charAt(j);

      if (patternChar != '*') {
        for (i = textLength - 1; i >= 0; i--) {
          matchArray[i+1] = !!matchArray[i] && (patternChar === '?' || (patternChar === text.charAt(i)));
        }
      } else {
        i = 0;

        while (i <= textLength && !matchArray[i]) {
          i++;
        }

        for (i; i <= textLength; i++) {
          matchArray[i] = true;
        }
      }
      matchArray[0] = (matchArray[0] && patternChar === '*');
    }

    return matchArray[textLength];
  },