/
MetadataStringComparer.cs
123 lines (107 loc) · 3.72 KB
/
MetadataStringComparer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace System.Reflection.Metadata
{
/// <summary>
/// Provides string comparison helpers to query strings in metadata while
/// avoiding allocation where possible.
/// </summary>
///
/// <remarks>
/// No allocation is performed unless both the handle argument and the
/// value argument contain non-ascii text.
///
/// Obtain instances using <see cref="MetadataReader.StringComparer"/>.
///
/// A default-initialized instance is useless and behaves as a null reference.
///
/// The code is optimized such that there is no additional overhead in
/// re-obtaining a comparer over hoisting it in to a local.
///
/// That is to say that a construct like:
///
/// <code>
/// if (reader.StringComparer.Equals(typeDef.Namespace, "System") &&
/// reader.StringComparer.Equals(typeDef.Name, "Object")
/// {
/// // found System.Object
/// }
/// </code>
///
/// is no less efficient than:
///
/// <code>
/// var comparer = reader.StringComparer;
/// if (comparer.Equals(typeDef.Namespace, "System") &&
/// comparer.Equals(typeDef.Name, "Object")
/// {
/// // found System.Object
/// }
/// </code>
///
/// The choice between them is therefore one of style and not performance.
/// </remarks>
public readonly struct MetadataStringComparer
{
private readonly MetadataReader _reader;
internal MetadataStringComparer(MetadataReader reader)
{
Debug.Assert(reader != null);
_reader = reader;
}
public bool Equals(StringHandle handle, string value)
{
return Equals(handle, value, ignoreCase: false);
}
public bool Equals(StringHandle handle, string value, bool ignoreCase)
{
if (value == null)
{
Throw.ValueArgumentNull();
}
return _reader.StringHeap.Equals(handle, value, _reader.UTF8Decoder, ignoreCase);
}
public bool Equals(NamespaceDefinitionHandle handle, string value)
{
return Equals(handle, value, ignoreCase: false);
}
public bool Equals(NamespaceDefinitionHandle handle, string value, bool ignoreCase)
{
if (value == null)
{
Throw.ValueArgumentNull();
}
if (handle.HasFullName)
{
return _reader.StringHeap.Equals(handle.GetFullName(), value, _reader.UTF8Decoder, ignoreCase);
}
return value == _reader.NamespaceCache.GetFullName(handle);
}
public bool Equals(DocumentNameBlobHandle handle, string value)
{
return Equals(handle, value, ignoreCase: false);
}
public bool Equals(DocumentNameBlobHandle handle, string value, bool ignoreCase)
{
if (value == null)
{
Throw.ValueArgumentNull();
}
return _reader.BlobHeap.DocumentNameEquals(handle, value, ignoreCase);
}
public bool StartsWith(StringHandle handle, string value)
{
return StartsWith(handle, value, ignoreCase: false);
}
public bool StartsWith(StringHandle handle, string value, bool ignoreCase)
{
if (value == null)
{
Throw.ValueArgumentNull();
}
return _reader.StringHeap.StartsWith(handle, value, _reader.UTF8Decoder, ignoreCase);
}
}
}