forked from fortran-lang/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdlib_string_to_string.fypp
124 lines (98 loc) · 3.75 KB
/
stdlib_string_to_string.fypp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#:include "common.fypp"
submodule(stdlib_strings) stdlib_strings_to_string
integer, parameter :: buffer_len = 128
contains
#:for k1, t1 in REAL_KINDS_TYPES
!> Format or transfer a ${t1}$ scalar as a string.
pure module function to_string_${t1[0]}$_${k1}$(value, format) result(string)
${t1}$, intent(in) :: value
character(len=*), intent(in), optional :: format
character(len=:), allocatable :: string
character(len=buffer_len) :: buffer
integer :: stat
write(buffer, optval(format, "(g0)"), iostat=stat) value
if (stat == 0) then
string = trim(buffer)
else
string = '[*]'
!!\TODO: [*]?
end if
end function to_string_${t1[0]}$_${k1}$
#:endfor
#:for k1, t1 in CMPLX_KINDS_TYPES
!> Format or transfer a ${t1}$ scalar as a string.
pure module function to_string_${t1[0]}$_${k1}$(value, format) result(string)
${t1}$, intent(in) :: value
character(len=*), intent(in), optional :: format
character(len=:), allocatable :: string
string = '(' // to_string_r_${k1}$(value%re, format) // ',' // &
& to_string_r_${k1}$(value%im, format) // ')'
end function to_string_${t1[0]}$_${k1}$
#:endfor
#:for k1, t1 in INT_KINDS_TYPES
!> Represent an integer of kind ${k1}$ as character sequence
pure module function to_string_1_${t1[0]}$_${k1}$(value) result(string)
${t1}$, intent(in) :: value
character(len=:), allocatable :: string
integer, parameter :: buffer_len = range(value)+2
character(len=buffer_len) :: buffer
integer :: pos
${t1}$ :: n
character(len=1), parameter :: numbers(0:9) = &
["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
if (value == 0_${k1}$) then
string = numbers(0)
return
end if
n = abs(value)
buffer = ""
pos = buffer_len + 1
do while (n > 0_${k1}$)
pos = pos - 1
buffer(pos:pos) = numbers(mod(n, 10_${k1}$))
n = n/10_${k1}$
end do
if (value < 0_${k1}$) then
pos = pos - 1
buffer(pos:pos) = '-'
end if
string = buffer(pos:)
end function to_string_1_${t1[0]}$_${k1}$
pure module function to_string_2_${t1[0]}$_${k1}$(value, format) result(string)
${t1}$, intent(in) :: value
character(len=*), intent(in) :: format
character(len=:), allocatable :: string
character(len=buffer_len) :: buffer
integer :: stat
write(buffer, format, iostat=stat) value
if (stat == 0) then
string = trim(buffer)
else
string = '[*]'
!!\TODO: [*]?
end if
end function to_string_2_${t1[0]}$_${k1}$
#:endfor
#:for k1, t1 in LOG_KINDS_TYPES
!> Represent an logical of kind ${k1}$ as character sequence
pure module function to_string_1_${t1[0]}$_${k1}$(value) result(string)
${t1}$, intent(in) :: value
character(len=1) :: string
string = merge("T", "F", value)
end function to_string_1_${t1[0]}$_${k1}$
pure module function to_string_2_${t1[0]}$_${k1}$(value, format) result(string)
${t1}$, intent(in) :: value
character(len=*), intent(in) :: format
character(len=:), allocatable :: string
character(len=buffer_len) :: buffer
integer :: stat
write(buffer, format, iostat=stat) value
if (stat == 0) then
string = trim(buffer)
else
string = '[*]'
!!\TODO: [*]?
end if
end function to_string_2_${t1[0]}$_${k1}$
#:endfor
end submodule stdlib_strings_to_string