Skip to content
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
133 changes: 75 additions & 58 deletions readme.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void Base64UrlUsage()
}
#endif

#if FeatureMemory
#if FeatureMemory && !RefsBclMemory
void Base64Usage()
{
ReadOnlySpan<byte> source = default;
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;NETSDK1138;NU1901;NU1902;NU1903;CA1822;CA1847;CA1861;NU1510;NU1608;NU1109</NoWarn>
<Version>10.8.1</Version>
<Version>10.9.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Polyfill</PackageTags>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
Expand Down
221 changes: 221 additions & 0 deletions src/Polyfill/Base64Polyfill.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
#if FeatureMemory && !RefsBclMemory && !NET11_0_OR_GREATER

namespace Polyfills;

using System;
using System.Buffers;
using System.Buffers.Text;

static partial class Polyfill
{
extension(Base64)
{
/// <summary>
/// Returns the length (in chars) of the result if you were to encode binary data within a byte span of size <paramref name="bytesLength"/>.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.getencodedlength?view=net-11.0
public static int GetEncodedLength(int bytesLength)
{
if (bytesLength < 0)
{
throw new ArgumentOutOfRangeException(nameof(bytesLength));
}

return (bytesLength + 2) / 3 * 4;
}

/// <summary>
/// Returns the maximum length (in bytes) of the result if you were to decode base-64 encoded text of size <paramref name="base64Length"/>.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.getmaxdecodedlength?view=net-11.0
public static int GetMaxDecodedLength(int base64Length)
{
if (base64Length < 0)
{
throw new ArgumentOutOfRangeException(nameof(base64Length));
}

return base64Length / 4 * 3;
}

/// <summary>
/// Encodes the span of binary data into a string that is represented as base-64.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.encodetostring?view=net-11.0
public static string EncodeToString(ReadOnlySpan<byte> source) =>
EncodeBase64(source);

/// <summary>
/// Encodes the span of binary data into UTF-16 encoded text represented as base-64.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.encodetochars?view=net-11.0#system-buffers-text-base64-encodetochars(system-readonlyspan((system-byte))-system-span((system-char)))
public static int EncodeToChars(ReadOnlySpan<byte> source, Span<char> destination)
{
var encoded = EncodeBase64(source);

if (encoded.Length > destination.Length)
{
throw new ArgumentException("Destination is too small.", nameof(destination));
}

encoded.AsSpan().CopyTo(destination);
return encoded.Length;
}

/// <summary>
/// Encodes the span of binary data into UTF-16 encoded text represented as base-64.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.encodetochars?view=net-11.0#system-buffers-text-base64-encodetochars(system-readonlyspan((system-byte)))
public static char[] EncodeToChars(ReadOnlySpan<byte> source) =>
EncodeBase64(source).ToCharArray();

/// <summary>
/// Encodes the span of binary data into UTF-16 encoded text represented as base-64.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.encodetochars?view=net-11.0#system-buffers-text-base64-encodetochars(system-readonlyspan((system-byte))-system-span((system-char))-system-int32@-system-int32@-system-boolean)
public static OperationStatus EncodeToChars(ReadOnlySpan<byte> source, Span<char> destination, out int bytesConsumed, out int charsWritten, bool isFinalBlock = true)
{
var utf8 = new byte[destination.Length];
var status = Base64.EncodeToUtf8(source, utf8, out bytesConsumed, out charsWritten, isFinalBlock);

for (var i = 0; i < charsWritten; i++)
{
destination[i] = (char) utf8[i];
}

return status;
}

/// <summary>
/// Tries to encode the span of binary data into UTF-16 encoded text represented as base-64.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.tryencodetochars?view=net-11.0
public static bool TryEncodeToChars(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten)
{
var encoded = EncodeBase64(source);

if (encoded.Length > destination.Length)
{
charsWritten = 0;
return false;
}

encoded.AsSpan().CopyTo(destination);
charsWritten = encoded.Length;
return true;
}

/// <summary>
/// Encodes the span of binary data into a byte array of UTF-8 encoded text represented as base-64.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.encodetoutf8?view=net-11.0#system-buffers-text-base64-encodetoutf8(system-readonlyspan((system-byte)))
public static byte[] EncodeToUtf8(ReadOnlySpan<byte> source)
{
var encoded = EncodeBase64(source);
var result = new byte[encoded.Length];

for (var i = 0; i < encoded.Length; i++)
{
result[i] = (byte) encoded[i];
}

return result;
}

/// <summary>
/// Decodes the span of UTF-16 encoded text represented as base-64 into binary data.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.decodefromchars?view=net-11.0#system-buffers-text-base64-decodefromchars(system-readonlyspan((system-char)))
public static byte[] DecodeFromChars(ReadOnlySpan<char> source) =>
DecodeBase64(source);

/// <summary>
/// Decodes the span of UTF-16 encoded text represented as base-64 into binary data.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.decodefromchars?view=net-11.0#system-buffers-text-base64-decodefromchars(system-readonlyspan((system-char))-system-span((system-byte)))
public static int DecodeFromChars(ReadOnlySpan<char> source, Span<byte> destination)
{
var decoded = DecodeBase64(source);

if (decoded.Length > destination.Length)
{
throw new ArgumentException("Destination is too small.", nameof(destination));
}

decoded.CopyTo(destination);
return decoded.Length;
}

/// <summary>
/// Decodes the span of UTF-16 encoded text represented as base-64 into binary data.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.decodefromchars?view=net-11.0#system-buffers-text-base64-decodefromchars(system-readonlyspan((system-char))-system-span((system-byte))-system-int32@-system-int32@-system-boolean)
public static OperationStatus DecodeFromChars(ReadOnlySpan<char> source, Span<byte> destination, out int charsConsumed, out int bytesWritten, bool isFinalBlock = true)
{
var utf8 = new byte[source.Length];

for (var i = 0; i < source.Length; i++)
{
utf8[i] = (byte) source[i];
}

return Base64.DecodeFromUtf8(utf8, destination, out charsConsumed, out bytesWritten, isFinalBlock);
}

/// <summary>
/// Tries to decode the span of UTF-16 encoded text represented as base-64 into binary data.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.trydecodefromchars?view=net-11.0
public static bool TryDecodeFromChars(ReadOnlySpan<char> source, Span<byte> destination, out int bytesWritten)
{
try
{
var decoded = DecodeBase64(source);

if (decoded.Length > destination.Length)
{
bytesWritten = 0;
return false;
}

decoded.CopyTo(destination);
bytesWritten = decoded.Length;
return true;
}
catch (FormatException)
{
bytesWritten = 0;
return false;
}
}

/// <summary>
/// Decodes the span of UTF-8 encoded text represented as base-64 into binary data.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.buffers.text.base64.decodefromutf8?view=net-11.0#system-buffers-text-base64-decodefromutf8(system-readonlyspan((system-byte)))
public static byte[] DecodeFromUtf8(ReadOnlySpan<byte> source)
{
if (source.IsEmpty)
{
return Array.Empty<byte>();
}

var chars = new char[source.Length];

for (var i = 0; i < source.Length; i++)
{
chars[i] = (char) source[i];
}

return Convert.FromBase64CharArray(chars, 0, chars.Length);
}

static string EncodeBase64(ReadOnlySpan<byte> source) =>
source.IsEmpty ? string.Empty : Convert.ToBase64String(source.ToArray());

static byte[] DecodeBase64(ReadOnlySpan<char> source) =>
source.IsEmpty ? Array.Empty<byte>() : Convert.FromBase64CharArray(source.ToArray(), 0, source.Length);
}
}

#endif
19 changes: 19 additions & 0 deletions src/Polyfill/UriPolyfill.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#if !NET11_0_OR_GREATER

namespace Polyfills;

using System;

static partial class Polyfill
{
extension(Uri)
{
/// <summary>
/// Provides the scheme name for the <c>data</c> URI scheme (RFC 2397). This field is read-only.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.uri.urischemedata?view=net-11.0
public static string UriSchemeData => "data";
}
}

#endif
66 changes: 66 additions & 0 deletions src/Polyfill/Utf16.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#if FeatureMemory

#if !NET11_0_OR_GREATER

#pragma warning disable

namespace System.Text.Unicode;

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

/// <summary>Provides static methods for validating UTF-16 text.</summary>
[ExcludeFromCodeCoverage]
[DebuggerNonUserCode]
#if PolyUseEmbeddedAttribute
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
#endif
#if PolyPublic
public
#endif
static class Utf16
{
/// <summary>
/// Returns the index in <paramref name="value"/> where the first ill-formed UTF-16 subsequence begins, or -1 if <paramref name="value"/> is well-formed.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.text.unicode.utf16.indexofinvalidsubsequence?view=net-11.0
public static int IndexOfInvalidSubsequence(ReadOnlySpan<char> value)
{
for (var i = 0; i < value.Length; i++)
{
var c = value[i];
if (char.IsHighSurrogate(c))
{
if (i + 1 >= value.Length ||
!char.IsLowSurrogate(value[i + 1]))
{
return i;
}

// valid surrogate pair, skip the low surrogate
i++;
}
else if (char.IsLowSurrogate(c))
{
// a low surrogate not preceded by a high surrogate
return i;
}
}

return -1;
}

/// <summary>
/// Returns a value indicating whether <paramref name="value"/> is well-formed UTF-16.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.text.unicode.utf16.isvalid?view=net-11.0
public static bool IsValid(ReadOnlySpan<char> value) =>
IndexOfInvalidSubsequence(value) < 0;
}

#else
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Text.Unicode.Utf16))]
#endif

#endif
Loading
Loading