split_lines_first_last Subroutine

public pure subroutine split_lines_first_last(string, first, last)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: string
integer, intent(out), allocatable :: first(:)
integer, intent(out), allocatable :: last(:)

Source Code

pure subroutine split_lines_first_last(string, first, last)
    character(*), intent(in) :: string
    integer, allocatable, intent(out) :: first(:)
    integer, allocatable, intent(out) :: last(:)

    integer, dimension(len(string) + 1) :: istart, iend
    integer :: p, n, slen
    character, parameter :: CR = achar(13)
    character, parameter :: LF = new_line('A')

    slen = len(string)

    n = 0
    if (slen > 0) then
        p = 1
        do while (p <= slen)
            
            if (index(CR//LF, string(p:p)) == 0) then
                n = n + 1
                istart(n) = p
                do while (p <= slen)
                    if (index(CR//LF, string(p:p)) /= 0) exit
                    p = p + 1
                end do
                iend(n) = p - 1
            end if
            
            ! Handle Windows CRLF by skipping LF after CR
            if (p < slen) then 
               if (string(p:p) == CR .and. string(p+1:p+1) == LF) p = p + 1
            endif
            
            p = p + 1
        end do
    end if

    first = istart(:n)
    last = iend(:n)

end subroutine split_lines_first_last