Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(string), | intent(in) | :: | self | The string. |
||
character(kind=CK,len=*), | intent(in), | optional | :: | sep | Separator. |
after the separator.
Split string at separator and return the 3 parts (before, the separator and after).
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
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. |
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