PageRenderTime 38ms CodeModel.GetById 24ms RepoModel.GetById 0ms 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. USING: kernel arrays io locals byte-arrays accessors math sequences ;
  4. IN: arc4.stream-helpers
  5. MIXIN: input-stream
  6. M:: input-stream stream-read ( n stream -- seq )
  7. BV{ } clone :> bytes n iota [ drop stream
  8. stream-read1 dup [ bytes push ] when* not ] find 2drop bytes >byte-array ;
  9. : separator-or-eof? ( val seps -- ? )
  10. [ member? ] curry [ f = ] bi or ;
  11. M:: input-stream stream-read-until ( seps stream -- seq sep/f )
  12. BV{ } clone :> bytes f :> sep!
  13. [ stream stream-read1
  14. [ dup ] [ sep! seps separator-or-eof? ] bi ]
  15. [ bytes push ] until drop bytes >byte-array sep ;
  16. M: input-stream stream-read-partial ( n stream -- seq )
  17. stream-read ;