< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 21
Coverable lines: 21
Total lines: 83
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
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%
ContainsCore(...)100%110%
GetValues()100%110%
IndexOfAny(...)0%440%
IndexOfAnyExcept(...)100%110%
LastIndexOfAny(...)0%440%
LastIndexOfAnyExcept(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/SearchValues/Strings/StringSearchValuesBase.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.Collections.Generic;
 5using System.Diagnostics;
 6
 7namespace System.Buffers
 8{
 9    /// <summary>
 10    /// Implements the base <see cref="SearchValues{T}"/> {Last}IndexOfAny{Except} operations.
 11    /// While these operations are exposed such that you can call string[].IndexOfAny(searchValues),
 12    /// they are not expected to be used in performance-critical paths.
 13    /// <see cref="MemoryExtensions.IndexOfAny(ReadOnlySpan{char}, SearchValues{string})"/> is the main
 14    /// reason why someone would create an instance of <see cref="string"/> <see cref="SearchValues{T}"/>.
 15    /// </summary>
 16    internal abstract class StringSearchValuesBase : SearchValues<string>
 17    {
 18        private readonly HashSet<string>? _uniqueValues;
 19
 20        /// <summary>
 21        /// This exists to allow <see cref="SingleStringSearchValuesThreeChars{TValueLength, TCaseSensitivity}"/> to avo
 22        /// </summary>
 023        protected bool HasUniqueValues => _uniqueValues is not null;
 24
 025        public StringSearchValuesBase(HashSet<string>? uniqueValues) =>
 026            _uniqueValues = uniqueValues;
 27
 28        internal override bool ContainsCore(string value)
 29        {
 030            Debug.Assert(_uniqueValues is not null, "ContainsCore should be overridden if uniqueValues weren't provided.
 031            return _uniqueValues.Contains(value);
 32        }
 33
 34        internal override string[] GetValues()
 35        {
 036            Debug.Assert(_uniqueValues is not null, "GetValues should be overridden if uniqueValues weren't provided.");
 037            string[] values = new string[_uniqueValues.Count];
 038            _uniqueValues.CopyTo(values);
 039            return values;
 40        }
 41
 42        internal sealed override int IndexOfAny(ReadOnlySpan<string> span) =>
 043            IndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(span);
 44
 45        internal sealed override int IndexOfAnyExcept(ReadOnlySpan<string> span) =>
 046            IndexOfAny<IndexOfAnyAsciiSearcher.Negate>(span);
 47
 48        internal sealed override int LastIndexOfAny(ReadOnlySpan<string> span) =>
 049            LastIndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(span);
 50
 51        internal sealed override int LastIndexOfAnyExcept(ReadOnlySpan<string> span) =>
 052            LastIndexOfAny<IndexOfAnyAsciiSearcher.Negate>(span);
 53
 54        private int IndexOfAny<TNegator>(ReadOnlySpan<string> span)
 55            where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
 56        {
 057            for (int i = 0; i < span.Length; i++)
 58            {
 059                if (TNegator.NegateIfNeeded(ContainsCore(span[i])))
 60                {
 061                    return i;
 62                }
 63            }
 64
 065            return -1;
 66        }
 67
 68        private int LastIndexOfAny<TNegator>(ReadOnlySpan<string> span)
 69            where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
 70        {
 071            for (int i = span.Length - 1; i >= 0; i--)
 72            {
 073                if (TNegator.NegateIfNeeded(ContainsCore(span[i])))
 74                {
 075                    return i;
 76                }
 77            }
 78
 079            return -1;
 80        }
 81    }
 82}
 83