28
28
! > @{
29
29
module fms_string_utils_mod
30
30
use , intrinsic :: iso_c_binding
31
+ use platform_mod, only: r4 _kind, r8 _kind, i4_kind, i8_kind
31
32
use mpp_mod
32
33
33
34
implicit none
@@ -43,6 +44,7 @@ module fms_string_utils_mod
43
44
public :: fms_cstring2cpointer
44
45
public :: string
45
46
public :: string_copy
47
+ public :: stringify
46
48
! > @}
47
49
48
50
interface
@@ -112,11 +114,12 @@ subroutine c_free(ptr) bind(c,name="free")
112
114
module procedure cpointer_fortran_conversion
113
115
end interface
114
116
115
- ! > Converts a number to a string
117
+ ! > Converts an array of real numbers to a string
116
118
! > @ingroup fms_mod
117
- interface string
118
- module procedure string_from_integer
119
- module procedure string_from_real
119
+ interface stringify
120
+ module procedure stringify_1d_r4 , stringify_1d_r8
121
+ module procedure stringify_2d_r4 , stringify_2d_r8
122
+ module procedure stringify_3d_r4 , stringify_3d_r8
120
123
end interface
121
124
122
125
! > @addtogroup fms_string_utils_mod
@@ -237,31 +240,65 @@ subroutine fms_f2c_string (dest, str_in)
237
240
enddo
238
241
end subroutine fms_f2c_string
239
242
240
-
241
- ! > @brief Converts an integer to a string
242
- ! > @return The integer as a string
243
- function string_from_integer (i ) result (res)
244
- integer , intent (in ) :: i ! < Integer to be converted to a string
245
- character (:),allocatable :: res ! < String converted frominteger
246
- character (range (i)+ 2 ) :: tmp ! < Temp string that is set to correct size
247
- write (tmp,' (i0)' ) i
248
- res = trim (tmp)
249
- return
250
-
251
- end function string_from_integer
252
-
253
- ! #######################################################################
254
- ! > @brief Converts a real to a string
255
- ! > @return The real number as a string
256
- function string_from_real (r )
257
- real , intent (in ) :: r ! < Real number to be converted to a string
258
- character (len= 32 ) :: string_from_real
259
-
260
- write (string_from_real,* ) r
261
-
262
- return
263
-
264
- end function string_from_real
243
+ ! > @brief Converts a number or a Boolean value to a string
244
+ ! > @return The argument as a string
245
+ function string (v , fmt )
246
+ class(* ), intent (in ) :: v ! < Value to be converted to a string
247
+ character (* ), intent (in ), optional :: fmt ! < Optional format string for a real or integral argument
248
+ character (:), allocatable :: string
249
+
250
+ select type (v)
251
+ type is (logical )
252
+ if (present (fmt)) then
253
+ call mpp_error(WARNING, " string(): Ignoring `fmt` argument for type `logical`" )
254
+ endif
255
+ if (v) then
256
+ string = " True"
257
+ else
258
+ string = " False"
259
+ endif
260
+
261
+ type is (integer (i4_kind))
262
+ allocate (character (32 ) :: string)
263
+ if (present (fmt)) then
264
+ write (string, " (" // fmt // " )" ) v
265
+ else
266
+ write (string, ' (i0)' ) v
267
+ endif
268
+ string = trim (adjustl (string))
269
+
270
+ type is (integer (i8_kind))
271
+ allocate (character (32 ) :: string)
272
+ if (present (fmt)) then
273
+ write (string, " (" // fmt // " )" ) v
274
+ else
275
+ write (string, ' (i0)' ) v
276
+ endif
277
+ string = trim (adjustl (string))
278
+
279
+ type is (real (r4 _kind))
280
+ allocate (character (32 ) :: string)
281
+ if (present (fmt)) then
282
+ write (string, " (" // fmt // " )" ) v
283
+ else
284
+ write (string, * ) v
285
+ endif
286
+ string = trim (adjustl (string))
287
+
288
+ type is (real (r8 _kind))
289
+ allocate (character (32 ) :: string)
290
+ if (present (fmt)) then
291
+ write (string, " (" // fmt // " )" ) v
292
+ else
293
+ write (string, * ) v
294
+ endif
295
+ string = trim (adjustl (string))
296
+
297
+ class default
298
+ call mpp_error(FATAL, " string(): Called with incompatible argument type. Possible types &
299
+ &include integer(4), integer(8), real(4), real(8), or logical." )
300
+ end select
301
+ end function string
265
302
266
303
! > @brief Safely copy a string from one buffer to another.
267
304
subroutine string_copy (dest , source , check_for_null )
@@ -290,6 +327,9 @@ subroutine string_copy(dest, source, check_for_null)
290
327
dest = adjustl (trim (source(1 :i)))
291
328
end subroutine string_copy
292
329
330
+ #include " fms_string_utils_r4.fh"
331
+ #include " fms_string_utils_r8.fh"
332
+
293
333
end module fms_string_utils_mod
294
334
! > @}
295
335
! close documentation grouping
0 commit comments