documentation update
This commit is contained in:
@@ -2,10 +2,15 @@ using System.Runtime.InteropServices;
|
||||
|
||||
namespace Just.Core;
|
||||
|
||||
/// <summary>
|
||||
/// URL-safe Base64 encoder/decoder for bytes, <see cref="long"/>, and <see cref="Guid"/>.
|
||||
/// Uses the standard Base64Url character set (- and _ instead of + and /), padding stripped by default.
|
||||
/// </summary>
|
||||
public static class Base64Url
|
||||
{
|
||||
private const char Padding = '=';
|
||||
|
||||
/// <summary>Decodes an 11-character Base64Url string into a <see cref="long"/>.</summary>
|
||||
[Pure] public static long DecodeLong(ReadOnlySpan<char> value)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNotEqual(value.Length, 11);
|
||||
@@ -24,6 +29,7 @@ public static class Base64Url
|
||||
return MemoryMarshal.Read<long>(longBytes);
|
||||
}
|
||||
|
||||
/// <summary>Decodes a 22-character Base64Url string into a <see cref="Guid"/>.</summary>
|
||||
[Pure] public static Guid DecodeGuid(ReadOnlySpan<char> value)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNotEqual(value.Length, 22);
|
||||
@@ -42,6 +48,7 @@ public static class Base64Url
|
||||
return new Guid(guidBytes);
|
||||
}
|
||||
|
||||
/// <summary>Decodes a Base64Url string into a byte array.</summary>
|
||||
[Pure] public static byte[] Decode(ReadOnlySpan<char> input)
|
||||
{
|
||||
if (input.IsEmpty) return [];
|
||||
@@ -53,6 +60,7 @@ public static class Base64Url
|
||||
return output[..size].ToArray();
|
||||
}
|
||||
|
||||
/// <summary>Decodes a Base64Url string into a pre-allocated byte span. Returns the number of bytes written.</summary>
|
||||
[Pure] public static int Decode(ReadOnlySpan<char> value, Span<byte> output)
|
||||
{
|
||||
var padding = (4 - (value.Length & 3)) & 3;
|
||||
@@ -72,6 +80,7 @@ public static class Base64Url
|
||||
return outputBytes;
|
||||
}
|
||||
|
||||
/// <summary>Encodes a <see cref="long"/> into an 11-character Base64Url string.</summary>
|
||||
[Pure] public static string Encode(in long id)
|
||||
{
|
||||
Span<byte> longBytes = stackalloc byte[8];
|
||||
@@ -84,6 +93,7 @@ public static class Base64Url
|
||||
return new string(chars[..^1]);
|
||||
}
|
||||
|
||||
/// <summary>Encodes a <see cref="Guid"/> into a 22-character Base64Url string.</summary>
|
||||
[Pure] public static string Encode(in Guid id)
|
||||
{
|
||||
Span<byte> guidBytes = stackalloc byte[16];
|
||||
@@ -96,6 +106,7 @@ public static class Base64Url
|
||||
return new string(chars[..^2]);
|
||||
}
|
||||
|
||||
/// <summary>Encodes a byte span into a Base64Url string.</summary>
|
||||
[Pure] public static string Encode(ReadOnlySpan<byte> input)
|
||||
{
|
||||
if (input.IsEmpty) return string.Empty;
|
||||
@@ -107,6 +118,7 @@ public static class Base64Url
|
||||
return new string(output[..strlen]);
|
||||
}
|
||||
|
||||
/// <summary>Encodes a byte span into a pre-allocated char span. Returns the number of characters written.</summary>
|
||||
[Pure] public static int Encode(ReadOnlySpan<byte> input, Span<char> output)
|
||||
{
|
||||
if (input.IsEmpty) return 0;
|
||||
|
||||
Reference in New Issue
Block a user