SCM-NV / ftl

The Fortran Template Library
GNU General Public License v3.0
113 stars 13 forks source link

ftlRegex: Zero length match does not work #5

Closed GaidaiIgor closed 4 years ago

GaidaiIgor commented 4 years ago

The following program tries to find zero-length match:

program test_prg
  use ftlStringModule
  use ftlRegexModule

  character(:), allocatable :: line
  type(ftlRegex) :: r
  type(ftlRegexMatch), allocatable :: m(:)

  line = 'foo'
  call r % New('w*')
  m = r % Match(line)
end program

The call to r % Match(line) sends the program to an infinite loop because begin value in NumMatchesRaw does not change.

Proposed solution: advance begin by at least 1: begin = begin + max(m%end - 1, 1) This fixes infinite loop, but the empty match after the last character of the string would still not be detected.

Not sure if overlapping matches are supposed to be supported, but if they are then you could always advance begin by 1.

robertrueger commented 4 years ago

I'm afraid I didn't really consider regular expressions that match the empty string. I added you example as a unit test and changed some things in ftlRegex. It now matches the empty string 4 times in "foo", once before the first character, and then after every character. I believe this is correct result.

Thanks for reporting this :)

(I'm not so sure about the overlapping of matches. I think normally matches should not overlap and you have to use tricks like the positive lookahead to get overlapping matches. I'm afraid this will break pretty badly with ftlRegex though ...)