         i' string regexp quot (each-match)
     ] [ 3drop ] if ; inline recursive
 
-PRIVATE>
-
 : prepare-match-iterator ( string regexp -- i string regexp )
     [ check-string ] dip [ end/start nip ] 2keep ; inline
+
+PRIVATE>
 
 : each-match ( string regexp quot: ( start end string -- ) -- )
     [ prepare-match-iterator ] dip (each-match) ; inline


<PRIVATE

TUPLE: match { i read-only } { start read-only } { end read-only } { string read-only } ;

:: <match> ( i string quot: ( i string -- i seq j ) reverse? -- match/f )
    i string quot call dup [| j |
        j i j
        reverse? [ swap [ 1+ ] bi@ ] when
        string match boa
    ] when ; inline

: search-range ( i string reverse? -- seq )
    [ drop 0 [a,b] ] [ length [a,b) ] if ; inline

: match>result ( match -- i start end string )
    dup
    [ { [ i>> ] [ start>> ] [ end>> ] [ string>> ] } cleave ]
    [ drop f f f f ]
    if ; inline

:: next-match ( i string quot reverse? -- i start end ? )
    i string reverse? search-range
    [ string quot reverse? <match> ] map-find drop
    match>result ; inline

: do-next-match ( i string regexp -- i start end ? )
    dup next-match>>

    dup '[
        dup \ next-initial-word = [
            drop _ [ compile-regexp dfa>> ] [ reverse-regexp? ] bi
            '[ _ _ next-match ]
            (( i string regexp -- i start end string )) simple-define-temp
        ] when
    ] change-next-match ;
