Add DebuggerDisplay for SearchValues (#86559)
authorStephen Toub <stoub@microsoft.com>
Mon, 22 May 2023 01:55:04 +0000 (21:55 -0400)
committerGitHub <noreply@github.com>
Mon, 22 May 2023 01:55:04 +0000 (21:55 -0400)
src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValues.T.cs
src/libraries/System.Private.CoreLib/src/System/SearchValues/SearchValuesDebugView.cs

index 071d4c51495faaf3b084e427fb7ace2f84b08355..3188dabe53b76eff306043be5a4a460b2d8bca1f 100644 (file)
@@ -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>
@@ -80,5 +81,24 @@ namespace System.Buffers
 
             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;
+            }
+        }
     }
 }
index adfad1b278468aefd117ada8c32e3f2a131b6d88..00dda551581247646aa897f8137f56ee7154397c 100644 (file)
@@ -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>?
@@ -13,6 +15,7 @@ namespace System.Buffers
             _values = values;
         }
 
+        [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
         public T[] Values => _values.GetValues();
     }
 }