scount Function

private elemental function scount(self, substring, ignore_isolated) result(No)

Arguments

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

The string.

character(len=*), intent(in) :: substring

Substring.

logical, intent(in), optional :: ignore_isolated

Ignore "isolated" occurrences.

Return Value integer

Number of occurrences.

Description

Count the number of occurences of a substring into a string.


Variables

TypeVisibility AttributesNameInitial
logical, public :: ignore_isolated_

Ignore "isolated" occurrences, local variable.

integer, public :: c1

Counter.

integer, public :: c2

Counter.

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

Temporary storage, workaround for GNU bug.


Source Code

  elemental function scount(self, substring, ignore_isolated) result(No)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Count the number of occurences of a substring into a string.
  !<
  !< @note If `ignore_isolated` is set to true the eventual "isolated" occurences are ignored: an isolated occurrences are those
  !< occurrences happening at the start of string (thus not having a left companion) or at the end of the string (thus not having a
  !< right companion).
  !---------------------------------------------------------------------------------------------------------------------------------
  class(string), intent(in)              :: self             !< The string.
  character(*),  intent(in)              :: substring        !< Substring.
  logical,       intent(in), optional    :: ignore_isolated  !< Ignore "isolated" occurrences.
  integer                                :: No               !< Number of occurrences.
  logical                                :: ignore_isolated_ !< Ignore "isolated" occurrences, local variable.
  integer                                :: c1               !< Counter.
  integer                                :: c2               !< Counter.
#ifdef __GFORTRAN__
  character(kind=CK, len=:), allocatable :: temporary        !< Temporary storage, workaround for GNU bug.
#endif
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  No = 0
  if (allocated(self%raw)) then
    if (len(substring)>len(self%raw)) return
    ignore_isolated_ = .false. ; if (present(ignore_isolated)) ignore_isolated_ = ignore_isolated
#ifdef __GFORTRAN__
    temporary = self%raw
#endif
    c1 = 1
    do
#ifdef __GFORTRAN__
      c2 = index(string=temporary(c1:), substring=substring)
#else
      c2 = index(string=self%raw(c1:), substring=substring)
#endif
      if (c2==0) return
      if (.not.(ignore_isolated_.and.(c1==1.or.c1+c2-1==len(self%raw)-len(substring)+1))) then
        No = No + 1
      endif
      c1 = c1 + c2 - 1 + len(substring)
    enddo
  endif
  !---------------------------------------------------------------------------------------------------------------------------------
  endfunction scount