Skip to content

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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e3829b4
add:
zoziha Jun 25, 2021
da2881c
remove a redundant comma in doc
St-Maxwell Jun 26, 2021
c622a5b
fix format_string routines update its doc.
zoziha Jun 26, 2021
193f07f
Expand unit testing for more cases
awvwgk Jun 26, 2021
7f6c3c6
Merge pull request #1 from awvwgk/format_string
St-Maxwell Jun 26, 2021
41c5783
Try to make default formatter tests compiler independent
awvwgk Jun 26, 2021
785902c
fix test_strings_format_string.f90 for [this problem](https://github.…
zoziha Jun 26, 2021
39d5d13
Merge branch 'zoziha/feature/format_string' of https://github.com/St-…
zoziha Jun 26, 2021
99954bb
update test_string_format_string.f90
zoziha Jun 26, 2021
f9f7755
Make invalid logical example more compiler agnostic
awvwgk Jun 26, 2021
e7ce1e7
update format_string example in stdlib_string.md(doc)
zoziha Jun 27, 2021
65ab05d
improved aesthetics to make code consistent with stdlib's format
aman-godara Jun 27, 2021
34fa3cd
improved aesthetics of test file and documentation
aman-godara Jun 28, 2021
7ae89fb
Merge pull request #2 from Aman-Godara/format_string
zoziha Jun 28, 2021
1fe6a07
renamed strings_format_string to string_format_string
aman-godara Jul 1, 2021
f155525
Merge pull request #3 from Aman-Godara/format_string
St-Maxwell Jul 1, 2021
f1bc676
Merge branch 'master' into zoziha/feature/format_string
zoziha Jul 2, 2021
835de22
Fix manual Makefile build
awvwgk Jul 3, 2021
b05cbae
rename `format_string` to `format_to_string`;
zoziha Jul 5, 2021
e646fc5
Merge branch 'master' into zoziha/feature/format_string
zoziha Jul 5, 2021
c717724
Merge `stdlib_ascii(module):to_string(interface)` to `stdlib_strings(…
zoziha Aug 3, 2021
7fa847a
Merge branch 'master' into merge_tostring
zoziha Aug 3, 2021
bdc33f5
Some clean works for `to_string` func.
zoziha Aug 4, 2021
f3c17a0
Fix `test_string_to_string.f90`: `merge` -> `optval`
zoziha Aug 11, 2021
ce272d7
Merge branch 'master' into zoziha/feature/format_string
zoziha Aug 11, 2021
527daed
Consistent indentation in Makefile
milancurcic Aug 19, 2021
32f9837
Improve `to_string`: add support for `format` like `'f6.2'`.
zoziha Aug 22, 2021
3e31220
Merge branch 'zoziha/feature/format_string' of https://github.com/St-…
zoziha Aug 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix format_string routines update its doc.
fix:
1. src/stdlib_strings.fypp%format_string interface comments
2. src/stdlib_strings_format_string.fypp
update:
1. src/tests/string/test_strings_format_string.f90
2. doc/specs/stdlib_strings.md
note:
make passed
cmake passed
  • Loading branch information
zoziha committed Jun 26, 2021
commit c622a5bbd1fe8f4a54a992b7fbd10fca5c510b0c
45 changes: 32 additions & 13 deletions doc/specs/stdlib_strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ Format or transfer a integer/real/complex/logical variable as a character sequen

#### Syntax

`format_string = [[stdlib_strings(module):format_string(interface)]] (value[, format])`
`format_string = [[stdlib_strings(module):format_string(interface)]] (value [, format])`

#### Status

Expand Down Expand Up @@ -367,24 +367,43 @@ program demo_strings_format_string
use, non_intrinsic :: stdlib_strings, only: format_string
implicit none
print *, 'format_string(complex) : '
print *, format_string((1, 1)) ! (1.00000000,1.00000000)
print *, format_string((1, 1), '(F6.2)') ! (1.00,1.00)
print *, format_string((1, 1), '(F6.2)'), format_string((2, 2), '(F7.3)') ! (1.00,1.00)(2.000,2.000)
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) ! 100
print *, format_string(100, '(I6)') ! 100
print *, format_string(100, '(I6)'), format_string(1000, '(I7)') ! 1001000
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.) ! 100.000000
print *, format_string(100., '(F6.2)') ! 100.00
print *, format_string(100.)
print *, format_string(100., '(F12.2)')
print *, format_string(100., '(F6.2)'), &
format_string(1000., '(F7.3)'), format_string(1000, '(F7.3)') ! 100.00********
format_string(1000., '(F7.3)'), format_string(1000, '(F7.3)')
!! Wrong demonstration
print *, 'format_string(logical) : '
print *, format_string(.true.) ! T
print *, format_string(.true., '(L2)') ! T
print *, format_string(.true.)
print *, format_string(.true., '(L2)')
print *, format_string(.false., '(L2)'), format_string(.true., '(L5)'), &
format_string(.false., '(I5)') ! FT*
format_string(.false., '(I5)')
!! Wrong demonstration
end program demo_strings_format_string
```
**Results**
```fortran
format_string(complex) :
(1.00000000,1.00000000)
( 1.00, 1.00)
( 1.00, 1.00) ( 2.000, 2.000)
format_string(integer) :
100
100
100 1000
format_string(real) :
100.000000
100.00
100.00********
format_string(logical) :
T
T
F T*
```
4 changes: 2 additions & 2 deletions src/stdlib_strings.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ module stdlib_strings
interface format_string
!! version: experimental
!!
!! Format ${type}$ variable as character sequence
!! ([Specification](../page/specs/stdlib_string.html#description))
!! Format other types as character sequence.
!! ([Specification](../page/specs/stdlib_strings.html#description))
#:for kind, type in KINDS_TYPES
pure module function format_string_${type[0]}$${kind}$(val, fmt) result(string)
character(len=:), allocatable :: string
Expand Down
3 changes: 1 addition & 2 deletions src/stdlib_strings_format_string.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#:set RIL_KINDS_TYPES = REAL_KINDS_TYPES + INT_KINDS_TYPES + LOG_KINDS_TYPES
submodule (stdlib_strings) stdlib_strings_format_string

use stdlib_optval, only: optval
implicit none
integer, parameter :: buffer_len = 512

Expand All @@ -14,7 +13,7 @@ contains
character(len=buffer_len) :: buffer
integer :: stat

write(buffer, optval(fmt, "g0"), iostat=stat) val
write(buffer, optval(fmt, "(g0)"), iostat=stat) val
if (stat == 0) then
string = trim(buffer)
else
Expand Down
2 changes: 1 addition & 1 deletion src/tests/string/test_strings_format_string.f90
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ program test_strings_format_string
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., '(F12.2)')
print *, format_string(100., '(F6.2)'), &
format_string(1000., '(F7.3)'), format_string(1000, '(F7.3)')
!! Wrong demonstration
Expand Down