]> gitweb.factorcode.org Git - factor.git/blob - core/io/streams/sequence/sequence.factor
io.encodings.utf8 fixed for bootstrap; add unit tests
[factor.git] / core / io / streams / sequence / sequence.factor
1 ! Copyright (C) 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: sequences io kernel accessors math math.order ;
4 IN: io.streams.sequence
5
6 SLOT: underlying
7 SLOT: i
8
9 : >sequence-stream< ( stream -- i underlying )
10     [ i>> ] [ underlying>> ] bi ; inline
11
12 : next ( stream -- )
13     [ 1+ ] change-i drop ;
14
15 : sequence-read1 ( stream -- elt/f )
16     [ >sequence-stream< ?nth ]
17     [ next ] bi ; inline
18
19 : add-length ( n stream -- i+n )
20     [ i>> + ] [ underlying>> length ] bi min  ;
21
22 : (sequence-read) ( n stream -- seq/f )
23     [ add-length ] keep
24     [ [ swap dup ] change-i drop ]
25     [ underlying>> ] bi
26     subseq ; inline
27
28 : sequence-read ( n stream -- seq/f )
29     dup >sequence-stream< bounds-check?
30     [ (sequence-read) ] [ 2drop f ] if ; inline
31
32 : find-sep ( seps stream -- sep/f n )
33     swap [ >sequence-stream< ] dip
34     [ memq? ] curry find-from swap ; inline
35
36 : sequence-read-until ( separators stream -- seq sep/f )
37     [ find-sep ] keep
38     [ sequence-read ] [ next ] bi swap ; inline