コード例 #1
0
        private static bool Match(string query, string haystack, bool strictOnly, float strictWeight, float looseWeight, out float score, bool[] indices)
        {
            score = 0;
            var isStreaking   = false;
            var haystackIndex = 0;
            var matchedAny    = false;

            for (var queryIndex = 0; queryIndex < query.Length; queryIndex++)
            {
                var queryCharacter = query[queryIndex];

                if (StringUtility.IsWordDelimiter(queryCharacter))
                {
                    continue;
                }

                var matched = false;

                for (; haystackIndex < haystack.Length; haystackIndex++)
                {
                    var haystackCharacter = haystack[haystackIndex];

                    if (StringUtility.IsWordDelimiter(haystackCharacter))
                    {
                        isStreaking = false;
                        continue;
                    }

                    var matchesLoose  = Compare(queryCharacter, haystackCharacter);
                    var matchesStrict = matchesLoose && (isStreaking || StringUtility.IsWordBeginning(haystack, haystackIndex));

                    var matches = strictOnly ? matchesStrict : matchesLoose;

                    if (matches)
                    {
                        score  += matchesStrict ? strictWeight : looseWeight;
                        matched = true;

                        if (indices != null)
                        {
                            indices[haystackIndex] = true;
                        }

                        isStreaking = true;
                        haystackIndex++;
                        break;
                    }
                    else
                    {
                        isStreaking = false;
                    }
                }

                if (matched)
                {
                    matchedAny = true;
                }
                else
                {
                    return(false);
                }
            }

            if (!matchedAny)
            {
                return(false);
            }

            score /= query.Length;
            return(true);
        }