|
| 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 | +using System.Runtime.InteropServices; |
| 7 | +using System.Runtime.Intrinsics.Arm; |
| 8 | +using System.Runtime.Intrinsics.Wasm; |
| 9 | +using System.Runtime.Intrinsics.X86; |
| 10 | + |
| 11 | +namespace System.Buffers |
| 12 | +{ |
| 13 | + internal sealed class Any2CharPackedIgnoreCaseSearchValues : SearchValues<char> |
| 14 | + { |
| 15 | + // While this most commonly applies to ASCII letters, it also works for other values that differ by 0x20 (e.g. "[]{}" => "{}"). |
| 16 | + // _e0 and _e1 are therefore not necessarily lower case ASCII letters, but just the higher values (the ones with the 0x20 bit set). |
| 17 | + private readonly char _e0, _e1; |
| 18 | + private readonly uint _uint0, _uint1; |
| 19 | + private IndexOfAnyAsciiSearcher.AsciiState _state; |
| 20 | + |
| 21 | + public Any2CharPackedIgnoreCaseSearchValues(char value0, char value1) |
| 22 | + { |
| 23 | + Debug.Assert((value0 | 0x20) == value0 && char.IsAscii(value0)); |
| 24 | + Debug.Assert((value1 | 0x20) == value1 && char.IsAscii(value1)); |
| 25 | + |
| 26 | + (_e0, _e1) = (value0, value1); |
| 27 | + (_uint0, _uint1) = (value0, value1); |
| 28 | + IndexOfAnyAsciiSearcher.ComputeAsciiState([(char)(_e0 & ~0x20), _e0, (char)(_e1 & ~0x20), _e1], out _state); |
| 29 | + } |
| 30 | + |
| 31 | + internal override char[] GetValues() => |
| 32 | + [(char)(_e0 & ~0x20), _e0, (char)(_e1 & ~0x20), _e1]; |
| 33 | + |
| 34 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 35 | + internal override bool ContainsCore(char value) |
| 36 | + { |
| 37 | + uint lowerCase = (uint)(value | 0x20); |
| 38 | + return lowerCase == _uint0 || lowerCase == _uint1; |
| 39 | + } |
| 40 | + |
| 41 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 42 | + [CompExactlyDependsOn(typeof(Sse2))] |
| 43 | + internal override int IndexOfAny(ReadOnlySpan<char> span) => |
| 44 | + PackedSpanHelpers.IndexOfAnyIgnoreCase(ref MemoryMarshal.GetReference(span), _e0, _e1, span.Length); |
| 45 | + |
| 46 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 47 | + [CompExactlyDependsOn(typeof(Sse2))] |
| 48 | + internal override int IndexOfAnyExcept(ReadOnlySpan<char> span) => |
| 49 | + PackedSpanHelpers.IndexOfAnyExceptIgnoreCase(ref MemoryMarshal.GetReference(span), _e0, _e1, span.Length); |
| 50 | + |
| 51 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 52 | + [CompExactlyDependsOn(typeof(Ssse3))] |
| 53 | + [CompExactlyDependsOn(typeof(AdvSimd))] |
| 54 | + [CompExactlyDependsOn(typeof(PackedSimd))] |
| 55 | + internal override int LastIndexOfAny(ReadOnlySpan<char> span) => |
| 56 | + IndexOfAnyAsciiSearcher.LastIndexOfAny<IndexOfAnyAsciiSearcher.DontNegate, IndexOfAnyAsciiSearcher.Default>( |
| 57 | + ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), span.Length, ref _state); |
| 58 | + |
| 59 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 60 | + [CompExactlyDependsOn(typeof(Ssse3))] |
| 61 | + [CompExactlyDependsOn(typeof(AdvSimd))] |
| 62 | + [CompExactlyDependsOn(typeof(PackedSimd))] |
| 63 | + internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) => |
| 64 | + IndexOfAnyAsciiSearcher.LastIndexOfAny<IndexOfAnyAsciiSearcher.Negate, IndexOfAnyAsciiSearcher.Default>( |
| 65 | + ref Unsafe.As<char, short>(ref MemoryMarshal.GetReference(span)), span.Length, ref _state); |
| 66 | + } |
| 67 | +} |
0 commit comments