Beispiel #1
0
        public static Boolean Run(string search, string value)
        {
            char[]     searchCharArr = search.ToCharArray();
            char[]     valueCharArr  = value.ToCharArray();
            List <int> percentIndex  = new List <int>();

            //Check length of search
            for (int i = 0; i < searchCharArr.Length; i++)
            {
                if (searchCharArr[i] == '%')
                {
                    percentIndex.Add(i);
                }
            }
            if (searchCharArr.Length - percentIndex.Count > valueCharArr.Length)
            {
                return(false);
            }

            var firstloop = true;

            for (int i = 0; i < percentIndex.Count; i++)
            {
                int index = percentIndex[i];
                if (i > 0)
                {
                    index = index - percentIndex[i - 1] - 1;
                }

                var search_section = OtherAlgorithms.SubCharArray(searchCharArr, 0, index);
                int containsindex  = -1;

                //Match leading characters
                if (firstloop)
                {
                    if (!OtherAlgorithms.WildcardEquals(OtherAlgorithms.SubCharArray(valueCharArr, 0, index), search_section))
                    {
                        return(false);
                    }
                    valueCharArr = OtherAlgorithms.SubCharArray(valueCharArr, search_section.Length);
                    firstloop    = false;
                }
                //Match intermediate characters
                else
                {
                    containsindex = OtherAlgorithms.WildcardContains(search_section, valueCharArr);
                    if (containsindex == -1)
                    {
                        return(false);
                    }
                    valueCharArr = OtherAlgorithms.SubCharArray(valueCharArr, containsindex + search_section.Length);
                }

                searchCharArr = OtherAlgorithms.SubCharArray(searchCharArr, index + 1);
            }

            //Match trailing characters
            if (!OtherAlgorithms.WildcardEquals(OtherAlgorithms.SubCharArray(valueCharArr, valueCharArr.Length - searchCharArr.Length), searchCharArr))
            {
                return(false);
            }

            return(true);
        }
        public static Boolean Run(string search, string value)
        {
            char[] searchCharArr = search.ToCharArray();
            char[] valueCharArr  = value.ToCharArray();

            var state        = EvalState.Start;
            var search_index = 0;
            var value_index  = 0;
            var match        = true;

            while (state != EvalState.End)
            {
                //If column string length is shorter than search value length
                if (value_index >= valueCharArr.Length)
                {
                    return(CheckTrailingPercent(OtherAlgorithms.SubCharArray(searchCharArr, search_index)));
                }

                state = IdentifyState(searchCharArr[RestrainSearchIndex(search_index, searchCharArr.Length)], search_index.Equals(searchCharArr.Length));

                switch (state)
                {
                case EvalState.End:
                    break;

                case EvalState.Character:
                    match = searchCharArr[search_index] == valueCharArr[value_index];
                    search_index++;
                    value_index++;
                    break;

                case EvalState.Percent:
                    search_index = search_index + NextNonPercentIndex(OtherAlgorithms.SubCharArray(searchCharArr, search_index));
                    if (search_index >= searchCharArr.Length)
                    {
                        return(true);                                          //Trailing characters of search value consist only of %
                    }
                    var search_remaining = OtherAlgorithms.SubCharArray(searchCharArr, search_index);
                    var column_remaining = OtherAlgorithms.SubCharArray(valueCharArr, value_index);

                    //Checks trailing characters when no more percent symbols are found
                    int search_chars = NextPercentIndex(OtherAlgorithms.SubCharArray(searchCharArr, search_index));
                    if (search_chars == -1)
                    {
                        if (CompareTrailingCharacters(search_remaining, column_remaining))
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }

                    //Check if current section of search exists in value
                    var a_block       = OtherAlgorithms.SubCharArray(searchCharArr, search_index, search_chars);
                    int matchingindex = OtherAlgorithms.WildcardContains(a_block, column_remaining);
                    if (matchingindex == -1)
                    {
                        return(false);                          //Substring of search value does not exist in column value
                    }
                    //Advance index pointers
                    search_index = search_index + search_chars;
                    value_index  = value_index + matchingindex + a_block.Length;
                    break;

                case EvalState.Underscore:
                    search_index++;
                    value_index++;
                    break;
                }
                if (!match)
                {
                    return(false);
                }
            }

            if (value_index == value.Length)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }