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


C# ReadOnlySpan.Cast方法代码示例

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


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

示例1: TryParseUInt16

 public static bool TryParseUInt16(ReadOnlySpan<char> text, out ushort value, out int charactersConsumed)
 {
     var byteSpan = text.Cast<char, byte>();
     int bytesConsumed;
     bool result = PrimitiveParser.TryParseUInt16(byteSpan, out value, out bytesConsumed, EncodingData.InvariantUtf16, 'X');
     charactersConsumed = bytesConsumed / sizeof(char);
     return result;
 }
开发者ID:AlexGhiondea,项目名称:corefxlab,代码行数:8,代码来源:InvariantUtf16_UInt16.cs

示例2: TryParseBoolean

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

示例3: TryParseUInt32

        public static bool TryParseUInt32(ReadOnlySpan<char> text, out uint value, out int charactersConsumed)
        {
            Precondition.Require(text.Length > 0);

            ReadOnlySpan<byte> span = text.Cast<char, byte>();
            int bytesConsumed;
            var result = TryParseUInt32(span, TextEncoding.Utf16, out value, out bytesConsumed);
            charactersConsumed = bytesConsumed >> 1;
            return result;
        }
开发者ID:AlexGhiondea,项目名称:corefxlab,代码行数:10,代码来源:PrimitiveParser_uint.cs

示例4: TryParse

        public static bool TryParse(ReadOnlySpan<char> utf8Text, out uint value, out int charactersConsumed)
        {
            Precondition.Require(utf8Text.Length > 0);

            ReadOnlySpan<byte> span = utf8Text.Cast<char, byte>();
            int bytesConsumed;
            var result = TryParse(span, FormattingData.Encoding.Utf16, out value, out bytesConsumed);
            charactersConsumed = bytesConsumed >> 1;
            return result;
        }
开发者ID:tarekgh,项目名称:corefxlab,代码行数:10,代码来源:InvariantParser_uint.cs

示例5: TryParseDecimal

        public static bool TryParseDecimal(ReadOnlySpan<byte> text, out decimal value, out int bytesConsumed, EncodingData encoding = default(EncodingData))
        {
            bytesConsumed = 0;
            value = default(decimal);

            if (encoding.IsInvariantUtf8)
            {
                return InvariantUtf8.TryParseDecimal(text, out value, out bytesConsumed);
            }
            else if (encoding.IsInvariantUtf16)
            {
                ReadOnlySpan<char> textChars = text.Cast<byte, char>();
                int charactersConsumed;
                bool result = InvariantUtf16.TryParseDecimal(textChars, out value, out charactersConsumed);
                bytesConsumed = charactersConsumed * sizeof(char);
                return result;
            }

            return false;
        }
开发者ID:AlexGhiondea,项目名称:corefxlab,代码行数:20,代码来源:PrimitiveParser_Decimal.cs

