From 7ae14a746f6cd6677da23133f33b52b5a66d302e Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg <ehrenbed@carleton.edu> Date: Sat, 16 Feb 2008 13:58:38 -0600 Subject: [PATCH] Refactoring byte-array and string streams --- .../byte-array/byte-array-tests.factor | 9 ++++++ core/io/streams/byte-array/byte-array.factor | 16 +++++++++++ core/io/streams/string/string-tests.factor | 5 +--- core/io/streams/string/string.factor | 28 ++++++------------- 4 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 core/io/streams/byte-array/byte-array-tests.factor create mode 100644 core/io/streams/byte-array/byte-array.factor diff --git a/core/io/streams/byte-array/byte-array-tests.factor b/core/io/streams/byte-array/byte-array-tests.factor new file mode 100644 index 0000000000..77a9126740 --- /dev/null +++ b/core/io/streams/byte-array/byte-array-tests.factor @@ -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 diff --git a/core/io/streams/byte-array/byte-array.factor b/core/io/streams/byte-array/byte-array.factor new file mode 100644 index 0000000000..eb224650f3 --- /dev/null +++ b/core/io/streams/byte-array/byte-array.factor @@ -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 diff --git a/core/io/streams/string/string-tests.factor b/core/io/streams/string/string-tests.factor index ad46233501..4bd31fe7d8 100644 --- a/core/io/streams/string/string-tests.factor +++ b/core/io/streams/string/string-tests.factor @@ -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 diff --git a/core/io/streams/string/string.factor b/core/io/streams/string/string.factor index 42726e7762..a45c616b9a 100755 --- a/core/io/streams/string/string.factor +++ b/core/io/streams/string/string.factor @@ -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