Skip to content

Commit

Permalink
Correctly trim white spaces left and right of string for reliable string
Browse files Browse the repository at this point in the history
to int or real conversion.
  • Loading branch information
theurich committed Aug 24, 2023
1 parent f6568e9 commit 8843d07
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/Infrastructure/Util/interface/ESMF_Util.F90
Original file line number Diff line number Diff line change
Expand Up @@ -860,9 +860,10 @@ function ESMF_UtilString2Int(string, keywordEnforcer, &
!EOPI
!-----------------------------------------------------------------------------
! local variables
logical :: ssL, svL
integer :: i
integer :: ioerr
logical :: ssL, svL
integer :: i
integer :: ioerr
character(:), allocatable :: tempString

if (present(rc)) rc = ESMF_SUCCESS

Expand All @@ -881,7 +882,9 @@ function ESMF_UtilString2Int(string, keywordEnforcer, &
rcToReturn=rc)
return ! bail out
endif


tempString = trim(adjustl(string))! remove leading and trailing white spaces

if (ssL) then
! special strings and values present
if (size(specialStringList) /= size(specialValueList)) then
Expand All @@ -894,20 +897,20 @@ function ESMF_UtilString2Int(string, keywordEnforcer, &
return ! bail out
endif
do i=1, size(specialStringList)
if (trim(string)==trim(specialStringList(i))) then
if (tempString==trim(specialStringList(i))) then
! found a matching special string
ESMF_UtilString2Int = specialValueList(i)
return ! successful early return
endif
enddo
endif

if (verify(trim(adjustl(string)),"-+0123456789e") == 0) then
if (verify(tempString,"-+0123456789") == 0) then
! should convert to integer just fine
read (string, "(i12)", iostat=ioerr) ESMF_UtilString2Int
read (tempString, "(i12)", iostat=ioerr) ESMF_UtilString2Int
if (ioerr /= 0) then
call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
msg="The string '"//trim(string)//"' could not be converted to integer.", &
msg="The string '"//tempString//"' could not be converted to integer.", &
line=__LINE__, &
file=ESMF_FILENAME, &
rcToReturn=rc)
Expand All @@ -916,7 +919,7 @@ function ESMF_UtilString2Int(string, keywordEnforcer, &
else
! the string contains characters besides numbers
call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
msg="The string '"//trim(string)//"' contains characters besides "// &
msg="The string '"//tempString//"' contains characters besides "// &
"numbers, cannot convert to integer.", &
line=__LINE__, &
file=ESMF_FILENAME, &
Expand Down Expand Up @@ -958,18 +961,21 @@ function ESMF_UtilString2Real(string, keywordEnforcer, rc)
!EOPI
!-----------------------------------------------------------------------------
! local variables
integer :: ioerr
integer :: ioerr
character(:), allocatable :: tempString

if (present(rc)) rc = ESMF_SUCCESS

ESMF_UtilString2Real = 0 ! initialize

if (verify(trim(adjustl(string)),".-+0123456789e") == 0) then
tempString = trim(adjustl(string))! remove leading and trailing white spaces

if (verify(tempString,".-+0123456789e") == 0) then
! should convert to real just fine
read (string, *, iostat=ioerr) ESMF_UtilString2Real
read (tempString, *, iostat=ioerr) ESMF_UtilString2Real
if (ioerr /= 0) then
call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
msg="The string '"//trim(string)//"' could not be converted to real.", &
msg="The string '"//tempString//"' could not be converted to real.", &
line=__LINE__, &
file=ESMF_FILENAME, &
rcToReturn=rc)
Expand All @@ -978,7 +984,7 @@ function ESMF_UtilString2Real(string, keywordEnforcer, rc)
else
! the string contains characters besides numbers
call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
msg="The string '"//trim(string)//"' contains characters besides "// &
msg="The string '"//tempString//"' contains characters besides "// &
"numbers, cannot convert to real.", &
line=__LINE__, &
file=ESMF_FILENAME, &
Expand Down

0 comments on commit 8843d07

Please sign in to comment.