Exemplo n.º 1
0
        /// <summary>
        /// Performs an equality comparison using a <see cref="StringComparison.Ordinal"/> comparer.
        /// </summary>
        public bool Equals(Utf8String?value)
        {
            // First, a very quick check for referential equality.

            if (ReferenceEquals(this, value))
            {
                return(true);
            }

            // Otherwise, perform a simple bitwise equality check.

            return(!(value is null) &&
                   this.Length == value.Length
#if SYSTEM_PRIVATE_CORELIB
                   && SpanHelpers.SequenceEqual(ref this.DangerousGetMutableReference(), ref value.DangerousGetMutableReference(), (uint)Length));
Exemplo n.º 2
0
        /// <summary>
        /// Compares two <see cref="Utf8String"/> instances using a <see cref="StringComparison.Ordinal"/> comparer.
        /// </summary>
        public static bool Equals(Utf8String?left, Utf8String?right)
        {
            // First, a very quick check for referential equality.

            if (ReferenceEquals(left, right))
            {
                return(true);
            }

            // Otherwise, perform a simple bitwise equality check.

            return(!(left is null) &&
                   !(right is null) &&
                   left.Length == right.Length &&
                   SpanHelpers.SequenceEqual(ref left.DangerousGetMutableReference(), ref right.DangerousGetMutableReference(), (uint)left.Length));
        }
Exemplo n.º 3
0
        public override unsafe bool Equals([NotNullWhen(true)] object?obj)
        {
            if (null == obj)
            {
                return(false);
            }

            if (GetType() != obj.GetType())
            {
                return(false);
            }

            // if there are no GC references in this object we can avoid reflection
            // and do a fast memcmp
            if (CanCompareBits(this))
            {
                return(SpanHelpers.SequenceEqual(
                           ref RuntimeHelpers.GetRawData(this),
                           ref RuntimeHelpers.GetRawData(obj),
                           RuntimeHelpers.GetMethodTable(this)->GetNumInstanceFieldBytes()));
            }

            FieldInfo[] thisFields = GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            for (int i = 0; i < thisFields.Length; i++)
            {
                object?thisResult = thisFields[i].GetValue(this);
                object?thatResult = thisFields[i].GetValue(obj);

                if (thisResult == null)
                {
                    if (thatResult != null)
                    {
                        return(false);
                    }
                }
                else
                if (!thisResult.Equals(thatResult))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 4
0
        public static bool EndsWith <T>(this ReadOnlySpan <T> span, ReadOnlySpan <T> value)
            where T : IEquatable <T>
        {
            int spanLength  = span.Length;
            int valueLength = value.Length;

            if (typeof(T) == typeof(byte))
            {
                return(valueLength <= spanLength &&
                       SpanHelpers.SequenceEqual(
                           ref Unsafe.As <T, byte>(ref Unsafe.Add(ref MemoryMarshal.GetReference(span), spanLength - valueLength)),
                           ref Unsafe.As <T, byte>(ref MemoryMarshal.GetReference(value)),
                           valueLength));
            }
            return(valueLength <= spanLength &&
                   SpanHelpers.SequenceEqual(
                       ref Unsafe.Add(ref MemoryMarshal.GetReference(span), spanLength - valueLength),
                       ref MemoryMarshal.GetReference(value),
                       valueLength));
        }
Exemplo n.º 5
0
        public static bool EndsWith <T>(this Span <T> span, ReadOnlySpan <T> value)
            where T : IEquatable <T>
        {
            int spanLength  = span.Length;
            int valueLength = value.Length;

            if (typeof(T) == typeof(byte))
            {
                return(valueLength <= spanLength &&
                       SpanHelpers.SequenceEqual(
                           ref Unsafe.As <T, byte>(ref Unsafe.Add(ref span.DangerousGetPinnableReference(), spanLength - valueLength)),
                           ref Unsafe.As <T, byte>(ref value.DangerousGetPinnableReference()),
                           valueLength));
            }
            return(valueLength <= spanLength &&
                   SpanHelpers.SequenceEqual(
                       ref Unsafe.Add(ref span.DangerousGetPinnableReference(), spanLength - valueLength),
                       ref value.DangerousGetPinnableReference(),
                       valueLength));
        }
Exemplo n.º 6
0
        public static bool SequenceEqual(this Span <char> first, ReadOnlySpan <char> second)
        {
            int length = first.Length;

            return(length == second.Length && SpanHelpers.SequenceEqual(ref first.DangerousGetPinnableReference(), ref second.DangerousGetPinnableReference(), length));
        }
Exemplo n.º 7
0
        public static bool StartsWith(this ReadOnlySpan <byte> span, ReadOnlySpan <byte> value)
        {
            int valueLength = value.Length;

            return(valueLength <= span.Length && SpanHelpers.SequenceEqual(ref span.DangerousGetPinnableReference(), ref value.DangerousGetPinnableReference(), valueLength));
        }