当前位置: 首页>>代码示例>>C#>>正文


C# Span.Pin方法代码示例

本文整理汇总了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();
            }
        }
开发者ID:Applied-Duality,项目名称:corefxlab,代码行数:89,代码来源:PrimitiveFormatters.cs

示例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();
            }
        }
开发者ID:ericstj,项目名称:corefxlab,代码行数:81,代码来源:PrimitiveFormatters.cs


注:本文中的Span.Pin方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。