Redoing string streams and byte-array streams without copying

db4
Daniel Ehrenberg 2009-02-27 00:53:05 -06:00
parent 63c9b1a6b8
commit 2c462745f1
6 changed files with 68 additions and 43 deletions

View File

@ -96,8 +96,6 @@ M: object specializer-declaration class ;
{ string string } { string string }
"specializer" set-word-prop "specializer" set-word-prop
\ find-last-sep { string sbuf } "specializer" set-word-prop
\ >string { sbuf } "specializer" set-word-prop \ >string { sbuf } "specializer" set-word-prop
\ >array { { vector } } "specializer" set-word-prop \ >array { { vector } } "specializer" set-word-prop

View File

@ -1,5 +1,8 @@
! Copyright (C) 2008, 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
USING: byte-arrays byte-vectors kernel io.encodings io.streams.string USING: byte-arrays byte-vectors kernel io.encodings io.streams.string
sequences io namespaces io.encodings.private accessors ; sequences io namespaces io.encodings.private accessors sequences.private
io.streams.sequence destructors ;
IN: io.streams.byte-array IN: io.streams.byte-array
: <byte-writer> ( encoding -- stream ) : <byte-writer> ( encoding -- stream )
@ -9,8 +12,16 @@ IN: io.streams.byte-array
[ <byte-writer> ] dip [ output-stream get ] compose with-output-stream* [ <byte-writer> ] dip [ output-stream get ] compose with-output-stream*
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 } ;
M: byte-reader stream-read-partial stream-read ;
M: byte-reader stream-read sequence-read ;
M: byte-reader stream-read1 sequence-read1 ;
M: byte-reader stream-read-until sequence-read-until ;
M: byte-reader dispose drop ;
: <byte-reader> ( byte-array encoding -- stream ) : <byte-reader> ( byte-array encoding -- stream )
[ >byte-vector dup reverse-here ] dip <decoder> ; [ B{ } like 0 byte-reader boa ] dip <decoder> ;
: with-byte-reader ( byte-array encoding quot -- ) : with-byte-reader ( byte-array encoding quot -- )
[ <byte-reader> ] dip with-input-stream* ; inline [ <byte-reader> ] dip with-input-stream* ; inline

View File

@ -1,7 +1,7 @@
! Copyright (C) 2006 Doug Coleman ! Copyright (C) 2006 Doug Coleman
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel math sequences sequences.private namespaces USING: kernel math sequences sequences.private namespaces
words io io.binary io.files io.streams.string quotations words io io.binary io.files quotations
definitions checksums ; definitions checksums ;
IN: checksums.crc32 IN: checksums.crc32

View File

@ -0,0 +1,38 @@
! Copyright (C) 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
USING: sequences io kernel accessors math math.order ;
IN: io.streams.sequence
SLOT: underlying
SLOT: i
: >sequence-stream< ( stream -- i underlying )
[ i>> ] [ underlying>> ] bi ; inline
: next ( stream -- )
[ 1+ ] change-i drop ;
: sequence-read1 ( stream -- elt/f )
[ >sequence-stream< ?nth ]
[ next ] bi ; inline
: add-length ( n stream -- i+n )
[ i>> + ] [ underlying>> length ] bi min ;
: (sequence-read) ( n stream -- seq/f )
[ add-length ] keep
[ [ swap dup ] change-i drop ]
[ underlying>> ] bi
subseq ; inline
: sequence-read ( n stream -- seq/f )
dup >sequence-stream< bounds-check?
[ (sequence-read) ] [ 2drop f ] if ; inline
: find-sep ( seps stream -- sep/f n )
swap [ >sequence-stream< ] dip
[ memq? ] curry find-from swap ; inline
: sequence-read-until ( separators stream -- seq sep/f )
[ find-sep ] keep
[ sequence-read ] [ next ] bi swap ; inline

View File

@ -15,12 +15,12 @@ unit-test
[ "xyzzy" ] [ [ "xyzzy" write ] with-string-writer ] unit-test [ "xyzzy" ] [ [ "xyzzy" write ] with-string-writer ] unit-test
[ "a" ] [ 1 SBUF" cba" stream-read ] unit-test [ "a" ] [ 1 "abc" <string-reader> stream-read ] unit-test
[ "ab" ] [ 2 SBUF" cba" stream-read ] unit-test [ "ab" ] [ 2 "abc" <string-reader> stream-read ] unit-test
[ "abc" ] [ 3 SBUF" cba" stream-read ] unit-test [ "abc" ] [ 3 "abc" <string-reader> stream-read ] unit-test
[ "abc" ] [ 4 SBUF" cba" stream-read ] unit-test [ "abc" ] [ 4 "abc" <string-reader> stream-read ] unit-test
[ "abc" f ] [ [ "abc" f ] [
3 SBUF" cba" [ stream-read ] keep stream-read1 3 "abc" <string-reader> [ stream-read ] keep stream-read1
] unit-test ] unit-test
[ [

View File

@ -1,18 +1,12 @@
! Copyright (C) 2003, 2009 Slava Pestov. ! Copyright (C) 2003, 2009 Slava Pestov, Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors io kernel math namespaces sequences sbufs USING: accessors io kernel math namespaces sequences sbufs
strings generic splitting continuations destructors strings generic splitting continuations destructors sequences.private
io.streams.plain io.encodings math.order growable ; io.streams.plain io.encodings math.order growable io.streams.sequence ;
IN: io.streams.string IN: io.streams.string
<PRIVATE <PRIVATE
: harden-as ( seq growble-exemplar -- newseq )
underlying>> like ;
: growable-read-until ( growable n -- str )
>fixnum dupd tail-slice swap harden-as dup reverse-here ;
SINGLETON: null-encoding SINGLETON: null-encoding
M: null-encoding decode-char drop stream-read1 ; M: null-encoding decode-char drop stream-read1 ;
@ -32,34 +26,18 @@ M: growable stream-flush drop ;
<string-writer> swap [ output-stream get ] compose with-output-stream* <string-writer> swap [ output-stream get ] compose with-output-stream*
>string ; inline >string ; inline
M: growable stream-read1 [ f ] [ pop ] if-empty ; ! New implementation
: find-last-sep ( seq seps -- n ) TUPLE: string-reader { underlying string read-only } { i array-capacity } ;
swap [ memq? ] curry find-last drop ;
M: growable stream-read-until M: string-reader stream-read-partial stream-read ;
[ find-last-sep ] keep over [ M: string-reader stream-read sequence-read ;
[ swap 1+ growable-read-until ] 2keep [ nth ] 2keep M: string-reader stream-read1 sequence-read1 ;
set-length M: string-reader stream-read-until sequence-read-until ;
] [ M: string-reader dispose drop ;
[ swap drop 0 growable-read-until f like f ] keep
delete-all
] if ;
M: growable stream-read
[
drop f
] [
[ length swap - 0 max ] keep
[ swap growable-read-until ] 2keep
set-length
] if-empty ;
M: growable stream-read-partial
stream-read ;
: <string-reader> ( str -- stream ) : <string-reader> ( str -- stream )
>sbuf dup reverse-here null-encoding <decoder> ; 0 string-reader boa null-encoding <decoder> ;
: with-string-reader ( str quot -- ) : with-string-reader ( str quot -- )
[ <string-reader> ] dip with-input-stream ; inline [ <string-reader> ] dip with-input-stream ; inline