partition Function

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

Split string at separator and return the 3 parts (before, the separator and after).

 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)

Type Bound

string

Arguments

Type IntentOptional Attributes Name
class(string), intent(in) :: self

The string.

character(kind=CK, len=*), intent(in), optional :: sep

Separator.

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

after the separator.


Calls

proc~~partition~~CallsGraph proc~partition stringifor_string_t::string%partition raw raw proc~partition->raw

Called by

proc~~partition~~CalledByGraph proc~partition stringifor_string_t::string%partition proc~split stringifor_string_t::string%split proc~split->proc~partition proc~camelcase stringifor_string_t::string%camelcase proc~camelcase->proc~split proc~glob_string stringifor_string_t::string%glob_string proc~glob_string->proc~split proc~snakecase stringifor_string_t::string%snakecase proc~snakecase->proc~split proc~split_chunked stringifor_string_t::string%split_chunked proc~split_chunked->proc~split proc~startcase stringifor_string_t::string%startcase proc~startcase->proc~split proc~write_lines~2 stringifor_string_t::string%write_lines proc~write_lines~2->proc~split interface~glob stringifor_string_t::glob interface~glob->proc~glob_string proc~glob_character stringifor_string_t::string%glob_character interface~glob->proc~glob_character proc~write_file~2 stringifor_string_t::string%write_file proc~write_file~2->proc~write_lines~2 proc~glob_character->interface~glob program~volatile_doctest~16 volatile_doctest program~volatile_doctest~16->interface~glob program~volatile_doctest~69 volatile_doctest program~volatile_doctest~69->interface~glob program~volatile_doctest~80 volatile_doctest program~volatile_doctest~80->interface~glob

Contents

Source Code


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