Example #1
0
        /// <inheritdoc cref="SearchCompare_Nullable(string?, string, string[], bool, StringComparison)"/>
        public static bool SearchCompare(string inputText, string name, string[] pinyinArray, bool ignoreOtherChar = false, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase)
        {
            if (name.Contains(inputText, comparisonType))
            {
                return(true);
            }
            if (!pinyinArray.Any_Nullable())
            {
                return(false);
            }

            #region 汉字拼音混合比较算法

            try
            {
                var index = 0;                                 // 输入分词数组的比较下标
                for (int i = 0; i < pinyinArray !.Length; i++) // 循环所有字的拼音
                {
                    var item_pinyin = pinyinArray[i];          // 当前字的拼音
                    if (index >= inputText.Length)
                    {
                        return(true);                           // 下标溢出即搜索完毕,返回比较成功。
                    }
                    int cache_index      = 0;
                    var break_next       = false;
                    var ignore_separator = true;              // 本次循环忽略分隔符。
                    for (; index < inputText.Length; index++) // 循环输入内容
                    {
                        if (break_next)
                        {
                            break;
                        }
                        var input_item = inputText[index];                        // 当前输入内容字符
                        if (input_item == Separator || input_item == SeparatorZH) // 拼音分隔分号字符无视
                        {
                            if (index == 0)
                            {
                                return(false);              // 第一个字符不能是分隔分号开头
                            }
                            if (i < pinyinArray.Length - 1) // 前面的可以输入分隔符跳过
                            {
                                if (ignore_separator)
                                {
                                    ignore_separator = !ignore_separator;
                                }
                                else
                                {
                                    break_next = true;
                                }
                            }
                            continue;
                        }
                        var isChinese = IsChinese(input_item); // 当前字符是否为中文字符。
                        if (isChinese)
                        {
                            if (name[i] != input_item)
                            {
                                return(false);
                            }
                            else
                            {
                                continue;
                            }
                        }
                        // ↑ 当前字符为中文字符比较内容不正确返回比较失败。
                        char letter = default;

                        var indexOfUpperCase = Constants.UpperCaseLetters.IndexOf(input_item); // 大写字母搜索
                        if (indexOfUpperCase >= 0)
                        {
                            letter = Constants.UpperCaseLetters[indexOfUpperCase];
                        }
                        var indexOfLowerCase = Constants.LowerCaseLetters.IndexOf(input_item); // 小写字母搜索
                        if (indexOfLowerCase >= 0)
                        {
                            letter = Constants.UpperCaseLetters[indexOfLowerCase];
                        }

                        var letterHasValue = letter != default;

                        if (!letterHasValue && !(ignoreOtherChar && index != 0))
                        {
                            return(false);                                                     // 无效字符是否允许
                        }
                        var item_pinyin_letter_upper = item_pinyin[cache_index];
                        indexOfLowerCase = Constants.LowerCaseLetters.IndexOf(item_pinyin_letter_upper);
                        if (indexOfLowerCase >= 0)
                        {
                            item_pinyin_letter_upper = Constants.UpperCaseLetters[indexOfLowerCase];
                        }

                        if (letterHasValue && item_pinyin_letter_upper != letter)
                        {
                            return(false);
                        }
                        else
                        {
                            cache_index += 1;
                        }
                        if (cache_index >= item_pinyin.Length)
                        {
                            ignore_separator = true;
                            break_next       = true;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(TAG, e, "SearchCompare catch, name: {0}, inputText: {1}", name, inputText);
                return(false);
            }

            return(true);

            #endregion
        }