1 ! Copyright (C) 2003, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors io kernel math namespaces sequences sbufs
4 strings generic splitting continuations destructors
5 io.streams.plain io.encodings math.order growable ;
10 : harden-as ( seq growble-exemplar -- newseq )
13 : growable-read-until ( growable n -- str )
14 >fixnum dupd tail-slice swap harden-as dup reverse-here ;
16 SINGLETON: null-encoding
18 M: null-encoding decode-char drop stream-read1 ;
22 M: growable dispose drop ;
24 M: growable stream-write1 push ;
25 M: growable stream-write push-all ;
26 M: growable stream-flush drop ;
28 : <string-writer> ( -- stream )
31 : with-string-writer ( quot -- str )
32 <string-writer> swap [ output-stream get ] compose with-output-stream*
35 M: growable stream-read1 [ f ] [ pop ] if-empty ;
37 : find-last-sep ( seq seps -- n )
38 swap [ memq? ] curry find-last drop ;
40 M: growable stream-read-until
41 [ find-last-sep ] keep over [
42 [ swap 1+ growable-read-until ] 2keep [ nth ] 2keep
45 [ swap drop 0 growable-read-until f like f ] keep
49 M: growable stream-read
53 [ length swap - 0 max ] keep
54 [ swap growable-read-until ] 2keep
58 M: growable stream-read-partial
61 : <string-reader> ( str -- stream )
62 >sbuf dup reverse-here null-encoding <decoder> ;
64 : with-string-reader ( str quot -- )
65 [ <string-reader> ] dip with-input-stream ; inline
67 INSTANCE: growable plain-writer