From 6c4043bb258b56e995ea6f5cba1df15ade9ffc87 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Sat, 25 Aug 2012 17:03:51 -0700 Subject: [PATCH] io: Make stream-contents call stream-contents* and do the disposal in the top-level. Fixes #645. --- basis/delegate/protocols/protocols.factor | 2 +- core/io/encodings/encodings.factor | 4 ++-- core/io/io-docs.factor | 2 +- core/io/io.factor | 20 +++++++++----------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/basis/delegate/protocols/protocols.factor b/basis/delegate/protocols/protocols.factor index c496979dc2..eb2434cf0f 100644 --- a/basis/delegate/protocols/protocols.factor +++ b/basis/delegate/protocols/protocols.factor @@ -14,7 +14,7 @@ delete-at clear-assoc new-assoc assoc-like ; PROTOCOL: input-stream-protocol stream-read1 stream-read-unsafe stream-read-partial-unsafe -stream-readln stream-read-until stream-contents ; +stream-readln stream-read-until stream-contents* ; PROTOCOL: output-stream-protocol stream-flush stream-write1 stream-write stream-nl ; diff --git a/core/io/encodings/encodings.factor b/core/io/encodings/encodings.factor index aa5d3af93e..b0b5ff7f7e 100644 --- a/core/io/encodings/encodings.factor +++ b/core/io/encodings/encodings.factor @@ -93,8 +93,8 @@ M: decoder stream-read-unsafe ] [ 2drop 2drop 0 ] if* ] if ; inline -M: decoder stream-contents - (stream-contents-by-element) ; +M: decoder stream-contents* + (stream-contents-by-element) ; inline : line-ends/eof ( stream str -- str ) f like swap cr- ; inline diff --git a/core/io/io-docs.factor b/core/io/io-docs.factor index bfa16e4ed7..38002bed77 100644 --- a/core/io/io-docs.factor +++ b/core/io/io-docs.factor @@ -307,7 +307,7 @@ HELP: each-block HELP: stream-contents { $values { "stream" "an input stream" } { "seq" { $or string byte-array } } } -{ $description "Reads all elements in the given stream until the stream is exhausted. The type of the sequence depends on the stream's element type." } +{ $description "Reads all elements in the given stream until the stream is exhausted. The type of the sequence depends on the stream's element type. The stream is closed after completion." } $io-error ; HELP: contents diff --git a/core/io/io.factor b/core/io/io.factor index 20d3b491b3..2485716165 100644 --- a/core/io/io.factor +++ b/core/io/io.factor @@ -13,7 +13,8 @@ GENERIC: stream-read-unsafe ( n buf stream -- count ) GENERIC: stream-read-until ( seps stream -- seq sep/f ) GENERIC: stream-read-partial-unsafe ( n buf stream -- count ) GENERIC: stream-readln ( stream -- str/f ) -GENERIC: stream-contents ( stream -- seq ) +GENERIC: stream-contents* ( stream -- seq ) +: stream-contents ( stream -- seq ) [ stream-contents* ] with-disposal ; GENERIC: stream-write1 ( elt stream -- ) GENERIC: stream-write ( data stream -- ) @@ -183,16 +184,13 @@ CONSTANT: each-block-size 65536 input-stream get swap each-stream-block ; inline : (stream-contents-by-length) ( stream len -- seq ) - dup rot [ - [ (new-sequence-for-stream) ] - [ [ stream-read-unsafe ] curry keep resize ] bi - ] with-disposal ; inline + dup rot + [ (new-sequence-for-stream) ] + [ [ stream-read-unsafe ] curry keep resize ] bi ; inline : (stream-contents-by-block) ( stream -- seq ) - [ - [ [ ] collector [ each-stream-block ] dip { } like ] - [ stream-exemplar concat-as ] bi - ] with-disposal ; inline + [ [ ] collector [ each-stream-block ] dip { } like ] + [ stream-exemplar concat-as ] bi ; inline : (stream-contents-by-length-or-block) ( stream -- seq ) dup stream-length @@ -238,7 +236,7 @@ M: input-stream stream-read-partial-unsafe stream-read-unsafe ; inline M: input-stream stream-read-until read-until-loop ; inline M: input-stream stream-readln "\n" swap stream-read-until drop ; inline -M: input-stream stream-contents (stream-contents-by-length-or-block) ; inline +M: input-stream stream-contents* (stream-contents-by-length-or-block) ; inline M: input-stream stream-seekable? drop f ; inline M: input-stream stream-length drop f ; inline @@ -253,7 +251,7 @@ M: f stream-read-unsafe 3drop 0 ; inline M: f stream-read-until 2drop f f ; inline M: f stream-read-partial-unsafe 3drop 0 ; inline M: f stream-readln drop f ; inline -M: f stream-contents drop f ; inline +M: f stream-contents* drop f ; inline M: f stream-write1 2drop ; inline M: f stream-write 2drop ; inline -- 2.34.1