Skip to content

Commit

Permalink
Add DebuggerDisplay for SearchValues (#86559)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored May 22, 2023
1 parent 8f04ae1 commit d2d51c8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ namespace System.Buffers
/// <remarks>
/// <see cref="SearchValues{T}"/> are optimized for situations where the same set of values is frequently used for searching at runtime.
/// </remarks>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
[DebuggerTypeProxy(typeof(SearchValuesDebugView<>))]
public class SearchValues<T> where T : IEquatable<T>?
{
// Only CoreLib can create derived types
private protected SearchValues() { }

/// <summary>Used by <see cref="SearchValuesDebugView{T}"/>.</summary>
/// <summary>Used by <see cref="DebuggerDisplay"/>s and <see cref="DebuggerTypeProxyAttribute"/>s for <see cref="SearchValues{T}"/>.</summary>
internal virtual T[] GetValues() => throw new UnreachableException();

/// <summary>
Expand Down Expand Up @@ -80,5 +81,24 @@ internal static int LastIndexOfAnyExcept(ReadOnlySpan<T> span, SearchValues<T> v

return values.LastIndexOfAnyExcept(span);
}

private string DebuggerDisplay
{
get
{
T[] values = GetValues();

string display = $"{GetType().Name}, Count={values.Length}";
if (values.Length > 0)
{
display += ", Values=";
display += typeof(T) == typeof(char) ?
"\"" + new string(Unsafe.As<T[], char[]>(ref values)) + "\"" :
string.Join(",", values);
}

return display;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace System.Buffers
{
internal sealed class SearchValuesDebugView<T> where T : IEquatable<T>?
Expand All @@ -13,6 +15,7 @@ public SearchValuesDebugView(SearchValues<T> values)
_values = values;
}

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public T[] Values => _values.GetValues();
}
}

0 comments on commit d2d51c8

Please sign in to comment.