Move MemoryExtensions.TryGetString to MemoryMarshal and remove TryGetArray (#16692)
authorAhson Khan <ahkha@microsoft.com>
Thu, 1 Mar 2018 21:18:37 +0000 (13:18 -0800)
committerGitHub <noreply@github.com>
Thu, 1 Mar 2018 21:18:37 +0000 (13:18 -0800)
* Remove Span.NonGenerics and update leftover AsRoS -> AsSpan changes

* Move MemoryExtensions.TryGetString to MemoryMarshal and remove TryGetArray

* Move TryGetString to common MemoryMarshal.cs

* Remove the `this` keyword, not an extension method

src/mscorlib/shared/System/IO/MemoryStream.cs
src/mscorlib/shared/System/IO/TextReader.cs
src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs
src/mscorlib/shared/System/Memory.cs
src/mscorlib/shared/System/MemoryDebugView.cs
src/mscorlib/shared/System/MemoryExtensions.Fast.cs
src/mscorlib/shared/System/Runtime/InteropServices/MemoryMarshal.cs
src/mscorlib/src/System/IO/Stream.cs

index 8e573b7..ffe7f60 100644 (file)
@@ -448,7 +448,7 @@ namespace System.IO
                 // something other than an array and this is a MemoryStream-derived type that doesn't override Read(Span<byte>) will
                 // it then fall back to doing the ArrayPool/copy behavior.
                 return new ValueTask<int>(
-                    destination.TryGetArray(out ArraySegment<byte> destinationArray) ?
+                    MemoryMarshal.TryGetArray(destination, out ArraySegment<byte> destinationArray) ?
                         Read(destinationArray.Array, destinationArray.Offset, destinationArray.Count) :
                         Read(destination.Span));
             }
index c4727cd..eb94dd7 100644 (file)
@@ -7,6 +7,7 @@ using System.Threading;
 using System.Threading.Tasks;
 using System.Diagnostics;
 using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
 using System.Buffers;
 
 namespace System.IO
@@ -251,7 +252,7 @@ namespace System.IO
         }
 
         public virtual ValueTask<int> ReadAsync(Memory<char> buffer, CancellationToken cancellationToken = default(CancellationToken)) =>
