unescape Function

private elemental function unescape(self, to_unescape, unesc) result(unescaped)

Arguments

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

The string.

character(kind=CK,len=1), intent(in) :: to_unescape

Character to be unescaped.

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

Character used to unescape.

Return Value type(string)

Escaped string.

Description

Unescape double backslashes (or custom escaped character).


Variables

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

Character to unescape, local variable.

integer, public :: c

Character counter.


Source Code

  elemental function unescape(self, to_unescape, unesc) result(unescaped)
  !---------------------------------------------------------------------------------------------------------------------------------
  !< Unescape double backslashes (or custom escaped character).
  !---------------------------------------------------------------------------------------------------------------------------------
  class(string),             intent(in)           :: self        !< The string.
  character(kind=CK, len=1), intent(in)           :: to_unescape !< Character to be unescaped.
  character(kind=CK, len=*), intent(in), optional :: unesc       !< Character used to unescape.
  type(string)                                    :: unescaped   !< Escaped string.
  character(kind=CK, len=:), allocatable          :: unesc_      !< Character to unescape, local variable.
  integer                                         :: c           !< Character counter.
  !---------------------------------------------------------------------------------------------------------------------------------

  !---------------------------------------------------------------------------------------------------------------------------------
  if (allocated(self%raw)) then
    unesc_ = '' ; if (present(unesc)) unesc_ = unesc
    unescaped%raw = ''
    c = 1
    do
      if (c>len(self%raw)) exit
      if (c==len(self%raw)) then
        unescaped%raw = unescaped%raw//self%raw(c:c)
        exit
      else
        if (self%raw(c:c+1)==BACKSLASH//to_unescape) then
          unescaped%raw = unescaped%raw//to_unescape
          c = c + 2
        else
          unescaped%raw = unescaped%raw//self%raw(c:c)
          c = c + 1
        endif
      endif
    enddo
  endif
  return
  !---------------------------------------------------------------------------------------------------------------------------------
  endfunction unescape