1 ! Copyright (C) 2003, 2009 Slava Pestov, Daniel Ehrenberg.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors destructors io io.encodings io.streams.sequence
4 kernel math sbufs sequences sequences.private strings ;
8 TUPLE: string-reader { underlying string read-only } { i array-capacity } ;
9 INSTANCE: string-reader input-stream
11 M: string-reader stream-element-type drop +character+ ; inline
13 M: string-reader stream-read-unsafe sequence-read-unsafe ;
14 M: string-reader stream-read1 sequence-read1 ;
15 M: string-reader stream-read-until sequence-read-until ;
16 M: string-reader stream-readln
17 dup >sequence-stream< bounds-check? [
18 "\r\n" over sequence-read-until CHAR: \r eq? [
19 over >sequence-stream< dupd ?nth CHAR: \n eq?
20 [ 1 + pick i<< ] [ drop ] if
24 M: string-reader stream-tell i>> ;
25 M: string-reader stream-seek sequence-seek ;
26 M: string-reader stream-seekable? drop t ; inline
27 M: string-reader stream-length underlying>> length ;
28 M: string-reader dispose drop ;
30 : <string-reader> ( str -- stream )
33 : with-string-reader ( str quot -- )
34 [ <string-reader> ] dip with-input-stream ; inline
37 M: sbuf stream-element-type drop +character+ ; inline
38 M: sbuf stream-tell length ; inline
40 : <string-writer> ( -- stream )
43 : with-string-writer ( quot -- str )
45 swap with-output-stream*
46 ] keep >string ; inline