Skip to content

Commit 7bc299e

Browse files
committed
toString impls do not need to do allocations
[BFB]
1 parent 0f29546 commit 7bc299e

File tree

1 file changed

+25
-43
lines changed

1 file changed

+25
-43
lines changed

share/util/shr_strconvert_mod.F90

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -59,106 +59,88 @@ module shr_strconvert_mod
5959

6060
contains
6161

62-
pure function i4ToString(input, format_string) result(string)
62+
pure character(len=cs) function i4ToString(input, format_string)
6363
integer(i4), intent(in) :: input
6464
character(len=*), intent(in), optional :: format_string
65-
character(len=:), allocatable :: string
66-
67-
character(len=cs) :: buffer
6865

6966
if (present(format_string)) then
70-
write(buffer, format_string) input
67+
write(i4ToString, format_string) input
7168
else
7269
! For most compilers, these two statements are equivalent to a format of
7370
! '(I0)', but that's not technically in the standard.
74-
write(buffer, '(I11)') input
75-
buffer = adjustl(buffer)
71+
write(i4ToString, '(I11)') input
7672
end if
7773

78-
allocate(string, source=trim(buffer))
74+
i4ToString = trim(adjustl(i4ToString))
7975

8076
end function i4ToString
8177

82-
pure function i8ToString(input, format_string) result(string)
78+
pure character(len=cs) function i8ToString(input, format_string)
8379
integer(i8), intent(in) :: input
8480
character(len=*), intent(in), optional :: format_string
85-
character(len=:), allocatable :: string
86-
87-
character(len=cs) :: buffer
8881

8982
if (present(format_string)) then
90-
write(buffer, format_string) input
83+
write(i8ToString, format_string) input
9184
else
9285
! For most compilers, these two statements are equivalent to a format of
9386
! '(I0)', but that's not technically in the standard.
94-
write(buffer, '(I20)') input
95-
buffer = adjustl(buffer)
87+
write(i8ToString, '(I20)') input
9688
end if
9789

98-
allocate(string, source=trim(buffer))
90+
i8ToString = trim(adjustl(i8ToString))
9991

10092
end function i8ToString
10193

102-
pure function r4ToString(input, format_string) result(string)
94+
pure character(len=cs) function r4ToString(input, format_string)
10395
real(r4), intent(in) :: input
10496
character(len=*), intent(in), optional :: format_string
105-
character(len=:), allocatable :: string
106-
107-
character(len=cs) :: buffer
10897

10998
if (present(format_string)) then
110-
write(buffer, format_string) input
99+
write(r4ToString, format_string) input
111100
else
112-
write(buffer, '(ES15.8 E2)') input
113-
buffer = adjustl(buffer)
101+
write(r4ToString, '(ES15.8 E2)') input
102+
r4ToString = adjustl(r4ToString)
114103
! Deal with the fact that the "+" sign is optional by simply adding it if
115104
! it is not present, so that the default format is standardized across
116105
! compilers.
117106
! Assumes that compilers do not treat the sign bit on NaN values specially.
118-
if (.not. isnan(input) .and. all(buffer(1:1) /= ["-", "+"])) then
119-
buffer = "+" // trim(buffer)
107+
if (.not. isnan(input) .and. all(r4ToString(1:1) /= ["-", "+"])) then
108+
r4ToString = "+" // trim(r4ToString)
120109
end if
121110
end if
122111

123-
allocate(string, source=trim(buffer))
112+
r4ToString = trim(adjustl(r4ToString))
124113

125114
end function r4ToString
126115

127-
pure function r8ToString(input, format_string) result(string)
116+
pure character(len=cs) function r8ToString(input, format_string)
128117
real(r8), intent(in) :: input
129118
character(len=*), intent(in), optional :: format_string
130-
character(len=:), allocatable :: string
131-
132-
character(len=cs) :: buffer
133119

134120
if (present(format_string)) then
135-
write(buffer, format_string) input
121+
write(r8ToString, format_string) input
136122
else
137-
write(buffer, '(ES24.16 E3)') input
138-
buffer = adjustl(buffer)
123+
write(r8ToString, '(ES24.16 E3)') input
124+
r8ToString = adjustl(r8ToString)
139125
! Deal with the fact that the "+" sign is optional by simply adding it if
140126
! it is not present, so that the default format is standardized across
141127
! compilers.
142128
! Assumes that compilers do not treat the sign bit on NaN values specially.
143-
if (.not. isnan(input) .and. all(buffer(1:1) /= ["-", "+"])) then
144-
buffer = "+" // trim(buffer)
129+
if (.not. isnan(input) .and. all(r8ToString(1:1) /= ["-", "+"])) then
130+
r8ToString = "+" // trim(r8ToString)
145131
end if
146132
end if
147133

148-
allocate(string, source=trim(buffer))
134+
r8ToString = trim(adjustl(r8ToString))
149135

150136
end function r8ToString
151137

152-
pure function logicalToString(input) result(string)
138+
pure character(len=cs) function logicalToString(input)
153139
logical, intent(in) :: input
154-
character(len=:), allocatable :: string
155-
156-
! We could use a write statement, but this is easier.
157-
allocate(character(len=1) :: string)
158140
if (input) then
159-
string = "T"
141+
logicalToString = "T"
160142
else
161-
string = "F"
143+
logicalToString = "F"
162144
end if
163145

164146
end function logicalToString

0 commit comments

Comments
 (0)