fdoc

functionsplit_lines

type(string_t) function split_lines(text) result(lines)

The lines of text, ended by LF, CRLF or CR. As in Python's splitlines, a final line ending does not start another line.

Arguments

NameTypeIntentAttributesDescription
textcharacter(len=*)in

the text

Result

NameTypeAttributesDescription
linestype(string_t)privateallocatabledimension(:)

Source

Show the code
function split_lines(text) result(lines)
    character(len=*), intent(in) :: text    !! the text
    type(string_t), allocatable :: lines(:)
    integer :: i, start, n

    n = 0
    do i = 1, len(text)
        if (text(i:i) == achar(10)) n = n + 1
        if (text(i:i) == achar(13)) n = n + 1
    end do
    allocate (lines(n + 1))
    n = 0
    start = 1
    i = 1
    do while (i <= len(text))
        if (text(i:i) == achar(10) .or. text(i:i) == achar(13)) then
            n = n + 1
            lines(n)%s = text(start:i - 1)
            if (text(i:i) == achar(13) .and. i < len(text)) then
                if (text(i + 1:i + 1) == achar(10)) i = i + 1
            end if
            start = i + 1
        end if
        i = i + 1
    end do
    if (start <= len(text)) then
        n = n + 1
        lines(n)%s = text(start:)
    end if
    lines = lines(1:n)
end function split_lines

Graphs

Called by