示例#1
0
        /// <summary>
        /// Compares two strings.
        /// </summary>
        /// <param name="a">The first string to compare.</param>
        /// <param name="b">The second string to compare.</param>
        /// <returns>True if the strings are equal.</returns>
        public bool Equals(ISpanString a, ISpanString b)
        {
            // Find optimized comparison methods.
            if (a is SpanString1 a1)
            {
                if (b is SpanString1 b1)
                {
                    return(_ignoreCase ? SpanString1.EqualsIgnoreCase(a1, b1) : SpanString1.Equals(a1, b1));
                }

                if (b is SpanString2 b2)
                {
                    return(_ignoreCase ? SpanString1.EqualsIgnoreCase(a1, b2) : SpanString1.Equals(a1, b2));
                }

                throw new ArgumentException($"Second param 'b' is an unknown SpanString type '{b.GetType().Name}', is this a bug?");
            }

            if (a is SpanString2 a2)
            {
                if (b is SpanString1 b1)
                {
                    return(_ignoreCase ? SpanString2.EqualsIgnoreCase(a2, b1) : SpanString2.Equals(a2, b1));
                }

                if (b is SpanString2 b2)
                {
                    return(_ignoreCase ? SpanString2.EqualsIgnoreCase(a2, b2) : SpanString2.Equals(a2, b2));
                }

                throw new ArgumentException($"Second param 'b' is an unknown SpanString type '{b.GetType().Name}', is this a bug?");
            }

            throw new ArgumentException($"First param 'a' is an unknown SpanString type '{a.GetType().Name}', is this a bug?");
        }
示例#2
0
        /// <summary>
        /// Gets a hash code for a SpanString. If strings A and B are such that A.Equals(B), then
        /// they will return the same hash code.
        /// </summary>
        public int GetHashCode(SpanString1 s)
        {
            if (!_ignoreCase)
            {
                return(s.GetHashCode());
            }

            return(s.GetHashCodeIgnoreCase());
        }
示例#3
0
        /// <summary>
        /// Compares two SpanStrings using an ordinal comparison.
        /// </summary>
        /// <param name="a">The first string to compare.</param>
        /// <param name="b">The second string to compare.</param>
        /// <returns>True if the strings are equal.</returns>
        public static bool EqualsIgnoreCase(SpanString2 a, SpanString1 b)
        {
            if (a.Length != b.Length)
            {
                return(false);
            }

            var s1Enum = new Enumerator(a);
            var s2Enum = new SpanString1.Enumerator(b);

            for (int i = 0; i < a.Length; i++)
            {
                int c1 = SpanStringComparer.ToUpperAscii(s1Enum.Next());
                int c2 = SpanStringComparer.ToUpperAscii(s2Enum.Next());
                if (c1 != c2)
                {
                    return(false);
                }
            }

            return(true);
        }
示例#4
0
        /// <summary>
        /// Compares two SpanStrings using an ordinal comparison.
        /// </summary>
        /// <param name="a">The first string to compare.</param>
        /// <param name="b">The second string to compare.</param>
        /// <returns>True if the strings are equal.</returns>
        public static bool Equals(SpanString2 a, SpanString1 b)
        {
            if (a.Length != b.Length)
            {
                return(false);
            }

            var s1Enum = new Enumerator(a);
            var s2Enum = new SpanString1.Enumerator(b);

            for (int i = 0; i < a.Length; i++)
            {
                int c1 = s1Enum.Next();
                int c2 = s2Enum.Next();
                if (c1 != c2)
                {
                    return(false);
                }
            }

            return(true);
        }
示例#5
0
        /// <summary>
        /// Compares this instance with a specified SpanString1 using an ordinal
        /// comparison and case-insensitivity, and indicates whether this
        /// instance precedes, follows, or appears in the same position
        /// in the sort order as the specified string.
        /// </summary>
        /// <param name="other">The string to compare with this instance.</param>
        /// <returns>A 32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the strB parameter.</returns>
        public int CompareToIgnoreCase(SpanString1 other)
        {
            // Adapted from https://referencesource.microsoft.com/#mscorlib/system/string.cs CompareOrdinalHelper()

            int length = Math.Min(Length, other.Length);

            var s1Enum = new Enumerator(this);
            var s2Enum = new SpanString1.Enumerator(other);

            for (int i = 0; i < length; i++)
            {
                int c1  = SpanStringComparer.ToUpperAscii(s1Enum.Next());
                int c2  = SpanStringComparer.ToUpperAscii(s2Enum.Next());
                int cmp = c1.CompareTo(c2);
                if (cmp != 0)
                {
                    return(cmp);
                }
            }

            // At this point, we have compared all the characters in at least one string.
            // The longer string will be larger.
            return(Length - other.Length);
        }
示例#6
0
 public Enumerator(SpanString1 ss)
 {
     _currentIndex = 0;
     _spanString = ss;
 }
示例#7
0
 /// <summary>
 /// Compares two strings.
 /// </summary>
 /// <param name="a">The first string to compare.</param>
 /// <param name="b">The second string to compare.</param>
 /// <returns>True if the strings are equal.</returns>
 public bool Equals(SpanString1 a, SpanString1 b)
 {
     return(_ignoreCase ? SpanString1.EqualsIgnoreCase(a, b) : a.Equals(b));
 }