| | | 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 | | |
| | | 7 | | namespace System.Buffers |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides an immutable, read-only set of values optimized for efficient searching. |
| | | 11 | | /// Instances are created by <see cref="SearchValues.Create(ReadOnlySpan{byte})"/>, <see cref="SearchValues.Create(R |
| | | 12 | | /// <see cref="SearchValues.Create(ReadOnlySpan{string}, StringComparison)"/>. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <typeparam name="T">The type of the values to search for.</typeparam> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// <see cref="SearchValues{T}"/> are optimized for situations where the same set of values is frequently used for s |
| | | 17 | | /// </remarks> |
| | | 18 | | [DebuggerDisplay("{DebuggerDisplay,nq}")] |
| | | 19 | | [DebuggerTypeProxy(typeof(SearchValuesDebugView<>))] |
| | | 20 | | public class SearchValues<T> where T : IEquatable<T>? |
| | | 21 | | { |
| | | 22 | | // Only CoreLib can create derived types |
| | 35698 | 23 | | private protected SearchValues() { } |
| | | 24 | | |
| | | 25 | | /// <summary>Used by <see cref="DebuggerDisplay"/>s and <see cref="DebuggerTypeProxyAttribute"/>s for <see cref= |
| | 0 | 26 | | internal virtual T[] GetValues() => throw new UnreachableException(); |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Searches for the specified value and returns true if found. If not found, returns false. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="value">The value to search for.</param> |
| | | 32 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3809756 | 33 | | public bool Contains(T value) => ContainsCore(value); |
| | | 34 | | |
| | 0 | 35 | | internal virtual bool ContainsCore(T value) => throw new UnreachableException(); |
| | | 36 | | |
| | 0 | 37 | | internal virtual int IndexOfAny(ReadOnlySpan<T> span) => throw new UnreachableException(); |
| | 0 | 38 | | internal virtual int IndexOfAnyExcept(ReadOnlySpan<T> span) => throw new UnreachableException(); |
| | 0 | 39 | | internal virtual int LastIndexOfAny(ReadOnlySpan<T> span) => throw new UnreachableException(); |
| | 0 | 40 | | internal virtual int LastIndexOfAnyExcept(ReadOnlySpan<T> span) => throw new UnreachableException(); |
| | | 41 | | |
| | 12182 | 42 | | internal virtual bool ContainsAny(ReadOnlySpan<T> span) => IndexOfAny(span) >= 0; |
| | 12182 | 43 | | internal virtual bool ContainsAnyExcept(ReadOnlySpan<T> span) => IndexOfAnyExcept(span) >= 0; |
| | | 44 | | |
| | | 45 | | // This is only implemented and used by SearchValues<string>. |
| | 0 | 46 | | internal virtual int IndexOfAnyMultiString(ReadOnlySpan<char> span) => throw new UnreachableException(); |
| | | 47 | | |
| | | 48 | | private string DebuggerDisplay |
| | | 49 | | { |
| | | 50 | | get |
| | | 51 | | { |
| | 0 | 52 | | T[] values = GetValues(); |
| | | 53 | | |
| | 0 | 54 | | string display = $"{GetType().Name}, Count = {values.Length}"; |
| | 0 | 55 | | if (values.Length > 0) |
| | | 56 | | { |
| | 0 | 57 | | display += ", Values = "; |
| | 0 | 58 | | display += typeof(T) == typeof(char) ? |
| | 0 | 59 | | "\"" + new string(Unsafe.As<T[], char[]>(ref values)) + "\"" : |
| | 0 | 60 | | string.Join(",", values); |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | return display; |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |