io: stream-read-into and stream-read-partial-into
Safe user-facing wrappers for stream-read-unsafe.db4
parent
863ab575e3
commit
4d224b552f
|
@ -25,6 +25,16 @@ M: up-to-13-reader stream-read1
|
|||
{ B{ 0 1 2 3 4 5 6 7 8 9 10 11 12 } f }
|
||||
[ up-to-13-reader new [ 20 swap stream-read ] [ 20 swap stream-read ] bi ] unit-test
|
||||
|
||||
{
|
||||
T{ slice f 0 8 B{ 0 1 2 3 4 5 6 7 } }
|
||||
T{ slice f 0 5 B{ 8 9 10 11 12 205 206 207 } }
|
||||
f
|
||||
} [
|
||||
up-to-13-reader new
|
||||
[ B{ 200 201 202 203 204 205 206 207 } swap stream-read-into ]
|
||||
[ B{ 200 201 202 203 204 205 206 207 } swap stream-read-into ]
|
||||
[ B{ 200 201 202 203 204 205 206 207 } swap stream-read-into ] tri
|
||||
] unit-test
|
||||
|
||||
{
|
||||
B{ 0 1 2 3 4 5 6 7 8 } 9
|
||||
|
|
|
@ -98,10 +98,14 @@ SYMBOL: error-stream
|
|||
: (new-sequence-for-stream) ( n stream -- seq )
|
||||
stream-exemplar new-sequence ; inline
|
||||
|
||||
: (read-into-new) ( n stream quot -- byte-array/f )
|
||||
: (read-into-new) ( n stream quot: ( n buf stream -- count ) -- seq/f )
|
||||
[ 2dup (new-sequence-for-stream) swap ] dip curry keep
|
||||
over 0 = [ 2drop f ] [ resize ] if ; inline
|
||||
|
||||
: (read-into) ( buf stream quot: ( n buf stream -- count ) -- buf-slice/f )
|
||||
[ dup length over ] 2dip call
|
||||
[ drop f ] [ head-slice ] if-zero ; inline
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: stream-read ( n stream -- seq/f )
|
||||
|
@ -110,8 +114,20 @@ PRIVATE>
|
|||
: stream-read-partial ( n stream -- seq/f )
|
||||
[ stream-read-partial-unsafe ] (read-into-new) ; inline
|
||||
|
||||
ERROR: invalid-read-buffer buf stream ;
|
||||
|
||||
|
||||
: stream-read-into ( buf stream -- buf-slice/f )
|
||||
[ stream-read-unsafe ] (read-into) ; inline
|
||||
: stream-read-partial-into ( buf stream -- buf-slice/f )
|
||||
[ stream-read-partial-unsafe ] (read-into) ; inline
|
||||
|
||||
: read ( n -- seq ) input-stream get stream-read ; inline
|
||||
: read-partial ( n -- seq ) input-stream get stream-read-partial ; inline
|
||||
: read-into ( buf -- buf-slice )
|
||||
input-stream get stream-read-into ; inline
|
||||
: read-partial-into ( buf -- buf-slice )
|
||||
input-stream get stream-read-partial-into ; inline
|
||||
|
||||
: each-stream-line ( ... stream quot: ( ... line -- ... ) -- ... )
|
||||
swap [ stream-readln ] curry each-morsel ; inline
|
||||
|
|
|
@ -4,7 +4,7 @@ USING: accessors byte-arrays combinators
|
|||
combinators.short-circuit destructors io io.encodings.ascii
|
||||
io.encodings.binary io.private io.streams.byte-array
|
||||
io.streams.peek io.streams.string kernel locals make math
|
||||
sequences tools.test vectors ;
|
||||
sequences tools.test vectors io.streams.memory ;
|
||||
IN: io.streams.peek.tests
|
||||
|
||||
[ CHAR: a ]
|
||||
|
|
Loading…
Reference in New Issue