Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update NamingSettings and DocumentationSettings to keep one Regex instance instead of calling Regex.IsMatch #3639

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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);
}
}
}