Skip to content

Commit d04535a

Browse files
committed
Update strings.f90
- Added checks to make sure we do not extend past the length of input character string. [skip ci]
1 parent 1cb4805 commit d04535a

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

strings.f90

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function str_count(str,substr,match_case) result(count)
2626
endif
2727
count=0
2828
do i=1,len_trim(strtmp)
29-
if(strtmp(i:i+(len_trim(substrtmp)-1)).eq.substrtmp)count=count+1
29+
if(strtmp(i:min(len_trim(strtmp),i+(len_trim(substrtmp)-1))).eq.substrtmp)count=count+1
3030
end do
3131
end function str_count
3232

@@ -44,7 +44,7 @@ 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,iend,len_str,len_old,len_new
47+
integer :: i,len_str,len_old,len_new
4848
logical(kind=1), dimension(len_trim(str)) :: work
4949
work(:)=.false.
5050
len_str=len_trim(str)
@@ -53,8 +53,7 @@ function str_replace(str,old,new) result(strout)
5353
strout=""
5454
do i=1,len_str
5555
if(work(i))cycle
56-
iend=min(len_str,i+(len_old-1))
57-
if(str(i:iend).eq.old)then
56+
if(str(i:min(len_str,i+(len_old-1))).eq.old)then
5857
strout=strout//new
5958
work(i:i+(len_old-1))=.true.
6059
else
@@ -178,7 +177,7 @@ function str_uniq(str,delim) result(strout)
178177
endif
179178
strfound=.false.
180179
do nn=1,nuniq
181-
if(col.eq.strout(nn:(nn+len(col))-1))then
180+
if(col.eq.strout(nn:min(len_trim(strout),(nn+len(col))-1)))then
182181
strfound=.true.
183182
exit
184183
endif

0 commit comments

Comments
 (0)