From 51b4ba8458cd2144f28c62958817fccccec8007b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Hellander?= Date: Tue, 25 Apr 2023 17:39:19 +0200 Subject: [PATCH] Update NamingSettings and DocumentationSettings to not call Regex.IsMatch() #3629 --- .../ObjectModel/DocumentationSettings.cs | 16 +++++++++++++++- .../Settings/ObjectModel/NamingSettings.cs | 19 ++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs index 82698721b..c0629daa0 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/DocumentationSettings.cs @@ -199,7 +199,7 @@ protected internal DocumentationSettings(JsonObject documentationSettingsObject, { string name = child.Key; - if (!Regex.IsMatch(name, "^[a-zA-Z0-9]+$")) + if (!IsValidVariableName(name)) { continue; } @@ -354,6 +354,20 @@ public string GetCopyrightText(string fileName) return this.copyrightTextCache; } + private static bool IsValidVariableName(string name) + { + // Equivalent to Regex.IsMatch(prefix, "^[a-zA-Z0-9]+$") + for (var i = 0; i < name.Length; i++) + { + if (name[i] is not ((>= 'a' and <= 'z') or (>= 'A' and <= 'Z') or (>= '0' and <= '9'))) + { + return false; + } + } + + return name.Length > 0; + } + private KeyValuePair BuildCopyrightText(string fileName) { bool canCache = true; diff --git a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs index 9dd7b5e96..0b7278f91 100644 --- a/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs +++ b/StyleCop.Analyzers/StyleCop.Analyzers/Settings/ObjectModel/NamingSettings.cs @@ -7,7 +7,6 @@ namespace StyleCop.Analyzers.Settings.ObjectModel { using System.Collections.Immutable; using System.Linq; - using System.Text.RegularExpressions; using LightJson; using StyleCop.Analyzers.Lightup; @@ -55,7 +54,7 @@ protected internal NamingSettings(JsonObject namingSettingsObject, AnalyzerConfi { var prefix = prefixJsonValue.ToStringValue(kvp.Key); - if (!Regex.IsMatch(prefix, "^[a-z]{1,2}$")) + if (!IsValidHungarianPrefix(prefix)) { continue; } @@ -86,7 +85,7 @@ protected internal NamingSettings(JsonObject namingSettingsObject, AnalyzerConfi allowCommonHungarianPrefixes ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.naming.allowCommonHungarianPrefixes"); allowedHungarianPrefixes ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.naming.allowedHungarianPrefixes") - ?.Where(value => Regex.IsMatch(value, "^[a-z]{1,2}$")) + ?.Where(value => IsValidHungarianPrefix(value)) .ToImmutableArray() .ToBuilder(); allowedNamespaceComponents ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.naming.allowedNamespaceComponents")?.ToBuilder(); @@ -115,5 +114,19 @@ protected internal NamingSettings(JsonObject namingSettingsObject, AnalyzerConfi public bool IncludeInferredTupleElementNames { get; } public TupleElementNameCase TupleElementNameCasing { get; } + + private static bool IsValidHungarianPrefix(string prefix) + { + // Equivalent to Regex.IsMatch(prefix, "^[a-z]{1,2}$") + for (var i = 0; i < prefix.Length; i++) + { + if (prefix[i] is not (>= 'a' and <= 'z')) + { + return false; + } + } + + return prefix.Length is (>= 1 and <= 2); + } } }