io.streams.sequence: change to provide read-unsafe

Change io.streams.sequence and its children io.streams.byte-array and io.streams.string to implement the -unsafe protocol and mixin noncopying-reader.
db4
Joe Groff 2011-10-11 16:00:22 -07:00
parent 66bb07bbcf
commit d5f4b6f155
3 changed files with 21 additions and 16 deletions

View File

@ -8,10 +8,11 @@ IN: io.streams.string
! Readers ! Readers
TUPLE: string-reader { underlying string read-only } { i array-capacity } ; TUPLE: string-reader { underlying string read-only } { i array-capacity } ;
INSTANCE: string-reader noncopying-reader
M: string-reader stream-element-type drop +character+ ; M: string-reader stream-element-type drop +character+ ;
M: string-reader stream-read-partial stream-read ; M: string-reader stream-read-partial-unsafe sequence-read-unsafe ;
M: string-reader stream-read sequence-read ; M: string-reader stream-read-unsafe sequence-read-unsafe ;
M: string-reader stream-read1 sequence-read1 ; M: string-reader stream-read1 sequence-read1 ;
M: string-reader stream-read-until sequence-read-until ; M: string-reader stream-read-until sequence-read-until ;
M: string-reader stream-tell i>> ; M: string-reader stream-tell i>> ;

View File

@ -15,11 +15,12 @@ M: byte-vector stream-element-type drop +byte+ ;
dup encoder? [ stream>> ] when >byte-array ; inline dup encoder? [ stream>> ] when >byte-array ; inline
TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ; TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ;
INSTANCE: byte-reader noncopying-reader
M: byte-reader stream-element-type drop +byte+ ; M: byte-reader stream-element-type drop +byte+ ;
M: byte-reader stream-read-partial stream-read ; M: byte-reader stream-read-partial-unsafe sequence-read-unsafe ;
M: byte-reader stream-read sequence-read ; M: byte-reader stream-read-unsafe sequence-read-unsafe ;
M: byte-reader stream-read1 sequence-read1 ; M: byte-reader stream-read1 sequence-read1 ;
M: byte-reader stream-read-until sequence-read-until ; M: byte-reader stream-read-until sequence-read-until ;
M: byte-reader dispose drop ; M: byte-reader dispose drop ;

View File

@ -1,7 +1,7 @@
! Copyright (C) 2009 Daniel Ehrenberg ! Copyright (C) 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: sequences io io.streams.plain kernel accessors math math.order USING: sequences io io.streams.plain kernel accessors math math.order
growable destructors combinators ; growable destructors combinators sequences.private ;
IN: io.streams.sequence IN: io.streams.sequence
! Readers ! Readers
@ -17,26 +17,29 @@ SLOT: i
: sequence-read1 ( stream -- elt/f ) : sequence-read1 ( stream -- elt/f )
[ >sequence-stream< ?nth ] [ next ] bi ; inline [ >sequence-stream< ?nth ] [ next ] bi ; inline
: add-length ( n stream -- i+n ) : (sequence-read-length) ( n buf stream -- buf count )
[ i>> + ] [ underlying>> length ] bi min ; inline [ underlying>> length ] [ i>> ] bi - rot min ; inline
: (sequence-read) ( n stream -- seq/f ) : <sequence-copy> ( dest n i src -- n copy )
[ add-length ] keep [ 0 ] 3curry dip <copy> ; inline
[ [ swap dup ] change-i drop ]
[ underlying>> ] bi
subseq ; inline
: sequence-read ( n stream -- seq/f ) : (sequence-read) ( n buf stream -- count )
[ (sequence-read-length) ]
[ [ dup pick + ] change-i underlying>> ] bi
[ <sequence-copy> (copy) drop ] 2curry keep ; inline
: sequence-read-unsafe ( n buf stream -- count )
dup >sequence-stream< bounds-check? dup >sequence-stream< bounds-check?
[ (sequence-read) ] [ 2drop f ] if ; inline [ (sequence-read) ] [ 3drop 0 ] if ; inline
: find-sep ( seps stream -- sep/f n ) : find-sep ( seps stream -- sep/f n )
swap [ >sequence-stream< swap tail-slice ] dip swap [ >sequence-stream< swap tail-slice ] dip
[ member-eq? ] curry find swap ; inline [ member-eq? ] curry [ find swap ] curry keep
over [ drop ] [ nip length ] if ; inline
: sequence-read-until ( separators stream -- seq sep/f ) : sequence-read-until ( separators stream -- seq sep/f )
[ find-sep ] keep [ find-sep ] keep
[ sequence-read ] [ next ] bi swap ; inline [ [ sequence-read-unsafe ] (read-into-new) ] [ next ] bi swap ; inline
! Writers ! Writers
M: growable dispose drop ; M: growable dispose drop ;