Skip to content

Commit bdc33f5

Browse files
committed
Some clean works for to_string func.
1 parent 7fa847a commit bdc33f5

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

doc/specs/stdlib_strings.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -563,43 +563,44 @@ 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` scalar like `'(F6.2)'`.
566+
- `format`: Shall be a `character(len=*)` scalar like `'(F6.2)'`.
567567
This is an `intent(in)` and `optional` argument.
568568

569569
#### Result value
570570

571-
The result is an allocatable length `character` scalar with up to 128 cached `character` length.
571+
The result is an `allocatable` length `character` scalar with up to `128` cached `character` length.
572572

573573
#### Example
574574

575575
```fortran
576576
program demo_to_string
577-
use :: stdlib_strings, only: to_string
577+
use stdlib_strings, only: to_string
578578
implicit none
579579
580-
print *, 'to_string(complex) : '
581-
print *, to_string((1, 1)) ! (1.00000000,1.00000000)
582-
print *, to_string((1, 1), '(F6.2)') ! ( 1.00, 1.00)
583-
print *, to_string((1000, 1), '(ES0.2)'), to_string((1000, 1), '(SP,F6.3)') ! (1.00E+3,1.00)(******,+1.000)
584-
!! Too narrow formatter for real number
585-
!! Normal demonstration(`******` from Fortran Standard)
586-
587-
print *, 'to_string(integer) : '
588-
print *, to_string(1) ! 1
589-
print *, to_string(1, '(I4)') ! 1
590-
print *, to_string(1, '(I0.4)'), to_string(2, '(B4)') ! 0001 10
591-
592-
print *, 'to_string(real) : '
593-
print *, to_string(1.) ! 1.00000000
594-
print *, to_string(1., '(F6.2)') ! 1.00
595-
print *, to_string(1., '(SP,ES9.2)'), to_string(1, '(F7.3)') ! +1.00E+00[*]
596-
!! 1 wrong demonstration(`*` from `to_string`)
597-
598-
print *, 'to_string(logical) : '
599-
print *, to_string(.true.) ! T
600-
print *, to_string(.true., '(L2)') ! T
601-
print *, to_string(.true., 'L2'), to_string(.false., '(I5)') ! [*][*]
602-
!! 2 wrong demonstrations(`*` from `to_string`)
580+
!> Example for `complex` type
581+
print *, to_string((1, 1)) !! "(1.00000000,1.00000000)"
582+
print *, to_string((1, 1), '(F6.2)') !! "( 1.00, 1.00)"
583+
print *, to_string((1000, 1), '(ES0.2)'), to_string((1000, 1), '(SP,F6.3)')
584+
!! "(1.00E+3,1.00)""(******,+1.000)"
585+
!! Too narrow formatter for real number
586+
!! Normal demonstration(`******` from Fortran Standard)
587+
588+
!> Example for `integer` type
589+
print *, to_string(-3) !! "-3"
590+
print *, to_string(42, '(I4)') !! " 42"
591+
print *, to_string(1, '(I0.4)'), to_string(2, '(B4)') !! "0001"" 10"
592+
593+
!> Example for `real` type
594+
print *, to_string(1.) !! "1.00000000"
595+
print *, to_string(1., '(F6.2)') !! " 1.00"
596+
print *, to_string(1., '(SP,ES9.2)'), to_string(1, '(F7.3)') !! "+1.00E+00""[*]"
597+
!! 1 wrong demonstration (`[*]` from `to_string`)
598+
599+
!> Example for `logical` type
600+
print *, to_string(.true.) !! "T"
601+
print *, to_string(.true., '(L2)') !! " T"
602+
print *, to_string(.true., 'L2'), to_string(.false., '(I5)') !! "[*]""[*]"
603+
!! 2 wrong demonstrations(`[*]` from `to_string`)
603604
604605
end program demo_to_string
605606
```

src/stdlib_string_to_string.fypp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
submodule(stdlib_strings) stdlib_strings_to_string
33

44
integer, parameter :: buffer_len = 128
5+
character(len=*), parameter :: err_sym = "[*]"
6+
!!TODO: [*]?
57

68
contains
79

@@ -19,8 +21,7 @@ contains
1921
if (stat == 0) then
2022
string = trim(buffer)
2123
else
22-
string = '[*]'
23-
!!\TODO: [*]?
24+
string = err_sym
2425
end if
2526

2627
end function to_string_${t1[0]}$_${k1}$
@@ -40,7 +41,7 @@ contains
4041
#:endfor
4142

4243
#:for k1, t1 in INT_KINDS_TYPES
43-
!> Represent an integer of kind ${k1}$ as character sequence
44+
!> Represent an integer of kind ${k1}$ as character sequence.
4445
pure module function to_string_1_${t1[0]}$_${k1}$(value) result(string)
4546
${t1}$, intent(in) :: value
4647
character(len=:), allocatable :: string
@@ -85,15 +86,14 @@ contains
8586
if (stat == 0) then
8687
string = trim(buffer)
8788
else
88-
string = '[*]'
89-
!!\TODO: [*]?
89+
string = err_sym
9090
end if
9191

9292
end function to_string_2_${t1[0]}$_${k1}$
9393
#:endfor
9494

9595
#:for k1, t1 in LOG_KINDS_TYPES
96-
!> Represent an logical of kind ${k1}$ as character sequence
96+
!> Represent an logical of kind ${k1}$ as character sequence.
9797
pure module function to_string_1_${t1[0]}$_${k1}$(value) result(string)
9898
${t1}$, intent(in) :: value
9999
character(len=1) :: string
@@ -114,8 +114,7 @@ contains
114114
if (stat == 0) then
115115
string = trim(buffer)
116116
else
117-
string = '[*]'
118-
!!\TODO: [*]?
117+
string = err_sym
119118
end if
120119

121120
end function to_string_2_${t1[0]}$_${k1}$

0 commit comments

Comments
 (0)