diff --git a/src/WireMock.Net/Matchers/RegexMatcher.cs b/src/WireMock.Net/Matchers/RegexMatcher.cs index 6f4c9ea0..dd3e6404 100644 --- a/src/WireMock.Net/Matchers/RegexMatcher.cs +++ b/src/WireMock.Net/Matchers/RegexMatcher.cs @@ -18,12 +18,18 @@ namespace WireMock.Matchers /// Initializes a new instance of the class. /// /// The pattern. - public RegexMatcher([NotNull, RegexPattern] string pattern) + /// IgnoreCase + public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) { Check.NotNull(pattern, nameof(pattern)); _pattern = pattern; - _expression = new Regex(_pattern, RegexOptions.Compiled); + + RegexOptions options = RegexOptions.Compiled; + if (ignoreCase) + options |= RegexOptions.IgnoreCase; + + _expression = new Regex(_pattern, options); } /// diff --git a/src/WireMock.Net/Matchers/WildcardMatcher.cs b/src/WireMock.Net/Matchers/WildcardMatcher.cs index 1ff651be..1cc107e7 100644 --- a/src/WireMock.Net/Matchers/WildcardMatcher.cs +++ b/src/WireMock.Net/Matchers/WildcardMatcher.cs @@ -1,111 +1,21 @@ -using JetBrains.Annotations; -using WireMock.Validation; +using System.Text.RegularExpressions; +using JetBrains.Annotations; namespace WireMock.Matchers { /// /// WildcardMatcher /// - /// - public class WildcardMatcher : IMatcher + /// + public class WildcardMatcher : RegexMatcher { - private readonly string _pattern; - private readonly bool _ignoreCase; - /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The pattern. /// IgnoreCase - public WildcardMatcher([NotNull] string pattern, bool ignoreCase = false) + public WildcardMatcher([NotNull] string pattern, bool ignoreCase = false) : base("^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$", ignoreCase) { - Check.NotNull(pattern, nameof(pattern)); - - _pattern = pattern; - _ignoreCase = ignoreCase; - } - - /// - /// Determines whether the specified input is match. - /// - /// The input. - /// - /// true if the specified input is match; otherwise, false. - /// - public bool IsMatch(string input) - { - return MatchWildcardString(_pattern, input); - } - - /// - /// Gets the pattern. - /// - /// Pattern - public string GetPattern() - { - return _pattern; - } - - /// - /// Copy/paste from http://www.codeproject.com/Tips/57304/Use-wildcard-characters-and-to-compare-strings - /// - private bool MatchWildcardString([NotNull] string pattern, string input) - { - if (input != null && _ignoreCase) - input = input.ToLower(); - - if (_ignoreCase) - pattern = pattern.ToLower(); - - if (string.CompareOrdinal(pattern, input) == 0) - { - return true; - } - - if (string.IsNullOrEmpty(input)) - { - return string.IsNullOrEmpty(pattern.Trim('*')); - } - - if (pattern.Length == 0) - { - return false; - } - - if (pattern[0] == '?') - { - return MatchWildcardString(pattern.Substring(1), input.Substring(1)); - } - - if (pattern[pattern.Length - 1] == '?') - { - return MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input.Substring(0, input.Length - 1)); - } - - if (pattern[0] == '*') - { - if (MatchWildcardString(pattern.Substring(1), input)) - { - return true; - } - return MatchWildcardString(pattern, input.Substring(1)); - } - - if (pattern[pattern.Length - 1] == '*') - { - if (MatchWildcardString(pattern.Substring(0, pattern.Length - 1), input)) - { - return true; - } - return MatchWildcardString(pattern, input.Substring(0, input.Length - 1)); - } - - if (pattern[0] == input[0]) - { - return MatchWildcardString(pattern.Substring(1), input.Substring(1)); - } - - return false; } } } \ No newline at end of file