More flexible io.streams.limited, works with encoded streams
parent
24e9149a2e
commit
dc7b414f57
|
@ -30,3 +30,11 @@ namespaces tools.test strings kernel ;
|
||||||
[ "abc" CHAR: \n ] [ "\n" "limited" get stream-read-until [ >string ] dip ] unit-test
|
[ "abc" CHAR: \n ] [ "\n" "limited" get stream-read-until [ >string ] dip ] unit-test
|
||||||
|
|
||||||
[ "\n" "limited" get stream-read-until ] [ limit-exceeded? ] must-fail-with
|
[ "\n" "limited" get stream-read-until ] [ limit-exceeded? ] must-fail-with
|
||||||
|
|
||||||
|
[ "he" CHAR: l ] [
|
||||||
|
B{ CHAR: h CHAR: e CHAR: l CHAR: l CHAR: o }
|
||||||
|
ascii <byte-reader> [
|
||||||
|
5 limit-input
|
||||||
|
"l" read-until
|
||||||
|
] with-input-stream
|
||||||
|
] unit-test
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
! 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 destructors accessors sequences
|
USING: kernel math io io.encodings destructors accessors
|
||||||
namespaces ;
|
sequences namespaces ;
|
||||||
IN: io.streams.limited
|
IN: io.streams.limited
|
||||||
|
|
||||||
TUPLE: limited-stream stream count limit ;
|
TUPLE: limited-stream stream count limit ;
|
||||||
|
@ -12,8 +12,13 @@ TUPLE: limited-stream stream count limit ;
|
||||||
swap >>stream
|
swap >>stream
|
||||||
0 >>count ;
|
0 >>count ;
|
||||||
|
|
||||||
: limit-input ( limit -- )
|
GENERIC# limit 1 ( stream limit -- stream' )
|
||||||
input-stream [ swap <limited-stream> ] change ;
|
|
||||||
|
M: decoder limit [ clone ] dip [ limit ] curry change-stream ;
|
||||||
|
|
||||||
|
M: object limit <limited-stream> ;
|
||||||
|
|
||||||
|
: limit-input ( limit -- ) input-stream [ swap limit ] change ;
|
||||||
|
|
||||||
ERROR: limit-exceeded ;
|
ERROR: limit-exceeded ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue