expand(3f) - [M_strings:NONALPHA] expand C-like escape sequences
(LICENSE:PD)
function expand(line,escape) result(lineout)
character(len=*) :: line
character(len=1),intent(in),optional :: escape
character(len=:),allocatable :: lineout
EXPAND() expands sequences used to represent commonly used escape
sequences or control characters. By default ...
Escape sequences
\ backslash
a alert (BEL) -- g is an alias for a
b backspace
c suppress further output
e escape
f form feed
n new line
r carriage return
t horizontal tab
v vertical tab
oNNN byte with octal value NNN (3 digits)
dNNN byte with decimal value NNN (3 digits)
xHH byte with hexadecimal value HH (2 digits) -- h is an alias for x
The default escape character is the backslash, but this may be
changed using the optional parameter ESCAPE.
Sample Program:
program demo_expand
! test filter to expand escape sequences in input lines
use M_strings, only : expand
character(len=1024) :: line
integer :: ios
READFILE: block
do
read(*,'(A)',iostat=ios)line
if(ios /= 0) exit READFILE
write(*,'(a)')trim(expand(line))
enddo
endblock READFILE
end program demo_expand
Sample input:
\e[2J
\tABC\tabc
\tA\a
\nONE\nTWO\nTHREE
John S. Urban
Public Domain
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | line | |||
character(len=1), | intent(in), | optional | :: | escape |
function expand(line,escape) result(lineout)
!x!USE ISO_C_BINDING ,ONLY: c_horizontal_tab
! ident_31="@(#) M_strings expand(3f) return string with escape sequences expanded"
character(len=*),parameter :: c_horizontal_tab=char(9)
character(len=*),intent(in) :: line
character(len=1),intent(in),optional :: escape ! escape character. Default is backslash
! expand escape sequences found in input string
! Escape sequences
! %% escape character %a alert (BEL) -- gi is an alias for a
! %b backspace %c suppress further output
! %e escape %E escape
! %f form feed %n new line
! %r carriage return %t horizontal tab
! %v vertical tab
! %oNNN byte with octal value NNN (3 digits)
! %dNNN byte with decimal value NNN (3 digits)
! %xHH byte with hexadecimal value HH (2 digits) -- h is an alias for x
character(len=1) :: esc ! escape character. Default is %
character(len=:),allocatable :: lineout
integer :: i
integer :: lgth
character(len=3) :: thr
integer :: xxx
integer :: ios
i=0 ! pointer into input
lgth=len_trim(line)
lineout=''
if(lgth == 0)return
if (present(escape))then
esc=escape
else
esc=char(92)
endif
EXP: do
i=i+1
if(i > lgth)exit
if(line(i:i) == esc)then
i=i+1
if(i > lgth)exit
if(line(i:i) /= esc)then
BACKSLASH: select case(line(i:i))
case('a','A','g','G');lineout=lineout//char( 7) ! %a alert (BEL)
case('b','B');lineout=lineout//char( 8) ! %b backspace
case('c','C');exit EXP ! %c suppress further output
case('d','D') ! %d Dnnn decimal value
thr=line(i+1:)
read(thr,'(i3)',iostat=ios)xxx
lineout=lineout//char(xxx)
i=i+3
case('e','E');lineout=lineout//char( 27) ! %e escape
case('f','F');lineout=lineout//char( 12) ! %f form feed
case('n','N');lineout=lineout//char( 10) ! %n new line
!case('n','N');lineout=lineout//new_line('A') ! %n new line
case('o','O')
thr=line(i+1:)
read(thr,'(o3)',iostat=ios)xxx
lineout=lineout//char(xxx)
i=i+3
case('r','R');lineout=lineout//char( 13) ! %r carriage return
case('t','T');lineout=lineout//c_horizontal_tab ! %t horizontal tab
case('v','V');lineout=lineout//char( 11) ! %v vertical tab
case('x','X','h','H') ! %x xHH byte with hexadecimal value HH (1 to 2 digits)
thr=line(i+1:)
read(thr,'(z2)',iostat=ios)xxx
lineout=lineout//char(xxx)
i=i+2
end select BACKSLASH
else
lineout=lineout//esc ! escape character, defaults to backslash
endif
else
lineout=lineout//line(i:i)
endif
if(i >= lgth)exit EXP
enddo EXP
end function expand