|  | 
| 5 | 5 | 
 | 
| 6 | 6 | #pragma warning disable | 
| 7 | 7 | 
 | 
| 8 |  | -#if FeatureMemory && AllowUnsafeBlocks && !NETCOREAPP2_1_OR_GREATER | 
|  | 8 | +#if FeatureMemory | 
| 9 | 9 | 
 | 
| 10 | 10 | namespace Polyfills; | 
| 11 | 11 | 
 | 
| 12 | 12 | using System; | 
| 13 | 13 | 
 | 
| 14 | 14 | static partial class Polyfill | 
| 15 | 15 | { | 
|  | 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 | 
| 16 | 29 |     /// <summary>When overridden in a derived class, encodes into a span of bytes a set of characters from the specified read-only span.</summary> | 
| 17 | 30 |     /// <param name="chars">The span containing the set of characters to encode.</param> | 
| 18 | 31 |     /// <param name="bytes">The byte span to hold the encoded bytes.</param> | 
| 19 | 32 |     /// <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) | 
| 22 | 35 |     { | 
| 23 |  | -        if (encoding is null) | 
|  | 36 | +        if (target is null) | 
| 24 | 37 |         { | 
| 25 |  | -            throw new ArgumentNullException(nameof(encoding)); | 
|  | 38 | +            throw new ArgumentNullException(nameof(target)); | 
| 26 | 39 |         } | 
| 27 | 40 | 
 | 
| 28 | 41 |         fixed (char* charsPtr = chars) | 
| 29 | 42 |         fixed (byte* bytesPtr = bytes) | 
| 30 | 43 |         { | 
| 31 |  | -            return encoding.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length); | 
|  | 44 | +            return target.GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length); | 
| 32 | 45 |         } | 
| 33 | 46 |     } | 
| 34 | 47 | 
 | 
| 35 | 48 |     /// <summary>When overridden in a derived class, decodes all the bytes in the specified byte span into a string.</summary> | 
| 36 | 49 |     /// <param name="bytes">A read-only byte span to decode to a Unicode string.</param> | 
| 37 | 50 |     /// <returns>A string that contains the decoded bytes from the provided read-only span.</returns> | 
| 38 | 51 |     //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) | 
| 40 | 53 |     { | 
| 41 |  | -        if (encoding is null) | 
|  | 54 | +        if (target is null) | 
| 42 | 55 |         { | 
| 43 |  | -            throw new ArgumentNullException(nameof(encoding)); | 
|  | 56 | +            throw new ArgumentNullException(nameof(target)); | 
| 44 | 57 |         } | 
| 45 | 58 | 
 | 
| 46 | 59 |         fixed (byte* bytesPtr = bytes) | 
| 47 | 60 |         { | 
| 48 |  | -            return encoding.GetString(bytesPtr, bytes.Length); | 
|  | 61 | +            return target.GetString(bytesPtr, bytes.Length); | 
| 49 | 62 |         } | 
| 50 | 63 |     } | 
|  | 64 | +#endif | 
| 51 | 65 | } | 
| 52 | 66 | 
 | 
| 53 | 67 | #endif | 
0 commit comments