Created
April 23, 2020 20:52
-
-
Save kadukf/d4f895a116bd75a939c5a0f63bedf00f to your computer and use it in GitHub Desktop.
Base64Encoding.UsingSpanBase64BinaryGuid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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