match_dense_dfa_exactly Function

public pure function match_dense_dfa_exactly(automaton, string) result(res)

This procedure reads a text, performs regular expression matching using compiled DFA, and returns .true. if it matches exactly.

Arguments

Type IntentOptional Attributes Name
type(automaton_t), intent(in) :: automaton
character(len=*), intent(in) :: string

Return Value logical


Source Code

   pure function match_dense_dfa_exactly(automaton, string) result(res)
      use :: forgex_utf8_m, only: idxutf8
      implicit none
      type(automaton_t), intent(in) :: automaton
      character(*), intent(in) :: string
      logical :: res

      integer :: cur_i, dst_i ! current and destination index of DFA nodes
      integer :: ci           ! character index
      integer :: next_ci      ! next character index
      integer :: max_match    !

      cur_i = automaton%initial_index

      if (cur_i == DFA_NOT_INIT) then
         error stop "DFA have not been initialized."
      end if

      if (len(string) == 0) then
         res = automaton%dfa%nodes(cur_i)%accepted
         return
      end if

      max_match = 0
      ci = 1
      do while(cur_i /= DFA_INVALID_INDEX)
         if (automaton%dfa%nodes(cur_i)%accepted) then
            max_match = ci
         end if

         if (ci > len(string)) exit

         next_ci = idxutf8(string, ci) + 1

         dst_i = next_state_dense_dfa(automaton, cur_i, string(ci:next_ci-1))

         cur_i = dst_i
         ci = next_ci
      end do

      if (max_match == len(string)+1) then
         res = .true.
      else
         res = .false.
      end if
   end function match_dense_dfa_exactly