diff --git a/basis/io/streams/string/string.factor b/basis/io/streams/string/string.factor index be9016d1f2..3a3815471a 100644 --- a/basis/io/streams/string/string.factor +++ b/basis/io/streams/string/string.factor @@ -8,10 +8,11 @@ IN: io.streams.string ! Readers 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-read-partial stream-read ; -M: string-reader stream-read sequence-read ; +M: string-reader stream-read-partial-unsafe sequence-read-unsafe ; +M: string-reader stream-read-unsafe sequence-read-unsafe ; M: string-reader stream-read1 sequence-read1 ; M: string-reader stream-read-until sequence-read-until ; M: string-reader stream-tell i>> ; diff --git a/core/io/streams/byte-array/byte-array.factor b/core/io/streams/byte-array/byte-array.factor index 6f9b05cf18..b917086206 100644 --- a/core/io/streams/byte-array/byte-array.factor +++ b/core/io/streams/byte-array/byte-array.factor @@ -15,11 +15,12 @@ M: byte-vector stream-element-type drop +byte+ ; dup encoder? [ stream>> ] when >byte-array ; inline 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-read-partial stream-read ; -M: byte-reader stream-read sequence-read ; +M: byte-reader stream-read-partial-unsafe sequence-read-unsafe ; +M: byte-reader stream-read-unsafe sequence-read-unsafe ; M: byte-reader stream-read1 sequence-read1 ; M: byte-reader stream-read-until sequence-read-until ; M: byte-reader dispose drop ; diff --git a/core/io/streams/sequence/sequence.factor b/core/io/streams/sequence/sequence.factor index 22882d6a24..47762628fe 100644 --- a/core/io/streams/sequence/sequence.factor +++ b/core/io/streams/sequence/sequence.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2009 Daniel Ehrenberg ! See http://factorcode.org/license.txt for BSD license. USING: sequences io io.streams.plain kernel accessors math math.order -growable destructors combinators ; +growable destructors combinators sequences.private ; IN: io.streams.sequence ! Readers @@ -17,26 +17,29 @@ SLOT: i : sequence-read1 ( stream -- elt/f ) [ >sequence-stream< ?nth ] [ next ] bi ; inline -: add-length ( n stream -- i+n ) - [ i>> + ] [ underlying>> length ] bi min ; inline +: (sequence-read-length) ( n buf stream -- buf count ) + [ underlying>> length ] [ i>> ] bi - rot min ; inline -: (sequence-read) ( n stream -- seq/f ) - [ add-length ] keep - [ [ swap dup ] change-i drop ] - [ underlying>> ] bi - subseq ; inline +: ( dest n i src -- n copy ) + [ 0 ] 3curry dip ; inline -: sequence-read ( n stream -- seq/f ) +: (sequence-read) ( n buf stream -- count ) + [ (sequence-read-length) ] + [ [ dup pick + ] change-i underlying>> ] bi + [ (copy) drop ] 2curry keep ; inline + +: sequence-read-unsafe ( n buf stream -- count ) dup >sequence-stream< bounds-check? - [ (sequence-read) ] [ 2drop f ] if ; inline + [ (sequence-read) ] [ 3drop 0 ] if ; inline : find-sep ( seps stream -- sep/f n ) 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 ) [ find-sep ] keep - [ sequence-read ] [ next ] bi swap ; inline + [ [ sequence-read-unsafe ] (read-into-new) ] [ next ] bi swap ; inline ! Writers M: growable dispose drop ;