A collection of convenient span helpers, exposed as extension methods.
Пример #1
0
        /// <summary>
        /// Determines whether two spans are structurally (byte-wise) equal by comparing the elements by using memcmp
        /// </summary>
        /// <param name="first">A span, of type T to compare to second.</param>
        /// <param name="second">A span, of type U to compare to first.</param>
        public static bool BlockEquals <[Primitive] T, [Primitive] U>(this ReadOnlySpan <T> first, ReadOnlySpan <U> second)
            where T : struct
            where U : struct
        {
            var bytesCount = (ulong)first.Length * (ulong)Unsafe.SizeOf <T>();

            if (bytesCount != (ulong)second.Length * (ulong)Unsafe.SizeOf <U>())
            {
                return(false);
            }

            // perf: it is cheaper to compare 'n' long elements than 'n*8' bytes (in a loop)
            if ((bytesCount & 0x00000007) == 0) // fast % sizeof(long)
            {
                return(SequenceEqual(first.NonPortableCast <T, long>(), second.NonPortableCast <U, long>()));
            }
            if ((bytesCount & 0x00000003) == 0) // fast % sizeof(int)
            {
                return(SequenceEqual(first.NonPortableCast <T, int>(), second.NonPortableCast <U, int>()));
            }
            if ((bytesCount & 0x00000001) == 0) // fast % sizeof(short)
            {
                return(SequenceEqual(first.NonPortableCast <T, short>(), second.NonPortableCast <U, short>()));
            }

            return(SpanExtensions.SequenceEqual(first.NonPortableCast <T, byte>(), second.NonPortableCast <U, byte>()));
        }
Пример #2
0
 /// <summary>
 /// Determines whether two spans are equal by comparing the elements by using generic Equals method
 /// </summary>
 /// <param name="first">A span of type T to compare to second.</param>
 /// <param name="second">A span of type T to compare to first.</param>
 public static bool SequenceEqual <T>(this Span <T> first, ReadOnlySpan <T> second)
     where T : struct, IEquatable <T>
 {
     return(SpanExtensions.SequenceEqual(first, second));
 }
Пример #3
0
 /// <summary>
 /// Determines whether two read-only spans are equal (byte-wise) by comparing the elements by using memcmp
 /// </summary>
 /// <param name="first">A span of long integers to compare to second.</param>
 /// <param name="second">A span of long integers T to compare to first.</param>
 public static bool SequenceEqual(this ReadOnlySpan <long> first, ReadOnlySpan <long> second)
 {
     return(first.Length >= 256
         ? MemoryEqual(first, second)
         : SpanExtensions.SequenceEqual(first, second));
 }
Пример #4
0
 /// <summary>
 /// Determines whether two read-only spans are equal (byte-wise) by comparing the elements by using memcmp
 /// </summary>
 /// <param name="first">A span of shorts to compare to second.</param>
 /// <param name="second">A span of shorts T to compare to first.</param>
 public static bool SequenceEqual(this ReadOnlySpan <short> first, ReadOnlySpan <short> second)
 {
     return(first.Length >= 512
         ? MemoryEqual(first, second)
         : SpanExtensions.SequenceEqual <short>(first, second));
 }
Пример #5
0
 public static int IndexOf(this ReadOnlyMemory <byte> memory, ReadOnlySpan <byte> values)
 {
     return(SpanExtensions.IndexOf(memory.Span, values));
 }
Пример #6
0
 public static int IndexOf(this ReadOnlySpan <char> str, string value)
 {
     return(SpanExtensions.IndexOf(str, value.Slice()));
 }