glob_to_regex Function

public function glob_to_regex(glob) result(re)

A regular expression (for a full match) equivalent to the glob pattern glob.

Arguments

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

the glob pattern

Return Value character(len=:), allocatable


Calls

proc~~glob_to_regex~~CallsGraph proc~glob_to_regex glob_to_regex re_escape re_escape proc~glob_to_regex->re_escape

Called by

proc~~glob_to_regex~~CalledByGraph proc~glob_to_regex glob_to_regex proc~collect_files collect_files proc~collect_files->proc~glob_to_regex proc~directory_files directory_files proc~directory_files->proc~collect_files proc~run run proc~run->proc~directory_files

Source Code

function glob_to_regex(glob) result(re)
    !! A regular expression (for a full match) equivalent to the glob pattern `glob`.
    character(len=*), intent(in) :: glob   !! the glob pattern
    character(len=:), allocatable :: re
    integer :: i, j
    re = ''
    i = 1
    do while (i <= len(glob))
        select case (glob(i:i))
        case ('*')
            if (i < len(glob)) then
                if (glob(i + 1:i + 1) == '*') then
                    if (i + 1 < len(glob)) then
                        if (glob(i + 2:i + 2) == '/') then
                            re = re//'(?:.*/)?'          ! `**/`: any number of directories
                            i = i + 3
                            cycle
                        end if
                    end if
                    re = re//'.*'
                    i = i + 2
                    cycle
                end if
            end if
            re = re//'[^/]*'
        case ('?')
            re = re//'[^/]'
        case ('[')
            j = index(glob(i + 1:), ']')
            if (j > 1) then
                if (glob(i + 1:i + 1) == '!') then
                    re = re//'[^'//glob(i + 2:i + j)
                else
                    re = re//glob(i:i + j)
                end if
                i = i + j + 1
                cycle
            end if
            re = re//'\['
        case default
            re = re//re_escape(glob(i:i))
        end select
        i = i + 1
    end do
end function glob_to_regex