partition Function

private pure function partition(self, sep) result(partitions)

Type Bound

string

Arguments

Type IntentOptional Attributes Name
class(string), intent(in) :: self
character(kind=CK, len=*), intent(in), optional :: sep

Return Value type(string), (1:3)


Called by

proc~~partition~2~~CalledByGraph proc~partition~2 string%partition proc~split~2 string%split proc~split~2->proc~partition~2 proc~camelcase~2 string%camelcase proc~camelcase~2->proc~split~2 proc~glob_string~2 string%glob_string proc~glob_string~2->proc~split~2 proc~snakecase~2 string%snakecase proc~snakecase~2->proc~split~2 proc~split_chunked~2 string%split_chunked proc~split_chunked~2->proc~split~2 proc~startcase~2 string%startcase proc~startcase~2->proc~split~2 proc~write_lines~2 string%write_lines proc~write_lines~2->proc~split~2 interface~glob~2 glob interface~glob~2->proc~glob_string~2 proc~glob_character~2 string%glob_character interface~glob~2->proc~glob_character~2 none~glob~2 string%glob none~glob~2->proc~glob_string~2 none~glob~2->proc~glob_character~2 proc~write_file~2 string%write_file proc~write_file~2->proc~write_lines~2 proc~glob_character~2->none~glob~2

Source Code

   pure function partition(self, sep) result(partitions)
   !< Split string at separator and return the 3 parts (before, the separator and after).
   !<
   !<```fortran
   !< type(string) :: astring
   !< type(string) :: strings(3)
   !< logical      :: test_passed(3)
   !< astring = 'Hello WorLD!'
   !< strings = astring%partition(sep='lo Wo')
   !< test_passed(1) = (strings(1)//''=='Hel'.and.strings(2)//''=='lo Wo'.and.strings(3)//''=='rLD!')
   !< strings = astring%partition(sep='Hello')
   !< test_passed(2) = (strings(1)//''==''.and.strings(2)//''=='Hello'.and.strings(3)//''==' WorLD!')
   !< astring = 'Hello WorLD!'
   !< strings = astring%partition()
   !< test_passed(3) = (strings(1)//''=='Hello'.and.strings(2)//''==' '.and.strings(3)//''=='WorLD!')
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string),             intent(in)           :: self            !< The string.
   character(kind=CK, len=*), intent(in), optional :: sep             !< Separator.
   type(string)                                    :: partitions(1:3) !< Partions: before the separator, the separator itsels and
                                                                      !< after the separator.
   character(kind=CK, len=:), allocatable          :: sep_            !< Separator, default value.
   integer                                         :: c               !< Character counter.

   if (allocated(self%raw)) then
      sep_ = SPACE ; if (present(sep)) sep_ = sep

      partitions(1) = self
      partitions(2) = sep_
      partitions(3) = ''
      if (len(sep_)>=len(self%raw)) return
      c = index(self%raw, sep_)
      if (c>0) then
         partitions(1)%raw = self%raw(1:c-1)
         partitions(2)%raw = self%raw(c:c+len(sep_)-1)
         partitions(3)%raw = self%raw(c+len(sep_):)
      endif
   endif
   endfunction partition