Skip to content

Commit

Permalink
Merge branch 'main' into TryFormat-byte
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Dec 19, 2024
2 parents 0cf7197 + f329299 commit 2048f91
Showing 1 changed file with 15 additions and 1 deletion.
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 2048f91

Please sign in to comment.