fortran_name Function

public elemental function fortran_name(line) result(lout)

NAME

 fortran_name(3f) - [M_strings:COMPARE] test if string meets criteria
 for being a fortran name

SYNOPSIS

 elemental function fortran_name(line) result (lout)

  character(len=*),intent(in)  :: line
  logical                      :: lout

DESCRIPTION

 Determines if a string is an allowed Fortran name. To pass the input
 string must be composed of 1 to 63 ASCII characters and start with a
 letter and be composed entirely of alphanumeric characters [a-zA-Z0-9]
 and underscores.

OPTIONS

 LINE   input string to test. Leading spaces are significant but
        trailing spaces are ignored.

RETURNS

 LOUT   a logical value indicating if the input string passed or failed
        the test to see if it is a valid Fortran name or not.

EXAMPLE

Sample program

  program demo_fortran_name
  use M_strings, only : fortran_name
  implicit none
  character(len=20),parameter :: names(*)=[character(len=20) ::  &
   & '_name',         'long_variable_name', 'name_',         &
   & '12L',           'a__b__c  ',          'PropertyOfGas', &
   & '3%3',           '$NAME',              ' ',             &
   & 'Variable-name', 'A',                  'x@x' ]
  integer :: i
     write(*,'(i3,1x,a20,1x,l1)')&
     & (i,names(i),fortran_name(names(i)),i=1,size(names))
  end program demo_fortran_name

Results:

  >  1 _name                F
  >  2 long_variable_name   T
  >  3 name_                T
  >  4 12L                  F
  >  5 a__b__c              T
  >  6 PropertyOfGas        T
  >  7 3%3                  F
  >  8 $NAME                F
  >  9                      F
  > 10 Variable-name        F
  > 11 A                    T
  > 12 x@x                  F

Arguments

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

Return Value logical


Contents

Source Code


Source Code

elemental function fortran_name(line) result (lout)

! ident_75="@(#) M_strings fortran_name(3f) Return .true. if name is a valid Fortran name"

! determine if a string is a valid Fortran name ignoring trailing spaces (but not leading spaces)
character(len=*),parameter   :: int='0123456789'
character(len=*),parameter   :: lower='abcdefghijklmnopqrstuvwxyz'
character(len=*),parameter   :: upper='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
character(len=*),parameter   :: allowed=upper//lower//int//'_'

character(len=*),intent(in)  :: line
character(len=:),allocatable :: name
logical                      :: lout
   name=trim(line)
   if(len(name) /= 0)then
      lout = verify(name(1:1), lower//upper) == 0  &
       & .and. verify(name,allowed) == 0           &
       & .and. len(name) <= 63
   else
      lout = .false.
   endif
end function fortran_name