From c2af91d9c0c3a084d34c49886e4f1af846dc2e6b Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Wed, 5 Jun 2024 15:57:19 +0300 Subject: [PATCH] Use RegEx source generators on .NET 7 or later (#269) --- Directory.Build.props | 1 + .../Airtel/AirtelPhoneNumberAttribute.cs | 9 +++------ .../Airtel/AirtelPhoneNumberValidator.cs | 13 ++++++++++--- .../Safaricom/SafaricomPhoneNumberAttribute.cs | 9 +++------ .../Safaricom/SafaricomPhoneNumberValidator.cs | 13 ++++++++++--- .../Telkom/TelkomPhoneNumberAttribute.cs | 9 +++------ .../Telkom/TelkomPhoneNumberValidator.cs | 13 ++++++++++--- .../MongoDbFixture.cs | 3 +-- 8 files changed, 41 insertions(+), 29 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index cb2607c1..cabb09d4 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -10,6 +10,7 @@ $(WaringsAsErrors),IL2026,IL3050 + $(WarningsAsErrors),SYSLIB1045 diff --git a/src/Tingle.Extensions.PhoneValidators/Airtel/AirtelPhoneNumberAttribute.cs b/src/Tingle.Extensions.PhoneValidators/Airtel/AirtelPhoneNumberAttribute.cs index 4c67e21f..bee77a90 100644 --- a/src/Tingle.Extensions.PhoneValidators/Airtel/AirtelPhoneNumberAttribute.cs +++ b/src/Tingle.Extensions.PhoneValidators/Airtel/AirtelPhoneNumberAttribute.cs @@ -1,5 +1,4 @@ -using System.Text.RegularExpressions; -using Tingle.Extensions.PhoneValidators.Airtel; +using Tingle.Extensions.PhoneValidators.Airtel; namespace System.ComponentModel.DataAnnotations; @@ -9,8 +8,6 @@ namespace System.ComponentModel.DataAnnotations; [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] public sealed class AirtelPhoneNumberAttribute : ValidationAttribute { - private static readonly Regex regex = new(AirtelPhoneNumberValidator.RegExComplete); - /// /// Initializes a new instance of the class. /// @@ -19,6 +16,8 @@ public AirtelPhoneNumberAttribute() : base("The field {0} must be a valid Airtel /// public override bool IsValid(object? value) { + static bool IsValidByRegEx(string value) => AirtelPhoneNumberValidator.Expression.IsMatch(value); + if (value is string s && !string.IsNullOrEmpty(s)) return IsValidByRegEx(s); if (value is IEnumerable values) @@ -32,6 +31,4 @@ public override bool IsValid(object? value) return true; } - - private bool IsValidByRegEx(string value) => regex.IsMatch(value); } diff --git a/src/Tingle.Extensions.PhoneValidators/Airtel/AirtelPhoneNumberValidator.cs b/src/Tingle.Extensions.PhoneValidators/Airtel/AirtelPhoneNumberValidator.cs index f23cce5e..87dce500 100644 --- a/src/Tingle.Extensions.PhoneValidators/Airtel/AirtelPhoneNumberValidator.cs +++ b/src/Tingle.Extensions.PhoneValidators/Airtel/AirtelPhoneNumberValidator.cs @@ -6,7 +6,7 @@ namespace Tingle.Extensions.PhoneValidators.Airtel; /// /// Implementation of specifically for Airtel phone numbers /// -public class AirtelPhoneNumberValidator : AbstractPhoneNumberValidator +public partial class AirtelPhoneNumberValidator : AbstractPhoneNumberValidator { // This regular expression will match numbers with known formats. // The intention is to ensure the line number (after country code or local code) are actually standard. @@ -16,12 +16,19 @@ public class AirtelPhoneNumberValidator : AbstractPhoneNumberValidator // The digits are 30-39, 50-56, 85-89 when prefixed with 7 and 00-02 when prefixed with 1 internal const string RegExComplete = @"^(?:254|\+254|0)?((?:(?:7(?:(?:3[0-9])|(?:5[0-6])|(8[5-9])))|(?:1(?:[0][0-2])))[0-9]{6})$"; - private static readonly Regex regex = new(@RegExComplete); + internal static readonly Regex Expression = GetExpression(); /// /// Creates an instance of /// public AirtelPhoneNumberValidator() : base() { } - internal override Regex RegularExpression => regex; + internal override Regex RegularExpression => Expression; + +#if NET7_0_OR_GREATER + [GeneratedRegex(RegExComplete)] + private static partial Regex GetExpression(); +#else + private static Regex GetExpression() => new(RegExComplete); +#endif } diff --git a/src/Tingle.Extensions.PhoneValidators/Safaricom/SafaricomPhoneNumberAttribute.cs b/src/Tingle.Extensions.PhoneValidators/Safaricom/SafaricomPhoneNumberAttribute.cs index 09a4557b..8118ad79 100644 --- a/src/Tingle.Extensions.PhoneValidators/Safaricom/SafaricomPhoneNumberAttribute.cs +++ b/src/Tingle.Extensions.PhoneValidators/Safaricom/SafaricomPhoneNumberAttribute.cs @@ -1,5 +1,4 @@ -using System.Text.RegularExpressions; -using Tingle.Extensions.PhoneValidators.Safaricom; +using Tingle.Extensions.PhoneValidators.Safaricom; namespace System.ComponentModel.DataAnnotations; @@ -9,8 +8,6 @@ namespace System.ComponentModel.DataAnnotations; [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] public sealed class SafaricomPhoneNumberAttribute : ValidationAttribute { - private static readonly Regex regex = new(SafaricomPhoneNumberValidator.RegExComplete); - /// /// Initializes a new instance of the class. /// @@ -19,6 +16,8 @@ public SafaricomPhoneNumberAttribute() : base("The field {0} must be a valid Saf /// public override bool IsValid(object? value) { + static bool IsValidByRegEx(string value) => SafaricomPhoneNumberValidator.Expression.IsMatch(value); + if (value is string s && !string.IsNullOrEmpty(s)) return IsValidByRegEx(s); if (value is IEnumerable values) @@ -32,6 +31,4 @@ public override bool IsValid(object? value) return true; } - - private bool IsValidByRegEx(string value) => regex.IsMatch(value); } diff --git a/src/Tingle.Extensions.PhoneValidators/Safaricom/SafaricomPhoneNumberValidator.cs b/src/Tingle.Extensions.PhoneValidators/Safaricom/SafaricomPhoneNumberValidator.cs index aa1f6bdb..94a13eb1 100644 --- a/src/Tingle.Extensions.PhoneValidators/Safaricom/SafaricomPhoneNumberValidator.cs +++ b/src/Tingle.Extensions.PhoneValidators/Safaricom/SafaricomPhoneNumberValidator.cs @@ -6,7 +6,7 @@ namespace Tingle.Extensions.PhoneValidators.Safaricom; /// /// Implementation of specifically for Safaricom phone numbers /// -public class SafaricomPhoneNumberValidator : AbstractPhoneNumberValidator +public partial class SafaricomPhoneNumberValidator : AbstractPhoneNumberValidator { // This regular expression will match numbers with known formats. // The intention is to ensure the line number (after country code or local code) are actually standard. @@ -16,12 +16,19 @@ public class SafaricomPhoneNumberValidator : AbstractPhoneNumberValidator // The digits are 00-09, 10-19, 20-29, 40-49, 90-99, 57-59, 68-69 when prefixed with 7 and 10-15 when prefixed with 1 internal const string RegExComplete = @"^(?:254|\+254|0)?((?:(?:7(?:(?:[01249][0-9])|(?:5[789])|(?:6[89])))|(?:1(?:[1][0-5])))[0-9]{6})$"; - private static readonly Regex regex = new(@RegExComplete); + internal static readonly Regex Expression = GetExpression(); /// /// Creates a instance of /// public SafaricomPhoneNumberValidator() : base() { } - internal override Regex RegularExpression => regex; + internal override Regex RegularExpression => Expression; + +#if NET7_0_OR_GREATER + [GeneratedRegex(RegExComplete)] + private static partial Regex GetExpression(); +#else + private static Regex GetExpression() => new(RegExComplete); +#endif } diff --git a/src/Tingle.Extensions.PhoneValidators/Telkom/TelkomPhoneNumberAttribute.cs b/src/Tingle.Extensions.PhoneValidators/Telkom/TelkomPhoneNumberAttribute.cs index 4b69fee4..a7733ce9 100644 --- a/src/Tingle.Extensions.PhoneValidators/Telkom/TelkomPhoneNumberAttribute.cs +++ b/src/Tingle.Extensions.PhoneValidators/Telkom/TelkomPhoneNumberAttribute.cs @@ -1,5 +1,4 @@ -using System.Text.RegularExpressions; -using Tingle.Extensions.PhoneValidators.Telkom; +using Tingle.Extensions.PhoneValidators.Telkom; namespace System.ComponentModel.DataAnnotations; @@ -9,8 +8,6 @@ namespace System.ComponentModel.DataAnnotations; [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)] public sealed class TelkomPhoneNumberAttribute : ValidationAttribute { - private static readonly Regex regex = new(TelkomPhoneNumberValidator.RegExComplete); - /// /// Initializes a new instance of the class. /// @@ -19,6 +16,8 @@ public TelkomPhoneNumberAttribute() : base("The field {0} must be a valid Telkom /// public override bool IsValid(object? value) { + static bool IsValidByRegEx(string value) => TelkomPhoneNumberValidator.Expression.IsMatch(value); + if (value is string s && !string.IsNullOrEmpty(s)) return IsValidByRegEx(s); if (value is IEnumerable values) @@ -32,6 +31,4 @@ public override bool IsValid(object? value) return true; } - - private bool IsValidByRegEx(string value) => regex.IsMatch(value); } diff --git a/src/Tingle.Extensions.PhoneValidators/Telkom/TelkomPhoneNumberValidator.cs b/src/Tingle.Extensions.PhoneValidators/Telkom/TelkomPhoneNumberValidator.cs index 60f19d90..a74c3422 100644 --- a/src/Tingle.Extensions.PhoneValidators/Telkom/TelkomPhoneNumberValidator.cs +++ b/src/Tingle.Extensions.PhoneValidators/Telkom/TelkomPhoneNumberValidator.cs @@ -6,7 +6,7 @@ namespace Tingle.Extensions.PhoneValidators.Telkom; /// /// Implementation of specifically for Telkom phone numbers /// -public class TelkomPhoneNumberValidator : AbstractPhoneNumberValidator +public partial class TelkomPhoneNumberValidator : AbstractPhoneNumberValidator { // This regular expression will match numbers with known formats. // The intention is to ensure the line number (after country code or local code) are actually standard. @@ -16,12 +16,19 @@ public class TelkomPhoneNumberValidator : AbstractPhoneNumberValidator // The digits are 70-79 when prefixed with 7 internal const string RegExComplete = @"^(?:254|\+254|0)?(7(?:(?:7[0-9]))[0-9]{6})$"; - private static readonly Regex regex = new(@RegExComplete); + internal static readonly Regex Expression = GetExpression(); /// /// Creates an instance of /// public TelkomPhoneNumberValidator() : base() { } - internal override Regex RegularExpression => regex; + internal override Regex RegularExpression => Expression; + +#if NET7_0_OR_GREATER + [GeneratedRegex(RegExComplete)] + private static partial Regex GetExpression(); +#else + private static Regex GetExpression() => new(RegExComplete); +#endif } diff --git a/tests/Tingle.Extensions.Caching.MongoDB.Tests/MongoDbFixture.cs b/tests/Tingle.Extensions.Caching.MongoDB.Tests/MongoDbFixture.cs index a4949c25..a8086b6e 100644 --- a/tests/Tingle.Extensions.Caching.MongoDB.Tests/MongoDbFixture.cs +++ b/tests/Tingle.Extensions.Caching.MongoDB.Tests/MongoDbFixture.cs @@ -1,5 +1,4 @@ using MongoDB.Driver; -using System.Text.RegularExpressions; namespace Tingle.Extensions.Caching.MongoDB.Tests; @@ -7,7 +6,7 @@ public sealed class MongoDbFixture : IDisposable { public MongoDbFixture() { - var dbName = Regex.Replace(Guid.NewGuid().ToString(), "[^a-zA-Z0-9]", ""); + var dbName = Guid.NewGuid().ToString("n"); var mub = new MongoUrlBuilder() { Server = MongoServerAddress.Parse("localhost:27017"),