Skip to content

Commit ac195a0

Browse files
committed
a little efficient
1 parent 1c4e5f7 commit ac195a0

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/stdlib_system.F90

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -953,22 +953,25 @@ subroutine make_directory_all(path, err)
953953
character(len=*), intent(in) :: path
954954
type(state_type), optional, intent(out) :: err
955955

956-
integer :: code, i, indx
956+
character(:), allocatable :: trim_path
957+
integer :: i, indx
957958
type(state_type) :: err0
958959
character(len=1) :: sep
959-
logical :: is_dir
960+
logical :: is_dir, check_is_dir
960961

962+
trim_path = trim(path)
961963
sep = path_sep()
962964
i = 1
963-
indx = find(path, sep, i)
965+
indx = find(trim_path, sep, i)
966+
check_is_dir = .true.
964967

965968
do
966969
! Base case to exit the loop
967-
if (indx == 0 .or. indx == len(trim(path))) then
968-
is_dir = is_directory(path)
970+
if (indx == 0 .or. indx == len(trim_path)) then
971+
is_dir = is_directory(trim_path)
969972

970973
if (.not. is_dir) then
971-
call make_directory(path, err0)
974+
call make_directory(trim_path, err0)
972975

973976
if (err0%error()) then
974977
call err0%handle(err)
@@ -978,19 +981,24 @@ subroutine make_directory_all(path, err)
978981
end if
979982
end if
980983

981-
is_dir = is_directory(path(1:indx))
984+
if (check_is_dir) then
985+
is_dir = is_directory(trim_path(1:indx))
986+
end if
982987

983988
if (.not. is_dir) then
984-
call make_directory(path(1:indx), err0)
989+
! no need for further `is_dir` checks
990+
! all paths going forward need to be created
991+
check_is_dir = .false.
992+
call make_directory(trim_path(1:indx), err0)
985993

986994
if (err0%error()) then
987995
call err0%handle(err)
988996
return
989997
end if
990998
end if
991999

992-
i = i + 1
993-
indx = find(path, sep, i)
1000+
i = i + 1 ! the next occurence of `sep`
1001+
indx = find(trim_path, sep, i)
9941002
end do
9951003
end subroutine make_directory_all
9961004

0 commit comments

Comments
 (0)