Skip to content

Commit 0937744

Browse files
committed
add Encoding GetChars
1 parent cb84a6f commit 0937744

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

apiCount.include.md

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

api_list.include.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
* `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))))
9191
* `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))))
9292
* `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))))
93+
* `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))))
9394
* `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))))
9495

9596

readme.md

Lines changed: 2 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`
1313

1414

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

1717

1818
**See [Milestones](../../milestones?state=closed) for release notes.**
@@ -559,6 +559,7 @@ The class `Polyfill` includes the following extension methods:
559559
* `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))))
560560
* `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))))
561561
* `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))))
562+
* `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))))
562563
* `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))))
563564

564565

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// <auto-generated />
2+
3+
#pragma warning disable
4+
5+
#if FeatureMemory
6+
7+
namespace Polyfills;
8+
9+
using System;
10+
using System.Runtime.InteropServices;
11+
using System.Text;
12+
13+
static partial class Polyfill
14+
{
15+
#if NETCOREAPP2_0 || NETFRAMEWORK || NETSTANDARD2_0
16+
/// <summary>
17+
/// Decodes all the bytes in the specified read-only byte span into a character span.
18+
/// </summary>
19+
/// <param name="chars">The character span receiving the decoded bytes.</param>
20+
/// <returns>The actual number of characters written at the span indicated by the chars parameter.</returns>
21+
//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)))
22+
public static int GetChars(this Encoding target, ReadOnlySpan<byte> bytes, Span<char> chars)
23+
{
24+
if (target is null)
25+
{
26+
throw new ArgumentNullException(nameof(target));
27+
}
28+
29+
char[] charArray = new char[bytes.Length];
30+
var array = bytes.ToArray();
31+
var count = target.GetChars(array, 0, bytes.Length, charArray, 0);
32+
new ReadOnlySpan<char>(charArray).CopyTo(chars);
33+
return count;
34+
}
35+
#endif
36+
37+
}
38+
39+
#endif

src/Tests/PolyfillTests_Encoding.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ public void Encoding_GetByteCount()
1111
Assert.AreEqual(13, byteCount);
1212
}
1313

14+
[Test]
15+
public void Encoding_GetChars()
16+
{
17+
// Arrange
18+
var encoding = Encoding.UTF8;
19+
var utf8Bytes = "Hello, World!"u8.ToArray();
20+
var byteSpan = new ReadOnlySpan<byte>(utf8Bytes);
21+
var charArray = new char[utf8Bytes.Length];
22+
var charSpan = new Span<char>(charArray);
23+
24+
// Act
25+
var charCount = encoding.GetChars(byteSpan, charSpan);
26+
27+
// Assert
28+
var result = charSpan.Slice(0, charCount).ToString();
29+
Assert.AreEqual("Hello, World!", result);
30+
}
31+
1432
[Test]
1533
public void Encoding_GetString()
1634
{

0 commit comments

Comments
 (0)