Skip to content

Commit

Permalink
Merge pull request #3639 from bjornhellander/feature/naming-settings-…
Browse files Browse the repository at this point in the history
…regex

Update NamingSettings and DocumentationSettings to keep one Regex instance instead of calling Regex.IsMatch
  • Loading branch information
sharwell authored Apr 25, 2023
2 parents 1c7f5f3 + 51b4ba8 commit 48ae833
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<string, bool> BuildCopyrightText(string fileName)
{
bool canCache = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 48ae833

Please sign in to comment.