Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion apiCount.include.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**API count: 455**
**API count: 456**
1 change: 1 addition & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
* `int GetByteCount(Encoding, ReadOnlySpan<char>)` [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<char>, Span<byte>)` [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<byte>)` [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<byte>, Span<char>)` [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<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getstring#system-text-encoding-getstring(system-readonlyspan((system-byte))))


Expand Down
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
**API count: 456**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->


**See [Milestones](../../milestones?state=closed) for release notes.**
Expand Down Expand Up @@ -559,6 +559,7 @@ The class `Polyfill` includes the following extension methods:
* `int GetByteCount(Encoding, ReadOnlySpan<char>)` [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<char>, Span<byte>)` [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<byte>)` [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<byte>, Span<char>)` [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<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getstring#system-text-encoding-getstring(system-readonlyspan((system-byte))))


Expand Down
39 changes: 39 additions & 0 deletions src/Polyfill/Polyfill_Encoding_GetChars.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// <auto-generated />

#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
/// <summary>
/// Decodes all the bytes in the specified read-only byte span into a character span.
/// </summary>
/// <param name="chars">The character span receiving the decoded bytes.</param>
/// <returns>The actual number of characters written at the span indicated by the chars parameter.</returns>
//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<byte> bytes, Span<char> 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<char>(charArray).CopyTo(chars);
return count;
}
#endif

}

#endif
18 changes: 18 additions & 0 deletions src/Tests/PolyfillTests_Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte>(utf8Bytes);
var charArray = new char[utf8Bytes.Length];
var charSpan = new Span<char>(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()
{
Expand Down