escape Function

public function escape(string) result(ans)

Converting C escape characters to Fortran equivalents

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: string

input string

Return Value character(len=:), allocatable

output string


Contents

Source Code


Source Code

    function escape(string) result(ans)
        character(len=*), intent(in) :: string  !! input string
        character(:), allocatable :: ans        !! output string
        character(len=1) :: tmp(len(string))
        integer :: i, j, length, count

        length = len(string)
        if (length < 2) then
            allocate(ans, source=string)
            return
        end if

        tmp = transfer(string, tmp)
        count = 0

        !$omp parallel do private(i, j) reduction(+:count)
        do i = 1, length - 1
            if (tmp(i) == backslash) then
                j = index(c_escape_chars, tmp(i + 1))
                if (j > 0) then
                    tmp(i) = null_char    ! mark the backslash by NULL character
                    tmp(i + 1) = fortran_escape_chars(j)
                    count = count + 1
                end if
            end if
        end do

        ans = transfer(pack(tmp, tmp /= null_char), string(:length - count))

    end function escape