-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polyfill non-allocating encoding span->string without byte[]
Originally proposed by @iamcarbon in #380 (comment) This requires allowing unsafe blocks for `net462` and `netstandard1.3`. Co-authored-by: Jason Nelson <[email protected]>
- Loading branch information
1 parent
4a9cf7d
commit ae0bcdb
Showing
4 changed files
with
36 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
#if NETFRAMEWORK || NETSTANDARD1_3 | ||
|
||
internal static class EncodingExtensions | ||
{ | ||
/// <summary> | ||
/// Converts a span of bytes into a string, following the specified encoding's rules. | ||
/// </summary> | ||
/// <param name="encoding">The encoding to follow.</param> | ||
/// <param name="bytes">The bytes to convert.</param> | ||
/// <returns>The decoded string.</returns> | ||
public unsafe static string GetString(this Encoding encoding, ReadOnlySpan<byte> bytes) | ||
{ | ||
// Poly-fill for method available in newer versions of .NET | ||
|
||
fixed (byte* pBytes = bytes) | ||
{ | ||
return encoding.GetString(pBytes, bytes.Length); | ||
} | ||
} | ||
} | ||
|
||
#endif |