Skip to content

Commit ce37e23

Browse files
committed
Refactoring. Updated syntax.
1 parent 55273a8 commit ce37e23

File tree

3 files changed

+77
-80
lines changed

3 files changed

+77
-80
lines changed

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,29 @@ program main
5353
use, intrinsic :: iso_c_binding
5454
use :: pcre2
5555
implicit none (type, external)
56-
integer, parameter :: OVECSIZE = 30
56+
57+
integer, parameter :: OVECSIZE = 30 ! Must be multiple of 3.
5758
5859
character(len=128) :: buffer
5960
character(len=:), allocatable :: pattern, subject
6061
integer :: err_code, rc
61-
integer(kind=PCRE2_SIZE) :: err_offset
62+
integer(kind=pcre2_size) :: err_offset
6263
type(c_ptr) :: match_data, re
6364
6465
pattern = '^([A-Z][a-z]+)$'
6566
subject = 'Fortran'
6667
6768
! Compile regular expression.
6869
re = pcre2_compile(pattern = pattern, &
69-
length = len(pattern, kind=PCRE2_SIZE), &
70+
length = len(pattern, kind=pcre2_size), &
7071
options = 0, &
7172
errorcode = err_code, &
7273
erroroffset = err_offset, &
7374
ccontext = c_null_ptr)
7475
7576
if (.not. c_associated(re)) then
7677
buffer = ' '
77-
rc = pcre2_get_error_message(err_code, buffer, len(buffer, kind=PCRE2_SIZE))
78+
rc = pcre2_get_error_message(err_code, buffer, len(buffer, kind=pcre2_size))
7879
print '("Error ", i0, ": ", a)', err_code, trim(buffer)
7980
stop
8081
end if
@@ -84,8 +85,8 @@ program main
8485
8586
rc = pcre2_match(code = re, &
8687
subject = subject, &
87-
length = len(subject, kind=PCRE2_SIZE), &
88-
startoffset = int(0, kind=PCRE2_SIZE), &
88+
length = len(subject, kind=pcre2_size), &
89+
startoffset = int(0, kind=pcre2_size), &
8990
options = 0, &
9091
match_data = match_data, &
9192
mcontext = c_null_ptr)

src/pcre2.f90

+63-67
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ module pcre2
1111
private
1212

1313
integer, parameter, public :: c_uint32_t = c_int32_t
14-
integer, parameter, public :: PCRE2_UCHAR = c_char
15-
integer, parameter, public :: PCRE2_SPTR = c_char
16-
integer, parameter, public :: PCRE2_SIZE = c_size_t
17-
integer, parameter, public :: PCRE2_SIZE_MAX = storage_size(int(0, kind=PCRE2_SIZE)) / 8
18-
integer(kind=PCRE2_SIZE), parameter, public :: PCRE2_ZERO_TERMINATED = not(int(0, kind=PCRE2_SIZE))
19-
integer(kind=PCRE2_SIZE), parameter, public :: PCRE2_UNSET = not(int(0, kind=PCRE2_SIZE))
14+
integer, parameter, public :: pcre2_uchar = c_char
15+
integer, parameter, public :: pcre2_sptr = c_char
16+
integer, parameter, public :: pcre2_size = c_size_t
17+
integer, parameter, public :: pcre2_size_max = storage_size(int(0, kind=pcre2_size)) / 8
18+
integer(kind=pcre2_size), parameter, public :: pcre2_zero_terminated = not(int(0, kind=pcre2_size))
19+
integer(kind=pcre2_size), parameter, public :: pcre2_unset = not(int(0, kind=pcre2_size))
2020

2121
integer(kind=c_int), parameter, public :: PCRE2_ANCHORED = int(z'80000000')
2222
integer(kind=c_int), parameter, public :: PCRE2_NO_UTF_CHECK = int(z'40000000')
@@ -309,40 +309,39 @@ module pcre2
309309
public :: pcre2_match_data_create
310310
public :: pcre2_match_data_free
311311
public :: pcre2_substring_copy_byname
312+
public :: pcre2_substring_copy_byname_
312313
public :: pcre2_substring_copy_bynumber
314+
public :: pcre2_substring_copy_bynumber_
313315
public :: pcre2_substring_free
314316
public :: pcre2_substring_get_byname
317+
public :: pcre2_substring_get_byname_
315318
public :: pcre2_substring_get_bynumber
319+
public :: pcre2_substring_get_bynumber_
316320
public :: pcre2_substring_number_from_name
317321

