io.streams.string: faster M\ string-reader stream-readln.
parent
dbfa18e8fc
commit
0b04d3f279
|
@ -11,6 +11,14 @@ IN: io.streams.string.tests
|
||||||
]
|
]
|
||||||
unit-test
|
unit-test
|
||||||
|
|
||||||
|
{ { "line 1" "line 2" "line 3" } } [
|
||||||
|
"line 1\nline 2\nline 3" <string-reader> stream-lines
|
||||||
|
] unit-test
|
||||||
|
|
||||||
|
{ { "" "foo" "bar" "baz" } } [
|
||||||
|
"\rfoo\r\nbar\rbaz\n" <string-reader> stream-lines
|
||||||
|
] unit-test
|
||||||
|
|
||||||
[ f ]
|
[ f ]
|
||||||
[ "" <string-reader> stream-readln ]
|
[ "" <string-reader> stream-readln ]
|
||||||
unit-test
|
unit-test
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
! Copyright (C) 2003, 2009 Slava Pestov, Daniel Ehrenberg.
|
! 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 destructors io io.encodings io.streams.sequence
|
||||||
strings generic splitting continuations destructors sequences.private
|
kernel math sbufs sequences sequences.private strings ;
|
||||||
io.streams.plain io.encodings math.order growable io.streams.sequence
|
|
||||||
io.private ;
|
|
||||||
IN: io.streams.string
|
IN: io.streams.string
|
||||||
|
|
||||||
! Readers
|
! Readers
|
||||||
|
@ -11,28 +9,37 @@ TUPLE: string-reader { underlying string read-only } { i array-capacity } ;
|
||||||
INSTANCE: string-reader input-stream
|
INSTANCE: string-reader input-stream
|
||||||
|
|
||||||
M: string-reader stream-element-type drop +character+ ; inline
|
M: string-reader stream-element-type drop +character+ ; inline
|
||||||
M: string-reader stream-read-unsafe sequence-read-unsafe ;
|
|
||||||
|
M: string-reader stream-read-unsafe
|
||||||
|
[ integer>fixnum ]
|
||||||
|
[ dup string? [ "not a string" throw ] unless ]
|
||||||
|
[ sequence-read-unsafe ] tri* ;
|
||||||
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
|
||||||
|
dup >sequence-stream< bounds-check?
|
||||||
|
[ sequence-read-until ] [ 2drop f f ] if ;
|
||||||
|
M: string-reader stream-readln
|
||||||
|
dup >sequence-stream< bounds-check? [
|
||||||
|
"\r\n" over sequence-read-until CHAR: \r eq? [
|
||||||
|
over >sequence-stream< dupd ?nth CHAR: \n eq?
|
||||||
|
[ 1 + pick i<< ] [ drop ] if
|
||||||
|
] when nip "" or
|
||||||
|
] [ drop f ] if ;
|
||||||
|
|
||||||
M: string-reader stream-tell i>> ;
|
M: string-reader stream-tell i>> ;
|
||||||
M: string-reader stream-seek (stream-seek) ;
|
M: string-reader stream-seek (stream-seek) ;
|
||||||
M: string-reader stream-seekable? drop t ; inline
|
M: string-reader stream-seekable? drop t ; inline
|
||||||
M: string-reader stream-length underlying>> length ;
|
M: string-reader stream-length underlying>> length ;
|
||||||
M: string-reader dispose drop ;
|
M: string-reader dispose drop ;
|
||||||
|
|
||||||
<PRIVATE
|
|
||||||
SINGLETON: null-encoding
|
|
||||||
M: null-encoding decode-char drop stream-read1 ;
|
|
||||||
PRIVATE>
|
|
||||||
|
|
||||||
: <string-reader> ( str -- stream )
|
: <string-reader> ( str -- stream )
|
||||||
0 string-reader boa null-encoding <decoder> ;
|
0 string-reader boa ;
|
||||||
|
|
||||||
: with-string-reader ( str quot -- )
|
: with-string-reader ( str quot -- )
|
||||||
[ <string-reader> ] dip with-input-stream ; inline
|
[ <string-reader> ] dip with-input-stream ; inline
|
||||||
|
|
||||||
! Writers
|
! Writers
|
||||||
M: sbuf stream-element-type drop +character+ ;
|
M: sbuf stream-element-type drop +character+ ; inline
|
||||||
|
|
||||||
: <string-writer> ( -- stream )
|
: <string-writer> ( -- stream )
|
||||||
512 <sbuf> ; inline
|
512 <sbuf> ; inline
|
||||||
|
|
Loading…
Reference in New Issue