Using native Convert from hex function for .NET 8
This commit is contained in:
@@ -22,6 +22,8 @@ namespace System
|
||||
public static class StringExtensions
|
||||
#endif
|
||||
{
|
||||
private const string EmailRegex = @"^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$";
|
||||
|
||||
/// <summary>
|
||||
/// Converts a hex string into a byte array.
|
||||
/// </summary>
|
||||
@@ -30,23 +32,28 @@ namespace System
|
||||
/// <returns>The bytes.</returns>
|
||||
public static IEnumerable<byte> HexToBytes(this string hexString, string delimiter = "")
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(hexString))
|
||||
yield break;
|
||||
if (hexString == null)
|
||||
throw new ArgumentNullException(nameof(hexString));
|
||||
|
||||
string str = string.IsNullOrEmpty(delimiter) ? hexString : hexString.Replace(delimiter, "");
|
||||
if (str.Length % 2 == 1)
|
||||
yield break;
|
||||
string str = string.IsNullOrEmpty(delimiter)
|
||||
? hexString
|
||||
: hexString.Replace(delimiter, "");
|
||||
|
||||
#if NET8_0_OR_GREATER
|
||||
if (InvalidHexCharRegex().IsMatch(str))
|
||||
yield break;
|
||||
return Convert.FromHexString(str);
|
||||
#else
|
||||
if (Regex.IsMatch(str, "[^0-9a-fA-F]", RegexOptions.Compiled))
|
||||
yield break;
|
||||
#endif
|
||||
throw new FormatException("The input is not a valid hex string as it contains a non-hex character.");
|
||||
|
||||
for (int i = 0; i < str.Length; i += 2)
|
||||
yield return Convert.ToByte(str.Substring(i, 2), 16);
|
||||
if (str.Length % 2 != 0)
|
||||
throw new FormatException("The input is not a valid hex string as its length is not a multiple of 2.");
|
||||
|
||||
byte[] bytes = new byte[str.Length / 2];
|
||||
for (int i = 0; i < bytes.Length; i++)
|
||||
bytes[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
|
||||
|
||||
return bytes;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -193,8 +200,7 @@ namespace System
|
||||
if (!ValidEmailRegex().IsMatch(emailAddress))
|
||||
return false;
|
||||
#else
|
||||
string emailRegex = @"^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$";
|
||||
if (!Regex.IsMatch(emailAddress, emailRegex, RegexOptions.Compiled))
|
||||
if (!Regex.IsMatch(emailAddress, EmailRegex, RegexOptions.Compiled))
|
||||
return false;
|
||||
#endif
|
||||
|
||||
@@ -248,10 +254,7 @@ namespace System
|
||||
=> sb.Append(value).Append(newLine);
|
||||
|
||||
#if NET8_0_OR_GREATER
|
||||
[GeneratedRegex("[^0-9a-fA-F]", RegexOptions.Compiled)]
|
||||
private static partial Regex InvalidHexCharRegex();
|
||||
|
||||
[GeneratedRegex(@"^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$", RegexOptions.Compiled)]
|
||||
[GeneratedRegex(EmailRegex, RegexOptions.Compiled)]
|
||||
private static partial Regex ValidEmailRegex();
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AMWD.NetRevisionTask" Version="1.2.0">
|
||||
<PackageReference Include="AMWD.NetRevisionTask" Version="1.2.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
|
||||
Reference in New Issue
Block a user