Skip to content

Commit 084f3c4

Browse files
authored
Merge pull request #617 from lewisfish/master
Made format constants public
2 parents 15f3270 + 809c85c commit 084f3c4

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Changes to existing modules
4747
[#562](https://github.com/fortran-lang/stdlib/pull/562)
4848
- support for quadruple precision made optional
4949
[#565](https://github.com/fortran-lang/stdlib/pull/565)
50+
- change in module `stdlib_io`
51+
- Modified format constants, and made public
52+
[#617](https://github.com/fortran-lang/stdlib/pull/617)
5053

5154

5255
# Version 0.1.0

doc/specs/stdlib_io.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,45 @@ program demo_getline
273273
end do
274274
end program demo_getline
275275
```
276+
277+
## Formatting constants
278+
279+
### Status
280+
281+
Experimental
282+
283+
### Description
284+
285+
Formatting constants for printing out integer, floating point, and complex numbers at their full precision.
286+
Provides formats for all kinds as defined in the `stdlib_kinds` module.
287+
288+
### Example
289+
290+
```fortran
291+
program demo_fmt_constants
292+
use, stdlib_kinds, only : int32, int64, sp, dp
293+
use stdlib_io, only : FMT_INT, FMT_REAL_SP, FMT_REAL_DP, FMT_COMPLEX_SP, FMT_COMPLEX_DP
294+
implicit none
295+
296+
integer(kind=int32) :: i32
297+
integer(kind=int64) :: i64
298+
real(kind=sp) :: r32
299+
real(kind=dp) :: r64
300+
complex(kind=sp) :: c32
301+
complex(kind=dp) :: c64
302+
303+
i32 = 100_int32
304+
i64 = 100_int64
305+
r32 = 100.0_sp
306+
r64 = 100.0_dp
307+
c32 = cmplx(100.0_sp, kind=sp)
308+
c64 = cmplx(100.0_dp, kind=dp)
309+
310+
print "(2("//FMT_INT//",1x))", i32, i64 ! outputs: 100 100
311+
print FMT_REAL_SP, r32 ! outputs: 1.00000000E+02
312+
print FMT_REAL_DP, r64 ! outputs: 1.0000000000000000E+002
313+
print FMT_COMPLEX_SP, c32 ! outputs: 1.00000000E+02 0.00000000E+00
314+
print FMT_COMPLEX_DP, c64 ! outputs: 1.0000000000000000E+002 0.0000000000000000E+000
315+
316+
end program demo_fmt_constants
317+
```

src/stdlib_io.fypp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,32 @@ module stdlib_io
2121
! Private API that is exposed so that we can test it in tests
2222
public :: parse_mode
2323

24-
! Format strings with edit descriptors for each type and kind
24+
!> Version: experimental
25+
!>
26+
!> Format strings with edit descriptors for each type and kind
27+
!> ([Specification](../page/specs/stdlib_io.html))
2528
character(*), parameter :: &
26-
FMT_INT = '(*(i0,1x))', &
27-
FMT_REAL_SP = '(*(es15.8e2,1x))', &
28-
FMT_REAL_DP = '(*(es24.16e3,1x))', &
29-
FMT_REAL_XDP = '(*(es26.18e3,1x))', &
30-
FMT_REAL_QP = '(*(es44.35e4,1x))', &
31-
FMT_COMPLEX_SP = '(*(es15.8e2,1x,es15.8e2))', &
32-
FMT_COMPLEX_DP = '(*(es24.16e3,1x,es24.16e3))', &
33-
FMT_COMPLEX_XDP = '(*(es26.18e3,1x,es26.18e3))', &
34-
FMT_COMPLEX_QP = '(*(es44.35e4,1x,es44.35e4))'
29+
!> Format string for integers
30+
FMT_INT = '(i0)', &
31+
!> Format string for single precision real numbers
32+
FMT_REAL_SP = '(es15.8e2)', &
33+
!> Format string for souble precision real numbers
34+
FMT_REAL_DP = '(es24.16e3)', &
35+
!> Format string for extended double precision real numbers
36+
FMT_REAL_XDP = '(es26.18e3)', &
37+
!> Format string for quadruple precision real numbers
38+
FMT_REAL_QP = '(es44.35e4)', &
39+
!> Format string for single precision complex numbers
40+
FMT_COMPLEX_SP = '(es15.8e2,1x,es15.8e2)', &
41+
!> Format string for double precision complex numbers
42+
FMT_COMPLEX_DP = '(es24.16e3,1x,es24.16e3)', &
43+
!> Format string for extended double precision complex numbers
44+
FMT_COMPLEX_XDP = '(es26.18e3,1x,es26.18e3)', &
45+
!> Format string for quadruple precision complex numbers
46+
FMT_COMPLEX_QP = '(es44.35e4,1x,es44.35e4)'
47+
48+
public :: FMT_INT, FMT_REAL_SP, FMT_REAL_DP, FMT_REAL_XDP, FMT_REAL_QP
49+
public :: FMT_COMPLEX_SP, FMT_COMPLEX_DP, FMT_COMPLEX_XDP, FMT_COMPLEX_QP
3550

3651
!> Version: experimental
3752
!>
@@ -112,9 +127,9 @@ contains
112127
allocate(d(nrow, ncol))
113128
do i = 1, nrow
114129
#:if 'real' in t1
115-
read(s, FMT_REAL_${k1}$) d(i, :)
130+
read(s, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",1x))") d(i, :)
116131
#:elif 'complex' in t1
117-
read(s, FMT_COMPLEX_${k1}$) d(i, :)
132+
read(s, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",1x))") d(i, :)
118133
#:else
119134
read(s, *) d(i, :)
120135
#:endif
@@ -150,11 +165,11 @@ contains
150165
s = open(filename, "w")
151166
do i = 1, size(d, 1)
152167
#:if 'real' in t1
153-
write(s, FMT_REAL_${k1}$) d(i, :)
168+
write(s, "(*"//FMT_REAL_${k1}$(1:len(FMT_REAL_${k1}$)-1)//",1x))") d(i, :)
154169
#:elif 'complex' in t1
155-
write(s, FMT_COMPLEX_${k1}$) d(i, :)
170+
write(s, "(*"//FMT_COMPLEX_${k1}$(1:len(FMT_COMPLEX_${k1}$)-1)//",1x))") d(i, :)
156171
#:elif 'integer' in t1
157-
write(s, FMT_INT) d(i, :)
172+
write(s, "(*"//FMT_INT(1:len(FMT_INT)-1)//",1x))") d(i, :)
158173
#:else
159174
write(s, *) d(i, :)
160175
#:endif

0 commit comments

Comments
 (0)