factor/library/io/string-streams.factor

29 lines
827 B
Factor
Raw Normal View History

2005-06-19 18:31:02 -04:00
USING: io kernel math namespaces sequences strings ;
! String buffers support the stream output protocol.
M: sbuf stream-write-attr nip sbuf-append ;
M: sbuf stream-close drop ;
M: sbuf stream-flush drop ;
M: sbuf stream-auto-flush drop ;
: string-out ( quot -- str )
[ 512 <sbuf> stdio set call stdio get >string ] with-scope ;
! Reversed string buffers support the stream input protocol.
M: sbuf stream-read1 ( sbuf -- char/f )
dup empty? [ drop f ] [ pop ] ifte ;
M: sbuf stream-read ( count sbuf -- string )
dup empty? [
2drop f
] [
swap over length min empty-sbuf
[ [ drop dup pop ] nmap drop ] keep
] ifte ;
: <string-reader> ( string -- stream )
>sbuf dup nreverse <line-reader> ;
: string-in ( str quot -- )
[ swap <string-reader> stdio set call ] with-scope ;