replace boolean with singletons for io.streams.limited -- stream-throws or stream-eofs

db4
U-C4\Administrator 2009-01-20 12:02:54 -06:00
parent 7ba4035993
commit 8676ebafea
2 changed files with 18 additions and 11 deletions

View File

@ -44,12 +44,12 @@ IN: io.streams.limited.tests
[ "abc" ] [ "abc" ]
[ [
"abc" <string-reader> 3 <limited-stream> t >>no-throw? "abc" <string-reader> 3 <limited-stream> stream-eofs >>mode
4 swap stream-read 4 swap stream-read
] unit-test ] unit-test
[ f ] [ f ]
[ [
"abc" <string-reader> 3 <limited-stream> t >>no-throw? "abc" <string-reader> 3 <limited-stream> stream-eofs >>mode
4 over stream-read drop 10 swap stream-read 4 over stream-read drop 10 swap stream-read
] unit-test ] unit-test

View File

@ -1,16 +1,19 @@
! Copyright (C) 2008 Slava Pestov ! Copyright (C) 2008 Slava Pestov
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel math io io.encodings destructors accessors USING: kernel math io io.encodings destructors accessors
sequences namespaces byte-vectors fry ; sequences namespaces byte-vectors fry combinators ;
IN: io.streams.limited IN: io.streams.limited
TUPLE: limited-stream stream count limit no-throw? ; TUPLE: limited-stream stream count limit mode ;
SINGLETONS: stream-throws stream-eofs ;
: <limited-stream> ( stream limit -- stream' ) : <limited-stream> ( stream limit -- stream' )
limited-stream new limited-stream new
swap >>limit swap >>limit
swap >>stream swap >>stream
0 >>count ; 0 >>count
stream-throws >>mode ;
GENERIC# limit 1 ( stream limit -- stream' ) GENERIC# limit 1 ( stream limit -- stream' )
@ -22,16 +25,20 @@ M: object limit <limited-stream> ;
ERROR: limit-exceeded ; ERROR: limit-exceeded ;
ERROR: bad-stream-mode mode ;
: adjust-limit ( n stream -- n' stream ) : adjust-limit ( n stream -- n' stream )
2dup [ + ] change-count 2dup [ + ] change-count
[ count>> ] [ limit>> ] bi > [ count>> ] [ limit>> ] bi >
[ [
dup no-throw?>> [ dup mode>> {
dup [ count>> ] [ limit>> ] bi - { stream-throws [ limit-exceeded ] }
'[ _ - ] dip { stream-eofs [
] [ dup [ count>> ] [ limit>> ] bi -
limit-exceeded '[ _ - ] dip
] if ] }
[ bad-stream-mode ]
} case
] when ; inline ] when ; inline
: maybe-read ( n limited-stream quot: ( n stream -- seq/f ) -- seq/f ) : maybe-read ( n limited-stream quot: ( n stream -- seq/f ) -- seq/f )