unescape Function

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

Arguments

TypeIntentOptionalAttributesName
class(string), intent(in) :: self
character(kind=CK,len=1), intent(in) :: to_unescape
character(kind=CK,len=*), intent(in), optional :: unesc

Return Value type(string)


Contents

Source Code


Source Code

   elemental function unescape(self, to_unescape, unesc) result(unescaped)
   !< Unescape double backslashes (or custom escaped character).
   !<
   !<```fortran
   !< type(string) :: astring
   !< logical      :: test_passed(2)
   !< astring = '^\\s \\d+\\s*'
   !< test_passed(1) = (astring%unescape(to_unescape='\')//''=='^\s \d+\s*')
   !< test_passed(2) = (astring%unescape(to_unescape='s')//''=='^\s \\d+\s*')
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   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
   endfunction unescape