Extension methods for ReadOnlySpan<T>.
Ejemplo n.º 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(Cast <T, long>(first), Cast <U, long>(second)));
            }
            if ((bytesCount & 0x00000003) == 0) // fast % sizeof(int)
            {
                return(SequenceEqual(Cast <T, int>(first), Cast <U, int>(second)));
            }
            if ((bytesCount & 0x00000001) == 0) // fast % sizeof(short)
            {
                return(SequenceEqual(Cast <T, short>(first), Cast <U, short>(second)));
            }

            return(ReadOnlySpanExtensions.SequenceEqual(Cast <T, byte>(first), Cast <U, byte>(second)));
        }
Ejemplo n.º 2
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)
         : ReadOnlySpanExtensions.SequenceEqual(first, second));
 }
Ejemplo n.º 3
0
 public static int IndexOf(this ReadOnlySpan <char> str, string value)
 {
     return(ReadOnlySpanExtensions.IndexOf(str, value.Slice()));
 }
Ejemplo n.º 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)
         : ReadOnlySpanExtensions.SequenceEqual <short>(first, second));
 }
Ejemplo n.º 5
0
 public static int IndexOf(this ReadOnlyMemory <byte> memory, ReadOnlySpan <byte> values)
 {
     return(ReadOnlySpanExtensions.IndexOf(memory.Span, values));
 }