replace_leading_tabs Subroutine

private subroutine replace_leading_tabs(line)

Replaces all leading tabs with space. Note we consider any tabs appearing before the first non-space/tab character.

Arguments

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

Text to modify in-place


Contents

Source Code


Source Code

  subroutine replace_leading_tabs (line)
    use iso_c_binding, only: C_HORIZONTAL_TAB
    implicit none
    !> Text to modify in-place
    character(*), intent (in out) :: line
    integer :: tab_location, i, length
    tab_location = scan(line, C_HORIZONTAL_TAB)
    ! If there are no tabs in the line then return immediately
    if (tab_location == 0) return

    length = len(line)

    ! Consider each character in turn
    do i = 1, length
       if (line(i:i) == ' ') then
          ! If it is a space then just move to next character
          cycle
       else if (line(i:i) == C_HORIZONTAL_TAB) then
          ! If it is a tab replace with a space and move to next character
          line(i:i) = ' '
          cycle
       else
          ! We've reached a non-space/tab character so can stop checking
          exit
       end if
    end do

  end subroutine replace_leading_tabs