318322
private :: c_f_str_ptr
319-
private :: c_strlen
320-
private :: pcre2_substring_copy_byname_
321-
private :: pcre2_substring_copy_bynumber_
322-
private :: pcre2_substring_get_byname_
323-
private :: pcre2_substring_get_bynumber_
324323

325324
interface
326325
! pcre2_code *pcre2_compile(PCRE2_SPTR pattern, PCRE2_SIZE length, uint32_t options, int *errorcode, PCRE2_SIZE *erroroffset, pcre2_compile_context *ccontext)
327326
function pcre2_compile(pattern, length, options, errorcode, erroroffset, ccontext) bind(c, name='pcre2_compile_8')
328-
import :: PCRE2_SIZE, PCRE2_SPTR, c_uint32_t, c_int, c_ptr
327+
import :: c_uint32_t, c_int, c_ptr, pcre2_size, pcre2_sptr
329328
implicit none
330-
character(kind=PCRE2_SPTR), intent(in) :: pattern
331-
integer(kind=PCRE2_SIZE), intent(in), value :: length
329+
character(kind=pcre2_sptr), intent(in) :: pattern
330+
integer(kind=pcre2_size), intent(in), value :: length
332331
integer(kind=c_uint32_t), intent(in), value :: options
333332
integer(kind=c_int), intent(out) :: errorcode
334-
integer(kind=PCRE2_SIZE), intent(out) :: erroroffset
333+
integer(kind=pcre2_size), intent(out) :: erroroffset
335334
type(c_ptr), intent(in), value :: ccontext
336335
type(c_ptr) :: pcre2_compile
337336
end function pcre2_compile
338337

339338
! int pcre2_get_error_message(int errorcode, PCRE2_UCHAR *buffer, PCRE2_SIZE bufflen)
340339
function pcre2_get_error_message(errorcode, buffer, bufflen) bind(c, name='pcre2_get_error_message_8')
341-
import :: c_int, PCRE2_SIZE, PCRE2_UCHAR
340+
import :: c_int, pcre2_size, pcre2_uchar
342341
implicit none
343342
integer(kind=c_int), intent(in), value :: errorcode
344-
character(kind=PCRE2_UCHAR), intent(inout) :: buffer
345-
integer(kind=PCRE2_SIZE), intent(in), value :: bufflen
343+
character(kind=pcre2_uchar), intent(inout) :: buffer
344+
integer(kind=pcre2_size), intent(in), value :: bufflen
346345
integer(kind=c_int) :: pcre2_get_error_message
347346
end function pcre2_get_error_message
348347

@@ -364,12 +363,12 @@ end function pcre2_get_ovector_pointer
364363

365364
! int pcre2_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length, PCRE2_SIZE startoffset, uint32_t options, pcre2_match_data *match_data, pcre2_match_context *mcontext)
366365
function pcre2_match(code, subject, length, startoffset, options, match_data, mcontext) bind(c, name='pcre2_match_8')
367-
import :: c_int, c_ptr, c_uint32_t, PCRE2_SIZE, PCRE2_SPTR
366+
import :: c_int, c_ptr, c_uint32_t, pcre2_size, pcre2_sptr
368367
implicit none
369368
type(c_ptr), intent(in), value :: code
370-
character(kind=PCRE2_SPTR), intent(in) :: subject
371-
integer(kind=PCRE2_SIZE), intent(in), value :: length
372-
integer(kind=PCRE2_SIZE), intent(in), value :: startoffset
369+
character(kind=pcre2_sptr), intent(in) :: subject
370+
integer(kind=pcre2_size), intent(in), value :: length
371+
integer(kind=pcre2_size), intent(in), value :: startoffset
373372
integer(kind=c_uint32_t), intent(in), value :: options
374373
type(c_ptr), intent(in), value :: match_data
375374
type(c_ptr), intent(in), value :: mcontext
@@ -387,55 +386,55 @@ end function pcre2_match_data_create
387386

