split Subroutine

private pure subroutine split(self, tokens, sep)

Arguments

Type IntentOptional AttributesName
class(string), intent(in) :: self

The string.

type(string), intent(out), allocatable:: tokens(:)

Tokens substring.

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

Separator.

Description

Return a list of substring in the string, using sep as the delimiter string.


Variables

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

Separator, default value.

integer, public :: No

Number of occurrences of sep.

integer, public :: t

Character counter.

type(string), public :: temporary

Temporary storage.

type(string), public, allocatable:: temp_toks(:,:)

Temporary tokens substring.


Source Code

  pure subroutine split(self, tokens, sep)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Return a list of substring in the string, using sep as the delimiter string.
  !<
  !< @note Multiple subsequent separators are collapsed to one occurence.
  !---------------------------------------------------------------------------------------------------------------------------------
  class(string),             intent(in)           :: self           !< The string.
  character(kind=CK, len=*), intent(in), optional :: sep            !< Separator.
  type(string), allocatable, intent(out)          :: tokens(:)      !< Tokens substring.
  character(kind=CK, len=:), allocatable          :: sep_           !< Separator, default value.
  integer                                         :: No             !< Number of occurrences of sep.
  integer                                         :: t              !< Character counter.
  type(string)                                    :: temporary      !< Temporary storage.
  type(string), allocatable                       :: temp_toks(:,:) !< Temporary tokens substring.
  !---------------------------------------------------------------------------------------------------------------------------------

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

    temporary = self%unique(sep_)
    No = temporary%count(sep_)
    allocate(temp_toks(3, No))
    temp_toks(:, 1) = temporary%partition(sep_)
    if (No>1) then
      do t=2, No
        temp_toks(:, t) = temp_toks(3, t-1)%partition(sep_)
      enddo
    endif
    if (temp_toks(1, 1)%raw/=''.and.temp_toks(3, No)%raw/='') then
      allocate(tokens(No+1))
      do t=1, No
        if (t==No) then
          tokens(t  ) = temp_toks(1, t)
          tokens(t+1) = temp_toks(3, t)
        else
          tokens(t) = temp_toks(1, t)
        endif
      enddo
    elseif (temp_toks(1, 1)%raw/='') then
      allocate(tokens(No))
      do t=1, No
        tokens(t) = temp_toks(1, t)
      enddo
    elseif (temp_toks(3, No)%raw/='') then
      allocate(tokens(No))
      do t=2, No
        if (t==No) then
          tokens(t-1) = temp_toks(1, t)
          tokens(t  ) = temp_toks(3, t)
        else
          tokens(t-1) = temp_toks(1, t)
        endif
      enddo
    else
      allocate(tokens(No-1))
      do t=2, No
        tokens(t-1) = temp_toks(1, t)
      enddo
    endif
  endif
  return
  !---------------------------------------------------------------------------------------------------------------------------------
  endsubroutine split