• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# ReadOnlySpan类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中ReadOnlySpan的典型用法代码示例。如果您正苦于以下问题:C# ReadOnlySpan类的具体用法?C# ReadOnlySpan怎么用?C# ReadOnlySpan使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



ReadOnlySpan类属于命名空间,在下文中一共展示了ReadOnlySpan类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Append

 //TODO: this should use ByteSpan
 public void Append(ReadOnlySpan<char> substring)
 {
     for (int i = 0; i < substring.Length; i++)
     {
         Append(substring[i]);
     }
 }
开发者ID:ryaneliseislalom,项目名称:corefxlab,代码行数:8,代码来源:StringFormatter.cs


示例2: DangerousGetPinnableReferenceArray

 public static void DangerousGetPinnableReferenceArray()
 {
     int[] a = { 91, 92, 93, 94, 95 };
     ReadOnlySpan<int> span = new ReadOnlySpan<int>(a, 1, 3);
     ref int pinnableReference = ref span.DangerousGetPinnableReference();
     Assert.True(Unsafe.AreSame<int>(ref a[1], ref pinnableReference));
 }
开发者ID:dotnet,项目名称:corefx,代码行数:7,代码来源:DangerousGetPinnableReference.cs


示例3: TryFormatInt64

        // TODO: format should be ReadOnlySpan<char>
        internal static bool TryFormatInt64(long value, byte numberOfBytes, Span<byte> buffer, ReadOnlySpan<char> format, EncodingData formattingData, out int bytesWritten)
        {
            Precondition.Require(numberOfBytes <= sizeof(long));

            TextFormat parsedFormat = TextFormat.Parse(format);
            return TryFormatInt64(value, numberOfBytes, buffer, parsedFormat, formattingData, out bytesWritten);
        }
开发者ID:AlexGhiondea,项目名称:corefxlab,代码行数:8,代码来源:IntegerFormatter.cs


示例4: TwoReadOnlySpansCreatedOverSameIntArrayAreEqual

    public void TwoReadOnlySpansCreatedOverSameIntArrayAreEqual()
    {
        for (int i = 0; i < 2; i++)
        {
            var ints = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            // Try out two ways of creating a slice:
            ReadOnlySpan<int> slice;
            if (i == 0)
            {
                slice = new ReadOnlySpan<int>(ints);
            }
            else
            {
                slice = ints.Slice();
            }
            Assert.Equal(ints.Length, slice.Length);
            // Now try out two ways of walking the slice's contents:
            for (int j = 0; j < ints.Length; j++)
            {
                Assert.Equal(ints[j], slice[j]);
            }
            {
                int j = 0;
                foreach (var x in slice)
                {
                    Assert.Equal(ints[j], x);
                    j++;
                }
            }
        }
    }
开发者ID:TerabyteX,项目名称:corefxlab,代码行数:32,代码来源:Tests.cs


示例5: IsFalse

        private static bool IsFalse(ReadOnlySpan<char> utf16Chars)
        {
            if (utf16Chars.Length < 5)
                return false;

            char firstChar = utf16Chars[0];
            if (firstChar != 'f' && firstChar != 'F')
                return false;

            char secondChar = utf16Chars[1];
            if (secondChar != 'a' && secondChar != 'A')
                return false;

            char thirdChar = utf16Chars[2];
            if (thirdChar != 'l' && thirdChar != 'L')
                return false;

            char fourthChar = utf16Chars[3];
            if (fourthChar != 's' && fourthChar != 'S')
                return false;

            char fifthChar = utf16Chars[4];
            if (fifthChar != 'e' && fifthChar != 'E')
                return false;

            return true;
        }
开发者ID:benaadams,项目名称:corefxlab,代码行数:27,代码来源:PrimitiveParser2_bool.cs


示例6: ByteReadOnlySpanEqualsTestsTwoDifferentInstancesOfBuffersWithOneValueDifferent

        public unsafe void ByteReadOnlySpanEqualsTestsTwoDifferentInstancesOfBuffersWithOneValueDifferent()
        {
            const int bufferLength = 128;
            byte[] buffer1 = new byte[bufferLength];
            byte[] buffer2 = new byte[bufferLength];

            for (int i = 0; i < bufferLength; i++)
            {
                buffer1[i] = (byte)(bufferLength + 1 - i);
                buffer2[i] = (byte)(bufferLength + 1 - i);
            }

            fixed (byte* buffer1pinned = buffer1)
            fixed (byte* buffer2pinned = buffer2)
            {
                ReadOnlySpan<byte> b1 = new ReadOnlySpan<byte>(buffer1pinned, bufferLength);
                ReadOnlySpan<byte> b2 = new ReadOnlySpan<byte>(buffer2pinned, bufferLength);

                for (int i = 0; i < bufferLength; i++)
                {
                    for (int diffPosition = i; diffPosition < bufferLength; diffPosition++)
                    {
                        buffer1[diffPosition] = unchecked((byte)(buffer1[diffPosition] + 1));
                        Assert.False(b1.Slice(i).SequenceEqual(b2.Slice(i)));
                    }
                }
            }
        }
