diff options
author | Ahson Khan <ahkha@microsoft.com> | 2018-03-02 00:18:37 +0300 |
---|---|---|
committer | Ahson Khan <ahkha@microsoft.com> | 2018-03-02 07:26:31 +0300 |
commit | 979dbf50d906c4ef9a9a132df52d2b27c777b100 (patch) | |
tree | 44d0a80a88f658460794d6ffab041845f050c201 | |
parent | 2cadd05bdc4d1f475ad9cc8230c0d206e2b18c41 (diff) |
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 <dotnet-bot@microsoft.com>
7 files changed, 30 insertions, 63 deletions
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<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)); } 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<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 => { 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<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)); } 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 @@ -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. 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<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(); } 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<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; - } - } } } 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]; } + + /// <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; + } + } } } |