diff options
author | Ahson Khan <ahkha@microsoft.com> | 2018-03-04 04:55:31 +0300 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2018-03-04 18:39:17 +0300 |
commit | 99aa9df1e2df834e45a1d8eb0d04007e2e736bdb (patch) | |
tree | f1b8457cc591cdb4b035ba47d50105d2a672be5e /src | |
parent | 8185792cdbc9f8c260928e3e3df5365be426b8e0 (diff) |
Fix MemoryDebugView and override Memory.ToString similar to Span (#16732)
* Fix MemoryDebugView and add Memory.ToString similar to Span
* Simplify implementation of ToString
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Diffstat (limited to 'src')
3 files changed, 29 insertions, 34 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Memory.cs b/src/System.Private.CoreLib/shared/System/Memory.cs index fca015f5e..82c66d6ea 100644 --- a/src/System.Private.CoreLib/shared/System/Memory.cs +++ b/src/System.Private.CoreLib/shared/System/Memory.cs @@ -18,8 +18,8 @@ namespace System /// Memory represents a contiguous region of arbitrary memory similar to <see cref="Span{T}"/>. /// Unlike <see cref="Span{T}"/>, it is not a byref-like type. /// </summary> - [DebuggerDisplay("{DebuggerDisplay,nq}")] [DebuggerTypeProxy(typeof(MemoryDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] public readonly struct Memory<T> { // NOTE: With the current implementation, Memory<T> and ReadOnlyMemory<T> must have the same layout, @@ -145,9 +145,6 @@ namespace System public static implicit operator ReadOnlyMemory<T>(Memory<T> memory) => Unsafe.As<Memory<T>, ReadOnlyMemory<T>>(ref memory); - //Debugger Display = {T[length]} - private string DebuggerDisplay => string.Format("{{{0}[{1}]}}", typeof(T).Name, _length); - /// <summary> /// Returns an empty <see cref="Memory{T}"/> /// </summary> @@ -164,6 +161,19 @@ namespace System public bool IsEmpty => _length == 0; /// <summary> + /// For <see cref="Memory{Char}"/>, returns a new instance of string that represents the characters pointed to by the memory. + /// Otherwise, returns a <see cref="string"/> with the name of the type and the number of elements. + /// </summary> + public override string ToString() + { + if (typeof(T) == typeof(char)) + { + return (_object is string str) ? str.Substring(_index, _length) : Span.ToString(); + } + return string.Format("System.Memory<{0}>[{1}]", typeof(T).Name, _length); + } + + /// <summary> /// Forms a slice out of the given memory, beginning at 'start'. /// </summary> /// <param name="start">The index at which to begin this slice.</param> diff --git a/src/System.Private.CoreLib/shared/System/MemoryDebugView.cs b/src/System.Private.CoreLib/shared/System/MemoryDebugView.cs index b1ed88199..f56a67c63 100644 --- a/src/System.Private.CoreLib/shared/System/MemoryDebugView.cs +++ b/src/System.Private.CoreLib/shared/System/MemoryDebugView.cs @@ -22,31 +22,6 @@ namespace System } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] - public T[] Items - { - // This is a work around since we cannot use _memory.ToArray() due to - // https://devdiv.visualstudio.com/DevDiv/_workitems?id=286592 - get - { - if (MemoryMarshal.TryGetArray(_memory, out ArraySegment<T> segment)) - { - T[] array = new T[_memory.Length]; - Array.Copy(segment.Array, segment.Offset, array, 0, array.Length); - return array; - } - - if (typeof(T) == typeof(char) && - MemoryMarshal.TryGetString((ReadOnlyMemory<char>)(object)_memory, out string text, out int start, out int length)) - { - return (T[])(object)text.Substring(start, length).ToCharArray(); - } - -#if FEATURE_PORTABLE_SPAN - return SpanHelpers.PerTypeValues<T>.EmptyArray; -#else - return Array.Empty<T>(); -#endif // FEATURE_PORTABLE_SPAN - } - } + public T[] Items => _memory.ToArray(); } } diff --git a/src/System.Private.CoreLib/shared/System/ReadOnlyMemory.cs b/src/System.Private.CoreLib/shared/System/ReadOnlyMemory.cs index 166a204ed..90a9decf6 100644 --- a/src/System.Private.CoreLib/shared/System/ReadOnlyMemory.cs +++ b/src/System.Private.CoreLib/shared/System/ReadOnlyMemory.cs @@ -18,8 +18,8 @@ namespace System /// Represents a contiguous region of memory, similar to <see cref="ReadOnlySpan{T}"/>. /// Unlike <see cref="ReadOnlySpan{T}"/>, it is not a byref-like type. /// </summary> - [DebuggerDisplay("{DebuggerDisplay,nq}")] [DebuggerTypeProxy(typeof(MemoryDebugView<>))] + [DebuggerDisplay("{ToString(),raw}")] public readonly struct ReadOnlyMemory<T> { // NOTE: With the current implementation, Memory<T> and ReadOnlyMemory<T> must have the same layout, @@ -97,9 +97,6 @@ namespace System _length = length; } - //Debugger Display = {T[length]} - private string DebuggerDisplay => string.Format("{{{0}[{1}]}}", typeof(T).Name, _length); - /// <summary> /// Defines an implicit conversion of an array to a <see cref="ReadOnlyMemory{T}"/> /// </summary> @@ -126,6 +123,19 @@ namespace System public bool IsEmpty => _length == 0; /// <summary> + /// For <see cref="ReadOnlyMemory{Char}"/>, returns a new instance of string that represents the characters pointed to by the memory. + /// Otherwise, returns a <see cref="string"/> with the name of the type and the number of elements. + /// </summary> + public override string ToString() + { + if (typeof(T) == typeof(char)) + { + return (_object is string str) ? str.Substring(_index, _length) : Span.ToString(); + } + return string.Format("System.ReadOnlyMemory<{0}>[{1}]", typeof(T).Name, _length); + } + + /// <summary> /// Forms a slice out of the given memory, beginning at 'start'. /// </summary> /// <param name="start">The index at which to begin this slice.</param> |