Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Span support to Webencoders with direct encoding #338

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Rearranged the #if to make it more readable
  • Loading branch information
gfoidl committed Jun 20, 2018
commit aa412661449d00a97807bffcdc394c7d656b9230
25 changes: 15 additions & 10 deletions shared/Microsoft.Extensions.WebEncoders.Sources/WebEncoders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,28 +263,22 @@ public static unsafe string Base64UrlEncode(ReadOnlySpan<byte> data)
Debug.Assert(base64Url.Length == written);
});
}
#else
#if !NET461

#elif NETCOREAPP2_0
char[] arrayToReturnToPool = null;
try
{
#endif

var base64Url = base64UrlLen <= MaxStackallocBytes / sizeof(char)
? stackalloc char[base64UrlLen]
#if NET461
: new char[base64UrlLen];
#else
: arrayToReturnToPool = ArrayPool<char>.Shared.Rent(base64UrlLen);
#endif

var status = Base64UrlEncodeCore(data, base64Url, out int consumed, out int written);
Debug.Assert(base64UrlLen == written);

fixed (char* ptr = &MemoryMarshal.GetReference(base64Url))
{
return new string(ptr, 0, written);
}
#if !NET461
}
finally
{
Expand All @@ -293,7 +287,18 @@ public static unsafe string Base64UrlEncode(ReadOnlySpan<byte> data)
ArrayPool<char>.Shared.Return(arrayToReturnToPool);
}
}
#endif
#else
var base64Url = base64UrlLen <= MaxStackallocBytes / sizeof(char)
? stackalloc char[base64UrlLen]
: new char[base64UrlLen];

var status = Base64UrlEncodeCore(data, base64Url, out int consumed, out int written);
Debug.Assert(base64UrlLen == written);

fixed (char* ptr = &MemoryMarshal.GetReference(base64Url))
{
return new string(ptr, 0, written);
}
#endif
}

Expand Down