byte array streams

db4
Daniel Ehrenberg 2008-02-15 21:49:53 -06:00
parent 97dbf32608
commit 989b7a468a
2 changed files with 29 additions and 13 deletions

View File

@ -1,4 +1,4 @@
USING: io.streams.string io kernel arrays namespaces tools.test ; USING: io.streams.string io kernel arrays namespaces tools.test io.encodings.latin1 ;
IN: temporary IN: temporary
[ "line 1" CHAR: l ] [ "line 1" CHAR: l ]
@ -56,3 +56,6 @@ unit-test
dup stream-readln dup stream-readln
2 rot stream-read 2 rot stream-read
] unit-test ] unit-test
[ B{ 1 2 3 } ] [ latin1 [ { 1 2 3 } write ] with-byte-writer ] unit-test
[ "\u000001\u000002\u000003" ] [ { 1 2 3 } latin1 [ 3 read ] with-byte-reader ] unit-test

View File

@ -2,21 +2,28 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
IN: io.streams.string IN: io.streams.string
USING: io kernel math namespaces sequences sbufs strings USING: io kernel math namespaces sequences sbufs strings
generic splitting io.streams.plain io.streams.lines generic splitting io.streams.plain io.streams.lines growable
continuations ; continuations byte-vectors io.encodings byte-arrays ;
M: sbuf dispose drop ; M: growable dispose drop ;
M: sbuf stream-write1 push ; M: growable stream-write1 push ;
M: sbuf stream-write push-all ; M: growable stream-write push-all ;
M: sbuf stream-flush drop ; M: growable stream-flush drop ;
: <string-writer> ( -- stream ) : <string-writer> ( -- stream )
512 <sbuf> <plain-writer> ; 512 <sbuf> <plain-writer> ;
: string-out ( quot -- str ) : string-out ( quot -- str )
<string-writer> [ call stdio get >string ] with-stream* ; <string-writer> swap [ stdio get ] compose with-stream*
inline >string ; inline
: <byte-writer> ( encoding -- stream )
512 <byte-vector> swap <encoding> ;
: with-byte-writer ( encoding quot -- byte-array )
>r <byte-writer> r> [ stdio get ] compose with-stream*
>byte-array ; inline
: format-column ( seq ? -- seq ) : format-column ( seq ? -- seq )
[ [
@ -37,14 +44,14 @@ M: plain-writer stream-write-table
M: plain-writer make-cell-stream 2drop <string-writer> ; M: plain-writer make-cell-stream 2drop <string-writer> ;
M: sbuf stream-read1 dup empty? [ drop f ] [ pop ] if ; M: growable stream-read1 dup empty? [ drop f ] [ pop ] if ;
: sbuf-read-until ( sbuf n -- str ) : sbuf-read-until ( sbuf n -- str )
tail-slice >string dup reverse-here ; tail-slice >string dup reverse-here ;
: find-last-sep swap [ memq? ] curry find-last drop ; : find-last-sep swap [ memq? ] curry find-last drop ;
M: sbuf stream-read-until M: growable stream-read-until
[ find-last-sep ] keep over [ [ find-last-sep ] keep over [
[ swap 1+ sbuf-read-until ] 2keep [ nth ] 2keep [ swap 1+ sbuf-read-until ] 2keep [ nth ] 2keep
set-length set-length
@ -53,7 +60,7 @@ M: sbuf stream-read-until
delete-all delete-all
] if ; ] if ;
M: sbuf stream-read M: growable stream-read
dup empty? [ dup empty? [
2drop f 2drop f
] [ ] [
@ -62,7 +69,7 @@ M: sbuf stream-read
set-length set-length
] if ; ] if ;
M: sbuf stream-read-partial M: growable stream-read-partial
stream-read ; stream-read ;
: <string-reader> ( str -- stream ) : <string-reader> ( str -- stream )
@ -70,3 +77,9 @@ M: sbuf stream-read-partial
: string-in ( str quot -- ) : string-in ( str quot -- )
>r <string-reader> r> with-stream ; inline >r <string-reader> r> with-stream ; inline
: <byte-reader> ( byte-array encoding -- stream )
>r >byte-vector dup reverse-here r> <decoding> ;
: with-byte-reader ( byte-array encoding quot -- )
>r <byte-reader> r> with-stream ; inline