Exemple #1
0
        /// <summary>
        /// Process state that has wildcard transition.
        /// </summary>
        /// <param name="state">State with wildcard transition</param>
        /// <param name="sSearchIn">String to search in.</param>
        /// <param name="nCurrIndex">Current index of the search</param>
        /// <param name="nSearchUpTo">Index where to stop the search.</param>
        private void ProcessWildCard(State state, string sSearchIn, ref int nCurrIndex, int nSearchUpTo)
        {
            State toState = null;
            int   nIndex  = nCurrIndex;

            while (nIndex <= nSearchUpTo)
            {
                char ch = sSearchIn[nIndex];

                toState = state.GetSingleTransition(ch.ToString());

                if (toState != null)
                {
                    nCurrIndex = nIndex;
                }
                nIndex++;
            }
        }
Exemple #2
0
 /// <summary>
 /// Determins if a state contains a wildcard transition.
 /// i.e., A_*B
 /// </summary>
 /// <param name="state">State to check</param>
 /// <returns>true if the state contains wildcard transition, otherwise false</returns>
 private bool IsWildCard(State state)
 {
     return(state == state.GetSingleTransition(MetaSymbol.ANY_ONE_CHAR_TRANS));
 }
Exemple #3
0
        /// <summary>
        /// Search and finds a match for the compiled pattern.
        /// One must call Compile method before calling this method.
        /// </summary>
        /// <param name="sSearchIn">String to search in.</param>
        /// <param name="nSearchStartAt">Index at which to begin the search.</param>
        /// <param name="nSearchEndAt">Index at which to end the search.</param>
        /// <param name="nFoundBeginAt">If match found, recives the index where the match started, otherwise -1</param>
        /// <param name="nFoundEndAt">If match found, recives the index where the match ended, otherwise -1</param>
        /// <returns>true if match found, otherwise false.</returns>
        public bool FindMatch(string sSearchIn,
                              int nSearchStartAt,
                              int nSearchEndAt,
                              ref int nFoundBeginAt,
                              ref int nFoundEndAt)
        {
            if (m_stateStartDfaM == null)
            {
                return(false);
            }

            if (nSearchStartAt < 0)
            {
                return(false);
            }

            State stateStart = m_stateStartDfaM;

            nFoundBeginAt = -1;
            nFoundEndAt   = -1;

            bool  bAccepted   = false;
            State toState     = null;
            State stateCurr   = stateStart;
            int   nIndex      = nSearchStartAt;
            int   nSearchUpTo = nSearchEndAt;

            //看不懂这个匹配

            while (nIndex <= nSearchUpTo)
            {
                if (m_bGreedy && IsWildCard(stateCurr) == true)
                {
                    if (nFoundBeginAt == -1)
                    {
                        nFoundBeginAt = nIndex;
                    }
                    ProcessWildCard(stateCurr, sSearchIn, ref nIndex, nSearchUpTo);
                }

                char chInputSymbol = sSearchIn[nIndex];


                toState = stateCurr.GetSingleTransition(chInputSymbol.ToString());

                if (toState == null)
                {
                    //找不到就寻找是否有any符号
                    toState = stateCurr.GetSingleTransition(MetaSymbol.ANY_ONE_CHAR_TRANS);
                }

                if (toState != null)
                {
                    if (nFoundBeginAt == -1)
                    {
                        nFoundBeginAt = nIndex;
                    }

                    if (toState.AcceptingState)
                    {
                        if (m_bMatchAtEnd && nIndex != nSearchUpTo)  // then we ignore the accepting state
                        {
                            //toState = stateStart ;
                        }
                        else
                        {
                            bAccepted   = true;
                            nFoundEndAt = nIndex;
                            if (m_bGreedy == false)
                            {
                                break;
                            }
                        }
                    }

                    stateCurr = toState;
                    nIndex++;
                }
                else
                {
                    //找不到状态就重新开始

                    if (!m_bMatchAtStart && !bAccepted)  // we reset everything
                    {
                        nIndex = (nFoundBeginAt != -1 ? nFoundBeginAt + 1 : nIndex + 1);

                        nFoundBeginAt = -1;
                        nFoundEndAt   = -1;
                        //nIndex++;
                        stateCurr = stateStart;  // start from begining
                    }
                    else
                    {
                        break;
                    }
                }
            }  // end of while..loop

            if (!bAccepted)
            {
                if (stateStart.AcceptingState == false)
                {
                    return(false);
                }
                else // matched an empty string
                {
                    nFoundBeginAt = nSearchStartAt;
                    nFoundEndAt   = nFoundBeginAt - 1;
                    return(true);
                }
            }


            return(true);
        }