Skip to content

Commit 0739be5

Browse files
authored
Add ConvertPolyfill (#325)
1 parent bc66819 commit 0739be5

File tree

6 files changed

+441
-3
lines changed

6 files changed

+441
-3
lines changed

apiCount.include.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
**API count: 558**
1+
**API count: 568**

api_list.include.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,20 @@
532532
* `bool TryParse(string?, IFormatProvider?, byte)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryparse#system-byte-tryparse(system-string-system-iformatprovider-system-byte@))
533533

534534

535+
#### ConvertPolyfill
536+
537+
* `byte[] FromHexString(ReadOnlySpan<char>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.fromhexstring#system-convert-fromhexstring(system-readonlyspan((system-char))))
538+
* `byte[] FromHexString(string)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.fromhexstring#system-convert-fromhexstring(system-string))
539+
* `string ToHexString(byte[], int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte()-system-int32-system-int32))
540+
* `string ToHexString(byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte()))
541+
* `string ToHexString(ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-readonlyspan((system-byte))))
542+
* `string ToHexStringLower(byte[], int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte()-system-int32-system-int32))
543+
* `string ToHexStringLower(byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte()))
544+
* `string ToHexStringLower(ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-readonlyspan((system-byte))))
545+
* `bool TryToHexString(ReadOnlySpan<byte>, Span<char>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.trytohexstring)
546+
* `bool TryToHexStringLower(ReadOnlySpan<byte>, Span<char>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.trytohexstringlower)
547+
548+
535549
#### DateTimeOffsetPolyfill
536550

537551
* `bool TryParse(ReadOnlySpan<char>, DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-datetimeoffset@))

readme.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The package targets `netstandard2.0` and is designed to support the following ru
1212
* `net5.0`, `net6.0`, `net7.0`, `net8.0`, `net9.0`, `net10.0`
1313

1414

15-
**API count: 558**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
15+
**API count: 568**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
1616

1717

1818
**See [Milestones](../../milestones?state=closed) for release notes.**
@@ -1001,6 +1001,20 @@ The class `Polyfill` includes the following extension methods:
10011001
* `bool TryParse(string?, IFormatProvider?, byte)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryparse#system-byte-tryparse(system-string-system-iformatprovider-system-byte@))
10021002

10031003

1004+
#### ConvertPolyfill
1005+
1006+
* `byte[] FromHexString(ReadOnlySpan<char>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.fromhexstring#system-convert-fromhexstring(system-readonlyspan((system-char))))
1007+
* `byte[] FromHexString(string)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.fromhexstring#system-convert-fromhexstring(system-string))
1008+
* `string ToHexString(byte[], int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte()-system-int32-system-int32))
1009+
* `string ToHexString(byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte()))
1010+
* `string ToHexString(ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-readonlyspan((system-byte))))
1011+
* `string ToHexStringLower(byte[], int, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte()-system-int32-system-int32))
1012+
* `string ToHexStringLower(byte[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte()))
1013+
* `string ToHexStringLower(ReadOnlySpan<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-readonlyspan((system-byte))))
1014+
* `bool TryToHexString(ReadOnlySpan<byte>, Span<char>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.trytohexstring)
1015+
* `bool TryToHexStringLower(ReadOnlySpan<byte>, Span<char>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.convert.trytohexstringlower)
1016+
1017+
10041018
#### DateTimeOffsetPolyfill
10051019

10061020
* `bool TryParse(ReadOnlySpan<char>, DateTimeOffset)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.datetimeoffset.tryparse#system-datetimeoffset-tryparse(system-readonlyspan((system-char))-system-datetimeoffset@))

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;NETSDK1138;NU1901;NU1902;NU1903;CA1822;CA1847;CA1861;NU1510;NU1608;NU1109</NoWarn>
5-
<Version>7.27.0</Version>
5+
<Version>7.28.0</Version>
66
<AssemblyVersion>1.0.0</AssemblyVersion>
77
<PackageTags>Polyfill</PackageTags>
88
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>

