Merge branch 'master' of git://factorcode.org/git/littledan

db4
Slava Pestov 2008-02-16 14:06:19 -06:00
commit a5e70241a9
4 changed files with 35 additions and 23 deletions

View File

@ -0,0 +1,9 @@
USING: tools.test io.streams.byte-array io.encodings.binary
io.encodings.utf8 io kernel arrays strings ;
[ B{ 1 2 3 } ] [ binary [ { 1 2 3 } write ] with-byte-writer ] unit-test
[ B{ 1 2 3 } ] [ { 1 2 3 } binary [ 3 read ] with-byte-reader ] unit-test
[ B{ BIN: 11110101 BIN: 10111111 BIN: 10000000 BIN: 10111111 BIN: 11101111 BIN: 10000000 BIN: 10111111 BIN: 11011111 BIN: 10000000 CHAR: x } ]
[ { BIN: 101111111000000111111 BIN: 1111000000111111 BIN: 11111000000 CHAR: x } utf8 [ write ] with-byte-writer ] unit-test
[ { BIN: 101111111000000111111 } t ] [ { BIN: 11110101 BIN: 10111111 BIN: 10000000 BIN: 10111111 } utf8 <byte-reader> contents dup >array swap string? ] unit-test

View File

@ -0,0 +1,16 @@
USING: byte-arrays byte-vectors kernel io.encodings io.streams.string
sequences io namespaces ;
IN: io.streams.byte-array
: <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
: <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

View File

@ -1,4 +1,4 @@
USING: io.streams.string io kernel arrays namespaces tools.test io.encodings.latin1 ;
USING: io.streams.string io kernel arrays namespaces tools.test ;
IN: temporary
[ "line 1" CHAR: l ]
@ -56,6 +56,3 @@ unit-test
dup stream-readln
2 rot stream-read
] 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

@ -3,7 +3,7 @@
IN: io.streams.string
USING: io kernel math namespaces sequences sbufs strings
generic splitting io.streams.plain io.streams.lines growable
continuations byte-vectors io.encodings byte-arrays ;
continuations ;
M: growable dispose drop ;
@ -18,13 +18,6 @@ M: growable stream-flush drop ;
<string-writer> swap [ stdio get ] compose with-stream*
>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 )
[
[ 0 [ length max ] reduce ] keep
@ -46,17 +39,20 @@ M: plain-writer make-cell-stream 2drop <string-writer> ;
M: growable stream-read1 dup empty? [ drop f ] [ pop ] if ;
: sbuf-read-until ( sbuf n -- str )
tail-slice >string dup reverse-here ;
: harden-as ( seq growble-exemplar -- newseq )
underlying like ;
: growable-read-until ( growable n -- str )
dupd tail-slice swap harden-as dup reverse-here ;
: find-last-sep swap [ memq? ] curry find-last drop ;
M: growable stream-read-until
[ find-last-sep ] keep over [
[ swap 1+ sbuf-read-until ] 2keep [ nth ] 2keep
[ swap 1+ growable-read-until ] 2keep [ nth ] 2keep
set-length
] [
[ swap drop 0 sbuf-read-until f like f ] keep
[ swap drop 0 growable-read-until f like f ] keep
delete-all
] if ;
@ -65,7 +61,7 @@ M: growable stream-read
2drop f
] [
[ length swap - 0 max ] keep
[ swap sbuf-read-until ] 2keep
[ swap growable-read-until ] 2keep
set-length
] if ;
@ -77,9 +73,3 @@ M: growable stream-read-partial
: with-string-reader ( str quot -- )
>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