! Copyright (C) 2008, 2009 Daniel Ehrenberg, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: kernel sequences math splitting make fry locals math.ranges accessors arrays ; IN: regexp.matchers ! For now, a matcher is just something with a method to do the ! equivalent of match. GENERIC: match-index-from ( i string matcher -- index/f ) : match-index-head ( string matcher -- index/f ) [ 0 ] 2dip match-index-from ; : match-slice ( i string matcher -- slice/f ) [ 2dup ] dip match-index-from [ swap ] [ 2drop f ] if* ; : matches? ( string matcher -- ? ) dupd match-index-head [ swap length = ] [ drop f ] if* ; : match-from ( i string matcher -- slice/f ) [ [ length [a,b) ] keep ] dip '[ _ _ match-slice ] map-find drop ; : match-head ( str matcher -- slice/f ) [ 0 ] 2dip match-from ; > ] when ] keep ; PRIVATE> :: all-matches ( string matcher -- seq ) 0 [ dup ] [ string matcher next-match ] produce nip but-last ; : count-matches ( string matcher -- n ) all-matches length ; > ] map 0 prefix slices [ from>> ] map string length suffix [ string ] 2map ; PRIVATE> : re-split1 ( string matcher -- before after/f ) dupd match-head [ 1array split-slices first2 ] [ f ] if* ; : re-split ( string matcher -- seq ) dupd all-matches split-slices ; : re-replace ( string matcher replacement -- result ) [ re-split ] dip join ;