@@ -21,6 +21,7 @@ module regex_module
2121 private
2222
2323 public :: parse_pattern
24+ public :: check_pattern
2425 public :: regex
2526
2627 ! Character kind
@@ -108,6 +109,29 @@ module regex_module
108109
109110 contains
110111
112+ ! Check that a pattern matches the expected result
113+ logical function check_pattern (string ,pattern ,expected ) result(success)
114+ character (len=* ,kind= RCK), intent (in ) :: string
115+ character (len=* ,kind= RCK), intent (in ) :: pattern
116+ character (len=* ,kind= RCK), intent (in ) :: expected
117+
118+ integer :: idx,length
119+
120+ idx = regex(string,pattern,length)
121+
122+ if (idx> 0 ) then
123+ success = length== len (expected)
124+ if (success) success = string (idx:idx+ length-1 )==expected
125+ else
126+ success = len (expected)<= 0
127+ end if
128+
129+ if (DEBUG .and. .not. success) &
130+ print " ('[regex] test FAILED: text=',a,' pattern=',a,' index=',i0,' len=',i0)" , &
131+ string,pattern,idx,length
132+
133+ end function check_pattern
134+
111135 ! Clean up a pattern
112136 elemental subroutine pat_destroy (this )
113137 class(regex_pattern), intent (inout ) :: this
@@ -332,7 +356,6 @@ logical function matchstar(p, pattern, text, it0, matchlength)
332356 it = it-1
333357 if (matchstar) return
334358 matchlength = matchlength-1
335-
336359 end do
337360
338361 matchlength = prelen
@@ -586,24 +609,24 @@ integer function re_matchp_nolength(string, pattern) result(index)
586609 end function re_matchp_nolength
587610
588611
589- integer function re_matchp (string , pattern , matchlength ) result(index)
612+ integer function re_matchp (string , pattern , length ) result(index)
590613 type (regex_op), intent (in ) :: pattern
591614 character (len=* ,kind= RCK), intent (in ) :: string
592- integer , intent (out ) :: matchlength
593-
594- matchlength = 0
615+ integer , intent (out ) :: length
595616
596617 if (pattern% n> 0 ) then
597618
598619 if (pattern% pattern(1 )% type == BEGIN_WITH) then
599620
600621 ! String must begin with this pattern
601- index = merge (1 ,0 ,matchpattern(pattern% pattern(2 :), string, matchlength))
622+ length = 0
623+ index = merge (1 ,0 ,matchpattern(pattern% pattern(2 :), string, length))
602624
603625 else
604626
605627 do index= 1 ,len (string)
606- if (matchpattern(pattern% pattern,string (index:),matchlength)) return
628+ length = 0
629+ if (matchpattern(pattern% pattern,string (index:),length)) goto 1
607630 end do
608631
609632 index = 0
@@ -616,6 +639,13 @@ integer function re_matchp(string, pattern, matchlength) result(index)
616639
617640 end if
618641
642+ 1 if (DEBUG) then
643+ if (index== 0 ) then
644+ print " ('[regex] end: pattern not found. ')"
645+ else
646+ print " ('[regex] end: pattern found at ',i0,': ',a)" , index,string (index:)
647+ end if
648+ end if
619649
620650 end function re_matchp
621651
@@ -651,16 +681,17 @@ logical function matchpattern(pattern, text, matchlength) result(match)
651681
652682 elseif (pattern(ip)% type == END_WITH .and. pattern(ip+1 )% type == UNUSED) then
653683
684+ if (DEBUG .and. len (text(it:))>0 ) print * , ' [regex] at end: remaining = ' ,text(it:),' len=' ,matchlength
654685
655- match = len (text(it:))< = 1
686+ match = it > len (text)
656687 return
657688
658689 end if
659690
660- matchlength = matchlength+1
661-
662691 if (it> len (text)) exit iterate
663692
693+ matchlength = matchlength+1
694+
664695 if (DEBUG) print " ('[regex] matching ',i0,'-th pattern on chunk <',i0,':',i0,'>')" , ip,it,len (text)
665696 if (.not. pat_match(pattern(ip), text(it:it))) exit iterate
666697 ip = ip+1
0 commit comments