do_find_match_forgex Subroutine

public subroutine do_find_match_forgex(flags, pattern, text, is_exactly)

Uses

Arguments

Type IntentOptional Attributes Name
logical, intent(in) :: flags(:)
character(len=*), intent(in) :: pattern
character(len=*), intent(in) :: text
logical, intent(in) :: is_exactly

Source Code

   subroutine do_find_match_forgex(flags, pattern, text, is_exactly)
      use :: forgex, only: regex, operator(.in.), operator(.match.)
      use :: forgex_parameters_m, only: INVALID_CHAR_INDEX
      use :: forgex_cli_time_measurement_m
      use :: forgex_cli_utils_m, only: text_highlight_green
      implicit none
      logical, intent(in) :: flags(:)
      character(*), intent(in) :: pattern, text
      logical, intent(in) :: is_exactly

      real(real64) :: lap
      logical :: res
      character(:), allocatable :: res_string
      integer :: from, to, unused

      res_string = ''
      from = INVALID_CHAR_INDEX
      to = INVALID_CHAR_INDEX

      call time_begin()
      if (is_exactly) then
         res = pattern .match. text
      else
         res = pattern .in. text
      end if
      lap = time_lap()

      ! Invoke regex subroutine to highlight matched substring.
      call regex(pattern, text, res_string, unused, from, to)

      output: block
         character(NUM_DIGIT_KEY) :: pattern_key, text_key
         character(NUM_DIGIT_KEY) :: total_time, matching_result
         character(NUM_DIGIT_KEY) :: buf(4)

         pattern_key = "pattern:"
         text_key = "text:"
         total_time = "time:"
         matching_result = "result:"
         if (flags(FLAG_NO_TABLE)) then
            write(stdout, *) res
         else
            buf = [pattern_key, text_key, total_time, matching_result]
            call right_justify(buf)
            write(stdout, '(a, 1x, a)') trim(buf(1)), trim(adjustl(pattern))
            write(stdout, '(a, 1x, a)') trim(buf(2)), '"'//text_highlight_green(text, from, to)//'"'
            write(stdout, fmt_out_time) trim(buf(3)), get_lap_time_in_appropriate_unit(lap)
            write(stdout, fmt_out_logi) trim(buf(4)), res
         end if
      end block output

   end subroutine do_find_match_forgex