unique Function

private elemental function unique(self, substring) result(uniq)

Arguments

TypeIntentOptionalAttributesName
class(string), intent(in) :: self
character(kind=CK,len=*), intent(in), optional :: substring

Return Value type(string)


Contents

Source Code


Source Code

   elemental function unique(self, substring) result(uniq)
   !< Reduce to one (unique) multiple (sequential) occurrences of a substring into a string.
   !<
   !< For example the string ' ab-cre-cre-ab' is reduce to 'ab-cre-ab' if the substring is '-cre'.
   !< @note Eventual multiple trailing white space are not reduced to one occurrence.
   !<
   !<```fortran
   !< type(string) :: astring
   !< logical      :: test_passed(1)
   !< astring = '+++ab-++cre-++cre-ab+++++'
   !< test_passed(1) = astring%unique(substring='+')//''=='+ab-+cre-+cre-ab+'
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string),             intent(in)           :: self       !< The string.
   character(kind=CK, len=*), intent(in), optional :: substring  !< Substring which multiple occurences must be reduced to one.
   character(kind=CK, len=:), allocatable          :: substring_ !< Substring, default value.
   type(string)                                    :: uniq       !< String parsed.
#ifdef _NVF
   character(9999)                                 :: nvf_bug  !< Work around for NVFortran bug.
#endif

   if (allocated(self%raw)) then
     substring_ = SPACE ; if (present(substring)) substring_ = substring

     uniq = self
     do
#ifdef _NVF
       nvf_bug = substring_
       if (.not.uniq%index(repeat(trim(nvf_bug), 2))>0) exit
       uniq = uniq%replace(old=repeat(trim(nvf_bug), 2), new=substring_)
#else
       if (.not.uniq%index(repeat(substring_, 2))>0) exit
       uniq = uniq%replace(old=repeat(substring_, 2), new=substring_)
#endif
     enddo
   endif
   endfunction unique