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.


Called by

proc~~partition~~CalledByGraph proc~partition string%partition proc~split string%split proc~split->proc~partition program~volatile_doctest~92 volatile_doctest program~volatile_doctest~92->proc~partition proc~camelcase string%camelcase proc~camelcase->proc~split proc~glob_string string%glob_string proc~glob_string->proc~split proc~snakecase string%snakecase proc~snakecase->proc~split proc~split_chunked string%split_chunked proc~split_chunked->proc~split proc~startcase string%startcase proc~startcase->proc~split proc~write_lines~2 string%write_lines proc~write_lines~2->proc~split program~stringifor_test_csv_naive_parser stringifor_test_csv_naive_parser program~stringifor_test_csv_naive_parser->proc~split program~stringifor_test_parse_large_csv stringifor_test_parse_large_csv program~stringifor_test_parse_large_csv->proc~split program~stringifor_test_parse_large_csv->proc~split_chunked program~volatile_doctest~107 volatile_doctest program~volatile_doctest~107->proc~split program~volatile_doctest~15 volatile_doctest program~volatile_doctest~15->proc~split program~volatile_doctest~17 volatile_doctest program~volatile_doctest~17->proc~split program~volatile_doctest~23 volatile_doctest program~volatile_doctest~23->proc~split program~volatile_doctest~44 volatile_doctest program~volatile_doctest~44->proc~split proc~write_file~2 string%write_file program~volatile_doctest~44->proc~write_file~2 interface~glob glob interface~glob->proc~glob_string proc~glob_character string%glob_character interface~glob->proc~glob_character none~glob string%glob none~glob->proc~glob_string none~glob->proc~glob_character proc~write_file~2->proc~write_lines~2 program~volatile_doctest~18 volatile_doctest program~volatile_doctest~18->proc~camelcase program~volatile_doctest~2 volatile_doctest program~volatile_doctest~2->proc~startcase program~volatile_doctest~75 volatile_doctest program~volatile_doctest~75->proc~split_chunked program~volatile_doctest~98 volatile_doctest program~volatile_doctest~98->proc~snakecase proc~glob_character->none~glob program~volatile_doctest~100 volatile_doctest program~volatile_doctest~100->interface~glob program~volatile_doctest~103 volatile_doctest program~volatile_doctest~103->none~glob program~volatile_doctest~82 volatile_doctest program~volatile_doctest~82->none~glob

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