FUTILS
Some basic Fortran Utilities
Loading...
Searching...
No Matches
strings.f90
Go to the documentation of this file.
2
3 !> @page strings_page Strings
4 !> @ref index "< Back to main page"
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 type :: string_t
23 integer :: len=0
24 character(len=:), allocatable :: s
25 contains
26 procedure :: write => write_string
27 procedure :: write_unformatted => write_string_unformatted
28 procedure :: read => read_string
29 generic :: WRITE(formatted) => WRITE
30 generic :: WRITE(unformatted) => write_unformatted
31 generic :: READ(formatted) => READ
32
33 procedure, pass(self), public :: make_upper
34 procedure, pass(self), public :: make_lower
35 procedure, pass(self), public :: remove
36
37 end type string_t
38
39 character*26, private, parameter :: &
40 upper_case="ABCDEFGHIJKLMNOPQRSTUVWXYZ", &
41 lower_case="abcdefghijklmnopqrstuvwxyz"
42
43 interface string_t
44 module procedure create_string
45 end interface string_t
46
47 interface assignment (=)
48 module procedure assign_character_to_string
49 module procedure assign_int4_to_string
50 module procedure assign_string_to_string
51 end interface
52
53 interface operator (//)
54 module procedure concat_character_and_string
55 module procedure concat_string_and_character
56 module procedure concat_string_and_string
57 end interface
58
59 interface operator (==)
60 module procedure equiv_character_string
61 module procedure equiv_string_character
62 module procedure equiv_string_string
63 end interface
64
65 interface operator (/=)
66 module procedure nequiv_character_string
67 module procedure nequiv_string_character
68 module procedure nequiv_string_string
69 end interface
70
71 interface index
72 module procedure string_index
73 end interface
74
75 type, private :: string_tools_t
76 contains
77 procedure, nopass :: sentence => make_sentence_from_array_of_strings
78 end type string_tools_t
79
80 type(string_tools_t), public, parameter :: string_tools = string_tools_t()
81
82contains
83
84 pure type(string_t) function create_string(s) result(res)
85 implicit none
86 character(len=*), intent(in) :: s
87
88 res = s
89 end function create_string
90
91 pure logical function equiv_character_string(lhs,rhs) result(retval)
92 implicit none
93 character(len=*), intent(in) :: lhs
94 type(string_t), intent(in) :: rhs
95
96 retval = (lhs == rhs%s)
97
98 end function
99
100 pure logical function equiv_string_character(lhs,rhs) result(retval)
101 implicit none
102 type(string_t), intent(in) :: lhs
103 character(len=*), intent(in) :: rhs
104
105 retval = (lhs%s == rhs)
106
107 end function
108
109 pure logical function equiv_string_string(lhs,rhs) result(retval)
110 implicit none
111 type(string_t), intent(in) :: lhs, rhs
112
113 retval = (lhs%s == rhs%s)
114
115 end function
116
117 pure logical function nequiv_character_string(lhs,rhs) result(retval)
118 implicit none
119 character(len=*), intent(in) :: lhs
120 type(string_t), intent(in) :: rhs
121
122 retval = (lhs /= rhs%s)
123
124 end function
125
126 pure logical function nequiv_string_character(lhs,rhs) result(retval)
127 implicit none
128 type(string_t), intent(in) :: lhs
129 character(len=*), intent(in) :: rhs
130
131 retval = (lhs%s /= rhs)
132
133 end function
134
135 pure logical function nequiv_string_string(lhs,rhs) result(retval)
136 implicit none
137 type(string_t), intent(in) :: lhs, rhs
138
139 retval = (lhs%s /= rhs%s)
140
141 end function
142
143
144 pure function concat_character_and_string(lhs,rhs) result(retval)
145 implicit none
146 character(len=*), intent(in) :: lhs
147 type(string_t), intent(in) :: rhs
148 character(len=:), allocatable :: retval
149
150 allocate(character(len(lhs)+rhs%len) :: retval)
151 retval = lhs//rhs%s
152
153 end function
154
155 pure function concat_string_and_character(lhs,rhs) result(retval)
156 implicit none
157 type(string_t), intent(in) :: lhs
158 character(len=*), intent(in) :: rhs
159 character(len=:), allocatable :: retval
160
161 allocate(character(len(rhs)+lhs%len) :: retval)
162 retval = lhs%s//rhs
163
164 end function
165
166 pure function concat_string_and_string(lhs,rhs) result(retval)
167 implicit none
168 type(string_t), intent(in) :: lhs, rhs
169 character(len=:), allocatable :: retval
170
171 allocate(character(lhs%len+rhs%len) :: retval)
172 retval = lhs%s//rhs%s
173
174 end function
175
176 subroutine write_string(str,unit, iotype, v_list, iostat, iomsg)
177 class(string_t), intent(in) :: str
178 INTEGER, INTENT(IN) :: unit
179 CHARACTER(*), INTENT(IN) :: iotype
180 INTEGER, INTENT(IN) :: v_list(:)
181 INTEGER, INTENT(OUT) :: iostat
182 CHARACTER(*), INTENT(INOUT) :: iomsg
183
184 write(unit, '(A)' , iostat=iostat) str%s
185
186 end subroutine write_string
187
188 subroutine write_string_unformatted(str,unit, iostat, iomsg)
189 class(string_t), intent(in) :: str
190 INTEGER, INTENT(IN) :: unit
191 INTEGER, INTENT(OUT) :: iostat
192 CHARACTER(*), INTENT(INOUT) :: iomsg
193
194 write(unit, *, iostat=iostat) str%s
195
196 end subroutine write_string_unformatted
197
198 SUBROUTINE read_string(str, unit, iotype, v_list, iostat, iomsg)
199 class(string_t), INTENT(INOUT) :: str
200 INTEGER, INTENT(IN) :: unit
201 CHARACTER(*), INTENT(IN) :: iotype
202 INTEGER, INTENT(IN) :: v_list(:)
203 INTEGER, INTENT(OUT) :: iostat
204 CHARACTER(*), INTENT(INOUT) :: iomsg
205
206 READ (unit, *, iostat=iostat) str%s
207
208 END SUBROUTINE
209
210 pure subroutine assign_int4_to_string(outstring, inint)
211 implicit none
212 type(string_t), intent(out) :: outstring
213 integer*4, intent(in) :: inint
214 character(len=512) :: tempchar
215 integer :: len
216
217 if(allocated(outstring%s)) deallocate(outstring%s)
218 write(tempchar,'(I0)') inint
219 len = len_trim(tempchar)
220 allocate(character(len) :: outstring%s)
221 outstring%s = trim(adjustl(tempchar))
222
223 end subroutine assign_int4_to_string
224
225 pure subroutine assign_character_to_string(outstring, inchar)
226 implicit none
227 type(string_t), intent(inout) :: outstring
228 character(len=*), intent(in) :: inchar
229 character(len=:), allocatable :: tempchar
230
231 !Note: outstring is intent(inout) and we are making a copy of inchar below to account for
232 !Edge cases like string = string%s(1:5)
233 allocate(character(len(inchar)) :: tempchar)
234 tempchar = inchar
235
236 if(allocated(outstring%s)) deallocate(outstring%s)
237 allocate(character(len(tempchar)) :: outstring%s)
238 outstring%s = tempchar
239 outstring%len = len(outstring%s)
240
241 end subroutine assign_character_to_string
242
243 pure subroutine assign_string_to_string(outstring, instring)
244 implicit none
245 type(string_t), intent(out) :: outstring
246 type(string_t), intent(in) :: instring
247
248 if(allocated(outstring%s)) deallocate(outstring%s)
249 allocate(character(instring%len) :: outstring%s)
250 outstring%s = instring%s
251 outstring%len = instring%len
252 end subroutine assign_string_to_string
253
254 pure function make_sentence_from_array_of_strings(array) result(s)
255 implicit none
256 type(string_t), dimension(:), intent(in) :: array
257 type(string_t) :: s
258
259 integer :: n, i
260
261 n = size(array)
262
263 s = array(1)
264 do i=2, n
265 s = s // ' ' // array(i)
266 enddo
267
269
270 subroutine make_upper(self)
271 implicit none
272 class(string_t), intent(inout) :: self
273 integer :: i, k
274
275 do i=1, len(self%s)
276 k = index(lower_case,self%s(i:i))
277 if(k /= 0) self%s(i:i) = upper_case(k:k)
278 enddo
279
280 end subroutine make_upper
281
282 subroutine make_lower(self)
283 implicit none
284 class(string_t), intent(inout) :: self
285 integer :: i, k
286
287 do i=1, len(self%s)
288 k = index(upper_case,self%s(i:i))
289 if(k /= 0) self%s(i:i) = lower_case(k:k)
290 enddo
291
292 end subroutine make_lower
293
294 pure integer function string_index(self, s)
295 implicit none
296 class(string_t), intent(in) :: self
297 character(len=*), intent(in) :: s
298
299 string_index = index(self%s, s)
300 end function string_index
301
302 pure subroutine remove(self, s)
303 implicit none
304 class(string_t), intent(inout) :: self
305 character(len=*), intent(in) :: s
306
307 integer :: i, j
308
309 do i=1, len(s)
310
311 j = index(self%s, s(i:i))
312 do while(j /= 0)
313 self = self%s(1:j-1)//self%s(j+1:)
314
315 j = index(self%s, s(i:i))
316 enddo
317
318 enddo
319
320
321 end subroutine remove
322
323end module strings_m
pure logical function nequiv_string_string(lhs, rhs)
Definition strings.f90:136
pure character(len=:) function, allocatable concat_string_and_character(lhs, rhs)
Definition strings.f90:156
pure type(string_t) function make_sentence_from_array_of_strings(array)
Definition strings.f90:255
pure logical function nequiv_string_character(lhs, rhs)
Definition strings.f90:127
subroutine read_string(str, unit, iotype, v_list, iostat, iomsg)
Definition strings.f90:199
pure logical function equiv_string_string(lhs, rhs)
Definition strings.f90:110
pure character(len=:) function, allocatable concat_character_and_string(lhs, rhs)
Definition strings.f90:145
pure subroutine remove(self, s)
Definition strings.f90:303
pure subroutine assign_character_to_string(outstring, inchar)
Definition strings.f90:226
character *26, parameter, private upper_case
Definition strings.f90:39
pure integer function string_index(self, s)
Definition strings.f90:295
pure logical function equiv_string_character(lhs, rhs)
Definition strings.f90:101
pure subroutine assign_string_to_string(outstring, instring)
Definition strings.f90:244
subroutine write_string_unformatted(str, unit, iostat, iomsg)
Definition strings.f90:189
subroutine make_lower(self)
Definition strings.f90:283
pure type(string_t) function create_string(s)
Definition strings.f90:85
pure subroutine assign_int4_to_string(outstring, inint)
Definition strings.f90:211
pure character(len=:) function, allocatable concat_string_and_string(lhs, rhs)
Definition strings.f90:167
subroutine write_string(str, unit, iotype, v_list, iostat, iomsg)
Definition strings.f90:177
type(string_tools_t), parameter, public string_tools
Definition strings.f90:80
pure logical function equiv_character_string(lhs, rhs)
Definition strings.f90:92
subroutine make_upper(self)
Definition strings.f90:271
character *26, parameter, private lower_case
Definition strings.f90:39
pure logical function nequiv_character_string(lhs, rhs)
Definition strings.f90:118