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.
|
2008-06-28 03:36:20 -04:00
|
|
|
USING: accessors io kernel math namespaces sequences sbufs
|
2009-02-27 01:53:05 -05:00
|
|
|
strings generic splitting continuations destructors sequences.private
|
2009-10-03 19:27:09 -04:00
|
|
|
io.streams.plain io.encodings math.order growable io.streams.sequence
|
|
|
|
io.private ;
|
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
|
2011-10-11 19:00:22 -04: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 ;
|
|
|
|
M: string-reader stream-read-until sequence-read-until ;
|
2009-10-03 19:27:09 -04:00
|
|
|
M: string-reader stream-tell i>> ;
|
|
|
|
M: string-reader stream-seek (stream-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
|
|
|
|
2009-03-15 18:11:18 -04:00
|
|
|
<PRIVATE
|
|
|
|
SINGLETON: null-encoding
|
|
|
|
M: null-encoding decode-char drop stream-read1 ;
|
|
|
|
PRIVATE>
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: <string-reader> ( str -- stream )
|
2009-02-27 01:53:05 -05:00
|
|
|
0 string-reader boa null-encoding <decoder> ;
|
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
|
|
|
|
M: sbuf stream-element-type drop +character+ ;
|
|
|
|
|
|
|
|
: <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
|