This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
regular_expressions.f90
320 lines (283 loc) · 12.5 KB
/
regular_expressions.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
! regular_expressions.f90
! https://docs.microsoft.com/en-us/cpp/standard-library/regex?view=vs-2019
!> @brief A collection of routines providing regular expression support.
module regular_expressions
use iso_fortran_env
use iso_c_binding
use strings
implicit none
private
public :: regex_match
public :: regex_search
public :: regex_replace
! ******************************************************************************
! C INTERFACE
! ------------------------------------------------------------------------------
interface
function c_regex_match(src, pattern, numbuff, buffsizes, buffer, &
itemsizes) result(rst) bind(C, name = "c_regex_match")
! Arguments
use iso_c_binding
character(kind = c_char), intent(in) :: src(*), pattern(*)
integer(c_int), intent(in), value :: numbuff, buffsizes
type(c_ptr), intent(out) :: buffer(numbuff)
integer(c_int), intent(out) :: itemsizes(numbuff)
integer(c_int) :: rst
end function
function c_regex_search(src, pattern, numbuff, buffsizes, buffer, &
itemsizes) result(rst) bind(C, name = "c_regex_search")
! Arguments
use iso_c_binding
character(kind = c_char), intent(in) :: src(*), pattern(*)
integer(c_int), intent(in), value :: numbuff, buffsizes
type(c_ptr), intent(out) :: buffer(numbuff)
integer(c_int), intent(out) :: itemsizes(numbuff)
integer(c_int) :: rst
end function
subroutine c_regex_replace(src, pattern, rplc, buffsize, buffer, &
nbuff) bind(C, name = "c_regex_replace")
! Arguments
use iso_c_binding
character(kind = c_char), intent(in) :: src(*), pattern(*), rplc(*)
integer(c_int), intent(in), value :: buffsize
character(kind = c_char), intent(out) :: buffer(*)
integer(c_int), intent(out) :: nbuff
end subroutine
end interface
! ******************************************************************************
! FORTRAN INTERFACES
! ------------------------------------------------------------------------------
!> @brief Looks for sequences that match the requested pattern. The entire
!! target sequence must match the regular expression for this function to
!! succeed. Utilize regex_search if only a partial match is required.
interface regex_match
module procedure :: regex_match_char
module procedure :: regex_match_str
end interface
! ------------------------------------------------------------------------------
!> @brief Looks for sequences that match the requested pattern.
interface regex_search
module procedure :: regex_search_char
module procedure :: regex_search_str
end interface
! ------------------------------------------------------------------------------
!> @brief
interface regex_replace
module procedure :: regex_replace_char
module procedure :: regex_replace_str
end interface
contains
! ------------------------------------------------------------------------------
!> @brief Looks for sequences that match the requested pattern. The entire
!! target sequence must match the regular expression for this function to
!! succeed.
!!
!! @param[in] src The string to search.
!! @param[in] pattern The pattern to match.
!! @param[in] buffsz An optional input that allows the user to control the
!! size of each internal storage buffer. The default is 1024 characters.
!! @param[in] nbuff An optional input that allows the user to control the
!! quantity of internal storage buffers available to the regular expression
!! parser. The default is 1024.
!!
!! @return A list of all matching sequences in @p src.
function regex_match_char(src, pattern, buffsz, nbuff) result(rst)
! Arguments
character(len = *), intent(in) :: src, pattern
integer(int32), intent(in), optional :: buffsz, nbuff
type(string), allocatable, dimension(:) :: rst
! Parameters
integer(int32), parameter :: defaultBufferSize = 1024
integer(int32), parameter :: defaultNumBuffers = 1024
! Local Variables
integer(int32) :: i, nsrc, nptrn, nitems, n, numBuffers, bufferSize
type(c_ptr), allocatable, dimension(:) :: buffer
character(kind = c_char, len = :), allocatable, target, &
dimension(:) :: bufferStrings
character(kind = c_char), allocatable, dimension(:) :: csrc, cpattern
integer(int32), allocatable, dimension(:) :: sizeList
! Initialization
if (present(buffsz)) then
bufferSize = buffsz
if (bufferSize < 1) bufferSize = defaultBufferSize
else
bufferSize = defaultBufferSize
end if
if (present(nbuff)) then
numBuffers = nbuff
if (numBuffers < 1) numBuffers = defaultNumBuffers
else
numBuffers = defaultNumBuffers
end if
! Get the location of each buffer string
allocate(buffer(numBuffers))
allocate(character(kind = c_char, len = bufferSize) :: &
bufferStrings(numBuffers))
do i = 1, numBuffers
buffer(i) = c_loc(bufferStrings(i))
end do
! Convert the input strings into something useful for C
nsrc = len(src) + 1
nptrn = len(pattern) + 1 ! +1 allows for null character
allocate(csrc(nsrc))
allocate(cpattern(nptrn))
call to_c_string(src, csrc, nsrc)
call to_c_string(pattern, cpattern, nptrn)
! Perform the operation
allocate(sizeList(numBuffers))
nitems = c_regex_match(csrc, cpattern, numBuffers, bufferSize, &
buffer, sizeList)
! Define output
allocate(rst(nitems))
do i = 1, nitems
n = sizeList(i)
rst(i)%str = bufferStrings(i)(1:n)
end do
end function
! --------------------
!> @brief Looks for sequences that match the requested pattern. The entire
!! target sequence must match the regular expression for this function to
!! succeed.
!!
!! @param[in] src The string to search.
!! @param[in] pattern The pattern to match.
!! @param[in] buffsz An optional input that allows the user to control the
!! size of each internal storage buffer. The default is 1024 characters.
!! @param[in] nbuff An optional input that allows the user to control the
!! quantity of internal storage buffers available to the regular expression
!! parser. The default is 1024.
!!
!! @return A list of all matching sequences in @p src.
function regex_match_str(src, pattern, buffsz, nbuff) result(rst)
! Arguments
class(string), intent(in) :: src, pattern
integer(int32), intent(in), optional :: buffsz, nbuff
type(string), allocatable, dimension(:) :: rst
! Process
rst = regex_match_char(src%str, pattern%str, buffsz, nbuff)
end function
! ------------------------------------------------------------------------------
!> @brief Looks for sequences that match the requested pattern.
!!
!! @param[in] src The string to search.
!! @param[in] pattern The pattern to match.
!! @param[in] buffsz An optional input that allows the user to control the
!! size of each internal storage buffer. The default is 1024 characters.
!! @param[in] nbuff An optional input that allows the user to control the
!! quantity of internal storage buffers available to the regular expression
!! parser. The default is 1024.
!!
!! @return A list of all matching sequences in @p src.
function regex_search_char(src, pattern, buffsz, nbuff) result(rst)
! Arguments
character(len = *), intent(in) :: src, pattern
integer(int32), intent(in), optional :: buffsz, nbuff
type(string), allocatable, dimension(:) :: rst
! Parameters
integer(int32), parameter :: defaultBufferSize = 1024
integer(int32), parameter :: defaultNumBuffers = 1024
! Local Variables
integer(int32) :: i, nsrc, nptrn, nitems, n, numBuffers, bufferSize
type(c_ptr), allocatable, dimension(:) :: buffer
character(kind = c_char, len = :), allocatable, target, &
dimension(:) :: bufferStrings
character(kind = c_char), allocatable, dimension(:) :: csrc, cpattern
integer(int32), allocatable, dimension(:) :: sizeList
! Initialization
if (present(buffsz)) then
bufferSize = buffsz
if (bufferSize < 1) bufferSize = defaultBufferSize
else
bufferSize = defaultBufferSize
end if
if (present(nbuff)) then
numBuffers = nbuff
if (numBuffers < 1) numBuffers = defaultNumBuffers
else
numBuffers = defaultNumBuffers
end if
! Get the location of each buffer string
allocate(buffer(numBuffers))
allocate(character(kind = c_char, len = bufferSize) :: &
bufferStrings(numBuffers))
do i = 1, numBuffers
buffer(i) = c_loc(bufferStrings(i))
end do
! Convert the input strings into something useful for C
nsrc = len(src) + 1
nptrn = len(pattern) + 1 ! +1 allows for null character
allocate(csrc(nsrc))
allocate(cpattern(nptrn))
call to_c_string(src, csrc, nsrc)
call to_c_string(pattern, cpattern, nptrn)
! Perform the operation
allocate(sizeList(numBuffers))
nitems = c_regex_search(csrc, cpattern, numBuffers, bufferSize, &
buffer, sizeList)
! Define output
allocate(rst(nitems))
do i = 1, nitems
n = sizeList(i)
rst(i)%str = bufferStrings(i)(1:n)
end do
end function
! --------------------
!> @brief Looks for sequences that match the requested pattern.
!!
!! @param[in] src The string to search.
!! @param[in] pattern The pattern to match.
!! @param[in] buffsz An optional input that allows the user to control the
!! size of each internal storage buffer. The default is 1024 characters.
!! @param[in] nbuff An optional input that allows the user to control the
!! quantity of internal storage buffers available to the regular expression
!! parser. The default is 1024.
!!
!! @return A list of all matching sequences in @p src.
function regex_search_str(src, pattern, buffsz, nbuff) result(rst)
! Arguments
class(string), intent(in) :: src, pattern
integer(int32), intent(in), optional :: buffsz, nbuff
type(string), allocatable, dimension(:) :: rst
! Process
rst = regex_search_char(src%str, pattern%str, buffsz, nbuff)
end function
! ------------------------------------------------------------------------------
!
function regex_replace_char(src, pattern, rplc) result(rst)
! Arguments
character(len = *), intent(in) :: src, pattern, rplc
character(len = :), allocatable :: rst
! Local Variables
integer(int32) :: nsrc, nptrn, nrplc, nbuffer, nout
character(kind = c_char), allocatable, dimension(:) :: csrc, &
cpattern, crplc, buffer
! Initialization
nsrc = len(src) + 1
nptrn = len(pattern) + 1 ! +1 allows for null character
nrplc = len(rplc) + 1
nbuffer = nrplc * nsrc
allocate(csrc(nsrc))
allocate(cpattern(nptrn))
allocate(crplc(nrplc))
allocate(buffer(nbuffer))
! Convert input Fortran strings to C-friendly versions
call to_c_string(src, csrc, nsrc)
call to_c_string(pattern, cpattern, nptrn)
call to_c_string(rplc, crplc, nrplc)
! Process
call c_regex_replace(csrc, cpattern, crplc, nbuffer, buffer, nout)
! Return the C string output as a Fortran string
rst = to_fortran_string(buffer, nout)
end function
! --------------------
!
function regex_replace_str(src, pattern, rplc) result(rst)
! Arguments
class(string), intent(in) :: src, pattern, rplc
type(string) :: rst
! Process
rst%str = regex_replace_char(src%str, pattern%str, rplc%str)
end function
! ------------------------------------------------------------------------------
! ------------------------------------------------------------------------------
end module