From 6002a442683701a44ca21c87e62d29b549bdda16 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 27 Jun 2019 15:04:18 -0700 Subject: [PATCH 1/4] Add fast path for wildcard patterns that contains no wildcard characters --- .../engine/regex.cs | 35 ++++++++++++++++--- .../singleshell/config/MshSnapinInfo.cs | 6 +--- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index cd0f1aa12e6..4907b6314b4 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -60,6 +60,16 @@ public sealed class WildcardPattern // private Predicate _isMatch; + // + // chars that are considered special in a wildcard pattern + // + private readonly static char[] s_specialChars = new char[] { '*', '?', '[', ']', '`' }; + + // + // static match-all delegate that is shared by all WildcardPattern instances + // + private readonly static Predicate s_matchAll = _ => true; + // // wildcard pattern // @@ -149,7 +159,26 @@ private void Init() { if (Pattern.Length == 1 && Pattern[0] == '*') { - _isMatch = _ => true; + _isMatch = s_matchAll; + } + else if (Pattern.IndexOfAny(s_specialChars) == -1) + { + // No special characters present in the pattern, so we can just do a string comparison. + StringComparison stringComparison; + if (Options.HasFlag(WildcardOptions.IgnoreCase)) + { + stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant) + ? StringComparison.InvariantCultureIgnoreCase + : StringComparison.CurrentCultureIgnoreCase; + } + else + { + stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant) + ? StringComparison.InvariantCulture + : StringComparison.CurrentCulture; + } + + _isMatch = str => string.Equals(str, Pattern, stringComparison); } else { @@ -181,8 +210,6 @@ public bool IsMatch(string input) /// internal static string Escape(string pattern, char[] charsNotToEscape) { -#pragma warning disable 56506 - if (pattern == null) { throw PSTraceSource.NewArgumentNullException("pattern"); @@ -223,8 +250,6 @@ internal static string Escape(string pattern, char[] charsNotToEscape) } return s; - -#pragma warning restore 56506 } /// diff --git a/src/System.Management.Automation/singleshell/config/MshSnapinInfo.cs b/src/System.Management.Automation/singleshell/config/MshSnapinInfo.cs index a4aa70a1a5e..5a85ab04315 100644 --- a/src/System.Management.Automation/singleshell/config/MshSnapinInfo.cs +++ b/src/System.Management.Automation/singleshell/config/MshSnapinInfo.cs @@ -1039,11 +1039,7 @@ internal static Collection ReadEnginePSSnapIns() string moduleName = Path.Combine(applicationBase, defaultMshSnapinInfo.AssemblyName + ".dll"); - if (File.Exists(moduleName)) - { - moduleName = Path.Combine(applicationBase, defaultMshSnapinInfo.AssemblyName + ".dll"); - } - else + if (!File.Exists(moduleName)) { moduleName = defaultMshSnapinInfo.AssemblyName; } From 7abfd5fc22029038be2708c59e24fac26fd654e3 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Thu, 27 Jun 2019 15:38:27 -0700 Subject: [PATCH 2/4] Fix CodeFactor issues --- src/System.Management.Automation/engine/regex.cs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index 4907b6314b4..ddfa7ce1dcd 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -50,34 +50,22 @@ public enum WildcardOptions /// public sealed class WildcardPattern { - // // char that escapes special chars - // private const char escapeChar = '`'; - // // we convert a wildcard pattern to a predicate - // private Predicate _isMatch; - // // chars that are considered special in a wildcard pattern - // - private readonly static char[] s_specialChars = new char[] { '*', '?', '[', ']', '`' }; + private static readonly char[] s_specialChars = new char[] { '*', '?', '[', ']', '`' }; - // // static match-all delegate that is shared by all WildcardPattern instances - // - private readonly static Predicate s_matchAll = _ => true; + private static readonly Predicate s_matchAll = _ => true; - // // wildcard pattern - // internal string Pattern { get; } - // // options that control match behavior - // internal WildcardOptions Options { get; } = WildcardOptions.None; /// From db57347da7a919619a073486bb0ac4f792f38a68 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 28 Jun 2019 11:33:32 -0700 Subject: [PATCH 3/4] Refactor the init() method --- .../engine/regex.cs | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index ddfa7ce1dcd..ad9990a78e7 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -143,37 +143,40 @@ public static WildcardPattern Get(string pattern, WildcardOptions options) /// True on success, false otherwise. private void Init() { - if (_isMatch == null) + if (_isMatch != null) { - if (Pattern.Length == 1 && Pattern[0] == '*') - { - _isMatch = s_matchAll; - } - else if (Pattern.IndexOfAny(s_specialChars) == -1) - { - // No special characters present in the pattern, so we can just do a string comparison. - StringComparison stringComparison; - if (Options.HasFlag(WildcardOptions.IgnoreCase)) - { - stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant) - ? StringComparison.InvariantCultureIgnoreCase - : StringComparison.CurrentCultureIgnoreCase; - } - else - { - stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant) - ? StringComparison.InvariantCulture - : StringComparison.CurrentCulture; - } + return; + } + + if (Pattern.Length == 1 && Pattern[0] == '*') + { + _isMatch = s_matchAll; + return; + } - _isMatch = str => string.Equals(str, Pattern, stringComparison); + if (Pattern.IndexOfAny(s_specialChars) == -1) + { + // No special characters present in the pattern, so we can just do a string comparison. + StringComparison stringComparison; + if (Options.HasFlag(WildcardOptions.IgnoreCase)) + { + stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant) + ? StringComparison.InvariantCultureIgnoreCase + : StringComparison.CurrentCultureIgnoreCase; } else { - var matcher = new WildcardPatternMatcher(this); - _isMatch = matcher.IsMatch; + stringComparison = Options.HasFlag(WildcardOptions.CultureInvariant) + ? StringComparison.InvariantCulture + : StringComparison.CurrentCulture; } + + _isMatch = str => string.Equals(str, Pattern, stringComparison); + return; } + + var matcher = new WildcardPatternMatcher(this); + _isMatch = matcher.IsMatch; } /// From 674e5e27fc765d5fb17df4a9bc9ed6dfa4b2f2b2 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 28 Jun 2019 14:50:56 -0700 Subject: [PATCH 4/4] fix Codacy issue Co-Authored-By: Joel Sallow (/u/ta11ow) <32407840+vexx32@users.noreply.github.com> --- src/System.Management.Automation/engine/regex.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/regex.cs b/src/System.Management.Automation/engine/regex.cs index ad9990a78e7..f4e7ee1ecd6 100644 --- a/src/System.Management.Automation/engine/regex.cs +++ b/src/System.Management.Automation/engine/regex.cs @@ -57,7 +57,7 @@ public sealed class WildcardPattern private Predicate _isMatch; // chars that are considered special in a wildcard pattern - private static readonly char[] s_specialChars = new char[] { '*', '?', '[', ']', '`' }; + private static readonly char[] s_specialChars = new[] { '*', '?', '[', ']', '`' }; // static match-all delegate that is shared by all WildcardPattern instances private static readonly Predicate s_matchAll = _ => true;