Skip to content

Commit

Permalink
Use regex properties instead of methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Apollo3zehn committed Jul 26, 2024
1 parent 98b825b commit 11b0106
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 33 deletions.
7 changes: 3 additions & 4 deletions src/Nexus.UI/Core/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ public static string EscapeDataString(string catalogId)
private static readonly long[] _nanoseconds = [(long)1e0, (long)1e3, (long)1e6, (long)1e9, (long)60e9, (long)3600e9, (long)86400e9];
private static readonly int[] _quotients = [1000, 1000, 1000, 60, 60, 24, 1];
private static readonly string[] _postFixes = ["ns", "us", "ms", "s", "min", "h", "d"];

// ... except this line
private static readonly Regex _unitStringEvaluator = UnitStringEvaluator();
[GeneratedRegex(@"^\s*([0-9]+)[\s_]*([a-zA-Z]+)\s*$")]
private static partial Regex _unitStringEvaluator { get; }

public static string ToUnitString(this TimeSpan samplePeriod, bool withUnderScore = false)
{
Expand Down Expand Up @@ -334,7 +336,4 @@ private static JsonElement GetJsonObjectFromPath(this JsonElement root, Span<str

return default;
}

[GeneratedRegex(@"^\s*([0-9]+)[\s_]*([a-zA-Z]+)\s*$", RegexOptions.Compiled)]
private static partial Regex UnitStringEvaluator();
}
10 changes: 5 additions & 5 deletions src/Nexus/PackageManagement/PackageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ private async Task<string[]> DiscoverGithubReleasesAsync(CancellationToken cance
.First()
.Split(",")
.Where(current => current.Contains("rel=\"next\""))
.Select(current => GitHubRegex().Match(current).Groups[1].Value)
.Select(current => GitHubRegex.Match(current).Groups[1].Value)
.FirstOrDefault();

if (requestUrl == default)
Expand All @@ -497,7 +497,7 @@ private async Task<string[]> DiscoverGithubReleasesAsync(CancellationToken cance
continue;
}

return [.. result];
return result.ToArray();
}

private async Task<string> RestoreGitHubReleasesAsync(string restoreRoot, CancellationToken cancellationToken)
Expand Down Expand Up @@ -749,7 +749,7 @@ private static async IAsyncEnumerable<JsonElement> GetGitLabPackagesGenericAsync
.First()
.Split(",")
.Where(current => current.Contains("rel=\"next\""))
.Select(current => GitlabRegex().Match(current).Groups[1].Value)
.Select(current => GitlabRegex.Match(current).Groups[1].Value)
.FirstOrDefault();

if (requestUrl == default)
Expand All @@ -760,10 +760,10 @@ private static async IAsyncEnumerable<JsonElement> GetGitLabPackagesGenericAsync
}

[GeneratedRegex("\\<(https:.*)\\>; rel=\"next\"")]
private static partial Regex GitHubRegex();
private static partial Regex GitHubRegex { get; }

[GeneratedRegex("\\<(https:.*)\\>; rel=\"next\"")]
private static partial Regex GitlabRegex();
private static partial Regex GitlabRegex { get; }

#endregion

Expand Down
8 changes: 4 additions & 4 deletions src/Nexus/Utilities/NexusUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static string DefaultBaseUrl

if (aspnetcoreEnvVar is not null)
{
var match = AspNetCoreEnvVarRegex().Match(aspnetcoreEnvVar);
var match = AspNetCoreEnvVarRegex.Match(aspnetcoreEnvVar);

if (match.Success && int.TryParse(match.Groups[1].Value, out var parsedPort))
port = parsedPort;
Expand All @@ -37,6 +37,9 @@ public static string DefaultBaseUrl
}
}

[GeneratedRegex(":([0-9]+)")]
private static partial Regex AspNetCoreEnvVarRegex { get; }

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Scale(TimeSpan value, TimeSpan samplePeriod) => (int)(value.Ticks / samplePeriod.Ticks);

Expand Down Expand Up @@ -151,7 +154,4 @@ public static IEnumerable<T> GetCustomAttributes<T>(this Type type) where T : At
{
return type.GetCustomAttributes(false).OfType<T>();
}

[GeneratedRegex(":([0-9]+)")]
private static partial Regex AspNetCoreEnvVarRegex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ public static string ToPath(this Uri url)
private static readonly string[] _postFixes = ["ns", "us", "ms", "s", "min", "h", "d"];

// ... except these lines
private static readonly Regex _unitStringEvaluator = UnitStringEvaluator();

[GeneratedRegex(@"^([0-9]+)_([a-z]+)$")]
private static partial Regex _unitStringEvaluator { get; }

