Skip to content

Commit d2d51c8

Browse files
authored
Add DebuggerDisplay for SearchValues (#86559)
1 parent 8f04ae1 commit d2d51c8

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValues.T.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ namespace System.Buffers
1414
/// <remarks>
1515
/// <see cref="SearchValues{T}"/> are optimized for situations where the same set of values is frequently used for searching at runtime.
1616
/// </remarks>
17+
[DebuggerDisplay("{DebuggerDisplay,nq}")]
1718
[DebuggerTypeProxy(typeof(SearchValuesDebugView<>))]
1819
public class SearchValues<T> where T : IEquatable<T>?
1920
{
2021
// Only CoreLib can create derived types
2122
private protected SearchValues() { }
2223

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

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

8182
return values.LastIndexOfAnyExcept(span);
8283
}
84+
85+
private string DebuggerDisplay
86+
{
87+
get
88+
{
89+
T[] values = GetValues();
90+
91+
string display = $"{GetType().Name}, Count={values.Length}";
92+
if (values.Length > 0)
93+
{
94+
display += ", Values=";
95+
display += typeof(T) == typeof(char) ?
96+
"\"" + new string(Unsafe.As<T[], char[]>(ref values)) + "\"" :
97+
string.Join(",", values);
98+
}
99+
100+
return display;
101+
}
102+
}
83103
}
84104
}

src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValuesDebugView.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics;
5+
46
namespace System.Buffers
57
{
68
internal sealed class SearchValuesDebugView<T> where T : IEquatable<T>?
@@ -13,6 +15,7 @@ public SearchValuesDebugView(SearchValues<T> values)
1315
_values = values;
1416
}
1517

18+
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
1619
public T[] Values => _values.GetValues();
1720
}
1821
}

0 commit comments

Comments
 (0)