From 979dbf50d906c4ef9a9a132df52d2b27c777b100 Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Thu, 1 Mar 2018 13:18:37 -0800 Subject: Move MemoryExtensions.TryGetString to MemoryMarshal and remove TryGetArray (#16692) * 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 Signed-off-by: dotnet-bot --- .../shared/System/IO/MemoryStream.cs | 2 +- .../shared/System/IO/TextReader.cs | 5 ++-- .../shared/System/IO/UnmanagedMemoryStream.cs | 2 +- src/System.Private.CoreLib/shared/System/Memory.cs | 34 ---------------------- .../shared/System/MemoryDebugView.cs | 2 +- .../shared/System/MemoryExtensions.Fast.cs | 24 --------------- .../Runtime/InteropServices/MemoryMarshal.cs | 24 +++++++++++++++ 7 files changed, 30 insertions(+), 63 deletions(-) (limited to 'src') diff --git a/src/System.Private.CoreLib/shared/System/IO/MemoryStream.cs b/src/System.Private.CoreLib/shared/System/IO/MemoryStream.cs index 8e573b749..ffe7f6093 100644 --- a/src/System.Private.CoreLib/shared/System/IO/MemoryStream.cs +++ b/src/System.Private.CoreLib/shared/System/IO/MemoryStream.cs @@ -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) will // it then fall back to doing the ArrayPool/copy behavior. return new ValueTask( - destination.TryGetArray(out ArraySegment destinationArray) ? + MemoryMarshal.TryGetArray(destination, out ArraySegment destinationArray) ? Read(destinationArray.Array, destinationArray.Offset, destinationArray.Count) : Read(destination.Span)); } diff --git a/src/System.Private.CoreLib/shared/System/IO/TextReader.cs b/src/System.Private.CoreLib/shared/System/IO/TextReader.cs index c4727cd84..eb94dd759 100644 --- a/src/System.Private.CoreLib/shared/System/IO/TextReader.cs +++ b/src/System.Private.CoreLib/shared/System/IO/TextReader.cs @@ -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 ReadAsync(Memory buffer, CancellationToken cancellationToken = default(CancellationToken)) => - new ValueTask(buffer.TryGetArray(out ArraySegment array) ? + new ValueTask(MemoryMarshal.TryGetArray(buffer, out ArraySegment array) ? ReadAsync(array.Array, array.Offset, array.Count) : Task.Factory.StartNew(state => { @@ -289,7 +290,7 @@ namespace System.IO } public virtual ValueTask ReadBlockAsync(Memory buffer, CancellationToken cancellationToken = default(CancellationToken)) => - new ValueTask(buffer.TryGetArray(out ArraySegment array) ? + new ValueTask(MemoryMarshal.TryGetArray(buffer, out ArraySegment array) ? ReadBlockAsync(array.Array, array.Offset, array.Count) : Task.Factory.StartNew(state => { diff --git a/src/System.Private.CoreLib/shared/System/IO/UnmanagedMemoryStream.cs b/src/System.Private.CoreLib/shared/System/IO/UnmanagedMemoryStream.cs index 2f0f34afe..d1a13156a 100644 --- a/src/System.Private.CoreLib/shared/System/IO/UnmanagedMemoryStream.cs +++ b/src/System.Private.CoreLib/shared/System/IO/UnmanagedMemoryStream.cs @@ -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) will // it then fall back to doing the ArrayPool/copy behavior. return new ValueTask( - destination.TryGetArray(out ArraySegment destinationArray) ? + MemoryMarshal.TryGetArray(destination, out ArraySegment destinationArray) ? Read(destinationArray.Array, destinationArray.Offset, destinationArray.Count) : Read(destination.Span)); } diff --git a/src/System.Private.CoreLib/shared/System/Memory.cs b/src/System.Private.CoreLib/shared/System/Memory.cs index 4a1ce4dc9..fca015f5e 100644 --- a/src/System.Private.CoreLib/shared/System/Memory.cs +++ b/src/System.Private.CoreLib/shared/System/Memory.cs @@ -309,40 +309,6 @@ namespace System return memoryHandle; } - /// - /// Get an array segment from the underlying memory. - /// If unable to get the array segment, return false with a default array segment. - /// - public bool TryGetArray(out ArraySegment arraySegment) - { - if (_index < 0) - { - if (((OwnedMemory)_object).TryGetArray(out var segment)) - { - arraySegment = new ArraySegment(segment.Array, segment.Offset + (_index & RemoveOwnedFlagBitMask), _length); - return true; - } - } - else if (_object is T[] arr) - { - arraySegment = new ArraySegment(arr, _index, _length); - return true; - } - - if (_length == 0) - { -#if FEATURE_PORTABLE_SPAN - arraySegment = new ArraySegment(SpanHelpers.PerTypeValues.EmptyArray); -#else - arraySegment = ArraySegment.Empty; -#endif // FEATURE_PORTABLE_SPAN - return true; - } - - arraySegment = default(ArraySegment); - return false; - } - /// /// Copies the contents from the memory into a new array. This heap /// allocates, so should generally be avoided, however it is sometimes diff --git a/src/System.Private.CoreLib/shared/System/MemoryDebugView.cs b/src/System.Private.CoreLib/shared/System/MemoryDebugView.cs index fa508b286..b1ed88199 100644 --- a/src/System.Private.CoreLib/shared/System/MemoryDebugView.cs +++ b/src/System.Private.CoreLib/shared/System/MemoryDebugView.cs @@ -36,7 +36,7 @@ namespace System } if (typeof(T) == typeof(char) && - ((ReadOnlyMemory)(object)_memory).TryGetString(out string text, out int start, out int length)) + MemoryMarshal.TryGetString((ReadOnlyMemory)(object)_memory, out string text, out int start, out int length)) { return (T[])(object)text.Substring(start, length).ToCharArray(); } diff --git a/src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs b/src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs index 4b330b7b1..56dd203e1 100644 --- a/src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs +++ b/src/System.Private.CoreLib/shared/System/MemoryExtensions.Fast.cs @@ -512,29 +512,5 @@ namespace System return new ReadOnlyMemory(text, start, length); } - - /// Attempts to get the underlying from a . - /// The memory that may be wrapping a object. - /// The string. - /// The starting location in . - /// The number of items in . - /// - public static bool TryGetString(this ReadOnlyMemory 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; - } - } } } diff --git a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/MemoryMarshal.cs b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/MemoryMarshal.cs index 6544081df..316ce12aa 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/MemoryMarshal.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/MemoryMarshal.cs @@ -98,5 +98,29 @@ namespace System.Runtime.InteropServices for (int i = 0; i < memory.Length; i++) yield return memory.Span[i]; } + + /// Attempts to get the underlying from a . + /// The memory that may be wrapping a object. + /// The string. + /// The starting location in . + /// The number of items in . + /// + public static bool TryGetString(ReadOnlyMemory 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; + } + } } } -- cgit v1.2.3