示例6: Int16PositiveHexTests

        public unsafe void Int16PositiveHexTests(string text, int length, short expectedValue, int expectedConsumed)
        {
            byte[] byteBuffer = new Utf8String(text).CopyBytes();
            ReadOnlySpan<byte> byteSpan = new ReadOnlySpan<byte>(byteBuffer);

            char[] charBuffer = text.ToCharArray();
            ReadOnlySpan<char> charSpan = new ReadOnlySpan<char>(charBuffer);

            bool result;
            short actualValue;
            int actualConsumed;

            result = PrimitiveParser.TryParseInt16(byteSpan, out actualValue, out actualConsumed, EncodingData.InvariantUtf8, 'X');

            Assert.True(result);
            Assert.Equal(expectedValue, actualValue);
            Assert.Equal(expectedConsumed, actualConsumed);

            fixed (byte* bytePointer = byteBuffer)
            {
                result = PrimitiveParser.InvariantUtf8.Hex.TryParseInt16(bytePointer, length, out actualValue);

                Assert.True(result);
                Assert.Equal(expectedValue, actualValue);

                result = PrimitiveParser.InvariantUtf8.Hex.TryParseInt16(bytePointer, length, out actualValue, out actualConsumed);

                Assert.True(result);
                Assert.Equal(expectedValue, actualValue);
                Assert.Equal(expectedConsumed, actualConsumed);
            }

            result = PrimitiveParser.InvariantUtf8.Hex.TryParseInt16(byteSpan, out actualValue);

            Assert.True(result);
            Assert.Equal(expectedValue, actualValue);

            result = PrimitiveParser.InvariantUtf8.Hex.TryParseInt16(byteSpan, out actualValue, out actualConsumed);

            Assert.True(result);
            Assert.Equal(expectedValue, actualValue);
            Assert.Equal(expectedConsumed, actualConsumed);

            ReadOnlySpan<byte> utf16ByteSpan = charSpan.Cast<char, byte>();
            result = PrimitiveParser.TryParseInt16(utf16ByteSpan, out actualValue, out actualConsumed, EncodingData.InvariantUtf16, 'X');
            Assert.True(result);
            Assert.Equal(expectedValue, actualValue);
            Assert.Equal(expectedConsumed, actualConsumed / 2);

            fixed (char* charPointer = charBuffer)
            {
                result = PrimitiveParser.InvariantUtf16.Hex.TryParseInt16(charPointer, length, out actualValue);

                Assert.True(result);
                Assert.Equal(expectedValue, actualValue);

                result = PrimitiveParser.InvariantUtf16.Hex.TryParseInt16(charPointer, length, out actualValue, out actualConsumed);

                Assert.True(result);
                Assert.Equal(expectedValue, actualValue);
                Assert.Equal(expectedConsumed, actualConsumed);
            }

            result = PrimitiveParser.InvariantUtf16.Hex.TryParseInt16(charSpan, out actualValue);

            Assert.True(result);
            Assert.Equal(expectedValue, actualValue);

            result = PrimitiveParser.InvariantUtf16.Hex.TryParseInt16(charSpan, out actualValue, out actualConsumed);

            Assert.True(result);
            Assert.Equal(expectedValue, actualValue);
            Assert.Equal(expectedConsumed, actualConsumed);
        }
开发者ID:AlexGhiondea,项目名称:corefxlab,代码行数:74,代码来源:PrimitiveParserInt16Tests.cs

示例7: TryParseBoolean

        public static bool TryParseBoolean(ReadOnlySpan<byte> text, out bool value, out int consumedBytes, EncodingData encoding = default(EncodingData))
        {
            consumedBytes = 0;
            value = default(bool);
            if (text.Length < 1)
            {
                return false;
            }

            if (encoding.IsInvariantUtf8)
            {
                byte firstCodeUnit = text[0];

                if (firstCodeUnit == '1')
                {
                    consumedBytes = 1;
                    value = true;
                    return true;
                }
                else if (firstCodeUnit == '0')
                {
                    consumedBytes = 1;
                    value = false;
                    return true;
                }
                else if (IsTrue(text))
                {
                    consumedBytes = 4;
                    value = true;
                    return true;
                }
                else if (IsFalse(text))
                {
                    consumedBytes = 5;
                    value = false;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            if (encoding.IsInvariantUtf16)
            {
                ReadOnlySpan<char> textChars = text.Cast<byte, char>();
                char firstCodeUnit = textChars[0];

                if (firstCodeUnit == '1')
                {
                    consumedBytes = 2;
                    value = true;
                    return true;
                }
                else if (firstCodeUnit == '0')
                {
                    consumedBytes = 2;
                    value = false;
                    return true;
                }
                else if (IsTrue(textChars))
                {
                    consumedBytes = 8;
                    value = true;
                    return true;
                }
                else if (IsFalse(textChars))
                {
                    consumedBytes = 10;
                    value = false;
                    return true;
                }
                else
                {
                    return false;
                }
            }

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

示例8: ReadOnlyCastOverflow

        public void ReadOnlyCastOverflow()
        {
            var huge = Marshal.AllocHGlobal(new IntPtr(ThreeGiB));
            try
            {
                var span = new ReadOnlySpan<ulong>((void*)huge, ULongThreeGiBLimit);

                var asInt = span.Cast<ulong, int>();
                Assert.Equal(Int32ThreeGiBLimit, asInt.Length);

                var asULong = asInt.Cast<int, ulong>();
                Assert.Equal(ULongThreeGiBLimit, asULong.Length);

                var writable = new Span<ulong>((void*)huge, ULongThreeGiBLimit);
                writable[ULongTwoGiBLimit + 4] = 42;
                Assert.Equal((ulong)42, asULong[ULongTwoGiBLimit + 4]);
            }
            finally
            {
                Marshal.FreeHGlobal(huge);
            }
        }
开发者ID:AlexGhiondea,项目名称:corefxlab,代码行数:22,代码来源:OverflowTests.cs


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