Created
April 26, 2021 22:13
-
-
Save ivan-pi/4f8b10c3c2ed58ebfdb8f830ddbbd469 to your computer and use it in GitHub Desktop.
Padding function for https://github.com/fortran-lang/stdlib/issues/394
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module pad_mod | |
implicit none | |
interface padl | |
module procedure padl_blank | |
module procedure padl_char | |
end interface | |
interface padr | |
module procedure padr_blank | |
module procedure padr_char | |
end interface | |
contains | |
function padl_blank(str,width) result(res) | |
character(len=*), intent(in) :: str | |
integer, intent(in) :: width | |
character(len=max(len_trim(str),width)) :: res | |
res = str | |
res = adjustr(res) | |
end function | |
function padr_blank(str,width) result(res) | |
character(len=*), intent(in) :: str | |
integer, intent(in) :: width | |
character(len=max(len_trim(str),width)) :: res | |
res = str | |
end function | |
function padl_char(str,width,char) result(res) | |
character(len=*), intent(in) :: str | |
integer, intent(in) :: width | |
character(len=1) :: char | |
character(len=max(len_trim(str),width)) :: res | |
integer :: nchar | |
nchar = width - len_trim(str) | |
if (nchar <= 0) then | |
res = str | |
else | |
res = repeat(char,nchar)//str | |
end if | |
end function | |
function padr_char(str,width,char) result(res) | |
character(len=*), intent(in) :: str | |
integer, intent(in) :: width | |
character(len=1) :: char | |
character(len=max(len_trim(str),width)) :: res | |
integer :: nchar | |
nchar = width - len_trim(str) | |
if (nchar <= 0) then | |
res = str | |
else | |
res = str//repeat(char,nchar) | |
end if | |
end function | |
end module | |
program test_pad | |
use pad_mod | |
implicit none | |
print *, "Hello" | |
print *, padl("Hello",10) | |
print *, padr("Hello",12), len(padr("Hello",12)) | |
print *, padl("Hello",10,'0') | |
print *, padr("Hello",10,'0') | |
print *, padl(padr("End",7,'_'),11,'_') | |
end program |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment