upper_quoted Function

public pure elemental function upper_quoted(str) result(string)

NAME

upper_quoted(3f) - [M_strings:CASE] elemental function converts string to miniscule skipping strings quoted per Fortran syntax rules (LICENSE:PD)

SYNOPSIS

elemental pure function upper_quoted(str) result (string)

 character(*), intent(in)    :: str
 character(len(str))         :: string  ! output string

DESCRIPTION

upper_quoted(string) returns a copy of the input string with all not-quoted
characters converted to uppercase, assuming ASCII character sets
are being used. The quoting rules are the same as for Fortran source.
Either a single or double quote starts a quoted string, and a quote
character of the same type is doubled when it appears internally in
the quoted string. If a double quote quotes the string single quotes
may appear in the quoted string as single characters, and vice-versa
for single quotes.

OPTIONS

str    string to convert to uppercase

RESULTS

upper  copy of the input string with all unquoted characters converted
       to uppercase

EXAMPLE

Sample program:

 program demo_upper_quoted
 use M_strings, only: upper_quoted
 implicit none
 character(len=:),allocatable  :: s
 s=' ABCDEFG abcdefg "Double-Quoted" ''Single-Quoted'' "with ""&
    & Quote" everything else'
    write(*,*) 'mixed-case input string is ....',s
    write(*,*) 'upper-case output string is ...',upper_quoted(s)
    write(*,'(1x,a,*(a:,"+"))') 'upper_quoted(3f) is elemental ==>', &
    & upper_quoted(["abc","def","ghi"])
 end program demo_upper_quoted

Expected output:

 mixed-case input string is .... ABCDEFG abcdefg "Double-Quoted"
 'Single-Quoted' "with "" Quote" everything else
 upper-case output string is ... ABCDEFG ABCDEFG "Double-Quoted"
 'Single-Quoted' "with "" Quote" EVERYTHING ELSE
 upper_quoted(3f) is elemental ==>ABC+DEF+GHI

AUTHOR

John S. Urban

LICENSE

Public Domain

Arguments

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

Return Value character(len=len)


Contents

Source Code


Source Code

elemental pure function upper_quoted(str) result (string)

! ident_22="@(#) M_strings upper_quoted(3f) elemental function converts string to miniscule skipping strings quoted per Fortran syntax rules"

character(len=*), intent(in)   :: str     ! The input string
character(len=len(str))        :: string  ! The output string
logical                        :: toggle
character(len=1)               :: togglechar
integer                        :: irnk
integer                        :: i
character(len=26), parameter   :: large="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
character(len=26), parameter   :: small="abcdefghijklmnopqrstuvwxyz"

   string=str
   toggle = .TRUE.
   do i = 1, len_trim(string)
      if(toggle) then
         if(string(i:i) == '"' .or. string(i:i) == "'") then
            toggle = .not. toggle
            togglechar = string(i:i)
         endif
         irnk = index(small, string(i:i))
         if(irnk > 0) then
            string(i:i) = large(irnk:irnk)
         endif
      else
         if(string(i:i) == togglechar) toggle = .not. toggle
      endif
   enddo
end function upper_quoted