< Summary

Information
Class: System.Buffers.BitmapCharSearchValues
Assembly: System.Private.CoreLib
File(s): D:\runner\runtime\src\libraries\System.Private.CoreLib\src\System\SearchValues\BitmapCharSearchValues.cs
Line coverage
80%
Covered lines: 25
Uncovered lines: 6
Coverable lines: 31
Total lines: 104
Line coverage: 80.6%
Branch coverage
75%
Covered branches: 12
Total branches: 16
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%22100%
GetValues()0%440%
ContainsCore(...)100%11100%
Contains(...)100%22100%
IndexOfAny(...)100%44100%
IndexOfAnyExcept(...)100%11100%
LastIndexOfAny(...)100%44100%
LastIndexOfAnyExcept(...)100%11100%

File(s)

D:\runner\runtime\src\libraries\System.Private.CoreLib\src\System\SearchValues\BitmapCharSearchValues.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;
 6using System.Runtime.CompilerServices;
 7
 8namespace System.Buffers
 9{
 10    internal sealed class BitmapCharSearchValues : SearchValues<char>
 11    {
 12        private readonly uint[] _bitmap;
 13
 75214        public BitmapCharSearchValues(ReadOnlySpan<char> values, int maxInclusive)
 15        {
 75216            Debug.Assert(maxInclusive <= char.MaxValue);
 17
 75218            _bitmap = new uint[maxInclusive / 32 + 1];
 19
 43518620            foreach (char c in values)
 21            {
 21684122                _bitmap[c >> 5] |= 1u << c;
 23            }
 75224        }
 25
 26        internal override char[] GetValues()
 27        {
 028            var chars = new List<char>();
 029            uint[] bitmap = _bitmap;
 30
 031            for (int i = 0; i < _bitmap.Length * 32; i++)
 32            {
 033                if (Contains(bitmap, i))
 34                {
 035                    chars.Add((char)i);
 36                }
 37            }
 38
 039            return chars.ToArray();
 40        }
 41
 42        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 43        internal override bool ContainsCore(char value) =>
 4720644            Contains(_bitmap, value);
 45
 46        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 47        private static bool Contains(uint[] bitmap, int value)
 48        {
 38021849            uint offset = (uint)(value >> 5);
 38021850            return offset < (uint)bitmap.Length && (bitmap[offset] & (1u << value)) != 0;
 51        }
 52
 53        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 54        internal override int IndexOfAny(ReadOnlySpan<char> span) =>
 300855            IndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(span);
 56
 57        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 58        internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) =>
 300859            IndexOfAny<IndexOfAnyAsciiSearcher.Negate>(span);
 60
 61        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 62        internal override int LastIndexOfAny(ReadOnlySpan<char> span) =>
 150463            LastIndexOfAny<IndexOfAnyAsciiSearcher.DontNegate>(span);
 64
 65        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 66        internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) =>
 150467            LastIndexOfAny<IndexOfAnyAsciiSearcher.Negate>(span);
 68
 69        private int IndexOfAny<TNegator>(ReadOnlySpan<char> span)
 70            where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
 71        {
 601672            uint[] bitmap = _bitmap;
 73
 56355474            for (int i = 0; i < span.Length; i++)
 75            {
 28061476                char c = span[i];
 28061477                if (TNegator.NegateIfNeeded(Contains(bitmap, c)))
 78                {
 485379                    return i;
 80                }
 81            }
 82
 116383            return -1;
 84        }
 85
 86        private int LastIndexOfAny<TNegator>(ReadOnlySpan<char> span)
 87            where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
 88        {
 300889            uint[] bitmap = _bitmap;
 300890            int searchSpaceLength = span.Length;
 91
 5267292            while (--searchSpaceLength >= 0)
 93            {
 5239894                char c = span[searchSpaceLength];
 5239895                if (TNegator.NegateIfNeeded(Contains(bitmap, c)))
 96                {
 97                    break;
 98                }
 99            }
 100
 3008101            return searchSpaceLength;
 102        }
 103    }
 104}