本文整理汇总了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);
}
}
示例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));
}
}
示例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);
}