noesc Function

public elemental function noesc(INSTR)

NAME

 noesc(3f) - [M_strings:NONALPHA] convert non-printable characters
 to a space
 (LICENSE:PD)

SYNOPSIS

elemental function noesc(INSTR)

 character(len=*),intent(in) :: INSTR
 character(len=len(instr))   :: noesc

DESCRIPTION

  Convert non-printable characters to a space.

EXAMPLES

Sample Program:

program demo_noesc

 use M_strings, only : noesc
 implicit none
 character(len=128) :: ascii
 character(len=128) :: cleared
 integer            :: i
 ! fill variable with base ASCII character set
 do i=1,128
    ascii(i:i)=char(i-1)
 enddo
 cleared=noesc(ascii)
 write(*,*)'characters and their ADE (ASCII Decimal Equivalent)'
 call ade(ascii)
 write(*,*)'Cleared of non-printable characters'
 call ade(cleared)
 write(*,*)'Cleared string:'
 write(*,*)cleared
 contains
   subroutine ade(string)
   implicit none
   ! the string to print
   character(len=*),intent(in) :: string
   ! number of characters in string to print
   integer :: lgth
   ! counter used to step thru string
   integer :: i
      ! get trimmed length of input string
      lgth=len_trim(string(:len(string)))

      ! replace lower unprintable characters with spaces
      write(*,101)(merge(string(i:i),' ',&
      & iachar(string(i:i)) >= 32        &
      & .and.                            &
      & iachar(string(i:i)) <= 126)      &
      & ,i=1,lgth)

      ! print ADE value of character underneath it
      write(*,202)     (iachar(string(i:i))/100,    i=1,lgth)
      write(*,202)(mod( iachar(string(i:i)),100)/10,i=1,lgth)
      write(*,202)(mod((iachar(string(i:i))),10),   i=1,lgth)
   ! format for printing string characters
   101   format(*(a1:))
   ! format for printing ADE values
   202   format(*(i1:))
   end subroutine ade
 end program demo_noesc

Expected output

The string is printed with the ADE value vertically beneath.
The original string has all the ADEs from 000 to 127. After
NOESC(3f) is called on the string all the "non-printable"
characters are replaced with a space (ADE of 032).

characters and their ADE (ASCII Decimal Equivalent)

>                                 !"#$%&'()*+,-./0123456789
:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
>0000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000001111111111111111111111111111
>00000000001111111111222222222233333333334444444444555555555566666666
667777777777888888888899999999990000000000111111111122222222
>012345678901234567890123456789012345678901234567890123456789012345678
90123456789012345678901234567890123456789012345678901234567

Cleared of non-printable characters

>                                 !"#$%&'()*+,-./0123456789
:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
>0000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000111111111111111111111111111
>3333333333333333333333333333333333333333444444444455555555
556666666666777777777788888888889999999999000000000011111111112222222
>2222222222222222222222222222222223456789012345678901234567
890123456789012345678901234567890123456789012345678901234567890123456

Cleared string:

>                                  !"#$%&'()*+,-./0123456789:;<=>?@
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

AUTHOR

John S. Urban

LICENSE

Public Domain

Arguments

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

Return Value character(len=len)


Contents

Source Code


Source Code

elemental function noesc(INSTR)

! ident_48="@(#) M_strings noesc(3f) convert non-printable characters to a space"

character(len=*),intent(in) :: INSTR      ! string that might contain nonprintable characters
character(len=len(instr))   :: noesc
integer                     :: ic,i10
!-----------------------------------------------------------------------------------------------------------------------------------
   noesc=''                               ! initialize output string
   do i10=1,len_trim(INSTR(1:len(INSTR)))
      ic=iachar(INSTR(i10:i10))
      if(ic <= 31.or.ic == 127)then       ! find characters with ADE of 0-31, 127
         noesc(I10:I10)=' '               ! replace non-printable characters with a space
      else
         noesc(I10:I10)=INSTR(i10:i10)    ! copy other characters as-is from input string to output string
      endif
   enddo
end function noesc