Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose Encoding.GetString when not unsafe #262

Merged
merged 1 commit into from
Dec 19, 2024
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
5 changes: 3 additions & 2 deletions 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: 415**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
**API count: 416**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->


**See [Milestones](../../milestones?state=closed) for release notes.**
Expand Down Expand Up @@ -527,7 +527,8 @@ The class `Polyfill` includes the following extension methods:

#### Encoding

* `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 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))))
* `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
16 changes: 15 additions & 1 deletion src/Polyfill/Polyfill_Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ public static unsafe int GetBytes(this Encoding target, ReadOnlySpan<char> chars
}
}

#endif
#if !NETCOREAPP2_1_OR_GREATER
/// <summary>When overridden in a derived class, decodes all the bytes in the specified byte span into a string.</summary>
/// <param name="bytes">A read-only byte span to decode to a Unicode string.</param>
/// <returns>A string that contains the decoded bytes from the provided read-only span.</returns>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.text.encoding.getstring#system-text-encoding-getstring(system-readonlyspan((system-byte)))
#if AllowUnsafeBlocks
public static unsafe string GetString(this Encoding target, ReadOnlySpan<byte> bytes)
{
if (target is null)
Expand All @@ -61,7 +64,18 @@ public static unsafe string GetString(this Encoding target, ReadOnlySpan<byte> b
return target.GetString(bytesPtr, bytes.Length);
}
}
#else
public static string GetString(this Encoding target, ReadOnlySpan<byte> bytes)
{
if (target is null)
{
throw new ArgumentNullException(nameof(target));
}

return target.GetString(bytes.ToArray());
}
#endif
#endif
}

#endif
#endif