unique Function

private elemental function unique(self, substring) result(uniq)

Arguments

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

The string.

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

Substring which multiple occurences must be reduced to one.

Return Value type(string)

String parsed.

Description

Reduce to one (unique) multiple (sequential) occurrences of a substring into a string.

For example the string ' ab-cre-cre-ab' is reduce to 'ab-cre-ab' if the substring is '-cre'.


Variables

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

Substring, default value.


Source Code

  elemental function unique(self, substring) result(uniq)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Reduce to one (unique) multiple (sequential) occurrences of a substring into a string.
  !<
  !< For example the string ' ab-cre-cre-ab' is reduce to 'ab-cre-ab' if the substring is '-cre'.
  !< @note Eventual multiple trailing white space are not reduced to one occurrence.
  !---------------------------------------------------------------------------------------------------------------------------------
  class(string),             intent(in)           :: self       !< The string.
  character(kind=CK, len=*), intent(in), optional :: substring  !< Substring which multiple occurences must be reduced to one.
  character(kind=CK, len=:), allocatable          :: substring_ !< Substring, default value.
  type(string)                                    :: uniq       !< String parsed.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  if (allocated(self%raw)) then
    substring_ = SPACE ; if (present(substring)) substring_ = substring

    uniq = self
    do
      if (.not.uniq%index(repeat(substring_, 2))>0) exit
      uniq = uniq%replace(old=repeat(substring_, 2), new=substring_)
    enddo
  endif
  return
  !---------------------------------------------------------------------------------------------------------------------------------
  endfunction unique