388387
! int pcre2_substring_copy_byname(pcre2_match_data *match_data, PCRE2_SPTR name, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
389388
function pcre2_substring_copy_byname_(match_data, name, buffer, bufflen) bind(c, name='pcre2_substring_copy_byname_8')
390-
import :: c_int, c_ptr, PCRE2_SIZE, PCRE2_SPTR, PCRE2_UCHAR
389+
import :: c_int, c_ptr, pcre2_size, pcre2_sptr, pcre2_uchar
391390
implicit none
392391
type(c_ptr), intent(in), value :: match_data
393-
character(kind=PCRE2_SPTR), intent(in) :: name
394-
character(kind=PCRE2_UCHAR), intent(inout) :: buffer
395-
integer(kind=PCRE2_SIZE), intent(inout) :: bufflen
392+
character(kind=pcre2_sptr), intent(in) :: name
393+
character(kind=pcre2_uchar), intent(inout) :: buffer
394+
integer(kind=pcre2_size), intent(inout) :: bufflen
396395
integer(kind=c_int) :: pcre2_substring_copy_byname_
397396
end function pcre2_substring_copy_byname_
398397

399398
! int pcre2_substring_copy_bynumber(pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR *buffer, PCRE2_SIZE *bufflen)
400399
function pcre2_substring_copy_bynumber_(match_data, number, buffer, bufflen) bind(c, name='pcre2_substring_copy_bynumber_8')
401-
import :: c_int, c_ptr, c_uint32_t, PCRE2_SIZE, PCRE2_UCHAR
400+
import :: c_int, c_ptr, c_uint32_t, pcre2_size, pcre2_uchar
402401
implicit none
403402
type(c_ptr), intent(in), value :: match_data
404403
integer(kind=c_uint32_t), intent(in), value :: number
405-
character(kind=PCRE2_UCHAR), intent(inout) :: buffer
406-
integer(kind=PCRE2_SIZE), intent(inout) :: bufflen
404+
character(kind=pcre2_uchar), intent(inout) :: buffer
405+
integer(kind=pcre2_size), intent(inout) :: bufflen
407406
integer(kind=c_int) :: pcre2_substring_copy_bynumber_
408407
end function pcre2_substring_copy_bynumber_
409408

410409
! int pcre2_substring_get_byname(pcre2_match_data *match_data, PCRE2_SPTR name, PCRE2_UCHAR **bufferptr, PCRE2_SIZE *bufflen)
411410
function pcre2_substring_get_byname_(match_data, name, bufferptr, bufflen) bind(c, name='pcre2_substring_get_byname_8')
412-
import :: c_int, c_ptr, PCRE2_SIZE, PCRE2_SPTR
411+
import :: c_int, c_ptr, pcre2_size, pcre2_sptr
413412
implicit none
414413
type(c_ptr), intent(in), value :: match_data
415-
character(kind=PCRE2_SPTR), intent(in) :: name
414+
character(kind=pcre2_sptr), intent(in) :: name
416415
type(c_ptr), intent(out) :: bufferptr
417-
integer(kind=PCRE2_SIZE), intent(out) :: bufflen
416+
integer(kind=pcre2_size), intent(out) :: bufflen
418417
integer(kind=c_int) :: pcre2_substring_get_byname_
419418
end function pcre2_substring_get_byname_
420419

421420
! int pcre2_substring_get_bynumber(pcre2_match_data *match_data, uint32_t number, PCRE2_UCHAR **bufferptr, PCRE2_SIZE *bufflen)
422421
function pcre2_substring_get_bynumber_(match_data, number, bufferptr, bufflen) &
423422
bind(c, name='pcre2_substring_get_bynumber_8')
424-
import :: c_int, c_ptr, c_uint32_t, PCRE2_SIZE
423+
import :: c_int, c_ptr, c_uint32_t, pcre2_size
425424
implicit none
426425
type(c_ptr), intent(in), value :: match_data
427426
integer(kind=c_uint32_t), intent(in), value :: number
428427
type(c_ptr), intent(out) :: bufferptr
429-
integer(kind=PCRE2_SIZE), intent(out) :: bufflen
428+
integer(kind=pcre2_size), intent(out) :: bufflen
430429
integer(kind=c_int) :: pcre2_substring_get_bynumber_
431430
end function pcre2_substring_get_bynumber_
432431

433432
! int pcre2_substring_number_from_name(const pcre2_code *code, PCRE2_SPTR name)
434433
function pcre2_substring_number_from_name(code, name) bind(c, name='pcre2_substring_number_from_name_8')
435-
import :: c_int, c_ptr, PCRE2_SPTR
434+
import :: c_int, c_ptr, pcre2_sptr
436435
implicit none
437436
type(c_ptr), intent(in), value :: code
438-
character(kind=PCRE2_SPTR), intent(in) :: name
437+
character(kind=pcre2_sptr), intent(in) :: name
439438
integer(kind=c_int) :: pcre2_substring_number_from_name
440439
end function pcre2_substring_number_from_name
441440

@@ -468,28 +467,19 @@ subroutine pcre2_substring_free(buffer) bind(c, name='pcre2_substring_free_8')
468467
end subroutine pcre2_substring_free
469468
end interface
470469

471-
interface
472-
! size_t strlen(const char *str)
473-
function c_strlen(str) bind(c, name='strlen')
474-
import :: c_ptr, c_size_t
475-
implicit none
476-
type(c_ptr), intent(in), value :: str
477-
integer(kind=c_size_t) :: c_strlen
478-
end function c_strlen
479-
end interface
480470
contains
481471
function pcre2_substring_copy_byname(match_data, name, buffer, buff_len) result(rc)
482472
type(c_ptr), intent(in) :: match_data
483473
character(len=*), intent(in) :: name
484474
character(len=*), intent(inout) :: buffer
485-
integer(kind=PCRE2_SIZE), intent(inout), optional :: buff_len
475+
integer(kind=pcre2_size), intent(inout), optional :: buff_len
486476
integer :: rc
487-
integer(kind=PCRE2_SIZE) :: sz
477+
integer(kind=pcre2_size) :: sz
488478

489479
if (present(buff_len)) then
490480
sz = buff_len
491481
else
492-
sz = len(buffer, kind=PCRE2_SIZE)
482+
sz = len(buffer, kind=pcre2_size)
493483
end if
494484

495485
rc = pcre2_substring_copy_byname_(match_data, name // c_null_char, buffer, sz)
@@ -500,14 +490,14 @@ function pcre2_substring_copy_bynumber(match_data, number, buffer, buff_len) res
500490
type(c_ptr), intent(in) :: match_data
501491
integer, intent(in) :: number
502492
character(len=*), intent(inout) :: buffer
503-
integer(kind=PCRE2_SIZE), intent(inout), optional :: buff_len
493+
integer(kind=pcre2_size), intent(inout), optional :: buff_len
504494
integer :: rc
505-
integer(kind=PCRE2_SIZE) :: sz
495+
integer(kind=pcre2_size) :: sz
506496

507497
if (present(buff_len)) then
508498
sz = buff_len
509499
else
510-
sz = len(buffer, kind=PCRE2_SIZE)
500+
sz = len(buffer, kind=pcre2_size)
511501
end if
512502

513503
rc = pcre2_substring_copy_bynumber_(match_data, number, buffer, sz)
@@ -518,34 +508,30 @@ function pcre2_substring_get_byname(match_data, name, buffer, buff_len) result(r
518508
type(c_ptr), intent(in) :: match_data
519509
character(len=*), intent(in) :: name
520510
character(len=:), allocatable, intent(out) :: buffer
521-
integer(kind=PCRE2_SIZE), intent(out) :: buff_len
522-
integer :: rc
523-
type(c_ptr) :: ptr
511+
integer(kind=pcre2_size), intent(out) :: buff_len
512+
513+
integer :: rc
514+
type(c_ptr) :: ptr
524515

525516
ptr = c_null_ptr
526517
rc = pcre2_substring_get_byname_(match_data, name // c_null_char, ptr, buff_len)
527-
528-
if (c_associated(ptr)) then
529-
call c_f_str_ptr(ptr, buffer)
530-
call pcre2_substring_free(ptr)
531-
end if
518+
call c_f_str_ptr(ptr, buffer)
519+
if (c_associated(ptr)) call pcre2_substring_free(ptr)
532520
end function pcre2_substring_get_byname
533521

534522
function pcre2_substring_get_bynumber(match_data, number, buffer, buff_len) result(rc)
535523
type(c_ptr), intent(in) :: match_data
536524
integer, intent(in) :: number
537525
character(len=:), allocatable, intent(out) :: buffer
538-
integer(kind=PCRE2_SIZE), intent(out) :: buff_len
539-
integer :: rc
540-
type(c_ptr) :: ptr
526+
integer(kind=pcre2_size), intent(out) :: buff_len
527+
528+
integer :: rc
529+
type(c_ptr) :: ptr
541530

542531
ptr = c_null_ptr
543532
rc = pcre2_substring_get_bynumber_(match_data, number, ptr, buff_len)
544-
545-
if (c_associated(ptr)) then
546-
call c_f_str_ptr(ptr, buffer)
547-
call pcre2_substring_free(ptr)
548-
end if
533+
call c_f_str_ptr(ptr, buffer)
534+
if (c_associated(ptr)) call pcre2_substring_free(ptr)
549535
end function pcre2_substring_get_bynumber
550536

551537
subroutine c_f_str_ptr(c_str, f_str)
@@ -556,6 +542,16 @@ subroutine c_f_str_ptr(c_str, f_str)
556542
character(kind=c_char), pointer :: ptrs(:)
557543
integer(kind=c_size_t) :: i, sz
558544

545+
interface
546+
! size_t strlen(const char *str)
547+
function c_strlen(str) bind(c, name='strlen')
548+
import :: c_ptr, c_size_t
549+
implicit none
550+
type(c_ptr), intent(in), value :: str
551+
integer(kind=c_size_t) :: c_strlen
552+
end function c_strlen
553+
end interface
554+
559555
copy_block: block
560556
if (.not. c_associated(c_str)) exit copy_block
561557
sz = c_strlen(c_str)

test/test_pcre2.f90

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ subroutine test(pattern, subject)
2121
character(len=:), allocatable :: substring
2222
integer :: i, rc, rc2
2323
integer :: err_code, oveccount
24-
integer(kind=PCRE2_SIZE) :: buffer_length, err_offset
25-
integer(kind=PCRE2_SIZE) :: substring_length, substring_start
26-
integer(kind=PCRE2_SIZE), pointer :: ovector(:)
24+
integer(kind=pcre2_size) :: buffer_length, err_offset
25+
integer(kind=pcre2_size) :: substring_length, substring_start
26+
integer(kind=pcre2_size), pointer :: ovector(:)
2727
type(c_ptr) :: match_data, ptr, re
2828

2929
! Compile regular expression.
30-
re = pcre2_compile(pattern, len(pattern, kind=PCRE2_SIZE), 0, err_code, err_offset, c_null_ptr)
30+
re = pcre2_compile(pattern, len(pattern, kind=pcre2_size), 0, err_code, err_offset, c_null_ptr)
3131

3232
if (.not. c_associated(re)) then
3333
buffer = ' '
34-
rc = pcre2_get_error_message(err_code, buffer, len(buffer, kind=PCRE2_SIZE))
34+
rc = pcre2_get_error_message(err_code, buffer, len(buffer, kind=pcre2_size))
3535
print '("Error ", i0, ": ", a)', err_code, trim(buffer)
3636
return
3737
end if
@@ -40,8 +40,8 @@ subroutine test(pattern, subject)
4040
match_data = pcre2_match_data_create(OVECSIZE, c_null_ptr)
4141
rc = pcre2_match(code = re, &
4242
subject = subject, &
43-
length = len(subject, kind=PCRE2_SIZE), &
44-
startoffset = int(0, kind=PCRE2_SIZE), &
43+
length = len(subject, kind=pcre2_size), &
44+
startoffset = int(0, kind=pcre2_size), &
4545
options = 0, &
4646
match_data = match_data, &
4747
mcontext = c_null_ptr)

0 commit comments

Comments
 (0)