Esempio n. 1
0
        public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expectedSign)
        {
            SortKey sk1 = compareInfo.GetSortKey(string1, options);
            SortKey sk2 = compareInfo.GetSortKey(string2, options);

            Assert.Equal(expectedSign, Math.Sign(SortKey.Compare(sk1, sk2)));
            Assert.Equal(expectedSign == 0, sk1.Equals(sk2));

            if (!WindowsVersionHasTheCompareStringRegression)
            {
                Assert.Equal(Math.Sign(compareInfo.Compare(string1, string2, options)), Math.Sign(SortKey.Compare(sk1, sk2)));
            }

            Assert.Equal(compareInfo.GetHashCode(string1, options), sk1.GetHashCode());
            Assert.Equal(compareInfo.GetHashCode(string2, options), sk2.GetHashCode());

            Assert.Equal(string1, sk1.OriginalString);
            Assert.Equal(string2, sk2.OriginalString);

            // Now try the span-based versions - use BoundedMemory to detect buffer overruns

            RunSpanSortKeyTest(compareInfo, string1, options, sk1.KeyData);
            RunSpanSortKeyTest(compareInfo, string2, options, sk2.KeyData);

            unsafe static void RunSpanSortKeyTest(CompareInfo compareInfo, ReadOnlySpan <char> source, CompareOptions options, byte[] expectedSortKey)
Esempio n. 2
0
    public static void Main()
    {
        CompareInfo cmpi       = null;
        SortKey     sk1        = null;
        SortKey     sk2        = null;
        string      s          = "ABC";
        string      ignoreCase = "Ignore case";
        string      useCase    = "Use case   ";

// Get a CompareInfo object for the English-Great Britain culture.
        cmpi = CompareInfo.GetCompareInfo("en-GB");

// Get a sort key that ignores case for the specified string.
        sk1 = cmpi.GetSortKey(s, CompareOptions.IgnoreCase);
// Get a sort key with no compare option for the specified string.
        sk2 = cmpi.GetSortKey(s);

// Display the original string.
        Console.WriteLine("Original string: \"{0}\"", sk1.OriginalString);
        Console.WriteLine();

// Display the the string equivalent of the two sort keys.
        Console.WriteLine("CompareInfo (culture) name: {0}", cmpi.Name);
        Console.WriteLine("ToString - {0}: \"{1}\"", ignoreCase, sk1.ToString());
        Console.WriteLine("ToString - {0}: \"{1}\"", useCase, sk2.ToString());
        Console.WriteLine();

// Display the key data of the two sort keys.
        DisplayKeyData(sk1, ignoreCase);
        DisplayKeyData(sk2, useCase);
    }
Esempio n. 3
0
        public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expected)
        {
            SortKey sk1 = compareInfo.GetSortKey(string1, options);
            SortKey sk2 = compareInfo.GetSortKey(string2, options);

            Assert.Equal(expected, SortKey.Compare(sk1, sk2));
            Assert.Equal(string1, sk1.OriginalString);
            Assert.Equal(string2, sk2.OriginalString);
        }
Esempio n. 4
0
        public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expectedSign)
        {
            SortKey sk1 = compareInfo.GetSortKey(string1, options);
            SortKey sk2 = compareInfo.GetSortKey(string2, options);

            Assert.Equal(expectedSign, Math.Sign(SortKey.Compare(sk1, sk2)));
            Assert.Equal(expectedSign == 0, sk1.Equals(sk2));
            Assert.Equal(Math.Sign(compareInfo.Compare(string1, string2, options)), Math.Sign(SortKey.Compare(sk1, sk2)));

            Assert.Equal(compareInfo.GetHashCode(string1, options), sk1.GetHashCode());
            Assert.Equal(compareInfo.GetHashCode(string2, options), sk2.GetHashCode());

            Assert.Equal(string1, sk1.OriginalString);
            Assert.Equal(string2, sk2.OriginalString);
        }
