#region License

/*
 * Copyright © 2002-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

using System;

namespace Spring.Util
{
    /// <summary>
    /// Utility methods for simple pattern matching, in particular for
    /// Spring's typical "xxx*", "*xxx" and "*xxx*" pattern styles.
    /// </summary>
    /// <author>Juergen Hoeller</author>
    /// <author>Mark Pollack</author>
    public static class PatternMatchUtils
    {
        /// <summary> Match a String against the given pattern, supporting the following simple
        /// pattern styles: "xxx*", "*xxx" and "*xxx*" matches, as well as direct equality.
        /// </summary>
        /// <param name="pattern">the pattern to match against
        /// </param>
        /// <param name="str">the String to match
        /// </param>
        /// <param name="stringComparison"></param>
        /// <returns> whether the String matches the given pattern
        /// </returns>
        public static bool SimpleMatch(string pattern, string str, StringComparison stringComparison)
        {
            if (string.Equals(pattern, str, stringComparison) || "*" == pattern)
            {
                return true;
            }

            if (pattern == null || str == null || pattern.Length == 0)
            {
                return false;
            }

            var startsWithWildcard = pattern[0] == '*';
            var endsWithWildcard = pattern[pattern.Length - 1] == '*';

            if (startsWithWildcard
                && endsWithWildcard
                && str.IndexOf(pattern.Substring(1, pattern.Length - 2), 1, stringComparison) != -1)
            {
                return true;
            }

            if (startsWithWildcard
                && str.EndsWith(pattern.Substring(1, pattern.Length - 1), stringComparison))
            {
                return true;
            }

            if (endsWithWildcard
                && str.StartsWith(pattern.Substring(0, pattern.Length - 1), stringComparison))
            {
                return true;
            }
            return false;
        }

        /// <summary> Match a String against the given patterns, supporting the following simple
        /// pattern styles: "xxx*", "*xxx" and "*xxx*" matches, as well as direct equality.
        /// </summary>
        /// <param name="patterns">the patterns to match against
        /// </param>
        /// <param name="str">the String to match
        /// </param>
        /// <returns> whether the String matches any of the given patterns
        /// </returns>
        public static bool SimpleMatch(string[] patterns, string str)
        {
            if (patterns != null)
            {
                for (int i = 0; i < patterns.Length; i++)
                {
                    
                    if (SimpleMatch(patterns[i], str, StringComparison.Ordinal))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

    }
}