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


C# MemoryBlock.Utf8NullTerminatedStartsWith方法代码示例

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


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

示例1: TestComparisons

        private unsafe void TestComparisons(string heapValue, int offset, string value, bool unicode = false)
        {
            byte[] heap;
            MetadataStringDecoder decoder = MetadataStringDecoder.DefaultUTF8;

            fixed (byte* heapPtr = (heap = Encoding.UTF8.GetBytes(heapValue)))
            {
                var block = new MemoryBlock(heapPtr, heap.Length);
                string heapSubstr = GetStringHeapValue(heapValue, offset);

                // compare:
                if (!unicode)
                {
                    int actualCmp = block.CompareUtf8NullTerminatedStringWithAsciiString(offset, value);
                    int expectedCmp = StringComparer.Ordinal.Compare(heapSubstr, value);
                    Assert.Equal(Math.Sign(expectedCmp), Math.Sign(actualCmp));
                }

                // equals:
                bool actualEq = block.Utf8NullTerminatedEquals(offset, value, decoder);
                bool expectedEq = StringComparer.Ordinal.Equals(heapSubstr, value);
                Assert.Equal(expectedEq, actualEq);

                // starts with:
                bool actualSW = block.Utf8NullTerminatedStartsWith(offset, value, decoder);
                bool expectedSW = heapSubstr.StartsWith(value, StringComparison.Ordinal);
                Assert.Equal(actualSW, expectedSW);
            }
        }
开发者ID:hitomi333,项目名称:corefx,代码行数:29,代码来源:MemoryBlockTests.cs

示例2: ComparisonToInvalidByteSequenceMatchesFallback

        public unsafe void ComparisonToInvalidByteSequenceMatchesFallback()
        {
            MetadataStringDecoder decoder = MetadataStringDecoder.DefaultUTF8;

            // dangling lead byte
            byte[] buffer;
            fixed (byte* ptr = (buffer = new byte[] { 0xC0 }))
            {
                var block = new MemoryBlock(ptr, buffer.Length);
                Assert.False(block.Utf8NullTerminatedStartsWith(0, new string((char)0xC0, 1), decoder));
                Assert.False(block.Utf8NullTerminatedEquals(0, new string((char)0xC0, 1), decoder));
                Assert.True(block.Utf8NullTerminatedStartsWith(0, "\uFFFD", decoder));
                Assert.True(block.Utf8NullTerminatedEquals(0, "\uFFFD", decoder));
            }

            // overlong encoding
            fixed (byte* ptr = (buffer = new byte[] { (byte)'a', 0xC0, 0xAF, (byte)'b', 0x0 }))
            {
                var block = new MemoryBlock(ptr, buffer.Length);
                Assert.False(block.Utf8NullTerminatedStartsWith(0, "a\\", decoder));
                Assert.False(block.Utf8NullTerminatedEquals(0, "a\\b", decoder));
                Assert.True(block.Utf8NullTerminatedStartsWith(0, "a\uFFFD", decoder));
                Assert.True(block.Utf8NullTerminatedEquals(0, "a\uFFFD\uFFFDb", decoder));
            }
        }
开发者ID:hitomi333,项目名称:corefx,代码行数:25,代码来源:MemoryBlockTests.cs

示例3: TestComparison

        private static unsafe void TestComparison(MemoryBlock block, int offset, string value, string heapSubstr, MetadataStringDecoder decoder, bool ignoreCase)
        {
            // equals:
            bool actualEq = block.Utf8NullTerminatedEquals(offset, value, decoder, terminator: '\0', ignoreCase: ignoreCase);
            bool expectedEq = string.Equals(heapSubstr, value, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
            Assert.Equal(expectedEq, actualEq);

            // starts with:
            bool actualSW = block.Utf8NullTerminatedStartsWith(offset, value, decoder, terminator: '\0', ignoreCase: ignoreCase);
            bool expectedSW = heapSubstr.StartsWith(value, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);
            Assert.Equal(actualSW, expectedSW);
        }
开发者ID:ChuangYang,项目名称:corefx,代码行数:12,代码来源:MemoryBlockTests.cs


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