insert_string Function

private elemental function insert_string(self, substring, pos) result(inserted)

Arguments

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

The string.

type(string), intent(in) :: substring

Substring.

integer, intent(in) :: pos

Position from which insert substring.

Return Value type(string)

Inserted string.

Description

Insert substring into string at a specified position.


Variables

TypeVisibility AttributesNameInitial
integer, public :: safepos

Safe position from which insert substring.


Source Code

  elemental function insert_string(self, substring, pos) result(inserted)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Insert substring into string at a specified position.
  !---------------------------------------------------------------------------------------------------------------------------------
  class(string), intent(in) :: self      !< The string.
  type(string),  intent(in) :: substring !< Substring.
  integer,       intent(in) :: pos       !< Position from which insert substring.
  type(string)              :: inserted  !< Inserted string.
  integer                   :: safepos   !< Safe position from which insert substring.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  if (allocated(self%raw)) then
    inserted = self
    if (allocated(substring%raw)) then
      safepos = min(max(1, pos), len(self%raw))
      if (safepos==1) then
        inserted%raw = substring%raw//self%raw
      elseif (safepos==len(self%raw)) then
        inserted%raw = self%raw//substring%raw
      else
        inserted%raw = self%raw(1:safepos-1)//substring%raw//self%raw(safepos:)
      endif
    endif
  else
    if (allocated(substring%raw)) inserted%raw = substring%raw
  endif
  return
  !---------------------------------------------------------------------------------------------------------------------------------
  endfunction insert_string