unquote(3f) - [M_strings:QUOTES] remove quotes from string as if read with list-directed input (LICENSE:PD)
Synopsis
Description
Options
Returns
Examples
Author
License
function unquote(quoted_str,esc) result (unquoted_str)
character(len=*),intent(in) :: quoted_str character(len=1),optional,intent(in) :: esc character(len=:),allocatable :: unquoted_str
Remove quotes from a CHARACTER variable as if it was read using list-directed input. This is particularly useful for processing tokens read from input such as CSV files.Fortran can now read using list-directed input from an internal file, which should handle quoted strings, but list-directed input does not support escape characters, which UNQUOTE(3f) does.
quoted_str input string to remove quotes from, using the rules of list-directed input (two adjacent quotes inside a quoted region are replaced by a single quote, a single quote or double quote is selected as the delimiter based on which is encountered first going from left to right, ...) esc optional character used to protect the next quote character from being processed as a quote, but simply as a plain character.
unquoted_str The output string, which is based on removing quotes from quoted_str.
Sample program:
program demo_unquote use M_strings, only : unquote implicit none character(len=128) :: quoted_str character(len=:),allocatable :: unquoted_str character(len=1),parameter :: esc=#146; character(len=1024) :: iomsg integer :: iostat character(len=1024) :: dummy do write(*,(a),advance=no)Enter test string: read(*,(a),iostat=iostat,iomsg=iomsg)quoted_str if(iostat /= 0)then write(*,*)trim(iomsg) exit endif! the original string write(*,(a))QUOTED [//trim(quoted_str)//]
! the string processed by unquote(3f) unquoted_str=unquote(trim(quoted_str),esc) write(*,(a))UNQUOTED [//unquoted_str//]
! read the string list-directed to compare the results read(quoted_str,*,iostat=iostat,iomsg=iomsg)dummy if(iostat /= 0)then write(*,*)trim(iomsg) else write(*,(a))LIST DIRECTED[//trim(dummy)//] endif enddo end program demo_unquote
John S. Urban
Public Domain
Nemo Release 3.1 | unquote (3m_strings) | January 10, 2025 |