add an unlimit word, refactor limited-streams, better docs
parent
57ecdbe2c3
commit
9a06ce94a5
|
@ -5,16 +5,23 @@ IN: io.streams.limited
|
||||||
|
|
||||||
HELP: <limited-stream>
|
HELP: <limited-stream>
|
||||||
{ $values
|
{ $values
|
||||||
{ "stream" "an input stream" } { "limit" integer }
|
{ "stream" "an input stream" } { "limit" integer } { "mode" "a " { $link limited-stream } " mode singleton" }
|
||||||
{ "stream'" "an input stream" }
|
{ "stream'" "an input stream" }
|
||||||
}
|
}
|
||||||
{ $description "Constructs a new " { $link limited-stream } " from an existing stream. Upon exhaustion, the stream will throw an error by default." }
|
{ $description "Constructs a new " { $link limited-stream } " from an existing stream. User code should use " { $link limit } " or " { $link limit-input } "." } ;
|
||||||
|
|
||||||
|
HELP: limit
|
||||||
|
{ $values
|
||||||
|
{ "stream" "an input stream" } { "limit" integer } { "mode" "a " { $link limited-stream } " mode singleton" }
|
||||||
|
{ "stream'" "a stream" }
|
||||||
|
}
|
||||||
|
{ $description "Changes a decoder's stream to be a limited stream, or wraps " { $snippet "stream" } " in a " { $link limited-stream } "." }
|
||||||
{ $examples "Throwing an exception:"
|
{ $examples "Throwing an exception:"
|
||||||
{ $example
|
{ $example
|
||||||
"USING: continuations io io.streams.limited io.streams.string"
|
"USING: continuations io io.streams.limited io.streams.string"
|
||||||
"kernel prettyprint ;"
|
"kernel prettyprint ;"
|
||||||
"["
|
"["
|
||||||
" \"123456\" <string-reader> 3 <limited-stream>"
|
" \"123456\" <string-reader> 3 stream-throws limit"
|
||||||
" 100 swap stream-read ."
|
" 100 swap stream-read ."
|
||||||
"] [ ] recover ."
|
"] [ ] recover ."
|
||||||
"T{ limit-exceeded }"
|
"T{ limit-exceeded }"
|
||||||
|
@ -23,32 +30,34 @@ HELP: <limited-stream>
|
||||||
{ $example
|
{ $example
|
||||||
"USING: accessors continuations io io.streams.limited"
|
"USING: accessors continuations io io.streams.limited"
|
||||||
"io.streams.string kernel prettyprint ;"
|
"io.streams.string kernel prettyprint ;"
|
||||||
"\"123456\" <string-reader> 3 <limited-stream>"
|
"\"123456\" <string-reader> 3 stream-eofs limit"
|
||||||
"stream-eofs >>mode"
|
|
||||||
"100 swap stream-read ."
|
"100 swap stream-read ."
|
||||||
"\"123\""
|
"\"123\""
|
||||||
}
|
}
|
||||||
} ;
|
} ;
|
||||||
|
|
||||||
HELP: limit
|
HELP: unlimit
|
||||||
{ $values
|
{ $values
|
||||||
{ "stream" "a stream" } { "limit" integer }
|
{ "stream" "an input stream" }
|
||||||
{ "stream'" "a stream" }
|
{ "stream'" "a stream" }
|
||||||
}
|
}
|
||||||
{ $description "Changes a decoder's stream to be a limited stream, or wraps " { $snippet "stream" } " in a " { $link limited-stream } "." } ;
|
{ $description "Returns the underlying stream of a limited stream." } ;
|
||||||
|
|
||||||
HELP: limited-stream
|
HELP: limited-stream
|
||||||
{ $values
|
{ $values
|
||||||
{ "value" "a limited-stream class" }
|
{ "value" "a limited-stream class" }
|
||||||
}
|
}
|
||||||
{ $description "Limited streams wrap other streams, changing their behavior to throw an exception or return " { $link f } " upon exhaustion. The default behavior is to throw an exception." } ;
|
{ $description "Limited streams wrap other streams, changing their behavior to throw an exception or return " { $link f } " upon exhaustion." } ;
|
||||||
|
|
||||||
HELP: limit-input
|
HELP: limit-input
|
||||||
{ $values
|
{ $values
|
||||||
{ "limit" integer }
|
{ "limit" integer } { "mode" "a " { $link limited-stream } " mode singleton" }
|
||||||
}
|
}
|
||||||
{ $description "Wraps the current " { $link input-stream } " in a " { $link limited-stream } "." } ;
|
{ $description "Wraps the current " { $link input-stream } " in a " { $link limited-stream } "." } ;
|
||||||
|
|
||||||
|
HELP: unlimit-input
|
||||||
|
{ $description "Returns the underlying stream of the limited-stream stored in " { $link input-stream } "." } ;
|
||||||
|
|
||||||
HELP: stream-eofs
|
HELP: stream-eofs
|
||||||
{ $values
|
{ $values
|
||||||
{ "value" "a " { $link limited-stream } " mode singleton" }
|
{ "value" "a " { $link limited-stream } " mode singleton" }
|
||||||
|
@ -64,13 +73,15 @@ HELP: stream-throws
|
||||||
{ stream-eofs stream-throws } related-words
|
{ stream-eofs stream-throws } related-words
|
||||||
|
|
||||||
ARTICLE: "io.streams.limited" "Limited input streams"
|
ARTICLE: "io.streams.limited" "Limited input streams"
|
||||||
"The " { $vocab-link "io.streams.limited" } " vocabulary wraps a stream to behave as if it had only a limited number of bytes, either throwing an error or returning " { $link f } " upon reaching the end. The default behavior is to throw an error." $nl
|
"The " { $vocab-link "io.streams.limited" } " vocabulary wraps a stream to behave as if it had only a limited number of bytes, either throwing an error or returning " { $link f } " upon reaching the end." $nl
|
||||||
"Wrap an existing stream in a limited stream:"
|
|
||||||
{ $subsection <limited-stream> }
|
|
||||||
"Wrap a stream in a limited stream:"
|
"Wrap a stream in a limited stream:"
|
||||||
{ $subsection limit }
|
{ $subsection limit }
|
||||||
"Wrap the current " { $link input-stream } " in a limited stream:"
|
"Wrap the current " { $link input-stream } " in a limited stream:"
|
||||||
{ $subsection limit-input }
|
{ $subsection limit-input }
|
||||||
|
"Unlimits a limited stream:"
|
||||||
|
{ $subsection unlimit }
|
||||||
|
"Unlimits the current " { $link input-stream } ":"
|
||||||
|
{ $subsection limit-input }
|
||||||
"Make a limited stream throw an exception on exhaustion:"
|
"Make a limited stream throw an exception on exhaustion:"
|
||||||
{ $subsection stream-throws }
|
{ $subsection stream-throws }
|
||||||
"Make a limited stream return " { $link f } " on exhaustion:"
|
"Make a limited stream return " { $link f } " on exhaustion:"
|
||||||
|
|
|
@ -8,7 +8,7 @@ IN: io.streams.limited.tests
|
||||||
ascii encode binary <byte-reader> "data" set
|
ascii encode binary <byte-reader> "data" set
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ ] [ "data" get 24 <limited-stream> "limited" set ] unit-test
|
[ ] [ "data" get 24 stream-throws <limited-stream> "limited" set ] unit-test
|
||||||
|
|
||||||
[ CHAR: h ] [ "limited" get stream-read1 ] unit-test
|
[ CHAR: h ] [ "limited" get stream-read1 ] unit-test
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ IN: io.streams.limited.tests
|
||||||
ascii encode binary <byte-reader> "data" set
|
ascii encode binary <byte-reader> "data" set
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ ] [ "data" get 7 <limited-stream> "limited" set ] unit-test
|
[ ] [ "data" get 7 stream-throws <limited-stream> "limited" set ] unit-test
|
||||||
|
|
||||||
[ "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
|
||||||
|
|
||||||
|
@ -34,22 +34,28 @@ IN: io.streams.limited.tests
|
||||||
[ "he" CHAR: l ] [
|
[ "he" CHAR: l ] [
|
||||||
B{ CHAR: h CHAR: e CHAR: l CHAR: l CHAR: o }
|
B{ CHAR: h CHAR: e CHAR: l CHAR: l CHAR: o }
|
||||||
ascii <byte-reader> [
|
ascii <byte-reader> [
|
||||||
5 limit-input
|
5 stream-throws limit-input
|
||||||
"l" read-until
|
"l" read-until
|
||||||
] with-input-stream
|
] with-input-stream
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ CHAR: a ]
|
[ CHAR: a ]
|
||||||
[ "a" <string-reader> 1 <limited-stream> stream-read1 ] unit-test
|
[ "a" <string-reader> 1 stream-eofs <limited-stream> stream-read1 ] unit-test
|
||||||
|
|
||||||
[ "abc" ]
|
[ "abc" ]
|
||||||
[
|
[
|
||||||
"abc" <string-reader> 3 <limited-stream> stream-eofs >>mode
|
"abc" <string-reader> 3 stream-eofs <limited-stream>
|
||||||
4 swap stream-read
|
4 swap stream-read
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ f ]
|
[ f ]
|
||||||
[
|
[
|
||||||
"abc" <string-reader> 3 <limited-stream> stream-eofs >>mode
|
"abc" <string-reader> 3 stream-eofs <limited-stream>
|
||||||
4 over stream-read drop 10 swap stream-read
|
4 over stream-read drop 10 swap stream-read
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
[ t ]
|
||||||
|
[
|
||||||
|
"abc" <string-reader> 3 stream-eofs limit unlimit
|
||||||
|
"abc" <string-reader> =
|
||||||
|
] unit-test
|
||||||
|
|
|
@ -9,20 +9,27 @@ TUPLE: limited-stream stream count limit mode ;
|
||||||
|
|
||||||
SINGLETONS: stream-throws stream-eofs ;
|
SINGLETONS: stream-throws stream-eofs ;
|
||||||
|
|
||||||
: <limited-stream> ( stream limit -- stream' )
|
: <limited-stream> ( stream limit mode -- stream' )
|
||||||
limited-stream new
|
limited-stream new
|
||||||
|
swap >>mode
|
||||||
swap >>limit
|
swap >>limit
|
||||||
swap >>stream
|
swap >>stream
|
||||||
0 >>count
|
0 >>count ;
|
||||||
stream-throws >>mode ;
|
|
||||||
|
|
||||||
GENERIC# limit 1 ( stream limit -- stream' )
|
GENERIC# limit 2 ( stream limit mode -- stream' )
|
||||||
|
|
||||||
M: decoder limit [ clone ] dip [ limit ] curry change-stream ;
|
M: decoder limit ( stream limit mode -- stream' )
|
||||||
|
[ clone ] 2dip '[ _ _ limit ] change-stream ;
|
||||||
|
|
||||||
M: object limit <limited-stream> ;
|
M: object limit ( stream limit mode -- stream' )
|
||||||
|
<limited-stream> ;
|
||||||
|
|
||||||
: limit-input ( limit -- ) input-stream [ swap limit ] change ;
|
: unlimit ( stream -- stream' )
|
||||||
|
[ stream>> ] change-stream ;
|
||||||
|
|
||||||
|
: limit-input ( limit mode -- ) input-stream [ -rot limit ] change ;
|
||||||
|
|
||||||
|
: unlimit-input ( -- ) input-stream [ unlimit ] change ;
|
||||||
|
|
||||||
ERROR: limit-exceeded ;
|
ERROR: limit-exceeded ;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue