more stream-seekable? and stream-length methods
- limited-streams and throw-on-eof-streams - string-readers and byte-readersdb4
parent
1b0b765e82
commit
e426512534
|
@ -122,3 +122,7 @@ IN: io.streams.limited.tests
|
||||||
[ contents ] with-unlimited-input
|
[ contents ] with-unlimited-input
|
||||||
] with-input-stream
|
] with-input-stream
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
|
{ 4 } [ B{ 0 1 2 3 4 5 } binary <byte-reader> 4 <limited-stream> stream-length ] unit-test
|
||||||
|
{ 6 } [ B{ 0 1 2 3 4 5 } binary <byte-reader> 8 <limited-stream> stream-length ] unit-test
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: accessors byte-vectors combinators destructors fry io
|
USING: accessors byte-vectors combinators destructors fry io
|
||||||
io.encodings io.files io.files.info kernel locals math
|
io.encodings io.files io.files.info kernel locals math
|
||||||
namespaces sequences ;
|
namespaces sequences math.order ;
|
||||||
IN: io.streams.limited
|
IN: io.streams.limited
|
||||||
|
|
||||||
TUPLE: limited-stream stream count limit current start stop ;
|
TUPLE: limited-stream stream count limit current start stop ;
|
||||||
|
@ -22,7 +22,8 @@ TUPLE: limited-stream stream count limit current start stop ;
|
||||||
GENERIC# limit-stream 1 ( stream limit -- stream' )
|
GENERIC# limit-stream 1 ( stream limit -- stream' )
|
||||||
|
|
||||||
M: decoder limit-stream ( stream limit -- stream' )
|
M: decoder limit-stream ( stream limit -- stream' )
|
||||||
[ clone ] dip '[ _ limit-stream ] change-stream ;
|
'[ stream>> _ limit-stream ] [ code>> ] [ cr>> ] tri
|
||||||
|
decoder boa ; inline
|
||||||
|
|
||||||
M: object limit-stream ( stream limit -- stream' )
|
M: object limit-stream ( stream limit -- stream' )
|
||||||
<limited-stream> ;
|
<limited-stream> ;
|
||||||
|
@ -78,7 +79,8 @@ ERROR: limit-exceeded n stream ;
|
||||||
pick 0 <= [ 3drop f ] [ [ stream>> ] dip call ] if ; inline
|
pick 0 <= [ 3drop f ] [ [ stream>> ] dip call ] if ; inline
|
||||||
|
|
||||||
:: maybe-read-unsafe ( n buf limited-stream quot: ( n buf stream -- count ) -- count )
|
:: maybe-read-unsafe ( n buf limited-stream quot: ( n buf stream -- count ) -- count )
|
||||||
n limited-stream [| n' stream | n' buf stream quot call ] maybe-read ; inline
|
n limited-stream adjust-limited-read :> ( n' lstream' )
|
||||||
|
n' 0 <= [ 0 ] [ n' buf lstream' stream>> quot call ] if ; inline
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
|
@ -130,6 +132,13 @@ M: limited-stream stream-seek
|
||||||
[ stream>> stream-seek ]
|
[ stream>> stream-seek ]
|
||||||
[ limited-stream-seek ] 3bi ;
|
[ limited-stream-seek ] 3bi ;
|
||||||
|
|
||||||
|
M: limited-stream stream-seekable?
|
||||||
|
stream>> stream-seekable? ; inline
|
||||||
|
|
||||||
|
M: limited-stream stream-length
|
||||||
|
dup stream>> stream-length
|
||||||
|
[ swap limit>> min ] [ drop f ] if* ; inline
|
||||||
|
|
||||||
M: limited-stream dispose stream>> dispose ;
|
M: limited-stream dispose stream>> dispose ;
|
||||||
|
|
||||||
M: limited-stream stream-element-type
|
M: limited-stream stream-element-type
|
||||||
|
@ -138,7 +147,7 @@ M: limited-stream stream-element-type
|
||||||
GENERIC: unlimit-stream ( stream -- stream' )
|
GENERIC: unlimit-stream ( stream -- stream' )
|
||||||
|
|
||||||
M: decoder unlimit-stream ( stream -- stream' )
|
M: decoder unlimit-stream ( stream -- stream' )
|
||||||
[ stream>> ] change-stream ;
|
[ stream>> stream>> ] [ code>> ] [ cr>> ] tri decoder boa ;
|
||||||
|
|
||||||
M: limited-stream unlimit-stream ( stream -- stream' ) stream>> ;
|
M: limited-stream unlimit-stream ( stream -- stream' ) stream>> ;
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,8 @@ M: string-reader stream-read1 sequence-read1 ;
|
||||||
M: string-reader stream-read-until sequence-read-until ;
|
M: string-reader stream-read-until sequence-read-until ;
|
||||||
M: string-reader stream-tell i>> ;
|
M: string-reader stream-tell i>> ;
|
||||||
M: string-reader stream-seek (stream-seek) ;
|
M: string-reader stream-seek (stream-seek) ;
|
||||||
|
M: string-reader stream-seekable? drop t ; inline
|
||||||
|
M: string-reader stream-length underlying>> length ;
|
||||||
M: string-reader dispose drop ;
|
M: string-reader dispose drop ;
|
||||||
|
|
||||||
<PRIVATE
|
<PRIVATE
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
! Copyright (C) 2010 Doug Coleman.
|
! Copyright (C) 2010 Doug Coleman.
|
||||||
! See http://factorcode.org/license.txt for BSD license.
|
! See http://factorcode.org/license.txt for BSD license.
|
||||||
USING: io io.encodings.utf8 io.files io.streams.string
|
USING: destructors io io.encodings.binary io.encodings.utf8
|
||||||
io.streams.throwing kernel tools.test destructors ;
|
io.files io.streams.byte-array io.streams.string
|
||||||
|
io.streams.throwing kernel namespaces tools.test ;
|
||||||
IN: io.streams.throwing.tests
|
IN: io.streams.throwing.tests
|
||||||
|
|
||||||
[ "asdf" ]
|
[ "asdf" ]
|
||||||
|
@ -39,14 +40,16 @@ IN: io.streams.throwing.tests
|
||||||
] with-file-reader
|
] with-file-reader
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[ "asdf" "asdf" ] [
|
[ B{ 0 1 2 3 } B{ 0 1 2 3 } ] [
|
||||||
"asdf" [
|
B{ 0 1 2 3 } binary [
|
||||||
[ 4 read 0 seek-absolute seek-input 4 read ] throw-on-eof
|
[ 4 read 0 seek-absolute seek-input 4 read ] throw-on-eof
|
||||||
] with-string-reader
|
] with-byte-reader
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
||||||
[
|
[
|
||||||
"asdf" [ [ 1 seek-absolute seek-input 4 read drop ] throw-on-eof ] with-string-reader
|
B{ 0 1 2 3 } binary [
|
||||||
|
[ 1 seek-absolute seek-input 4 read drop ] throw-on-eof
|
||||||
|
] with-byte-reader
|
||||||
] [ stream-exhausted? ] must-fail-with
|
] [ stream-exhausted? ] must-fail-with
|
||||||
|
|
||||||
[ "asd" CHAR: f ] [
|
[ "asd" CHAR: f ] [
|
||||||
|
@ -57,6 +60,14 @@ IN: io.streams.throwing.tests
|
||||||
"asdf" [ [ "g" read-until ] throw-on-eof ] with-string-reader
|
"asdf" [ [ "g" read-until ] throw-on-eof ] with-string-reader
|
||||||
] [ stream-exhausted? ] must-fail-with
|
] [ stream-exhausted? ] must-fail-with
|
||||||
|
|
||||||
[ 1 ] [
|
{ 1 } [
|
||||||
"asdf" [ [ 1 seek-absolute seek-input tell-input ] throw-on-eof ] with-string-reader
|
B{ 0 1 2 3 } binary [
|
||||||
|
[ 1 seek-absolute seek-input tell-input ] throw-on-eof
|
||||||
|
] with-byte-reader
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
{ t 4 } [
|
||||||
|
B{ 0 1 2 3 } binary [ [
|
||||||
|
input-stream get [ stream-seekable? ] [ stream-length ] bi
|
||||||
|
] throw-on-eof ] with-byte-reader
|
||||||
] unit-test
|
] unit-test
|
||||||
|
|
|
@ -10,8 +10,6 @@ TUPLE: throws-on-eof-stream stream ;
|
||||||
|
|
||||||
C: <throws-on-eof-stream> throws-on-eof-stream
|
C: <throws-on-eof-stream> throws-on-eof-stream
|
||||||
|
|
||||||
<PRIVATE
|
|
||||||
|
|
||||||
M: throws-on-eof-stream stream-element-type stream>> stream-element-type ;
|
M: throws-on-eof-stream stream-element-type stream>> stream-element-type ;
|
||||||
|
|
||||||
M: throws-on-eof-stream dispose stream>> dispose ;
|
M: throws-on-eof-stream dispose stream>> dispose ;
|
||||||
|
@ -34,12 +32,16 @@ M: throws-on-eof-stream stream-tell
|
||||||
M: throws-on-eof-stream stream-seek
|
M: throws-on-eof-stream stream-seek
|
||||||
stream>> stream-seek ;
|
stream>> stream-seek ;
|
||||||
|
|
||||||
|
M: throws-on-eof-stream stream-seekable?
|
||||||
|
stream>> stream-seekable? ;
|
||||||
|
|
||||||
|
M: throws-on-eof-stream stream-length
|
||||||
|
stream>> stream-length ;
|
||||||
|
|
||||||
M: throws-on-eof-stream stream-read-until
|
M: throws-on-eof-stream stream-read-until
|
||||||
[ stream>> stream-read-until ]
|
[ stream>> stream-read-until ]
|
||||||
[ '[ length _ \ read-until stream-exhausted ] unless* ] bi ;
|
[ '[ length _ \ read-until stream-exhausted ] unless* ] bi ;
|
||||||
|
|
||||||
PRIVATE>
|
|
||||||
|
|
||||||
: stream-throw-on-eof ( ..a stream quot: ( ..a stream' -- ..b ) -- ..b )
|
: stream-throw-on-eof ( ..a stream quot: ( ..a stream' -- ..b ) -- ..b )
|
||||||
[ <throws-on-eof-stream> ] dip with-input-stream* ; inline
|
[ <throws-on-eof-stream> ] dip with-input-stream* ; inline
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,8 @@ M: byte-reader dispose drop ;
|
||||||
|
|
||||||
M: byte-reader stream-tell i>> ;
|
M: byte-reader stream-tell i>> ;
|
||||||
M: byte-reader stream-seek (stream-seek) ;
|
M: byte-reader stream-seek (stream-seek) ;
|
||||||
|
M: byte-reader stream-seekable? drop t ; inline
|
||||||
|
M: byte-reader stream-length underlying>> length ; inline
|
||||||
|
|
||||||
: <byte-reader> ( byte-array encoding -- stream )
|
: <byte-reader> ( byte-array encoding -- stream )
|
||||||
[ B{ } like 0 byte-reader boa ] dip <decoder> ;
|
[ B{ } like 0 byte-reader boa ] dip <decoder> ;
|
||||||
|
|
Loading…
Reference in New Issue