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