diff --git a/apiCount.include.md b/apiCount.include.md index e81b5840..39e71e11 100644 --- a/apiCount.include.md +++ b/apiCount.include.md @@ -1 +1 @@ -**API count: 455** \ No newline at end of file +**API count: 456** \ No newline at end of file diff --git a/api_list.include.md b/api_list.include.md index f475a73d..d9911c88 100644 --- a/api_list.include.md +++ b/api_list.include.md @@ -90,6 +90,7 @@ * `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)))) * `int GetCharCount(Encoding, ReadOnlySpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getcharcount#system-text-encoding-getcharcount(system-readonlyspan((system-byte)))) + * `int GetChars(Encoding, ReadOnlySpan, Span)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getchars#system-text-encoding-getchars(system-readonlyspan((system-byte))-system-span((system-char)))) * `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 b70a79c3..4de62a13 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,7 @@ The package targets `netstandard2.0` and is designed to support the following ru * `net5.0`, `net6.0`, `net7.0`, `net8.0`, `net9.0` -**API count: 455** +**API count: 456** **See [Milestones](../../milestones?state=closed) for release notes.** @@ -559,6 +559,7 @@ The class `Polyfill` includes the following extension methods: * `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)))) * `int GetCharCount(Encoding, ReadOnlySpan)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getcharcount#system-text-encoding-getcharcount(system-readonlyspan((system-byte)))) + * `int GetChars(Encoding, ReadOnlySpan, Span)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getchars#system-text-encoding-getchars(system-readonlyspan((system-byte))-system-span((system-char)))) * `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/src/Polyfill/Polyfill_Encoding_GetChars.cs b/src/Polyfill/Polyfill_Encoding_GetChars.cs new file mode 100644 index 00000000..7dfa1993 --- /dev/null +++ b/src/Polyfill/Polyfill_Encoding_GetChars.cs @@ -0,0 +1,39 @@ +// + +#pragma warning disable + +#if FeatureMemory + +namespace Polyfills; + +using System; +using System.Runtime.InteropServices; +using System.Text; + +static partial class Polyfill +{ +#if NETCOREAPP2_0 || NETFRAMEWORK || NETSTANDARD2_0 + /// + /// Decodes all the bytes in the specified read-only byte span into a character span. + /// + /// The character span receiving the decoded bytes. + /// The actual number of characters written at the span indicated by the chars parameter. + //Link: https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getchars#system-text-encoding-getchars(system-readonlyspan((system-byte))-system-span((system-char))) + public static int GetChars(this Encoding target, ReadOnlySpan bytes, Span chars) + { + if (target is null) + { + throw new ArgumentNullException(nameof(target)); + } + + char[] charArray = new char[bytes.Length]; + var array = bytes.ToArray(); + var count = target.GetChars(array, 0, bytes.Length, charArray, 0); + new ReadOnlySpan(charArray).CopyTo(chars); + return count; + } +#endif + +} + +#endif \ No newline at end of file diff --git a/src/Tests/PolyfillTests_Encoding.cs b/src/Tests/PolyfillTests_Encoding.cs index 24355c56..9e4983eb 100644 --- a/src/Tests/PolyfillTests_Encoding.cs +++ b/src/Tests/PolyfillTests_Encoding.cs @@ -11,6 +11,24 @@ public void Encoding_GetByteCount() Assert.AreEqual(13, byteCount); } + [Test] + public void Encoding_GetChars() + { + // Arrange + var encoding = Encoding.UTF8; + var utf8Bytes = "Hello, World!"u8.ToArray(); + var byteSpan = new ReadOnlySpan(utf8Bytes); + var charArray = new char[utf8Bytes.Length]; + var charSpan = new Span(charArray); + + // Act + var charCount = encoding.GetChars(byteSpan, charSpan); + + // Assert + var result = charSpan.Slice(0, charCount).ToString(); + Assert.AreEqual("Hello, World!", result); + } + [Test] public void Encoding_GetString() {