internal const string NEXUS_KEY = "nexus";

Expand Down Expand Up @@ -215,9 +217,6 @@ internal static TimeSpan ToSamplePeriod(string unitString)
return new TimeSpan(ticks);
}

[GeneratedRegex(@"^([0-9]+)_([a-z]+)$", RegexOptions.Compiled)]
private static partial Regex UnitStringEvaluator();

internal static ResourceCatalog EnsureAndSanitizeMandatoryProperties(
this ResourceCatalog catalog,
int pipelinePosition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ internal static partial class DataModelUtilities
* /a/b/c/T1/600_s_mean(abc=456)#base=1s
*/
// keep in sync with Nexus.UI.Core.Utilities
private static readonly Regex _resourcePathEvaluator = ResourcePathEvaluator();

[GeneratedRegex(@"^(?'catalog'.*)\/(?'resource'.*)\/(?'sample_period'[0-9]+_[a-zA-Z]+)(?:_(?'kind'[^\(#\s]+))?(?:\((?'parameters'.*)\))?(?:#(?'fragment'.*))?$")]
private static partial Regex _resourcePathEvaluator { get; }

private static string ToPascalCase(string input)
{
Expand Down Expand Up @@ -288,7 +290,4 @@ private static void MergeArrays(JsonArray currentArray, JsonElement root1, JsonE
_ => JsonValue.Create(element)
};
}

[GeneratedRegex(@"^(?'catalog'.*)\/(?'resource'.*)\/(?'sample_period'[0-9]+_[a-zA-Z]+)(?:_(?'kind'[^\(#\s]+))?(?:\((?'parameters'.*)\))?(?:#(?'fragment'.*))?$", RegexOptions.Compiled)]
private static partial Regex ResourcePathEvaluator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace Nexus.DataModel;
[DebuggerDisplay("{Id,nq}")]
public partial record Representation
{
private static readonly Regex _snakeCaseEvaluator = SnakeCaseEvaluator();
[GeneratedRegex("(?<=[a-z])([A-Z])")]
private static partial Regex _snakeCaseEvaluator { get; }

private static readonly HashSet<NexusDataType> _nexusDataTypeValues = new(Enum.GetValues<NexusDataType>());

private IReadOnlyDictionary<string, JsonElement>? _parameters;
Expand Down Expand Up @@ -139,7 +141,4 @@ private static void ValidateParameters(IReadOnlyDictionary<string, JsonElement>
throw new Exception("The representation argument identifier is not valid.");
}
}

[GeneratedRegex("(?<=[a-z])([A-Z])", RegexOptions.Compiled)]
private static partial Regex SnakeCaseEvaluator();
}
6 changes: 2 additions & 4 deletions src/extensibility/dotnet-extensibility/DataModel/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public Resource(
/// <summary>
/// Gets a regular expression to validate a resource identifier.
/// </summary>
public static Regex ValidIdExpression { get; } = ValidExpression();
[GeneratedRegex(@"^[a-zA-Z_][a-zA-Z_0-9]*$")]
public static partial Regex ValidIdExpression { get; }

/// <summary>
/// Gets a regular expression to find invalid characters in a resource identifier.
Expand Down Expand Up @@ -126,7 +127,4 @@ private static void ValidateRepresentations(IReadOnlyList<Representation> repres
if (uniqueIds.Count() != representations.Count)
throw new ArgumentException("There are multiple representations with the same identifier.");
}

[GeneratedRegex(@"^[a-zA-Z_][a-zA-Z_0-9]*$")]
private static partial Regex ValidExpression();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public ResourceCatalog(
/// <summary>
/// Gets a regular expression to validate a resource catalog identifier.
/// </summary>
public static Regex ValidIdExpression { get; } = ValidIdExpressionRegex();
[GeneratedRegex(@"^(?:\/[a-zA-Z_][a-zA-Z_0-9]*)+$")]
public static partial Regex ValidIdExpression { get; }

private static Regex _matchSingleParametersExpression { get; } = new Regex(@"\s*(.+?)\s*=\s*([^,\)]+)\s*,?", RegexOptions.Compiled);

Expand Down Expand Up @@ -203,7 +204,4 @@ private static void ValidateResources(IReadOnlyList<Resource> resources)
if (uniqueIds.Count() != resources.Count)
throw new ArgumentException("There are multiple resources with the same identifier.");
}

[GeneratedRegex(@"^(?:\/[a-zA-Z_][a-zA-Z_0-9]*)+$", RegexOptions.Compiled)]
private static partial Regex ValidIdExpressionRegex();
}

0 comments on commit 11b0106

Please sign in to comment.