PageRenderTime 263ms CodeModel.GetById 140ms app.highlight 5ms RepoModel.GetById 116ms app.codeStats 0ms

/stream-helpers/stream-helpers.factor

http://github.com/phred/arc4
Unknown | 24 lines | 17 code | 7 blank | 0 comment | 0 complexity | bc55070d4379349367c19437718f2ce1 MD5 | raw file
 1! Copyright (C) 2011 Fred Alger
 2! See http://factorcode.org/license.txt for BSD license.
 3
 4USING: kernel arrays io locals byte-arrays accessors math sequences ;
 5
 6IN: arc4.stream-helpers
 7MIXIN: input-stream
 8
 9M:: input-stream stream-read ( n stream -- seq )
10    BV{ } clone :> bytes n iota [ drop stream
11    stream-read1 dup [ bytes push ] when* not ] find 2drop bytes >byte-array ;
12
13: separator-or-eof? ( val seps -- ? )
14    [ member? ] curry [ f = ] bi or ;
15
16M:: input-stream stream-read-until ( seps stream -- seq sep/f )
17    BV{ } clone :> bytes f :> sep!
18    [ stream stream-read1
19    [ dup ] [ sep! seps separator-or-eof? ] bi ]
20    [ bytes push ] until drop bytes >byte-array sep ;
21
22M: input-stream stream-read-partial ( n stream -- seq )
23    stream-read ;
24