partition Function

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

Arguments

Type IntentOptional AttributesName
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.

Description

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


Variables

TypeVisibility AttributesNameInitial
character(kind=CK,len=:), public, allocatable:: sep_

Separator, default value.

integer, public :: c

Character counter.

character(kind=CK,len=:), public, allocatable:: temporary

Temporary storage, workaround for GNU bug.


Source Code

  pure function partition(self, sep) result(partitions)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Split string at separator and return the 3 parts (before, the separator and after).
  !---------------------------------------------------------------------------------------------------------------------------------
  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.
#ifdef __GFORTRAN__
  character(kind=CK, len=:), allocatable          :: temporary       !< Temporary storage, workaround for GNU bug.
#endif
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  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
#ifdef __GFORTRAN__
      temporary = self%raw
      partitions(1)%raw = temporary(1:c-1)
      partitions(2)%raw = temporary(c:c+len(sep_)-1)
      partitions(3)%raw = temporary(c+len(sep_):)
#else
      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
  endif
  return
  !---------------------------------------------------------------------------------------------------------------------------------
  endfunction partition