Esempio n. 5
0
        /// <summary>
        /// Tries to find the key <paramref name="searchText"/> in the dictionary.
        /// </summary>
        /// <param name="searchText">The text to search. If this original text was not found, on return, the parameter contains the key for which the content is returned.</param>
        /// <param name="dictionary">The dictionary.</param>
        /// <returns>A tuple consisting of the content, the content identifer, and a boolean. The boolean is true if the original search text was found in the dictionary.
        /// If the original search text was not found, the boolean is false, and <paramref name="searchText"/>contains the search text that was found.</returns>
        private (string Content, string ContentId, bool foundOriginal) GetResult(ref string searchText, ISlobDictionary dictionary)
        {
            var searchKey = _compareInfo.GetSortKey(searchText);
            var index     = Array.BinarySearch(_sortKeys, searchKey, new UnicodeStringSorter());


            (string Content, string ContentId)result = (null, null);
            bool found = false;

            if (index >= 0)
            {
                result = dictionary[searchText];
                found  = true;
            }
            else
            {
                index = ~index;

                if (index < _keys.Length)
                {
                    searchText = _keys[index];
                    result     = dictionary[searchText];
                }
            }

            return(result.Content, result.ContentId, found);
        }
Esempio n. 6
0
 public static byte[] SortKey(string orig)
 {
     if (orig == null)
     {
         return(null);
     }
     return(culture_compare_info.GetSortKey(orig, CompareOptions.IgnoreCase).KeyData);
 }
Esempio n. 7
0
            public int GetHashCode([NotNull] string o)
            {
#if NETSTANDARD1_3
                return(CompareInfo.GetHashCode(o, CompareOptions.IgnoreWidth));
#else
                return(CompareInfo.GetSortKey(o, CompareOptions.IgnoreWidth).GetHashCode());
#endif
            }
Esempio n. 8
0
            public int GetHashCode(string obj)
            {
#if NET45 || NET451
                return(CompareInfo.GetSortKey(obj, CompareOptions.IgnoreWidth).GetHashCode());
#else
                return(CompareInfo.GetHashCode(obj, CompareOptions.IgnoreWidth));
#endif
            }
Esempio n. 9
0
            public int GetHashCode([NotNull] string o)
            {
#if NET45 || NET451
                return(CompareInfo.GetSortKey(o, CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase).GetHashCode());
#else
                return(CompareInfo.GetHashCode(o, CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase));
#endif
            }
Esempio n. 10
0
    public static void Main()
    {
        // Define names.
        String[]    names = { "Adam", "Ignatius", "Batholomew", "Gregory",
                              "Clement", "Frances",  "Harold",     "Dalmatius",
                              "Edgar",   "John",     "Benedict",   "Paul", "George" };
        SortKey[]   sortKeys = new SortKey[names.Length];
        CompareInfo ci       = CultureInfo.CurrentCulture.CompareInfo;

        for (int ctr = 0; ctr < names.Length; ctr++)
        {
            sortKeys[ctr] = ci.GetSortKey(names[ctr], CompareOptions.IgnoreCase);
        }

        // Sort array based on value of sort keys.
        Array.Sort(names, sortKeys);

        Console.WriteLine("Sorted array: ");
        foreach (var name in names)
        {
            Console.WriteLine(name);
        }

        Console.WriteLine();

        String[] namesToFind = { "Paul", "PAUL", "Wilberforce" };

        Console.WriteLine("Searching an array:");
        foreach (var nameToFind in namesToFind)
        {
            SortKey searchKey = ci.GetSortKey(nameToFind, CompareOptions.IgnoreCase);
            int     index     = Array.FindIndex(sortKeys, (x) => x.Equals(searchKey));
            if (index >= 0)
            {
                Console.WriteLine("{0} found at index {1}: {2}", nameToFind,
                                  index, names[index]);
            }
            else
            {
                Console.WriteLine("{0} not found", nameToFind);
            }
        }
    }
Esempio n. 11
0
            unsafe static void RunSpanSortKeyTest(CompareInfo compareInfo, ReadOnlySpan <char> source, CompareOptions options, byte[] expectedSortKey)
            {
                using BoundedMemory <char> sourceBoundedMemory = BoundedMemory.AllocateFromExistingData(source);
                sourceBoundedMemory.MakeReadonly();

                Assert.Equal(expectedSortKey.Length, compareInfo.GetSortKeyLength(sourceBoundedMemory.Span, options));

                using BoundedMemory <byte> sortKeyBoundedMemory = BoundedMemory.Allocate <byte>(expectedSortKey.Length);

                // First try with a destination which is too small - should result in an error

                Assert.Throws <ArgumentException>("destination", () => compareInfo.GetSortKey(sourceBoundedMemory.Span, sortKeyBoundedMemory.Span.Slice(1), options));

                // Next, try with a destination which is perfectly sized - should succeed

                Span <byte> sortKeyBoundedSpan = sortKeyBoundedMemory.Span;

                sortKeyBoundedSpan.Clear();

                Assert.Equal(expectedSortKey.Length, compareInfo.GetSortKey(sourceBoundedMemory.Span, sortKeyBoundedSpan, options));
                Assert.Equal(expectedSortKey, sortKeyBoundedSpan[0..expectedSortKey.Length].ToArray());
Esempio n. 12
0
        public override int GetHashCode(string s)
        {
            if (s == null)
            {
                throw new ArgumentNullException("s");
            }

            CompareOptions co = _ignoreCase ? CompareOptions.IgnoreCase :
                                CompareOptions.None;

            return(_compareInfo.GetSortKey(s, co).GetHashCode());
        }
Esempio n. 13
0
        /// <summary>
        /// Hashes the provided string using the given CompareOptions.
        /// Doing this allows all custom compare options to be applied in determining the hash value.
        /// </summary>
        /// <param name="str">The string.</param>
        /// <param name="info">The CompareInfo object doing the unicode-aware comparison.</param>
        /// <param name="options">The options to apply to the comparison.</param>
        /// <returns>Returns the hash code for the string.</returns>
        public int HashFunction(string str, CompareInfo info, CompareOptions options)
        {
            if (string.IsNullOrEmpty(str))
            {
                return(0);
            }

            SortKey sortOrder = info.GetSortKey(str, options);

            int hash = GetByteArrayHash(sortOrder.KeyData);

            return(hash);
        }
Esempio n. 14
0
        public static Key Fixed(string data, int length, CompareInfo collation, CompareOptions collationOptions, Direction direction = Direction.Ascending)
        {
            Key i;
            var tmp = collation.GetSortKey(data, collationOptions).KeyData;

            if (direction == Direction.Descending)
            {
                InvertBits(tmp);
            }

            i.bytes  = tmp;
            i.length = length * 6; // Assumes 2 bytes per character is enough, may not be true for all UTF-16 characters
            return(i);
        }
Esempio n. 15
0
        private string GetStringSortKey(IConvertible value)
        {
            string text = value.ToString(null);

            if (_compareOptions.HasFlag(CompareOptions.Ordinal))
            {
                return(text);
            }
            if (_compareOptions.HasFlag(CompareOptions.OrdinalIgnoreCase))
            {
                return(text.ToUpperInvariant());
            }
            return(ToComparableBase64String(_compareInfo.GetSortKey(text, _compareOptions).KeyData));
        }
    public static void Main()
    {
        string lowerABC = "abc";
        string upperABC = "ABC";
        int    result   = 0;

// Create a CompareInfo object for the en-US culture.
        Console.WriteLine("\nCreate a CompareInfo object for the en-US culture...\n");
        CompareInfo cmpi = CompareInfo.GetCompareInfo("en-US");
// Alternatively:
//  CompareInfo cmpi = new CultureInfo("en-US").CompareInfo;

// Create sort keys for lowercase and uppercase "abc", the en-US culture, and
// ignore case.
        SortKey sk1LowerIgnCase = cmpi.GetSortKey(lowerABC, CompareOptions.IgnoreCase);
        SortKey sk2UpperIgnCase = cmpi.GetSortKey(upperABC, CompareOptions.IgnoreCase);

// Create sort keys for lowercase and uppercase "abc", the en-US culture, and
// use case.
        SortKey sk1LowerUseCase = cmpi.GetSortKey(lowerABC, CompareOptions.None);
        SortKey sk2UpperUseCase = cmpi.GetSortKey(upperABC, CompareOptions.None);

// Compare lowercase and uppercase "abc", ignoring case and using CompareInfo.
        result = cmpi.Compare(lowerABC, upperABC, CompareOptions.IgnoreCase);
        Display(result, "CompareInfo, Ignore case", lowerABC, upperABC);
// Compare lowercase and uppercase "abc", ignoring case and using SortKey.
        result = SortKey.Compare(sk1LowerIgnCase, sk2UpperIgnCase);
        Display(result, "SortKey, Ignore case", lowerABC, upperABC);
        Console.WriteLine();

// Compare lowercase and uppercase "abc", using case and using CompareInfo.
        result = cmpi.Compare(lowerABC, upperABC, CompareOptions.None);
        Display(result, "CompareInfo, Use case", lowerABC, upperABC);
// Compare lowercase and uppercase "abc", using case and using SortKey.
        result = SortKey.Compare(sk1LowerUseCase, sk2UpperUseCase);
        Display(result, "SortKey, Use case", lowerABC, upperABC);
    }
Esempio n. 17
0
        /// <summary>
        /// Collects the keys of all loaded dictionaries and sorts them alphabetically. The list of keys is then published in <see cref="KeyList"/>.
        /// </summary>
        public void CollectAndSortKeys()
        {
            var hashSet = new HashSet <string>();

            foreach (var dict in _dictionaries)
            {
                hashSet.UnionWith(dict.GetKeys());
            }

            _keys = hashSet.ToArray();

            _compareInfo = new CultureInfo("en-US", false).CompareInfo;

            _sortKeys = _keys.Select(x => _compareInfo.GetSortKey(x)).ToArray();
            Array.Sort(_sortKeys, _keys, new UnicodeStringSorter());

            KeyList = new ObservableCollection <string>(_keys);
        }
Esempio n. 18
0
        /// <summary>
        /// Hashes the provided string using the given CompareOptions.
        /// Doing this allows all custom compare options to be applied in determining the hash value.
        /// </summary>
        /// <param name="str">The string.</param>
        /// <param name="info">The CompareInfo object doing the unicode-aware comparison.</param>
        /// <param name="options">The options to apply to the comparison.</param>
        /// <returns>Returns the hash code for the string.</returns>
        public static int HashFunction(string str, CompareInfo info, CompareOptions options)
        {
            if (info == null)
            {
                throw new ArgumentNullException(nameof(info));
            }

            if (string.IsNullOrEmpty(str))
            {
                return(0);
            }

            SortKey sortOrder = info.GetSortKey(str, options);

            int hash = GetByteArrayHash(sortOrder.KeyData);

            return(hash);
        }
Esempio n. 19
0
 public int GetHashCode(string obj)
 {
     return(CompareInfo.GetSortKey(obj, CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase).GetHashCode());
 }
Esempio n. 20
0
 public int GetHashCode([NotNull] string o)
 => CompareInfo.GetSortKey(o, CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase).GetHashCode();
Esempio n. 21
0
 // Windows Test Function
 private void TestWin(int lineNum)
 {
     wComp.GetSortKey(wLines[lineNum], wOptions);
 }
Esempio n. 22
0
 public int GetHashCode(string o)
 => CompareInfo.GetSortKey(o, CompareOptions.IgnoreWidth | CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType).GetHashCode();
Esempio n. 23
0
 public override int GetHashCode(string obj)
 {
     return(m_compareInfo.GetSortKey(obj, m_options).GetHashCode());
 }
        public void SortKeyTest(CompareInfo compareInfo, string string1, string string2, CompareOptions options, int expected)
        {
            SortKey sk1 = compareInfo.GetSortKey(string1, options);
            SortKey sk2 = compareInfo.GetSortKey(string2, options);

            Assert.Equal(expected, SortKey.Compare(sk1, sk2));
            Assert.Equal(string1, sk1.OriginalString);
            Assert.Equal(string2, sk2.OriginalString);
        }