Skip to content

Commit 92d8cf5

Browse files
committed
Update strings.f90
Recoded str_uniq function. It worked well for small strings, but crashes were occurring with long strings. I was able to replace and optimize with another string uniq function I had built a while ago.
1 parent a717f17 commit 92d8cf5

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

strings.f90

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,39 @@ function str_uniq(str,delim) result(strout)
138138
character(len=*), intent(in) :: str
139139
character(len=1), intent(in) :: delim
140140
character(len=:), allocatable :: strout
141-
integer :: i,ii,ncols
142-
logical(kind=1), allocatable, dimension(:) :: work
143-
if(allocated(work))deallocate(work)
144-
ncols=str_count(str,delim)+1
145-
allocate(work(ncols))
146-
work(:)=.false.
147-
do i=1,ncols
148-
do ii=i+1,ncols
149-
if(str_split(str,delim,ii).eq.str_split(str,delim,i))work(ii)=.true.
150-
end do
151-
if(.not.work(i))then
152-
if(len_trim(strout).eq.0)then
153-
strout=str_split(str,delim,i)
154-
elseif(len_trim(strout).gt.0)then
155-
strout=strout//delim//str_split(str,delim,i)
141+
character(len=:), allocatable :: ctemp
142+
character(len=:), allocatable :: col
143+
integer :: n,nn,ncols,ndelims,nuniq,strlen
144+
logical(kind=1) :: strfound
145+
ncols=0
146+
ndelims=0
147+
nuniq=0
148+
strlen=0
149+
ctemp=trim(adjustl(str))
150+
strlen=len(ctemp)
151+
ndelims=str_count(ctemp,delim)
152+
ncols=ndelims+1
153+
do n=1,ncols
154+
col=str_split(ctemp,delim,n)
155+
if(n.eq.1)then
156+
strout=col//delim
157+
nuniq=len(strout)
158+
endif
159+
strfound=.false.
160+
do nn=1,nuniq
161+
if(col.eq.strout(nn:(nn+len(col))-1))then
162+
strfound=.true.
163+
exit
156164
endif
165+
end do
166+
if(.not.strfound)then
167+
strout=strout//col//delim
168+
nuniq=len(strout)
157169
endif
158170
end do
159-
if(allocated(work))deallocate(work)
171+
if(strout(1:1).eq.delim)strout=strout(2:len(strout))
172+
strlen=len(strout)
173+
if(strout(strlen:strlen).eq.delim)strout=strout(1:len(strout)-1)
160174
end function str_uniq
161175

162176
! -------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)