quote Function

public function quote(str, mode, clip) result(quoted_str)

NAME

 quote(3f) - [M_strings:QUOTES] add quotes to string as if written
 with list-directed input
 (LICENSE:PD)

SYNOPSIS

function quote(str,mode,clip) result (quoted_str)

character(len=*),intent(in)          :: str
character(len=*),optional,intent(in) :: mode
logical,optional,intent(in)          :: clip
character(len=:),allocatable         :: quoted_str

DESCRIPTION

Add quotes to a CHARACTER variable as if it was written using
list-directed input. This is particularly useful for processing
strings to add to CSV files.

OPTIONS

str    input string to add quotes to, using the rules of
       list-directed input (single quotes are replaced by two
       adjacent quotes)
mode   alternate quoting methods are supported:

         DOUBLE   default. replace quote with double quotes
         ESCAPE   replace quotes with backslash-quote instead of
                  double quotes

clip   default is to trim leading and trailing spaces from the
       string. If CLIP is .FALSE. spaces are not trimmed

RESULT

quoted_str  The output string, which is based on adding quotes to STR.

EXAMPLE

Sample program:

program demo_quote
use M_strings, only : quote
implicit none
character(len=:),allocatable :: str
character(len=1024)          :: msg
integer                      :: ios
character(len=80)            :: inline
   do
      write(*,'(a)',advance='no')'Enter test string:'
      read(*,'(a)',iostat=ios,iomsg=msg)inline
      if(ios /= 0)then
         write(*,*)trim(inline)
         exit
      endif

      ! the original string
      write(*,'(a)')'ORIGINAL     ['//trim(inline)//']'

      ! the string processed by quote(3f)
      str=quote(inline)
      write(*,'(a)')'QUOTED     ['//str//']'

      ! write the string list-directed to compare the results
      write(*,'(a)',iostat=ios,iomsg=msg) 'LIST DIRECTED:'
      write(*,*,iostat=ios,iomsg=msg,delim='none') inline
      write(*,*,iostat=ios,iomsg=msg,delim='quote') inline
      write(*,*,iostat=ios,iomsg=msg,delim='apostrophe') inline
   enddo
end program demo_quote

AUTHOR

John S. Urban

LICENSE

Public Domain

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: str
character(len=*), intent(in), optional :: mode
logical, intent(in), optional :: clip

Return Value character(len=:), allocatable


Contents

Source Code


Source Code

function quote(str,mode,clip) result (quoted_str)
character(len=*),intent(in)          :: str                ! the string to be quoted
character(len=*),optional,intent(in) :: mode
logical,optional,intent(in)          :: clip
character(len=:),allocatable         :: quoted_str

character(len=1),parameter           :: double_quote = '"'
character(len=20)                    :: local_mode

   if(present(clip))then
      if(clip)then
         quoted_str=adjustl(str)
      else
         quoted_str=str
      endif
   else
      quoted_str=str
   endif

   local_mode=merge_str(mode,'DOUBLE',present(mode))

   select case(lower(local_mode))
   case('double')
      quoted_str=double_quote//trim(replace(quoted_str,'"','""'))//double_quote
   case('escape')
      quoted_str=double_quote//trim(replace(quoted_str,'"','\"'))//double_quote
   case default
      call journal('sc','*quote* ERROR: unknown quote mode ',local_mode)
      quoted_str=str
   end select
!-----------------------------------------------------------------------------------------------------------------------------------
end function quote