diff options
Diffstat (limited to 'src/System.Private.CoreLib/shared/System/Runtime')
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Runtime/InteropServices/MemoryMarshal.cs | 24 |
1 files changed, 24 insertions, 0 deletions
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; + } + } } } |