Manual Reference Pages  - pad (3m_unicode)

NAME

PAD(3f) - [M_unicode:PAD] return string padded to at least specified length (LICENSE:MIT)

CONTENTS

Synopsis
Description
Options
Returns
Examples
See Also
Author
License

SYNOPSIS

function pad(str,length,pattern,right,clip) result(out)

    type(unicode_type)                         :: str
    integer,intent(in)                         :: length
    type(unicode_type)                         :: out
    type(unicode_type),intent(in),optional     :: pattern
    logical,intent(in),optional                :: right
    logical,intent(in),optional                :: clip

DESCRIPTION

pad(3f) pads a string with a pattern to at least the specified length. If the trimmed input string is longer than the requested length the trimmed string is returned.

OPTIONS

str the input string to return trimmed, but then padded to the specified length if shorter than length
length The minimum string length to return
pattern
  optional string to use as padding. Defaults to a space.
right if true pads string on the right, else on the left. Defaults to true.
clip trim spaces from input string ends. Defaults to .true.

RETURNS

out The input string padded to the requested length or the trimmed input string if the input string is longer than the requested length.

EXAMPLES

Sample Program:

  program demo_pad
  use M_unicode, only  : pad, assignment(=)
  !use M_unicode, only : write(formatted)
  use M_unicode, only  : len
  use M_unicode, only  : ch=> character
  use M_unicode, only  : ut=> unicode_type
  implicit none
  type(ut)                   :: string
  type(ut)                   :: answer
  integer                    :: i
  !character(len=*),parameter :: u=’(*(DT))’
  character(len=*),parameter :: u=’(*(g0))’
    !
    string=’abcdefghij’
    !
    write(*,*)’pad on right till 20 characters long’
    answer=pad(string,20)
    write(*,’("[",g0,"]",/)’) answer%character()
    !
    write(*,*)’original is not trimmed for short length requests’
    answer=pad(string,5)
    write(*,’("[",g0,"]",/)’) answer%character()
    !
    i=30
    write(*,*)’pad with specified string and left-justified integers’
    write(*,’(1x,g0,1x,i0)’) &
     & ch(pad(ut(’CHAPTER 1 : The beginning ’),i,ut(’.’) )), 1   , &
     & ch(pad(ut(’CHAPTER 2 : The end ’),i,ut(’.’) )),       1234, &
     & ch(pad(ut(’APPENDIX ’),i,ut(’.’) )),                  1235
    !
    write(*,*)’pad with specified string and right-justified integers’
    write(*,’(1x,g0,i7)’) &
     & ch(pad(ut(’CHAPTER 1 : The beginning ’),i,ut(’.’) )), 1   , &
     & ch(pad(ut(’CHAPTER 2 : The end ’),i,ut(’.’) )),       1234, &
     & ch(pad(ut(’APPENDIX ’),i,ut(’.’) )),                  1235
    !
    write(*,*)’pad on left with zeros’
    write(*,u)ch(pad(ut(’12’),5,ut(’0’),right=.false.))
    !
    write(*,*)’various lengths with clip .true. and .false.’
    write(*,u)ch(pad(ut(’12345 ’),30,ut(’_’),right=.false.))
    write(*,u)ch(pad(ut(’12345 ’),30,ut(’_’),right=.false.,clip=.true.))
    write(*,u)ch(pad(ut(’12345 ’), 7,ut(’_’),right=.false.))
    write(*,u)ch(pad(ut(’12345 ’), 7,ut(’_’),right=.false.,clip=.true.))
    write(*,u)ch(pad(ut(’12345 ’), 6,ut(’_’),right=.false.))
    write(*,u)ch(pad(ut(’12345 ’), 6,ut(’_’),right=.false.,clip=.true.))
    write(*,u)ch(pad(ut(’12345 ’), 5,ut(’_’),right=.false.))
    write(*,u)ch(pad(ut(’12345 ’), 5,ut(’_’),right=.false.,clip=.true.))
    write(*,u)ch(pad(ut(’12345 ’), 4,ut(’_’),right=.false.))
    write(*,u)ch(pad(ut(’12345 ’), 4,ut(’_’),right=.false.,clip=.true.))
end program demo_pad

  Results:

> pad on right till 20 characters long > [abcdefghij ] > > original is not trimmed for short length requests > [abcdefghij] > > pad with specified string and left-justified integers > CHAPTER 1 : The beginning .... 1 > CHAPTER 2 : The end .......... 1234 > APPENDIX ..................... 1235 > pad with specified string and right-justified integers > CHAPTER 1 : The beginning .... 1 > CHAPTER 2 : The end .......... 1234 > APPENDIX ..................... 1235 > pad on left with zeros > 00012 > various lengths with clip .true. and .false. > ________________________12345 > _________________________12345 > _12345 > __12345 > 12345 > _12345 > 12345 > 12345 > 12345 > 2345

SEE ALSO

adjustl(3f), adjustr(3f), repeat(3f), trim(3f), len_trim(3f), len(3f)

AUTHOR

John S. Urban

LICENSE

    MIT