-
Notifications
You must be signed in to change notification settings - Fork 191
Add format_string routine to format other types to strings #444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
e3829b4
da2881c
c622a5b
193f07f
7f6c3c6
41c5783
785902c
39d5d13
99954bb
f9f7755
e7ce1e7
65ab05d
34fa3cd
7ae89fb
1fe6a07
f155525
f1bc676
835de22
b05cbae
e646fc5
c717724
7fa847a
bdc33f5
f3c17a0
ce272d7
527daed
32f9837
3e31220
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#:include "common.fypp" | ||
#:set RIL_KINDS_TYPES = REAL_KINDS_TYPES + INT_KINDS_TYPES + LOG_KINDS_TYPES | ||
submodule (stdlib_strings) stdlib_strings_format_string | ||
|
||
implicit none | ||
integer, parameter :: buffer_len = 512 | ||
|
||
contains | ||
|
||
#:for kind, type in RIL_KINDS_TYPES | ||
module procedure format_string_${type[0]}$${kind}$ | ||
!! Format ${type}$ variable as character sequence | ||
character(len=buffer_len) :: buffer | ||
integer :: stat | ||
if(present(fmt)) then | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
write(buffer, fmt, iostat=stat) val | ||
if(stat == 0) then | ||
string = trim(adjustl(buffer)) | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else | ||
string = '*' | ||
!!\TODO: *? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a meaningful number of asterisks in the output we must be able to parse format strings and determine the width of the field we are writing into. Just writing a single asterisk until we are able to do so in stdlib sounds like a good compromise for now. What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is OK, just writing a single asterisk until we are able to do so in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking a failed formatter should be simple, currently I'm using How about returning an empty string on failure? This would be clearly distinct from a too narrow formatter? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is mainly to remind the users: an empty string may not be effectively fed back to the user, this is a
|
||
end if | ||
else | ||
write(buffer, *, iostat=stat) val | ||
if(stat == 0) then | ||
string = trim(adjustl(buffer)) | ||
else | ||
string = '*' | ||
!!\TODO: *? | ||
end if | ||
end if | ||
end procedure format_string_${type[0]}$${kind}$ | ||
#:endfor | ||
|
||
#:for kind, type in CMPLX_KINDS_TYPES | ||
module procedure format_string_${type[0]}$${kind}$ | ||
!! Format ${type}$ variable as character sequence | ||
character(len=buffer_len) :: buffer | ||
if(present(fmt)) then | ||
write(buffer, *) '('//& | ||
format_string_r${kind}$(real(val), fmt)//','// & | ||
format_string_r${kind}$(aimag(val), fmt)//')' | ||
else | ||
write(buffer, *) '('//& | ||
format_string_r${kind}$(real(val))//','// & | ||
format_string_r${kind}$(aimag(val))//')' | ||
end if | ||
string = trim(adjustl(buffer)) | ||
end procedure format_string_${type[0]}$${kind}$ | ||
#:endfor | ||
|
||
end submodule stdlib_strings_format_string |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
program test_strings_format_string | ||
use, non_intrinsic :: stdlib_strings, only: format_string | ||
implicit none | ||
print *, 'format_string(complex) : ' | ||
print *, format_string((1, 1)) | ||
print *, format_string((1, 1), '(F6.2)') | ||
print *, format_string((1, 1), '(F6.2)'), format_string((2, 2), '(F7.3)') | ||
print *, 'format_string(integer) : ' | ||
print *, format_string(100) | ||
print *, format_string(100, '(I6)') | ||
print *, format_string(100, '(I6)'), format_string(1000, '(I7)') | ||
print *, 'format_string(real) : ' | ||
print *, format_string(100.) | ||
print *, format_string(100., '(F6.2)') | ||
print *, format_string(100., '(F6.2)'), & | ||
format_string(1000., '(F7.3)'), format_string(1000, '(F7.3)') | ||
!! Wrong demonstration | ||
print *, 'format_string(logical) : ' | ||
print *, format_string(.true.) | ||
print *, format_string(.true., '(L2)') | ||
print *, format_string(.false., '(L2)'), format_string(.true., '(L5)'), & | ||
format_string(.false., '(I5)') | ||
!! Wrong demonstration | ||
end program test_strings_format_string |
Uh oh!
There was an error while loading. Please reload this page.