longest_code Function

public pure function longest_code(text) result(width)

An upper bound of the width of the longest code line of text: the characters of each line before a comment, without the blanks before it, as long_code_lines counts them. Preprocessor lines do not count. Strings are followed across continuation lines, so that a ! inside one does not end the code, but the scan does not tokenize, so it may count more than a token-based count would; never less.

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: text

the formatted text

Return Value integer


Called by

proc~~longest_code~~CalledByGraph proc~longest_code longest_code proc~too_long too_long proc~too_long->proc~longest_code proc~format_text format_text proc~format_text->proc~too_long proc~process_text process_text proc~process_text->proc~format_text proc~process_file process_file proc~process_file->proc~process_text proc~process_stdin process_stdin proc~process_stdin->proc~process_text proc~run run proc~run->proc~process_file proc~run->proc~process_stdin

Source Code

pure integer function longest_code(text) result(width)
    !! An upper bound of the width of the longest code line of `text`: the characters of each line
    !! before a comment, without the blanks before it, as [[long_code_lines]] counts them.
    !! Preprocessor lines do not count. Strings are followed across continuation lines, so that a
    !! `!` inside one does not end the code, but the scan does not tokenize, so it may count more
    !! than a token-based count would; never less.
    character(len=*), intent(in) :: text   !! the formatted text
    integer :: k, c, w, code_w, last_nonblank
    character :: quote
    logical :: line_start, skip, in_comment

    width = 0
    w = 0
    code_w = 0
    quote = ' '
    line_start = .true.
    skip = .false.
    in_comment = .false.
    last_nonblank = 0
    do k = 1, len(text)
        c = iand(ichar(text(k:k)), 255)
        if (c == 13 .and. k < len(text)) then
            if (text(k + 1:k + 1) == achar(10)) cycle      ! CRLF: the line ends at the LF
        end if
        if (c == 10 .or. c == 13) then
            width = max(width, code_w)
            ! a string continued on the next line ends this one with `&`
            if (quote /= ' ' .and. .not. (last_nonblank > 0 .and. text(max(1, last_nonblank):max(1, last_nonblank)) == '&')) &
                quote = ' '
            w = 0
            code_w = 0
            line_start = .true.
            skip = .false.
            in_comment = .false.
            last_nonblank = 0
            cycle
        end if
        if (skip .or. in_comment) cycle
        if (c >= 128 .and. c < 192) cycle                  ! a UTF-8 continuation byte
        w = w + 1
        if (text(k:k) == ' ' .or. text(k:k) == achar(9)) cycle
        if (line_start) then
            line_start = .false.
            if (quote == ' ' .and. (text(k:k) == '#' .or. (index('$@', text(k:k)) > 0 .and. &
                                                           text(min(k + 1, len(text)):min(k + 1, len(text))) == ':'))) then
                skip = .true.                              ! a preprocessor line
                cycle
            end if
        end if
        if (quote /= ' ') then
            if (text(k:k) == quote) quote = ' '            ! a doubled quote reopens at once
        else if (text(k:k) == '"' .or. text(k:k) == "'") then
            quote = text(k:k)
        else if (text(k:k) == '!') then
            in_comment = .true.
            cycle
        end if
        code_w = w
        last_nonblank = k
    end do
    width = max(width, code_w)
end function longest_code