minor refactoring and tests
All checks were successful
.NET Test / test (push) Successful in 45s
.NET Publish / publish (push) Successful in 42s

This commit is contained in:
2025-08-06 22:02:38 +04:00
parent 7eb3008738
commit 312219d42f
5 changed files with 189 additions and 76 deletions

View File

@@ -1,24 +1,41 @@
using System.Runtime.InteropServices;
namespace Just.Core;
public static class Base64Url
{
private const char Padding = '=';
[Pure] public static long DecodeLong(ReadOnlySpan<char> value)
{
ArgumentOutOfRangeException.ThrowIfNotEqual(value.Length, 11);
Span<byte> longBytes = stackalloc byte[8];
Span<char> chars = stackalloc char[12];
value.CopyTo(chars);
chars[^1] = Padding;
ReplaceNonUrlChars(chars);
if (!Convert.TryFromBase64Chars(chars, longBytes, out int _))
throw new FormatException("Invalid Base64 string.");
return MemoryMarshal.Read<long>(longBytes);
}
[Pure] public static Guid DecodeGuid(ReadOnlySpan<char> value)
{
ArgumentOutOfRangeException.ThrowIfNotEqual(value.Length, 22);
Span<byte> guidBytes = stackalloc byte[16];
Span<char> chars = stackalloc char[24];
value.CopyTo(chars);
for (int i = 0; i < value.Length; i++)
{
switch (value[i])
{
case '-': chars[i] = '+'; continue;
case '_': chars[i] = '/'; continue;
default: continue;
}
}
chars[^2..].Fill('=');
chars[^2..].Fill(Padding);
ReplaceNonUrlChars(chars);
if (!Convert.TryFromBase64Chars(chars, guidBytes, out int _))
throw new FormatException("Invalid Base64 string.");
@@ -28,9 +45,9 @@ public static class Base64Url
[Pure] public static byte[] Decode(ReadOnlySpan<char> input)
{
if (input.IsEmpty) return [];
Span<byte> output = stackalloc byte[3 * ((input.Length + 3) / 4)];
var size = Decode(input, output);
return output[..size].ToArray();
@@ -40,28 +57,33 @@ public static class Base64Url
{
var padding = (4 - (value.Length & 3)) & 3;
var charlen = value.Length + padding;
var outputBytes = charlen / 4;
var outputBytes = 3 * (charlen / 4);
ArgumentOutOfRangeException.ThrowIfLessThan(output.Length, outputBytes);
Span<char> chars = stackalloc char[charlen];
value.CopyTo(chars);
for (int i = 0; i < value.Length; i++)
{
switch (value[i])
{
case '-': chars[i] = '+'; continue;
case '_': chars[i] = '/'; continue;
default: continue;
}
}
chars[^padding..].Fill('=');
chars[^padding..].Fill(Padding);
ReplaceNonUrlChars(chars);
if (!Convert.TryFromBase64Chars(chars, output, out outputBytes))
throw new FormatException("Invalid Base64 string.");
return outputBytes;
}
[Pure] public static string Encode(in long id)
{
Span<byte> longBytes = stackalloc byte[8];
MemoryMarshal.Write(longBytes, id);
Span<char> chars = stackalloc char[12];
Convert.TryToBase64Chars(longBytes, chars, out int _);
ReplaceUrlChars(chars[..^1]);
return new string(chars[..^1]);
}
[Pure] public static string Encode(in Guid id)
{
Span<byte> guidBytes = stackalloc byte[16];
@@ -69,15 +91,7 @@ public static class Base64Url
Span<char> chars = stackalloc char[24];
Convert.TryToBase64Chars(guidBytes, chars, out int _);
for (int i = 0; i < chars.Length - 2; i++)
{
switch (chars[i])
{
case '+': chars[i] = '-'; continue;
case '/': chars[i] = '_'; continue;
default: continue;
}
}
ReplaceUrlChars(chars[..^2]);
return new string(chars[..^2]);
}
@@ -86,7 +100,7 @@ public static class Base64Url
{
if (input.IsEmpty) return string.Empty;
int outLength = 8 * ((input.Length + 5) / 6);
int outLength = 4 * ((input.Length + 2) / 3);
Span<char> output = stackalloc char[outLength];
int strlen = Encode(input, output);
@@ -97,24 +111,47 @@ public static class Base64Url
{
if (input.IsEmpty) return 0;
var charlen = 8 * ((input.Length + 5) / 6);
var charlen = 4 * ((input.Length + 2) / 3);
Span<char> chars = stackalloc char[charlen];
Convert.TryToBase64Chars(input, chars, out int charsWritten);
int i;
for (i = 0; i < charsWritten; i++)
int i = ReplaceUrlChars(chars[..charsWritten]);
chars[..i].CopyTo(output);
return i;
}
private static int ReplaceUrlChars(Span<char> chars)
{
int i = 0;
for (; i < chars.Length; i++)
{
switch (chars[i])
{
case '+': chars[i] = '-'; continue;
case '/': chars[i] = '_'; continue;
case '=': goto exitLoop;
case Padding: goto break_loop;
default: continue;
}
}
exitLoop:
chars[..i].CopyTo(output);
break_loop:
return i;
}
private static int ReplaceNonUrlChars(Span<char> chars)
{
int i = 0;
for (; i < chars.Length; i++)
{
switch (chars[i])
{
case '-': chars[i] = '+'; continue;
case '_': chars[i] = '/'; continue;
case Padding: goto break_loop;
default: continue;
}
}
break_loop:
return i;
}
}