/
string_module.f90
95 lines (62 loc) · 1.79 KB
/
string_module.f90
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
module string_module
use kind_module, only : i2b, i4b, i8b, sp, dp
implicit none
public :: string_tolower, string_toint, string_tolong, string_tofloat, string_todouble, &
string_digits
contains
function string_tolower(s) result(l)
implicit none
character(len=*), intent(in) :: s
character(len=len(s)) :: l
integer(kind=i4b), parameter :: cap_a = 65, cap_z = 90, off = 32
integer(kind=i4b) :: i, a
l = adjustl(s)
do i=1, len_trim(l)
a = iachar(l(i:i))
if ((a>=cap_a).and.(a<=cap_z)) then
a = a + off
l(i:i) = achar(a)
end if
end do
end function string_tolower
function string_toshort(s) result(i)
implicit none
character(len=*), intent(in) :: s
integer(kind=i2b) :: i
read(s,fmt=*) i
end function string_toshort
function string_toint(s) result(i)
implicit none
character(len=*), intent(in) :: s
integer(kind=i4b) :: i
read(s,fmt=*) i
end function string_toint
function string_tolong(s) result(i)
implicit none
character(len=*), intent(in) :: s
integer(kind=i8b) :: i
read(s,fmt=*) i
end function string_tolong
function string_tofloat(s) result(x)
implicit none
character(len=*), intent(in) :: s
real(kind=sp) :: x
read(s,fmt=*) x
end function string_tofloat
function string_todouble(s) result(x)
implicit none
character(len=*), intent(in) :: s
real(kind=dp) :: x
read(s,fmt=*) x
end function string_todouble
function string_digits(a, i, j) result(b)
implicit none
integer(kind=i4b), intent(in) :: a, i, j
integer(kind=i4b) :: b
character(len=16) astr, bstr
write(astr, *) a
astr = adjustl(astr)
bstr = astr(i:j)
read(bstr, *) b
end function string_digits
end module string_module