< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 35
Coverable lines: 35
Total lines: 112
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%110%
Equals(...)100%110%
Equals(...)100%110%
Equals(...)100%110%
Equals(...)0%220%
Equals(...)100%110%
Equals(...)100%110%
StartsWith(...)100%110%
StartsWith(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/MetadataStringComparer.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5using System.Runtime.CompilerServices;
 6
 7namespace 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") &amp;&amp;
 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") &amp;&amp;
 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)
 053        {
 054            Debug.Assert(reader != null);
 055            _reader = reader;
 056        }
 57
 58        public bool Equals(StringHandle handle, string value)
 059        {
 060            return Equals(handle, value, ignoreCase: false);
 061        }
 62
 63        public bool Equals(StringHandle handle, string value, bool ignoreCase)
 064        {
 065            ArgumentNullException.ThrowIfNull(value);
 66
 067            return _reader.StringHeap.Equals(handle, value, _reader.UTF8Decoder, ignoreCase);
 068        }
 69
 70        public bool Equals(NamespaceDefinitionHandle handle, string value)
 071        {
 072            return Equals(handle, value, ignoreCase: false);
 073        }
 74
 75        public bool Equals(NamespaceDefinitionHandle handle, string value, bool ignoreCase)
 076        {
 077            ArgumentNullException.ThrowIfNull(value);
 78
 079            if (handle.HasFullName)
 080            {
 081                return _reader.StringHeap.Equals(handle.GetFullName(), value, _reader.UTF8Decoder, ignoreCase);
 82            }
 83
 084            return value == _reader.NamespaceCache.GetFullName(handle);
 085        }
 86
 87        public bool Equals(DocumentNameBlobHandle handle, string value)
 088        {
 089            return Equals(handle, value, ignoreCase: false);
 090        }
 91
 92        public bool Equals(DocumentNameBlobHandle handle, string value, bool ignoreCase)
 093        {
 094            ArgumentNullException.ThrowIfNull(value);
 95
 096            return _reader.BlobHeap.DocumentNameEquals(handle, value, ignoreCase);
 097        }
 98
 99        public bool StartsWith(StringHandle handle, string value)
 0100        {
 0101            return StartsWith(handle, value, ignoreCase: false);
 0102        }
 103
 104        public bool StartsWith(StringHandle handle, string value, bool ignoreCase)
 0105        {
 0106            ArgumentNullException.ThrowIfNull(value);
 107
 0108            return _reader.StringHeap.StartsWith(handle, value, _reader.UTF8Decoder, ignoreCase);
 0109        }
 110    }
 111}
 112