-            new ValueTask<int>(buffer.TryGetArray(out ArraySegment<char> array) ?
+            new ValueTask<int>(MemoryMarshal.TryGetArray(buffer, out ArraySegment<char> array) ?
                 ReadAsync(array.Array, array.Offset, array.Count) :
                 Task<int>.Factory.StartNew(state =>
                 {
@@ -289,7 +290,7 @@ namespace System.IO
         }
 
         public virtual ValueTask<int> ReadBlockAsync(Memory<char> buffer, CancellationToken cancellationToken = default(CancellationToken)) =>
-            new ValueTask<int>(buffer.TryGetArray(out ArraySegment<char> array) ?
+            new ValueTask<int>(MemoryMarshal.TryGetArray(buffer, out ArraySegment<char> array) ?
                 ReadBlockAsync(array.Array, array.Offset, array.Count) :
                 Task<int>.Factory.StartNew(state =>
                 {
index 2f0f34a..d1a1315 100644 (file)
@@ -510,7 +510,7 @@ namespace System.IO
                 // something other than an array and this is an UnmanagedMemoryStream-derived type that doesn't override Read(Span<byte>) will
                 // it then fall back to doing the ArrayPool/copy behavior.
                 return new ValueTask<int>(
-                    destination.TryGetArray(out ArraySegment<byte> destinationArray) ?
+                    MemoryMarshal.TryGetArray(destination, out ArraySegment<byte> destinationArray) ?
                         Read(destinationArray.Array, destinationArray.Offset, destinationArray.Count) :
                         Read(destination.Span));
             }
index 4a1ce4d..fca015f 100644 (file)
@@ -310,40 +310,6 @@ namespace System
         }
 
         /// <summary>
-        /// Get an array segment from the underlying memory.
-        /// If unable to get the array segment, return false with a default array segment.
-        /// </summary>
-        public bool TryGetArray(out ArraySegment<T> arraySegment)
-        {
-            if (_index < 0)
-            {
-                if (((OwnedMemory<T>)_object).TryGetArray(out var segment))
-                {
-                    arraySegment = new ArraySegment<T>(segment.Array, segment.Offset + (_index & RemoveOwnedFlagBitMask), _length);
-                    return true;
-                }
-            }
-            else if (_object is T[] arr)
-            {
-                arraySegment = new ArraySegment<T>(arr, _index, _length);
-                return true;
-            }
-
-            if (_length == 0)
-            {
-#if FEATURE_PORTABLE_SPAN
-                arraySegment = new ArraySegment<T>(SpanHelpers.PerTypeValues<T>.EmptyArray);
-#else
-                arraySegment = ArraySegment<T>.Empty;
-#endif // FEATURE_PORTABLE_SPAN
-                return true;
-            }
-
-            arraySegment = default(ArraySegment<T>);
-            return false;
-        }
-
-        /// <summary>
         /// Copies the contents from the memory into a new array.  This heap
         /// allocates, so should generally be avoided, however it is sometimes
         /// necessary to bridge the gap with APIs written in terms of arrays.
index fa508b2..b1ed881 100644 (file)
@@ -36,7 +36,7 @@ namespace System
                 }
 
                 if (typeof(T) == typeof(char) &&
-                    ((ReadOnlyMemory<char>)(object)_memory).TryGetString(out string text, out int start, out int length))
+                    MemoryMarshal.TryGetString((ReadOnlyMemory<char>)(object)_memory, out string text, out int start, out int length))
                 {
                     return (T[])(object)text.Substring(start, length).ToCharArray();
                 }
index 4b330b7..56dd203 100644 (file)
@@ -512,29 +512,5 @@ namespace System
 
             return new ReadOnlyMemory<char>(text, start, length);
         }
-
-        /// <summary>Attempts to get the underlying <see cref="string"/> from a <see cref="ReadOnlyMemory{T}"/>.</summary>
-        /// <param name="readOnlyMemory">The memory that may be wrapping a <see cref="string"/> object.</param>
-        /// <param name="text">The string.</param>
-        /// <param name="start">The starting location in <paramref name="text"/>.</param>
-        /// <param name="length">The number of items in <paramref name="text"/>.</param>
-        /// <returns></returns>
-        public static bool TryGetString(this ReadOnlyMemory<char> readOnlyMemory, out string text, out int start, out int length)
-        {
-            if (readOnlyMemory.GetObjectStartLength(out int offset, out int count) is string s)
-            {
-                text = s;
-                start = offset;
-                length = count;
-                return true;
-            }
-            else
-            {
-                text = null;
-                start = 0;
-                length = 0;
-                return false;
-            }
-        }
     }
 }
index 6544081..316ce12 100644 (file)
@@ -98,5 +98,29 @@ namespace System.Runtime.InteropServices
             for (int i = 0; i < memory.Length; i++)
                 yield return memory.Span[i];
         }
+
+        /// <summary>Attempts to get the underlying <see cref="string"/> from a <see cref="ReadOnlyMemory{T}"/>.</summary>
+        /// <param name="readOnlyMemory">The memory that may be wrapping a <see cref="string"/> object.</param>
+        /// <param name="text">The string.</param>
+        /// <param name="start">The starting location in <paramref name="text"/>.</param>
+        /// <param name="length">The number of items in <paramref name="text"/>.</param>
+        /// <returns></returns>
+        public static bool TryGetString(ReadOnlyMemory<char> readOnlyMemory, out string text, out int start, out int length)
+        {
+            if (readOnlyMemory.GetObjectStartLength(out int offset, out int count) is string s)
+            {
+                text = s;
+                start = offset;
+                length = count;
+                return true;
+            }
+            else
+            {
+                text = null;
+                start = 0;
+                length = 0;
+                return false;
+            }
+        }
     }
 }
index 1304a2e..ec29dc2 100644 (file)
@@ -376,7 +376,7 @@ namespace System.IO
 
         public virtual ValueTask<int> ReadAsync(Memory<byte> destination, CancellationToken cancellationToken = default(CancellationToken))
         {
-            if (destination.TryGetArray(out ArraySegment<byte> array))
+            if (MemoryMarshal.TryGetArray(destination, out ArraySegment<byte> array))
             {
                 return new ValueTask<int>(ReadAsync(array.Array, array.Offset, array.Count, cancellationToken));
             }