escape Function

private elemental function escape(self, to_escape, esc) result(escaped)

Arguments

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

Return Value type(string)


Contents

Source Code


Source Code

   elemental function escape(self, to_escape, esc) result(escaped)
   !< Escape backslashes (or custom escape character).
   !<
   !<```fortran
   !< type(string) :: astring
   !< logical      :: test_passed(2)
   !< astring = '^\s \d+\s*'
   !< test_passed(1) = astring%escape(to_escape='\')//''=='^\\s \\d+\\s*'
   !< test_passed(2) = astring%escape(to_escape='\', esc='|')//''=='^|\s |\d+|\s*'
   !< print '(L1)', all(test_passed)
   !<```
   !=> T <<<
   class(string),             intent(in)           :: self      !< The string.
   character(kind=CK, len=1), intent(in)           :: to_escape !< Character to be escaped.
   character(kind=CK, len=*), intent(in), optional :: esc       !< Character used to escape.
   type(string)                                    :: escaped   !< Escaped string.
   character(kind=CK, len=:), allocatable          :: esc_      !< Character to escape, local variable.
   integer                                         :: c         !< Character counter.

   if (allocated(self%raw)) then
     esc_ = BACKSLASH ; if (present(esc)) esc_ = esc
     escaped%raw = ''
     do c=1, len(self%raw)
       if (self%raw(c:c)==to_escape) then
         escaped%raw = escaped%raw//esc_//to_escape
       else
         escaped%raw = escaped%raw//self%raw(c:c)
       endif
     enddo
   endif
   endfunction escape