开发者ID:GrimDerp,项目名称:corefxlab,代码行数:28,代码来源:BasicUnitTests.cs


示例7: Encode

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <returns>Number of bytes written to the destination.</returns>
        public static int Encode(ReadOnlySpan<byte> source, Span<byte> destination)
        {
            int di = 0;
            int si = 0;
            byte b0, b1, b2, b3;
            for (; si<source.Length - 2;) {
                var result = Encode(source.Slice(si));
                si += 3;
                destination.Slice(di).Write(result);
                di += 4;
            }

            if (si == source.Length - 1) {
                Encode(source[si], 0, 0, out b0, out b1, out b2, out b3);
                destination[di++] = b0;
                destination[di++] = b1;
                destination[di++] = s_encodingMap[64];
                destination[di++] = s_encodingMap[64];
            }
            else if(si == source.Length - 2) {
                Encode(source[si++], source[si], 0, out b0, out b1, out b2, out b3);
                destination[di++] = b0;
                destination[di++] = b1;
                destination[di++] = b2;
                destination[di++] = s_encodingMap[64];
            }

            return di; 
        }
开发者ID:jkotas,项目名称:corefxlab,代码行数:35,代码来源:Base64.cs


示例8: CtorArrayIntStartEqualsLength

 public static void CtorArrayIntStartEqualsLength()
 {
     // Valid for start to equal the array length. This returns an empty span that starts "just past the array."
     int[] a = { 91, 92, 93 };
     ReadOnlySpan<int> span = new ReadOnlySpan<int>(a, 3);
     span.Validate<int>();
 }
开发者ID:dotnet,项目名称:corefx,代码行数:7,代码来源:CtorArrayInt.cs


示例9: JsonObject

 internal JsonObject(ReadOnlySpan<byte> values, ReadOnlySpan<byte> db, BufferPool pool = null, OwnedMemory<byte> dbMemory = null)
 {
     _db = db;
     _values = values;
     _pool = pool;
     _dbMemory = dbMemory;
 }
开发者ID:AlexGhiondea,项目名称:corefxlab,代码行数:7,代码来源:JsonParseObject.cs


示例10: Parse

        // TODO: format should be ReadOnlySpan<T>
        public static Format.Parsed Parse(ReadOnlySpan<char> format)
        {
            if (format.Length == 0)
            {
                return default(Format.Parsed);
            }

            uint precision = NoPrecision;
            if (format.Length > 1)
            {
                var span = format.Slice(1, format.Length - 1);

                if (!PrimitiveParser.TryParseUInt32(span, out precision))
                {
                    throw new NotImplementedException("UnableToParsePrecision");
                }

                if (precision > Parsed.MaxPrecision)
                {
                    // TODO: this is a contract violation
                    throw new Exception("PrecisionValueOutOfRange");
                }
            }

            // TODO: this is duplicated from above. It needs to be refactored
            var specifier = format[0];
            return new Parsed(specifier, (byte)precision);
        }
开发者ID:jkotas,项目名称:corefxlab,代码行数:29,代码来源:ParsedFormat.cs


示例11: SliceIntIntPastEnd

 public static void SliceIntIntPastEnd()
 {
     int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
     ReadOnlySpan<int> span = new ReadOnlySpan<int>(a).Slice(a.Length, 0);
     Assert.Equal(0, span.Length);
     Assert.True(Unsafe.AreSame<int>(ref a[a.Length - 1], ref Unsafe.Subtract<int>(ref span.DangerousGetPinnableReference(), 1)));
 }
开发者ID:dotnet,项目名称:corefx,代码行数:7,代码来源:Slice.cs


示例12: SliceIntIntUpToEnd

 public static void SliceIntIntUpToEnd()
 {
     int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
     ReadOnlySpan<int> span = new ReadOnlySpan<int>(a).Slice(4, 6);
     Assert.Equal(6, span.Length);
     Assert.True(Unsafe.AreSame<int>(ref a[4], ref span.DangerousGetPinnableReference()));
 }
开发者ID:dotnet,项目名称:corefx,代码行数:7,代码来源:Slice.cs


