From ca14b99065d0b6aad79638c7f50ff3c054093fa6 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Sun, 13 Aug 2023 18:56:28 +0200 Subject: [PATCH 1/9] Improve ArraySegment debugging --- .../src/System/ArraySegment.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs index e4ec370796bf60..47e3fb7ed8855e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; +using System.Text; namespace System { @@ -19,6 +20,8 @@ namespace System // (ie, users could assign a new value to the old location). [Serializable] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] + [DebuggerDisplay("{ToString(),raw}")] + [DebuggerTypeProxy(typeof(ArraySegmentDebugView<>))] #pragma warning disable CA1066 // adding IEquatable implementation could change semantics of code like that in xunit that queries for IEquatable vs enumerating contents public readonly struct ArraySegment : IList, IReadOnlyList #pragma warning restore CA1066 @@ -276,6 +279,41 @@ private void ThrowInvalidOperationIfDefault() } } + /// + /// For , returns a new instance of string that represents the characters pointed to by the segment. + /// Otherwise, returns a with the name of the type and the number of elements. + /// + public override string ToString() + { + if (typeof(T) == typeof(char)) + { + StringBuilder stringBuilder = new StringBuilder(_count); + + for (int i = 0; i < _count; i++) + { + T element = this[i]; + stringBuilder.Append(Unsafe.As(ref element)); + } + + return stringBuilder.ToString(); + } + + return $"System.ArraySegment<{typeof(T).Name}>[{_count}]"; + } + + private sealed class ArraySegmentDebugView + { + private readonly T[] _array; + + public ArraySegmentDebugView(ArraySegment arraySegments) + { + _array = arraySegments.ToArray(); + } + + [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] + public T[] Items => _array; + } + public struct Enumerator : IEnumerator { private readonly T[]? _array; From 451deac7a2ee5298b03d3f6e25cd28d17430cc42 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Sun, 13 Aug 2023 19:10:32 +0200 Subject: [PATCH 2/9] fix build --- .../System.Private.CoreLib/src/System/ArraySegment.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs index 47e3fb7ed8855e..e4b8ec97b01903 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs @@ -301,17 +301,17 @@ public override string ToString() return $"System.ArraySegment<{typeof(T).Name}>[{_count}]"; } - private sealed class ArraySegmentDebugView + private sealed class ArraySegmentDebugView { - private readonly T[] _array; + private readonly U[] _array; - public ArraySegmentDebugView(ArraySegment arraySegments) + public ArraySegmentDebugView(ArraySegment arraySegments) { _array = arraySegments.ToArray(); } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - public T[] Items => _array; + public U[] Items => _array; } public struct Enumerator : IEnumerator From dc0b4ef2a121f2ca219732d560807fb9b3e8f59d Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Mon, 14 Aug 2023 08:29:53 +0200 Subject: [PATCH 3/9] fix remarks --- .../src/System/ArraySegment.cs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs index e4b8ec97b01903..fb5a0b0c66f3b2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs @@ -20,7 +20,7 @@ namespace System // (ie, users could assign a new value to the old location). [Serializable] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - [DebuggerDisplay("{ToString(),raw}")] + [DebuggerDisplay("{DebuggerToString(),nq}")] [DebuggerTypeProxy(typeof(ArraySegmentDebugView<>))] #pragma warning disable CA1066 // adding IEquatable implementation could change semantics of code like that in xunit that queries for IEquatable vs enumerating contents public readonly struct ArraySegment : IList, IReadOnlyList @@ -279,23 +279,11 @@ private void ThrowInvalidOperationIfDefault() } } - /// - /// For , returns a new instance of string that represents the characters pointed to by the segment. - /// Otherwise, returns a with the name of the type and the number of elements. - /// - public override string ToString() + private string DebuggerToString() { if (typeof(T) == typeof(char)) { - StringBuilder stringBuilder = new StringBuilder(_count); - - for (int i = 0; i < _count; i++) - { - T element = this[i]; - stringBuilder.Append(Unsafe.As(ref element)); - } - - return stringBuilder.ToString(); + return new string(new ReadOnlySpan(ref Unsafe.As(ref _array![_offset]), _count)); } return $"System.ArraySegment<{typeof(T).Name}>[{_count}]"; From 18511fcbb12a1270c3f57b76400d7d78eb44147a Mon Sep 17 00:00:00 2001 From: Badre BSAILA <54767641+pedrobsaila@users.noreply.github.com> Date: Mon, 14 Aug 2023 13:41:00 +0200 Subject: [PATCH 4/9] fix remark 2 --- .../src/System/ArraySegment.cs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs index fb5a0b0c66f3b2..8ae8f10f687d7f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs @@ -21,7 +21,7 @@ namespace System [Serializable] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] [DebuggerDisplay("{DebuggerToString(),nq}")] - [DebuggerTypeProxy(typeof(ArraySegmentDebugView<>))] + [DebuggerTypeProxy(typeof(ArraySegmentDebugView))] #pragma warning disable CA1066 // adding IEquatable implementation could change semantics of code like that in xunit that queries for IEquatable vs enumerating contents public readonly struct ArraySegment : IList, IReadOnlyList #pragma warning restore CA1066 @@ -283,23 +283,16 @@ private string DebuggerToString() { if (typeof(T) == typeof(char)) { - return new string(new ReadOnlySpan(ref Unsafe.As(ref _array![_offset]), _count)); + return AsSpan().ToString(); } return $"System.ArraySegment<{typeof(T).Name}>[{_count}]"; } - private sealed class ArraySegmentDebugView + private sealed class ArraySegmentDebugView(ArraySegment array) { - private readonly U[] _array; - - public ArraySegmentDebugView(ArraySegment arraySegments) - { - _array = arraySegments.ToArray(); - } - [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - public U[] Items => _array; + public T[] Items => array.AsSpan().ToArray(); } public struct Enumerator : IEnumerator From 9c7de7c6feae4234b9129af9e8621f72e96ae63a Mon Sep 17 00:00:00 2001 From: Badre BSAILA <54767641+pedrobsaila@users.noreply.github.com> Date: Mon, 14 Aug 2023 14:16:32 +0200 Subject: [PATCH 5/9] fix build --- .../System.Private.CoreLib/src/System/ArraySegment.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs index 8ae8f10f687d7f..e79495b6114474 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs @@ -21,7 +21,7 @@ namespace System [Serializable] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] [DebuggerDisplay("{DebuggerToString(),nq}")] - [DebuggerTypeProxy(typeof(ArraySegmentDebugView))] + [DebuggerTypeProxy(typeof(ArraySegmentDebugView<>))] #pragma warning disable CA1066 // adding IEquatable implementation could change semantics of code like that in xunit that queries for IEquatable vs enumerating contents public readonly struct ArraySegment : IList, IReadOnlyList #pragma warning restore CA1066 @@ -289,10 +289,10 @@ private string DebuggerToString() return $"System.ArraySegment<{typeof(T).Name}>[{_count}]"; } - private sealed class ArraySegmentDebugView(ArraySegment array) + private sealed class ArraySegmentDebugView(ArraySegment array) { [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - public T[] Items => array.AsSpan().ToArray(); + public U[] Items => array.AsSpan().ToArray(); } public struct Enumerator : IEnumerator From 8d7ee676b9791abf94cc7bd1bd38a0634263242b Mon Sep 17 00:00:00 2001 From: Badre BSAILA <54767641+pedrobsaila@users.noreply.github.com> Date: Mon, 14 Aug 2023 14:38:48 +0200 Subject: [PATCH 6/9] fix build 2 --- src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs index e79495b6114474..475c5f806f79e2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs @@ -283,7 +283,7 @@ private string DebuggerToString() { if (typeof(T) == typeof(char)) { - return AsSpan().ToString(); + return this.AsSpan().ToString(); } return $"System.ArraySegment<{typeof(T).Name}>[{_count}]"; From 84f43faaaea239ba781e121e2af9a7c129861a4a Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Mon, 14 Aug 2023 20:41:55 +0200 Subject: [PATCH 7/9] drop generics --- .../System.Private.CoreLib/src/System/ArraySegment.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs index 475c5f806f79e2..6be26d24e40599 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs @@ -21,7 +21,7 @@ namespace System [Serializable] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] [DebuggerDisplay("{DebuggerToString(),nq}")] - [DebuggerTypeProxy(typeof(ArraySegmentDebugView<>))] + [DebuggerTypeProxy(typeof(ArraySegment<>.ArraySegmentDebugView))] #pragma warning disable CA1066 // adding IEquatable implementation could change semantics of code like that in xunit that queries for IEquatable vs enumerating contents public readonly struct ArraySegment : IList, IReadOnlyList #pragma warning restore CA1066 @@ -289,10 +289,10 @@ private string DebuggerToString() return $"System.ArraySegment<{typeof(T).Name}>[{_count}]"; } - private sealed class ArraySegmentDebugView(ArraySegment array) + private sealed class ArraySegmentDebugView(ArraySegment array) { [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - public U[] Items => array.AsSpan().ToArray(); + public T[] Items => array.AsSpan().ToArray(); } public struct Enumerator : IEnumerator From f220fdb90856f1987ab94cf8cd171a667eef3004 Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Sat, 28 Oct 2023 12:13:43 +0200 Subject: [PATCH 8/9] use simpler solution --- .../System.Private.CoreLib/src/System/ArraySegment.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs index 6be26d24e40599..7fb46d31e51595 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs @@ -6,7 +6,6 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -using System.Text; namespace System { @@ -20,8 +19,8 @@ namespace System // (ie, users could assign a new value to the old location). [Serializable] [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - [DebuggerDisplay("{DebuggerToString(),nq}")] - [DebuggerTypeProxy(typeof(ArraySegment<>.ArraySegmentDebugView))] + [DebuggerTypeProxy(typeof(ICollectionDebugView<>))] + [DebuggerDisplay("Count = {Count}")] #pragma warning disable CA1066 // adding IEquatable implementation could change semantics of code like that in xunit that queries for IEquatable vs enumerating contents public readonly struct ArraySegment : IList, IReadOnlyList #pragma warning restore CA1066 @@ -289,12 +288,6 @@ private string DebuggerToString() return $"System.ArraySegment<{typeof(T).Name}>[{_count}]"; } - private sealed class ArraySegmentDebugView(ArraySegment array) - { - [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - public T[] Items => array.AsSpan().ToArray(); - } - public struct Enumerator : IEnumerator { private readonly T[]? _array; From 788687d2088d08335fd69168f96716aeddfcda1e Mon Sep 17 00:00:00 2001 From: pedrobsaila Date: Sat, 28 Oct 2023 12:14:55 +0200 Subject: [PATCH 9/9] clean DebuggerToString --- .../System.Private.CoreLib/src/System/ArraySegment.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs index 7fb46d31e51595..5561c8ec3a4200 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArraySegment.cs @@ -278,16 +278,6 @@ private void ThrowInvalidOperationIfDefault() } } - private string DebuggerToString() - { - if (typeof(T) == typeof(char)) - { - return this.AsSpan().ToString(); - } - - return $"System.ArraySegment<{typeof(T).Name}>[{_count}]"; - } - public struct Enumerator : IEnumerator { private readonly T[]? _array;