Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions PolyShim.Tests/Net50/ConvertTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net50;

public class ConvertTests
{
[Fact]
public void FromHexString_Test()
{
// Act & assert
Convert.FromHexString("4A6F686E").Should().Equal("John"u8.ToArray());
Convert.FromHexString("").Should().BeEmpty();
Assert.Throws<FormatException>(() => Convert.FromHexString("4A6F686")); // Odd length
Assert.Throws<FormatException>(() => Convert.FromHexString("4A6F68GH")); // Invalid character
}

[Fact]
public void ToHexString_Test()
{
// Act & assert
Convert.ToHexString("John"u8.ToArray()).Should().Be("4A6F686E");
Convert.ToHexString(Array.Empty<byte>()).Should().Be("");
Convert.ToHexString("John"u8.ToArray(), 1, 2).Should().Be("6F68");
}
}
17 changes: 17 additions & 0 deletions PolyShim.Tests/Net90/ConvertTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using FluentAssertions;
using Xunit;

namespace PolyShim.Tests.Net90;

public class ConvertTests
{
[Fact]
public void ToHexStringLower_Test()
{
// Act & assert
Convert.ToHexStringLower("John"u8.ToArray()).Should().Be("4a6f686e");
Convert.ToHexStringLower(Array.Empty<byte>()).Should().Be("");
Convert.ToHexStringLower("John"u8.ToArray(), 1, 2).Should().Be("6f68");
}
}
66 changes: 66 additions & 0 deletions PolyShim/Net50/Convert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#if (NETCOREAPP && !NET5_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System;

internal static partial class PolyfillExtensions
{
extension(Convert)
{
// https://learn.microsoft.com/dotnet/api/system.convert.fromhexstring#system-convert-fromhexstring(system-string)
public static byte[] FromHexString(string s)
{
static int GetHexValue(char c)
{
if (c is >= '0' and <= '9')
return c - '0';
if (c is >= 'A' and <= 'F')
return c - 'A' + 10;
if (c is >= 'a' and <= 'f')
return c - 'a' + 10;

throw new FormatException($"Invalid hex character: {c}");
}

if (s.Length % 2 != 0)
throw new FormatException("The hex string must have an even length.");

var byteCount = s.Length / 2;
var bytes = new byte[byteCount];

for (var i = 0; i < byteCount; i++)
{
var highNibble = s[i * 2];
var lowNibble = s[i * 2 + 1];

bytes[i] = (byte)((GetHexValue(highNibble) << 4) + GetHexValue(lowNibble));
}

return bytes;
}

// https://learn.microsoft.com/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte()-system-int32-system-int32)
public static string ToHexString(byte[] value, int startIndex, int length)
{
var c = new char[length * 2];

for (var i = 0; i < length; i++)
{
var b = value[startIndex + i] >> 4;
c[i * 2] = (char)(55 + b + (((b - 10) >> 31) & -7));
b = value[startIndex + i] & 0xF;
c[i * 2 + 1] = (char)(55 + b + (((b - 10) >> 31) & -7));
}

return new string(c);
}

// https://learn.microsoft.com/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte())
public static string ToHexString(byte[] value) => ToHexString(value, 0, value.Length);
}
}
#endif
23 changes: 23 additions & 0 deletions PolyShim/Net90/Convert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#if (NETCOREAPP && !NET9_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD)
#nullable enable
// ReSharper disable RedundantUsingDirective
// ReSharper disable CheckNamespace
// ReSharper disable InconsistentNaming
// ReSharper disable PartialTypeWithSinglePart

using System;

internal static partial class PolyfillExtensions
{
extension(Convert)
{
// https://learn.microsoft.com/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte()-system-int32-system-int32)
public static string ToHexStringLower(byte[] value, int startIndex, int length) =>
Convert.ToHexString(value, startIndex, length).ToLowerInvariant();

// https://learn.microsoft.com/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte())
public static string ToHexStringLower(byte[] value) =>
ToHexStringLower(value, 0, value.Length);
}
}
#endif
10 changes: 8 additions & 2 deletions PolyShim/Signatures.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Signatures

- **Total:** 259
- **Total:** 264
- **Types:** 62
- **Members:** 197
- **Members:** 202

___

Expand All @@ -26,6 +26,12 @@ ___
- [`Task CancelAsync()`](https://learn.microsoft.com/dotnet/api/system.threading.cancellationtokensource.cancelasync) <sup><sub>.NET 8.0</sub></sup>
- `CompilerFeatureRequiredAttribute`
- [**[class]**](https://learn.microsoft.com/dotnet/api/system.runtime.compilerservices.compilerfeaturerequiredattribute) <sup><sub>.NET 7.0</sub></sup>
- `Convert`
- [`byte[] FromHexString(string)`](https://learn.microsoft.com/dotnet/api/system.convert.fromhexstring#system-convert-fromhexstring(system-string)) <sup><sub>.NET 5.0</sub></sup>
- [`string ToHexString(byte[], int, int)`](https://learn.microsoft.com/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte()-system-int32-system-int32)) <sup><sub>.NET 5.0</sub></sup>
- [`string ToHexString(byte[])`](https://learn.microsoft.com/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte())) <sup><sub>.NET 5.0</sub></sup>
- [`string ToHexStringLower(byte[], int, int)`](https://learn.microsoft.com/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte()-system-int32-system-int32)) <sup><sub>.NET 9.0</sub></sup>
- [`string ToHexStringLower(byte[])`](https://learn.microsoft.com/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte())) <sup><sub>.NET 9.0</sub></sup>
- `DateTime`
- [`DateTime UnixEpoch`](https://learn.microsoft.com/dotnet/api/system.datetime.unixepoch) <sup><sub>.NET Core 2.1</sub></sup>
- `DateTimeOffset`
Expand Down
Loading