示例13: CtorReadOnlySpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks

        public void CtorReadOnlySpanOverByteArrayValidCasesWithPropertiesAndBasicOperationsChecks(byte[] array)
        {
            ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(array);
            Assert.Equal(array.Length, span.Length);

            Assert.NotSame(array, span.CreateArray());
            Assert.False(span.Equals(array));

            ReadOnlySpan<byte>.Enumerator it = span.GetEnumerator();
            for (int i = 0; i < span.Length; i++)
            {
                Assert.True(it.MoveNext());
                Assert.Equal(array[i], it.Current);
                Assert.Equal(array[i], span.Slice(i).Read<byte>());
                Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);

                array[i] = unchecked((byte)(array[i] + 1));
                Assert.Equal(array[i], it.Current);
                Assert.Equal(array[i], span.Slice(i).Read<byte>());
                Assert.Equal(array[i], span.Slice(i).Read<MyByte>().Value);
            }
            Assert.False(it.MoveNext());

            it.Reset();
            for (int i = 0; i < span.Length; i++)
            {
                Assert.True(it.MoveNext());
                Assert.Equal(array[i], it.Current);
            }
            Assert.False(it.MoveNext());
        }
开发者ID:hanin,项目名称:corefxlab,代码行数:31,代码来源:UsageScenarioTests.cs


示例14: Contains

 // Helper methods similar to System.ArrayExtension:
 // String helper methods, offering methods like String on Slice<char>:
 // TODO(joe): culture-sensitive comparisons.
 // TODO: should these move to string/text related assembly
 public static bool Contains(this ReadOnlySpan<char> str, ReadOnlySpan<char> value)
 {
     if (value.Length > str.Length)
     {
         return false;
     }
     return str.IndexOf(value) >= 0;
 }
开发者ID:AlexGhiondea,项目名称:corefxlab,代码行数:12,代码来源:SpanExtensions_text.cs


示例15: GetByteCount

 public static int GetByteCount(ReadOnlySpan buffer)
 {
     var count = 0;
     foreach (var x in buffer)
         if ((x & 0b1100_0000) != 0b1000_0000)
             count++;
     return count;
 }
开发者ID:ufcpp,项目名称:UfcppSample,代码行数:8,代码来源:Decoder.cs


示例16: TryParseBoolean

 public unsafe static bool TryParseBoolean(char* text, int length, out bool value, out int charactersConsumed)
 {
     var span = new ReadOnlySpan<byte>(text, length * 2);
     int bytesConsumed;
     bool result = PrimitiveParser.TryParseBoolean(span, out value, out bytesConsumed, EncodingData.InvariantUtf16);
     charactersConsumed = bytesConsumed / 2;
     return result;
 }
开发者ID:benaadams,项目名称:corefxlab,代码行数:8,代码来源:InvariantUtf16_bool.cs


示例17: ToArray1

 public static void ToArray1()
 {
     int[] a = { 91, 92, 93 };
     ReadOnlySpan<int> span = new ReadOnlySpan<int>(a);
     int[] copy = span.ToArray();
     Assert.Equal<int>(a, copy);
     Assert.NotSame(a, copy);
 }
开发者ID:dotnet,项目名称:corefx,代码行数:8,代码来源:ToArray.cs


示例18: EmptySpansNotUnified

        public static void EmptySpansNotUnified()
        {
            ReadOnlySpan<int> left = new ReadOnlySpan<int>(new int[0]);
            ReadOnlySpan<int> right = new ReadOnlySpan<int>(new int[0]);

            Assert.False(left == right);
            Assert.False(!(left != right));
        }
开发者ID:dotnet,项目名称:corefx,代码行数:8,代码来源:Equality.cs


示例19: EqualityComparesRangeNotContent

        public static void EqualityComparesRangeNotContent()
        {
            ReadOnlySpan<int> left = new ReadOnlySpan<int>(new int[] { 0, 1, 2 }, 1, 1);
            ReadOnlySpan<int> right = new ReadOnlySpan<int>(new int[] { 0, 1, 2 }, 1, 1);

            Assert.False(left == right);
            Assert.False(!(left != right));
        }
开发者ID:dotnet,项目名称:corefx,代码行数:8,代码来源:Equality.cs


示例20: EqualityReflexivity

        public static void EqualityReflexivity()
        {
            int[] a = { 91, 92, 93, 94, 95 };
            ReadOnlySpan<int> left = new ReadOnlySpan<int>(a, 2, 3);

            Assert.True(left == left);
            Assert.True(!(left != left));
        }
开发者ID:dotnet,项目名称:corefx,代码行数:8,代码来源:Equality.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# ReadOperation类代码示例发布时间:2022-05-24
下一篇:
C# ReadOnlyObservableCollection类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap