diff options
Diffstat (limited to 'src/System.Private.CoreLib/shared/System/MemoryExtensions.cs')
-rw-r--r-- | src/System.Private.CoreLib/shared/System/MemoryExtensions.cs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/System.Private.CoreLib/shared/System/MemoryExtensions.cs b/src/System.Private.CoreLib/shared/System/MemoryExtensions.cs index 739bc3145..f0937c448 100644 --- a/src/System.Private.CoreLib/shared/System/MemoryExtensions.cs +++ b/src/System.Private.CoreLib/shared/System/MemoryExtensions.cs @@ -183,6 +183,56 @@ namespace System } /// <summary> + /// Searches for the specified value and returns true if found. If not found, returns false. Values are compared using IEquatable{T}.Equals(T). + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="span">The span to search.</param> + /// <param name="value">The value to search for.</param> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool Contains<T>(this Span<T> span, T value) + where T : IEquatable<T> + { + if (typeof(T) == typeof(byte)) + return SpanHelpers.Contains( + ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), + Unsafe.As<T, byte>(ref value), + span.Length); + + if (typeof(T) == typeof(char)) + return SpanHelpers.Contains( + ref Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), + Unsafe.As<T, char>(ref value), + span.Length); + + return SpanHelpers.Contains(ref MemoryMarshal.GetReference(span), value, span.Length); + } + + /// <summary> + /// Searches for the specified value and returns true if found. If not found, returns false. Values are compared using IEquatable{T}.Equals(T). + /// </summary> + /// <typeparam name="T"></typeparam> + /// <param name="span">The span to search.</param> + /// <param name="value">The value to search for.</param> + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool Contains<T>(this ReadOnlySpan<T> span, T value) + where T : IEquatable<T> + { + if (typeof(T) == typeof(byte)) + return SpanHelpers.Contains( + ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), + Unsafe.As<T, byte>(ref value), + span.Length); + + if (typeof(T) == typeof(char)) + return SpanHelpers.Contains( + ref Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), + Unsafe.As<T, char>(ref value), + span.Length); + + return SpanHelpers.Contains(ref MemoryMarshal.GetReference(span), value, span.Length); + } + + /// <summary> /// Searches for the specified value and returns the index of its first occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). /// </summary> /// <param name="span">The span to search.</param> @@ -196,6 +246,7 @@ namespace System ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), Unsafe.As<T, byte>(ref value), span.Length); + if (typeof(T) == typeof(char)) return SpanHelpers.IndexOf( ref Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), @@ -238,6 +289,7 @@ namespace System ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), Unsafe.As<T, byte>(ref value), span.Length); + if (typeof(T) == typeof(char)) return SpanHelpers.LastIndexOf( ref Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), @@ -322,6 +374,7 @@ namespace System ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), Unsafe.As<T, byte>(ref value), span.Length); + if (typeof(T) == typeof(char)) return SpanHelpers.IndexOf( ref Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), @@ -364,6 +417,7 @@ namespace System ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)), Unsafe.As<T, byte>(ref value), span.Length); + if (typeof(T) == typeof(char)) return SpanHelpers.LastIndexOf( ref Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)), |