Skip to content

Commit 7396cca

Browse files
authored
Add GetByteCount(Span) (#261)
1 parent 0876e27 commit 7396cca

File tree

6 files changed

+68
-14
lines changed

6 files changed

+68
-14
lines changed

apiCount.include.md

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

api_list.include.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676

7777
#### Encoding
7878

79-
* `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))))
79+
* `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))))
80+
* `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))))
8081
* `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))))
8182

8283

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ Enable by adding and MSBuild property `PolyGuard`
12121212

12131213
* `ArgumentException.ThrowIfNullOrEmpty` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorempty)
12141214
* `ArgumentException.ThrowIfNullOrWhiteSpace` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.argumentexception.throwifnullorwhitespace)
1215-
* `ArgumentNullException.ThrowIfNull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.argumentnullexception.throwifnull?view=net-8.0)
1215+
* `ArgumentNullException.ThrowIfNull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.argumentnullexception.throwifnull)
12161216

12171217
With the equivalent Guard APIs:
12181218

src/Polyfill/Polyfill_Encoding.cs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,63 @@
55

66
#pragma warning disable
77

8-
#if FeatureMemory && AllowUnsafeBlocks && !NETCOREAPP2_1_OR_GREATER
8+
#if FeatureMemory
99

1010
namespace Polyfills;
1111

1212
using System;
1313

1414
static partial class Polyfill
1515
{
16+
#if NETCOREAPP2_0 || NETFRAMEWORK || NETSTANDARD2_0
17+
/// <summary>
18+
/// When overridden in a derived class, calculates the number of bytes produced by encoding the characters in the specified character span.
19+
/// </summary>
20+
/// <param name="encoding"></param>
21+
/// <param name="chars">The span of characters to encode.</param>
22+
/// <returns>The number of bytes produced by encoding the specified character span.</returns>
23+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getbytecount#system-text-encoding-getbytecount(system-readonlyspan((system-char)))
24+
public static int GetByteCount(this Encoding target, ReadOnlySpan<char> chars) =>
25+
target.GetByteCount(chars.ToArray());
26+
#endif
27+
28+
#if AllowUnsafeBlocks && !NETCOREAPP2_1_OR_GREATER
1629
/// <summary>When overridden in a derived class, encodes into a span of bytes a set of characters from the specified read-only span.</summary>
1730
/// <param name="chars">The span containing the set of characters to encode.</param>
1831
/// <param name="bytes">The byte span to hold the encoded bytes.</param>
1932
/// <returns>The number of encoded bytes.</returns>
20-
//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)))
21-
public static unsafe int GetBytes(this Encoding encoding, ReadOnlySpan<char> chars, Span<byte> bytes)
33+
//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)))
34+
public static unsafe int GetBytes(this Encoding target, ReadOnlySpan<char> chars, Span<byte> bytes)
2235
{
23-
if (encoding is null)
36+
if (target is null)
2437
{
25-
throw new ArgumentNullException(nameof(encoding));
38+
throw new ArgumentNullException(nameof(target));
2639
}
2740

2841
fixed (char* charsPtr = chars)
2942
fixed (byte* bytesPtr = bytes)
3043
{
31-
return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
44+
return target.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length);
3245
}
3346
}
3447

3548
/// <summary>When overridden in a derived class, decodes all the bytes in the specified byte span into a string.</summary>
3649
/// <param name="bytes">A read-only byte span to decode to a Unicode string.</param>
3750
/// <returns>A string that contains the decoded bytes from the provided read-only span.</returns>
3851
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getstring#system-text-encoding-getstring(system-readonlyspan((system-byte)))
39-
public static unsafe string GetString(this Encoding encoding, ReadOnlySpan<byte> bytes)
52+
public static unsafe string GetString(this Encoding target, ReadOnlySpan<byte> bytes)
4053
{
41-
if (encoding is null)
54+
if (target is null)
4255
{
43-
throw new ArgumentNullException(nameof(encoding));
56+
throw new ArgumentNullException(nameof(target));
4457
}
4558

4659
fixed (byte* bytesPtr = bytes)
4760
{
48-
return encoding.GetString(bytesPtr, bytes.Length);
61+
return target.GetString(bytesPtr, bytes.Length);
4962
}
5063
}
64+
#endif
5165
}
5266

5367
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
partial class PolyfillTests
2+
{
3+
#if FeatureMemory
4+
[Test]
5+
public void Encoding_GetByteCount()
6+
{
7+
var encoding = Encoding.UTF8;
8+
var chars = "Hello, World!".AsSpan();
9+
var byteCount = encoding.GetByteCount(chars);
10+
Assert.AreEqual(13, byteCount);
11+
}
12+
#endif
13+
14+
#if FeatureMemory && AllowUnsafeBlocks
15+
16+
[Test]
17+
public void Encoding_GetString()
18+
{
19+
var array = (ReadOnlySpan<byte>)"value"u8.ToArray().AsSpan();
20+
var result = Encoding.UTF8.GetString(array);
21+
Assert.AreEqual("value", result);
22+
}
23+
24+
[Test]
25+
public void Encoding_GetBytes()
26+
{
27+
var encoding = Encoding.UTF8;
28+
var chars = "Hello, World!".AsSpan();
29+
var bytes = new byte[encoding.GetByteCount(chars)].AsSpan();
30+
31+
// Act
32+
var byteCount = encoding.GetBytes(chars, bytes);
33+
34+
// Assert
35+
Assert.AreEqual(encoding.GetByteCount(chars), byteCount);
36+
Assert.AreEqual(encoding.GetBytes("Hello, World!"), bytes.ToArray());
37+
}
38+
39+
#endif
40+
}

src/Tests/PolyfillTests_Enum.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public void Parse()
4747
[Test]
4848
public void TryParse()
4949
{
50-
5150
var result = EnumPolyfill.TryParse<DayOfWeek>("Sunday".AsSpan(), out var dayOfWeek);
5251
Assert.AreEqual(DayOfWeek.Sunday, dayOfWeek);
5352
Assert.True(result);

0 commit comments

Comments
 (0)