2009-02-27 01:53:05 -05:00
|
|
|
! Copyright (C) 2003, 2009 Slava Pestov, Daniel Ehrenberg.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2014-02-25 00:47:49 -05:00
|
|
|
USING: accessors destructors io io.encodings io.streams.sequence
|
|
|
|
kernel math sbufs sequences sequences.private strings ;
|
2008-03-14 04:09:51 -04:00
|
|
|
IN: io.streams.string
|
2008-01-31 01:52:06 -05:00
|
|
|
|
2009-03-15 18:11:18 -04:00
|
|
|
! Readers
|
2009-02-27 01:53:05 -05:00
|
|
|
TUPLE: string-reader { underlying string read-only } { i array-capacity } ;
|
2011-10-18 19:24:50 -04:00
|
|
|
INSTANCE: string-reader input-stream
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2013-12-02 20:44:03 -05:00
|
|
|
M: string-reader stream-element-type drop +character+ ; inline
|
2014-02-25 00:47:49 -05:00
|
|
|
|
2014-02-25 12:32:47 -05:00
|
|
|
M: string-reader stream-read-unsafe sequence-read-unsafe ;
|
2009-02-27 01:53:05 -05:00
|
|
|
M: string-reader stream-read1 sequence-read1 ;
|
2014-02-25 10:52:07 -05:00
|
|
|
M: string-reader stream-read-until sequence-read-until ;
|
2014-02-25 00:47:49 -05:00
|
|
|
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 ;
|
|
|
|
|
2009-10-03 19:27:09 -04:00
|
|
|
M: string-reader stream-tell i>> ;
|
2014-02-25 10:56:28 -05:00
|
|
|
M: string-reader stream-seek sequence-seek ;
|
2011-10-15 00:35:25 -04:00
|
|
|
M: string-reader stream-seekable? drop t ; inline
|
|
|
|
M: string-reader stream-length underlying>> length ;
|
2009-02-27 01:53:05 -05:00
|
|
|
M: string-reader dispose drop ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: <string-reader> ( str -- stream )
|
2014-02-25 00:47:49 -05:00
|
|
|
0 string-reader boa ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-02-15 23:20:31 -05:00
|
|
|
: with-string-reader ( str quot -- )
|
2008-11-23 03:44:56 -05:00
|
|
|
[ <string-reader> ] dip with-input-stream ; inline
|
2008-02-21 16:22:49 -05:00
|
|
|
|
2009-03-15 18:11:18 -04:00
|
|
|
! Writers
|
2014-02-25 00:47:49 -05:00
|
|
|
M: sbuf stream-element-type drop +character+ ; inline
|
2009-03-15 18:11:18 -04:00
|
|
|
|
|
|
|
: <string-writer> ( -- stream )
|
2014-02-10 23:46:25 -05:00
|
|
|
512 <sbuf> ; inline
|
2009-03-15 18:11:18 -04:00
|
|
|
|
|
|
|
: with-string-writer ( quot -- str )
|
2009-04-20 21:05:17 -04:00
|
|
|
<string-writer> [
|
|
|
|
swap with-output-stream*
|
2009-10-03 19:27:09 -04:00
|
|
|
] keep >string ; inline
|