Skip to content

Commit

Permalink
Audit MemoryExtensions.IndexOf variants (#75754)
Browse files Browse the repository at this point in the history
* Add missing char specialisation to LastIndexOf(Span, ROS)

This method is updated to match LastIndexOf(ROS, ROS)

* Forward LastIndexOfAny(Span, ROS) to (ROS, ROS) version

This is consistent with IndexOfAny(Span, ROS)

* Add missing special cases for {Last}IndexOfAny<byte>(ROS, ROS)
  • Loading branch information
Rob-Hague authored Oct 26, 2022
1 parent e4471c1 commit 3e40074
Showing 1 changed file with 95 additions and 47 deletions.
142 changes: 95 additions & 47 deletions src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,25 @@ ref Unsafe.As<T, long>(ref MemoryMarshal.GetReference(span)),
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int LastIndexOf<T>(this Span<T> span, ReadOnlySpan<T> value) where T : IEquatable<T>?
{
if (Unsafe.SizeOf<T>() == sizeof(byte) && RuntimeHelpers.IsBitwiseEquatable<T>())
return SpanHelpers.LastIndexOf(
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)),
span.Length,
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)),
value.Length);
if (RuntimeHelpers.IsBitwiseEquatable<T>())
{
if (Unsafe.SizeOf<T>() == sizeof(byte))
{
return SpanHelpers.LastIndexOf(
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)),
span.Length,
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(value)),
value.Length);
}
else if (Unsafe.SizeOf<T>() == sizeof(char))
{
return SpanHelpers.LastIndexOf(
ref Unsafe.As<T, char>(ref MemoryMarshal.GetReference(span)),
span.Length,
ref Unsafe.As<T, char>(ref MemoryMarshal.GetReference(value)),
value.Length);
}
}

return SpanHelpers.LastIndexOf<T>(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(value), value.Length);
}
Expand Down Expand Up @@ -1574,23 +1587,50 @@ public static int IndexOfAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value
{
if (Unsafe.SizeOf<T>() == sizeof(byte))
{
ref byte spanRef = ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span));
ref byte valueRef = ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values));
if (values.Length == 2)
{
return SpanHelpers.IndexOfAnyValueType(
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)),
valueRef,
Unsafe.Add(ref valueRef, 1),
span.Length);
}
else if (values.Length == 3)
switch (values.Length)
{
return SpanHelpers.IndexOfAnyValueType(
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)),
valueRef,
Unsafe.Add(ref valueRef, 1),
Unsafe.Add(ref valueRef, 2),
span.Length);
case 0:
return -1;

case 1:
return SpanHelpers.IndexOfValueType(ref spanRef, valueRef, span.Length);

case 2:
return SpanHelpers.IndexOfAnyValueType(
ref spanRef,
valueRef,
Unsafe.Add(ref valueRef, 1),
span.Length);

case 3:
return SpanHelpers.IndexOfAnyValueType(
ref spanRef,
valueRef,
Unsafe.Add(ref valueRef, 1),
Unsafe.Add(ref valueRef, 2),
span.Length);

#if !MONO // We don't have a mono overload for 4 values
case 4:
return SpanHelpers.IndexOfAnyValueType(
ref spanRef,
valueRef,
Unsafe.Add(ref valueRef, 1),
Unsafe.Add(ref valueRef, 2),
Unsafe.Add(ref valueRef, 3),
span.Length);
#endif
case 5:
return SpanHelpers.IndexOfAnyValueType(
ref spanRef,
valueRef,
Unsafe.Add(ref valueRef, 1),
Unsafe.Add(ref valueRef, 2),
Unsafe.Add(ref valueRef, 3),
Unsafe.Add(ref valueRef, 4),
span.Length);
}
}

Expand Down Expand Up @@ -1751,18 +1791,8 @@ ref Unsafe.As<T, short>(ref MemoryMarshal.GetReference(span)),
/// </summary>
/// <param name="span">The span to search.</param>
/// <param name="values">The set of values to search for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int LastIndexOfAny<T>(this Span<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>?
{
if (Unsafe.SizeOf<T>() == sizeof(byte) && RuntimeHelpers.IsBitwiseEquatable<T>())
return SpanHelpers.LastIndexOfAny(
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)),
span.Length,
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values)),
values.Length);

return SpanHelpers.LastIndexOfAny(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length);
}
=> LastIndexOfAny((ReadOnlySpan<T>)span, values);

/// <summary>
/// Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator. If not found, returns -1.
Expand Down Expand Up @@ -1843,23 +1873,41 @@ public static int LastIndexOfAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> v
{
if (Unsafe.SizeOf<T>() == sizeof(byte))
{
ref byte spanRef = ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span));
ref byte valueRef = ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(values));
if (values.Length == 2)
{
return SpanHelpers.LastIndexOfAnyValueType(
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)),
valueRef,
Unsafe.Add(ref valueRef, 1),
span.Length);
}
else if (values.Length == 3)
switch (values.Length)
{
return SpanHelpers.LastIndexOfAnyValueType(
ref Unsafe.As<T, byte>(ref MemoryMarshal.GetReference(span)),
valueRef,
Unsafe.Add(ref valueRef, 1),
Unsafe.Add(ref valueRef, 2),
span.Length);
case 0:
return -1;

case 1:
return SpanHelpers.LastIndexOfValueType(ref spanRef, valueRef, span.Length);

case 2:
return SpanHelpers.LastIndexOfAnyValueType(
ref spanRef,
valueRef,
Unsafe.Add(ref valueRef, 1),
span.Length);

case 3:
return SpanHelpers.LastIndexOfAnyValueType(
ref spanRef,
valueRef,
Unsafe.Add(ref valueRef, 1),
Unsafe.Add(ref valueRef, 2),
span.Length);

#if !MONO // We don't have a mono overload for 4 values
case 4:
return SpanHelpers.LastIndexOfAnyValueType(
ref spanRef,
valueRef,
Unsafe.Add(ref valueRef, 1),
Unsafe.Add(ref valueRef, 2),
Unsafe.Add(ref valueRef, 3),
span.Length);
#endif
}
}

Expand Down

0 comments on commit 3e40074

Please sign in to comment.