strjoin_strings Function

private pure function strjoin_strings(array, sep) result(join)

Arguments

TypeIntentOptionalAttributesName
class(string), intent(in) :: array(1:)
character(kind=CK,len=*), intent(in), optional :: sep

Return Value type(string)


Called by

proc~~strjoin_strings~~CalledByGraph proc~strjoin_strings strjoin_strings interface~strjoin strjoin interface~strjoin->proc~strjoin_strings

Contents

Source Code


Source Code

   pure function strjoin_strings(array, sep) result(join)
   !< Return a string that is a join of an array of strings.
   !<
   !< The join-separator is set equals to a null string '' if custom separator isn't specified.
   !<
   !<```fortran
   !< type(string)     :: strings(3)
   !< logical          :: test_passed(5)
   !< strings(1) = 'one'
   !< strings(2) = 'two'
   !< strings(3) = 'three'
   !< test_passed(1) = (strjoin(array=strings)//''==strings(1)//strings(2)//strings(3))
   !< test_passed(2) = (strjoin(array=strings, sep='-')//''==strings(1)//'-'//strings(2)//'-'//strings(3))
   !< call strings(1)%free
   !< strings(2) = 'two'
   !< strings(3) = 'three'
   !< test_passed(3) = (strjoin(array=strings, sep='-')//''==strings(2)//'-'//strings(3))
   !< strings(1) = 'one'
   !< strings(2) = 'two'
   !< call strings(3)%free
   !< test_passed(4) = (strjoin(array=strings, sep='-')//''==strings(1)//'-'//strings(2))
   !< strings(1) = 'one'
   !< call strings(2)%free
   !< strings(3) = 'three'
   !< test_passed(5) = (strjoin(array=strings, sep='-')//''==strings(1)//'-'//strings(3))
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string),             intent(in)           :: array(1:) !< Array to be joined.
   character(kind=CK, len=*), intent(in), optional :: sep       !< Separator.
   type(string)                                    :: join      !< The join of array.
   character(kind=CK, len=:), allocatable          :: sep_      !< Separator, default value.
   integer                                         :: a         !< Counter.

   sep_ = ''
   if (present(sep)) sep_ = sep
   join = ''
   do a=2, size(array, dim=1)
      if (allocated(array(a)%raw))join%raw = join%raw//sep_//array(a)%raw
   enddo
   if (allocated(array(1)%raw)) then
      join%raw = array(1)%raw//join%raw
   else
      join%raw = join%raw(len(sep_)+1:len(join%raw))
   endif
   endfunction strjoin_strings