コード例 #1
0
        private static int FindMatchLength(string source, string searchValue, int searchIndex, int searchLength, StringComparison comparisonType, int matchIndex)
        {
            int matchLengthMaximum = searchLength - (matchIndex - searchIndex);
            int matchLengthInitial = Math.Min(searchValue.Length, matchLengthMaximum);

            // Hot path: match length is same as specified search string
            if (Substring.CompareInner(source, matchIndex, matchLengthInitial, searchValue, 0, searchValue.Length, comparisonType) == 0)
            {
                return(matchLengthInitial);
            }

            int matchLengthDecrementing = matchLengthInitial - 1;
            int matchLengthIncrementing = matchLengthInitial + 1;

            while (matchLengthDecrementing >= 0 || matchLengthIncrementing <= matchLengthMaximum)
            {
                if (matchLengthDecrementing >= 0)
                {
                    if (Substring.CompareInner(source, matchIndex, matchLengthDecrementing, searchValue, 0, searchValue.Length, comparisonType) == 0)
                    {
                        return(matchLengthDecrementing);
                    }

                    matchLengthDecrementing--;
                }

                if (matchLengthIncrementing <= matchLengthMaximum)
                {
                    if (Substring.CompareInner(source, matchIndex, matchLengthIncrementing, searchValue, 0, searchValue.Length, comparisonType) == 0)
                    {
                        return(matchLengthIncrementing);
                    }

                    matchLengthIncrementing++;
                }
            }

            // Should never happen
            return(-1);
        }