subroutine match(pattern, text, start, length)
character(len = *), intent(in):: pattern, text
integer, intent(out):: start, length
integer, allocatable:: ipattern(:)
integer:: text_length
! 空 pattern は空文字列に適合
if (len(pattern) <= 0) then
length = 0
start = 1
return
endif
! メタキャラクタの認識
allocate(ipattern(len(pattern) + 2))
call preprocess_pattern(pattern, ipattern)
! 頭寄せ指定のある場合
if (ipattern(1) == SYM_HEADFIX) then
start = 1
call match_here(ipattern(2: ), text, length)
if (length < 0) goto 995
goto 999
endif
! 最左原理
text_length = len(text)
do, start = 1, text_length + 1
call match_here(ipattern, text(start:text_length), length)
if (length >= 0) goto 999
end do
! みつからない場合
995 continue
start = 0
length = -1
999 continue
deallocate(ipattern)
end subroutine