- execute( i string regexp -- i start end ? ) ; inline
-
-:: (each-match) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
- i string regexp do-next-match [| i' start end |
- start end string quot call
- i' string regexp quot (each-match)
- ] [ 3drop ] if ; inline recursive
+ execute( i string regexp -- start end ? ) ; inline
+
+:: (each-match-forward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
+ i string length <= [
+ i string regexp do-next-match [| start end |
+ start end string quot call
+ start end eq? [ end 1 + ] [ end ] if
+ string regexp quot (each-match-forward)
+ ] [ 2drop ] if
+ ] when ; inline recursive
+
+:: (each-match-backward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
+ i -1 >= [
+ i string regexp do-next-match [| start end |
+ start 1 + end 1 + string quot call
+ start end eq? [ start 1 - ] [ start ] if
+ string regexp quot (each-match-backward)
+ ] [ 2drop ] if
+ ] when ; inline recursive
+
+: (each-match) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
+ over reverse-regexp? [ (each-match-backward) ] [ (each-match-forward) ] if ; inline
+
+GENERIC: match-iterator-start ( string regexp -- start )
+M: regexp match-iterator-start 2drop 0 ;
+M: reverse-regexp match-iterator-start drop length ;