我正在使用新的private static <T> void test(Collection<String> t) {
,我正在尝试了解它是如何工作的。我的目标是重做以下函数以使用ReadOnlySpan:
Span<T>
到目前为止我所拥有的是:
public static string Base64Encode(this string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
理想情况下,最终结果应如下所示:
public static unsafe ReadOnlySpan<char> Base64Encode(this ReadOnlySpan<char> s)
{
int byteCount = System.Text.Encoding.UTF8.GetByteCount(s);
IntPtr unmanagedPointer = Marshal.AllocHGlobal(byteCount);
Span<byte> plainTextBytes = new Span<byte>((void*)unmanagedPointer, byteCount);
System.Text.Encoding.UTF8.GetBytes(s, plainTextBytes);
return System.Convert.ToBase64String(plainTextBytes).AsSpan();
}
编辑:不返回public static unsafe ReadOnlySpan<char> Base64Encode(this ReadOnlySpan<char> s)
{
//code without allocations
}
Span<T>