Exemple #1
0
        private static bool NewPatternMatch(PatternTokenizer tokenizer, String str)
        {
            // If no more tokens and str is empty, we matched
            if (!tokenizer.HasNext())
                return (str.Length == 0);

            // Get the next token from the tokenizer
            string token = tokenizer.NextToken();

            // Is it a wild card token?
            if (tokenizer.LastWasWildcard) {
                // Yes, what are the minimum and maximum extent of characters to match
                // by this wildcard string?
                int strLen = str.Length;
                int min = 0;
                int max = 0;
                for (int i = 0; i < token.Length; ++i) {
                    if (token[i] == ONE_CHAR) {
                        ++min;
                        ++max;
                    } else if (token[i] == ZERO_OR_MORE_CHARS) {
                        max = strLen;
                    } else {
                        throw new ApplicationException("Tokenizer error");
                    }
                }
                // If it's not possible to match this size string,
                if (min > strLen) {
                    return false;
                }
                // If there are no more tokens to match,
                if (!tokenizer.HasNext()) {
                    // If str_len falls within the size of the pattern we can match
                    // then return true, otherwise false
                    return strLen >= min && strLen <= max;
                }

                // Search for the index of the next token. It's not possible for this to
                // be a wildcard.
                string next_tok = tokenizer.NextToken();
                int p = min;
                while (true) {
                    p = str.IndexOf(next_tok, p);
                    if (p < 0 || p > max) {
                        // Not found, so fail this
                        return false;
                    }
                    // Recurse at the point we found
                    int state = tokenizer.Position;
                    if (NewPatternMatch(tokenizer, str.Substring(p + next_tok.Length))) {
                        return true;
                    }
                    // Reverse state if the search failed and try again
                    tokenizer.Position = state;
                    ++p;
                }
            }

            // Not a wild card, so match

            // If the string doesn't match the token, we return false
            if (!str.StartsWith(token))
                return false;

            // Otherwise recurse
            return NewPatternMatch(tokenizer, str.Substring(token.Length));
        }