]> gitweb.factorcode.org Git - factor.git/blob - basis/io/streams/string/string.factor
mason: fix build duration during runs
[factor.git] / basis / io / streams / string / string.factor
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 ;
5 IN: io.streams.string
6
7 ! Readers
8 TUPLE: string-reader { underlying string read-only } { i array-capacity } ;
9 INSTANCE: string-reader input-stream
10
11 M: string-reader stream-element-type drop +character+ ; inline
12
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
21         ] when nip "" or
22     ] [ drop f ] if ;
23
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 ;
29
30 : <string-reader> ( str -- stream )
31     0 string-reader boa ;
32
33 : with-string-reader ( str quot -- )
34     [ <string-reader> ] dip with-input-stream ; inline
35
36 ! Writers
37 M: sbuf stream-element-type drop +character+ ; inline
38 M: sbuf stream-tell length ; inline
39
40 : <string-writer> ( -- stream )
41     512 <sbuf> ; inline
42
43 : with-string-writer ( quot -- str )
44     <string-writer> [
45         swap with-output-stream*
46     ] keep >string ; inline