Change the Span ToString semantics to return the contents for T=char (#16143)
authorAhson Khan <ahkha@microsoft.com>
Thu, 1 Feb 2018 08:03:49 +0000 (00:03 -0800)
committerJan Kotas <jkotas@microsoft.com>
Thu, 1 Feb 2018 08:03:49 +0000 (00:03 -0800)
src/mscorlib/shared/System/ReadOnlySpan.cs
src/mscorlib/shared/System/Span.cs

index 56d0ad9..a7d1736 100644 (file)
@@ -251,9 +251,21 @@ namespace System
         }
 
         /// <summary>
-        /// Returns a <see cref="String"/> with the name of the type and the number of elements
+        /// For <see cref="ReadOnlySpan{Char}"/>, returns a new instance of string that represents the characters pointed to by the span.
+        /// Otherwise, returns a <see cref="String"/> with the name of the type and the number of elements.
         /// </summary>
-        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<T, char>(ref _pointer.Value))
+                        return new string(src, 0, _length);
+                }
+            }
+            return string.Format("System.ReadOnlySpan<{0}>[{1}]", typeof(T).Name, _length);
+        }
 
         /// <summary>
         /// Defines an implicit conversion of an array to a <see cref="ReadOnlySpan{T}"/>
index e14dac7..fd5ad26 100644 (file)
@@ -326,9 +326,21 @@ namespace System
         }
 
         /// <summary>
-        /// Returns a <see cref="String"/> with the name of the type and the number of elements
+        /// For <see cref="Span{Char}"/>, returns a new instance of string that represents the characters pointed to by the span.
+        /// Otherwise, returns a <see cref="String"/> with the name of the type and the number of elements.
         /// </summary>
-        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<T, char>(ref _pointer.Value))
+                        return new string(src, 0, _length);
+                }
+            }
+            return string.Format("System.Span<{0}>[{1}]", typeof(T).Name, _length);
+        }
 
         /// <summary>
         /// Defines an implicit conversion of an array to a <see cref="Span{T}"/>