strip_comments Subroutine

private subroutine strip_comments(line)

Remove Fortran comments (!) from line

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(inout) :: line

Text to modify in-place


Contents

Source Code


Source Code

  subroutine strip_comments (line)
    implicit none
    !> Text to modify in-place
    character(*), intent (in out) :: line
    logical :: in_single_quotes, in_double_quotes
    integer :: i, length

    length = len_trim(line)
    i = 1
    in_single_quotes = .false.
    in_double_quotes = .false.
    loop: do
       if (in_single_quotes) then
          if (line(i:i) == "'") in_single_quotes = .false.
       else if (in_double_quotes) then
          if (line(i:i) == '"') in_double_quotes = .false.
       else
          select case (line(i:i))
          case ("'")
             in_single_quotes = .true.
          case ('"')
             in_double_quotes = .true.
          case ("!")
             i = i - 1
             exit loop
          end select
       end if
       if (i >= length) exit loop
       i = i + 1
    end do loop
    line = line(1:i)
  end subroutine strip_comments