public override IEnumerable <ISearchMatch> Search(string toFind, string toSearch)
        {
            if (toFind.Length > 0 && toSearch.Length > 0)
            {
                BadMatchTable badMatchTable = new BadMatchTable(toFind);

                int currStartIndex = 0;
                while (currStartIndex <= toSearch.Length - toFind.Length)
                {
                    int charsLeftToMatch = toFind.Length - 1;

                    while (charsLeftToMatch >= 0 &&
                           Compare(toFind[charsLeftToMatch], toSearch[currStartIndex + charsLeftToMatch]) == 0)
                    {
                        charsLeftToMatch--;
                    }

                    if (charsLeftToMatch < 0)
                    {
                        yield return(new SearchMatchStruct(currStartIndex, toFind.Length));

                        currStartIndex += toFind.Length;
                    }
                    else
                    {
                        currStartIndex += badMatchTable[toSearch[currStartIndex + toFind.Length - 1]];
                    }
                }
            }
        }
        public static List <int> BoyerMooreSearch(string pattern, string toSearch)
        {
            if (string.IsNullOrWhiteSpace(pattern) || string.IsNullOrWhiteSpace(toSearch) || pattern.Length > toSearch.Length)
            {
                return(null);
            }

            var matchIndexesFound = new List <int>();
            var badMatchTbl       = new BadMatchTable(pattern);
            var currentIdx        = 0;

            while (currentIdx <= toSearch.Length - pattern.Length)
            {
                int charsLeftToMatch = pattern.Length - 1;//reset & start from right
                while (charsLeftToMatch >= 0 && (pattern[charsLeftToMatch] == toSearch[currentIdx + charsLeftToMatch]))
                {
                    charsLeftToMatch--;
                }

                if (charsLeftToMatch < 0) //match found
                {
                    matchIndexesFound.Add(currentIdx);
                    currentIdx += pattern.Length;
                }
                else
                {
                    currentIdx += badMatchTbl[toSearch[currentIdx + pattern.Length - 1]];
                }
            }

            return(matchIndexesFound);
        }
Esempio n. 3
0
        public static int BoyerMooreSearch(string pattern, string toSearch)
        {
            if (string.IsNullOrWhiteSpace(toSearch))
            {
                return(-1);
            }

            var badMatchTbl = new BadMatchTable(pattern);
            var currentIdx  = 0;

            while (currentIdx <= toSearch.Length - pattern.Length)
            {
                int charsLeftToMatch = pattern.Length - 1;

                while (charsLeftToMatch >= 0 && pattern[charsLeftToMatch] == toSearch[currentIdx + charsLeftToMatch])
                {
                    charsLeftToMatch--;
                }

                if (charsLeftToMatch < 0)
                {
                    return(currentIdx);
                }

                currentIdx += badMatchTbl[toSearch[currentIdx + pattern.Length - 1]];
            }

            return(-1);
        }
Esempio n. 4
0
        public void TestBadMatchTable()
        {
            // Arrange

            // Act
            var badMatchTable = new BadMatchTable("TRUTH");

            // Assert
            Assert.AreEqual(1, badMatchTable.GetShift('T'));
            Assert.AreEqual(3, badMatchTable.GetShift('R'));
            Assert.AreEqual(2, badMatchTable.GetShift('U'));
            Assert.AreEqual(5, badMatchTable.GetShift('Z'));
        }