functionlexical_cleanprivate
character(len=:) function lexical_clean(path) result(out)
Remove empty, . and .. components from absolute path.
Arguments
| Name | Type | Intent | Attributes | Description |
|---|---|---|---|---|
| path | character(len=*) | in | the path |
Result
| Name | Type | Attributes | Description |
|---|---|---|---|
| out | character(len=:) | privateallocatable |
Calls
Source
Show the code
function lexical_clean(path) result(out)
character(len=*), intent(in) :: path !! the path
character(len=:), allocatable :: out
type(string_t), allocatable :: parts(:), kept(:)
integer :: k, n, first
first = merge(3, 1, has_drive(path))
parts = split_on(path(first:), '/')
allocate (kept(size(parts)))
n = 0
do k = 1, size(parts)
if (parts(k)%s == '' .or. parts(k)%s == '.') cycle
if (parts(k)%s == '..') then
n = max(0, n - 1)
cycle
end if
n = n + 1
kept(n)%s = parts(k)%s
end do
out = path(:first - 1)//'/'//join(kept(1:n), '/')
end function lexical_clean