@@ -73,8 +73,7 @@ module regex_module
7373 integer :: type = UNUSED
7474
7575 ! Single or multi-character pattern
76- character (kind= RCK,len= MAX_CHAR_CLASS_LEN) :: ccl = repeat (' ' ,MAX_CHAR_CLASS_LEN)
77-
76+ character (kind= RCK,len= :), allocatable :: ccl
7877 contains
7978
8079 procedure :: print = > print_pattern
@@ -112,8 +111,9 @@ module regex_module
112111 ! Clean up a pattern
113112 elemental subroutine pat_destroy (this )
114113 class(regex_pattern), intent (inout ) :: this
114+ integer :: ierr
115115 this% type = UNUSED
116- this% ccl = repeat ( ' ' ,MAX_CHAR_CLASS_LEN )
116+ deallocate ( this% ccl,stat = ierr )
117117 end subroutine pat_destroy
118118
119119 ! Number of rules in the current pattern
@@ -147,19 +147,17 @@ end subroutine write_pattern
147147
148148 elemental subroutine destroy (this )
149149 class(regex_op), intent (inout ) :: this
150- integer :: i,ierr
150+ integer :: i
151151 do i= 1 ,MAX_REGEXP_OBJECTS
152- this% pattern(i)% type = UNUSED
153- this% pattern(i)% ccl = ' '
152+ call this% pattern(i)% destroy()
154153 end do
155154 end subroutine destroy
156155
157156 subroutine finalize (this )
158157 type (regex_op), intent (inout ) :: this
159- integer :: i,ierr
158+ integer :: i
160159 do i= 1 ,MAX_REGEXP_OBJECTS
161- this% pattern(i)% type = UNUSED
162- this% pattern(i)% ccl = ' '
160+ call this% pattern(i)% destroy()
163161 end do
164162 end subroutine finalize
165163
@@ -237,7 +235,7 @@ logical function matchcharclass(c,str) result(match)
237235 i = 0
238236
239237 ! All characters in the charclass contents
240- loop: do while (i< len_trim (str))
238+ loop: do while (i< len (str))
241239
242240 i = i+1
243241
@@ -266,7 +264,7 @@ logical function matchcharclass(c,str) result(match)
266264 ! Character match
267265 if (c== DASH) then
268266 ! If this is a range, the character must be in this range, that we evaluate with the ASCII collating sequence
269- match = i<= 0 .or. i+1 > len_trim (str)
267+ match = i<= 0 .or. i+1 > len (str)
270268 else
271269 match = .true.
272270 end if
@@ -408,11 +406,12 @@ subroutine new_from_pattern(this,pattern)
408406
409407 ! Local variables
410408 character (len= MAX_CHAR_CLASS_LEN,kind= RCK) :: ccl_buf ! size of buffer for chars in all char-classes in the expression. */
411- integer :: loc,i,j,lenp
409+ integer :: loc,i,j,lenp,lenc
412410 character (kind= RCK) :: c
413411
414412 ! Initialize class
415413 call this% destroy()
414+ ccl_buf = repeat (SPACE,MAX_CHAR_CLASS_LEN)
416415
417416 if (DEBUG) print " ('[regex] parsing pattern: <',a,'>')" , trim (pattern)
418417
@@ -487,6 +486,7 @@ subroutine new_from_pattern(this,pattern)
487486
488487 ! Remove any escape characters
489488 loc = index (pattern(i+1 :),' ]' )
489+ lenc = loc-1
490490 if (loc> 0 ) then
491491 ccl_buf = pattern(i+1 :i+ loc-1 )
492492 i = i+ loc
@@ -512,8 +512,10 @@ subroutine new_from_pattern(this,pattern)
512512 end if
513513 end if
514514
515- ! Terminate string
516- this% pattern(j)% ccl = trim (ccl_buf)
515+ ! Ensure there are no spaces
516+
517+ allocate (character (len= lenc,kind= RCK) :: this% pattern(j)% ccl)
518+ this% pattern(j)% ccl = ccl_buf(:lenc)
517519
518520 case default
519521
0 commit comments