Skip to content

Commit 32f9837

Browse files
committed
Improve to_string: add support for format like 'f6.2'.
1 parent ce272d7 commit 32f9837

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

doc/specs/stdlib_strings.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,11 @@ Pure function.
563563

564564
- `value`: Shall be an `integer/real/complex/logical` scalar.
565565
This is an `intent(in)` argument.
566-
- `format`: Shall be a `character(len=*)` scalar like `'(F6.2)'`.
567-
This is an `intent(in)` and `optional` argument.
568-
566+
- `format`: Shall be a `character(len=*)` scalar like `'(F6.2)'` or just `'F6.2'`.
567+
This is an `intent(in)` and `optional` argument.
568+
Contains the edit descriptor to format `value` into a string, for example `'(F6.2)'` or `'(f6.2)'`.
569+
`to_string` will automatically enclose `format` in a set of parentheses, so passing `F6.2` or `f6.2` as `format` is possible as well.
570+
569571
#### Result value
570572

571573
The result is an `allocatable` length `character` scalar with up to `128` cached `character` length.
@@ -575,7 +577,6 @@ The result is an `allocatable` length `character` scalar with up to `128` cached
575577
```fortran
576578
program demo_to_string
577579
use stdlib_strings, only: to_string
578-
implicit none
579580
580581
!> Example for `complex` type
581582
print *, to_string((1, 1)) !! "(1.00000000,1.00000000)"
@@ -593,14 +594,16 @@ program demo_to_string
593594
!> Example for `real` type
594595
print *, to_string(1.) !! "1.00000000"
595596
print *, to_string(1., '(F6.2)') !! " 1.00"
597+
print *, to_string(1., 'F6.2') !! " 1.00"
596598
print *, to_string(1., '(SP,ES9.2)'), to_string(1, '(F7.3)') !! "+1.00E+00""[*]"
597599
!! 1 wrong demonstration (`[*]` from `to_string`)
598600
599601
!> Example for `logical` type
600602
print *, to_string(.true.) !! "T"
601603
print *, to_string(.true., '(L2)') !! " T"
602-
print *, to_string(.true., 'L2'), to_string(.false., '(I5)') !! "[*]""[*]"
603-
!! 2 wrong demonstrations(`[*]` from `to_string`)
604+
print *, to_string(.true., 'L2') !! " T"
605+
print *, to_string(.false., '(I5)') !! "[*]"
606+
!! 1 wrong demonstrations(`[*]` from `to_string`)
604607
605608
end program demo_to_string
606609
```

src/stdlib_strings_to_string.fypp

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ contains
1717
character(len=buffer_len) :: buffer
1818
integer :: stat
1919

