From 989b7a468aa566e40be0e74f7f1aed9ffc6aca1b Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Fri, 15 Feb 2008 21:49:53 -0600 Subject: [PATCH] byte array streams --- core/io/streams/string/string-tests.factor | 5 ++- core/io/streams/string/string.factor | 37 +++++++++++++++------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/core/io/streams/string/string-tests.factor b/core/io/streams/string/string-tests.factor index 9c8202afaf..277d9ebf9c 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 ; +USING: io.streams.string io kernel arrays namespaces tools.test io.encodings.latin1 ; IN: temporary [ "line 1" CHAR: l ] @@ -56,3 +56,6 @@ 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 3d5a55739b..f39855bb58 100755 --- a/core/io/streams/string/string.factor +++ b/core/io/streams/string/string.factor @@ -2,21 +2,28 @@ ! See http://factorcode.org/license.txt for BSD license. IN: io.streams.string USING: io kernel math namespaces sequences sbufs strings -generic splitting io.streams.plain io.streams.lines -continuations ; +generic splitting io.streams.plain io.streams.lines growable +continuations byte-vectors io.encodings byte-arrays ; -M: sbuf dispose drop ; +M: growable dispose drop ; -M: sbuf stream-write1 push ; -M: sbuf stream-write push-all ; -M: sbuf stream-flush drop ; +M: growable stream-write1 push ; +M: growable stream-write push-all ; +M: growable stream-flush drop ; : ( -- stream ) 512 ; : string-out ( quot -- str ) - [ call stdio get >string ] with-stream* ; - inline + swap [ stdio get ] compose with-stream* + >string ; inline + +: ( encoding -- stream ) + 512 swap ; + +: with-byte-writer ( encoding quot -- byte-array ) + >r r> [ stdio get ] compose with-stream* + >byte-array ; inline : format-column ( seq ? -- seq ) [ @@ -37,14 +44,14 @@ M: plain-writer stream-write-table M: plain-writer make-cell-stream 2drop ; -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 ) tail-slice >string dup reverse-here ; : find-last-sep swap [ memq? ] curry find-last drop ; -M: sbuf stream-read-until +M: growable stream-read-until [ find-last-sep ] keep over [ [ swap 1+ sbuf-read-until ] 2keep [ nth ] 2keep set-length @@ -53,7 +60,7 @@ M: sbuf stream-read-until delete-all ] if ; -M: sbuf stream-read +M: growable stream-read dup empty? [ 2drop f ] [ @@ -62,7 +69,7 @@ M: sbuf stream-read set-length ] if ; -M: sbuf stream-read-partial +M: growable stream-read-partial stream-read ; : ( str -- stream ) @@ -70,3 +77,9 @@ M: sbuf stream-read-partial : string-in ( str quot -- ) >r r> with-stream ; inline + +: ( byte-array encoding -- stream ) + >r >byte-vector dup reverse-here r> ; + +: with-byte-reader ( byte-array encoding quot -- ) + >r r> with-stream ; inline