diff --git a/basis/io/streams/string/string-tests.factor b/basis/io/streams/string/string-tests.factor index fe01626dec..bf20c7452e 100644 --- a/basis/io/streams/string/string-tests.factor +++ b/basis/io/streams/string/string-tests.factor @@ -11,6 +11,14 @@ IN: io.streams.string.tests ] unit-test +{ { "line 1" "line 2" "line 3" } } [ + "line 1\nline 2\nline 3" stream-lines +] unit-test + +{ { "" "foo" "bar" "baz" } } [ + "\rfoo\r\nbar\rbaz\n" stream-lines +] unit-test + [ f ] [ "" stream-readln ] unit-test diff --git a/basis/io/streams/string/string.factor b/basis/io/streams/string/string.factor index f27e308326..7e6042756f 100644 --- a/basis/io/streams/string/string.factor +++ b/basis/io/streams/string/string.factor @@ -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 ; - - : ( str -- stream ) - 0 string-reader boa null-encoding ; + 0 string-reader boa ; : with-string-reader ( str quot -- ) [ ] dip with-input-stream ; inline ! Writers -M: sbuf stream-element-type drop +character+ ; +M: sbuf stream-element-type drop +character+ ; inline : ( -- stream ) 512 ; inline