public static class SpanExtensions
{
- public static int IndexOf<T>(this Span<T> span, T value) where T:struct, IEquatable<T> { throw null; }
- public static int IndexOf<T>(this Span<T> span, ReadOnlySpan<T> value) where T : struct, IEquatable<T> { throw null; }
- public static int IndexOf(this Span<byte> span, byte value) { throw null; }
- public static int IndexOf(this Span<byte> span, ReadOnlySpan<byte> value) { throw null; }
+ public static int IndexOf<T>(this Span<T> span, T value) where T : IEquatable<T> { throw null; }
+ public static int IndexOf<T>(this Span<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { throw null; }
public static int IndexOfAny(this Span<byte> span, byte value0, byte value1) { throw null; }
public static int IndexOfAny(this Span<byte> span, byte value0, byte value1, byte value2) { throw null; }
public static int IndexOfAny(this Span<byte> span, ReadOnlySpan<byte> values) { throw null; }
- public static bool SequenceEqual<T>(this Span<T> first, ReadOnlySpan<T> second) where T:struct, IEquatable<T> { throw null; }
- public static bool SequenceEqual(this Span<byte> first, ReadOnlySpan<byte> second) { throw null; }
+ public static bool SequenceEqual<T>(this Span<T> first, ReadOnlySpan<T> second) where T : IEquatable<T> { throw null; }
- public static bool StartsWith<T>(this Span<T> span, ReadOnlySpan<T> value) where T : struct, IEquatable<T> { throw null; }
- public static bool StartsWith(this Span<byte> span, ReadOnlySpan<byte> value) { throw null; }
+ public static bool StartsWith<T>(this Span<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { throw null; }
public static Span<byte> AsBytes<T>(this Span<T> source) where T : struct { throw null; }
public static void CopyTo<T>(this T[] array, Span<T> destination) { throw null; }
- public static int IndexOf<T>(this ReadOnlySpan<T> span, T value) where T : struct, IEquatable<T> { throw null; }
- public static int IndexOf<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : struct, IEquatable<T> { throw null; }
- public static int IndexOf(this ReadOnlySpan<byte> span, byte value) { throw null; }
- public static int IndexOf(this ReadOnlySpan<byte> span, ReadOnlySpan<byte> value) { throw null; }
+ public static int IndexOf<T>(this ReadOnlySpan<T> span, T value) where T : IEquatable<T> { throw null; }
+ public static int IndexOf<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { throw null; }
public static int IndexOfAny(this ReadOnlySpan<byte> span, byte value0, byte value1) { throw null; }
public static int IndexOfAny(this ReadOnlySpan<byte> span, byte value0, byte value1, byte value2) { throw null; }
public static int IndexOfAny(this ReadOnlySpan<byte> span, ReadOnlySpan<byte> values) { throw null; }
- public static bool SequenceEqual<T>(this ReadOnlySpan<T> first, ReadOnlySpan<T> second) where T : struct, IEquatable<T> { throw null; }
- public static bool SequenceEqual(this ReadOnlySpan<byte> first, ReadOnlySpan<byte> second) { throw null; }
+ public static bool SequenceEqual<T>(this ReadOnlySpan<T> first, ReadOnlySpan<T> second) where T : IEquatable<T> { throw null; }
- public static bool StartsWith<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : struct, IEquatable<T> { throw null; }
- public static bool StartsWith(this ReadOnlySpan<byte> span, ReadOnlySpan<byte> value) { throw null; }
+ public static bool StartsWith<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : IEquatable<T> { throw null; }
public static ReadOnlySpan<byte> AsBytes<T>(this ReadOnlySpan<T> source) where T : struct { throw null; }
/// <param name="value">The value to search for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOf<T>(this Span<T> span, T value)
- where T:struct, IEquatable<T>
+ where T : IEquatable<T>
{
+ if (typeof(T) == typeof(byte))
+ return SpanHelpers.IndexOf(
+ ref Unsafe.As<T, byte>(ref span.DangerousGetPinnableReference()),
+ Unsafe.As<T, byte>(ref value),
+ span.Length);
return SpanHelpers.IndexOf<T>(ref span.DangerousGetPinnableReference(), value, span.Length);
}
/// <summary>
- /// Searches for the specified value and returns the index of its first occurrence. If not found, returns -1.
- /// </summary>
- /// <param name="span">The span to search.</param>
- /// <param name="value">The value to search for.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int IndexOf(this Span<byte> span, byte value)
- {
- return SpanHelpers.IndexOf(ref span.DangerousGetPinnableReference(), value, span.Length);
- }
-
- /// <summary>
/// Searches for the specified sequence 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>
/// <param name="value">The sequence to search for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOf<T>(this Span<T> span, ReadOnlySpan<T> value)
- where T : struct, IEquatable<T>
+ where T : IEquatable<T>
{
+ if (typeof(T) == typeof(byte))
+ return SpanHelpers.IndexOf(
+ ref Unsafe.As<T, byte>(ref span.DangerousGetPinnableReference()),
+ span.Length,
+ ref Unsafe.As<T, byte>(ref value.DangerousGetPinnableReference()),
+ value.Length);
return SpanHelpers.IndexOf<T>(ref span.DangerousGetPinnableReference(), span.Length, ref value.DangerousGetPinnableReference(), value.Length);
}
/// <summary>
- /// Searches for the specified sequence and returns the index of its first occurrence. If not found, returns -1.
- /// </summary>
- /// <param name="span">The span to search.</param>
- /// <param name="value">The sequence to search for.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int IndexOf(this Span<byte> span, ReadOnlySpan<byte> value)
- {
- return SpanHelpers.IndexOf(ref span.DangerousGetPinnableReference(), span.Length, ref value.DangerousGetPinnableReference(), value.Length);
- }
-
- /// <summary>
/// Determines whether two sequences are equal by comparing the elements using IEquatable<T>.Equals(T).
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool SequenceEqual<T>(this Span<T> first, ReadOnlySpan<T> second)
- where T:struct, IEquatable<T>
- {
- int length = first.Length;
- return length == second.Length && SpanHelpers.SequenceEqual(ref first.DangerousGetPinnableReference(), ref second.DangerousGetPinnableReference(), length);
- }
-
- /// <summary>
- /// Determines whether two sequences are equal by comparing the elements.
- /// </summary>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool SequenceEqual(this Span<byte> first, ReadOnlySpan<byte> second)
+ where T : IEquatable<T>
{
int length = first.Length;
+ if (typeof(T) == typeof(byte))
+ return length == second.Length &&
+ SpanHelpers.SequenceEqual(
+ ref Unsafe.As<T, byte>(ref first.DangerousGetPinnableReference()),
+ ref Unsafe.As<T, byte>(ref second.DangerousGetPinnableReference()),
+ length);
return length == second.Length && SpanHelpers.SequenceEqual(ref first.DangerousGetPinnableReference(), ref second.DangerousGetPinnableReference(), length);
}
/// <param name="value">The value to search for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOf<T>(this ReadOnlySpan<T> span, T value)
- where T : struct, IEquatable<T>
+ where T : IEquatable<T>
{
+ if (typeof(T) == typeof(byte))
+ return SpanHelpers.IndexOf(
+ ref Unsafe.As<T, byte>(ref span.DangerousGetPinnableReference()),
+ Unsafe.As<T, byte>(ref value),
+ span.Length);
return SpanHelpers.IndexOf<T>(ref span.DangerousGetPinnableReference(), value, span.Length);
}
/// <summary>
- /// Searches for the specified value and returns the index of its first occurrence. If not found, returns -1.
- /// </summary>
- /// <param name="span">The span to search.</param>
- /// <param name="value">The value to search for.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int IndexOf(this ReadOnlySpan<byte> span, byte value)
- {
- return SpanHelpers.IndexOf(ref span.DangerousGetPinnableReference(), value, span.Length);
- }
-
- /// <summary>
/// Searches for the specified sequence 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>
/// <param name="value">The sequence to search for.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOf<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value)
- where T : struct, IEquatable<T>
+ where T : IEquatable<T>
{
+ if (typeof(T) == typeof(byte))
+ return SpanHelpers.IndexOf(
+ ref Unsafe.As<T, byte>(ref span.DangerousGetPinnableReference()),
+ span.Length,
+ ref Unsafe.As<T, byte>(ref value.DangerousGetPinnableReference()),
+ value.Length);
return SpanHelpers.IndexOf<T>(ref span.DangerousGetPinnableReference(), span.Length, ref value.DangerousGetPinnableReference(), value.Length);
}
/// <summary>
- /// Searches for the specified sequence and returns the index of its first occurrence. If not found, returns -1.
- /// </summary>
- /// <param name="span">The span to search.</param>
- /// <param name="value">The sequence to search for.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int IndexOf(this ReadOnlySpan<byte> span, ReadOnlySpan<byte> value)
- {
- return SpanHelpers.IndexOf(ref span.DangerousGetPinnableReference(), span.Length, ref value.DangerousGetPinnableReference(), value.Length);
- }
-
-
- /// <summary>
/// Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator. If not found, returns -1.
/// </summary>
/// <param name="span">The span to search.</param>
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool SequenceEqual<T>(this ReadOnlySpan<T> first, ReadOnlySpan<T> second)
- where T : struct, IEquatable<T>
- {
- int length = first.Length;
- return length == second.Length && SpanHelpers.SequenceEqual(ref first.DangerousGetPinnableReference(), ref second.DangerousGetPinnableReference(), length);
- }
-
- /// <summary>
- /// Determines whether two sequences are equal by comparing the elements.
- /// </summary>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool SequenceEqual(this ReadOnlySpan<byte> first, ReadOnlySpan<byte> second)
+ where T : IEquatable<T>
{
int length = first.Length;
+ if (typeof(T) == typeof(byte))
+ return length == second.Length &&
+ SpanHelpers.SequenceEqual(
+ ref Unsafe.As<T, byte>(ref first.DangerousGetPinnableReference()),
+ ref Unsafe.As<T, byte>(ref second.DangerousGetPinnableReference()),
+ length);
return length == second.Length && SpanHelpers.SequenceEqual(ref first.DangerousGetPinnableReference(), ref second.DangerousGetPinnableReference(), length);
}
/// Determines whether the specified sequence appears at the start of the span.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool StartsWith(this Span<byte> span, ReadOnlySpan<byte> value)
- {
- int valueLength = value.Length;
- return valueLength <= span.Length && SpanHelpers.SequenceEqual(ref span.DangerousGetPinnableReference(), ref value.DangerousGetPinnableReference(), valueLength);
- }
-
- /// <summary>
- /// Determines whether the specified sequence appears at the start of the span.
- /// </summary>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool StartsWith<T>(this Span<T> span, ReadOnlySpan<T> value)
- where T : struct, IEquatable<T>
- {
- int valueLength = value.Length;
- return valueLength <= span.Length && SpanHelpers.SequenceEqual(ref span.DangerousGetPinnableReference(), ref value.DangerousGetPinnableReference(), valueLength);
- }
-
- /// <summary>
- /// Determines whether the specified sequence appears at the start of the span.
- /// </summary>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool StartsWith(this ReadOnlySpan<byte> span, ReadOnlySpan<byte> value)
+ where T : IEquatable<T>
{
int valueLength = value.Length;
+ if (typeof(T) == typeof(byte))
+ return valueLength <= span.Length &&
+ SpanHelpers.SequenceEqual(
+ ref Unsafe.As<T, byte>(ref span.DangerousGetPinnableReference()),
+ ref Unsafe.As<T, byte>(ref value.DangerousGetPinnableReference()),
+ valueLength);
return valueLength <= span.Length && SpanHelpers.SequenceEqual(ref span.DangerousGetPinnableReference(), ref value.DangerousGetPinnableReference(), valueLength);
}
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool StartsWith<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value)
- where T : struct, IEquatable<T>
+ where T : IEquatable<T>
{
int valueLength = value.Length;
+ if (typeof(T) == typeof(byte))
+ return valueLength <= span.Length &&
+ SpanHelpers.SequenceEqual(
+ ref Unsafe.As<T, byte>(ref span.DangerousGetPinnableReference()),
+ ref Unsafe.As<T, byte>(ref value.DangerousGetPinnableReference()),
+ valueLength);
return valueLength <= span.Length && SpanHelpers.SequenceEqual(ref span.DangerousGetPinnableReference(), ref value.DangerousGetPinnableReference(), valueLength);
}
internal static partial class SpanHelpers
{
public static int IndexOf<T>(ref T searchSpace, int searchSpaceLength, ref T value, int valueLength)
- where T : struct, IEquatable<T>
+ where T : IEquatable<T>
{
Debug.Assert(searchSpaceLength >= 0);
Debug.Assert(valueLength >= 0);
}
public static unsafe int IndexOf<T>(ref T searchSpace, T value, int length)
- where T : struct, IEquatable<T>
+ where T : IEquatable<T>
{
Debug.Assert(length >= 0);
}
public static bool SequenceEqual<T>(ref T first, ref T second, int length)
- where T : struct, IEquatable<T>
+ where T : IEquatable<T>
{
Debug.Assert(length >= 0);
{
for (int i = 0; i < Benchmark.InnerIterationCount; i++)
{
- index |= byteSpan.IndexOf(53); // '5' = 53
+ index |= byteSpan.IndexOf<byte>(53); // '5' = 53
}
}
}
{
public class Perf_Span_StartsWith
{
- [Benchmark]
+ [Benchmark(InnerIterationCount = 10000)]
[InlineData(1, 1)]
[InlineData(10, 1)]
[InlineData(100, 1)]
{
using (iteration.StartMeasurement())
{
- for (int i = 0; i < 10000; i++)
+ for (int i = 0; i < Benchmark.InnerIterationCount; i++)
{
bool result = span.StartsWith(value);
}
}
}
- [Benchmark]
+ [Benchmark(InnerIterationCount = 1000000)]
[InlineData(1, 1)]
[InlineData(10, 1)]
[InlineData(100, 1)]
{
using (iteration.StartMeasurement())
{
- for (int i = 0; i < 10000; i++)
+ for (int i = 0; i < Benchmark.InnerIterationCount; i++)
+ {
+ bool result = span.StartsWith(value);
+ }
+ }
+ }
+ }
+
+ [Benchmark(InnerIterationCount = 10000)]
+ [InlineData(1, 1)]
+ [InlineData(10, 1)]
+ [InlineData(100, 1)]
+ [InlineData(1000, 1)]
+ [InlineData(10000, 1)]
+ [InlineData(10, 10)]
+ [InlineData(100, 10)]
+ [InlineData(1000, 10)]
+ [InlineData(10000, 10)]
+ [InlineData(100, 100)]
+ [InlineData(1000, 100)]
+ [InlineData(10000, 100)]
+ [InlineData(1000, 1000)]
+ [InlineData(10000, 1000)]
+ [InlineData(10000, 10000)]
+ public void String(int size, int valSize)
+ {
+ var a = new string[size];
+ for (int i = 0; i < size; i++)
+ {
+ int num = 65 + i % 26;
+ a[i] = ((char)num).ToString();
+ }
+
+ var b = new string[valSize];
+ for (int i = 0; i < valSize; i++)
+ {
+ int num = 65 + i % 26;
+ b[i] = ((char)num).ToString();
+ }
+ var span = new Span<string>(a);
+ var value = new Span<string>(b);
+ foreach (var iteration in Benchmark.Iterations)
+ {
+ using (iteration.StartMeasurement())
+ {
+ for (int i = 0; i < Benchmark.InnerIterationCount; i++)
{
bool result = span.StartsWith(value);
}
public static void ZeroLengthIndexOf_Byte()
{
ReadOnlySpan<byte> sp = new ReadOnlySpan<byte>(Array.Empty<byte>());
- int idx = sp.IndexOf(0);
+ int idx = sp.IndexOf<byte>(0);
Assert.Equal(-1, idx);
}
for (int i = 0; i < length; i++)
{
byte target0 = default(byte);
- int idx = span.IndexOf(target0);
+ int idx = span.IndexOf<byte>(target0);
Assert.Equal(0, idx);
}
}
for (int targetIndex = 0; targetIndex < length; targetIndex++)
{
byte target = a[targetIndex];
- int idx = span.IndexOf(target);
+ int idx = span.IndexOf<byte>(target);
Assert.Equal(targetIndex, idx);
}
}
}
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a);
- int idx = span.IndexOf(target);
+ int idx = span.IndexOf<byte>(target);
Assert.Equal(-1, idx);
}
}
for (var i = 0; i < Vector<byte>.Count; i++)
{
var span = new ReadOnlySpan<byte>(array, i, 3 * Vector<byte>.Count);
- int idx = span.IndexOf((byte)'1');
+ int idx = span.IndexOf<byte>((byte)'1');
Assert.Equal(-1, idx);
span = new ReadOnlySpan<byte>(array, i, 3 * Vector<byte>.Count - 3);
- idx = span.IndexOf((byte)'1');
+ idx = span.IndexOf<byte>((byte)'1');
Assert.Equal(-1, idx);
}
}
for (var i = 0; i < Vector<byte>.Count; i++)
{
var span = new ReadOnlySpan<byte>(array, i, 3 * Vector<byte>.Count);
- int idx = span.IndexOf(5);
+ int idx = span.IndexOf<byte>(5);
Assert.Equal(0, idx);
span = new ReadOnlySpan<byte>(array, i, 3 * Vector<byte>.Count - 3);
- idx = span.IndexOf(5);
+ idx = span.IndexOf<byte>(5);
Assert.Equal(0, idx);
}
}
a[length - 2] = 200;
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a);
- int idx = span.IndexOf(200);
+ int idx = span.IndexOf<byte>(200);
Assert.Equal(length - 2, idx);
}
}
a[0] = 99;
a[length + 1] = 99;
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a, 1, length);
- int index = span.IndexOf(99);
+ int index = span.IndexOf<byte>(99);
Assert.Equal(-1, index);
}
}
var index = -1;
if (searchFor.Length == 1)
{
- index = span.IndexOf((byte)searchFor[0]);
+ index = span.IndexOf<byte>((byte)searchFor[0]);
}
else if (searchFor.Length == 2)
{
{
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 5, 1, 77, 2, 3, 77, 77, 4, 5, 77, 77, 77, 88, 6, 6, 77, 77, 88, 9 });
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 5, 1, 77 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(0, index);
}
{
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 1, 2, 3, 1, 2, 3, 1, 2, 3 });
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 2, 3 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(1, index);
}
{
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 0, 1, 77, 2, 3, 77, 77, 4, 5, 77, 77, 77, 88, 6, 6, 77, 77, 88, 9 });
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 77, 77, 88 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(10, index);
}
{
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 0, 1, 77, 2, 3, 77, 77, 4, 5, 77, 77, 77, 88, 6, 6, 77, 77, 88, 9 });
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 77, 77, 88, 99 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(-1, index);
}
{
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 0, 1, 77, 2, 3, 77, 77, 4, 5, 77, 77, 77, 88, 6, 6, 77, 77, 88, 9 });
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 100, 77, 88, 99 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(-1, index);
}
{
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 0, 1, 2, 3, 4, 5 });
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 3, 4, 5 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(3, index);
}
{
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 0, 1, 2, 3, 4, 5 }, 0, 5);
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 3, 4, 5 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(-1, index);
}
// A zero-length value is always "found" at the start of the span.
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 0, 1, 77, 2, 3, 77, 77, 4, 5, 77, 77, 77, 88, 6, 6, 77, 77, 88, 9 });
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(Array.Empty<byte>());
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(0, index);
}
{
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(Array.Empty<byte>());
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 1, 2, 3 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(-1, index);
}
// A zero-length value is always "found" at the start of the span.
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 0, 1, 2, 3, 4, 5 });
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 2 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(2, index);
}
// A zero-length value is always "found" at the start of the span.
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 0, 1, 2, 3, 4, 5 });
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 5 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(5, index);
}
// A zero-length value is always "found" at the start of the span.
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(new byte[] { 0, 1, 2, 3, 4, 5 }, 0, 5);
ReadOnlySpan<byte> value = new ReadOnlySpan<byte>(new byte[] { 5 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(-1, index);
}
}
ReadOnlySpan<byte> first = new ReadOnlySpan<byte>(a, 1, 0);
ReadOnlySpan<byte> second = new ReadOnlySpan<byte>(a, 2, 0);
- bool b = first.SequenceEqual(second);
+ bool b = first.SequenceEqual<byte>(second);
Assert.True(b);
}
{
byte[] a = { 4, 5, 6 };
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a);
- bool b = span.SequenceEqual(span);
+ bool b = span.SequenceEqual<byte>(span);
Assert.True(b);
}
{
byte[] a = { 4, 5, 6 };
ReadOnlySpan<byte> first = new ReadOnlySpan<byte>(a, 0, 3);
- bool b = first.SequenceEqual(a);
+ bool b = first.SequenceEqual<byte>(a);
Assert.True(b);
}
var segment = new ArraySegment<byte>(dst, 1, 3);
ReadOnlySpan<byte> first = new ReadOnlySpan<byte>(src, 0, 3);
- bool b = first.SequenceEqual(segment);
+ bool b = first.SequenceEqual<byte>(segment);
Assert.True(b);
}
byte[] a = { 4, 5, 6 };
ReadOnlySpan<byte> first = new ReadOnlySpan<byte>(a, 0, 3);
ReadOnlySpan<byte> second = new ReadOnlySpan<byte>(a, 0, 2);
- bool b = first.SequenceEqual(second);
+ bool b = first.SequenceEqual<byte>(second);
Assert.False(b);
}
ReadOnlySpan<byte> firstSpan = new ReadOnlySpan<byte>(first);
ReadOnlySpan<byte> secondSpan = new ReadOnlySpan<byte>(second);
- bool b = firstSpan.SequenceEqual(secondSpan);
+ bool b = firstSpan.SequenceEqual<byte>(secondSpan);
Assert.False(b);
}
}
second[length + 1] = 100;
ReadOnlySpan<byte> span1 = new ReadOnlySpan<byte>(first, 1, length);
ReadOnlySpan<byte> span2 = new ReadOnlySpan<byte>(second, 1, length);
- bool b = span1.SequenceEqual(span2);
+ bool b = span1.SequenceEqual<byte>(span2);
Assert.True(b);
}
}
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a);
ReadOnlySpan<byte> slice = new ReadOnlySpan<byte>(a, 2, 0);
- bool b = span.StartsWith(slice);
+ bool b = span.StartsWith<byte>(slice);
Assert.True(b);
}
{
byte[] a = { 4, 5, 6 };
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a);
- bool b = span.StartsWith(span);
+ bool b = span.StartsWith<byte>(span);
Assert.True(b);
}
byte[] a = { 4, 5, 6 };
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a, 0, 2);
ReadOnlySpan<byte> slice = new ReadOnlySpan<byte>(a, 0, 3);
- bool b = span.StartsWith(slice);
+ bool b = span.StartsWith<byte>(slice);
Assert.False(b);
}
byte[] a = { 4, 5, 6 };
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a, 0, 3);
ReadOnlySpan<byte> slice = new ReadOnlySpan<byte>(a, 0, 2);
- bool b = span.StartsWith(slice);
+ bool b = span.StartsWith<byte>(slice);
Assert.True(b);
}
byte[] b = { 4, 5, 6 };
ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(a, 0, 3);
ReadOnlySpan<byte> slice = new ReadOnlySpan<byte>(b, 0, 3);
- bool c = span.StartsWith(slice);
+ bool c = span.StartsWith<byte>(slice);
Assert.True(c);
}
ReadOnlySpan<byte> firstSpan = new ReadOnlySpan<byte>(first);
ReadOnlySpan<byte> secondSpan = new ReadOnlySpan<byte>(second);
- bool b = firstSpan.StartsWith(secondSpan);
+ bool b = firstSpan.StartsWith<byte>(secondSpan);
Assert.False(b);
}
}
second[length + 1] = 100;
ReadOnlySpan<byte> span1 = new ReadOnlySpan<byte>(first, 1, length);
ReadOnlySpan<byte> span2 = new ReadOnlySpan<byte>(second, 1, length);
- bool b = span1.StartsWith(span2);
+ bool b = span1.StartsWith<byte>(span2);
Assert.True(b);
}
}
public static void ZeroLengthIndexOf_Byte()
{
Span<byte> sp = new Span<byte>(Array.Empty<byte>());
- int idx = sp.IndexOf(0);
+ int idx = sp.IndexOf<byte>(0);
Assert.Equal(-1, idx);
}
for (int i = 0; i < length; i++)
{
byte target0 = default(byte);
- int idx = span.IndexOf(target0);
+ int idx = span.IndexOf<byte>(target0);
Assert.Equal(0, idx);
}
}
for (int targetIndex = 0; targetIndex < length; targetIndex++)
{
byte target = a[targetIndex];
- int idx = span.IndexOf(target);
+ int idx = span.IndexOf<byte>(target);
Assert.Equal(targetIndex, idx);
}
}
}
Span<byte> span = new Span<byte>(a);
- int idx = span.IndexOf(target);
+ int idx = span.IndexOf<byte>(target);
Assert.Equal(-1, idx);
}
}
for (var i = 0; i < Vector<byte>.Count; i++)
{
var span = new Span<byte>(array, i, 3 * Vector<byte>.Count);
- int idx = span.IndexOf((byte)'1');
+ int idx = span.IndexOf<byte>((byte)'1');
Assert.Equal(-1, idx);
span = new Span<byte>(array, i, 3 * Vector<byte>.Count - 3);
- idx = span.IndexOf((byte)'1');
+ idx = span.IndexOf<byte>((byte)'1');
Assert.Equal(-1, idx);
}
}
for (var i = 0; i < Vector<byte>.Count; i++)
{
var span = new Span<byte>(array, i, 3 * Vector<byte>.Count);
- int idx = span.IndexOf(5);
+ int idx = span.IndexOf<byte>(5);
Assert.Equal(0, idx);
span = new Span<byte>(array, i, 3 * Vector<byte>.Count - 3);
- idx = span.IndexOf(5);
+ idx = span.IndexOf<byte>(5);
Assert.Equal(0, idx);
}
}
a[length - 2] = 200;
Span<byte> span = new Span<byte>(a);
- int idx = span.IndexOf(200);
+ int idx = span.IndexOf<byte>(200);
Assert.Equal(length - 2, idx);
}
}
a[0] = 99;
a[length + 1] = 99;
Span<byte> span = new Span<byte>(a, 1, length);
- int index = span.IndexOf(99);
+ int index = span.IndexOf<byte>(99);
Assert.Equal(-1, index);
}
}
var index = -1;
if (searchFor.Length == 1)
{
- index = span.IndexOf((byte)searchFor[0]);
+ index = span.IndexOf<byte>((byte)searchFor[0]);
}
else if (searchFor.Length == 2)
{
{
Span<byte> span = new Span<byte>(new byte[] { 5, 1, 77, 2, 3, 77, 77, 4, 5, 77, 77, 77, 88, 6, 6, 77, 77, 88, 9 });
Span<byte> value = new Span<byte>(new byte[] { 5, 1, 77 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(0, index);
}
{
Span<byte> span = new Span<byte>(new byte[] { 1, 2, 3, 1, 2, 3, 1, 2, 3 });
Span<byte> value = new Span<byte>(new byte[] { 2, 3 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(1, index);
}
{
Span<byte> span = new Span<byte>(new byte[] { 0, 1, 77, 2, 3, 77, 77, 4, 5, 77, 77, 77, 88, 6, 6, 77, 77, 88, 9 });
Span<byte> value = new Span<byte>(new byte[] { 77, 77, 88 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(10, index);
}
{
Span<byte> span = new Span<byte>(new byte[] { 0, 1, 77, 2, 3, 77, 77, 4, 5, 77, 77, 77, 88, 6, 6, 77, 77, 88, 9 });
Span<byte> value = new Span<byte>(new byte[] { 77, 77, 88, 99 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(-1, index);
}
{
Span<byte> span = new Span<byte>(new byte[] { 0, 1, 77, 2, 3, 77, 77, 4, 5, 77, 77, 77, 88, 6, 6, 77, 77, 88, 9 });
Span<byte> value = new Span<byte>(new byte[] { 100, 77, 88, 99 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(-1, index);
}
{
Span<byte> span = new Span<byte>(new byte[] { 0, 1, 2, 3, 4, 5 });
Span<byte> value = new Span<byte>(new byte[] { 3, 4, 5 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(3, index);
}
{
Span<byte> span = new Span<byte>(new byte[] { 0, 1, 2, 3, 4, 5 }, 0, 5);
Span<byte> value = new Span<byte>(new byte[] { 3, 4, 5 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(-1, index);
}
// A zero-length value is always "found" at the start of the span.
Span<byte> span = new Span<byte>(new byte[] { 0, 1, 77, 2, 3, 77, 77, 4, 5, 77, 77, 77, 88, 6, 6, 77, 77, 88, 9 });
Span<byte> value = new Span<byte>(Array.Empty<byte>());
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(0, index);
}
{
Span<byte> span = new Span<byte>(Array.Empty<byte>());
Span<byte> value = new Span<byte>(new byte[] { 1, 2, 3 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(-1, index);
}
// A zero-length value is always "found" at the start of the span.
Span<byte> span = new Span<byte>(new byte[] { 0, 1, 2, 3, 4, 5 });
Span<byte> value = new Span<byte>(new byte[] { 2 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(2, index);
}
// A zero-length value is always "found" at the start of the span.
Span<byte> span = new Span<byte>(new byte[] { 0, 1, 2, 3, 4, 5 });
Span<byte> value = new Span<byte>(new byte[] { 5 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(5, index);
}
// A zero-length value is always "found" at the start of the span.
Span<byte> span = new Span<byte>(new byte[] { 0, 1, 2, 3, 4, 5 }, 0, 5);
Span<byte> value = new Span<byte>(new byte[] { 5 });
- int index = span.IndexOf(value);
+ int index = span.IndexOf<byte>(value);
Assert.Equal(-1, index);
}
}
Span<byte> first = new Span<byte>(a, 1, 0);
Span<byte> second = new Span<byte>(a, 2, 0);
- bool b = first.SequenceEqual(second);
+ bool b = first.SequenceEqual<byte>(second);
Assert.True(b);
}
{
byte[] a = { 4, 5, 6 };
Span<byte> span = new Span<byte>(a);
- bool b = span.SequenceEqual(span);
+ bool b = span.SequenceEqual<byte>(span);
Assert.True(b);
}
{
byte[] a = { 4, 5, 6 };
Span<byte> first = new Span<byte>(a, 0, 3);
- bool b = first.SequenceEqual(a);
+ bool b = first.SequenceEqual<byte>(a);
Assert.True(b);
}
var segment = new ArraySegment<byte>(dst, 1, 3);
Span<byte> first = new Span<byte>(src, 0, 3);
- bool b = first.SequenceEqual(segment);
+ bool b = first.SequenceEqual<byte>(segment);
Assert.True(b);
}
byte[] a = { 4, 5, 6 };
Span<byte> first = new Span<byte>(a, 0, 3);
Span<byte> second = new Span<byte>(a, 0, 2);
- bool b = first.SequenceEqual(second);
+ bool b = first.SequenceEqual<byte>(second);
Assert.False(b);
}
Span<byte> firstSpan = new Span<byte>(first);
ReadOnlySpan<byte> secondSpan = new ReadOnlySpan<byte>(second);
- bool b = firstSpan.SequenceEqual(secondSpan);
+ bool b = firstSpan.SequenceEqual<byte>(secondSpan);
Assert.False(b);
}
}
second[length + 1] = 100;
Span<byte> span1 = new Span<byte>(first, 1, length);
ReadOnlySpan<byte> span2 = new ReadOnlySpan<byte>(second, 1, length);
- bool b = span1.SequenceEqual(span2);
+ bool b = span1.SequenceEqual<byte>(span2);
Assert.True(b);
}
}
Span<byte> span = new Span<byte>(a);
ReadOnlySpan<byte> slice = new ReadOnlySpan<byte>(a, 2, 0);
- bool b = span.StartsWith(slice);
+ bool b = span.StartsWith<byte>(slice);
Assert.True(b);
}
{
byte[] a = { 4, 5, 6 };
Span<byte> span = new Span<byte>(a);
- bool b = span.StartsWith(span);
+ bool b = span.StartsWith<byte>(span);
Assert.True(b);
}
byte[] a = { 4, 5, 6 };
Span<byte> span = new Span<byte>(a, 0, 2);
ReadOnlySpan<byte> slice = new ReadOnlySpan<byte>(a, 0, 3);
- bool b = span.StartsWith(slice);
+ bool b = span.StartsWith<byte>(slice);
Assert.False(b);
}
byte[] a = { 4, 5, 6 };
Span<byte> span = new Span<byte>(a, 0, 3);
ReadOnlySpan<byte> slice = new ReadOnlySpan<byte>(a, 0, 2);
- bool b = span.StartsWith(slice);
+ bool b = span.StartsWith<byte>(slice);
Assert.True(b);
}
byte[] b = { 4, 5, 6 };
Span<byte> span = new Span<byte>(a, 0, 3);
ReadOnlySpan<byte> slice = new ReadOnlySpan<byte>(b, 0, 3);
- bool c = span.StartsWith(slice);
+ bool c = span.StartsWith<byte>(slice);
Assert.True(c);
}
Span<byte> firstSpan = new Span<byte>(first);
ReadOnlySpan<byte> secondSpan = new ReadOnlySpan<byte>(second);
- bool b = firstSpan.StartsWith(secondSpan);
+ bool b = firstSpan.StartsWith<byte>(secondSpan);
Assert.False(b);
}
}
second[length + 1] = 100;
Span<byte> span1 = new Span<byte>(first, 1, length);
ReadOnlySpan<byte> span2 = new ReadOnlySpan<byte>(second, 1, length);
- bool b = span1.StartsWith(span2);
+ bool b = span1.StartsWith<byte>(span2);
Assert.True(b);
}
}