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


C# Span.Set方法代码示例

本文整理汇总了C#中Span.Set方法的典型用法代码示例。如果您正苦于以下问题:C# Span.Set方法的具体用法?C# Span.Set怎么用?C# Span.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Span的用法示例。


在下文中一共展示了Span.Set方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: SetArray

 public void SetArray()
 {
     var destination = new Span<byte>(new byte[100]);
     var source = new byte[] { 1, 2, 3 };
     destination.Set(source);
     for (int i = 0; i < source.Length; i++)
     {
         Assert.Equal(source[i], destination[i]);
     }
 }
开发者ID:GrimDerp,项目名称:corefxlab,代码行数:10,代码来源:BasicUnitTests.cs

示例2: TryWriteDigitOrSymbol

        public bool TryWriteDigitOrSymbol(ulong digitOrSymbolIndex, Span<byte> buffer, out int bytesWritten)
        {
            byte[] bytes = _digitsAndSymbols[digitOrSymbolIndex];
            bytesWritten = bytes.Length;
            if (bytesWritten > buffer.Length)
            {
                bytesWritten = 0;
                return false;
            }

            if (bytesWritten == 2)
            {
                buffer[0] = bytes[0];
                buffer[1] = bytes[1];
                return true;
            }

            if (bytesWritten == 1)
            {
                buffer[0] = bytes[0];
                return true;
            }

            buffer.Set(bytes);
            return true;
        }
开发者ID:geoffkizer,项目名称:corefxlab,代码行数:26,代码来源:FormattingData.cs

示例3: TryFormat

        public static bool TryFormat(this Utf8String value, Span<byte> buffer, Format.Parsed format, EncodingData formattingData, out int bytesWritten)
        {
            if (formattingData.IsUtf16) {
                bytesWritten = 0;
                int justWritten;
                foreach(var cp in value.CodePoints) {
                    if(!Utf16.Utf16LittleEndianEncoder.TryEncodeCodePoint(cp, buffer.Slice(bytesWritten), out justWritten)) {
                        bytesWritten = 0;
                        return false;
                    }
                    bytesWritten += justWritten ;
                }
                return true;
            }

            if(buffer.Length < value.Length) {
                bytesWritten = 0;
                return false;
            }

            buffer.Set(value.Bytes);
            bytesWritten = value.Length;
            return true;
        }
开发者ID:jkotas,项目名称:corefxlab,代码行数:24,代码来源:PrimitiveFormatter.cs

示例4: ReadOnlySpanOfByteCopyToAnotherSpanOfByteTwoDifferentBuffersValidCasesNative

        public unsafe void ReadOnlySpanOfByteCopyToAnotherSpanOfByteTwoDifferentBuffersValidCasesNative(byte[] expected, byte[] a, int aidx, int acount, byte[] b, int bidx, int bcount)
        {
            IntPtr pa = Marshal.AllocHGlobal(a.Length);
            Span<byte> na = new Span<byte>(pa.ToPointer(), a.Length);
            na.Set(a);

            IntPtr pb = Marshal.AllocHGlobal(b.Length);
            Span<byte> nb = new Span<byte>(pb.ToPointer(), b.Length);
            nb.Set(b);

            ReadOnlySpan<byte> spanA = na.Slice(aidx, acount);
            Span<byte> spanB = nb.Slice(bidx, bcount);

            if (expected != null) {               
                spanA.CopyTo(spanB);
                Assert.Equal(expected, b);

                ReadOnlySpan<byte> spanExpected = new ReadOnlySpan<byte>(expected);
                ReadOnlySpan<byte> spanBAll = new ReadOnlySpan<byte>(b);
                Assert.True(spanExpected.SequenceEqual(spanBAll));
            }
            else {
                Assert.ThrowsAny<Exception>(() => { spanA.CopyTo(spanB); });
            }

            Marshal.FreeHGlobal(pa);
            Marshal.FreeHGlobal(pb);
        }
开发者ID:jkotas,项目名称:corefxlab,代码行数:28,代码来源:UsageScenarioTests.cs

示例5: TryFormat

        public static bool TryFormat(this Utf8String value, Span<byte> buffer, Format.Parsed format, FormattingData formattingData, out int bytesWritten)
        {
            if (formattingData.IsUtf16) {
                throw new NotImplementedException();
            }

            if(buffer.Length < value.Length) {
                bytesWritten = 0;
                return false;
            }

            buffer.Set(value.Bytes);
            bytesWritten = value.Length;
            return true;
        }
开发者ID:tarekgh,项目名称:corefxlab,代码行数:15,代码来源:PrimitiveFormatters.cs

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