fdoc

subroutineclosing_markpureprivate

pure subroutine closing_mark(text, start, c, width, close)

Find where emphasis of width marks c opened before start closes.

Arguments

NameTypeIntentAttributesDescription
textcharacter(len=*)in

the text

startintegerin

where its content starts

ccharacter(len=1)in

* or _

widthintegerin

how many marks

closeintegerout

where the closing marks start, or 0

Source

Show the code
pure subroutine closing_mark(text, start, c, width, close)
    character(len=*), intent(in) :: text    !! the text
    integer, intent(in) :: start            !! where its content starts
    character, intent(in) :: c              !! `*` or `_`
    integer, intent(in) :: width            !! how many marks
    integer, intent(out) :: close           !! where the closing marks start, or 0
    integer :: k, run

    close = 0
    k = start
    do while (k <= len(text) - width + 1)
        if (text(k:k) == '\') then
            k = k + 2
            cycle
        end if
        if (text(k:k) == '`') then
            run = index(text(k + 1:), '`')
            if (run > 0) k = k + run
        end if
        if (text(k:k) /= c .or. k == start) then
            k = k + 1
            cycle
        end if
        run = verify(text(k:)//' ', c) - 1
        ! marks with blanks on both sides are ordinary text
        if (run <= 3 .and. scan(text(k - 1:k - 1), ' '//NL) == 1 .and. &
            (k + run > len(text) .or. scan(text(min(k + run, len(text)):min(k + run, len(text))), ' '//NL) == 1)) then
            k = k + run
            cycle
        end if
        if (run >= width) then
            if (c == '*') then
                close = merge(k, k + run - width, width == 1)
                return
            end if
            ! `_` must not be followed by a word character
            if (k + run > len(text)) then
                close = k + run - width
                return
            end if
            if (index(WORD_CHARS, text(k + run:k + run)) == 0) then
                close = k + run - width
                return
            end if
        end if
        k = k + run
    end do
end subroutine closing_mark

Graphs

Called by