From f97389503fc6f9701eec6abc30f799d7086c7192 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 27 Mar 2009 19:26:16 -0500 Subject: [PATCH] Fix stream-read-until on byte-readers --- .../byte-array/byte-array-tests.factor | 22 ++++++++++++++++++- basis/io/streams/byte-array/byte-array.factor | 2 +- core/io/streams/sequence/sequence.factor | 9 ++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/basis/io/streams/byte-array/byte-array-tests.factor b/basis/io/streams/byte-array/byte-array-tests.factor index 77a9126740..44290bfb47 100644 --- a/basis/io/streams/byte-array/byte-array-tests.factor +++ b/basis/io/streams/byte-array/byte-array-tests.factor @@ -1,5 +1,5 @@ USING: tools.test io.streams.byte-array io.encodings.binary -io.encodings.utf8 io kernel arrays strings ; +io.encodings.utf8 io kernel arrays strings namespaces ; [ 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 @@ -7,3 +7,23 @@ io.encodings.utf8 io kernel arrays strings ; [ 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 contents dup >array swap string? ] unit-test + +[ B{ 121 120 } 0 ] [ + B{ 0 121 120 0 0 0 0 0 0 } binary + [ 1 read drop "\0" read-until ] with-byte-reader +] unit-test + +[ 1 1 4 11 f ] [ + B{ 1 2 3 4 5 6 7 8 9 10 11 12 } binary + [ + read1 + 0 seek-absolute input-stream get stream-seek + read1 + 2 seek-relative input-stream get stream-seek + read1 + -2 seek-end input-stream get stream-seek + read1 + 0 seek-end input-stream get stream-seek + read1 + ] with-byte-reader +] unit-test \ No newline at end of file diff --git a/basis/io/streams/byte-array/byte-array.factor b/basis/io/streams/byte-array/byte-array.factor index 25d879a534..2ffb9b9a63 100644 --- a/basis/io/streams/byte-array/byte-array.factor +++ b/basis/io/streams/byte-array/byte-array.factor @@ -28,7 +28,7 @@ M: byte-reader stream-seek ( n seek-type stream -- ) swap { { seek-absolute [ (>>i) ] } { seek-relative [ [ + ] change-i drop ] } - { seek-end [ dup underlying>> length >>i [ + ] change-i drop ] } + { seek-end [ [ underlying>> length + ] keep (>>i) ] } [ bad-seek-type ] } case ; diff --git a/core/io/streams/sequence/sequence.factor b/core/io/streams/sequence/sequence.factor index f455512ed3..0f922a37cc 100644 --- a/core/io/streams/sequence/sequence.factor +++ b/core/io/streams/sequence/sequence.factor @@ -15,11 +15,10 @@ SLOT: i [ 1+ ] change-i drop ; inline : sequence-read1 ( stream -- elt/f ) - [ >sequence-stream< ?nth ] - [ next ] bi ; inline + [ >sequence-stream< ?nth ] [ next ] bi ; inline : add-length ( n stream -- i+n ) - [ i>> + ] [ underlying>> length ] bi min ; inline + [ i>> + ] [ underlying>> length ] bi min ; inline : (sequence-read) ( n stream -- seq/f ) [ add-length ] keep @@ -32,8 +31,8 @@ SLOT: i [ (sequence-read) ] [ 2drop f ] if ; inline : find-sep ( seps stream -- sep/f n ) - swap [ >sequence-stream< ] dip - [ memq? ] curry find-from swap ; inline + swap [ >sequence-stream< swap tail-slice ] dip + [ memq? ] curry find swap ; inline : sequence-read-until ( separators stream -- seq sep/f ) [ find-sep ] keep