本文整理汇总了C#中Span.Pin方法的典型用法代码示例。如果您正苦于以下问题:C# Span.Pin方法的具体用法?C# Span.Pin怎么用?C# Span.Pin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Span
的用法示例。
在下文中一共展示了Span.Pin方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryFormat
public static bool TryFormat(this string value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
{
if (formattingData.IsUtf16)
{
var valueBytes = value.Length << 1;
if (valueBytes > buffer.Length)
{
bytesWritten = 0;
return false;
}
unsafe
{
fixed (char* pCharacters = value)
{
byte* pBytes = (byte*)pCharacters;
buffer.Set(pBytes, valueBytes);
}
}
bytesWritten = valueBytes;
return true;
}
GCHandle handle;
var byteSpan = buffer.Pin(out handle);
try {
var avaliableBytes = byteSpan.Length;
bytesWritten = 0;
for (int i = 0; i < value.Length; i++)
{
var c = value[i];
var codepoint = (ushort)c;
if (codepoint <= 0x7f) // this if block just optimizes for ascii
{
if (bytesWritten + 1 > avaliableBytes)
{
bytesWritten = 0;
return false;
}
byteSpan[bytesWritten++] = (byte)codepoint;
}
else
{
Utf8EncodedCodePoint encoded;
if (!char.IsSurrogate(c))
encoded = new Utf8EncodedCodePoint(c);
else
{
if (++i >= value.Length)
throw new ArgumentException("value", "Invalid surrogate pair.");
char lowSurrogate = value[i];
encoded = new Utf8EncodedCodePoint(c, lowSurrogate);
}
if (bytesWritten + encoded.Length > avaliableBytes)
{
bytesWritten = 0;
return false;
}
byteSpan[bytesWritten] = encoded.Byte0;
if (encoded.Length > 1)
{
byteSpan[bytesWritten + 1] = encoded.Byte1;
if (encoded.Length > 2)
{
byteSpan[bytesWritten + 2] = encoded.Byte2;
if (encoded.Length > 3)
{
byteSpan[bytesWritten + 3] = encoded.Byte3;
}
}
}
bytesWritten += encoded.Length;
}
}
return true;
}
finally
{
handle.Free();
}
}
示例2: TryFormat
public static bool TryFormat(this string value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
{
if (formattingData.IsUtf16)
{
var valueBytes = value.Length << 1;
if (valueBytes > buffer.Length)
{
bytesWritten = 0;
return false;
}
unsafe
{
fixed (char* pCharacters = value)
{
byte* pBytes = (byte*)pCharacters;
buffer.Set(pBytes, valueBytes);
}
}
bytesWritten = valueBytes;
return true;
}
GCHandle handle;
var byteSpan = buffer.Pin(out handle);
try {
var avaliableBytes = byteSpan.Length;
bytesWritten = 0;
for (int i = 0; i < value.Length; i++)
{
var c = value[i];
var codepoint = (ushort)c;
if (codepoint <= 0x7f) // this if block just optimizes for ascii
{
if (bytesWritten + 1 > avaliableBytes)
{
bytesWritten = 0;
return false;
}
byteSpan[bytesWritten++] = (byte)codepoint;
}
else
{
var encoded = new FourBytes();
var bytes = Utf8Encoder.CharToUtf8(c, ref encoded);
if (bytesWritten + bytes > avaliableBytes)
{
bytesWritten = 0;
return false;
}
byteSpan[bytesWritten] = encoded.B0;
if (bytes > 1)
{
byteSpan[bytesWritten + 1] = encoded.B1;
if (bytes > 2)
{
byteSpan[bytesWritten + 2] = encoded.B2;
if (bytes > 3)
{
byteSpan[bytesWritten + 3] = encoded.B3;
}
}
}
bytesWritten += bytes;
}
}
return true;
}
finally
{
handle.Free();
}
}