Skip to content

Commit

Permalink
expost Encoding.GetString when not unsafe (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Dec 19, 2024
1 parent 7396cca commit f329299
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
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

0 comments on commit f329299

Please sign in to comment.