PB3D [2.47]
Ideal linear high-n MHD stability in 3-D
Loading...
Searching...
No Matches
str_utilities.f90
Go to the documentation of this file.
1!------------------------------------------------------------------------------!
2!> Operations on strings.
3!------------------------------------------------------------------------------!
5#include <wrappers.h>
6 use num_vars, only: dp, max_str_ln
7 implicit none
8 private
11
12contains
13 !> Convert an integer to string.
14 !!
15 !! \see from
16 !! <http://stackoverflow.com/questions/1262695/converting-integers-to-strings-in-fortran>
17 elemental character(len=max_str_ln) function i2str(k)
18 ! input / output
19 integer, intent(in) :: k !< integer to convert
20
21 write (i2str, *) k
22 i2str = adjustl(i2str)
23 end function i2str
24 !> Convert an integer to string.
25 !!
26 !! Version with kind 8 integers.
27 !!
28 !! \see See i2str().
29 elemental character(len=max_str_ln) function ii2str(k)
30 ! input / output
31 integer(kind=8), intent(in) :: k !< integer to convert
32
33 write (ii2str, *) k
34 ii2str = adjustl(ii2str)
35 end function ii2str
36
37 !> Convert a real (double) to string
38 !!
39 !! \note See <http://www.fortran90.org/src/best-practices.html> how to not
40 !! lose precision.
41 elemental character(len=max_str_ln) function r2str(k)
42 ! input / output
43 real(dp), intent(in) :: k !< real to convert
44
45 write (r2str, '(ES23.16)') k
46 r2str = adjustl(r2str)
47 end function r2str
48 !> Convert a real (double) to string
49 !!
50 !! Version with less precise output.
51 !!
52 !! \see See r2str().
53 elemental character(len=max_str_ln) function r2strt(k)
54 ! input / output
55 real(dp), intent(in) :: k !< real to convert
56
57 write (r2strt, '(ES9.2)') k
58 r2strt = adjustl(r2strt)
59 end function r2strt
60
61 !> Convert a complex (double) to string
62 !!
63 !! \note See <http://www.fortran90.org/src/best-practices.html> how to not
64 !! lose precision.
65 elemental character(len=max_str_ln) function c2str(k)
66 ! input / output
67 complex(dp), intent(in) :: k !< complex to convert
68
69 ! local variables
70 character(len=max_str_ln) :: dum_str ! dummy string
71
72 write (c2str, '(ES23.16)') rp(k)
73 write (dum_str, '(ES23.16)') abs(imag(k))
74 if (imag(k).lt.0) then
75 c2str = trim(c2str)//' -'
76 else
77 c2str = trim(c2str)//' +'
78 end if
79 c2str = trim(c2str)//' '//dum_str
80 c2str = adjustl(c2str)
81 end function c2str
82 !> Convert a complex (double) to string
83 !!
84 !! Version with less precise output.
85 !!
86 !! \see See c2str().
87 elemental character(len=max_str_ln) function c2strt(k)
88 ! input / output
89 complex(dp), intent(in) :: k !< complex to convert
90
91 ! local variables
92 character(len=max_str_ln) :: dum_str ! dummy string
93
94 write (c2strt, '(ES9.2)') rp(k)
95 write (dum_str, '(ES9.2)') abs(imag(k))
96 if (imag(k).lt.0) then
97 c2strt = trim(c2strt)//' -'
98 else
99 c2strt = trim(c2strt)//' +'
100 end if
101 c2strt = trim(c2strt)//' '//trim(dum_str)//' i'
102 c2strt = adjustl(c2strt)
103 end function c2strt
104
105 !> Convert a string to lowercase.
106 !!
107 !! \see from \cite RedwineF90, figure 3.5B, pg 80.
108 function strh2l(input_string) result(output_string)
109 ! input / output
110 character(*), intent(in) :: input_string !< input string
111 character(len(input_string)) :: output_string !< lowercase version
112
113 ! local variables
114 integer :: i, n
115 character(*), parameter :: lower_case = 'abcdefghijklmnopqrstuvwxyz'
116 character(*), parameter :: upper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
117
118 ! copy input string
119 output_string = input_string
120
121 ! convert case character by character
122 do i = 1, len(output_string)
123 n = index(upper_case, output_string(i:i))
124 if ( n /= 0 ) output_string(i:i) = lower_case(n:n)
125 end do
126 end function strh2l
127 !> convert a string to uppercase.
128 !!
129 !! \see See strh2l()
130 function strl2h(input_string) result(output_string)
131 ! input / output
132 character(*), intent(in) :: input_string !< input string
133 character(len(input_string)) :: output_string !< uppercase version
134
135 ! local variables
136 integer :: i, n
137 character(*), parameter :: lower_case = 'abcdefghijklmnopqrstuvwxyz'
138 character(*), parameter :: upper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
139
140 ! copy input string
141 output_string = input_string
142
143 ! convert case character by character
144 do i = 1, len(output_string)
145 n = index(lower_case, output_string(i:i))
146 if ( n /= 0 ) output_string(i:i) = upper_case(n:n)
147 end do
148 end function strl2h
149
150 !> Merge array of strings.
151 function merge_strings(input_strings)
152 ! input / output
153 character(*), intent(in) :: input_strings(:) !< array of strings
154 character((len(input_strings)+2)*size(input_strings)) :: merge_strings !< merged string
155
156 ! local variables
157 integer :: id ! counter
158
159 ! start with first string
160 if (size(input_strings).gt.0) then
161 merge_strings = trim(input_strings(1))
162
163 ! loop over next strings
164 do id = 2,size(input_strings)
165 merge_strings = trim(merge_strings)//', '//&
166 &trim(input_strings(id))
167 end do
168 else
169 merge_strings = ''
170 end if
171 end function merge_strings
172end module str_utilities
Numerical variables used by most other modules.
Definition num_vars.f90:4
integer, parameter, public dp
double precision
Definition num_vars.f90:46
integer, parameter, public max_str_ln
maximum length of strings
Definition num_vars.f90:50
Operations on strings.
elemental character(len=max_str_ln) function, public i2str(k)
Convert an integer to string.
character(len(input_string)) function, public strh2l(input_string)
Convert a string to lowercase.
elemental character(len=max_str_ln) function, public c2str(k)
Convert a complex (double) to string.
character((len(input_strings)+2) *size(input_strings)) function, public merge_strings(input_strings)
Merge array of strings.
character(len(input_string)) function, public strl2h(input_string)
convert a string to uppercase.
elemental character(len=max_str_ln) function, public r2str(k)
Convert a real (double) to string.
elemental character(len=max_str_ln) function, public c2strt(k)
Convert a complex (double) to string.
elemental character(len=max_str_ln) function, public r2strt(k)
Convert a real (double) to string.
elemental character(len=max_str_ln) function, public ii2str(k)
Convert an integer to string.