-
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
Merged
milancurcic
merged 28 commits into
fortran-lang:master
from
St-Maxwell:zoziha/feature/format_string
Aug 22, 2021
Merged
Changes from 5 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
e3829b4
add:
zoziha da2881c
remove a redundant comma in doc
St-Maxwell c622a5b
fix format_string routines update its doc.
zoziha 193f07f
Expand unit testing for more cases
awvwgk 7f6c3c6
Merge pull request #1 from awvwgk/format_string
St-Maxwell 41c5783
Try to make default formatter tests compiler independent
awvwgk 785902c
fix test_strings_format_string.f90 for [this problem](https://github.…
zoziha 39d5d13
Merge branch 'zoziha/feature/format_string' of https://github.com/St-…
zoziha 99954bb
update test_string_format_string.f90
zoziha f9f7755
Make invalid logical example more compiler agnostic
awvwgk e7ce1e7
update format_string example in stdlib_string.md(doc)
zoziha 65ab05d
improved aesthetics to make code consistent with stdlib's format
aman-godara 34fa3cd
improved aesthetics of test file and documentation
aman-godara 7ae89fb
Merge pull request #2 from Aman-Godara/format_string
zoziha 1fe6a07
renamed strings_format_string to string_format_string
aman-godara f155525
Merge pull request #3 from Aman-Godara/format_string
St-Maxwell f1bc676
Merge branch 'master' into zoziha/feature/format_string
zoziha 835de22
Fix manual Makefile build
awvwgk b05cbae
rename `format_string` to `format_to_string`;
zoziha e646fc5
Merge branch 'master' into zoziha/feature/format_string
zoziha c717724
Merge `stdlib_ascii(module):to_string(interface)` to `stdlib_strings(…
zoziha 7fa847a
Merge branch 'master' into merge_tostring
zoziha bdc33f5
Some clean works for `to_string` func.
zoziha f3c17a0
Fix `test_string_to_string.f90`: `merge` -> `optval`
zoziha ce272d7
Merge branch 'master' into zoziha/feature/format_string
zoziha 527daed
Consistent indentation in Makefile
milancurcic 32f9837
Improve `to_string`: add support for `format` like `'f6.2'`.
zoziha 3e31220
Merge branch 'zoziha/feature/format_string' of https://github.com/St-…
zoziha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#: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 | ||
|
||
write(buffer, optval(fmt, "(g0)"), iostat=stat) val | ||
if (stat == 0) then | ||
string = trim(buffer) | ||
else | ||
string = '*' | ||
!!\TODO: *? | ||
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 | ||
|
||
string = '('//format_string_r${kind}$(val%re, fmt)//','// & | ||
format_string_r${kind}$(val%im, fmt)//')' | ||
|
||
end procedure format_string_${type[0]}$${kind}$ | ||
#:endfor | ||
|
||
end submodule stdlib_strings_format_string |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
program test_strings_format_string | ||
use stdlib_strings, only: format_string, starts_with | ||
use stdlib_error, only: check | ||
use stdlib_optval, only: optval | ||
implicit none | ||
print *, 'format_string(complex) : ' | ||
call check_formatter(format_string((1, 1)), "(1.00000000,1.00000000)", & | ||
& "Default formatter for complex number") | ||
call check_formatter(format_string((1, 1), '(F6.2)'), "( 1.00, 1.00)", & | ||
& "Formatter for complex number") | ||
call check_formatter(format_string((-1, -1), '(F6.2)'), "( -1.00, -1.00)", & | ||
& "Formatter for negative complex number") | ||
call check_formatter(format_string((1, 1), '(SP,F6.2)'), "( +1.00, +1.00)", & | ||
& "Formatter with sign control descriptor for complex number") | ||
call check_formatter(format_string((1, 1), '(F6.2)')//format_string((2, 2), '(F7.3)'), & | ||
& "( 1.00, 1.00)( 2.000, 2.000)", & | ||
& "Multiple formatters for complex numbers") | ||
print *, 'format_string(integer) : ' | ||
call check_formatter(format_string(100), "100", & | ||
& "Default formatter for integer number") | ||
call check_formatter(format_string(100, '(I6)'), " 100", & | ||
& "Formatter for integer number") | ||
call check_formatter(format_string(100, '(I0.6)'), "000100", & | ||
& "Formatter with zero padding for integer number") | ||
call check_formatter(format_string(100, '(I6)')//format_string(1000, '(I7)'), & | ||
& " 100 1000", & | ||
& "Multiple formatters for integers") | ||
call check_formatter(format_string(34, '(B8)'), " 100010", & | ||
& "Binary formatter for integer number") | ||
call check_formatter(format_string(34, '(O0.3)'), "042", & | ||
& "Octal formatter with zero padding for integer number") | ||
call check_formatter(format_string(34, '(Z3)'), " 22", & | ||
& "Hexadecimal formatter for integer number") | ||
print *, 'format_string(real) : ' | ||
call check_formatter(format_string(100.), "100.000000", & | ||
& "Default formatter for real number") | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
call check_formatter(format_string(100., '(F6.2)'), "100.00", & | ||
& "Formatter for real number") | ||
call check_formatter(format_string(289., '(E7.2)'), ".29E+03", & | ||
& "Exponential formatter with rounding for real number") | ||
call check_formatter(format_string(128., '(ES8.2)'), "1.28E+02", & | ||
& "Exponential formatter for real number") | ||
! Wrong demonstration | ||
call check_formatter(format_string(-100., '(F6.2)'), "*", & | ||
& "Too narrow formatter for signed real number", partial=.true.) | ||
call check_formatter(format_string(1000., '(F6.3)'), "*", & | ||
& "Too narrow formatter for real number", partial=.true.) | ||
call check_formatter(format_string(1000, '(F7.3)'), "*", & | ||
& "Real formatter for integer number", partial=.true.) | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print *, 'format_string(logical) : ' | ||
call check_formatter(format_string(.true.), "T", & | ||
& "Default formatter for logcal value") | ||
call check_formatter(format_string(.true., '(L2)'), " T", & | ||
& "Formatter for logical value") | ||
call check_formatter(format_string(.false., '(L2)')//format_string(.true., '(L5)'), & | ||
& " F T", & | ||
& "Multiple formatters for logical values") | ||
! Wrong demonstration | ||
call check_formatter(format_string(.false., '(I5)'), "*", & | ||
& "Integer formatter for logical value", partial=.true.) | ||
awvwgk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
contains | ||
subroutine check_formatter(actual, expected, description, partial) | ||
character(len=*), intent(in) :: actual, expected, description | ||
logical, intent(in), optional :: partial | ||
logical :: stat | ||
character(len=:), allocatable :: msg | ||
|
||
if (optval(partial, .false.)) then | ||
stat = starts_with(actual, expected) | ||
else | ||
stat = actual == expected | ||
end if | ||
if (.not.stat) then | ||
msg = description // new_line("a") // & | ||
& "Expected: '"//expected//"' but got '"//actual//"'" | ||
else | ||
print '(" - ", a, /, " Result: ''", a, "''")', description, actual | ||
end if | ||
call check(stat, msg) | ||
end subroutine check_formatter | ||
end program test_strings_format_string |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.