1 ! Copyright (C) 2005, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces math kernel sequences accessors fry circular ;
6 TUPLE: state string i ;
8 : get-i ( -- i ) state get i>> ;
10 : get-char ( -- char )
11 state get [ i>> ] [ string>> ] bi ?nth ;
13 : get-next ( -- char )
14 state get [ i>> 1+ ] [ string>> ] bi ?nth ;
17 state get [ 1+ ] change-i drop ;
19 : string-parse ( string quot -- )
20 [ 0 state boa state ] dip with-variable ;
22 : short* ( n seq -- n' seq )
23 over [ nip dup length swap ] unless ;
25 : skip-until ( quot: ( -- ? ) -- )
28 [ drop ] [ next skip-until ] if
29 ] [ drop ] if ; inline recursive
31 : take-until ( quot: ( -- ? ) -- )
32 [ get-i ] dip skip-until get-i
33 state get string>> subseq ;
35 : string-matches? ( string circular -- ? )
36 get-char over push-circular sequence= ;
38 : take-string ( match -- string )
39 dup length <circular-string>
40 [ 2dup string-matches? ] take-until nip
41 dup length rot length 1- - head next ;