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


C# ReadOnlySpan类代码示例

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


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


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