| | | 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.Runtime.CompilerServices; |
| | | 5 | | using System.Runtime.InteropServices; |
| | | 6 | | |
| | | 7 | | namespace System.Buffers |
| | | 8 | | { |
| | | 9 | | internal sealed class RangeCharSearchValues<TShouldUsePacked> : SearchValues<char> |
| | | 10 | | where TShouldUsePacked : struct, SearchValues.IRuntimeConst |
| | | 11 | | { |
| | | 12 | | private readonly char _rangeInclusive; |
| | | 13 | | private char _lowInclusive, _highInclusive; |
| | | 14 | | private readonly uint _lowUint, _highMinusLow; |
| | | 15 | | |
| | 1033 | 16 | | public RangeCharSearchValues(char lowInclusive, char highInclusive) |
| | | 17 | | { |
| | 1033 | 18 | | (_lowInclusive, _rangeInclusive, _highInclusive) = (lowInclusive, (char)(highInclusive - lowInclusive), high |
| | 1033 | 19 | | _lowUint = lowInclusive; |
| | 1033 | 20 | | _highMinusLow = (uint)(highInclusive - lowInclusive); |
| | 1033 | 21 | | } |
| | | 22 | | |
| | | 23 | | internal override char[] GetValues() |
| | | 24 | | { |
| | 0 | 25 | | char[] values = new char[_rangeInclusive + 1]; |
| | | 26 | | |
| | 0 | 27 | | int low = _lowInclusive; |
| | 0 | 28 | | for (int i = 0; i < values.Length; i++) |
| | | 29 | | { |
| | 0 | 30 | | values[i] = (char)(low + i); |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | return values; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 37 | | internal override bool ContainsCore(char value) => |
| | 221652 | 38 | | value - _lowUint <= _highMinusLow; |
| | | 39 | | |
| | | 40 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 41 | | internal override int IndexOfAny(ReadOnlySpan<char> span) => |
| | 4132 | 42 | | (PackedSpanHelpers.PackedIndexOfIsSupported && TShouldUsePacked.Value) |
| | 4132 | 43 | | ? PackedSpanHelpers.IndexOfAnyInRange(ref MemoryMarshal.GetReference(span), _lowInclusive, _rangeInclusi |
| | 4132 | 44 | | : SpanHelpers.NonPackedIndexOfAnyInRangeUnsignedNumber<ushort, SpanHelpers.DontNegate<ushort>>( |
| | 4132 | 45 | | ref Unsafe.As<char, ushort>(ref MemoryMarshal.GetReference(span)), |
| | 4132 | 46 | | _lowInclusive, |
| | 4132 | 47 | | _highInclusive, |
| | 4132 | 48 | | span.Length); |
| | | 49 | | |
| | | 50 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 51 | | internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => |
| | 4132 | 52 | | (PackedSpanHelpers.PackedIndexOfIsSupported && TShouldUsePacked.Value) |
| | 4132 | 53 | | ? PackedSpanHelpers.IndexOfAnyExceptInRange(ref MemoryMarshal.GetReference(span), _lowInclusive, _rangeI |
| | 4132 | 54 | | : SpanHelpers.NonPackedIndexOfAnyInRangeUnsignedNumber<ushort, SpanHelpers.Negate<ushort>>( |
| | 4132 | 55 | | ref Unsafe.As<char, ushort>(ref MemoryMarshal.GetReference(span)), |
| | 4132 | 56 | | _lowInclusive, |
| | 4132 | 57 | | _highInclusive, |
| | 4132 | 58 | | span.Length); |
| | | 59 | | |
| | | 60 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 61 | | internal override int LastIndexOfAny(ReadOnlySpan<char> span) => |
| | 2066 | 62 | | span.LastIndexOfAnyInRange(_lowInclusive, _highInclusive); |
| | | 63 | | |
| | | 64 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 65 | | internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => |
| | 2066 | 66 | | span.LastIndexOfAnyExceptInRange(_lowInclusive, _highInclusive); |
| | | 67 | | } |
| | | 68 | | } |
| | | 69 | | |