forked from AMReX-Codes/amrex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbl_string.f90
175 lines (146 loc) · 4.37 KB
/
bl_string.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
!! Module for string/character manipulations
module bl_string_module
implicit none
integer, parameter, private :: EOS = -1
contains
!! Converts an integer encoding of a character string
!! to a Fortran string
subroutine int2str(str, iarr, n)
use bl_error_module
character(len=*), intent(out) :: str
integer, intent(in) :: n
integer, intent(in) :: iarr(n)
integer :: i
if ( len(str) < n ) then
call bl_error("INT2STR: iarr to large for str: len = ", len(str))
end if
do i = 1, n
if ( iarr(i) == EOS ) exit
str(i:i) = char(iarr(i))
end do
end subroutine int2str
!! Converts a Fortran string to an integer encoding.
subroutine str2int(iarr, n, str)
use bl_error_module
character(len=*), intent(in) :: str
integer, intent(in) :: n
integer :: i, j
integer, intent(out) :: iarr(n)
if ( n <= len_trim(str) ) then
call bl_error("STR2INT: str to large for iarr: size(iarr) = ", n)
end if
iarr = 0
j = 1
do i = 1, len_trim(str)
iarr(j) = ichar(str(i:i))
j = j + 1
end do
iarr(j) = EOS
end subroutine str2int
!! Converts character to lowercase
pure function to_lower (c) result(r)
character :: r
character, intent(in) :: c
r = c
if ( is_upper(c) ) then
r = char ( ichar(c) + 32 )
end if
end function to_lower
!! Converts character to uppercase
pure function to_upper (c) result(r)
character :: r
character, intent(in) :: c
r = c
if ( is_lower(c) ) then
r = char ( ichar(c) - 32 )
end if
end function to_upper
!! Checks for an uppercase letter.
pure function is_upper(c) result(r)
logical :: r
character, intent(in) :: c
r = (ichar(c) >= ichar('A') .and. ichar(c) <= ichar('Z'))
end function is_upper
!! Checks for a lowercase letter.
pure function is_lower(c) result(r)
logical :: r
character, intent(in) :: c
r = (ichar(c) >= ichar('a') .and. ichar(c) <= ichar('z'))
end function is_lower
!! Checks for a digit (0 through 9)
pure function is_digit(c) result(r)
logical :: r
character, intent(in) :: c
r = (ichar(c) >= ichar('0') .and. ichar(c) <= ichar('9'))
end function is_digit
!! Checks for an alphabetic character
pure function is_alpha(c) result(r)
logical :: r
character, intent(in) :: c
r = is_lower(c) .or. is_upper(c)
end function is_alpha
!! Checks for an alphanumceric character
pure function is_alnum(c) result(r)
logical :: r
character, intent(in) :: c
r = is_alpha(c) .or. is_digit(c)
end function is_alnum
!! Checks for a blank character
pure function is_blank(c) result(r)
logical :: r
character, intent(in) :: c
r = ( c == ' ' .or. ichar(c) == 9 )
end function is_blank
!! Checks for white-space characters.
pure function is_space(c) result(r)
logical :: r
character, intent(in) :: c
r = ( c == ' ' .or. (ichar(c) >= 9 .and. ichar(c) < 13) )
end function is_space
!! Checks for any printable character including space.
pure function is_print(c) result(r)
logical :: r
character, intent(in) :: c
r = (ichar(c) >= 32 .and. ichar(c) <= 126 )
end function is_print
!! Checks for any printable character except space.
pure function is_graph(c) result(r)
logical :: r
character, intent(in) :: c
r = (ichar(c) > 32 .and. ichar(c) <= 126 )
end function is_graph
!! Checks fora ny printable character that is not a space
!! or an alphanumeric character
pure function is_punct(c) result(r)
logical :: r
character, intent(in) :: c
r = is_print(c) .and. .not. is_space(c) .and. .not. is_alnum(c)
end function is_punct
!! Checks for a hexadecima digit.
pure function is_xdigit(c) result(r)
logical :: r
character, intent(in) :: c
r = is_digit(c) .or. &
( &
ichar('a') <= ichar(to_lower(c)) .and. &
ichar(to_lower(c)) <= ichar('f') &
)
end function is_xdigit
!! case insensitive string comparison
pure function eq_i ( s1, s2 ) result(r)
logical r
character(len=*), intent(in) :: s1, s2
integer :: i, l1, l2, lc
l1 = len_trim (s1)
l2 = len_trim (s2)
r = .false.
if ( l1 /= l2 ) return
lc = l1
do i = 1, lc
if ( to_upper(s1(i:i)) /= to_upper(s2(i:i)) ) then
return
end if
end do
r = .true.
end function eq_i
end module bl_string_module