Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            var summary = BenchmarkRunner.Run <IntroBenchmarkBaseline>();

            Console.WriteLine("Result: {0}", SimpleCaseFolding.SimpleCaseFold("cASEfOLDING2"));
            Console.WriteLine("Result: {0}", SimpleCaseFolding.SimpleCaseFold("яЯяЯяЯяЯяЯя2"));
        }
        private static int GetHashCodeSimpleCaseFolding(string source)
        {
            Debug.Assert(source != null, "source must not be null");

            // Do not allocate on the stack if string is empty
            if (source.Length == 0)
            {
                return(source.GetHashCode());
            }

            char[]      borrowedArr = null;
            Span <char> destination = source.Length <= 255 ?
                                      stackalloc char[source.Length] :
                                      (borrowedArr = ArrayPool <char> .Shared.Rent(source.Length));

            SimpleCaseFolding.SimpleCaseFold(source, destination);

            int hash = String.GetHashCode(destination);

            // Return the borrowed array if necessary.
            if (borrowedArr != null)
            {
                ArrayPool <char> .Shared.Return(borrowedArr);
            }

            return(hash);
        }
        /// <summary>
        /// IComparer.Compare() implementation.
        /// </summary>
        /// <param name="x">Object to compare.</param>
        /// <param name="y">Object to compare.</param>
        /// <returns>
        /// Returns 0 - if equal, -1 - if x &lt; y, +1 - if x &gt; y.
        /// </returns>
        public int Compare(object x, object y)
        {
            if (x == y)
            {
                return(0);
            }

            if (x == null)
            {
                return(-1);
            }

            if (y == null)
            {
                return(1);
            }

            if (x is string sa && y is string sb)
            {
                return(SimpleCaseFolding.CompareUsingSimpleCaseFolding(sa, sb));
            }

            if (x is IComparable ia)
            {
                return(ia.CompareTo(y));
            }

            throw new ArgumentException("SR.Argument_ImplementIComparable");
        }
        /// <summary>
        /// IEqualityComparer&lt;string&gt;.Equals() implementation.
        /// </summary>
        /// <param name="x">Left object to compare.</param>
        /// <param name="y">Right object to compare.</param>
        /// <returns>
        /// Returns true if equal.
        /// </returns>
        public bool Equals(string x, string y)
        {
            if (object.ReferenceEquals(x, y))
            {
                return(true);
            }

            if (x == null || y == null)
            {
                return(false);
            }

            return(SimpleCaseFolding.CompareUsingSimpleCaseFolding(x, y) == 0);
        }
        /// <summary>
        /// IComparer&lt;string&gt;.GetHashCode() implementation.
        /// </summary>
        /// <param name="x">Left object to compare.</param>
        /// <param name="y">Right object to compare.</param>
        /// <returns>
        /// Returns 0 - if equal, -1 - if x &lt; y, +1 - if x &gt; y.
        /// </returns>
        public int Compare(string x, string y)
        {
            if (object.ReferenceEquals(x, y))
            {
                return(0);
            }

            if (x == null)
            {
                return(-1);
            }

            if (y == null)
            {
                return(1);
            }

            return(SimpleCaseFolding.CompareUsingSimpleCaseFolding(x, y));
        }
Exemplo n.º 6
0
        private static unsafe int ComputeHash32OrdinalIgnoreCaseSlow(ref char data, int count, uint p0, uint p1)
        {
            Debug.Assert(count > 0);

            char[]      borrowedArr = null;
            Span <char> scratch     = (uint)count <= 64 ? stackalloc char[64] : (borrowedArr = ArrayPool <char> .Shared.Rent(count));

            SimpleCaseFolding.SimpleCaseFold(new ReadOnlySpan <char>(Unsafe.AsPointer(ref data), count), scratch);

            // Slice the array to the size returned by ToUpperInvariant.
            // Multiplication below may overflow, that's fine since it's going to an unsigned integer.
            int hash = ComputeHash32(ref Unsafe.As <char, byte>(ref MemoryMarshal.GetReference(scratch)), count * 2, p0, p1);

            // Return the borrowed array if necessary.
            if (borrowedArr != null)
            {
                ArrayPool <char> .Shared.Return(borrowedArr);
            }

            return(hash);
        }
Exemplo n.º 7
0
 public char CharFold(char c)
 {
     return(SimpleCaseFolding.SimpleCaseFold1(c));
 }
Exemplo n.º 8
0
 public string SimpleCaseFold(string StrA)
 {
     return(SimpleCaseFolding.SimpleCaseFold(StrA));
 }
Exemplo n.º 9
0
 public char SimpleCaseFoldTest()
 {
     return(SimpleCaseFolding.SimpleCaseFoldTest(TestChar));
 }