src/Polyfill/ConvertPolyfill.cs

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
// <auto-generated />
2+
3+
#pragma warning disable
4+
5+
namespace Polyfills;
6+
7+
using System;
8+
using System.Text;
9+
using System.ComponentModel;
10+
using System.Collections.Generic;
11+
using System.Diagnostics;
12+
using System.Diagnostics.CodeAnalysis;
13+
14+
[ExcludeFromCodeCoverage]
15+
[DebuggerNonUserCode]
16+
#if PolyPublic
17+
public
18+
#endif
19+
static partial class ConvertPolyfill
20+
{
21+
/// <summary>
22+
/// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with uppercase hex characters.
23+
/// Parameters specify the subset as an offset in the input array and the number of elements in the array to convert.
24+
/// </summary>
25+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte()-system-int32-system-int32)
26+
public static string ToHexString(byte[] inArray, int offset, int length)
27+
#if NET5_0_OR_GREATER
28+
=> Convert.ToHexString(inArray, offset, length);
29+
#else
30+
{
31+
if (length < 0)
32+
throw new ArgumentOutOfRangeException(nameof(length));
33+
if (offset < 0)
34+
throw new ArgumentOutOfRangeException(nameof(offset));
35+
if (offset > (inArray.Length - length))
36+
throw new ArgumentOutOfRangeException(nameof(offset));
37+
38+
var builder = new StringBuilder();
39+
40+
var end = length + offset;
41+
for (int i = offset; i < end; i++)
42+
{
43+
var item = inArray[i];
44+
builder.Append(item.ToString("X2"));
45+
}
46+
47+
return builder.ToString();
48+
}
49+
#endif
50+
51+
/// <summary>
52+
/// Converts a subset of an array of 8-bit unsigned integers to its equivalent string representation that is encoded with lowercase hex characters.
53+
/// Parameters specify the subset as an offset in the input array and the number of elements in the array to convert.
54+
/// </summary>
55+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte()-system-int32-system-int32)
56+
public static string ToHexStringLower(byte[] inArray, int offset, int length)
57+
#if NET9_0_OR_GREATER
58+
=> Convert.ToHexStringLower(inArray, offset, length);
59+
#else
60+
{
61+
if (length < 0)
62+
throw new ArgumentOutOfRangeException(nameof(length));
63+
if (offset < 0)
64+
throw new ArgumentOutOfRangeException(nameof(offset));
65+
if (offset > (inArray.Length - length))
66+
throw new ArgumentOutOfRangeException(nameof(offset));
67+
68+
var builder = new StringBuilder();
69+
70+
var end = length + offset;
71+
for (int i = offset; i < end; i++)
72+
{
73+
var item = inArray[i];
74+
builder.Append(item.ToString("x2"));
75+
}
76+
77+
return builder.ToString();
78+
}
79+
#endif
80+
81+
/// <summary>
82+
/// Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with uppercase hex characters.
83+
/// </summary>
84+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-byte())
85+
public static string ToHexString(byte[] inArray) =>
86+
#if NET5_0_OR_GREATER
87+
Convert.ToHexString(inArray);
88+
#else
89+
ConvertPolyfill.ToHexString(inArray, 0, inArray.Length);
90+
#endif
91+
92+
/// <summary>
93+
/// Converts an array of 8-bit unsigned integers to its equivalent string representation that is encoded with lowercase hex characters.
94+
/// </summary>
95+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-byte())
96+
public static string ToHexStringLower(byte[] inArray) =>
97+
#if NET9_0_OR_GREATER
98+
Convert.ToHexStringLower(inArray);
99+
#else
100+
ConvertPolyfill.ToHexStringLower(inArray, 0, inArray.Length);
101+
#endif
102+
103+
#if FeatureMemory
104+
/// <summary>
105+
/// Converts a span of 8-bit unsigned integers to its equivalent string representation that is encoded with uppercase hex characters.
106+
/// </summary>
107+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstring#system-convert-tohexstring(system-readonlyspan((system-byte)))
108+
public static string ToHexString(ReadOnlySpan<byte> bytes) =>
109+
#if NET5_0_OR_GREATER
110+
Convert.ToHexString(bytes);
111+
#else
112+
ConvertPolyfill.ToHexString(bytes.ToArray());
113+
#endif
114+
115+
/// <summary>
116+
/// Converts a span of 8-bit unsigned integers to its equivalent string representation that is encoded with lowercase hex characters.
117+
/// </summary>
118+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.tohexstringlower#system-convert-tohexstringlower(system-readonlyspan((system-byte)))
119+
public static string ToHexStringLower(ReadOnlySpan<byte> bytes) =>
120+
#if NET9_0_OR_GREATER
121+
Convert.ToHexStringLower(bytes);
122+
#else
123+
ConvertPolyfill.ToHexStringLower(bytes.ToArray());
124+
#endif
125+
126+
/// <summary>
127+
/// Converts a span of 8-bit unsigned integers to its equivalent span representation that is encoded with uppercase hex characters.
128+
/// </summary>
129+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.trytohexstring
130+
public static bool TryToHexString(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten)
131+
#if NET9_0_OR_GREATER
132+
=> Convert.TryToHexString(source, destination, out charsWritten);
133+
#else
134+
{
135+
var hexString = ConvertPolyfill.ToHexString(source);
136+
if (hexString.TryCopyTo(destination))
137+
{
138+
charsWritten = hexString.Length;
139+
return true;
140+
}
141+
142+
charsWritten = 0;
143+
return false;
144+
}
145+
#endif
146+
147+
/// <summary>
148+
/// Converts a span of 8-bit unsigned integers to its equivalent span representation that is encoded with lowercase hex characters.
149+
/// </summary>
150+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.trytohexstringlower
151+
public static bool TryToHexStringLower(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten)
152+
#if NET9_0_OR_GREATER
153+
=> Convert.TryToHexStringLower(source, destination, out charsWritten);
154+
#else
155+
{
156+
var hexString = ConvertPolyfill.ToHexStringLower(source);
157+
if (hexString.TryCopyTo(destination))
158+
{
159+
charsWritten = hexString.Length;
160+
return true;
161+
}
162+
163+
charsWritten = 0;
164+
return false;
165+
}
166+
#endif
167+
168+
169+
#endif
170+
171+
/// <summary>
172+
/// Converts the specified string, which encodes binary data as hex characters, to an equivalent 8-bit unsigned integer array.
173+
/// </summary>
174+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.fromhexstring#system-convert-fromhexstring(system-string)
175+
public static byte[] FromHexString(string hexString)
176+
#if NET5_0_OR_GREATER
177+
=> Convert.FromHexString(hexString);
178+
#else
179+
{
180+
if (hexString.Length % 2 != 0)
181+
throw new FormatException("Hex string must have an even length.");
182+
183+
var result = new byte[hexString.Length / 2];
184+
185+
for (int i = 0; i < result.Length; i++)
186+
{
187+
result[i] = (byte) ((GetHexValue(hexString[i * 2]) << 4) + GetHexValue(hexString[i * 2 + 1]));
188+
}
189+
190+
return result;
191+
192+
static int GetHexValue(char hex)
193+
{
194+
return hex switch
195+
{
196+
>= '0' and <= '9' => hex - '0',
197+
>= 'A' and <= 'F' => hex - 'A' + 10,
198+
>= 'a' and <= 'f' => hex - 'a' + 10,
199+
_ => throw new FormatException($"Invalid hex character: {hex}")
200+
};
201+
}
202+
}
203+
#endif
204+
205+
#if FeatureMemory
206+
/// <summary>
207+
/// Converts the span, which encodes binary data as hex characters, to an equivalent 8-bit unsigned integer array.
208+
/// </summary>
209+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.convert.fromhexstring#system-convert-fromhexstring(system-readonlyspan((system-char)))
210+
public static byte[] FromHexString(ReadOnlySpan<char> chars)
211+
#if NET5_0_OR_GREATER
212+
=> Convert.FromHexString(chars);
213+
#else
214+
=> ConvertPolyfill.FromHexString(chars.ToString());
215+
#endif
216+
#endif
217+
}

0 commit comments

Comments
 (0)