Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadukf/d4f895a116bd75a939c5a0f63bedf00f to your computer and use it in GitHub Desktop.
Save kadukf/d4f895a116bd75a939c5a0f63bedf00f to your computer and use it in GitHub Desktop.
Base64Encoding.UsingSpanBase64BinaryGuid
public static string UsingSpanBase64BinaryGuid(Guid oid, params string[] input)
{
var totalUtf8Bytes = _guidSize;
for (int i = 0; i < input.Length; i++)
{
totalUtf8Bytes += Encoding.UTF8.GetByteCount(input[i]);
}
Span<byte> resultSpan = stackalloc byte[Base64.GetMaxEncodedToUtf8Length(totalUtf8Bytes)];
if (!MemoryMarshal.TryWrite(resultSpan, ref oid))
{
throw new ArithmeticException();
}
int writtenBytes = _guidSize;
for (int i = 0; i < input.Length; i++)
{
var ixs = EncodingHelper.GetUtf8Bytes(input[i].AsSpan(), resultSpan.Slice(writtenBytes));
writtenBytes += ixs;
}
OperationStatus status = Base64.EncodeToUtf8InPlace(resultSpan, totalUtf8Bytes, out int base64Written);
if (status != OperationStatus.Done)
{
throw new ArithmeticException();
}
var base64String = EncodingHelper.GetUrlSafeString(resultSpan.Slice(0, base64Written));
return base64String;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment