Skip to content

Commit 1cb4805

Browse files
committed
Update strings.f90
- Fixed issue in str_replace where the input str indexing would be greater than the str length. [skip ci]
1 parent 1d74db7 commit 1cb4805

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

strings.f90

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,17 @@ function str_replace(str,old,new) result(strout)
4444
character(len=*), intent(in) :: old
4545
character(len=*), intent(in) :: new
4646
character(len=:), allocatable :: strout
47-
integer :: i,len_old,len_new
47+
integer :: i,iend,len_str,len_old,len_new
4848
logical(kind=1), dimension(len_trim(str)) :: work
4949
work(:)=.false.
50+
len_str=len_trim(str)
5051
len_old=len_trim(old)
5152
len_new=len_trim(new)
5253
strout=""
53-
do i=1,len_trim(str)
54+
do i=1,len_str
5455
if(work(i))cycle
55-
if(str(i:i+(len_old-1)).eq.old)then
56+
iend=min(len_str,i+(len_old-1))
57+
if(str(i:iend).eq.old)then
5658
strout=strout//new
5759
work(i:i+(len_old-1))=.true.
5860
else
@@ -310,4 +312,4 @@ function str_swapcase(str) result(strout)
310312
end do
311313
end function str_swapcase
312314

313-
end module strings
315+
end module strings

0 commit comments

Comments
 (0)