20-
write(buffer, optval(format, "(g0)"), iostat=stat) value
20+
write(buffer, '(' // optval(format, "g0") // ')', iostat=stat) value
2121
if (stat == 0) then
2222
string = trim(buffer)
2323
else
@@ -82,7 +82,7 @@ contains
8282
character(len=buffer_len) :: buffer
8383
integer :: stat
8484

85-
write(buffer, format, iostat=stat) value
85+
write(buffer, "(" // format // ")", iostat=stat) value
8686
if (stat == 0) then
8787
string = trim(buffer)
8888
else
@@ -110,7 +110,7 @@ contains
110110
character(len=buffer_len) :: buffer
111111
integer :: stat
112112

113-
write(buffer, format, iostat=stat) value
113+
write(buffer, "(" // format // ")", iostat=stat) value
114114
if (stat == 0) then
115115
string = trim(buffer)
116116
else

src/tests/string/test_string_to_string.f90

+18-18
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ subroutine test_to_string_complex
3636
& "Default formatter for complex number", partial=.true.)
3737
call check_formatter(to_string((1, 1), '(F6.2)'), "( 1.00, 1.00)", &
3838
& "Formatter for complex number")
39-
call check_formatter(to_string((-1, -1), '(F6.2)'), "( -1.00, -1.00)", &
39+
call check_formatter(to_string((-1, -1), 'F6.2'), "( -1.00, -1.00)", &
4040
& "Formatter for negative complex number")
41-
call check_formatter(to_string((1, 1), '(SP,F6.2)'), "( +1.00, +1.00)", &
41+
call check_formatter(to_string((1, 1), 'SP,F6.2'), "( +1.00, +1.00)", &
4242
& "Formatter with sign control descriptor for complex number")
43-
call check_formatter(to_string((1, 1), '(F6.2)') // to_string((2, 2), '(F7.3)'), &
43+
call check_formatter(to_string((1, 1), 'F6.2') // to_string((2, 2), '(F7.3)'), &
4444
& "( 1.00, 1.00)( 2.000, 2.000)", &
4545
& "Multiple formatters for complex numbers")
4646

@@ -49,51 +49,51 @@ end subroutine test_to_string_complex
4949
subroutine test_to_string_integer
5050
call check_formatter(to_string(100), "100", &
5151
& "Default formatter for integer number")
52-
call check_formatter(to_string(100, '(I6)'), " 100", &
52+
call check_formatter(to_string(100, 'I6'), " 100", &
5353
& "Formatter for integer number")
54-
call check_formatter(to_string(100, '(I0.6)'), "000100", &
54+
call check_formatter(to_string(100, 'I0.6'), "000100", &
5555
& "Formatter with zero padding for integer number")
56-
call check_formatter(to_string(100, '(I6)') // to_string(1000, '(I7)'), &
56+
call check_formatter(to_string(100, 'I6') // to_string(1000, '(I7)'), &
5757
& " 100 1000", "Multiple formatters for integers")
58-
call check_formatter(to_string(34, '(B8)'), " 100010", &
58+
call check_formatter(to_string(34, 'B8'), " 100010", &
5959
& "Binary formatter for integer number")
60-
call check_formatter(to_string(34, '(O0.3)'), "042", &
60+
call check_formatter(to_string(34, 'O0.3'), "042", &
6161
& "Octal formatter with zero padding for integer number")
62-
call check_formatter(to_string(34, '(Z3)'), " 22", &
62+
call check_formatter(to_string(34, 'Z3'), " 22", &
6363
& "Hexadecimal formatter for integer number")
6464

6565
end subroutine test_to_string_integer
6666

6767
subroutine test_to_string_real
6868
call check_formatter(to_string(100.), "100.0", &
6969
& "Default formatter for real number", partial=.true.)
70-
call check_formatter(to_string(100., '(F6.2)'), "100.00", &
70+
call check_formatter(to_string(100., 'F6.2'), "100.00", &
7171
& "Formatter for real number")
72-
call check_formatter(to_string(289., '(E7.2)'), ".29E+03", &
72+
call check_formatter(to_string(289., 'E7.2'), ".29E+03", &
7373
& "Exponential formatter with rounding for real number")
74-
call check_formatter(to_string(128., '(ES8.2)'), "1.28E+02", &
74+
call check_formatter(to_string(128., 'ES8.2'), "1.28E+02", &
7575
& "Exponential formatter for real number")
7676

7777
! Wrong demonstration
78-
call check_formatter(to_string(-100., '(F6.2)'), "*", &
78+
call check_formatter(to_string(-100., 'F6.2'), "*", &
7979
& "Too narrow formatter for signed real number", partial=.true.)
80-
call check_formatter(to_string(1000., '(F6.3)'), "*", &
80+
call check_formatter(to_string(1000., 'F6.3'), "*", &
8181
& "Too narrow formatter for real number", partial=.true.)
82-
call check_formatter(to_string(1000., '(7.3)'), "[*]", &
82+
call check_formatter(to_string(1000., '7.3'), "[*]", &
8383
& "Invalid formatter for real number", partial=.true.)
8484

8585
end subroutine test_to_string_real
8686

8787
subroutine test_to_string_logical
8888
call check_formatter(to_string(.true.), "T", &
8989
& "Default formatter for logcal value")
90-
call check_formatter(to_string(.true., '(L2)'), " T", &
90+
call check_formatter(to_string(.true., 'L2'), " T", &
9191
& "Formatter for logical value")
92-
call check_formatter(to_string(.false., '(L2)') // to_string(.true., '(L5)'), &
92+
call check_formatter(to_string(.false., 'L2') // to_string(.true., '(L5)'), &
9393
& " F T", "Multiple formatters for logical values")
9494

9595
! Wrong demonstration
96-
call check_formatter(to_string(.false., '(1x)'), "[*]", &
96+
call check_formatter(to_string(.false., '1x'), "[*]", &
9797
& "Invalid formatter for logical value", partial=.true.)
9898

9999
end subroutine test_to_string_logical

0 commit comments

Comments
 (0)