diff --git a/apiCount.include.md b/apiCount.include.md index fdcbb52..00492ad 100644 --- a/apiCount.include.md +++ b/apiCount.include.md @@ -1 +1 @@ -**API count: 415** \ No newline at end of file +**API count: 416** \ No newline at end of file diff --git a/api_list.include.md b/api_list.include.md index 0e4af3a..a9ec1fd 100644 --- a/api_list.include.md +++ b/api_list.include.md @@ -76,7 +76,8 @@ #### Encoding - * `int GetBytes(Encoding, ReadOnlySpan, Span)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytes?#system-text-encoding-getbytes(system-readonlyspan((system-char))-system-span((system-byte)))) + * `int GetByteCount(Encoding, ReadOnlySpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytecount#system-text-encoding-getbytecount(system-readonlyspan((system-char)))) + * `int GetBytes(Encoding, ReadOnlySpan, Span)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytes#system-text-encoding-getbytes(system-readonlyspan((system-char))-system-span((system-byte)))) * `string GetString(Encoding, ReadOnlySpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getstring#system-text-encoding-getstring(system-readonlyspan((system-byte)))) diff --git a/readme.md b/readme.md index 70212d6..b0360ea 100644 --- a/readme.md +++ b/readme.md @@ -1212,7 +1212,7 @@ Enable by adding and MSBuild property `PolyGuard` * `ArgumentException.ThrowIfNullOrEmpty` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorempty) * `ArgumentException.ThrowIfNullOrWhiteSpace` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorwhitespace) - * `ArgumentNullException.ThrowIfNull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.argumentnullexception.throwifnull?view=net-8.0) + * `ArgumentNullException.ThrowIfNull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.argumentnullexception.throwifnull) With the equivalent Guard APIs: diff --git a/src/Polyfill/Polyfill_Encoding.cs b/src/Polyfill/Polyfill_Encoding.cs index 3ce95ed..d0d9a0e 100644 --- a/src/Polyfill/Polyfill_Encoding.cs +++ b/src/Polyfill/Polyfill_Encoding.cs @@ -5,7 +5,7 @@ #pragma warning disable -#if FeatureMemory && AllowUnsafeBlocks && !NETCOREAPP2_1_OR_GREATER +#if FeatureMemory namespace Polyfills; @@ -13,22 +13,35 @@ namespace Polyfills; static partial class Polyfill { +#if NETCOREAPP2_0 || NETFRAMEWORK || NETSTANDARD2_0 + /// + /// When overridden in a derived class, calculates the number of bytes produced by encoding the characters in the specified character span. + /// + /// + /// The span of characters to encode. + /// The number of bytes produced by encoding the specified character span. + //Link: https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytecount#system-text-encoding-getbytecount(system-readonlyspan((system-char))) + public static int GetByteCount(this Encoding target, ReadOnlySpan chars) => + target.GetByteCount(chars.ToArray()); +#endif + +#if AllowUnsafeBlocks && !NETCOREAPP2_1_OR_GREATER /// When overridden in a derived class, encodes into a span of bytes a set of characters from the specified read-only span. /// The span containing the set of characters to encode. /// The byte span to hold the encoded bytes. /// The number of encoded bytes. - //Link: https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytes?#system-text-encoding-getbytes(system-readonlyspan((system-char))-system-span((system-byte))) - public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan chars, Span bytes) + //Link: https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytes#system-text-encoding-getbytes(system-readonlyspan((system-char))-system-span((system-byte))) + public static unsafe int GetBytes(this Encoding target, ReadOnlySpan chars, Span bytes) { - if (encoding is null) + if (target is null) { - throw new ArgumentNullException(nameof(encoding)); + throw new ArgumentNullException(nameof(target)); } fixed (char* charsPtr = chars) fixed (byte* bytesPtr = bytes) { - return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length); + return target.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length); } } @@ -36,18 +49,19 @@ public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan cha /// A read-only byte span to decode to a Unicode string. /// A string that contains the decoded bytes from the provided read-only span. //Link: https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getstring#system-text-encoding-getstring(system-readonlyspan((system-byte))) - public static unsafe string GetString(this Encoding encoding, ReadOnlySpan bytes) + public static unsafe string GetString(this Encoding target, ReadOnlySpan bytes) { - if (encoding is null) + if (target is null) { - throw new ArgumentNullException(nameof(encoding)); + throw new ArgumentNullException(nameof(target)); } fixed (byte* bytesPtr = bytes) { - return encoding.GetString(bytesPtr, bytes.Length); + return target.GetString(bytesPtr, bytes.Length); } } +#endif } #endif diff --git a/src/Tests/PolyfillTests_Encoding.cs b/src/Tests/PolyfillTests_Encoding.cs new file mode 100644 index 0000000..3ec24c1 --- /dev/null +++ b/src/Tests/PolyfillTests_Encoding.cs @@ -0,0 +1,40 @@ +partial class PolyfillTests +{ +#if FeatureMemory + [Test] + public void Encoding_GetByteCount() + { + var encoding = Encoding.UTF8; + var chars = "Hello, World!".AsSpan(); + var byteCount = encoding.GetByteCount(chars); + Assert.AreEqual(13, byteCount); + } +#endif + +#if FeatureMemory && AllowUnsafeBlocks + + [Test] + public void Encoding_GetString() + { + var array = (ReadOnlySpan)"value"u8.ToArray().AsSpan(); + var result = Encoding.UTF8.GetString(array); + Assert.AreEqual("value", result); + } + + [Test] + public void Encoding_GetBytes() + { + var encoding = Encoding.UTF8; + var chars = "Hello, World!".AsSpan(); + var bytes = new byte[encoding.GetByteCount(chars)].AsSpan(); + + // Act + var byteCount = encoding.GetBytes(chars, bytes); + + // Assert + Assert.AreEqual(encoding.GetByteCount(chars), byteCount); + Assert.AreEqual(encoding.GetBytes("Hello, World!"), bytes.ToArray()); + } + +#endif +} \ No newline at end of file diff --git a/src/Tests/PolyfillTests_Enum.cs b/src/Tests/PolyfillTests_Enum.cs index 5e9181b..61fa24b 100644 --- a/src/Tests/PolyfillTests_Enum.cs +++ b/src/Tests/PolyfillTests_Enum.cs @@ -47,7 +47,6 @@ public void Parse() [Test] public void TryParse() { - var result = EnumPolyfill.TryParse("Sunday".AsSpan(), out var dayOfWeek); Assert.AreEqual(DayOfWeek.Sunday, dayOfWeek); Assert.True(result);