From 2c79366939091114afdd652e90eaa8477f3fb77c Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Thu, 1 Feb 2018 00:03:49 -0800 Subject: [PATCH] Change the Span ToString semantics to return the contents for T=char (#16143) --- src/mscorlib/shared/System/ReadOnlySpan.cs | 16 ++++++++++++++-- src/mscorlib/shared/System/Span.cs | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/mscorlib/shared/System/ReadOnlySpan.cs b/src/mscorlib/shared/System/ReadOnlySpan.cs index 56d0ad9..a7d1736 100644 --- a/src/mscorlib/shared/System/ReadOnlySpan.cs +++ b/src/mscorlib/shared/System/ReadOnlySpan.cs @@ -251,9 +251,21 @@ namespace System } /// - /// Returns a with the name of the type and the number of elements + /// For , returns a new instance of string that represents the characters pointed to by the span. + /// Otherwise, returns a with the name of the type and the number of elements. /// - public override string ToString() => string.Format("System.ReadOnlySpan<{0}>[{1}]", typeof(T).Name, _length); + public override string ToString() + { + if (typeof(T) == typeof(char)) + { + unsafe + { + fixed (char* src = &Unsafe.As(ref _pointer.Value)) + return new string(src, 0, _length); + } + } + return string.Format("System.ReadOnlySpan<{0}>[{1}]", typeof(T).Name, _length); + } /// /// Defines an implicit conversion of an array to a diff --git a/src/mscorlib/shared/System/Span.cs b/src/mscorlib/shared/System/Span.cs index e14dac7..fd5ad26 100644 --- a/src/mscorlib/shared/System/Span.cs +++ b/src/mscorlib/shared/System/Span.cs @@ -326,9 +326,21 @@ namespace System } /// - /// Returns a with the name of the type and the number of elements + /// For , returns a new instance of string that represents the characters pointed to by the span. + /// Otherwise, returns a with the name of the type and the number of elements. /// - public override string ToString() => string.Format("System.Span<{0}>[{1}]", typeof(T).Name, _length); + public override string ToString() + { + if (typeof(T) == typeof(char)) + { + unsafe + { + fixed (char* src = &Unsafe.As(ref _pointer.Value)) + return new string(src, 0, _length); + } + } + return string.Format("System.Span<{0}>[{1}]", typeof(T).Name, _length); + } /// /// Defines an implicit conversion of an array to a -- 2.7.4