io.streams.string: faster M\ string-reader stream-readln.

db4
John Benediktsson 2014-02-24 21:47:49 -08:00
parent dbfa18e8fc
commit 0b04d3f279
2 changed files with 28 additions and 13 deletions

View File

@ -11,6 +11,14 @@ IN: io.streams.string.tests
]
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 ]
[ "" <string-reader> stream-readln ]
unit-test

View File

@ -1,9 +1,7 @@
! Copyright (C) 2003, 2009 Slava Pestov, Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors io kernel math namespaces sequences sbufs
strings generic splitting continuations destructors sequences.private
io.streams.plain io.encodings math.order growable io.streams.sequence
io.private ;
USING: accessors destructors io io.encodings io.streams.sequence
kernel math sbufs sequences sequences.private strings ;
IN: io.streams.string
! Readers
@ -11,28 +9,37 @@ TUPLE: string-reader { underlying string read-only } { i array-capacity } ;
INSTANCE: string-reader input-stream
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-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-seek (stream-seek) ;
M: string-reader stream-seekable? drop t ; inline
M: string-reader stream-length underlying>> length ;
M: string-reader dispose drop ;
<PRIVATE
SINGLETON: null-encoding
M: null-encoding decode-char drop stream-read1 ;
PRIVATE>
: <string-reader> ( str -- stream )
0 string-reader boa null-encoding <decoder> ;
0 string-reader boa ;
: with-string-reader ( str quot -- )
[ <string-reader> ] dip with-input-stream ; inline
! Writers
M: sbuf stream-element-type drop +character+ ;
M: sbuf stream-element-type drop +character+ ; inline
: <string-writer> ( -- stream )
512 <sbuf> ; inline