]> gitweb.factorcode.org Git - factor.git/commitdiff
io: stream-read-into and stream-read-partial-into
authorJoe Groff <arcata@gmail.com>
Sat, 15 Oct 2011 02:58:29 +0000 (19:58 -0700)
committerJoe Groff <arcata@gmail.com>
Tue, 18 Oct 2011 04:23:08 +0000 (21:23 -0700)
Safe user-facing wrappers for stream-read-unsafe.

core/io/io-tests.factor
core/io/io.factor
extra/io/streams/peek/peek-tests.factor

index 970eef0e34ac5d06b242cfbad1dbda3de02197bb..511e7eebf48b673a8d565130d22012e76b45dcb9 100644 (file)
@@ -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
index c4ea0031731911f53d67eb29368954b20b8c0b86..8867ee3866650c87bebfbe00a1ea364a67ed3a52 100644 (file)
@@ -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
index 7aa8d3d97729a54eb3135e2cd512934a75e5917f..f35714d75a9cb79253e0ac9395f7bee562a0d067 100644 (file)
@@ -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 ]