Skip to content

Commit 184388e

Browse files
committed
Refactored str_uniq() with much simpler logic.
1 parent ea546a3 commit 184388e

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

src/strings.f90

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module strings
2-
2+
33
contains
4-
4+
55
! -------------------------------------------------------------------------------------
66
! Function: str_count
77
!> @brief Count the occurrences of a substring in a string.
@@ -24,7 +24,7 @@ function str_count(str,substr,match_case) result(count)
2424
strtmp=str_lower(str)
2525
substrtmp=str_lower(substr)
2626
endif
27-
count=0
27+
count=0
2828
do i=1,len_trim(strtmp)
2929
if(strtmp(i:min(len_trim(strtmp),i+(len_trim(substrtmp)-1))).eq.substrtmp)count=count+1
3030
end do
@@ -146,7 +146,7 @@ end function str_split
146146

147147
! -------------------------------------------------------------------------------------
148148
! Function: str_uniq
149-
!> @brief Removed duplicative entries from a \b delimited string.
149+
!> @brief Removed duplicative entries from a \b delimited string.
150150
!> @param[in] str - string to work on
151151
!> @param[in] delim - character delimiter
152152
!> @return modified string
@@ -157,39 +157,35 @@ function str_uniq(str,delim) result(strout)
157157
character(len=1), intent(in) :: delim
158158
character(len=:), allocatable :: strout
159159
character(len=:), allocatable :: ctemp
160-
character(len=:), allocatable :: col
161-
integer :: n,nn,ncols,ndelims,nuniq,strlen
162-
logical(kind=1) :: strfound
163-
ncols=0
164-
ndelims=0
160+
character(len=:), allocatable :: col,ucol
161+
integer :: n,nn
162+
integer :: nmatch,nuniq
163+
nmatch=0
165164
nuniq=0
166-
strlen=0
167-
ctemp=trim(adjustl(str))
168-
strlen=len(ctemp)
169-
ndelims=str_count(ctemp,delim)
170-
ncols=ndelims+1
171165
strout=""
166+
! Work with a copy of str
167+
ctemp=trim(adjustl(str))
168+
! Remove any leading or trailing delimiters
169+
if(ctemp(1:1).eq.delim) ctemp=ctemp(2:len(ctemp))
170+
if(ctemp(len(ctemp):len(ctemp)).eq.demlin) ctemp=ctemp(1:len(ctemp)-1)
171+
ncols=str_count(ctemp,delim)+1
172172
do n=1,ncols
173173
col=str_split(ctemp,delim,n)
174174
if(n.eq.1)then
175-
strout=col//delim
176-
nuniq=len(strout)
175+
strout=strout//col
177176
endif
178-
strfound=.false.
177+
nuniq=str_count(strout,delim)+1
178+
nmatch=0
179179
do nn=1,nuniq
180-
if(col.eq.strout(nn:min(len_trim(strout),(nn+len(col))-1)))then
181-
strfound=.true.
182-
exit
180+
ucol=str_split(strout,delim,nn)
181+
if(col.eq.ucol)then
182+
nmatch=nmatch+1
183183
endif
184184
end do
185-
if(.not.strfound)then
186-
strout=strout//col//delim
187-
nuniq=len(strout)
185+
if(nmatch.eq.0)then
186+
strout=strout//delim//col
188187
endif
189188
end do
190-
if(strout(1:1).eq.delim)strout=strout(2:len(strout))
191-
strlen=len(strout)
192-
if(strout(strlen:strlen).eq.delim)strout=strout(1:len(strout)-1)
193189
end function str_uniq
194190

195191
! -------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)